Advertisement
fernandezekiel

search controller

Jan 23rd, 2013
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.95 KB | None | 0 0
  1. <?php
  2.  
  3. class SearchController extends Controller {
  4.  
  5.     public function searchModels() {
  6.         return array(
  7.             'Customer' => array('id' => 'id', 'name' => 'name', 'resultUrl' => Yii::app()->createAbsoluteUrl('crm/customer/view/id')),
  8.             'Contact' => array('id' => 'id', 'name' => array('last_name', 'first_name'), 'resultUrl' => Yii::app()->createAbsoluteUrl('crm/contact/view/id')),
  9.             'Opportunity' => array('id' => 'id', 'name' => 'name', 'resultUrl' => Yii::app()->createAbsoluteUrl('sfa/opportunity/view/id')),
  10.             'QuotationHeader' => array('id' => 'id', 'name' => 'description', 'resultUrl' => Yii::app()->createAbsoluteUrl('sfa/quotation/view/id')),
  11.             'Lead' => array('id'=>'id', 'name'=>array('last_name', 'first_name'), 'resultUrl'=> Yii::app()->createAbsoluteUrl('crm/lead/view/id'))
  12.         );
  13.     }
  14.  
  15.     public function count() {
  16.         $models = $this->searchModels();
  17.         $sql = "";
  18.         $ctr = 0;
  19.         foreach ($models as $key => $value) {
  20.             $tableName = $key::model()->tableName();
  21.             $sql = "SELECT COUNT(*) FROM $tableName";
  22.             $ctr = $ctr + Yii::app()->db->createCommand($sql)->queryScalar();
  23.         }
  24.  
  25.         return $ctr;
  26.     }
  27.  
  28.     /**
  29.      *
  30.      * @param string $query string to be searched
  31.      * @param bool $hsd hide-soft-delete
  32.      * @param string $model filtering based on models, if blank searches all models
  33.      */
  34.     public function actionIndex($query = "", $hsd = true, $model = "") {
  35.         $nameAttribute = 'name';
  36.         $modelAttribute = 'model';
  37.         $count = $this->count();
  38.         $models = $this->searchModels();
  39.         $sql = "";
  40.  
  41.         foreach ($models as $modelName => $value) {
  42.             $tableName = $modelName::model()->tableName();
  43.             $id = $value['id'];
  44.             if (is_array($value['name'])) {
  45.                 $name = "";
  46.                 foreach($value['name'] as $nameval){
  47.                     $name .= "$tableName.$nameval, ";
  48.                 }
  49.                 $name = substr_replace($name, "", -(strlen(", ")));
  50.                 $name = "CONCAT($name)";
  51.             } else {
  52.                 $name = $value['name'];
  53.             }
  54.  
  55.             if (array_key_exists('resultUrl', $value)) {
  56.                 $resultUrl = $value['resultUrl'];
  57.             } else {
  58.                 $resultUrl = "#";
  59.             }
  60.            
  61.             $attributes = $modelName::model()->attributes;
  62.             $condition = "WHERE";
  63.             $condition .= "(";
  64.             foreach ($attributes as $attribute => $value) {
  65.                 $condition .= " $tableName.$attribute LIKE '%$query%' OR";
  66.             }
  67.             //$condition .= " $modelAttribute LIKE '%$query%' OR";
  68.             $condition = substr_replace($condition, "", -(strlen("OR")));
  69.             $condition .= ")";
  70.  
  71.             if ($hsd && $modelName::model()->hasAttribute('deleted')) {
  72.                 $condition .= " AND deleted!=1";
  73.             }
  74.             $sql .= "SELECT users.username as username, $tableName.$id, $name $nameAttribute, '$modelName' as $modelAttribute, CONCAT('$resultUrl','/',$tableName.id) as resultUrl FROM $tableName INNER JOIN users ON $tableName.owner_id = users.id INNER JOIN profiles ON users.id = profiles.user_id $condition UNION ";
  75.         }
  76.  
  77.         $sql = substr_replace($sql, "", -(strlen("UNION ")));
  78.  
  79.         $dataProvider = new CSqlDataProvider($sql, array(
  80.                     //'totalItemCount' => $count,
  81.                     'sort' => array(
  82.                         'attributes' => array(
  83.                             'id', $nameAttribute, $modelAttribute, 'username'
  84.                         ),
  85.                     ),
  86.                     'pagination' => false,
  87.                         /* 'pagination' => array(
  88.                           'pageSize' => 10,
  89.                           ), */
  90.                 ));
  91.         $this->render('index', array('dataProvider' => $dataProvider));
  92.     }
  93.  
  94.    
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement