/** * @return array of used attribute models */ public function getModels() { if (is_array($this->_models)) { return $this->_models; } $this->_models = array(); $cr = new CDbCriteria(); $cr->addInCondition('StoreAttribute.name', array_keys($this->_attributes)); $query = StoreAttribute::model()->displayOnFront()->findAll($cr); foreach ($query as $m) { $this->_models[$m->name] = $m; } return $this->_models; }
/** * @static * @param $type * @param $externalId * @param bool $loadModel */ public static function getObject($type, $externalId, $loadModel = true) { $query = Yii::app()->db->createCommand()->select("*")->from('accounting1c')->where('object_type=:type AND external_id=:externalId', array(':type' => $type, ':externalId' => $externalId))->limit(1)->queryRow(); if ($query === false) { return false; } if ($loadModel === true && $query['object_id']) { switch ($type) { case self::OBJECT_TYPE_CATEGORY: return StoreCategory::model()->findByPk($query['object_id']); break; case self::OBJECT_TYPE_ATTRIBUTE: return StoreAttribute::model()->findByPk($query['object_id']); break; case self::OBJECT_TYPE_PRODUCT: return StoreProduct::model()->findByPk($query['object_id']); break; } } return $query['object_id']; }
/** * Get data to render dropdowns for configurable product. * Used on product view. * array( * 'attributes' // Array of StoreAttribute models used for configurations * 'prices' // Key/value array with configurations prices array(product_id=>price) * 'data' // Array to render dropdowns. array(color=>array('Green'=>'1/3/5/', 'Silver'=>'7/')) * ) * @todo Optimize. Cache queries. * @return array */ public function getConfigurableData() { $attributeModels = StoreAttribute::model()->findAllByPk($this->model->configurable_attributes); $models = StoreProduct::model()->findAllByPk($this->model->configurations); $data = array(); $prices = array(); foreach ($attributeModels as $attr) { foreach ($models as $m) { $prices[$m->id] = $m->price; if (!isset($data[$attr->name])) { $data[$attr->name] = array('---' => '0'); } $method = 'eav_' . $attr->name; $value = $m->{$method}; if (!isset($data[$attr->name][$value])) { $data[$attr->name][$value] = ''; } $data[$attr->name][$value] .= $m->id . '/'; } } return array('attributes' => $attributeModels, 'prices' => $prices, 'data' => $data); }
/** * Update product type * @param bool $new * @throws CHttpException */ public function actionUpdate($new = false) { if ($new === true) { $model = new StoreProductType(); } else { $model = StoreProductType::model()->findByPk($_GET['id']); } if (!$model) { throw new CHttpException(404, Yii::t('StoreModule.admin', 'Тип продукта не найден.')); } if (Yii::app()->request->isPostRequest) { $model->attributes = $_POST['StoreProductType']; if (isset($_POST['categories']) && !empty($_POST['categories'])) { $model->categories_preset = serialize($_POST['categories']); $model->main_category = $_POST['main_category']; } else { //return defaults when all checkboxes were checked off $model->categories_preset = null; $model->main_category = 0; } if ($model->validate()) { $model->save(); // Set type attributes $model->useAttributes(Yii::app()->request->getPost('attributes', array())); $this->setFlashMessage(Yii::t('StoreModule.admin', 'Изменения успешно сохранены')); if (isset($_POST['REDIRECT'])) { $this->smartRedirect($model); } else { $this->redirect('create'); } } } // Select available(not used) attributes $cr = new CDbCriteria(); $cr->addNotInCondition('StoreAttribute.id', CHtml::listData($model->attributeRelation, 'attribute_id', 'attribute_id')); $allAttributes = StoreAttribute::model()->findAll($cr); $this->render('update', array('model' => $model, 'attributes' => $allAttributes)); }
/** * @param string $eav_prefix * @return array */ public function getImportableAttributes($eav_prefix = '') { $attributes = array('type' => Yii::t('StoreModule.core', 'Тип'), 'name' => Yii::t('StoreModule.core', 'Название'), 'category' => Yii::t('StoreModule.core', 'Категория'), 'additionalCategories' => Yii::t('StoreModule.core', 'Доп. Категории'), 'manufacturer' => Yii::t('StoreModule.core', 'Производитель'), 'sku' => Yii::t('StoreModule.core', 'Артикул'), 'url' => Yii::t('StoreModule.core', 'URL'), 'price' => Yii::t('StoreModule.core', 'Цена'), 'is_active' => Yii::t('StoreModule.core', 'Активен'), 'image' => Yii::t('StoreModule.core', 'Главное изображение'), 'short_description' => Yii::t('StoreModule.core', 'Краткое описание'), 'full_description' => Yii::t('StoreModule.core', 'Полное описание'), 'meta_title' => Yii::t('StoreModule.core', 'Meta Title'), 'meta_keywords' => Yii::t('StoreModule.core', 'Meta Keywords'), 'meta_description' => Yii::t('StoreModule.core', 'Meta Description'), 'layout' => Yii::t('StoreModule.core', 'Макет'), 'view' => Yii::t('StoreModule.core', 'Шаблон'), 'quantity' => Yii::t('StoreModule.core', 'Количество'), 'availability' => Yii::t('StoreModule.core', 'Доступность'), 'created' => Yii::t('StoreModule.core', 'Дата создания'), 'updated' => Yii::t('StoreModule.core', 'Дата обновления')); foreach (StoreAttribute::model()->findAll() as $attr) { $attributes[$eav_prefix . $attr->name] = $attr->title; } return $attributes; }
/** * Allows to access EAV attributes like normal model attrs. * e.g $model->eav_some_attribute_name * * @todo Optimize, cache. * @param $name * @return null */ public function __get($name) { if (substr($name, 0, 4) === 'eav_') { if ($this->getIsNewRecord()) { return null; } $attribute = substr($name, 4); $eavData = $this->getEavAttributes(); if (isset($eavData[$attribute])) { $value = $eavData[$attribute]; } else { return null; } $attributeModel = StoreAttribute::model()->findByAttributes(array('name' => $attribute)); return $attributeModel->renderValue($value); } return parent::__get($name); }
/** * @return array of available attributes in category */ public function getEavAttributes() { if (is_array($this->_eavAttributes)) { return $this->_eavAttributes; } // Find category types $model = new StoreProduct(null); $criteria = $model->applyCategories($this->model)->active()->getDbCriteria(); unset($model); $builder = new CDbCommandBuilder(Yii::app()->db->getSchema()); $criteria->select = 'type_id'; $criteria->group = 'type_id'; $criteria->distinct = true; $typesUsed = $builder->createFindCommand(StoreProduct::model()->tableName(), $criteria)->queryColumn(); // Find attributes by type $criteria = new CDbCriteria(); $criteria->addInCondition('types.type_id', $typesUsed); $query = StoreAttribute::model()->useInFilter()->with(array('types', 'options'))->findAll($criteria); $this->_eavAttributes = array(); foreach ($query as $attr) { $this->_eavAttributes[$attr->name] = $attr; } return $this->_eavAttributes; }
* Confirutable products tab * * @var Controller $this * @var StoreProduct $product Current product * @var StoreProduct $model */ Yii::app()->clientScript->registerScriptFile($this->module->assetsUrl . '/admin/products.configurations.js'); // For grid view we use new products instance $model = new StoreProduct(); if (isset($_GET['ConfProduct'])) { $model->attributes = $_GET['ConfProduct']; } $columns = array(array('class' => 'CCheckBoxColumn', 'checked' => !empty($product->configurations) && !isset($clearConfigurations) && !$product->isNewRecord ? 'true' : 'false'), array('name' => 'id', 'type' => 'text', 'value' => '$data->id', 'filter' => CHtml::textField('ConfProduct[id]', $model->id)), array('name' => 'name', 'type' => 'raw', 'value' => 'CHtml::link(CHtml::encode($data->name), array("update", "id"=>$data->id), array("target"=>"_blank"))', 'filter' => CHtml::textField('ConfProduct[name]', $model->name)), array('name' => 'sku', 'value' => '$data->sku', 'filter' => CHtml::textField('ConfProduct[sku]', $model->sku)), array('name' => 'price', 'value' => '$data->price', 'filter' => CHtml::textField('ConfProduct[price]', $model->price))); // Process attributes $eavAttributes = array(); $attributeModels = StoreAttribute::model(); $attributeModels->setTableAlias('StoreAttribute'); $attributeModels = $attributeModels->findAllByPk($product->configurable_attributes); foreach ($attributeModels as $attribute) { $selected = null; if (isset($_GET['eav'][$attribute->name]) && !empty($_GET['eav'][$attribute->name])) { $eavAttributes[$attribute->name] = $_GET['eav'][$attribute->name]; $selected = $_GET['eav'][$attribute->name]; } else { array_push($eavAttributes, $attribute->name); } $columns[] = array('name' => 'eav_' . $attribute->name, 'header' => $attribute->title, 'htmlOptions' => array('class' => 'eav'), 'filter' => CHtml::dropDownList('eav[' . $attribute->name . ']', $selected, CHtml::listData($attribute->options, 'id', 'value'), array('empty' => '---'))); } if (!empty($eavAttributes)) { $model = $model->withEavAttributes($eavAttributes); }
/** * @param $name * @return StoreAttribute */ public function getAttributeByName($name) { if (isset($this->attributesCache[$name])) { return $this->attributesCache[$name]; } $attribute = StoreAttribute::model()->findByAttributes(array('name' => $name)); if (!$attribute) { // Create new attribute $attribute = new StoreAttribute(); $attribute->name = $name; $attribute->title = ucfirst(str_replace('_', ' ', $name)); $attribute->type = StoreAttribute::TYPE_DROPDOWN; $attribute->display_on_front = true; $attribute->save(false); // Add to type $typeAttribute = new StoreTypeAttribute(); $typeAttribute->type_id = $this->model->type_id; $typeAttribute->attribute_id = $attribute->id; $typeAttribute->save(false); } $this->attributesCache[$name] = $attribute; return $attribute; }
/** * Load StoreAttribute models by names * @return array of StoreAttribute models */ public function getAttributes() { if ($this->_attributes === null) { $this->_attributes = array(); $names = array(); foreach ($this->getProducts() as $p) { $names = array_merge($names, array_keys($p->getEavAttributes())); } $cr = new CDbCriteria(); $cr->addInCondition('StoreAttribute.name', $names); $query = StoreAttribute::model()->displayOnFront()->useInCompare()->findAll($cr); foreach ($query as $m) { $this->_attributes[$m->name] = $m; } } return $this->_attributes; }
// Price echo CHtml::openTag('span', array('class' => 'amount')); echo StoreProduct::formatPrice(Yii::app()->currency->convert($price)); echo ' ' . Yii::app()->currency->active->symbol; echo CHtml::closeTag('span'); // Display variant options if (!empty($product['variant_models'])) { echo CHtml::openTag('span', array('class' => 'cartProductOptions')); foreach ($product['variant_models'] as $variant) { echo ' - ' . $variant->attribute->title . ': ' . $variant->option->value . '<br/>'; } echo CHtml::closeTag('span'); } // Display configurable options if (isset($product['configurable_model'])) { $attributeModels = StoreAttribute::model()->findAllByPk($product['model']->configurable_attributes); echo CHtml::openTag('span', array('class' => 'cartProductOptions')); foreach ($attributeModels as $attribute) { $method = 'eav_' . $attribute->name; echo ' - ' . $attribute->title . ': ' . $product['configurable_model']->{$method} . '<br/>'; } echo CHtml::closeTag('span'); } ?> </td> <td> <div class="quantity buttons_added"> <button class="small_silver_button plus">+</button> <?php echo CHtml::numberField("quantities[{$index}]", $product['quantity'], array('class' => 'count')); ?>
/** * Delete attribute * @param array $id */ public function actionDelete($id = array()) { if (Yii::app()->request->isPostRequest) { $model = StoreAttribute::model()->findAllByPk($_REQUEST['id']); if (!empty($model)) { foreach ($model as $m) { $count = StoreProduct::model()->withEavAttributes(array($m->name))->count(); if ($count) { throw new CHttpException(503, Yii::t('StoreModule.admin', 'Ошибка удаления атрибута - он используется продуктами.')); } $m->delete(); } } if (!Yii::app()->request->isAjaxRequest) { $this->redirect('index'); } } }
/** * Add option to store attribute * * @throws CHttpException */ public function actionAddOptionToAttribute() { $attribute = StoreAttribute::model()->findByPk($_GET['attr_id']); if (!$attribute) { throw new CHttpException(404, Yii::t('StoreModule.admin', 'Ошибка загрузки атрибута')); } $attributeOption = new StoreAttributeOption(); $attributeOption->attribute_id = $attribute->id; $attributeOption->value = $_GET['value']; $attributeOption->save(false); echo $attributeOption->id; }
/** * Create new order * @return Order */ public function createOrder() { if (Yii::app()->cart->countItems() == 0) { return false; } $order = new Order(); // Set main data $order->user_id = Yii::app()->user->isGuest ? null : Yii::app()->user->id; $order->user_name = $this->form->name; $order->user_email = $this->form->email; $order->user_phone = $this->form->phone; $order->user_address = $this->form->address; $order->user_comment = $this->form->comment; $order->delivery_id = $this->form->delivery_id; if ($order->validate()) { $order->save(); } else { throw new CHttpException(503, Yii::t('OrdersModule.core', 'Ошибка создания заказа')); } // Process products foreach (Yii::app()->cart->getDataWithModels() as $item) { $ordered_product = new OrderProduct(); $ordered_product->order_id = $order->id; $ordered_product->product_id = $item['model']->id; $ordered_product->configurable_id = $item['configurable_id']; $ordered_product->name = $item['model']->name; $ordered_product->quantity = $item['quantity']; $ordered_product->sku = $item['model']->sku; $ordered_product->price = StoreProduct::calculatePrices($item['model'], $item['variant_models'], $item['configurable_id']); // Process configurable product if (isset($item['configurable_model']) && $item['configurable_model'] instanceof StoreProduct) { $configurable_data = array(); $ordered_product->configurable_name = $item['configurable_model']->name; // Use configurable product sku $ordered_product->sku = $item['configurable_model']->sku; // Save configurable data $attributeModels = StoreAttribute::model()->findAllByPk($item['model']->configurable_attributes); foreach ($attributeModels as $attribute) { $method = 'eav_' . $attribute->name; $configurable_data[$attribute->title] = $item['configurable_model']->{$method}; } $ordered_product->configurable_data = serialize($configurable_data); } // Save selected variants as key/value array if (!empty($item['variant_models'])) { $variants = array(); foreach ($item['variant_models'] as $variant) { $variants[$variant->attribute->title] = $variant->option->value; } $ordered_product->variants = serialize($variants); } $ordered_product->save(); } // Reload order data. $order->refresh(); // All products added. Update delivery price. $order->updateDeliveryPrice(); // Send email to user. $this->sendEmail($order); return $order; }