/**
  * Admin action.
  * Create or edit selected product
  *
  * @var int $id Product
  */
 public function editAction()
 {
     if (!Zend_Auth::getInstance()->hasIdentity()) {
         throw new Zend_Exception("Access Forbidden", 403);
     }
     $parent_id = $this->getRequest()->getParam("parent_id");
     if ($parent_id) {
         // отдельный action для подтоваров
         $this->forward("subedit");
         return;
     }
     $product_id = $this->getRequest()->getParam("id");
     $productsModel = new Model_DbTable_Products();
     // признаки вида операции и вида товара
     $newRecord = $product_id == null;
     $this->view->newRecord = $newRecord;
     if ($product_id) {
         $product = $productsModel->find($product_id)->current();
     }
     // новый продукт
     if ($newRecord) {
         $product = $productsModel->createRow();
     }
     $editForm = Model_Static_Loader::loadForm("product");
     if ($this->getRequest()->isPost()) {
         // отправка формы
         if ($editForm->isValid($_POST)) {
             $file = $editForm->image->getFileInfo();
             $ext = pathinfo($file['image']['name'], PATHINFO_EXTENSION);
             $name = pathinfo($file['image']['name'], PATHINFO_FILENAME);
             $newName = time() . '_' . $name . '.' . $ext;
             $editForm->image->addFilter('Rename', APPLICATION_ROOT . "/files/images/product/" . $newName);
             $editForm->image->receive();
             // here save data
             // product first
             $values = $editForm->getValues();
             $newImages = isset($_FILES["images"]) ? $_FILES["images"] : array();
             $images = $this->getRequest()->getParam("images", array());
             if (count($newImages) > 0) {
                 foreach ($newImages['name'] as $i => $image) {
                     if ($image) {
                         $name = date("ymd-") . $image;
                         $images[] = $name;
                         move_uploaded_file($_FILES["images"]["tmp_name"][$i], APPLICATION_ROOT . "/files/images/product/{$name}");
                     }
                 }
             }
             $product->a_images = $images;
             if (!$values["image"]) {
                 unset($values["image"]);
             }
             foreach ($values as $name => $value) {
                 if (isset($product->{$name})) {
                     $product->{$name} = $value;
                 }
             }
             $product->mod_date = date("Y-m-d H:i:s");
             if (!$product->add_date) {
                 $product->add_date = $product->mod_date;
             }
             $product->save();
         }
         // перезаписываем отношения категорий
         $categories = $this->getRequest()->getParam("categories");
         $xrefModel = new Model_DbTable_CategoryXref();
         $xrefModel->delete("product_id = " . $product->id);
         if ($categories) {
             foreach ($categories as $category) {
                 try {
                     $xref = $xrefModel->createRow(array("product_id" => $product->id, "category_id" => $category));
                     $xref->save();
                 } catch (Exception $e) {
                     continue;
                 }
             }
         }
         // save productParams
         $newParams = $this->getRequest()->getParam("productparams");
         if ($newParams) {
             $newParams = array_values($newParams);
         } else {
             $newParams = array();
         }
         $productParams = new Model_DbTable_ProductParams();
         $oldParams = $product->getParams();
         $pCount = count($newParams) > count($oldParams) ? count($newParams) : count($oldParams);
         for ($i = 0; $i < $pCount; $i++) {
             if (isset($newParams[$i])) {
                 $newParam = (object) $newParams[$i];
                 try {
                     $oldParam = $oldParams->getRow($i);
                 } catch (Zend_Exception $e) {
                     $oldParam = $productParams->createRow(array('product_id' => $product->id));
                 }
                 $oldParam->name = $newParam->name;
                 $oldParam->value = $newParam->value;
                 $oldParam->order = $newParam->order;
                 $oldParam->save();
             } else {
                 $oldParam = $oldParams->getRow($i);
                 $oldParam->delete();
             }
         }
         $this->redirect("/catalog/products/view/category/" . $this->getRequest()->getParam("category") . "/id/" . $product->id);
     }
     $editForm->setDefaults($product->toArray());
     $this->view->newproduct = $editForm;
     $this->view->category = $this->getRequest()->getParam("category");
     if (!$newRecord) {
         // редактируем основной продукт
         $this->view->row = $product;
         $this->view->productParams = $product->getParams();
         $select = $productsModel->select()->order('order ASC');
         $this->view->subProducts = $product->findDependentRowset("Model_DbTable_Subproducts", 'SubproductsRel', $select);
     } else {
         // Новый продукт
         $this->view->row = $productsModel->createRow();
         $this->view->productParams = array();
         $this->view->subProducts = array();
     }
 }
Exemplo n.º 2
0
 public function getParams()
 {
     $paramsTable = new Model_DbTable_ProductParams();
     $select = $paramsTable->select()->order('order ASC');
     return $this->findDependentRowset("Model_DbTable_ProductParams", null, $select);
 }