Skip to content

Instantly share code, notes, and snippets.

@robksawyer
Last active December 22, 2015 09:29
Show Gist options
  • Save robksawyer/964415231b52f9183501 to your computer and use it in GitHub Desktop.
Save robksawyer/964415231b52f9183501 to your computer and use it in GitHub Desktop.
The applicable model code.
<?php
App::uses('AppModel', 'Model');
/**
* Cheese Model
*
* @property CheeseProducer $CheeseProducer
* @property NutritionFact $NutritionFact
* @property Attachment $Attachment
* @property MilkSource $MilkSource
*/
class Cheese extends AppModel {
/**
* Display field
*
* @var string
*/
public $displayField = 'name';
public $actsAs = array(
'Containable','ExtendAssociations2','Search.Searchable'
);
//public $findMethods = array('active' => true,'inactive' => true);
public $searchConditions = array();
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'name' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'You must enter the cheese\'s name.',
'allowEmpty' => false,
'required' => true,
//'last' => false, // Stop validation after this rule
'on' => 'create' // Limit validation to 'create' or 'update' operations
)
),
'rind_id' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'You must add the rind type.',
'allowEmpty' => true,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create' // Limit validation to 'create' or 'update' operations
)
),
'age_classification' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'You must add the cheese\'s maturity state.',
'allowEmpty' => false,
'required' => true,
//'last' => false, // Stop validation after this rule
'on' => 'create' // Limit validation to 'create' or 'update' operations
)
),
'texture_id' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'You must select the cheese texture.',
'allowEmpty' => true,
//'required' => false,
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create' // Limit validation to 'create' or 'update' operations
)
)
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
...
);
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
...
);
/**
* hasAndBelongsToMany associations
*
* @var array
*/
public $hasAndBelongsToMany = array(
...
);
/**
* SEARCH FILTER ARGS
* https://github.com/dereuromark/search
*/
public $filterArgs = array(
'search' => array(
'name' => 'query',
'type' => 'query',
'encode' => false,
'method' => 'orConditions'
),
'searchProducer' => array(
'name' => 'query',
'type' => 'query',
'encode' => false,
'method' => 'searchProducer',
),
'searchOverview' => array(
'name' => 'query',
'type' => 'query',
'encode' => false,
'method' => 'searchOverview'
),
);
/**
* orConditions method
* @param array $data
* @param bool $activeOnly Whether or not to search active records only
* @return array The search conditions
*/
public function orConditions($data = array(), $activeOnly = false) {
if(!empty($data['query'])){
$filter = $data['query'];
}else if(!empty($data[0])){
$filter = $data[0];
}
$filterWNoApostrophe = preg_replace("/'/",'',$data['query']);
$filterWNoS = preg_replace("/s+$/",'',$data['query']);
$filterWNoApostropheS = preg_replace("/'s+$/",'',$data['query']);
$cond['OR'][] = array($this->alias . '.id' => $filter);
$cond['OR'][] = array($this->alias . '.name LIKE' => '%'.$filter . '%');
$cond['OR'][] = array($this->alias . '.bacteria_other LIKE' => '%' . $filter . '%');
$cond['OR'][] = array($this->alias . '.fungi_other LIKE' => '%' . $filter . '%');
$cond['OR'][] = array($this->alias . '.rind_type_other LIKE' => '%' . $filter . '%');
$cond['OR'][] = array($this->alias . '.shape LIKE' => '%' . $filter . '%');
//$cond['OR'][] = array('CheeseProducer.name LIKE' => '%' . $filter . '%');
//$cond['OR'][] = array('Material.name LIKE' => '%' . $filter . '%');
//$cond['OR'][] = array('Color.name LIKE' => '%' . $filter . '%');
//$cond['OR'][] = array('Color.hex LIKE' => '%' . $filter . '%');
if($activeOnly === true) {
$cond['AND'][] = array(
$this->alias . '.active' => 1
);
}
if($filter != $filterWNoApostrophe){
$cond['OR'][] = array($this->alias . '.name LIKE' => '%' . $filterWNoApostrophe . '%');
}
if($filter != $filterWNoApostropheS){
$cond['OR'][] = array($this->alias . '.name LIKE' => '%' . $filterWNoApostropheS . '%');
}
if($filter != $filterWNoS){
$cond['OR'][] = array($this->alias . '.name LIKE' => '%' . $filterWNoS . '%');
}
$this->searchConditions = Set::merge($this->searchConditions, $cond);
return $this->searchConditions;
}
/**
* searchOverview method
* @param array $data
*/
public function searchOverview($data = array()) {
if(strlen($data['query']) > 10){
$filter = $data['query'];
$cond = array();
$cond['OR'] = array();
$cond['OR'][strval($this->alias . '.info_overview LIKE')] = '%' . $filter . '%';
$this->searchConditions = Set::merge($this->searchConditions, $cond);
/*$cond['OR'][count($this->searchConditions['OR'])] = array(
$this->alias . '.info_overview LIKE' => '%' . $filter . '%'
);*/
$this->searchConditions = Set::merge($this->searchConditions, $cond);
return $this->searchConditions;
}
}
/**
* searchProducer method
* @param array $data
*/
public function searchProducer($data = array()) {
if(!empty($data['query'])){
$filter = $data['query'];
$items = $this->CheeseProducer->find('list', array(
'conditions' => array(
'CheeseProducer.name LIKE' => $filter.'%'
),
'fields' => array('id')
));
if(!empty($items)){
$cond['OR'][count($this->searchConditions['OR'])] = array(
'Cheese.cheese_producer_id' => $items
);
$this->searchConditions = Set::merge($this->searchConditions, $cond);
return $this->searchConditions;
}
}
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment