PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/app/app/code/local/Magegiant/Dailydeal/controllers/Adminhtml/Dailydealadmin/DailydealController.php

https://bitbucket.org/workhousemarc/fuelsmart
PHP | 264 lines | 225 code | 31 blank | 8 comment | 30 complexity | a0682f5bbd9b3a01add0e688f9910c52 MD5 | raw file
  1. <?php
  2. class Magegiant_Dailydeal_Adminhtml_Dailydealadmin_DailydealController extends Mage_Adminhtml_Controller_Action
  3. {
  4. protected function _initAction()
  5. {
  6. $this->loadLayout()
  7. ->_setActiveMenu('dailydeal/dailydeal')
  8. ->_addBreadcrumb(Mage::helper('adminhtml')->__('Deal Manager'), Mage::helper('adminhtml')->__('Deal Manager'));
  9. return $this;
  10. }
  11. public function indexAction()
  12. {
  13. $this->_initAction()
  14. ->renderLayout();
  15. }
  16. public function editAction()
  17. {
  18. $id = $this->getRequest()->getParam('id');
  19. $model = Mage::getModel('dailydeal/dailydeal')->load($id);
  20. if ($model->getId() || $id == 0) {
  21. $data = Mage::getSingleton('adminhtml/session')->getFormData(true);
  22. if (!empty($data))
  23. $model->setData($data);
  24. Mage::register('dailydeal_data', $model);
  25. $this->loadLayout();
  26. $this->_setActiveMenu('dailydeal/dailydeal');
  27. $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item Manager'), Mage::helper('adminhtml')->__('Deal Manager'));
  28. $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item News'), Mage::helper('adminhtml')->__('Deal News'));
  29. $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
  30. $this->_addContent($this->getLayout()->createBlock('dailydeal/adminhtml_dailydeal_edit'))
  31. ->_addLeft($this->getLayout()->createBlock('dailydeal/adminhtml_dailydeal_edit_tabs'));
  32. $this->renderLayout();
  33. } else {
  34. Mage::getSingleton('adminhtml/session')->addError(Mage::helper('dailydeal')->__('Deal does not exist'));
  35. $this->_redirect('*/*/');
  36. }
  37. }
  38. public function newAction()
  39. {
  40. $this->_forward('edit');
  41. }
  42. public function saveAction()
  43. {
  44. if ($data = $this->getRequest()->getPost()) {
  45. if (isset($_FILES['thumbnail']['name']) && $_FILES['thumbnail']['name'] != '') {
  46. try {
  47. /* Starting upload */
  48. $uploader1 = new Varien_File_Uploader('thumbnail');
  49. // Any extention would work
  50. $uploader1->setAllowedExtensions(array('jpg', 'jpeg', 'gif', 'png'));
  51. $uploader1->setAllowRenameFiles(false);
  52. // Set the file upload mode
  53. // false -> get the file directly in the specified folder
  54. // true -> get the file in the product like folders
  55. // (file.jpg will go in something like /media/f/i/file.jpg)
  56. $uploader1->setFilesDispersion(false);
  57. // We set media as the upload dir
  58. $path_thumbnail_image = Mage::getBaseDir('media') . DS;
  59. $uploader1->save($path_thumbnail_image . 'dailydeal/main', $_FILES['thumbnail']['name']);
  60. } catch (Exception $e) {
  61. }
  62. //this way the name is saved in DB
  63. $data['thumbnail_image'] = 'dailydeal/main/' . $_FILES['thumbnail']['name'];
  64. }
  65. if (isset($data['thumbnail'])) {
  66. $delete_thumbnail = ($data['thumbnail']);
  67. if ($delete_thumbnail['delete'] == 1) {
  68. $data['thumbnail_image'] = null;
  69. }
  70. }
  71. if (isset($data['candidate_product_id']) && $data['candidate_product_id']) {
  72. $data['product_id'] = $data['candidate_product_id'];
  73. }
  74. if (isset($data['product_name']) && $data['product_name'] == '') {
  75. unset($data['product_name']);
  76. }
  77. $data = $this->_filterDateTime($data, array('start_time', 'close_time'));
  78. $model = Mage::getModel('dailydeal/dailydeal')->load($this->getRequest()->getParam('id'));
  79. try {
  80. $data['start_time'] = date('Y-m-d H:i:s', Mage::getModel('core/date')->gmtTimestamp(strtotime($data['start_time'])));
  81. $data['close_time'] = date('Y-m-d H:i:s', Mage::getModel('core/date')->gmtTimestamp(strtotime($data['close_time'])));
  82. } catch (Exception $e) {
  83. }
  84. $price = Mage::getModel('catalog/product')->load($data['product_id'])->getPrice();
  85. $data['deal_price'] = $price - $data['save'] * $price / 100;
  86. $data['status'] = $data['status_form'];
  87. $data['store_id'] = implode(',', $data['stores']);
  88. $model->setData($data)
  89. ->setId($this->getRequest()->getParam('id'));
  90. try {
  91. if ($price >= $data['deal_price']) {
  92. if (Mage::getModel('core/date')->timestamp($data['start_time']) <= Mage::getModel('core/date')->timestamp($data['close_time'])) {
  93. $model->save();
  94. Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('dailydeal')->__('Deal was successfully saved'));
  95. } else {
  96. Mage::getSingleton('adminhtml/session')->addError(Mage::helper('dailydeal')->__('Start time must be smaller than close time!'));
  97. }
  98. } else {
  99. Mage::getSingleton('adminhtml/session')->addError(Mage::helper('dailydeal')->__('Deal Price must be smaller than Product price'));
  100. }
  101. Mage::getSingleton('adminhtml/session')->setFormData(false);
  102. if ($this->getRequest()->getParam('back')) {
  103. $this->_redirect('*/*/edit', array('id' => $model->getId()));
  104. return;
  105. }
  106. $this->_redirect('*/*/');
  107. return;
  108. } catch (Exception $e) {
  109. Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  110. Mage::getSingleton('adminhtml/session')->setFormData($data);
  111. $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
  112. return;
  113. }
  114. }
  115. Mage::getSingleton('adminhtml/session')->addError(Mage::helper('dailydeal')->__('Unable to find deal to save'));
  116. $this->_redirect('*/*/');
  117. }
  118. public function deleteAction()
  119. {
  120. if ($this->getRequest()->getParam('id') > 0) {
  121. try {
  122. $model = Mage::getModel('dailydeal/dailydeal');
  123. $model->delete();
  124. Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Deal was successfully deleted'));
  125. $this->_redirect('*/*/');
  126. } catch (Exception $e) {
  127. Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  128. $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
  129. }
  130. }
  131. $this->_redirect('*/*/');
  132. }
  133. public function massDeleteAction()
  134. {
  135. $dealIds = $this->getRequest()->getParam('dailydeal');
  136. if (!is_array($dealIds)) {
  137. Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select Deal(s)'));
  138. } else {
  139. try {
  140. $i = 0;
  141. foreach ($dealIds as $dealId) {
  142. $deal = Mage::getModel('dailydeal/dailydeal')->load($dealId);
  143. $deal->delete();
  144. $i++;
  145. }
  146. if ($i > 0) {
  147. Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Total of %d Deal(s) were successfully deleted', $i));
  148. } else {
  149. Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Deals should not be deleted'));
  150. }
  151. } catch (Exception $e) {
  152. Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  153. }
  154. }
  155. $this->_redirect('*/*/index');
  156. }
  157. public function massStatusAction()
  158. {
  159. $dealIds = $this->getRequest()->getParam('dailydeal');
  160. if (!is_array($dealIds)) {
  161. Mage::getSingleton('adminhtml/session')->addError($this->__('Please select Deal(s)'));
  162. } else {
  163. try {
  164. foreach ($dealIds as $dealId) {
  165. $deal = Mage::getSingleton('dailydeal/dailydeal')
  166. ->load($dealId)
  167. ->setStatus($this->getRequest()->getParam('status'))
  168. ->setIsMassupdate(true)
  169. ->save();
  170. }
  171. $this->_getSession()->addSuccess(
  172. $this->__('Total of %d Deal(s) were successfully updated', count($dealIds))
  173. );
  174. } catch (Exception $e) {
  175. $this->_getSession()->addError($e->getMessage());
  176. }
  177. }
  178. $this->_redirect('*/*/index');
  179. }
  180. public function exportCsvAction()
  181. {
  182. $fileName = 'dailydeal.csv';
  183. $content = $this->getLayout()->createBlock('dailydeal/adminhtml_dailydeal_grid')->getCsv();
  184. $this->_prepareDownloadResponse($fileName, $content);
  185. }
  186. public function exportXmlAction()
  187. {
  188. $fileName = 'dailydeal.xml';
  189. $content = $this->getLayout()->createBlock('dailydeal/adminhtml_dailydeal_grid')->getXml();
  190. $this->_prepareDownloadResponse($fileName, $content);
  191. }
  192. public function changeproductAction()
  193. {
  194. $product_id = $this->getRequest()->getParam('product_id');
  195. if ($product_id) {
  196. $product = Mage::getModel('catalog/product')->load($product_id);
  197. $product_name = $product->getName();
  198. $product_name = str_replace('"', '', $product_name);
  199. $product_name = str_replace("'", '', $product_name);
  200. $html = '<input type="hidden" id="newproduct_name" name="newproduct_name" value="' . $product_name . '" >';
  201. $this->getResponse()->setHeader('Content-type', 'application/x-json');
  202. $this->getResponse()->setBody($html);
  203. }
  204. }
  205. public function listproductAction()
  206. {
  207. $this->loadLayout();
  208. $this->getLayout()->getBlockSingleton('dailydeal/adminhtml_dailydeal_edit_tab_listproduct')
  209. ->setProduct($this->getRequest()->getPost('aproduct', null))
  210. ;
  211. $this->renderLayout();
  212. }
  213. public function listproductGridAction()
  214. {
  215. $this->loadLayout();
  216. $this->getLayout()->getBlockSingleton('dailydeal/adminhtml_dailydeal_edit_tab_listproduct')
  217. ->setProduct($this->getRequest()->getPost('aproduct', null))
  218. ;
  219. $this->renderLayout();
  220. }
  221. public function listorderGridAction()
  222. {
  223. $this->loadLayout();
  224. $this->getLayout()->getBlock('dailydeal.edit.tab.listorder')
  225. ->setOrder($this->getRequest()->getPost('aorder', null));
  226. $this->renderLayout();
  227. }
  228. }