1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34:
35: class Mage_Tag_IndexController extends Mage_Core_Controller_Front_Action
36: {
37: 38: 39:
40: public function saveAction()
41: {
42: $customerSession = Mage::getSingleton('customer/session');
43: if(!$customerSession->authenticate($this)) {
44: return;
45: }
46: $tagName = (string) $this->getRequest()->getQuery('productTagName');
47: $productId = (int)$this->getRequest()->getParam('product');
48:
49: if(strlen($tagName) && $productId) {
50: $session = Mage::getSingleton('catalog/session');
51: $product = Mage::getModel('catalog/product')
52: ->load($productId);
53: if(!$product->getId()){
54: $session->addError($this->__('Unable to save tag(s).'));
55: } else {
56: try {
57: $customerId = $customerSession->getCustomerId();
58: $storeId = Mage::app()->getStore()->getId();
59:
60: $tagModel = Mage::getModel('tag/tag');
61:
62:
63: $counter = array(
64: Mage_Tag_Model_Tag::ADD_STATUS_NEW => array(),
65: Mage_Tag_Model_Tag::ADD_STATUS_EXIST => array(),
66: Mage_Tag_Model_Tag::ADD_STATUS_SUCCESS => array(),
67: Mage_Tag_Model_Tag::ADD_STATUS_REJECTED => array()
68: );
69:
70: $tagNamesArr = $this->_cleanTags($this->_extractTags($tagName));
71: foreach ($tagNamesArr as $tagName) {
72:
73: $tagModel->unsetData()
74: ->loadByName($tagName);
75:
76: if (!$tagModel->getId()) {
77: $tagModel->setName($tagName)
78: ->setFirstCustomerId($customerId)
79: ->setFirstStoreId($storeId)
80: ->setStatus($tagModel->getPendingStatus())
81: ->save();
82: }
83: $relationStatus = $tagModel->saveRelation($productId, $customerId, $storeId);
84: $counter[$relationStatus][] = $tagName;
85: }
86: $this->_fillMessageBox($counter);
87: } catch (Exception $e) {
88: Mage::logException($e);
89: $session->addError($this->__('Unable to save tag(s).'));
90: }
91: }
92: }
93: $this->_redirectReferer();
94: }
95:
96: 97: 98: 99: 100: 101:
102: protected function ($tagNamesInString)
103: {
104: return explode("\n", preg_replace("/(\'(.*?)\')|(\s+)/i", "$1\n", $tagNamesInString));
105: }
106:
107: 108: 109: 110: 111: 112:
113: protected function _cleanTags(array $tagNamesArr)
114: {
115: foreach( $tagNamesArr as $key => $tagName ) {
116: $tagNamesArr[$key] = trim($tagNamesArr[$key], '\'');
117: $tagNamesArr[$key] = trim($tagNamesArr[$key]);
118: if( $tagNamesArr[$key] == '' ) {
119: unset($tagNamesArr[$key]);
120: }
121: }
122: return $tagNamesArr;
123: }
124:
125: 126: 127: 128: 129: 130:
131: protected function _fillMessageBox($counter)
132: {
133: $session = Mage::getSingleton('catalog/session');
134: $helper = Mage::helper('core');
135:
136: if (count($counter[Mage_Tag_Model_Tag::ADD_STATUS_NEW])) {
137: $session->addSuccess(
138: $this->__('%s tag(s) have been accepted for moderation.', count($counter[Mage_Tag_Model_Tag::ADD_STATUS_NEW]))
139: );
140: }
141:
142: if (count($counter[Mage_Tag_Model_Tag::ADD_STATUS_EXIST])) {
143: foreach ($counter[Mage_Tag_Model_Tag::ADD_STATUS_EXIST] as $tagName) {
144: $session->addNotice(
145: $this->__('Tag "%s" has already been added to the product.' , $helper->escapeHtml($tagName))
146: );
147: }
148: }
149:
150: if (count($counter[Mage_Tag_Model_Tag::ADD_STATUS_SUCCESS])) {
151: foreach ($counter[Mage_Tag_Model_Tag::ADD_STATUS_SUCCESS] as $tagName) {
152: $session->addSuccess(
153: $this->__('Tag "%s" has been added to the product.' ,$helper->escapeHtml($tagName))
154: );
155: }
156: }
157:
158: if (count($counter[Mage_Tag_Model_Tag::ADD_STATUS_REJECTED])) {
159: foreach ($counter[Mage_Tag_Model_Tag::ADD_STATUS_REJECTED] as $tagName) {
160: $session->addNotice(
161: $this->__('Tag "%s" has been rejected by administrator.' ,$helper->escapeHtml($tagName))
162: );
163: }
164: }
165: }
166:
167: }
168: