Exemplo n.º 1
0
 function afterSave(&$Model)
 {
     App::import('Model', 'EavAttribute');
     $eavAttribute = new EavAttribute();
     foreach ($this->settings[$Model->alias] as $assoc) {
         $attributes = $eavAttribute->find('all', array('conditions' => array('backend_model =' => $assoc['with'])));
         if (!empty($attributes)) {
             $i = 0;
             foreach ($attributes as $attribute) {
                 $Model->{$assoc['with']}->create(false);
                 extract($attributes[$i][$eavAttribute->alias], EXTR_OVERWRITE);
                 if (isset($Model->data[$assoc['with']][$attribute_code])) {
                     $attributeExists = $Model->{$assoc['with']}->find('all', array('conditions' => array(array('attribute_id =' => $attribute_id), array('AND ' => array('entity_id' => $Model->data[$Model->alias]['entity_id'])))));
                     if (!empty($attributeExists)) {
                         $Model->{$assoc['with']}->set('id', $attributeExists[0][$Model->{$assoc['with']}->alias]['id']);
                     }
                     $Model->{$assoc['with']}->set('attribute_id', $attribute_id);
                     $Model->{$assoc['with']}->set('entity_id', $Model->data[$Model->alias]['entity_id']);
                     $Model->{$assoc['with']}->set('value', $Model->data[$assoc['with']][$attribute_code]);
                     $Model->{$assoc['with']}->save();
                 }
                 $i++;
             }
         }
     }
 }
 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.º 4
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.º 7
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.º 8
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.º 9
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);
 }
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getAttributes3()
 {
     return $this->hasMany(EavAttribute::className(), ['attribute_id' => 'attribute_id'])->viaTable('{{%customer_address_entity_varchar}}', ['entity_id' => 'entity_id']);
 }
Exemplo n.º 11
0
 public function getEavVariants($attrName)
 {
     $attr = EavAttribute::model()->findByAttributes(array('name' => $attrName));
     return $attr->getPossibleValues();
 }
Exemplo n.º 12
0
 /**
  * Returns the cache component.
  * @return ICache An instance of a class that implements the interface ICache.
  * @throws CException If the cache component is not initialized.
  */
 protected function getCache()
 {
     if (!is_null(self::$cache)) {
         return self::$cache;
     }
     if (isset(Yii::app()->{self::CACHE_ID}) && Yii::app()->{self::CACHE_ID} instanceof ICache) {
         self::$cache = Yii::app()->{self::CACHE_ID};
         return self::$cache;
     }
     $id = $this->getDbConnection()->schemaCacheID;
     if (isset(Yii::app()->{$id}) && Yii::app()->{$id} instanceof ICache) {
         self::$cache = Yii::app()->{$id};
         return self::$cache;
     }
     throw new CException('The cache component is not initialized and cannot be read.');
 }
 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.º 14
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.º 15
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 
 /**
  * Метод выполняемый перед удалением:
  *
  * @return void
  */
 public function beforeDelete()
 {
     // Удаляем связь с группами
     EavAttributeToSet::model()->deleteAllByAttributes(['eav_attribute_id' => $this->id]);
     // Удаляем значения атрибутов
     EavValue::model()->deleteAllByAttributes(['eav_attribute_id' => $this->id]);
     foreach ($this->variants as $variant) {
         $variant->delete();
     }
     return parent::beforeDelete();
 }
 protected function afterSave()
 {
     $attribute = new EavAttribute();
     $attribute->id = $this->id;
     $attribute->name = $this->name;
     $attribute->label = $this->label;
     $attribute->type = $this->type;
     $attribute->data_type = $this->data_type;
     $attribute->data = $this->data;
     $attribute->setIsNewRecord(false);
     $this->setCacheEavAttribute($attribute);
 }
Exemplo n.º 18
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getEavAttributes()
 {
     return $this->hasMany(EavAttribute::className(), ['id' => 'attribute_id'])->viaTable('{{%eav_entity_attribute}}', ['entity_id' => 'id']);
     //->orderBy(['eav_entity_attribute.order' => SORT_DESC]);
 }
Exemplo n.º 19
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;
             }
         }
     }
 }