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: class Mage_Adminhtml_Catalog_SearchController extends Mage_Adminhtml_Controller_Action
29: {
30: protected function _initAction()
31: {
32: $this->loadLayout()
33: ->_setActiveMenu('catalog/search')
34: ->_addBreadcrumb(Mage::helper('catalog')->__('Search'), Mage::helper('catalog')->__('Search'))
35: ;
36: return $this;
37: }
38:
39: public function indexAction()
40: {
41: $this->_title($this->__('Catalog'))->_title($this->__('Search Terms'));
42:
43: $this->_initAction()
44: ->_addBreadcrumb(Mage::helper('catalog')->__('Catalog'), Mage::helper('catalog')->__('Catalog'))
45: ->_addContent($this->getLayout()->createBlock('adminhtml/catalog_search'))
46: ->renderLayout();
47: }
48:
49: public function newAction()
50: {
51: $this->_forward('edit');
52: }
53:
54: public function editAction()
55: {
56: $this->_title($this->__('Catalog'))->_title($this->__('Search Terms'));
57:
58: $id = $this->getRequest()->getParam('id');
59: $model = Mage::getModel('catalogsearch/query');
60:
61: if ($id) {
62: $model->load($id);
63: if (! $model->getId()) {
64: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('catalog')->__('This search no longer exists.'));
65: $this->_redirect('*/*');
66: return;
67: }
68: }
69:
70:
71: $data = Mage::getSingleton('adminhtml/session')->getPageData(true);
72: if (!empty($data)) {
73: $model->addData($data);
74: }
75:
76: Mage::register('current_catalog_search', $model);
77:
78: $this->_initAction();
79:
80: $this->_title($id ? $model->getQueryText() : $this->__('New Search'));
81:
82: $this->getLayout()->getBlock('head')->setCanLoadRulesJs(true);
83:
84: $this->getLayout()->getBlock('catalog_search_edit')
85: ->setData('action', $this->getUrl('*/catalog_search/save'));
86:
87: $this
88: ->_addBreadcrumb($id ? Mage::helper('catalog')->__('Edit Search') : Mage::helper('catalog')->__('New Search'), $id ? Mage::helper('catalog')->__('Edit Search') : Mage::helper('catalog')->__('New Search'));
89:
90: $this->renderLayout();
91: }
92:
93: 94: 95: 96:
97: public function saveAction()
98: {
99: $hasError = false;
100: $data = $this->getRequest()->getPost();
101: $queryId = $this->getRequest()->getPost('query_id', null);
102: if ($this->getRequest()->isPost() && $data) {
103:
104: $model = Mage::getModel('catalogsearch/query');
105:
106:
107: $queryText = $this->getRequest()->getPost('query_text', false);
108: $storeId = $this->getRequest()->getPost('store_id', false);
109:
110: try {
111: if ($queryText) {
112: $model->setStoreId($storeId);
113: $model->loadByQueryText($queryText);
114: if ($model->getId() && $model->getId() != $queryId) {
115: Mage::throwException(
116: Mage::helper('catalog')->__('Search Term with such search query already exists.')
117: );
118: } else if (!$model->getId() && $queryId) {
119: $model->load($queryId);
120: }
121: } else if ($queryId) {
122: $model->load($queryId);
123: }
124:
125: $model->addData($data);
126: $model->setIsProcessed(0);
127: $model->save();
128:
129: } catch (Mage_Core_Exception $e) {
130: $this->_getSession()->addError($e->getMessage());
131: $hasError = true;
132: } catch (Exception $e) {
133: $this->_getSession()->addException($e,
134: Mage::helper('catalog')->__('An error occurred while saving the search query.')
135: );
136: $hasError = true;
137: }
138: }
139:
140: if ($hasError) {
141: $this->_getSession()->setPageData($data);
142: $this->_redirect('*/*/edit', array('id' => $queryId));
143: } else {
144: $this->_redirect('*/*');
145: }
146: }
147:
148: public function deleteAction()
149: {
150: if ($id = $this->getRequest()->getParam('id')) {
151: try {
152: $model = Mage::getModel('catalogsearch/query');
153: $model->setId($id);
154: $model->delete();
155: Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('catalog')->__('The search was deleted.'));
156: $this->_redirect('*/*/');
157: return;
158: }
159: catch (Exception $e) {
160: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
161: $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
162: return;
163: }
164: }
165: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('catalog')->__('Unable to find a search term to delete.'));
166: $this->_redirect('*/*/');
167: }
168:
169: public function massDeleteAction()
170: {
171: $searchIds = $this->getRequest()->getParam('search');
172: if(!is_array($searchIds)) {
173: Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select catalog searches.'));
174: } else {
175: try {
176: foreach ($searchIds as $searchId) {
177: $model = Mage::getModel('catalogsearch/query')->load($searchId);
178: $model->delete();
179: }
180: Mage::getSingleton('adminhtml/session')->addSuccess(
181: Mage::helper('adminhtml')->__('Total of %d record(s) were deleted', count($searchIds))
182: );
183: } catch (Exception $e) {
184: Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
185: }
186: }
187:
188: $this->_redirect('*/*/index');
189: }
190:
191: protected function _isAllowed()
192: {
193: return Mage::getSingleton('admin/session')->isAllowed('catalog/search');
194: }
195: }
196: