public function loadModel($id) { if (($model = AttributeDescription::model()->findByPk($id)) === null) { throw new CHttpException(404, 'Страница не найдена'); } return $model; }
public function actionAutocomplete($query) { // TODO: add locale $descriptions = AttributeDescription::model()->findAll("name LIKE '%{$query}%' "); foreach ($descriptions as $description) { $json[] = array('id' => $description->attribute_id, 'value' => $description->name); } echo CJSON::encode($json); }
public function actionAutocomplete($query) { $json = array(); // TODO: add locale $language_id = 1; $descriptions = AttributeDescription::model()->findAll("name LIKE '%{$query}%' AND language_id={$language_id}"); foreach ($descriptions as $description) { $json[] = array('id' => $description->attribute_id, 'value' => $description->attribute->getName(true)); } echo CJSON::encode($json); }
public function save() { $attribute = Attribute::model()->findByPk($this->id); if (is_null($attribute)) { // is insert $attribute = new Attribute(); $attribute->attribute_group_id = $this->group; $attribute->sort_order = $this->sortOrder; $attribute->save(); $attributeDescription = new AttributeDescription(); $attributeDescription->attribute_id = $attribute->attribute_id; $attributeDescription->language_id = 1; // TODO: read locale $attributeDescription->name = $this->name; $attributeDescription->save(); } else { $attribute->attribute_group_id = $this->group; $attribute->sort_order = $this->sortOrder; $attribute->save(); $attribute->description->name = $this->name; $attribute->description->save(); } }
public function actionCreate() { $model = new Attribute(); $description = new AttributeDescription(); $this->performAjaxValidation(array($model, $description), 'attribute-form'); if (isset($_POST[$this->modelName])) { $model->setAttributes($_POST[$this->modelName]); $description->setAttributes($_POST[$this->modelName . 'Description']); $suc = Yii::t('info', 'Attribute was successfully created'); $err = Yii::t('info', 'Could not update Attribute'); $description->attribute_id = 0; $description->locale_code = Yii::app()->getLanguage(); if ($model->validate() && $description->validate()) { if ($model->save()) { $description->attribute_id = $model->id; $description->save(); Yii::app()->user->setFlash(TbHtml::ALERT_COLOR_SUCCESS, $suc); if (Yii::app()->getRequest()->getIsAjaxRequest()) { $this->renderPartial('_view', array('model' => $model, 'description' => $description), false, true); Yii::app()->end(); } else { $this->redirect(array('view', 'id' => $model->id)); } } else { Yii::app()->user->setFlash(TbHtml::ALERT_COLOR_ERROR, $err); } } else { $description->validate(); } } if (Yii::app()->getRequest()->getIsAjaxRequest()) { $this->renderPartial('_form', array('model' => $model, 'description' => $description), false, true); Yii::app()->end(); } $this->render('create', array('model' => $model, 'description' => $description)); }
/** * Deletes a particular model. * If deletion is successful, the browser will be redirected to the 'admin' page. * @param integer $id the ID of the model to be deleted */ public function actionDelete($id) { $this->loadModel($id)->delete(); AttributeDescription::model()->deleteAll('attribute_id=' . $id); $Attribute2value = $this->loadAttrToValModel('attribute_id=' . $id); foreach ($Attribute2value as $record) { Value::model()->deleteAll('value_id=' . $record['value_id']); ValueDescription::model()->deleteAll('value_id=' . $record['value_id']); } Attribute2value::model()->deleteAll('attribute_id=' . $id); // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser if (!isset($_GET['ajax'])) $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('index')); }
public function afterDelete() { // delete dependencies AttributeDescription::model()->deleteAll("attribute_id={$this->cacheId}"); parent::afterDelete(); }
public function actionGetProductDetails() { $c = new CDbCriteria(); if ($_GET['product_id']) { // make a record counter + 1 that product was viewed one more time $temp_product = Product::model()->findByPk($_GET['product_id']); $temp_product->attributes = array('viewed_counter' => $temp_product->attributes['viewed_counter'] + 1); $temp_product->save(); //end //get details $c->with = array('product_description', 'variations' => array('with' => array('variation_description')), 'category2product'); //$c->together = true; $c->compare('t.product_id', $_GET['product_id']); $c->compare('t.status', 1); } $product = Product::model()->find($c); if ($product['variations']) { foreach ($product['variations'] as &$variation) { $attrs = array(); $attributes = Variation2attribute::model()->findAllByAttributes(array('variation_id' => $variation->variation_id)); $values = Variation2value::model()->findAllByAttributes(array('variation_id' => $variation->variation_id)); foreach ($attributes as $attribute) { //get attribute name $c = new CDbCriteria(); $c->together = true; $c->compare('t.attribute_id', $attribute->attribute_id); $c->compare('language_id', LanguageModel::getLanguageIdByCode(Yii::app()->language)); $attribute_desc = AttributeDescription::model()->find($c); $attrs[$attribute->attribute_id] = array('name' => $attribute_desc->attribute_name); } foreach ($values as $value) { //get attribute id for current value $c = new CDbCriteria(); $c->compare('value_id', $value->value_id); $attr2value = Attribute2value::model()->find($c); //get value name $c = new CDbCriteria(); $c->together = true; $c->compare('`t`.value_id', $value->value_id); $c->compare('language_id', LanguageModel::getLanguageIdByCode(Yii::app()->language)); $value_desc = ValueDescription::model()->find($c); $attrs[$attr2value->attribute_id]['value'] = $value_desc->value_name; } $variation->attributes = $attrs; } } $this->renderPartial('product_details', array('data' => $product)); }
public function actionUpdate($id) { //CVarDumper::dump($_POST, 10, true); //die; $model = $this->loadModel($id); if (isset($_POST['deleteVariationID'])) { Variation::model()->deleteAll('variation_id=' . $_POST['deleteVariationID']); VariationDescription::model()->deleteAll('variation_id=' . $_POST['deleteVariationID']); Variation2attribute::model()->deleteAll('variation_id=' . $_POST['deleteVariationID']); Variation2value::model()->deleteAll('variation_id=' . $_POST['deleteVariationID']); Product2variation::model()->deleteAll('variation_id=' . $_POST['deleteVariationID']); echo 'success'; exit; } $lang_model = new LanguageModel; $ProdDescModel = new ProductDescription; $Category2product = $this->loadCatToProdModel('product_id=' . $id); $assignedToCategory = $Category2product['category_id']; $CadDescModel = new CategoryDescription; $Product2variation = new Product2variation; $Variation = new Variation; $VariationDescription = new VariationDescription; $Variation2attribute = new Variation2attribute; $AttributeModel = new Attribute; $AttributeDescModel = new AttributeDescription; $Attribute2value = new Attribute2value; $ValueModel = new Value; $ValueDescription = new ValueDescription; $aCategories = Category::model()->generateTreeList(); $aLangNames = array(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); $allLangs = $lang_model->findAll('1', array('order' => 'id ASC')); // Filter colors $allFilterColors = FilterColors::model()->findAll( array( 'condition' => 'status = :status', 'params' => array(':status' => 1), 'order'=>'sort_order', ) ); // Delivery Time $allDeliveryTimes = DeliveryTime::model()->findAll( array( 'condition' => 'status = :status', 'params' => array(':status' => 1), 'order'=>'sort_order', ) ); //Existing Variations $aExistingVariations = array(); $aProd2Var = $Product2variation->findAll('product_id=' . $id); foreach ($aProd2Var as $prod_to_var) { $aVar = $Variation->find('variation_id=' . $prod_to_var['variation_id']); $aExistingVariations[$prod_to_var['variation_id']]['image'] = $aVar['variation_image']; $aExistingVariations[$prod_to_var['variation_id']]['filter_color_id'] = $aVar['filter_color_id']; $aVarDescrs = $VariationDescription->findAll('variation_id=' . $prod_to_var['variation_id']); foreach ($aVarDescrs as $var_descr) { $aExistingVariations[$prod_to_var['variation_id']]['lang_var_names'][$var_descr['language_id']] = $var_descr['variation_name']; $aExistingVariations[$prod_to_var['variation_id']]['description'][$var_descr['language_id']] = $var_descr['variation_description']; } $Variation2value = new Variation2value; $aVar2Val = $Variation2value->findAll('variation_id=' . $prod_to_var['variation_id']); foreach ($aVar2Val as $var_to_val) { $aExistingVariations[$prod_to_var['variation_id']]['set_values'][] = $var_to_val['value_id']; } $aVars2Attrs = $Variation2attribute->findAll('variation_id=' . $prod_to_var['variation_id']); //attributes foreach ($aVars2Attrs as $var_to_attr) { $aAttr = $AttributeModel->find('attribute_id=' . $var_to_attr['attribute_id']); $aAttrDescrs = $AttributeDescModel->find('attribute_id=\'' . $aAttr['attribute_id'] . '\' AND language_id=' . $this->language_id); $aExistingVariations[$prod_to_var['variation_id']]['attributes'][$aAttr['attribute_id']]['attribute_name'] = $aAttrDescrs['attribute_name']; //values $aAttr2Vals = $Attribute2value->findAll('attribute_id=' . (int)$aAttr['attribute_id']); foreach ($aAttr2Vals as $attr_to_val) { $aValDescrs = $ValueDescription->find('value_id=' . $attr_to_val['value_id'] . ' AND language_id=' . $this->language_id); $aExistingVariations[$prod_to_var['variation_id']]['attributes'][$aAttr['attribute_id']]['values'][$aValDescrs['value_id']] = $aValDescrs['value_name']; } } } $allLangsJS = array(); foreach ($allLangs as $language) { $allLangsJS[$language['id']] = $language['code']; } $aAttributes = $AttributeModel->findAll(); $aReadyAttrsValues = array(); foreach ($aAttributes as $attribute) { //descriptions $aAttrDescr = $AttributeDescModel->find("language_id='" . $this->language_id . "' AND attribute_id='" . $attribute['attribute_id'] . "'"); $aReadyAttrsValues[$attribute['attribute_id']]['attribute_name'] = $aAttrDescr['attribute_name']; //values $aValues = $Attribute2value->findAll('attribute_id=' . $attribute['attribute_id']); foreach ($aValues as $value) { $aValDescrs = $ValueDescription->findAll("value_id='" . $value['value_id'] . "' AND language_id='" . $this->language_id . "'"); foreach ($aValDescrs as $value_description) { $aReadyAttrsValues[$attribute['attribute_id']]['values'][$value['value_id']] = $value_description['value_name']; } } } $aPordNames = $ProdDescModel->findAll("product_id = '" . $id . "'"); foreach ($aPordNames as $lng_product) { $aLangNames[$lng_product['language_id']] = $lng_product['product_name']; } if (isset($_POST['Product'])) { $Category2product->updateAll(array('category_id' => $_POST['Category2product']['category_id']), 'product_id=' . $id); if (!empty($_POST['deliveryTime'])) { $_POST['Product']['delivery_time_id'] = $_POST['deliveryTime']; } $model->attributes = $_POST['Product']; if ($model->save()) { foreach ($_POST['ProductDescription']['product_name'] as $langId => $prodName) { $prod_desc_model = new ProductDescription; $prod_desc_model->updateByPk(array('product_id' => $id, 'language_id' => $langId), array('product_name' => $prodName)); } } //old variations if (isset($_POST['ExistingVariation'])) { foreach ($_POST['ExistingVariation'] as $varId => $variationData) { foreach ($variationData as $lang_id => $variation_name) { $VariationDescription = new VariationDescription; if (VariationDescription::model()->count("variation_id=:field", array("field" => $varId))) { $VariationDescription->setIsNewRecord(false); } $VariationDescription->variation_id = $varId; $VariationDescription->language_id = $lang_id; $VariationDescription->variation_name = $variation_name; $VariationDescription->variation_description = $_POST['ExistingVariationDesc'][$varId][$lang_id]; $VariationDescription->save(); } if (isset($_POST['ExistingAttributeValue'][$varId])) { $Variation2value = new Variation2value; $Variation2value->deleteAll('variation_id=' . $varId); foreach ($_POST['ExistingAttributeValue'][$varId] as $attr_id => $value_id) { if (isset($_POST['ExistingAttribute'][$varId][$attr_id])) { if ($_POST['ExistingAttribute'][$varId][$attr_id] == 'removed') { $Variation2attribute = new Variation2attribute; $Variation2attribute->deleteAll('attribute_id=' . $attr_id . ' AND variation_id=' . $varId); } $Variation2value = new Variation2value; if ($value_id > 0) { $Variation2value->attributes = array( 'variation_id' => $varId, 'value_id' => $value_id, ); $Variation2value->save(); } } } } if (isset($_POST['RegularAttribute'][$varId])) { foreach ($_POST['RegularAttribute'][$varId] as $attr_id => $value) { if (isset($_POST['RegularAttributeValue'][$varId][$attr_id]) && $_POST['RegularAttributeValue'][$varId][$attr_id] > 0) { $Variation2attribute = new Variation2attribute; $Variation2attribute->attributes = array( 'variation_id' => $varId, 'attribute_id' => $attr_id, ); $Variation2attribute->save(); $Variation2value = new Variation2value; $Variation2value->attributes = array( 'variation_id' => $varId, 'value_id' => $_POST['RegularAttributeValue'][$varId][$attr_id], ); $Variation2value->save(); } } } if (isset($_POST['ExistingFilterColor'][$varId])) { $Variation = new Variation; $Variation->variation_id = $varId; $Variation->setIsNewRecord(false); $Variation->filter_color_id = $_POST['ExistingFilterColor'][$varId]; $Variation->save(); } } } //variations if (isset($_POST['NewVariation'])) { foreach ($_POST['NewVariation'] as $specId => $variationData) { $Variation = new Variation; $Variation->attributes = array( 'variation_image' => 'some_image.jpg', 'status' => 1, ); $Variation->save(); $variation_id = $Variation->getPrimaryKey(); $Product2variation = new Product2variation; $Product2variation->attributes = array( 'product_id' => $id, 'variation_id' => $variation_id, ); $Product2variation->save(); foreach ($variationData as $lang_id => $variation_name) { $VariationDescription = new VariationDescription; $VariationDescription->attributes = array( 'variation_name' => $variation_name, 'variation_id' => $variation_id, 'language_id' => $lang_id, ); $VariationDescription->save(); } if (isset($_POST['NewAttribute'][$specId])) { foreach ($_POST['NewAttribute'][$specId] as $attr_id => $value) { if (isset($_POST['NewAttributeValue'][$specId][$attr_id]) && $_POST['NewAttributeValue'][$specId][$attr_id] > 0) { $Variation2attribute = new Variation2attribute; $Variation2attribute->attributes = array( 'variation_id' => $variation_id, 'attribute_id' => $attr_id, ); $Variation2attribute->save(); $Variation2value = new Variation2value; $Variation2value->attributes = array( 'variation_id' => $variation_id, 'value_id' => $_POST['NewAttributeValue'][$specId][$attr_id], ); $Variation2value->save(); } } } } } $this->redirect(array('index')); } $this->render('update', array( 'model' => $model, 'allLangs' => $allLangs, 'allLangsJS' => json_encode($allLangsJS), 'aReadyAttrsValues' => $aReadyAttrsValues, 'ProdDescModel' => $ProdDescModel, 'aLangNames' => $aLangNames, 'Category2product' => $Category2product, 'aExistingVariations' => $aExistingVariations, 'aCategories' => $aCategories, 'assignedToCategory' => $assignedToCategory, 'allFilterColors' => $allFilterColors, 'allDeliveryTimes' => $allDeliveryTimes, )); }