PageRenderTime 59ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/local/Magiccart/Magicproduct/controllers/Adminhtml/ManageController.php

https://gitlab.com/blingbang2016/shop
PHP | 253 lines | 202 code | 32 blank | 19 comment | 26 complexity | e4abf88068f0708229125e0929466f80 MD5 | raw file
  1. <?php
  2. /**
  3. * Magiccart
  4. * @category Magiccart
  5. * @copyright Copyright (c) 2014 Magiccart (http://www.magiccart.net/)
  6. * @license http://www.magiccart.net/license-agreement.html
  7. * @Author: Magiccart<team.magiccart@gmail.com>
  8. * @@Create Date: 2014-07-28 17:49:00
  9. * @@Modify Date: 2015-12-22 14:38:09
  10. * @@Function:
  11. */
  12. ?>
  13. <?php
  14. class Magiccart_Magicproduct_Adminhtml_ManageController extends Mage_Adminhtml_Controller_Action
  15. {
  16. const NAVIGATION = 'magiccart/magicproduct_items';
  17. const PATH = 'magicproduct/identifier/';
  18. protected function _initAction() {
  19. $this->loadLayout()
  20. ->_setActiveMenu(self::NAVIGATION)
  21. ->_addBreadcrumb(Mage::helper('adminhtml')->__('Itmes Manager'), Mage::helper('adminhtml')->__('Itmes Manager'));
  22. return $this;
  23. }
  24. public function indexAction() {
  25. $this->_initAction()
  26. // ->_addContent($this->getLayout()->createBlock('adminhtml/store_switcher'))
  27. ->_addContent($this->getLayout()->createBlock('magicproduct/adminhtml_manage'))
  28. ->renderLayout();
  29. }
  30. public function gridAction()
  31. {
  32. $this->getResponse()->setBody(
  33. $this->getLayout()->createBlock('magicproduct/adminhtml_manage_grid')->toHtml()
  34. );
  35. }
  36. public function editAction() {
  37. $id = $this->getRequest()->getParam('id');
  38. $model = Mage::getModel('core/config_data')->load($id);
  39. if($model->getValue()) $model->addData(unserialize($model->getValue()));
  40. if ($model->getId() || $id == 0) {
  41. $data = Mage::getSingleton('adminhtml/session')->getFormData(true);
  42. if (!empty($data)) {
  43. $model->setData($data);
  44. }
  45. Mage::register('magicproduct_data', $model);
  46. $this->loadLayout();
  47. $this->_setActiveMenu(self::NAVIGATION);
  48. $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Itmes Manager'), Mage::helper('adminhtml')->__('Itmes Manager'));
  49. $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Itmes News'), Mage::helper('adminhtml')->__('Itmes News'));
  50. $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
  51. // $this->_addContent($this->getLayout()->createBlock('adminhtml/store_switcher'));
  52. $this->_addContent($this->getLayout()->createBlock('magicproduct/adminhtml_manage_edit'))
  53. // ->_addLeft($this->getLayout()->createBlock('adminhtml/system_config_switcher'))
  54. ->_addLeft($this->getLayout()->createBlock('magicproduct/adminhtml_manage_edit_tabs'));
  55. $this->renderLayout();
  56. } else {
  57. Mage::getSingleton('adminhtml/session')->addError(Mage::helper('magicproduct')->__('Item does not exist'));
  58. $this->_redirect('*/*/');
  59. }
  60. }
  61. public function newAction() {
  62. $this->_forward('edit');
  63. }
  64. public function saveAction() {
  65. if ($data = $this->getRequest()->getPost()) {
  66. if(isset($_FILES['image']['name']) && $_FILES['image']['name'] != '') {
  67. try {
  68. $uploader = new Varien_File_Uploader('image');
  69. $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
  70. $uploader->setAllowRenameFiles(true);
  71. // Set the file upload mode
  72. // false -> get the file directly in the specified folder
  73. // true -> get the file in the product like folders
  74. // (file.jpg will go in something like /media/f/i/file.jpg)
  75. $uploader->setFilesDispersion(false);
  76. // We set media as the upload dir
  77. $path = Mage::getBaseDir('media') . DS . 'magiccart' . DS .'magicproduct'. DS;
  78. $result = $uploader->save($path, $_FILES['image']['name'] );
  79. //this way the name is saved in DB
  80. $data['image'] = 'magiccart/magicproduct/'. $result['file'];
  81. } catch (Exception $e) {
  82. }
  83. } else {
  84. if(isset($data['image']['delete']) && $data['image']['delete'] == 1) {
  85. $data['image'] = '';
  86. } else {
  87. unset($data['image']);
  88. }
  89. }
  90. if(isset($data['stores'])) $data['stores'] = implode(',', $data['stores']);
  91. $id = $this->getRequest()->getParam('id');
  92. $path = self::PATH . $data['identifier'];
  93. $value = serialize($data);
  94. $model = Mage::getModel('core/config_data');
  95. if($id){
  96. $check = Mage::getModel('core/config_data')->getCollection()->addFieldToFilter('path', $path)->getFirstItem();
  97. if($check->getConfigId() != $id){
  98. Mage::getSingleton('adminhtml/session')->addError(Mage::helper('magicproduct')->__('Identifier already exists')); //Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  99. Mage::getSingleton('adminhtml/session')->setFormData($data);
  100. $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
  101. return;
  102. }
  103. $model->load($id);
  104. $model->setData('path', $path)
  105. ->setData('value', $value);
  106. } else {
  107. $model->setData('path', $path)
  108. ->setData('value', $value);
  109. }
  110. try {
  111. $model->save(); //$model->saveConfig($path, $value, $scope, $storeId);
  112. Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('magicproduct')->__('Item was successfully saved'));
  113. Mage::getSingleton('adminhtml/session')->setFormData(false);
  114. if ($this->getRequest()->getParam('back')) {
  115. $this->_redirect('*/*/edit', array('id' => $model->getId()));
  116. return;
  117. }
  118. $this->_redirect('*/*/');
  119. return;
  120. } catch (Exception $e) {
  121. Mage::getSingleton('adminhtml/session')->addError(Mage::helper('magicproduct')->__('Identifier already exists')); //Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  122. Mage::getSingleton('adminhtml/session')->setFormData($data);
  123. $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
  124. return;
  125. }
  126. }
  127. Mage::getSingleton('adminhtml/session')->addError(Mage::helper('magicproduct')->__('Unable to find Item to save'));
  128. $this->_redirect('*/*/');
  129. }
  130. public function deleteAction() {
  131. if( $this->getRequest()->getParam('id') > 0 ) {
  132. try {
  133. $model = Mage::getModel('core/config_data');
  134. $model->setId($this->getRequest()->getParam('id'))->delete();
  135. Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully deleted'));
  136. $this->_redirect('*/*/');
  137. } catch (Exception $e) {
  138. Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  139. $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
  140. }
  141. }
  142. $this->_redirect('*/*/');
  143. }
  144. public function massDeleteAction() {
  145. $mpIds = $this->getRequest()->getParam('magicproduct');
  146. if(!is_array($mpIds)) {
  147. Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select Item(s)'));
  148. } else {
  149. try {
  150. foreach ($mpIds as $Id) {
  151. $model = Mage::getModel('core/config_data')->load($Id);
  152. $model->delete();
  153. }
  154. Mage::getSingleton('adminhtml/session')->addSuccess(
  155. Mage::helper('adminhtml')->__(
  156. 'Total of %d record(s) were successfully deleted', count($mpIds)
  157. )
  158. );
  159. } catch (Exception $e) {
  160. Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  161. }
  162. }
  163. $this->_redirect('*/*/index');
  164. }
  165. public function massStatusAction()
  166. {
  167. $mpIds = $this->getRequest()->getParam('magicproduct');
  168. if(!is_array($mpIds)) {
  169. Mage::getSingleton('adminhtml/session')->addError($this->__('Please select Item(s)'));
  170. } else {
  171. try {
  172. foreach ($mpIds as $Id) {
  173. $model = Mage::getModel('core/config_data')->load($Id);
  174. $tmp = unserialize($model->getValue());
  175. $tmp['status'] = $this->getRequest()->getParam('status');
  176. $model->setValue(serialize($tmp))
  177. ->setIsMassupdate(true)
  178. ->save();
  179. }
  180. $this->_getSession()->addSuccess(
  181. $this->__('Total of %d record(s) were successfully updated', count($mpIds))
  182. );
  183. } catch (Exception $e) {
  184. $this->_getSession()->addError($e->getMessage());
  185. }
  186. }
  187. $this->_redirect('*/*/index');
  188. }
  189. public function exportCsvAction()
  190. {
  191. $fileName = 'magicproduct.csv';
  192. $content = $this->getLayout()
  193. ->createBlock('magicproduct/adminhtml_manage_grid')
  194. ->getCsv();
  195. $this->_sendUploadResponse($fileName, $content);
  196. }
  197. public function exportXmlAction()
  198. {
  199. $fileName = 'magicproduct.xml';
  200. $content = $this->getLayout()
  201. ->createBlock('magicproduct/adminhtml_manage_grid')
  202. ->getXml();
  203. $this->_sendUploadResponse($fileName, $content);
  204. }
  205. protected function _sendUploadResponse($fileName, $content, $contentType='application/octet-stream')
  206. {
  207. $response = $this->getResponse();
  208. $response->setHeader('HTTP/1.1 200 OK','');
  209. $response->setHeader('Pragma', 'public', true);
  210. $response->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true);
  211. $response->setHeader('Content-Disposition', 'attachment; filename='.$fileName);
  212. $response->setHeader('Last-Modified', date('r'));
  213. $response->setHeader('Accept-Ranges', 'bytes');
  214. $response->setHeader('Content-Length', strlen($content));
  215. $response->setHeader('Content-type', $contentType);
  216. $response->setBody($content);
  217. $response->sendResponse();
  218. die;
  219. }
  220. protected function _isAllowed() { return true; }
  221. }