/** * Save associated criterion objects for a dynamic list * * Takes data from the dynamic list criteria designer form and turns them * into {@link X2ListCriterion} records. */ public function processCriteria() { X2ListCriterion::model()->deleteAllByAttributes(array('listId' => $this->id)); // delete old criteria foreach (array('attribute', 'comparison', 'value') as $property) { // My lazy refactor: bring properties into the current scope as // temporary variables with their names pluralized ${"{$property}s"} = $this->criteriaInput[$property]; } $comparisonList = self::getComparisonList(); $contactModel = Contacts::model(); $fields = $contactModel->getFields(true); for ($i = 0; $i < count($attributes); $i++) { // create new criteria if ((array_key_exists($attributes[$i], $contactModel->attributeLabels()) || $attributes[$i] == 'tags') && array_key_exists($comparisons[$i], $comparisonList)) { $fieldRef = isset($fields[$attributes[$i]]) ? $fields[$attributes[$i]] : null; if ($fieldRef instanceof Fields && $fieldRef->type == 'link') { $nameList = explode(',', $values[$i]); $namesParams = AuxLib::bindArray($nameList); $namesIn = AuxLib::arrToStrList(array_keys($namesParams)); $lookupModel = X2Model::model(ucfirst($fieldRef->linkType)); $lookupModels = $lookupModel->findAllBySql('SELECT * FROM `' . $lookupModel->tableName() . '` ' . 'WHERE `name` IN ' . $namesIn, $namesParams); if (!empty($lookupModels)) { $values[$i] = implode(',', array_map(function ($m) { return $m->nameId; }, $lookupModels)); //$lookup->nameId; } } $criterion = new X2ListCriterion(); $criterion->listId = $this->id; $criterion->type = 'attribute'; $criterion->attribute = $attributes[$i]; $criterion->comparison = $comparisons[$i]; $criterion->value = $values[$i]; $criterion->save(); } } }
public function actionUpdateList($id) { $list = X2List::model()->findByPk($id); if (!isset($list)) { throw new CHttpException(400, Yii::t('app', 'This list cannot be found.')); } if (!$this->editPermissions($list)) { throw new CHttpException(403, Yii::t('app', 'You do not have permission to modify this list.')); } $contactModel = new Contacts(); $comparisonList = array('=' => '=', '>' => '>', '<' => '<', '<>' => '<>', 'contains' => Yii::t('contacts', 'contains'), 'empty' => Yii::t('empty', 'empty'), 'notEmpty' => Yii::t('contacts', 'not empty'), 'list' => Yii::t('contacts', 'in list')); if ($list->type == 'dynamic') { $criteriaModels = X2ListCriterion::model()->findAllByAttributes(array('listId' => $list->id), new CDbCriteria(array('order' => 'id ASC'))); } if (isset($_POST['X2List'], $_POST['X2List']['attribute'], $_POST['X2List']['comparison'], $_POST['X2List']['value'])) { $attributes =& $_POST['X2List']['attribute']; $comparisons =& $_POST['X2List']['comparison']; $values =& $_POST['X2List']['value']; if (count($attributes) > 0 && count($attributes) == count($comparisons) && count($comparisons) == count($values)) { $list->attributes = $_POST['X2List']; $list->modelName = 'Contacts'; $list->lastUpdated = time(); if ($list->save()) { X2ListCriterion::model()->deleteAllByAttributes(array('listId' => $list->id)); // delete old criteria for ($i = 0; $i < count($attributes); $i++) { // create new criteria if ((array_key_exists($attributes[$i], $contactModel->attributeLabels()) || $attributes[$i] == 'tags') && array_key_exists($comparisons[$i], $comparisonList)) { //&& $values[$i] != '' $criterion = new X2ListCriterion(); $criterion->listId = $list->id; $criterion->type = 'attribute'; $criterion->attribute = $attributes[$i]; $criterion->comparison = $comparisons[$i]; $criterion->value = $values[$i]; $criterion->save(); } } $this->redirect(array('/contacts/list/' . $list->id)); } } } if (empty($criteriaModels)) { $default = new X2ListCriterion(); $default->value = ''; $default->attribute = ''; $default->comparison = 'contains'; $criteriaModels[] = $default; } $this->render('updateList', array('model' => $list, 'criteriaModels' => $criteriaModels, 'users' => User::getNames(), 'comparisonList' => $comparisonList, 'listTypes' => array('dynamic' => Yii::t('contacts', 'Dynamic'), 'static' => Yii::t('contacts', 'Static')), 'itemModel' => $contactModel)); }