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: class Mage_Adminhtml_TagController extends Mage_Adminhtml_Controller_Action
35: {
36:
37: protected function _initAction()
38: {
39: $this->loadLayout()
40: ->_setActiveMenu('catalog/tag')
41: ->_addBreadcrumb(Mage::helper('adminhtml')->__('Catalog'), Mage::helper('adminhtml')->__('Catalog'))
42: ->_addBreadcrumb(Mage::helper('adminhtml')->__('Tags'), Mage::helper('adminhtml')->__('Tags'));
43:
44: return $this;
45: }
46:
47: 48: 49: 50: 51:
52: protected function _initTag()
53: {
54: $model = Mage::getModel('tag/tag');
55: $storeId = $this->getRequest()->getParam('store');
56: $model->setStoreId($storeId);
57:
58: if (($id = $this->getRequest()->getParam('tag_id'))) {
59: $model->setAddBasePopularity();
60: $model->load($id);
61: $model->setStoreId($storeId);
62:
63: if (!$model->getId()) {
64: return false;
65: }
66: }
67:
68: Mage::register('current_tag', $model);
69: return $model;
70: }
71:
72: 73: 74: 75:
76: public function indexAction()
77: {
78: $this->_title($this->__('Catalog'))
79: ->_title($this->__('Tags'))
80: ->_title($this->__('All Tags'));
81:
82: $this->_initAction()
83: ->_addBreadcrumb(Mage::helper('adminhtml')->__('All Tags'), Mage::helper('adminhtml')->__('All Tags'))
84: ->_setActiveMenu('catalog/tag/all')
85: ->renderLayout();
86: }
87:
88: 89: 90: 91:
92: public function ajaxGridAction()
93: {
94: $this->loadLayout();
95: $this->renderLayout();
96: }
97:
98: 99: 100: 101:
102: public function ajaxPendingGridAction()
103: {
104: $this->loadLayout();
105: $this->renderLayout();
106: }
107:
108: 109: 110: 111:
112: public function newAction()
113: {
114: $this->_forward('edit');
115: }
116:
117: 118: 119: 120:
121: public function editAction()
122: {
123: $this->_title($this->__('Catalog'))
124: ->_title($this->__('Tags'));
125:
126: if (! (int) $this->getRequest()->getParam('store')) {
127: return $this->_redirect('*/*/*/', array('store' => Mage::app()->getAnyStoreView()->getId(), '_current' => true));
128: }
129:
130: if (! ($model = $this->_initTag())) {
131: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Wrong tag was specified.'));
132: return $this->_redirect('*/*/index', array('store' => $this->getRequest()->getParam('store')));
133: }
134:
135:
136: $data = Mage::getSingleton('adminhtml/session')->getTagData(true);
137: if (! empty($data)) {
138: $model->addData($data);
139: }
140:
141: $this->_title($model->getId() ? $model->getName() : $this->__('New Tag'));
142:
143: Mage::register('tag_tag', $model);
144:
145: $this->_initAction()->renderLayout();
146: }
147:
148: 149: 150: 151:
152: public function saveAction()
153: {
154: if ($postData = $this->getRequest()->getPost()) {
155: if (isset($postData['tag_id'])) {
156: $data['tag_id'] = $postData['tag_id'];
157: }
158:
159: $data['name'] = trim($postData['tag_name']);
160: $data['status'] = $postData['tag_status'];
161: $data['base_popularity'] = (isset($postData['base_popularity'])) ? $postData['base_popularity'] : 0;
162: $data['store'] = $postData['store_id'];
163:
164: if (!$model = $this->_initTag()) {
165: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Wrong tag was specified.'));
166: return $this->_redirect('*/*/index', array('store' => $data['store']));
167: }
168:
169: $model->addData($data);
170:
171: if (isset($postData['tag_assigned_products'])) {
172: $productIds = Mage::helper('adminhtml/js')->decodeGridSerializedInput(
173: $postData['tag_assigned_products']
174: );
175: $tagRelationModel = Mage::getModel('tag/tag_relation');
176: $tagRelationModel->addRelations($model, $productIds);
177: }
178:
179: try {
180: $model->save();
181:
182: Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('The tag has been saved.'));
183: Mage::getSingleton('adminhtml/session')->setTagData(false);
184:
185: if (($continue = $this->getRequest()->getParam('continue'))) {
186: return $this->_redirect('*/tag/edit', array('tag_id' => $model->getId(), 'store' => $model->getStoreId(), 'ret' => $continue));
187: } else {
188: return $this->_redirect('*/tag/' . $this->getRequest()->getParam('ret', 'index'));
189: }
190: } catch (Exception $e) {
191: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
192: Mage::getSingleton('adminhtml/session')->setTagData($data);
193:
194: return $this->_redirect('*/*/edit', array('tag_id' => $model->getId(), 'store' => $model->getStoreId()));
195: }
196: }
197:
198: return $this->_redirect('*/tag/index', array('_current' => true));
199: }
200:
201: 202: 203: 204: 205:
206: public function deleteAction()
207: {
208: $model = $this->_initTag();
209: $session = Mage::getSingleton('adminhtml/session');
210:
211: if ($model && $model->getId()) {
212: try {
213: $model->delete();
214: $session->addSuccess(Mage::helper('adminhtml')->__('The tag has been deleted.'));
215: } catch (Exception $e) {
216: $session->addError($e->getMessage());
217: }
218: } else {
219: $session->addError(Mage::helper('adminhtml')->__('Unable to find a tag to delete.'));
220: }
221:
222: $this->getResponse()->setRedirect($this->getUrl('*/tag/' . $this->getRequest()->getParam('ret', 'index')));
223: }
224:
225: 226: 227: 228:
229: public function pendingAction()
230: {
231: $this->_title($this->__('Catalog'))
232: ->_title($this->__('Tags'))
233: ->_title($this->__('Pending Tags'));
234:
235: $this->_initAction()
236: ->_addBreadcrumb(Mage::helper('adminhtml')->__('Pending Tags'), Mage::helper('adminhtml')->__('Pending Tags'))
237: ->_setActiveMenu('catalog/tag/pending')
238: ->renderLayout();
239: }
240:
241: 242: 243: 244:
245: public function assignedAction()
246: {
247: $this->_title($this->__('Tags'))->_title($this->__('Assigned'));
248:
249: $this->_initTag();
250: $this->loadLayout();
251: $this->renderLayout();
252: }
253:
254: 255: 256: 257:
258: public function assignedGridOnlyAction()
259: {
260: $this->_initTag();
261: $this->loadLayout();
262: $this->renderLayout();
263: }
264:
265: 266: 267: 268:
269: public function productAction()
270: {
271: $this->_initTag();
272: $this->loadLayout();
273: $this->renderLayout();
274: }
275:
276: 277: 278: 279:
280: public function customerAction()
281: {
282: $this->_initTag();
283: $this->loadLayout();
284: $this->renderLayout();
285: }
286:
287: 288: 289: 290:
291: public function massDeleteAction()
292: {
293: $tagIds = $this->getRequest()->getParam('tag');
294: if(!is_array($tagIds)) {
295: Mage::getSingleton('adminhtml/session')->addError($this->__('Please select tag(s).'));
296: } else {
297: try {
298: foreach ($tagIds as $tagId) {
299: $tag = Mage::getModel('tag/tag')->load($tagId);
300: $tag->delete();
301: }
302: Mage::getSingleton('adminhtml/session')->addSuccess(
303: $this->__('Total of %d record(s) have been deleted.', count($tagIds))
304: );
305: } catch (Exception $e) {
306: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
307: }
308: }
309:
310: $this->_redirect('*/*/' . $this->getRequest()->getParam('ret', 'index'));
311: }
312:
313: 314: 315: 316:
317: public function massStatusAction()
318: {
319: $tagIds = $this->getRequest()->getParam('tag');
320: $storeId = (int)$this->getRequest()->getParam('store', 0);
321: if(!is_array($tagIds)) {
322:
323: Mage::getSingleton('adminhtml/session')->addError($this->__('Please select tag(s).'));
324: } else {
325: try {
326: foreach ($tagIds as $tagId) {
327: $tag = Mage::getModel('tag/tag')
328: ->load($tagId)
329: ->setStatus($this->getRequest()->getParam('status'));
330: $tag->save();
331: }
332: Mage::getSingleton('adminhtml/session')->addSuccess(
333: $this->__('Total of %d record(s) have been updated.', count($tagIds))
334: );
335: } catch (Exception $e) {
336: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
337: }
338: }
339: $ret = $this->getRequest()->getParam('ret') ? $this->getRequest()->getParam('ret') : 'index';
340: $this->_redirect('*/*/'.$ret);
341: }
342:
343: 344: 345: 346:
347: protected function _isAllowed()
348: {
349: switch ($this->getRequest()->getActionName()) {
350: case 'pending':
351: return Mage::getSingleton('admin/session')->isAllowed('catalog/tag/pending');
352: break;
353: case 'all':
354: return Mage::getSingleton('admin/session')->isAllowed('catalog/tag/all');
355: break;
356: default:
357: return Mage::getSingleton('admin/session')->isAllowed('catalog/tag');
358: break;
359: }
360: }
361: }
362: