00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Tag_IndexController extends Mage_Core_Controller_Front_Action
00036 {
00037 public function saveAction()
00038 {
00039 if(!Mage::getSingleton('customer/session')->authenticate($this)) {
00040 return;
00041 }
00042 $tagName = (string) $this->getRequest()->getQuery('productTagName');
00043 $productId = (int)$this->getRequest()->getParam('product');
00044
00045 if(strlen($tagName) && $productId) {
00046 $session = Mage::getSingleton('catalog/session');
00047 $product = Mage::getModel('catalog/product')
00048 ->load($productId);
00049 if(!$product->getId()){
00050 $session->addError(Mage::helper('tag')->__('Unable to save tag(s)'));
00051 } else {
00052 try {
00053 $customerId = Mage::getSingleton('customer/session')->getCustomerId();
00054 $tagName = urldecode($tagName);
00055 $tagNamesArr = explode("\n", preg_replace("/(\'(.*?)\')|(\s+)/i", "$1\n", $tagName));
00056
00057 foreach( $tagNamesArr as $key => $tagName ) {
00058 $tagNamesArr[$key] = trim($tagNamesArr[$key], '\'');
00059 $tagNamesArr[$key] = trim($tagNamesArr[$key]);
00060 if( $tagNamesArr[$key] == '' ) {
00061 unset($tagNamesArr[$key]);
00062 }
00063 }
00064 $newCount = 0;
00065 foreach( $tagNamesArr as $tagName ) {
00066 if( $tagName ) {
00067 $tagModel = Mage::getModel('tag/tag');
00068 $tagModel->loadByName($tagName);
00069 if ($tagModel->getId()) {
00070 $status = $tagModel->getStatus();
00071 $session->addNotice(Mage::helper('tag')->__('Tag "%s" has already been added to the product' ,$tagName));
00072 }
00073 else {
00074 $status = $tagModel->getPendingStatus();
00075 $newCount++;
00076 }
00077
00078 $tagModel->setName($tagName)
00079 ->setStoreId(Mage::app()->getStore()->getId())
00080 ->setStatus($status)
00081 ->save();
00082
00083 $tagRelationModel = Mage::getModel('tag/tag_relation');
00084 $tagRelationModel->loadByTagCustomer($productId,
00085 $tagModel->getId(),
00086 $customerId,
00087 Mage::app()->getStore()->getId()
00088 );
00089
00090 if( $tagRelationModel->getCustomerId() == $customerId && $tagRelationModel->getActive()) {
00091 continue;
00092 }
00093 $tagRelationModel->setTagId($tagModel->getId())
00094 ->setCustomerId($customerId)
00095 ->setProductId($productId)
00096 ->setStoreId(Mage::app()->getStore()->getId())
00097 ->setCreatedAt( now() )
00098 ->setActive(1)
00099 ->save();
00100 $tagModel->aggregate();
00101 } else {
00102 continue;
00103 }
00104 }
00105 if ($newCount > 0) {
00106 $session->addSuccess(Mage::helper('tag')->__('%s tag(s) have been accepted for moderation', $newCount));
00107 }
00108 } catch (Exception $e) {
00109 $session->addError(Mage::helper('tag')->__('Unable to save tag(s)'));
00110 }
00111 }
00112 }
00113 $this->_redirectReferer();
00114 }
00115 }