public function loadModel($id)
 {
     if (($model = EavAttribute::model()->findByPk($id)) === null) {
         throw new CHttpException(404, 'Страница не найдена');
     }
     return $model;
 }
 protected function refreshEavAttributes()
 {
     $attachedEavAttributes = $this->attachedEavAttributes;
     $oldEavAttributes = EavAttributeToSet::model()->findAllByAttributes(array('eav_set_id' => $this->id));
     $orderArray = CHtml::listData($oldEavAttributes, 'eav_attribute_id', 'weight');
     EavAttributeToSet::model()->deleteAllByAttributes(array('eav_set_id' => $this->id));
     if (is_array($attachedEavAttributes)) {
         $counter = end($orderArray) + 10;
         //var_dump($counter);
         //exit();
         foreach ($attachedEavAttributes as $key => $id) {
             if (EavAttribute::model()->exists('t.id = :id', array(':id' => $id))) {
                 $rel = new EavAttributeToSet();
                 $rel->eav_set_id = $this->id;
                 $rel->eav_attribute_id = $id;
                 if (key_exists($id, $orderArray)) {
                     $rel->weight = $orderArray[$id];
                 } else {
                     $rel->weight = $counter;
                     $counter += 10;
                 }
                 $rel->save();
             }
         }
     }
 }
Exemplo n.º 3
0
 protected function refreshEavAttributes()
 {
     $attachedEavAttributes = $this->attachedEavAttributes;
     $oldEavAttributes = EavAttributeToSet::model()->findAllByAttributes(['eav_set_id' => $this->id], ['order' => 't.weight ASC']);
     $orderArray = CHtml::listData($oldEavAttributes, 'eav_attribute_id', 'weight');
     $oldList = CHtml::listData($oldEavAttributes, 'eav_attribute_id', function ($data) {
         return $data;
     });
     EavAttributeToSet::model()->deleteAllByAttributes(['eav_set_id' => $this->id]);
     if (is_array($attachedEavAttributes)) {
         $counter = end($orderArray) + 10;
         foreach ($attachedEavAttributes as $key => $id) {
             if (EavAttribute::model()->exists('t.id = :id', [':id' => $id])) {
                 $rel = new EavAttributeToSet();
                 $rel->eav_set_id = $this->id;
                 $rel->eav_attribute_id = $id;
                 if (key_exists($id, $oldList)) {
                     $rel->attributes = $oldList[$id]->attributes;
                 } else {
                     $rel->weight = $counter;
                     $counter += 10;
                 }
                 //var_dump($rel);
                 //echo '<hr>';
                 $rel->save();
             }
         }
     }
 }
 public function getEavAttributeLabels()
 {
     $data = $this->dbConnection->createCommand()->select('id, name')->from(EavAttribute::model()->tableName())->queryAll();
     if (empty($data)) {
         return array();
     }
     $list = array();
     foreach ($data as $row) {
         $list[$row['id']] = $row['name'];
     }
     return $list;
 }
 /**
  * Manages all models.
  *
  * @return void
  */
 public function actionIndex($attribute_id)
 {
     $model = EavAttribute::model()->findByPk((int) $attribute_id);
     if ($model === null) {
         throw new CHttpException(404, 'Страница не найдена');
     }
     $variants = new EavVariant('search');
     $variants->unsetAttributes();
     // clear any default values
     $variants->attribute_id = $model->id;
     $variants->setAttributes(Yii::app()->getRequest()->getParam('EavVariant', []));
     $this->render('index', ['model' => $model, 'variants' => $variants]);
 }
Exemplo n.º 6
0
 public function run($id)
 {
     $model = EavAttribute::model()->findByPk($id);
     if (is_null($model)) {
         throw new CHttpException(404);
     }
     if ($model->delete()) {
         Yii::app()->getComponent('user')->setFlash('success', Yii::t('EavModule.eavactiverecord', 'The attribute has been successfully deleted'));
     } else {
         Yii::app()->getComponent('user')->setFlash('error', Yii::t('EavModule.eavactiverecord', 'An error occurred while deleting the attribute'));
     }
     $this->controller->redirect(array('attribute/index'));
 }
Exemplo n.º 7
0
 protected function refreshEavAttributes()
 {
     $attachedEavAttributes = $this->attachedEavAttributes;
     EavAttributeToSet::model()->deleteAllByAttributes(array('set_id' => $this->id));
     if (is_array($attachedEavAttributes)) {
         foreach ($attachedEavAttributes as $id) {
             if (EavAttribute::model()->exists('t.id = :id', array(':id' => $id))) {
                 $rel = new EavAttributeToSet();
                 $rel->set_id = $this->id;
                 $rel->attribute_id = $id;
                 $rel->save();
             }
         }
     }
 }
Exemplo n.º 8
0
 public function testRemoveAddEavAttribute()
 {
     $model = new EavSet();
     $model->name = 'fsf';
     $model->addEavAttribute(1);
     $model->removeEavAttribute(EavAttribute::model()->findByPk(1));
     $model->addEavAttribute(EavAttribute::model()->findByPk(1));
     $model->removeEavAttribute(1);
     $this->assertTrue($model->save());
     $attributes = $model->getRelated(EavActiveRecord::EAV_ATTRIBUTE_RELATION_NAME);
     $this->assertEquals(0, count($attributes));
     $model = EavSet::model()->findByPk(2);
     $model->removeEavAttribute(1);
     $model->addEavAttribute(EavAttribute::model()->findByPk(1));
     $model->removeEavAttribute(EavAttribute::model()->findByPk(2));
     $model->addEavAttribute(2);
     $model->addEavAttribute(3);
     $this->assertTrue($model->save());
     $attributes = $model->getRelated(EavActiveRecord::EAV_ATTRIBUTE_RELATION_NAME);
     $this->assertEquals(3, count($attributes));
     $this->assertArrayHasKey(1, $attributes);
     $this->assertArrayHasKey(2, $attributes);
     $this->assertArrayHasKey(3, $attributes);
 }
Exemplo n.º 9
0
 public function getEavVariants($attrName)
 {
     $attr = EavAttribute::model()->findByAttributes(array('name' => $attrName));
     return $attr->getPossibleValues();
 }
 public function testPossibleValues()
 {
     $attribute = EavAttribute::model()->findByPk(1);
     $values = array('a' => 'Alberta', 'b' => 'Banff', 'c' => 'Calgary');
     $attribute->setPossibleValues($values);
     $newvalues = $attribute->getPossibleValues();
     $this->assertEquals($values, $newvalues);
     $attribute->save();
     $attribute = EavAttribute::model()->findByPk(1);
     $this->assertEquals($values, $attribute->getPossibleValues());
 }
Exemplo n.º 11
0
 /**
  * Adds a temporary relation which will be used while performing the search with EAV-attributes.
  * @throws CException
  */
 private function addFindEavRelation()
 {
     if (is_null($this->findEavAttributes)) {
         return;
     }
     $attributes = EavAttribute::model()->getEavAttributes($this->findEavAttributes);
     foreach ($attributes as $name => $attribute) {
         $class = EavValue::model($attribute->data_type);
         if (!$class instanceof EavValue) {
             throw new CException('The class ' . $attribute->data_type . '.php must be ' . 'a subclass of the class EavValue.');
         }
         //$relation = $attribute->type == EavAttribute::TYPE_SINGLE ? self::HAS_ONE : self::HAS_MANY;
         $relation = self::HAS_ONE;
         $name = self::EAV_FIND_RELATION_NAME . ucfirst($name);
         $condition = $this->quoteColumnName($name . '.eav_attribute_id') . ' = :' . $name . '_eav_attribute_id AND  ' . $this->quoteColumnName($name . '.entity') . ' = :' . $name . '_entity';
         $this->metaData->addRelation($name, array($relation, $attribute->data_type, 'entity_id', 'select' => false, 'on' => $condition, 'params' => array(':' . $name . '_eav_attribute_id' => $attribute->id, ':' . $name . '_entity' => $this->getEntity())));
         $this->getDbCriteria()->mergeWith(array('with' => array($name)));
     }
 }
Exemplo n.º 12
0
    <?php 
echo $form->errorSummary($model);
?>

    <div class='control-group <?php 
echo $model->hasErrors("title") ? "error" : "";
?>
'>
        <?php 
echo $form->textFieldRow($model, 'title', array('class' => 'span7', 'maxlength' => 250));
?>
    </div>

    <div class='row-fluid control-group'>
        <?php 
echo $form->checkBoxListRow($model, 'attachedEavAttributes', EavAttribute::model()->list);
?>
    </div>

    <?php 
$this->widget('bootstrap.widgets.TbButton', array('buttonType' => 'submit', 'type' => 'primary', 'label' => $model->isNewRecord ? 'Создать и продолжить' : 'Сохранить и продолжить'));
?>
    <?php 
$this->widget('bootstrap.widgets.TbButton', array('buttonType' => 'submit', 'htmlOptions' => array('name' => 'submit-type', 'value' => 'index'), 'label' => $model->isNewRecord ? 'Создать и закрыть' : 'Сохранить и закрыть'));
?>

<?php 
$this->endWidget();
?>

<?php 
 public static function model($className = __CLASS__)
 {
     return parent::model($className);
 }
Exemplo n.º 14
0
 /**
  * Prepares an SQL condition.
  * @param mixed $condition It must be a string or an array or an instance of the class CDbCriteria.
  * @return mixed Returns an instance of the class CDbCriteria or a string.
  */
 private function prepareCondition($condition)
 {
     $pattern = '/[:]{2}[a-zA-Z_\\x7f-\\xff]+[a-zA-Z0-9_\\x7f-\\xff]*[:]{2}/';
     $matches = array();
     $replace = array();
     if (is_string($condition)) {
         preg_match_all($pattern, $condition, $matches);
         if (!empty($matches[0])) {
             /** Oriole **/
             $attributes = EavAttribute::model()->findAll();
             if (!empty($attributes)) {
                 foreach ($attributes as $attr) {
                     if (!$this->hasAttribute($attr->name) && !$this->getMetaData()->hasRelation($attr->name)) {
                         $eavAttributeInstances[$attr->name] = $attr;
                     }
                 }
             }
             /** --Oriole **/
             foreach ($matches[0] as $name) {
                 $name = trim($name, '::');
                 $this->findEavAttributes[] = $name;
                 if (key_exists($name, $eavAttributeInstances)) {
                     $attribute = $eavAttributeInstances[$name];
                     $class = EavValue::model($attribute->data_type);
                     $replace[] = $this->quoteColumnName(self::EAV_FIND_RELATION_NAME . ucfirst($name) . '.' . $class->valueField);
                 }
             }
             $this->addFindEavRelation();
             return str_replace($matches[0], $replace, $condition);
         }
         return $condition;
     } else {
         if (is_array($condition)) {
             $criteria = new CDbCriteria($condition);
             $condition = $this->prepareCondition($criteria->condition);
             $criteria->condition = $condition;
             return $criteria;
         } else {
             if ($condition instanceof CDbCriteria) {
                 $condition->condition = $this->prepareCondition($condition->condition);
                 return $condition;
             }
         }
     }
 }