/** * Clear and set type attributes * @param $attributes array of attributes id. array(1,3,5) * @return mixed */ public function useAttributes($attributes) { // Clear all relations ShopTypeAttribute::model()->deleteAllByAttributes(array('type_id' => $this->id)); if (empty($attributes)) { return false; } foreach ($attributes as $attribute_id) { if ($attribute_id) { $record = new ShopTypeAttribute(); $record->type_id = $this->id; $record->attribute_id = $attribute_id; $record->save(false, false, false); } } }
/** * Import product properties */ public function importProperties() { foreach ($this->xml->{"Классификатор"}->{"Свойства"}->{"Свойство"} as $attribute) { $model = C1ExternalFinder::getObject(C1ExternalFinder::OBJECT_TYPE_ATTRIBUTE, $attribute->{"Ид"}); if ($attribute->{"ЭтоФильтр"} == 'false') { $useInFilter = false; } else { $useInFilter = true; } if (!$model) { // Create new attribute $model = new ShopAttribute(); $model->name = CMS::translit($attribute->{"Наименование"}); $model->name = str_replace('-', '_', $model->name); $model->title = $attribute->{"Наименование"}; $model->type = ShopAttribute::TYPE_DROPDOWN; $model->use_in_filter = $useInFilter; $model->display_on_front = true; if ($model->save(false, false)) { // Add to type $typeAttribute = new ShopTypeAttribute(); $typeAttribute->type_id = self::DEFAULT_TYPE; $typeAttribute->attribute_id = $model->id; $typeAttribute->save(false, false); $this->createExternalId(C1ExternalFinder::OBJECT_TYPE_ATTRIBUTE, $model->id, $attribute->{"Ид"}); } } // Update attributes $model->name = CMS::translit($attribute->{"Наименование"}); $model->use_in_filter = $useInFilter; $model->save(); } }
/** * @param $name * @return ShopAttribute */ public function getAttributeByName($name) { if (isset($this->attributesCache[$name])) { return $this->attributesCache[$name]; } $attribute = ShopAttribute::model()->findByAttributes(array('name' => $name)); if (!$attribute) { // Create new attribute $attribute = new ShopAttribute(); $attribute->name = $name; $attribute->title = ucfirst(str_replace('_', ' ', $name)); $attribute->type = ShopAttribute::TYPE_DROPDOWN; $attribute->display_on_front = true; $attribute->save(false, false, false); // Add to type $typeAttribute = new ShopTypeAttribute(); $typeAttribute->type_id = $this->model->type_id; $typeAttribute->attribute_id = $attribute->id; $typeAttribute->save(false, false, false); } $this->attributesCache[$name] = $attribute; return $attribute; }