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_CustomerController extends Mage_Core_Controller_Front_Action
00036 {
00037 protected function _getTagId()
00038 {
00039 $tagId = (int) $this->getRequest()->getParam('tagId');
00040 if ($tagId) {
00041 $customerId = Mage::getSingleton('customer/session')->getCustomerId();
00042 $model = Mage::getModel('tag/tag_relation');
00043 $model->loadByTagCustomer(null, $tagId, $customerId);
00044 Mage::register('tagModel', $model);
00045 return $model->getTagId();
00046 }
00047 return false;
00048 }
00049
00050 public function indexAction()
00051 {
00052 if( !Mage::getSingleton('customer/session')->getCustomerId() ) {
00053 Mage::getSingleton('customer/session')->authenticate($this);
00054 return;
00055 }
00056
00057 $this->loadLayout();
00058 $this->_initLayoutMessages('tag/session');
00059 $this->_initLayoutMessages('catalog/session');
00060
00061 if ($navigationBlock = $this->getLayout()->getBlock('customer_account_navigation')) {
00062 $navigationBlock->setActive('tag/customer');
00063 }
00064
00065 if ($block = $this->getLayout()->getBlock('customer_tags')) {
00066 $block->setRefererUrl($this->_getRefererUrl());
00067 }
00068
00069 $this->getLayout()->getBlock('head')->setTitle(Mage::helper('tag')->__('My Tags'));
00070 $this->renderLayout();
00071 }
00072
00073 public function viewAction()
00074 {
00075 if( !Mage::getSingleton('customer/session')->getCustomerId() ) {
00076 Mage::getSingleton('customer/session')->authenticate($this);
00077 return;
00078 }
00079 if ($tagId = $this->_getTagId()) {
00080 Mage::register('tagId', $tagId);
00081 $this->loadLayout();
00082 $this->_initLayoutMessages('tag/session');
00083
00084 if ($navigationBlock = $this->getLayout()->getBlock('customer_account_navigation')) {
00085 $navigationBlock->setActive('tag/customer');
00086 }
00087
00088 $this->_initLayoutMessages('checkout/session');
00089 $this->getLayout()->getBlock('head')->setTitle(Mage::helper('tag')->__('My Tags'));
00090 $this->renderLayout();
00091 }
00092 else {
00093 $this->_forward('noRoute');
00094 }
00095 }
00096
00097 public function editAction()
00098 {
00099 if( !Mage::getSingleton('customer/session')->getCustomerId() ) {
00100 Mage::getSingleton('customer/session')->authenticate($this);
00101 return;
00102 }
00103
00104 if ($tagId = $this->_getTagId()) {
00105 $this->loadLayout();
00106 $this->_initLayoutMessages('tag/session');
00107 $this->_initLayoutMessages('customer/session');
00108 if ($navigationBlock = $this->getLayout()->getBlock('customer_account_navigation')) {
00109 $navigationBlock->setActive('tag/customer');
00110 }
00111 $this->renderLayout();
00112 }
00113 else {
00114 $this->_forward('noRoute');
00115 }
00116 }
00117
00118 public function removeAction()
00119 {
00120 if( !Mage::getSingleton('customer/session')->getCustomerId() ) {
00121 Mage::getSingleton('customer/session')->authenticate($this);
00122 return;
00123 }
00124
00125 if ($tagId = $this->_getTagId()) {
00126 try {
00127 $model = Mage::registry('tagModel');
00128 $model->deactivate();
00129 $tag = Mage::getModel('tag/tag')->load($tagId)->aggregate();
00130 Mage::getSingleton('tag/session')->addSuccess(Mage::helper('tag')->__('Your tag was successfully deleted'));
00131 $this->getResponse()->setRedirect(Mage::getUrl('*/*/', array(
00132 self::PARAM_NAME_URL_ENCODED => Mage::helper('core')->urlEncode(Mage::getUrl('customer/account/'))
00133 )));
00134
00135 return;
00136 } catch (Exception $e) {
00137 Mage::getSingleton('tag/session')->addError(Mage::helper('tag')->__('Unable to remove tag. Please, try again later.'));
00138 }
00139 }
00140 else {
00141 $this->_forward('noRoute');
00142 }
00143 }
00144
00145 public function saveAction()
00146 {
00147 if( !Mage::getSingleton('customer/session')->getCustomerId() ) {
00148 Mage::getSingleton('customer/session')->authenticate($this);
00149 return;
00150 }
00151
00152 $tagId = (int) $this->getRequest()->getParam('tagId');
00153 $customerId = Mage::getSingleton('customer/session')->getCustomerId();
00154 $tagName = (string) $this->getRequest()->getPost('productTagName');
00155
00156 if (strlen($tagName) === 0) {
00157 Mage::getSingleton('tag/session')->addError(Mage::helper('tag')->__('Tag can\'t be empty.'));
00158 $this->_redirect('*/*/edit', array('tagId'=>$tagId));
00159 return;
00160 }
00161
00162 if($tagId) {
00163 try {
00164 $productId = 0;
00165 $isNew = false;
00166 $message = false;
00167 $storeId = Mage::app()->getStore()->getId();
00168
00169 $tagModel = Mage::getModel('tag/tag');
00170 $tagModel->load($tagId);
00171
00172 if( $tagModel->getName() != $tagName ) {
00173 $tagModel->loadByName($tagName);
00174
00175 if($tagModel->getId()) {
00176 $status = $tagModel->getStatus();
00177 }
00178 else {
00179 $isNew = true;
00180 $message= Mage::helper('tag')->__('Thank you. Your tag has been accepted for moderation.');
00181 $status = $tagModel->getPendingStatus();
00182 }
00183
00184 $tagModel->setName($tagName)
00185 ->setStatus($status)
00186 ->setStoreId($storeId)
00187 ->save();
00188 }
00189
00190 $tagRalationModel = Mage::getModel('tag/tag_relation');
00191 $tagRalationModel->loadByTagCustomer(null, $tagId, $customerId, $storeId);
00192
00193 if ($tagRalationModel->getCustomerId() == $customerId ) {
00194 $productIds = $tagRalationModel->getProductIds();
00195 if ($tagRalationModel->getTagId()!=$tagModel->getId()) {
00196 $tagRalationModel->deactivate();
00197 } else {
00198 $tagRalationModel->delete();
00199 }
00200
00201 foreach ($productIds as $productId) {
00202 $newTagRalationModel = Mage::getModel('tag/tag_relation')
00203 ->setTagId($tagModel->getId())
00204 ->setCustomerId($customerId)
00205 ->setStoreId($storeId)
00206 ->setActive(true)
00207 ->setProductId($productId)
00208 ->save();
00209 }
00210 }
00211
00212 if( $tagModel->getId() ) {
00213 $tagModel->aggregate();
00214 $this->getResponse()->setRedirect(Mage::getUrl('*/*/'));
00215 }
00216 $message = ($message) ? $message : Mage::helper('tag')->__('Your tag was successfully saved');
00217 Mage::getSingleton('tag/session')->addSuccess($message);
00218 $this->_redirect('*/*/');
00219 return;
00220 } catch (Exception $e) {
00221 Mage::getSingleton('tag/session')->addError(
00222 Mage::helper('tag')->__('Unable to save your tag. Please, try again later.')
00223 );
00224 }
00225 }
00226 $this->_redirectReferer();
00227 }
00228 }