public function afterUninstall() { Yii::app()->settings->clear('shop'); Yii::app()->unintallComponent('currency'); $db = Yii::app()->db; $tablesArray = array(ShopTypeAttribute::model()->tableName(), ShopAttribute::model()->tableName(), ShopAttributeOption::model()->tableName(), ShopAttributeOptionTranslate::model()->tableName(), ShopAttributeTranslate::model()->tableName(), ShopCategory::model()->tableName(), ShopCategoryTranslate::model()->tableName(), ShopCurrency::model()->tableName(), ShopManufacturer::model()->tableName(), ShopManufacturerTranslate::model()->tableName(), ShopProduct::model()->tableName(), ShopProductCategoryRef::model()->tableName(), ShopProductImage::model()->tableName(), ShopProductTranslate::model()->tableName(), ShopProductType::model()->tableName(), ShopProductVariant::model()->tableName(), ShopRelatedProduct::model()->tableName(), ShopSuppliers::model()->tableName(), $db->tablePrefix . 'shop_product_attribute_eav', $db->tablePrefix . 'shop_product_configurable_attributes', $db->tablePrefix . 'shop_product_configurations'); foreach ($tablesArray as $table) { $db->createCommand()->dropTable($table); } CFileHelper::removeDirectory(Yii::getPathOfAlias('webroot.uploads.product'), array('traverseSymlinks' => true)); CFileHelper::removeDirectory(Yii::getPathOfAlias('webroot.uploads.categories'), array('traverseSymlinks' => true)); CFileHelper::removeDirectory(Yii::getPathOfAlias('webroot.uploads.manufacturer'), array('traverseSymlinks' => true)); return parent::afterUninstall(); }
/** * @static * @param $type * @param $externalId * @param bool $loadModel */ public static function getObject($type, $externalId, $loadModel = true) { $query = Yii::app()->db->createCommand()->select("*")->from('{{exchange1c}}')->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 ShopCategory::model()->findByPk($query['object_id']); break; case self::OBJECT_TYPE_ATTRIBUTE: return ShopAttribute::model()->findByPk($query['object_id']); break; case self::OBJECT_TYPE_PRODUCT: return ShopProduct::model()->findByPk($query['object_id']); break; } } return $query['object_id']; }
/** * Update product type * @param bool $new * @throws CHttpException */ public function actionUpdate($new = false) { if ($new === true) { $model = new ShopProductType(); } else { $model = ShopProductType::model()->findByPk($_GET['id']); } if (!$model) { throw new CHttpException(404, Yii::t('ShopModule.admin', 'NO_FOUND_TYPEPRODUCT')); } Yii::app()->clientScript->registerScriptFile($this->module->assetsUrl . '/admin/productType.update.js', CClientScript::POS_END); $this->pageName = $model->isNewRecord ? Yii::t('ShopModule.admin', 'Создание нового типа продукта') : Yii::t('ShopModule.admin', 'Редактирование типа продукта'); $this->breadcrumbs = array(Yii::t('ShopModule.default', 'MODULE_NAME') => array('/admin/shop'), Yii::t('ShopModule.admin', 'TYPE_PRODUCTS') => $this->createUrl('index'), $this->pageName); if (Yii::app()->request->isPostRequest) { $model->attributes = $_POST['ShopProductType']; 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->redirect('index'); } } // Select available(not used) attributes //die(print_r(Html::listData($model->attributeRelation, 'attribute_id', 'attribute_id'))); $cr = new CDbCriteria(); $cr->addNotInCondition('t.id', Html::listData($model->attributeRelation, 'attribute_id', 'attribute_id')); //ShopAttribute.id $allAttributes = ShopAttribute::model()->findAll($cr); $this->render('update', array('model' => $model, 'attributes' => $allAttributes)); }
* Confirutable products tab * * @var Controller $this * @var ShopProduct $product Current product * @var ShopProduct $model */ Yii::app()->clientScript->registerScriptFile($this->module->assetsUrl . '/admin/products.configurations.js'); // For grid view we use new products instance $model = new ShopProduct(); if (isset($_GET['ConfProduct'])) { $model->attributes = $_GET['ConfProduct']; } $columns = array(array('class' => 'CheckBoxColumn', 'checked' => !empty($product->configurations) && !isset($clearConfigurations) && !$product->isNewRecord ? 'true' : 'false'), array('name' => 'id', 'type' => 'text', 'value' => '$data->id', 'filter' => Html::textField('ConfProduct[id]', $model->id)), array('name' => 'name', 'type' => 'raw', 'value' => 'Html::link(Html::encode($data->name), array("update", "id"=>$data->id), array("target"=>"_blank"))', 'filter' => Html::textField('ConfProduct[name]', $model->name)), array('name' => 'sku', 'value' => '$data->sku', 'filter' => Html::textField('ConfProduct[sku]', $model->sku)), array('name' => 'price', 'value' => '$data->price', 'filter' => Html::textField('ConfProduct[price]', $model->price))); // Process attributes $eavAttributes = array(); $attributeModels = ShopAttribute::model(); $attributeModels->setTableAlias('ShopAttribute'); $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); }
/** * Load ShopAttribute models by names * @return array of ShopAttribute 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('t.name', $names); $query = ShopAttribute::model()->displayOnFront()->useInCompare()->findAll($cr); foreach ($query as $m) { $this->_attributes[$m->name] = $m; } } return $this->_attributes; }
/** * @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; }
/** * 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 = ShopAttribute::model()->findByAttributes(array('name' => $attribute)); return $attributeModel->renderValue($value); } return parent::__get($name); }
/** * Create new order * @return Order */ public function createOrder() { if (Yii::app()->cart->countItems() == 0) { return false; } Yii::import('mod.cart.models.Order'); Yii::import('mod.cart.models.OrderProduct'); $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; $order->payment_id = $this->form->payment_id; if ($order->validate()) { if ($order->save()) { } } else { throw new CHttpException(503, Yii::t('CartModule.default', 'ERROR_CREATE_ORDER')); } // 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->currency_id = $item['model']->currency_id; $ordered_product->supplier_id = $item['model']->supplier_id; $ordered_product->name = $item['model']->name; $ordered_product->quantity = $item['quantity']; $ordered_product->sku = $item['model']->sku; $ordered_product->date_create = $order->date_create; // if($item['currency_id']){ // $currency = ShopCurrency::model()->findByPk($item['currency_id']); //$ordered_product->price = ShopProduct::calculatePrices($item['model'], $item['variant_models'], $item['configurable_id']) * $currency->rate; // }else{ $ordered_product->price = ShopProduct::calculatePrices($item['model'], $item['variant_models'], $item['configurable_id']); // } // Process configurable product if (isset($item['configurable_model']) && $item['configurable_model'] instanceof ShopProduct) { $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 = ShopAttribute::model()->cache($this->cacheTime)->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(); //@todo panix text email tpl // All products added. Update delivery price. $order->updateDeliveryPrice(); // Send email to user. $this->sendClientEmail($order); // Send email to admin. $this->sendAdminEmail($order); if (Yii::app()->hasModule('sms')) { /* $this->widget('mod.sms.widgets.SMSWidget', array( 'key' => 'ADMIN_ORDER_BEFORE', 'model' => $order ));*/ $this->widget('mod.sms.widgets.SMSWidget', array('key' => 'CLIENT_ORDER_BEFORE', 'model' => $order)); } return $order; }
/** * @param string $eav_prefix * @return array */ public function getImportableAttributes($eav_prefix = '') { $attributes = array('type' => Yii::t('core', 'Тип'), 'name' => Yii::t('core', 'Название'), 'category' => Yii::t('core', 'Категория'), 'additionalCategories' => Yii::t('core', 'Доп. Категории'), 'manufacturer' => Yii::t('core', 'Производитель'), 'sku' => Yii::t('core', 'Артикул'), 'price' => Yii::t('core', 'Цена'), 'switch' => Yii::t('core', 'Активен'), 'images' => Yii::t('core', 'Изображения'), 'short_description' => Yii::t('core', 'Краткое описание'), 'full_description' => Yii::t('core', 'Полное описание'), 'seo_title' => Yii::t('core', 'Meta Title'), 'seo_keywords' => Yii::t('core', 'Meta Keywords'), 'seo_description' => Yii::t('core', 'Meta Description'), 'quantity' => Yii::t('core', 'Количество'), 'availability' => Yii::t('core', 'Доступность'), 'date_create' => Yii::t('core', 'Дата создания'), 'date_update' => Yii::t('core', 'Дата обновления'), 'attributes' => Yii::t('core', 'attributes')); foreach (ShopAttribute::model()->findAll() as $attr) { $attributes[$eav_prefix . $attr->name] = $attr->title; } return $attributes; }
/** * Get data to render dropdowns for configurable product. * Used on product view. * array( * 'attributes' // Array of ShopAttribute 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 = ShopAttribute::model()->cache($this->cacheTime)->findAllByPk($this->model->configurable_attributes); $models = ShopProduct::model()->cache($this->cacheTime)->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); }
public function getGridColumns() { Yii::import('mod.shop.ShopModule'); return array(array('name' => 'title', 'type' => 'raw', 'htmlOptions' => array('class' => 'textL'), 'value' => 'Html::link(Html::encode($data->title), array("/shop/admin/attribute/update", "id"=>$data->id))'), array('name' => 'name', 'value' => '$data->name'), array('name' => 'abbreviation', 'value' => '$data->abbreviation'), array('name' => 'display_on_front', 'value' => '$data->display_on_front ? Yii::t("app", "YES") : Yii::t("app", "NO")'), array('name' => 'use_in_filter', 'value' => '$data->use_in_filter ? Yii::t("app", "YES") : Yii::t("app", "NO")'), array('name' => 'select_many', 'value' => '$data->select_many ? Yii::t("app", "YES") : Yii::t("app", "NO")'), array('name' => 'use_in_variants', 'value' => '$data->use_in_variants ? Yii::t("app", "YES") : Yii::t("app", "NO")'), array('name' => 'use_in_compare', 'value' => '$data->use_in_compare ? Yii::t("app", "YES") : Yii::t("app", "NO")'), array('name' => 'type', 'filter' => ShopAttribute::getTypesList(), 'value' => 'Html::encode(ShopAttribute::getTypeTitle($data->type))'), 'DEFAULT_CONTROL' => array('class' => 'ButtonColumn', 'template' => '{update}{delete}'), 'DEFAULT_COLUMNS' => array(array('class' => 'HandleColumn'))); }
/** * @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('t.name', array_keys($this->_attributes)); $query = ShopAttribute::model()->cache($this->controller->cacheTime)->displayOnFront()->findAll($cr); foreach ($query as $m) { $this->_models[$m->name] = $m; } return $this->_models; }
echo Html::openTag('span', array('class' => 'price')); echo ShopProduct::formatPrice(Yii::app()->currency->convert($price)); echo ' ' . Yii::app()->currency->active->symbol; //echo ' '.($product['currency_id']) ? Yii::app()->currency->getSymbol($product['currency_id']) : Yii::app()->currency->active->symbol; echo Html::closeTag('span'); // Display variant options if (!empty($product['variant_models'])) { echo Html::openTag('span', array('class' => 'cartProductOptions')); foreach ($product['variant_models'] as $variant) { echo ' - ' . $variant->attribute->title . ': ' . $variant->option->value . '<br/>'; } echo Html::closeTag('span'); } // Display configurable options if (isset($product['configurable_model'])) { $attributeModels = ShopAttribute::model()->findAllByPk($product['model']->configurable_attributes); echo Html::openTag('span', array('class' => 'cartProductOptions')); foreach ($attributeModels as $attribute) { $method = 'eav_' . $attribute->name; echo ' - ' . $attribute->title . ': ' . $product['configurable_model']->{$method} . '<br/>'; } echo Html::closeTag('span'); } ?> </td> <td> <?php echo Html::textField("quantities[{$index}]", $product['quantity'], array('class' => 'spinner btn-group form-control', 'product_id' => $index)); ?>
/** * @return array of available attributes in category */ public function getEavAttributes() { if (is_array($this->_eavAttributes)) { return $this->_eavAttributes; } // Find category types $model = new ShopProduct(null); $criteria = $model->cache($this->cacheTime)->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(ShopProduct::model()->tableName(), $criteria)->queryColumn(); // Find attributes by type $criteria = new CDbCriteria(); $criteria->addInCondition('types.type_id', $typesUsed); $query = ShopAttribute::model()->cache($this->cacheTime)->useInFilter()->with(array('types', 'options'))->findAll($criteria); $this->_eavAttributes = array(); foreach ($query as $attr) { $this->_eavAttributes[$attr->name] = $attr; } return $this->_eavAttributes; }
/** * 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(); } }
/** * Add option to shop attribute * * @throws CHttpException */ public function actionAddOptionToAttribute() { $attribute = ShopAttribute::model()->findByPk($_GET['attr_id']); if (!$attribute) { throw new CHttpException(404, Yii::t('ShopModule.admin', 'ERR_LOAD_ATTR')); } $attributeOption = new ShopAttributeOption(); $attributeOption->attribute_id = $attribute->id; $attributeOption->value = $_GET['value']; $attributeOption->save(false, false, false); echo $attributeOption->id; }
/** * Delete attribute * @param array $id */ public function actionDelete($id = array()) { if (Yii::app()->request->isPostRequest) { $model = ShopAttribute::model()->findAllByPk($_REQUEST['id']); if (!empty($model)) { foreach ($model as $m) { $count = ShopProduct::model()->withEavAttributes(array($m->name))->count(); if ($count) { throw new CHttpException(503, Yii::t('ShopModule.admin', 'ERR_DEL_ATTR')); } $m->delete(); } } if (!Yii::app()->request->isAjaxRequest) { $this->redirect('index'); } } }