PageRenderTime 33ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/application/modules/default/controllers/AdminCategoriesController.php

https://gitlab.com/devtoannh/cafe
PHP | 250 lines | 179 code | 47 blank | 24 comment | 21 complexity | 79ce29171e5ee489353e66e24ab0f4cb MD5 | raw file
  1. <?php
  2. class AdminCategoriesController extends Zendvn_Controller_Action
  3. {
  4. //Mang tham so nhan duoc khi mot Action chay
  5. protected $_arrParam;
  6. //Duong dan cua Controller
  7. protected $_currentController;
  8. //Duong dan cua Action chinh
  9. protected $_actionMain;
  10. //Thong so phan trang
  11. protected $_paginator = array('itemCountPerPage' => 15, 'pageRange' => 10, 'currentPage' => 1);
  12. protected $_namespace;
  13. //Url page
  14. protected $_page = '';
  15. public function init()
  16. {
  17. //Mang tham so nhan duoc khi mot Action chay
  18. $this->_arrParam = $this->_request->getParams();
  19. //Duong dan cua Controller
  20. $this->_currentController = '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller'];
  21. //Duong dan cua Action chinh
  22. $this->_actionMain = '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller'] . '/index';
  23. //Luu cac du lieu filter vaof SESSION
  24. //Dat ten SESSION
  25. $this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
  26. $ssFilter = new Zend_Session_Namespace($this->_namespace);
  27. //Lay thong tin so phan tu tren mot trang
  28. if (isset($this->_arrParam['limitPage'])) {
  29. $ssFilter->limitPage = $this->_request->getParam('limitPage');
  30. $this->_paginator['itemCountPerPage'] = $ssFilter->limitPage;
  31. } elseif (!empty($ssFilter->limitPage)) {
  32. $this->_paginator['itemCountPerPage'] = $ssFilter->limitPage;
  33. }
  34. //Trang hien tai
  35. if (isset($this->_arrParam['page'])) {
  36. $this->_paginator['currentPage'] = $this->_arrParam['page'];
  37. $this->_page = '/page/' . $this->_arrParam['page'];
  38. }
  39. //Truyen thong tin phan trang vao mang du lieu
  40. $this->_arrParam['paginator'] = $this->_paginator;
  41. //$ssFilter->unsetAll();
  42. if (empty($ssFilter->col)) {
  43. $ssFilter->keywords = '';
  44. $ssFilter->col = 'c.id';
  45. $ssFilter->id = 'DESC';
  46. $ssFilter->order = 'DESC';
  47. }
  48. $this->_arrParam['ssFilter']['keywords'] = $ssFilter->keywords;
  49. $this->_arrParam['ssFilter']['col'] = $ssFilter->col;
  50. $this->_arrParam['ssFilter']['order'] = $ssFilter->order;
  51. if (empty($ssFilter->lang_code)) {
  52. $language = new Zend_Session_Namespace('language');
  53. $this->_arrParam['ssFilter']['lang_code'] = $language->lang;
  54. } else {
  55. $this->_arrParam['ssFilter']['lang_code'] = $ssFilter->lang_code;
  56. }
  57. //Truyen ra ngoai view
  58. $this->view->arrParam = $this->_arrParam;
  59. $this->view->currentController = $this->_currentController;
  60. $this->view->actionMain = $this->_actionMain;
  61. $siteConfig = Zend_Registry::get('siteConfig');
  62. $template_path = TEMPLATE_PATH . "/admin/" . $siteConfig['template']['admin'];
  63. $this->loadTemplate($template_path, 'template.ini', 'template');
  64. }
  65. public function addAction()
  66. {
  67. header('Content-Type: application/json');
  68. $this->_helper->layout->disableLayout();
  69. $this->_helper->viewRenderer->setNoRender();
  70. $tblAjax = new Default_Model_Categories();
  71. $id = $this->getRequest()->getParam('id');
  72. if ($this->_request->isPost()) {
  73. if ($id != 0) {
  74. $tblAjax->saveItem($this->_arrParam, array(
  75. 'task' => 'public-update'
  76. ));
  77. } else {
  78. $tblAjax->saveItem($this->_arrParam, array(
  79. 'task' => 'public-add'
  80. ));
  81. }
  82. echo json_encode($this->_arrParam);
  83. }
  84. }
  85. public function editAction()
  86. {
  87. header('Content-Type: application/json');
  88. $this->_helper->layout->disableLayout();
  89. $this->_helper->viewRenderer->setNoRender();
  90. $tblAjax = new Default_Model_Categories();
  91. $id = $this->getRequest()->getParam('id');
  92. if ($this->_request->isPost()) {
  93. echo json_encode($tblAjax->getDataID($id));
  94. }
  95. }
  96. public function indexAction()
  97. {
  98. $this->view->Title = 'Danh sách sản phẩm';
  99. $this->view->headTitle($this->view->Title, true);
  100. $cate = new Default_Model_Categories();
  101. $this->view->Cate = $cate;
  102. $tblContact = new Default_Model_Categories();
  103. $this->view->Items = $tblContact->listItem($this->_arrParam, array(
  104. 'task' => 'admin-list'
  105. ));
  106. }
  107. public function deleteAction()
  108. {
  109. $this->_helper->layout->disableLayout();
  110. $this->_helper->viewRenderer->setNoRender();
  111. if ($this->_request->isPost()) {
  112. if (!empty($this->_arrParam['id'])) {
  113. $tblContact = new Default_Model_Categories();
  114. $upload_path = $_SERVER['DOCUMENT_ROOT'] . ''.$tblContact->getUrlImage($this->_arrParam['id']);
  115. if(file_exists($upload_path))
  116. {
  117. echo $upload_path;
  118. $img=unlink($upload_path);
  119. echo "Xoa Duoc";
  120. }else{
  121. echo $upload_path;
  122. echo "Khong Xoa Duoc";
  123. }
  124. $tblContact->deleteItem($this->_arrParam, array(
  125. 'task' => 'admin-delete'
  126. ));
  127. }
  128. }
  129. }
  130. public function uploadfileAction()
  131. {
  132. $requests = $this->_request->getParams();
  133. try {
  134. $mode = $requests['mode'];
  135. $result = array();
  136. //Create & get path uploader dir
  137. $upload_path = PUBLIC_PATH . '/uploads/categories/';
  138. if (!is_dir($upload_path)) {
  139. @mkdir($upload_path, 0755, true);
  140. }
  141. //Check writeable
  142. if (!is_writable($upload_path)) {
  143. die(htmlspecialchars(json_encode(array(
  144. 'success' => false,
  145. 'msg' => 'Thư mục lưu ảnh không thể ghi. Vui lòng thử lại !'
  146. )), ENT_NOQUOTES));
  147. }
  148. // list of valid extensions, ex. array("jpeg", "xml", "bmp")
  149. $allowedExtensions = array(
  150. 'jpeg',
  151. 'jpg',
  152. 'png',
  153. 'bmp',
  154. 'gif'
  155. );
  156. // max file size in bytes
  157. $sizeLimit = 2 * 1024 * 1024;
  158. //Create a instance uploader
  159. $uploader = new Zendvn_qqFileUploader($allowedExtensions, $sizeLimit);
  160. //Upload file
  161. $result = $uploader->handleUpload($upload_path);
  162. if (isset($result['error'])) {
  163. die(htmlspecialchars(json_encode(array(
  164. 'success' => false,
  165. 'msg' => $result['error']
  166. )), ENT_NOQUOTES));
  167. }
  168. //File uploaded name
  169. $file_uploaded = $upload_path . $result['filename'];
  170. if ($mode == 'simple') {
  171. $max_width = 500;
  172. $max_height = 500;
  173. $filename = trim($requests['qqfile']);
  174. //Rename file after upload
  175. $filename_new = $filename != '' ? time() . '_' . $filename : time() . '.jpg';
  176. @rename($upload_path . $result['filename'], $upload_path . $filename_new);
  177. $result['mode'] = $mode;
  178. $result['image'] = $filename_new;
  179. $result['url'] = '/public/uploads/categories/' . $filename_new;
  180. $result['path'] = $upload_path;
  181. } else {
  182. $max_width = 1024;
  183. $max_height = 1024;
  184. //Rename file after upload
  185. $filename_new = time() . '.' . strtolower($result['ext']);
  186. @fclose($upload_path . $result['filename']);
  187. if (file_exists($upload_path . $filename_new)) {
  188. $filename_new = random_string('unique') . '.' . strtolower($result['ext']);
  189. }
  190. @rename($upload_path . $result['filename'], $upload_path . $filename_new);
  191. $result['image'] = $filename_new;
  192. $result['path'] = '/public/uploads/categories/' . $upload_type . '/' . $upload_id . '/' . $filename_new;
  193. ;
  194. }
  195. echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
  196. die();
  197. }
  198. catch (Exception $e) {
  199. print_r($e);
  200. die();
  201. }
  202. }
  203. }