/** * Saving tag and relation between tag, customer, product and store */ public function saveAction() { $customerSession = Mage::getSingleton('customer/session'); if (!$customerSession->authenticate($this)) { return; } $tagName = (string) $this->getRequest()->getQuery('productTagName'); $productId = (int) $this->getRequest()->getParam('product'); if (strlen($tagName) && $productId) { $session = Mage::getSingleton('catalog/session'); $product = Mage::getModel('catalog/product')->load($productId); if (!$product->getId()) { $session->addError($this->__('Unable to save tag(s).')); } else { try { $customerId = $customerSession->getCustomerId(); $storeId = Mage::app()->getStore()->getId(); $tagNamesArr = $this->_cleanTags($this->_extractTags($tagName)); $counter = new Varien_Object(array("new" => 0, "exist" => array(), "success" => array(), "recurrence" => array())); $tagModel = Mage::getModel('tag/tag'); $tagRelationModel = Mage::getModel('tag/tag_relation'); foreach ($tagNamesArr as $tagName) { $tagModel->unsetData()->loadByName($tagName)->setStoreId($storeId)->setName($tagName); $tagRelationModel->unsetData()->setStoreId($storeId)->setProductId($productId)->setCustomerId($customerId)->setActive(1)->setCreatedAt($tagRelationModel->getResource()->formatDate(time())); if (!$tagModel->getId()) { $tagModel->setFirstCustomerId($customerId)->setFirstStoreId($storeId)->setStatus($tagModel->getPendingStatus())->save(); $tagRelationModel->setTagId($tagModel->getId())->save(); $counter->setNew($counter->getNew() + 1); } else { $tagStatus = $tagModel->getStatus(); $tagRelationModel->setTagId($tagModel->getId()); switch ($tagStatus) { case $tagModel->getApprovedStatus(): if ($this->_checkLinkBetweenTagProduct($tagRelationModel)) { if (!$this->_checkLinkBetweenTagCustomerProduct($tagRelationModel, $tagModel)) { $tagRelationModel->save(); } $counter->setExist(array_merge($counter->getExist(), array($tagName))); } else { $tagRelationModel->save(); $counter->setSuccess(array_merge($counter->getSuccess(), array($tagName))); } break; case $tagModel->getPendingStatus(): if (!$this->_checkLinkBetweenTagCustomerProduct($tagRelationModel, $tagModel)) { $tagRelationModel->save(); } $counter->setNew($counter->getNew() + 1); break; case $tagModel->getDisabledStatus(): if ($this->_checkLinkBetweenTagCustomerProduct($tagRelationModel, $tagModel)) { $counter->setRecurrence(array_merge($counter->getRecurrence(), array($tagName))); } else { $tagModel->setStatus($tagModel->getPendingStatus())->save(); $tagRelationModel->save(); $counter->setNew($counter->getNew() + 1); } break; } } } $this->_fillMessageBox($counter); } catch (Exception $e) { Mage::logException($e); $session->addError($this->__('Unable to save tag(s).')); } } } $this->_redirectReferer(); }