public function search() { $pageSize = Yii::app()->user->getState('pageSize', Yii::app()->params['defaultPageSize']); $sort = new CSort(); $sort->attributes = array('survey_id' => array('asc' => 't.sid asc', 'desc' => 't.sid desc'), 'title' => array('asc' => 'correct_relation_defaultlanguage.surveyls_title asc', 'desc' => 'correct_relation_defaultlanguage.surveyls_title desc'), 'creation_date' => array('asc' => 't.datecreated asc', 'desc' => 't.datecreated desc'), 'owner' => array('asc' => 'owner.users_name asc', 'desc' => 'owner.users_name desc'), 'anonymized_responses' => array('asc' => 't.anonymized asc', 'desc' => 't.anonymized desc'), 'running' => array('asc' => 't.active asc, t.expires asc', 'desc' => 't.active desc, t.expires desc')); $sort->defaultOrder = array('creation_date' => CSort::SORT_DESC); $criteria = new LSDbCriteria(); $aWithRelations = array('correct_relation_defaultlanguage'); // Search filter $sid_reference = Yii::app()->db->getDriverName() == 'pgsql' ? ' t.sid::varchar' : 't.sid'; $aWithRelations[] = 'owner'; $criteria->compare($sid_reference, $this->searched_value, true); $criteria->compare('t.admin', $this->searched_value, true, 'OR'); $criteria->compare('owner.users_name', $this->searched_value, true, 'OR'); $criteria->compare('correct_relation_defaultlanguage.surveyls_title', $this->searched_value, true, 'OR'); // Active filter if (isset($this->active)) { if ($this->active == 'N' || $this->active == "Y") { $criteria->compare("t.active", $this->active, false); } else { // Time adjust $sNow = date("Y-m-d H:i:s", strtotime(Yii::app()->getConfig('timeadjust'), strtotime(date("Y-m-d H:i:s")))); if ($this->active == "E") { $criteria->compare("t.active", 'Y'); $criteria->addCondition("t.expires <'{$sNow}'"); } if ($this->active == "S") { $criteria->compare("t.active", 'Y'); $criteria->addCondition("t.startdate >'{$sNow}'"); } if ($this->active == "R") { $now = new CDbExpression("NOW()"); $criteria->compare("t.active", 'Y'); $subCriteria1 = new CDbCriteria(); $subCriteria2 = new CDbCriteria(); $subCriteria1->addCondition($now . ' > t.startdate', 'OR'); $subCriteria2->addCondition($now . ' < t.expires', 'OR'); $subCriteria1->addCondition('t.expires IS NULL', "OR"); $subCriteria2->addCondition('t.startdate IS NULL', "OR"); $criteria->mergeWith($subCriteria1); $criteria->mergeWith($subCriteria2); } } } $criteria->with = $aWithRelations; // Permission // Note: reflect Permission::hasPermission if (!Permission::model()->hasGlobalPermission("surveys", 'read')) { $criteriaPerm = new CDbCriteria(); // Multiple ON conditions with string values such as 'survey' $criteriaPerm->mergeWith(array('join' => "LEFT JOIN {{permissions}} AS permissions ON (permissions.entity_id = t.sid AND permissions.permission='survey' AND permissions.entity='survey' AND permissions.uid='" . Yii::app()->user->id . "') ")); $criteriaPerm->compare('t.owner_id', Yii::app()->user->id, false); $criteriaPerm->compare('permissions.read_p', '1', false, 'OR'); $criteria->mergeWith($criteriaPerm, 'AND'); } // $criteria->addCondition("t.blabla == 'blub'"); $dataProvider = new CActiveDataProvider('Survey', array('sort' => $sort, 'criteria' => $criteria, 'pagination' => array('pageSize' => $pageSize))); $dataProvider->setTotalItemCount($this->count($criteria)); return $dataProvider; }
public function search() { $pageSize = Yii::app()->user->getState('pageSize', Yii::app()->params['defaultPageSize']); $sort = new CSort(); $sort->defaultOrder = 'tid ASC'; $sort->attributes = array('tid' => array('asc' => 'tid', 'desc' => 'tid desc'), 'partcipant' => array('asc' => 'partcipant', 'desc' => 'partcipant desc'), 'firstname' => array('asc' => 'firstname', 'desc' => 'firstname desc'), 'lastname' => array('asc' => 'lastname', 'desc' => 'lastname desc'), 'email' => array('asc' => 'email', 'desc' => 'email desc'), 'emailstatus' => array('asc' => 'emailstatus', 'desc' => 'emailstatus desc'), 'token' => array('asc' => 'token', 'desc' => 'token desc'), 'language' => array('asc' => 'language', 'desc' => 'language desc'), 'blacklisted' => array('asc' => 'blacklisted', 'desc' => 'blacklisted desc'), 'sent' => array('asc' => 'sent', 'desc' => 'sent desc'), 'remindersent' => array('asc' => 'remindersent', 'desc' => 'remindersent desc'), 'remindercount' => array('asc' => 'remindercount', 'desc' => 'remindercount desc'), 'completed' => array('asc' => 'completed', 'desc' => 'completed desc'), 'usesleft' => array('asc' => 'usesleft', 'desc' => 'usesleft desc'), 'validfrom' => array('asc' => 'validfrom', 'desc' => 'validfrom desc'), 'validuntil' => array('asc' => 'validuntil', 'desc' => 'validuntil desc')); // Make sortable custom attributes foreach ($this->custom_attributes as $sColName => $oColumn) { $sort->attributes[$sColName] = array('asc' => $sColName, 'desc' => $sColName . ' desc'); } $criteria = new LSDbCriteria(); $criteria->compare('tid', $this->tid, false); $criteria->compare('token', $this->token, true); $criteria->compare('firstname', $this->firstname, true); $criteria->compare('lastname', $this->lastname, true); $criteria->compare('email', $this->email, true); $criteria->compare('emailstatus', $this->emailstatus, true); $criteria->compare('token', $this->token, true); $criteria->compare('language', $this->language, true); $criteria->compare('sent', $this->sent, true); $criteria->compare('remindersent', $this->remindersent, true); $criteria->compare('remindercount', $this->remindercount, false); $criteria->compare('completed', $this->completed, true); $criteria->compare('usesleft', $this->usesleft, false); $dateformatdetails = getDateFormatData(Yii::app()->session['dateformat']); if ($this->validfrom) { $s = DateTime::createFromFormat($dateformatdetails['phpdate'] . ' H:i', $this->validfrom); $s2 = $s->format('Y-m-d H:i'); $criteria->addCondition('validfrom <= \'' . $s2 . '\''); } if ($this->validuntil) { $s = DateTime::createFromFormat($dateformatdetails['phpdate'] . ' H:i', $this->validuntil); $s2 = $s->format('Y-m-d H:i'); $criteria->addCondition('validuntil >= \'' . $s2 . '\''); } foreach ($this->custom_attributes as $sColName => $oColumn) { $criteria->compare($sColName, $this->{$sColName}, true); } $dataProvider = new CActiveDataProvider('TokenDynamic', array('sort' => $sort, 'criteria' => $criteria, 'pagination' => array('pageSize' => $pageSize))); return $dataProvider; }