Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class SearchController extends Controller {
- public function searchModels() {
- return array(
- 'Customer' => array('id' => 'id', 'name' => 'name', 'resultUrl' => Yii::app()->createAbsoluteUrl('crm/customer/view/id')),
- 'Contact' => array('id' => 'id', 'name' => array('last_name', 'first_name'), 'resultUrl' => Yii::app()->createAbsoluteUrl('crm/contact/view/id')),
- 'Opportunity' => array('id' => 'id', 'name' => 'name', 'resultUrl' => Yii::app()->createAbsoluteUrl('sfa/opportunity/view/id')),
- 'QuotationHeader' => array('id' => 'id', 'name' => 'description', 'resultUrl' => Yii::app()->createAbsoluteUrl('sfa/quotation/view/id')),
- 'Lead' => array('id'=>'id', 'name'=>array('last_name', 'first_name'), 'resultUrl'=> Yii::app()->createAbsoluteUrl('crm/lead/view/id'))
- );
- }
- public function count() {
- $models = $this->searchModels();
- $sql = "";
- $ctr = 0;
- foreach ($models as $key => $value) {
- $tableName = $key::model()->tableName();
- $sql = "SELECT COUNT(*) FROM $tableName";
- $ctr = $ctr + Yii::app()->db->createCommand($sql)->queryScalar();
- }
- return $ctr;
- }
- /**
- *
- * @param string $query string to be searched
- * @param bool $hsd hide-soft-delete
- * @param string $model filtering based on models, if blank searches all models
- */
- public function actionIndex($query = "", $hsd = true, $model = "") {
- $nameAttribute = 'name';
- $modelAttribute = 'model';
- $count = $this->count();
- $models = $this->searchModels();
- $sql = "";
- foreach ($models as $modelName => $value) {
- $tableName = $modelName::model()->tableName();
- $id = $value['id'];
- if (is_array($value['name'])) {
- $name = "";
- foreach($value['name'] as $nameval){
- $name .= "$tableName.$nameval, ";
- }
- $name = substr_replace($name, "", -(strlen(", ")));
- $name = "CONCAT($name)";
- } else {
- $name = $value['name'];
- }
- if (array_key_exists('resultUrl', $value)) {
- $resultUrl = $value['resultUrl'];
- } else {
- $resultUrl = "#";
- }
- $attributes = $modelName::model()->attributes;
- $condition = "WHERE";
- $condition .= "(";
- foreach ($attributes as $attribute => $value) {
- $condition .= " $tableName.$attribute LIKE '%$query%' OR";
- }
- //$condition .= " $modelAttribute LIKE '%$query%' OR";
- $condition = substr_replace($condition, "", -(strlen("OR")));
- $condition .= ")";
- if ($hsd && $modelName::model()->hasAttribute('deleted')) {
- $condition .= " AND deleted!=1";
- }
- $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 ";
- }
- $sql = substr_replace($sql, "", -(strlen("UNION ")));
- $dataProvider = new CSqlDataProvider($sql, array(
- //'totalItemCount' => $count,
- 'sort' => array(
- 'attributes' => array(
- 'id', $nameAttribute, $modelAttribute, 'username'
- ),
- ),
- 'pagination' => false,
- /* 'pagination' => array(
- 'pageSize' => 10,
- ), */
- ));
- $this->render('index', array('dataProvider' => $dataProvider));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement