/** * This is invoked after the record is deleted. */ protected function afterDelete() { parent::afterDelete(); $this->value->delete(); Photo::deleteByProduct($this); ProductRelation::model()->deleteAll('pid=' . $this->id . ' OR pr_id=' . $this->id); CatalogRelation::model()->deleteAll('pr_id=' . $this->id); }
function productImport($array) { if ($array['id'] || $array['artno']) { $log = array('error' => 0, 'warning' => '', 'note' => ''); if ($array['id']) { $product = $this->loadModel($array['id']); } else { $product = Product::model()->findByAttributes(array('artno' => $array['artno'])); } if ($product) { $value = $product->value; $log['new'] = 0; } else { $log['new'] = 1; $product = new Product(); $value = new ProductValue(); } foreach ($array as $f => $v) { if (array_key_exists($f, $product->attributes)) { if (in_array($f, array('id', 'photo_id', 'price', 'store', 'type', 'status'))) { if ($f != 'photo_id' && $f != 'id') { $product->setAttribute($f, (int) $v); } } else { $product->setAttribute($f, $v); } } elseif (array_key_exists($f, $value->attributes)) { $value->setAttribute($f, $v); } } if ($product->validate() && $value->validate()) { if ($log['new']) { $product->save(); $value->pr_id = $product->id; } if (isset($array['catalogs'])) { $catalogs = explode('|', $array['catalogs']); foreach ($catalogs as $branch) { $parent_id = 0; foreach (explode('=>', $branch) as $cat_name) { $catalog = Catalog::model()->findByAttributes(array('name' => $cat_name, 'pid' => $parent_id)); if ($catalog) { $carRel = new CatalogRelation(); $carRel->cid = $parent_id = $catalog->id; $carRel->pr_id = $product->id; $carRel->save(); } else { $log['note'] = CartModule::t('Not found catalog "{catalog}" in branch "{branch}".', 'import', array('{branch}' => $branch, '{catalog}' => $cat_name)); } } } } if ($array['photo']) { $photos = explode('|', $array['photo']); foreach ($photos as $photo) { $path = Yii::app()->getModule('cart')->importPathPhotos . '/' . $photo; $newPhoto = new Photo(); if ($newPhoto->addByFile($path, $product->id)) { if (!$product->photo_id && $newPhoto->id) { $product->photo_id = $newPhoto->id; $product->save(); } } else { $log['warning'] = CartModule::t('Failed to add photo: {photo}', 'import', array('{photo}' => $photo)); } } } $value->save(); $product->save(); if ($log['new']) { $log['message'] = CartModule::t('Addded product name (artno)', 'import', $array); } else { $log['message'] = CartModule::t('Changed product name (artno)', 'import', $array); } } else { $log['error'] = 1; if (!$product->validate()) { $log['message'] = CartModule::t('Product validation error', 'import') . ' (' . CHtml::errorSummary($product) . ')<br/>'; } if (!$value->validate()) { $log['message'] = CartModule::t('Product fields validation error', 'import') . ' (' . CHtml::errorSummary($value) . ')'; } } return $log; } else { return array('message' => CartModule::t('Not found the ID and Product Marking', 'import')); } }
/** * This is invoked after the record is deleted. */ protected function afterDelete() { parent::afterDelete(); foreach ($this->child as $child) { $child->delete(); } CatalogRelation::model()->deleteAll('cid=' . $this->id); if (isset(Yii::app()->cache)) { $items = Yii::app()->cache->delete(__CLASS__ . 'Tree'); } ProductFieldByCatalog::model()->deleteAll('cid=' . $this->id); }