Skip to content

Instantly share code, notes, and snippets.

@Tmw
Created November 17, 2011 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tmw/1372756 to your computer and use it in GitHub Desktop.
Save Tmw/1372756 to your computer and use it in GitHub Desktop.
Overview of our companies controller
<?php
class CompaniesController extends AppController {
function index(){
$companies = $this->Company->find('all', array('limit' => 10));
$this->set('companies', $companies);
$this->loadModel('Category');
$categories = $this->Category->find('all');
$this->set('categories', $categories);
}
function paginate_js($id){
$this->layout = 'ajax';
$this->header('Content-Type: text/javascript');
$this->set('companies', $this->Company->find('all', array('limit' => ($id*10).' ,10')));
}
function search(){
//initialize variables!
$nrOfRows = 0;
$shoutWordArray = array("Helaas", "Jammer");
$shoutWord = $shoutWordArray[array_rand($shoutWordArray)];
//when client used the normal search!
if(isset($this->params['url']['normalSearchIsActive'])){
//retrieve text in inputfield!
$keywordsNormal = $this->params['url']['keywordsNormal'];
//when there is text in inputfield!
if($keywordsNormal != ""){
//declare condition: search for match with double-wildcard!
$queryConditions = array("OR" => array('Company.name LIKE' => '%' . $keywordsNormal . '%', 'Company.details LIKE' => '%' . $keywordsNormal . '%'));
//inject query -> count rows -> $set resultSet to be used in view!
$resultSet = $this->Company->find('all', array('conditions' => $queryConditions));
$nrOfRows = count($resultSet);
$this->set('resultSet', $resultSet);
}
//when inputfield is empty!
elseif($keywordsNormal == ""){
$nrOfRows = -1;
}
}
//when client used the extra search!
if(isset($this->data['Company']['extraSearchIsActive'])){
//declare some arrays to build up our query-conditions!
$queryWhereConditions = array();
$queryInnerJoinConditions = array();
//retrieve text from input/selectfields!
$keywordsExtra = $this->data['Company']['keywordsExtra'];
$postalCode = $this->data['Company']['postalCode'];
//when there is text in inputfield[keywordsExtra] -> declare condition: search for match with double-wildcard!
if($keywordsExtra != "")
$queryWhereConditions[] = array("OR" => array('Company.name LIKE' => '%' . $keywordsExtra . '%', 'Company.details LIKE' => '%' . $keywordsExtra . '%'));
if(isset($this->data['Company']['subcategoryId'])){
$subCategoryId = $this->data['Company']['subcategoryId'];
if($subCategoryId != ""){
$queryInnerJoinConditions[] = array('table' => 'cat_comps', 'alias' => 'CatComps', 'type' => 'inner', 'conditions' => array('CatComps.company_id = Company.id'));
$queryWhereConditions[] = array('CatComps.subcategory_id' => $subCategoryId);
}
}
//when there is text in inputfield[postalCode] -> declare condition: search for match with single-begin-wildcard!
if($postalCode != ""){
$queryInnerJoinConditions[] = array('table' => 'locations', 'alias' => 'Locations', 'type' => 'inner', 'conditions' => array('Locations.company_id = Company.id'));
$queryWhereConditions[] = array('Locations.postcode LIKE' => $postalCode . '%');
}
//inject query -> count rows -> set $resultSet to be used in view!
$resultSet = $this->Company->find('all', array('conditions' => $queryWhereConditions, 'joins' => $queryInnerJoinConditions));
$nrOfRows = count($resultSet);
$this->set('resultSet', $resultSet);
}
$this->set('nrOfRows', $nrOfRows);
$this->set('shoutWord', $shoutWord);
}
function searchExtra(){
//load model Category -> retrieve a handy list array[Category.id => Category.value] for our selectbox -> set $categories to be used in view!
$this->loadModel('Category');
$categories = $this->Category->find('list');
array_unshift($categories, "");
$this->set('categories', $categories);
}
function getValuesDynamicSelectBox(){
$this->autoRender = false;
$categoryId = $_GET["categoryId"];
$resultSet = $this->Company->query("SELECT id, name FROM subcategories WHERE category_id = ".$categoryId);
echo json_encode($resultSet);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment