Example #1
0
 public function afterDelete()
 {
     $object = Object::findById($this->group->object_id);
     $staticValues = PropertyStaticValues::find()->where(['property_id' => $this->id])->all();
     foreach ($staticValues as $psv) {
         $psv->delete();
     }
     if (null !== $object) {
         if ($this->is_eav) {
             Yii::$app->db->createCommand()->delete($object->eav_table_name, ['key' => $this->key, 'property_group_id' => $this->group->id])->execute();
         }
         if ($this->is_column_type_stored) {
             Yii::$app->db->createCommand()->dropColumn($object->column_properties_table_name, $this->key)->execute();
             //                if ($object->object_class == Form::className()) {
             //                    $submissionObject = Object::getForClass(Submission::className());
             //                    Yii::$app->db->createCommand()
             //                        ->dropColumn($submissionObject->column_properties_table_name, $this->key)
             //                        ->execute();
             //                }
         }
     }
     FilterSets::deleteAll(['property_id' => $this->id]);
     parent::afterDelete();
 }
Example #2
0
 public function getPossibleSelections()
 {
     $data = ['propertyIds' => [], 'propertyStaticValueIds' => []];
     if ($this->onlyAvailableFilters) {
         Yii::beginProfile("onlyAvailableFilters");
         $object = Object::findById($this->objectId);
         if (!is_null($object) && isset($this->currentSelections['last_category_id'])) {
             $cacheKey = 'FilterWidget: ' . $object->id . ':' . $this->currentSelections['last_category_id'] . ':' . Json::encode($this->currentSelections['properties']);
             $data = Yii::$app->cache->get($cacheKey);
             if ($data === false) {
                 $data = [];
                 Yii::beginProfile("ObjectIds for this category");
                 $query = new Query();
                 $query = $query->select($object->categories_table_name . '.object_model_id')->distinct()->from($object->categories_table_name)->join("JOIN", Product::tableName(), sprintf("%s.`id` = %s.`object_model_id`", Product::tableName(), $object->categories_table_name))->andWhere([Product::tableName() . ".`active`" => 1])->andWhere(['category_id' => $this->currentSelections['last_category_id']]);
                 if (count($this->currentSelections['properties']) > 0) {
                     Yii::beginProfile("Apply currency selections(properties)");
                     foreach ($this->currentSelections['properties'] as $property_id => $values) {
                         $joinTableName = 'OSVJoinTable' . $property_id;
                         $query->join('JOIN', ObjectStaticValues::tableName() . ' ' . $joinTableName, $joinTableName . '.object_id = :objectId AND ' . $joinTableName . '.object_model_id = ' . $object->categories_table_name . '.object_model_id  ', [':objectId' => $object->id]);
                         $query->andWhere(['in', '`' . $joinTableName . '`.`property_static_value_id`', $values]);
                     }
                     Yii::endProfile("Apply currency selections(properties)");
                 }
                 Yii::endProfile("ObjectIds for this category");
                 $ids = array_map('intval', $query->column());
                 $query = null;
                 Yii::beginProfile("all PSV ids");
                 $data['propertyStaticValueIds'] = [];
                 if (count($ids) !== 0) {
                     $q4psv = (new Query())->select('property_static_value_id')->from(ObjectStaticValues::tableName())->distinct()->where(['object_id' => $object->id])->andWhere('object_model_id in (' . implode(',', $ids) . ')');
                     $data['propertyStaticValueIds'] = array_map('intval', $q4psv->column());
                 }
                 Yii::endProfile("all PSV ids");
                 $ids = null;
                 Yii::beginProfile("Property ids from PSV ids");
                 $data['propertyIds'] = [];
                 if (count($data['propertyStaticValueIds']) !== 0) {
                     $data['propertyIds'] = PropertyStaticValues::find()->select('property_id')->distinct()->where(['dont_filter' => 0])->andWhere('id IN (' . implode(',', $data['propertyStaticValueIds']) . ')')->asArray()->column();
                 }
                 Yii::endProfile("Property ids from PSV ids");
                 Yii::$app->cache->set($cacheKey, $data, 86400, new TagDependency(['tags' => [\devgroup\TagDependencyHelper\ActiveRecordHelper::getCommonTag($object->object_class)]]));
                 $object = null;
             }
         }
         Yii::endProfile("onlyAvailableFilters");
     }
     $this->possibleSelections = [];
     $groups = PropertyGroup::getForObjectId($this->objectId);
     foreach ($groups as $group) {
         if ($this->onlyGroupsIds !== null) {
             if (in_array($group->id, $this->onlyGroupsIds) === false) {
                 // skip this group
                 continue;
             }
         }
         if ($group->is_internal) {
             continue;
         }
         $this->possibleSelections[$group->id] = ['group' => $group, 'selections' => [], 'static_selections' => [], 'dynamic_selections' => []];
         $props = Property::getForGroupId($group->id);
         foreach ($props as $p) {
             if ($this->onlyAvailableFilters && !in_array($p->id, $data['propertyIds'])) {
                 if ($this->disableInsteadOfHide === false) {
                     continue;
                 }
             }
             if ($p->dont_filter) {
                 continue;
             }
             if ($p->has_static_values) {
                 $propertyStaticValues = PropertyStaticValues::getValuesForPropertyId($p->id);
                 foreach ($propertyStaticValues as $key => $propertyStaticValue) {
                     if ($propertyStaticValue['dont_filter']) {
                         unset($propertyStaticValues[$key]);
                     }
                 }
                 if ($this->onlyAvailableFilters) {
                     foreach ($propertyStaticValues as $key => $propertyStaticValue) {
                         if (!in_array($propertyStaticValue['id'], $data['propertyStaticValueIds'])) {
                             if ($this->disableInsteadOfHide === true) {
                                 $this->disabled_ids[] = $propertyStaticValue['id'];
                             } else {
                                 unset($propertyStaticValues[$key]);
                             }
                         }
                     }
                 }
                 $this->possibleSelections[$group->id]['static_selections'][$p->id] = $propertyStaticValues;
             } elseif ($p->is_column_type_stored && $p->value_type == 'NUMBER') {
                 $this->possibleSelections[$group->id]['dynamic_selections'][] = $p->id;
             }
         }
         if (count($this->possibleSelections[$group->id]) === 0) {
             unset($this->possibleSelections[$group->id]);
         }
     }
 }
Example #3
0
 /**
  * @param $exportFields
  * @param array $conditions
  * @param int $batchSize
  * @return bool
  * @throws \Exception
  */
 public function processExport($exportFields = [], $conditions = [], $batchSize = 100)
 {
     $fields = $this->getAllFields($exportFields);
     $class = $this->object->object_class;
     /** @var $select ActiveQuery */
     $select = $class::find();
     $representationConversions = ['text' => 'name', 'value' => 'value', 'id' => 'psv_id'];
     if (isset($conditions['category']) && is_array($conditions['category']) && $this->object->id == Object::getForClass(Product::className())->id) {
         foreach ($conditions['category'] as $condition) {
             $joinTableName = 'Category' . $condition['value'];
             $select->innerJoin("{{%product_category}} " . $joinTableName, "{$joinTableName}.object_model_id = product.id");
             $select->andWhere(new Expression('`' . $joinTableName . '`.`category_id` = "' . $condition['value'] . '"'));
         }
     }
     if (isset($conditions['field']) && is_array($conditions['field'])) {
         foreach ($conditions['field'] as $condition) {
             $conditionOptions = [$condition['operators'], $condition['value'], $condition['option']];
             if ($condition['comparison'] == 'AND') {
                 $select->andWhere($conditionOptions);
             } elseif ($condition['comparison'] == 'OR') {
                 $select->orWhere($conditionOptions);
             }
         }
     }
     if (isset($conditions['property']) && is_array($conditions['property'])) {
         foreach ($conditions['property'] as $condition) {
             $property = Property::findById($condition['value']);
             if ($property && isset($condition['option']) && !empty($condition['option'])) {
                 if ($property->is_eav) {
                     $joinTableName = 'EAVJoinTable' . $property->id;
                     $select->innerJoin($this->object->eav_table_name . " " . $joinTableName, "{$joinTableName}.object_model_id = " . Yii::$app->db->quoteTableName($this->object->object_table_name) . ".id ");
                     $select->andWhere(new Expression('`' . $joinTableName . '`.`value` ' . $condition['operators'] . ' "' . $condition['option'] . '" AND `' . $joinTableName . '`.`key` = "' . $property->key . '"'));
                 } elseif ($property->has_static_values) {
                     $joinTableName = 'OSVJoinTable' . $property->id;
                     $propertyStaticValue = PropertyStaticValues::find()->where(['value' => $condition['option']])->one();
                     if ($propertyStaticValue) {
                         $select->innerJoin(ObjectStaticValues::tableName() . " " . $joinTableName, "{$joinTableName}.object_id = " . intval($this->object->id) . " AND {$joinTableName}.object_model_id = " . Yii::$app->db->quoteTableName($this->object->object_table_name) . ".id ");
                         $select->andWhere(new Expression('`' . $joinTableName . '`.`property_static_value_id` ="' . $propertyStaticValue->id . '"'));
                     }
                 } else {
                     throw new \Exception("Wrong property type for " . $property->id);
                 }
             }
         }
     }
     $data = [];
     $batchSize = intval($batchSize) <= 0 ? 100 : intval($batchSize);
     foreach ($select->each($batchSize) as $object) {
         $row = [];
         foreach ($fields['fields_object'] as $field) {
             if ('internal_id' === $field) {
                 $row[] = $object->id;
             } else {
                 $row[] = isset($object->{$field}) ? $object->{$field} : '';
             }
         }
         foreach ($fields['fields_property'] as $field_id => $field) {
             $value = $object->getPropertyValuesByPropertyId($field_id);
             if (!is_object($value)) {
                 $value = '';
             } elseif (count($value->values) > 1 && isset($fields_property[$field_id])) {
                 if (isset($fields_property[$field_id]['processValuesAs'])) {
                     $attributeToGet = $representationConversions[$fields_property[$field_id]['processValuesAs']];
                     $newValues = [];
                     foreach ($value->values as $val) {
                         $newValues[] = $val[$attributeToGet];
                     }
                     $value = implode($this->multipleValuesDelimiter, $newValues);
                 }
             } else {
                 $value = (string) $value;
             }
             $row[] = $value;
         }
         if (!empty($fields['fields_additional']) && $object->hasMethod('getAdditionalFields')) {
             $fieldsFromModel = $object->getAdditionalFields($fields['fields_additional']);
             foreach ($fields['fields_additional'] as $key => $configuration) {
                 if (!isset($fieldsFromModel[$key])) {
                     $fieldsFromModel[$key] = '';
                 }
                 if (!empty($fieldsFromModel[$key])) {
                     $value = (array) $fieldsFromModel[$key];
                     $row[] = implode($this->multipleValuesDelimiter, $value);
                 } else {
                     $row[] = '';
                 }
             }
         }
         $data[] = $row;
     }
     unset($value, $row, $object, $select, $class);
     return $this->getData($fields['fields_header'], $data);
 }
 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]);
 }
Example #5
0
 /**
  * @inheritdoc
  */
 public function appendPart($route, $parameters = [], &$used_params = [], &$cacheTags = [])
 {
     if (isset($parameters['properties'])) {
         $used_params[] = 'properties';
         if (isset($parameters['properties'][$this->property_id]) && is_array($parameters['properties'][$this->property_id])) {
             $property_id = $this->property_id;
             $psvs = Yii::$app->db->cache(function ($db) use($property_id, $parameters) {
                 $vals = array_values($parameters['properties'][$property_id]);
                 $vals = array_map(function ($item) {
                     return intval($item);
                 }, $vals);
                 return PropertyStaticValues::find()->where(['id' => $vals])->orderBy('sort_order ASC, name ASC')->asArray(true)->all();
             }, 86400, new TagDependency(['tags' => [ActiveRecordHelper::getCommonTag(PropertyStaticValues::className())]]));
             foreach ($psvs as $psv) {
                 if (count($this->include_if_value) > 0) {
                     if (!in_array($psv['value'], $this->include_if_value)) {
                         return false;
                     }
                 }
             }
             if (!empty($psvs)) {
                 foreach ($psvs as $psv) {
                     $cacheTags[] = ActiveRecordHelper::getObjectTag(PropertyStaticValues::className(), $psv['id']);
                 }
                 return implode('/', ArrayHelper::getColumn($psvs, 'slug'));
             } else {
                 return false;
             }
         } else {
             return $this->checkIncludeIfValue();
         }
     } else {
         return $this->checkIncludeIfValue();
     }
 }