loadLayout() ->_setActiveMenu('catalog/search') ->_addBreadcrumb(Mage::helper('catalog')->__('Search'), Mage::helper('catalog')->__('Search')) ; return $this; } public function indexAction() { $this->_title($this->__('Catalog'))->_title($this->__('Search Terms')); $this->_initAction() ->_addBreadcrumb(Mage::helper('catalog')->__('Catalog'), Mage::helper('catalog')->__('Catalog')) ->_addContent($this->getLayout()->createBlock('adminhtml/catalog_search')) ->renderLayout(); } public function newAction() { $this->_forward('edit'); } public function editAction() { $this->_title($this->__('Catalog'))->_title($this->__('Search Terms')); $id = $this->getRequest()->getParam('id'); $model = Mage::getModel('catalogsearch/query'); if ($id) { $model->load($id); if (! $model->getId()) { Mage::getSingleton('adminhtml/session')->addError(Mage::helper('catalog')->__('This search no longer exists.')); $this->_redirect('*/*'); return; } } // set entered data if was error when we do save $data = Mage::getSingleton('adminhtml/session')->getPageData(true); if (!empty($data)) { $model->addData($data); } Mage::register('current_catalog_search', $model); $this->_initAction(); $this->_title($id ? $model->getQueryText() : $this->__('New Search')); $this->getLayout()->getBlock('head')->setCanLoadRulesJs(true); $this->getLayout()->getBlock('catalog_search_edit') ->setData('action', $this->getUrl('*/catalog_search/save')); $this ->_addBreadcrumb($id ? Mage::helper('catalog')->__('Edit Search') : Mage::helper('catalog')->__('New Search'), $id ? Mage::helper('catalog')->__('Edit Search') : Mage::helper('catalog')->__('New Search')); $this->renderLayout(); } /** * Save search query * */ public function saveAction() { $hasError = false; $data = $this->getRequest()->getPost(); $queryId = $this->getRequest()->getPost('query_id', null); if ($this->getRequest()->isPost() && $data) { /* @var $model Mage_CatalogSearch_Model_Query */ $model = Mage::getModel('catalogsearch/query'); // validate query $queryText = $this->getRequest()->getPost('query_text', false); $storeId = $this->getRequest()->getPost('store_id', false); try { if ($queryText) { $model->setStoreId($storeId); $model->loadByQueryText($queryText); if ($model->getId() && $model->getId() != $queryId) { Mage::throwException( Mage::helper('catalog')->__('Search Term with such search query already exists.') ); } else if (!$model->getId() && $queryId) { $model->load($queryId); } } else if ($queryId) { $model->load($queryId); } $model->addData($data); $model->setIsProcessed(0); $model->save(); } catch (Mage_Core_Exception $e) { $this->_getSession()->addError($e->getMessage()); $hasError = true; } catch (Exception $e) { $this->_getSession()->addException($e, Mage::helper('catalog')->__('An error occurred while saving the search query.') ); $hasError = true; } } if ($hasError) { $this->_getSession()->setPageData($data); $this->_redirect('*/*/edit', array('id' => $queryId)); } else { $this->_redirect('*/*'); } } public function deleteAction() { if ($id = $this->getRequest()->getParam('id')) { try { $model = Mage::getModel('catalogsearch/query'); $model->setId($id); $model->delete(); Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('catalog')->__('The search was deleted.')); $this->_redirect('*/*/'); return; } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id'))); return; } } Mage::getSingleton('adminhtml/session')->addError(Mage::helper('catalog')->__('Unable to find a search term to delete.')); $this->_redirect('*/*/'); } public function massDeleteAction() { $searchIds = $this->getRequest()->getParam('search'); if(!is_array($searchIds)) { Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select catalog searches.')); } else { try { foreach ($searchIds as $searchId) { $model = Mage::getModel('catalogsearch/query')->load($searchId); $model->delete(); } Mage::getSingleton('adminhtml/session')->addSuccess( Mage::helper('adminhtml')->__('Total of %d record(s) were deleted', count($searchIds)) ); } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); } } $this->_redirect('*/*/index'); } protected function _isAllowed() { return Mage::getSingleton('admin/session')->isAllowed('catalog/search'); } }