public function actionWhatsNew() { if (!Yii::app()->user->isGuest) { $user = User::model()->findByPk(Yii::app()->user->getId()); $lastLogin = $user->lastLogin; $contacts = Contacts::model()->findAll("lastUpdated > {$lastLogin} ORDER BY lastUpdated DESC LIMIT 50"); $actions = Actions::model()->findAll("lastUpdated > {$lastLogin} AND (assignedTo='" . Yii::app()->user->getName() . "' OR assignedTo='Anyone') ORDER BY lastUpdated DESC LIMIT 50"); $sales = Sales::model()->findAll("lastUpdated > {$lastLogin} ORDER BY lastUpdated DESC LIMIT 50"); $accounts = Accounts::model()->findAll("lastUpdated > {$lastLogin} ORDER BY lastUpdated DESC LIMIT 50"); $arr = array_merge($contacts, $actions, $sales, $accounts); $records = Record::convert($arr); $dataProvider = new CArrayDataProvider($records, array('id' => 'id', 'pagination' => array('pageSize' => ProfileChild::getResultsPerPage()), 'sort' => array('attributes' => array('lastUpdated', 'name')))); $this->render('whatsNew', array('records' => $records, 'dataProvider' => $dataProvider)); } else { $this->redirect('login'); } }
public function actionSearch() { ini_set('memory_limit', -1); $term = $_GET['term']; if (substr($term, 0, 1) != "#") { /*$contactsCriteria = new CDbCriteria(); $contactsCriteria->compare('firstName', $term, true, 'OR'); $contactsCriteria->compare('lastName', $term, true, 'OR'); $contactsCriteria->compare('name', $term, true, 'OR'); $contactsCriteria->compare('backgroundInfo', $term, true, 'OR'); $contactsCriteria->compare('email', $term, true, 'OR'); $contactsCriteria->compare('phone', $term, true, 'OR'); if(is_numeric($term)){ $temp=$term; $first=substr($temp,0,3); $second=substr($temp,3,3); $third=substr($temp,6,4); $contactsCriteria->compare('phone', "($first) $second-$third", true, 'OR'); $contactsCriteria->compare('phone', "$first-$second-$third", true, 'OR'); $contactsCriteria->compare('phone', "$first $second $third", true, 'OR'); $contactsCriteria->compare('phone2', "($first) $second-$third", true, 'OR'); $contactsCriteria->compare('phone2', "$first-$second-$third", true, 'OR'); $contactsCriteria->compare('phone2', "$first $second $third", true, 'OR'); } $contacts=Contacts::model()->findAll($contactsCriteria); $actionsCriteria=new CDbCriteria(); $actionsCriteria->compare('actionDescription',$term,true,'OR'); $actions=Actions::model()->findAll($actionsCriteria); $accountsCriteria=new CDbCriteria(); $accountsCriteria->compare("name",$term,true,"OR"); $accountsCriteria->compare("description",$term,true,"OR"); $accountsCriteria->compare('tickerSymbol',$term,true,'OR'); $accounts=Accounts::model()->findAll($accountsCriteria); $quotesCriteria=new CDbCriteria(); $quotesCriteria->compare("name",$term,TRUE,"OR"); $quotes=Quote::model()->findAll($quotesCriteria); $disallow=array( 'contacts', 'actions', 'accounts', 'quotes', );*/ $modules = Modules::model()->findAllByAttributes(array('searchable' => 1)); $comparisons = array(); foreach ($modules as $module) { $module->name == 'products' ? $type = ucfirst('Product') : ($type = ucfirst($module->name)); $module->name == 'quotes' ? $type = ucfirst('Quote') : ($type = $type); $criteria = new CDbCriteria(); $fields = Fields::model()->findAllByAttributes(array('modelName' => $type, 'searchable' => 1)); $temp = array(); foreach ($fields as $field) { $temp[] = $field->id; $criteria->compare($field->fieldName, $term, true, "OR"); } $arr = CActiveRecord::model($type)->findAll($criteria); $comparisons[$type] = $temp; $other[$type] = $arr; } $high = array(); $medium = array(); $low = array(); $records = array(); $regEx = "/{$term}/i"; foreach ($other as $key => $recordType) { $fieldList = $comparisons[$key]; foreach ($recordType as $otherRecord) { foreach ($fieldList as $field) { $fieldRecord = Fields::model()->findByPk($field); $fieldName = $fieldRecord->fieldName; if (preg_match($regEx, $otherRecord->{$fieldName}) > 0) { switch ($fieldRecord->relevance) { case "High": if (!in_array($otherRecord, $high) && !in_array($otherRecord, $medium) && !in_array($otherRecord, $low)) { $high[] = $otherRecord; } break; case "Medium": if (!in_array($otherRecord, $high) && !in_array($otherRecord, $medium) && !in_array($otherRecord, $low)) { $medium[] = $otherRecord; } break; case "Low": if (!in_array($otherRecord, $high) && !in_array($otherRecord, $medium) && !in_array($otherRecord, $low)) { $low[] = $otherRecord; } break; default: $low[] = $otherRecord; } } } } } $records = array_merge($high, $medium); $records = array_merge($records, $low); $records = Record::convert($records, false); $dataProvider = new CArrayDataProvider($records, array('id' => 'id', 'pagination' => array('pageSize' => ProfileChild::getResultsPerPage()))); $this->render('search', array('records' => $records, 'dataProvider' => $dataProvider, 'term' => $term)); } else { $results = new CActiveDataProvider('Tags', array('criteria' => array('condition' => 'tag="' . $term . '"'))); $this->render('searchTags', array('tags' => $results)); } }
/** * Search X2Engine for a record. * * This is the action called by the search bar in the main menu. */ public function actionSearch() { ini_set('memory_limit', -1); $term = isset($_GET['term']) ? $_GET['term'] : ""; if (empty($term)) { $dataProvider = new CArrayDataProvider(array()); Yii::app()->user->setFlash('error', Yii::t('app', "Search term cannot be empty.")); $this->render('search', array('dataProvider' => $dataProvider)); } else { if (substr($term, 0, 1) != "#") { $modules = Modules::model()->findAllByAttributes(array('searchable' => 1)); $comparisons = array(); $other = array(); foreach ($modules as $module) { $module->name == 'products' ? $type = ucfirst('Product') : ($type = ucfirst($module->name)); $module->name == 'quotes' ? $type = ucfirst('Quote') : ($type = $type); $module->name == 'opportunities' ? $type = ucfirst('Opportunity') : ($type = $type); $criteria = new CDbCriteria(); $fields = Fields::model()->findAllByAttributes(array('modelName' => $type, 'searchable' => 1)); $temp = array(); $fieldNames = array(); if (count($fields) < 1) { $criteria->compare('id', '<0', true, 'AND'); } foreach ($fields as $field) { $temp[] = $field->id; $fieldNames[] = $field->fieldName; $criteria->compare($field->fieldName, $term, true, "OR"); if ($field->type == 'phone') { $tempPhone = preg_replace('/\\D/', '', $term); $phoneLookup = PhoneNumber::model()->findByAttributes(array('modelType' => $field->modelName, 'number' => $tempPhone, 'fieldName' => $field->fieldName)); if (isset($phoneLookup)) { $criteria->compare('id', $phoneLookup->modelId, true, "OR"); } } } if (Yii::app()->user->getName() != 'admin' && X2Model::model($type)->hasAttribute('visibility') && X2Model::model($type)->hasAttribute('assignedTo')) { $condition = 'visibility="1" OR (assignedTo="Anyone" AND visibility!="0") OR assignedTo="' . Yii::app()->user->getName() . '"'; /* x2temp */ $groupLinks = Yii::app()->db->createCommand()->select('groupId')->from('x2_group_to_user')->where('userId=' . Yii::app()->user->getId())->queryColumn(); if (!empty($groupLinks)) { $condition .= ' OR assignedTo IN (' . implode(',', $groupLinks) . ')'; } $condition .= 'OR (visibility=2 AND assignedTo IN (SELECT username FROM x2_group_to_user WHERE groupId IN (SELECT groupId FROM x2_group_to_user WHERE userId=' . Yii::app()->user->getId() . ')))'; $criteria->addCondition($condition); } if ($module->name == 'actions') { $criteria->with = array('actionText'); $criteria->compare('actionText.text', $term, true, "OR"); } if (class_exists($type)) { $arr = X2Model::model($type)->findAll($criteria); $comparisons[$type] = $temp; $other[$type] = $arr; } } $high = array(); $medium = array(); $low = array(); $userHigh = array(); $userMedium = array(); $userLow = array(); $records = array(); $userRecords = array(); $regEx = "/" . preg_quote($term, '/') . "/i"; foreach ($other as $key => $recordType) { $fieldList = $comparisons[$key]; foreach ($recordType as $otherRecord) { if ($key == 'Actions') { if ($otherRecord->hasAttribute('assignedTo') && $otherRecord->assignedTo == Yii::app()->user->getName()) { $userHigh[] = $otherRecord; } else { $high[] = $otherRecord; } } else { foreach ($fieldList as $field) { $fieldRecord = Fields::model()->findByPk($field); $fieldName = $fieldRecord->fieldName; if (preg_match($regEx, $otherRecord->{$fieldName}) > 0) { switch ($fieldRecord->relevance) { case "High": if (!in_array($otherRecord, $high, true) && !in_array($otherRecord, $medium, true) && !in_array($otherRecord, $low, true) && !in_array($otherRecord, $userHigh, true) && !in_array($otherRecord, $userMedium, true) && !in_array($otherRecord, $userLow, true)) { if ($otherRecord->hasAttribute('assignedTo') && $otherRecord->assignedTo == Yii::app()->user->getName()) { $userHigh[] = $otherRecord; } else { $high[] = $otherRecord; } } break; case "Medium": if (!in_array($otherRecord, $high, true) && !in_array($otherRecord, $medium, true) && !in_array($otherRecord, $low, true) && !in_array($otherRecord, $userHigh, true) && !in_array($otherRecord, $userMedium, true) && !in_array($otherRecord, $userLow, true)) { if ($otherRecord->hasAttribute('assignedTo') && $otherRecord->assignedTo == Yii::app()->user->getName()) { $userMedium[] = $otherRecord; } else { $medium[] = $otherRecord; } } break; case "Low": if (!in_array($otherRecord, $high, true) && !in_array($otherRecord, $medium, true) && !in_array($otherRecord, $low, true) && !in_array($otherRecord, $userHigh, true) && !in_array($otherRecord, $userMedium, true) && !in_array($otherRecord, $userLow, true)) { if ($otherRecord->hasAttribute('assignedTo') && $otherRecord->assignedTo == Yii::app()->user->getName()) { $userLow[] = $otherRecord; } else { $low[] = $otherRecord; } } break; default: if ($otherRecord->hasAttribute('assignedTo') && $otherRecord->assignedTo == Yii::app()->user->getName()) { $userLow[] = $otherRecord; } else { $low[] = $otherRecord; } } } elseif ($fieldRecord->type == 'phone') { $tempPhone = preg_replace('/\\D/', '', $term); if (strlen($tempPhone) == 10) { $phoneLookup = PhoneNumber::model()->findByAttributes(array('modelType' => $fieldRecord->modelName, 'number' => $tempPhone, 'fieldName' => $fieldName)); if (!in_array($otherRecord, $high, true) && !in_array($otherRecord, $medium, true) && !in_array($otherRecord, $low, true) && !in_array($otherRecord, $userHigh, true) && !in_array($otherRecord, $userMedium, true) && !in_array($otherRecord, $userLow, true)) { if (isset($phoneLookup) && $otherRecord->id == $phoneLookup->modelId) { if ($otherRecord->hasAttribute('assignedTo') && $otherRecord->assignedTo == Yii::app()->user->getName()) { $userHigh[] = $otherRecord; } else { $high[] = $otherRecord; } } } } } } } } } $records = array_merge($high, $medium); $records = array_merge($records, $low); $userRecords = array_merge($userHigh, $userMedium); $userRecords = array_merge($userRecords, $userLow); $records = array_merge($userRecords, $records); $records = Record::convert($records, false); if (count($records) == 1) { // Only one match, so go straight to it. // // The record's corresponding model class must have // X2LinkableBehavior for this to be possible. if (!empty($records[0]['#recordUrl'])) { $this->redirect($records[0]['#recordUrl']); } } $dataProvider = new CArrayDataProvider($records, array('id' => 'id', 'pagination' => array('pageSize' => Profile::getResultsPerPage()))); $this->render('search', array('records' => $records, 'dataProvider' => $dataProvider, 'term' => $term)); } else { Yii::app()->user->setState('vcr-list', $term); $_COOKIE['vcr-list'] = $term; $tagQuery = "\n SELECT * \n FROM x2_tags\n WHERE tag=:tag\n group BY tag, type, itemId"; $params = array(':tag' => $term); // group by type and itemId to prevent display of duplicate tags $sql = Yii::app()->db->createCommand($tagQuery); $totalItemCount = Yii::app()->db->createCommand("\n SELECT count(*)\n FROM ({$tagQuery}) as t1;\n ")->queryScalar($params); $results = new CSqlDataProvider($sql, array('totalItemCount' => $totalItemCount, 'sort' => array('defaultOrder' => 'timestamp DESC'), 'pagination' => array('pageSize' => Profile::getResultsPerPage()), 'params' => $params)); $this->render('searchTags', array('tags' => $results, 'term' => $term)); } } }
public function actionWhatsNew(){ if(!Yii::app()->user->isGuest){ $user=UserChild::model()->findByPk(Yii::app()->user->getId()); $lastLogin=$user->lastLogin; $contacts=ContactChild::model()->findAll("lastUpdated > $lastLogin"); $actions=ActionChild::model()->findAll("lastUpdated > $lastLogin AND (assignedTo='".Yii::app()->user->getName()."' OR assignedTo='Anyone')"); $sales=SaleChild::model()->findAll("lastUpdated > $lastLogin"); $accounts=AccountChild::model()->findAll("lastUpdated > $lastLogin"); $arr=array_merge($contacts,$actions,$sales,$accounts); //$arr=array_merge($arr,$sales); //$arr=array_merge($arr,$accounts); $records=Record::convert($arr); $dataProvider=new CArrayDataProvider($records,array( 'id'=>'id', 'pagination'=>array( 'pageSize'=>10, ), )); $this->render('whatsNew',array( 'records'=>$records, 'dataProvider'=>$dataProvider, )); } else{ $this->redirect('login'); } }
public function actionSearch(){ $term=$_GET['term']; $contacts=ContactChild::model()->findAllBySql('SELECT * FROM x2_contacts WHERE CONCAT(firstName," ",lastName) LIKE "%'.$term.'%" OR backgroundInfo LIKE "%'.$term.'%" OR email LIKE "%'.$term.'%" OR firstName LIKE "%'.$term.'%" OR lastName LIKE "%'.$term.'%" OR phone LIKE "%'.$term.'%" OR address LIKE "%'.$term.'%"'); $actions=ActionChild::model()->findAllBySql('SELECT * FROM x2_actions WHERE actionDescription LIKE "%'.$term.'%" LIMIT 10000'); $sales=SaleChild::model()->findAllBySql('SELECT * FROM x2_sales WHERE name LIKE "%'.$term.'%" OR description LIKE "%'.$term.'%"'); $accounts=AccountChild::model()->findAllBySql('SELECT * FROM x2_accounts WHERE name LIKE "%'.$term.'%" OR description LIKE "%'.$term.'%" OR tickerSymbol LIKE "%'.$term.'%"'); $projects=ProjectChild::model()->findAll(); $cases=CaseChild::model()->findAll(); $marketing=MarketingChild::model()->findAll(); $names=array(); $descriptions=array(); $notes=array(); $records=array(); $regEx="/$term/i"; foreach($contacts as $contact){ if(preg_match($regEx,$contact->firstName." ".$contact->lastName)>0){ $names[]=$contact; }elseif(preg_match($regEx,$contact->firstName)>0 || preg_match($regEx,$contact->lastName)>0){ $notes[]=$contact; }elseif(preg_match($regEx,$contact->backgroundInfo)>0){ $descriptions[]=$contact; }elseif(preg_match($regEx,$contact->email)>0){ $names[]=$contact; }elseif(preg_match($regEx,$contact->phone)>0){ $names[]=$contact; }elseif(preg_match($regEx,$contact->address)>0){ $names[]=$contact; } } foreach($actions as $action){ if(preg_match($regEx,$action->actionDescription)>0){ $names[]=$action; } } foreach($sales as $sale){ if(preg_match($regEx,$sale->name)>0){ $names[]=$sale; }elseif(preg_match($regEx,$sale->description)>0){ $descriptions[]=$sale; } } foreach($accounts as $account){ if(preg_match($regEx,$account->name)>0){ $names[]=$account; }elseif(preg_match($regEx,$account->tickerSymbol)>0){ $names[]=$account; }elseif(preg_match($regEx,$account->description)>0){ $descriptions[]=$account; }elseif(preg_match($regEx,$account->website)>0){ $names[]=$account; } } foreach($projects as $project){ if(preg_match($regEx,$project->name)>0){ $names[]=$project; }elseif(preg_match($regEx,$project->description)>0){ $descriptions[]=$project; } } foreach($cases as $case){ if(preg_match($regEx,$case->name)>0){ $names[]=$case; }elseif(preg_match($regEx,$case->description)>0){ $descriptions[]=$case; } } foreach($marketing as $item){ if(preg_match($regEx,$item->name)>0){ $names[]=$item; }elseif(preg_match($regEx,$item->description)>0){ $descriptions[]=$item; } } $records=array_merge($names,$descriptions); $records=array_merge($records,$notes); asort($records); $records=Record::convert($records); $dataProvider=new CArrayDataProvider($records,array( 'id'=>'id', 'pagination'=>array( 'pageSize'=>10, ), )); $this->render('search',array( 'records'=>$records, 'dataProvider'=>$dataProvider, 'term'=>$term, )); }