Example #1
0
 /**
  * @inheritdoc
  */
 public function getNextPart($full_url, $next_part, &$previous_parts)
 {
     $property = Property::findById($this->property_id);
     if (is_null($property)) {
         return false;
     }
     if ($property->has_static_values && $property->has_slugs_in_values) {
         $cacheTags = [];
         $static_values = PropertyStaticValues::getValuesForPropertyId($this->property_id);
         $slugs = explode('/', $next_part);
         foreach ($static_values as $value) {
             if ($slugs[0] === $value['slug']) {
                 $this->parameters = ['properties' => [$this->property_id => [$value['id']]]];
                 if (!empty($value['title_append'])) {
                     $this->parameters['title_append'] = [$value['title_append']];
                 }
                 $cacheTags[] = ActiveRecordHelper::getObjectTag(PropertyStaticValues::className(), $value['id']);
                 $part = new self(['gathered_part' => $value['slug'], 'rest_part' => mb_substr($next_part, mb_strlen($value['slug'])), 'parameters' => $this->parameters, 'cacheTags' => $cacheTags]);
                 return $part;
             }
         }
         return false;
     } else {
         return false;
     }
 }
Example #2
0
 /**
  * @inheritdoc
  */
 public function getNextPart($full_url, $next_part, &$previous_parts)
 {
     $property = Property::findById($this->property_id);
     if (is_null($property)) {
         return false;
     }
     if ($property->has_static_values && $property->has_slugs_in_values) {
         $cacheTags = [];
         $static_values = PropertyStaticValues::getValuesForPropertyId($this->property_id);
         $slugs = explode('/', $next_part);
         $currentSlug = 0;
         $slugsCount = count($slugs);
         $appliedParts = [];
         foreach ($static_values as $value) {
             if ($slugs[$currentSlug] === $value['slug']) {
                 $appliedParts[] = $value['slug'];
                 if (isset($this->parameters['properties'][$this->property_id])) {
                     $this->parameters['properties'][$this->property_id][] = $value['id'];
                 } else {
                     $this->parameters = ['properties' => [$this->property_id => [$value['id']]]];
                 }
                 if (!empty($value['title_append'])) {
                     if ($value["title_prepend"] == 1) {
                         $this->addParam("title_prepend", $value["title_append"]);
                     } else {
                         $this->addParam("title_append", $value["title_append"]);
                     }
                 }
                 $cacheTags[] = ActiveRecordHelper::getObjectTag(PropertyStaticValues::className(), $value['id']);
                 $currentSlug++;
                 if ($currentSlug == $slugsCount) {
                     break;
                 }
             }
         }
         if ($currentSlug > 0) {
             $appliedPartsString = implode('/', $appliedParts);
             $part = new self(['gathered_part' => $appliedPartsString, 'rest_part' => mb_substr($next_part, mb_strlen($appliedPartsString)), 'parameters' => $this->parameters, 'cacheTags' => $cacheTags]);
             return $part;
         }
         return false;
     } else {
         return false;
     }
 }
Example #3
0
                                                        </td>
                                                        <td>
                                                            <?php 
                echo Yii::t('app', 'Slug');
                ?>
                                                        </td>
                                                        <td>
                                                            <?php 
                echo Yii::t('app', 'Display in filter');
                ?>
                                                        </td>
                                                    </tr>
                                                    </thead>
                                                    <tbody>
                                                    <?php 
                $staticValues = \app\models\PropertyStaticValues::getValuesForPropertyId($property->id);
                foreach ($staticValues as $staticValue) {
                    ?>
                                                        <tr psv-id="<?php 
                    echo $staticValue['id'];
                    ?>
">
                                                            <td class="td-no-padding">
                                                                <span class="psv-sort-handle">
                                                                    <?php 
                    echo Icon::show('arrows-v');
                    ?>
                                                                </span>
                                                            </td>
                                                            <td>
                                                                <?php 
Example #4
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]);
         }
     }
 }
 public static function getSelectForPropertyId($property_id)
 {
     $values = PropertyStaticValues::getValuesForPropertyId($property_id);
     $result = [];
     foreach ($values as $row) {
         $result[$row['id']] = $row['name'];
     }
     return $result;
 }
Example #6
0
 /**
  * @param $objectId
  * @param $object
  * @param array $objectFields
  * @param array $properties
  * @param array $propertiesFields
  * @param array $row
  * @param array $titleFields
  * @throws \Exception
  */
 protected function save($objectId, $object, $objectFields = [], $properties = [], $propertiesFields = [], $row = [], $titleFields = [], $columnsCount = null)
 {
     if ($columnsCount === null) {
         $columnsCount = count($titleFields);
     }
     try {
         $rowFields = array_combine(array_keys($titleFields), array_slice($row, 0, $columnsCount));
     } catch (\Exception $e) {
         echo "title fields: ";
         var_dump(array_keys($titleFields));
         echo "\n\nRow:";
         var_dump($row);
         echo "\n\n";
         throw $e;
     }
     $class = $this->object->object_class;
     if ($objectId > 0) {
         /** @var ActiveRecord $objectModel */
         $objectModel = $class::findOne($objectId);
         if (!is_object($objectModel)) {
             if ($this->createIfNotExists === true) {
                 $objectModel = new $class();
                 $objectModel->id = $objectId;
             } else {
                 return;
             }
         }
         $objectData = [];
         foreach ($objectFields as $field) {
             if (isset($object[$field])) {
                 $objectData[$field] = $object[$field];
             }
         }
     } else {
         /** @var ActiveRecord $objectModel */
         $objectModel = new $class();
         $objectModel->loadDefaultValues();
         $objectData = $object;
     }
     if ($objectModel) {
         if ($objectModel instanceof ImportableInterface) {
             $objectModel->processImportBeforeSave($rowFields, $this->multipleValuesDelimiter, $this->additionalFields);
         }
         if ($objectModel->save()) {
             // add PropertyGroup to object
             if (!is_array($this->addPropertyGroups)) {
                 $this->addPropertyGroups = [];
             }
             foreach ($this->addPropertyGroups as $propertyGroupId) {
                 $model = new ObjectPropertyGroup();
                 $model->object_id = $this->object->id;
                 $model->object_model_id = $objectModel->id;
                 $model->property_group_id = $propertyGroupId;
                 $model->save();
             }
             if (count($this->addPropertyGroups) > 0) {
                 $objectModel->updatePropertyGroupsInformation();
             }
             $propertiesData = [];
             $objectModel->getPropertyGroups();
             foreach ($propertiesFields as $propertyId => $field) {
                 if (isset($properties[$field['key']])) {
                     $value = $properties[$field['key']];
                     if (isset($field['processValuesAs'])) {
                         // it is PSV in text
                         // we should convert it to ids
                         $staticValues = PropertyStaticValues::getValuesForPropertyId($propertyId);
                         $representationConversions = ['text' => 'name', 'value' => 'value', 'id' => 'id'];
                         $attributeToGet = $representationConversions[$field['processValuesAs']];
                         $ids = [];
                         foreach ($value as $initial) {
                             $original = $initial;
                             $initial = mb_strtolower(trim($original));
                             $added = false;
                             foreach ($staticValues as $static) {
                                 if (mb_strtolower(trim($static[$attributeToGet])) === $initial) {
                                     $ids[] = $static['id'];
                                     $added = true;
                                 }
                             }
                             if (!$added) {
                                 // create PSV!
                                 $model = new PropertyStaticValues();
                                 $model->property_id = $propertyId;
                                 $model->name = $model->value = $model->slug = $original;
                                 $model->sort_order = 0;
                                 $model->title_append = '';
                                 if ($model->save()) {
                                     $ids[] = $model->id;
                                 }
                                 //flush cache!
                                 unset(PropertyStaticValues::$identity_map_by_property_id[$propertyId]);
                                 \yii\caching\TagDependency::invalidate(Yii::$app->cache, [\devgroup\TagDependencyHelper\ActiveRecordHelper::getObjectTag(Property::className(), $propertyId)]);
                             }
                         }
                         $value = $ids;
                     }
                     $propertiesData[$field['key']] = $value;
                 }
             }
             if (!empty($propertiesData)) {
                 $objectModel->saveProperties(["Properties_{$objectModel->formName()}_{$objectModel->id}" => $propertiesData]);
             }
             if ($objectModel instanceof ImportableInterface) {
                 $objectModel->processImportAfterSave($rowFields, $this->multipleValuesDelimiter, $this->additionalFields);
             }
             if ($objectModel->hasMethod('invalidateTags')) {
                 $objectModel->invalidateTags();
             }
         } else {
             throw new \Exception('Cannot save object: ' . var_export($objectModel->errors, true) . var_export($objectData, true) . var_export($objectModel->getAttributes(), true));
         }
     }
 }