PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Tag/controllers/IndexController.php

https://bitbucket.org/dnejedly/eaparts
PHP | 167 lines | 98 code | 15 blank | 54 comment | 12 complexity | fa6ca54459d360f0555d00f462dc7d8b MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Tag
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Tag Frontend controller
  28. *
  29. * @category Mage
  30. * @package Mage_Tag
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Tag_IndexController extends Mage_Core_Controller_Front_Action
  34. {
  35. /**
  36. * Saving tag and relation between tag, customer, product and store
  37. */
  38. public function saveAction()
  39. {
  40. $customerSession = Mage::getSingleton('customer/session');
  41. if(!$customerSession->authenticate($this)) {
  42. return;
  43. }
  44. $tagName = (string) $this->getRequest()->getQuery('productTagName');
  45. $productId = (int)$this->getRequest()->getParam('product');
  46. if(strlen($tagName) && $productId) {
  47. $session = Mage::getSingleton('catalog/session');
  48. $product = Mage::getModel('catalog/product')
  49. ->load($productId);
  50. if(!$product->getId()){
  51. $session->addError($this->__('Unable to save tag(s).'));
  52. } else {
  53. try {
  54. $customerId = $customerSession->getCustomerId();
  55. $storeId = Mage::app()->getStore()->getId();
  56. $tagModel = Mage::getModel('tag/tag');
  57. // added tag relation statuses
  58. $counter = array(
  59. Mage_Tag_Model_Tag::ADD_STATUS_NEW => array(),
  60. Mage_Tag_Model_Tag::ADD_STATUS_EXIST => array(),
  61. Mage_Tag_Model_Tag::ADD_STATUS_SUCCESS => array(),
  62. Mage_Tag_Model_Tag::ADD_STATUS_REJECTED => array()
  63. );
  64. $tagNamesArr = $this->_cleanTags($this->_extractTags($tagName));
  65. foreach ($tagNamesArr as $tagName) {
  66. // unset previously added tag data
  67. $tagModel->unsetData()
  68. ->loadByName($tagName);
  69. if (!$tagModel->getId()) {
  70. $tagModel->setName($tagName)
  71. ->setFirstCustomerId($customerId)
  72. ->setFirstStoreId($storeId)
  73. ->setStatus($tagModel->getPendingStatus())
  74. ->save();
  75. }
  76. $relationStatus = $tagModel->saveRelation($productId, $customerId, $storeId);
  77. $counter[$relationStatus][] = $tagName;
  78. }
  79. $this->_fillMessageBox($counter);
  80. } catch (Exception $e) {
  81. Mage::logException($e);
  82. $session->addError($this->__('Unable to save tag(s).'));
  83. }
  84. }
  85. }
  86. $this->_redirectReferer();
  87. }
  88. /**
  89. * Checks inputed tags on the correctness of symbols and split string to array of tags
  90. *
  91. * @param string $tagNamesInString
  92. * @return array
  93. */
  94. protected function _extractTags($tagNamesInString)
  95. {
  96. return explode("\n", preg_replace("/(\'(.*?)\')|(\s+)/i", "$1\n", $tagNamesInString));
  97. }
  98. /**
  99. * Clears the tag from the separating characters.
  100. *
  101. * @param array $tagNamesArr
  102. * @return array
  103. */
  104. protected function _cleanTags(array $tagNamesArr)
  105. {
  106. foreach( $tagNamesArr as $key => $tagName ) {
  107. $tagNamesArr[$key] = trim($tagNamesArr[$key], '\'');
  108. $tagNamesArr[$key] = trim($tagNamesArr[$key]);
  109. if( $tagNamesArr[$key] == '' ) {
  110. unset($tagNamesArr[$key]);
  111. }
  112. }
  113. return $tagNamesArr;
  114. }
  115. /**
  116. * Fill Message Box by success and notice messages about results of user actions.
  117. *
  118. * @param array $counter
  119. * @return void
  120. */
  121. protected function _fillMessageBox($counter)
  122. {
  123. $session = Mage::getSingleton('catalog/session');
  124. $helper = Mage::helper('core');
  125. if (count($counter[Mage_Tag_Model_Tag::ADD_STATUS_NEW])) {
  126. $session->addSuccess(
  127. $this->__('%s tag(s) have been accepted for moderation.', count($counter[Mage_Tag_Model_Tag::ADD_STATUS_NEW]))
  128. );
  129. }
  130. if (count($counter[Mage_Tag_Model_Tag::ADD_STATUS_EXIST])) {
  131. foreach ($counter[Mage_Tag_Model_Tag::ADD_STATUS_EXIST] as $tagName) {
  132. $session->addNotice(
  133. $this->__('Tag "%s" has already been added to the product.' , $helper->escapeHtml($tagName))
  134. );
  135. }
  136. }
  137. if (count($counter[Mage_Tag_Model_Tag::ADD_STATUS_SUCCESS])) {
  138. foreach ($counter[Mage_Tag_Model_Tag::ADD_STATUS_SUCCESS] as $tagName) {
  139. $session->addSuccess(
  140. $this->__('Tag "%s" has been added to the product.' ,$helper->escapeHtml($tagName))
  141. );
  142. }
  143. }
  144. if (count($counter[Mage_Tag_Model_Tag::ADD_STATUS_REJECTED])) {
  145. foreach ($counter[Mage_Tag_Model_Tag::ADD_STATUS_REJECTED] as $tagName) {
  146. $session->addNotice(
  147. $this->__('Tag "%s" has been rejected by administrator.' ,$helper->escapeHtml($tagName))
  148. );
  149. }
  150. }
  151. }
  152. }