Esempio n. 1
0
 /**
  * @inheritdoc
  */
 public function run()
 {
     $this->object = Object::getForClass(get_class($this->model));
     $cacheKey = 'PropertiesWidget: ' . get_class($this->model) . ':' . $this->model->id;
     $data = Yii::$app->cache->get($cacheKey);
     if ($data === false) {
         $this->objectPropertyGroups = ObjectPropertyGroup::getForModel($this->model);
         $addedPropertyGroupsIds = [];
         foreach ($this->objectPropertyGroups as $opg) {
             $addedPropertyGroupsIds[] = $opg->property_group_id;
         }
         $restPg = (new Query())->select('id, name')->from(PropertyGroup::tableName())->where(['object_id' => $this->object->id])->andWhere(['not in', 'id', $addedPropertyGroupsIds])->orderBy('sort_order')->all();
         $this->propertyGroupsToAdd = ArrayHelper::map($restPg, 'id', 'name');
         Yii::$app->cache->set($cacheKey, ['objectPropertyGroups' => $this->objectPropertyGroups, 'propertyGroupsToAdd' => $this->propertyGroupsToAdd], 86400, new TagDependency(['tags' => [ActiveRecordHelper::getCommonTag(get_class($this->model)), ActiveRecordHelper::getCommonTag(PropertyGroup::className()), ActiveRecordHelper::getCommonTag(Property::className())]]));
     } else {
         $this->objectPropertyGroups = $data['objectPropertyGroups'];
         $this->propertyGroupsToAdd = $data['propertyGroupsToAdd'];
     }
     return $this->render($this->viewFile, ['model' => $this->model, 'object' => $this->object, 'object_property_groups' => $this->objectPropertyGroups, 'property_groups_to_add' => $this->propertyGroupsToAdd, 'form' => $this->form, 'widget_id' => $this->getId()]);
 }
Esempio n. 2
0
 /**
  * @return PropertyGroup|null
  */
 public function getGroup()
 {
     return $this->hasOne(PropertyGroup::className(), ['id' => 'property_group_id']);
 }
Esempio n. 3
0
 public function getPropertyGroups($force = false, $getByObjectId = false, $createAbstractModel = false)
 {
     $message = "getPropertyGroups " . $this->owner->className() . ':' . $this->owner->id . " force:" . ($force ? 'true' : 'false') . " getByObjectId:" . ($getByObjectId ? 'true' : 'false') . " createAbstractModel:" . ($createAbstractModel ? 'true' : 'false');
     Yii::trace($message, 'properties');
     $cacheKey = $message;
     if ($this->properties === null && $force === false) {
         // try to get from cache
         $data = Yii::$app->cache->get($message);
         if ($data !== false) {
             Yii::trace("Properties from cache", 'properties');
             $this->abstract_model = isset($data['abstract_model']) ? $data['abstract_model'] : [];
             $this->properties = $data['properties'];
             $this->property_key_to_id = $data['property_key_to_id'];
             $this->property_id_to_group_id = $data['property_id_to_group_id'];
         }
     }
     if ($createAbstractModel === true && is_null($this->abstract_model)) {
         // force creating abstract model if it is null and needed
         $force = true;
     }
     if ($this->properties === null || $force === true) {
         $tags = [ActiveRecordHelper::getObjectTag($this->owner->className(), $this->owner->id)];
         $this->properties = [];
         if ($getByObjectId === true) {
             $groups = PropertyGroup::getForObjectId($this->getObject()->id);
         } else {
             $groups = PropertyGroup::getForModel($this->getObject()->id, $this->owner->id);
         }
         $values_for_abstract = [];
         $properties_models = [];
         $rules = [];
         /** @var PropertyGroup $group */
         foreach ($groups as $group) {
             $tags[] = ActiveRecordHelper::getObjectTag(PropertyGroup::className(), $group->id);
             $this->properties[$group->id] = [];
             $props = Property::getForGroupId($group->id);
             foreach ($props as $p) {
                 $values = $this->getPropertyValues($p);
                 $this->properties[$group->id][$p->key] = $values;
                 if ($createAbstractModel === true) {
                     $values_for_abstract[$p->key] = $values;
                 }
                 $properties_models[$p->key] = $p;
                 $this->property_key_to_id[$p->key] = $p->id;
                 if (!isset($this->property_id_to_group_id[$group->id])) {
                     $this->property_id_to_group_id[$group->id] = [$p->key];
                 } else {
                     $this->property_id_to_group_id[$group->id][] = $p->key;
                 }
                 if ($createAbstractModel === true) {
                     $handlerAdditionalParams = Json::decode($p->handler_additional_params);
                     if (isset($handlerAdditionalParams['rules']) && is_array($handlerAdditionalParams['rules'])) {
                         foreach ($handlerAdditionalParams['rules'] as $rule) {
                             if (is_array($rule)) {
                                 $rules[] = ArrayHelper::merge([$p->key], $rule);
                             } else {
                                 $rules[] = [$p->key, $rule];
                             }
                         }
                     }
                 }
             }
         }
         if ($createAbstractModel === true) {
             $this->abstract_model = new AbstractModel([], $this->owner);
             $this->abstract_model->setPropertiesModels($properties_models);
             $this->abstract_model->setAttributes($values_for_abstract);
             $this->abstract_model->setFormName('Properties_' . $this->owner->formName() . '_' . $this->owner->id);
             $this->abstract_model->addRules($rules);
         }
         $res = Yii::$app->cache->set($cacheKey, ['properties' => $this->properties, 'abstract_model' => $this->abstract_model, 'property_id_to_group_id' => $this->property_id_to_group_id, 'property_key_to_id' => $this->property_key_to_id], 86400, new TagDependency(['tags' => $tags]));
         Yii::trace('putting props to cache: ' . ($res ? 'true' : 'false') . ' key:' . $cacheKey, 'properties');
     }
     return $this->properties;
 }
Esempio n. 4
0
 /**
  * Returns array of ids of propertyGroups for user model
  * @return array
  * @throws \Exception
  */
 private function getPropertyIdsForUsers()
 {
     return PropertyGroup::getDb()->cache(function ($db) {
         return PropertyGroup::find()->select('id')->where(['object_id' => $this->getObject()->id])->asArray()->column($db);
     }, 86400, new TagDependency(['tags' => ActiveRecordHelper::getCommonTag(PropertyGroup::className())]));
 }
Esempio n. 5
0
 /**
  *
  */
 public function invalidateModelCache()
 {
     TagDependency::invalidate(Yii::$app->cache, [ActiveRecordHelper::getObjectTag(PropertyGroup::className(), $this->property_group_id), ActiveRecordHelper::getObjectTag(Property::className(), $this->id)]);
 }
 public function actionAddStaticValue($key, $value, $returnUrl, $objectId = null, $objectModelId = null)
 {
     $model = new PropertyStaticValues();
     /** @var Property $property */
     $property = Property::findOne(['key' => $key]);
     if (is_null($property)) {
         throw new NotFoundHttpException();
     }
     $model->property_id = $property->id;
     if (Yii::$app->request->isPost) {
         if ($model->load(Yii::$app->request->post()) && $model->save()) {
             $tags = [ActiveRecordHelper::getCommonTag(Property::className()), ActiveRecordHelper::getObjectTag(Property::className(), $property->id), ActiveRecordHelper::getCommonTag(PropertyGroup::className()), ActiveRecordHelper::getObjectTag(PropertyGroup::className(), $property->property_group_id)];
             if (!is_null($objectId) && !is_null($objectModelId)) {
                 if ($property->multiple == 0) {
                     $propertyStaticValueIds = PropertyStaticValues::find()->select('id')->where(['property_id' => $property->id])->column();
                     ObjectStaticValues::deleteAll(['object_id' => $objectId, 'object_model_id' => $objectModelId, 'property_static_value_id' => $propertyStaticValueIds]);
                 }
                 $objectStaticValues = new ObjectStaticValues();
                 $objectStaticValues->attributes = ['object_id' => $objectId, 'object_model_id' => $objectModelId, 'property_static_value_id' => $model->id];
                 $objectStaticValues->save();
                 $tags[] = ActiveRecordHelper::getCommonTag(Object::findById($objectId)->object_class);
                 $tags[] = ActiveRecordHelper::getObjectTag(Object::findById($objectId)->object_class, $objectModelId);
             }
             TagDependency::invalidate(Yii::$app->cache, $tags);
             return $this->redirect($returnUrl);
         }
     } elseif ($value !== "") {
         $model->name = $value;
         $model->value = $value;
         $model->slug = Helper::createSlug($value);
         $model->sort_order = 0;
     }
     return $this->renderAjax('ajax-static-value', ['model' => $model]);
 }