/admin/controller/sale/voucher_theme.php

https://github.com/smadi/arabian-land · PHP · 413 lines · 304 code · 109 blank · 0 comment · 69 complexity · 52d8c2b150b3483bf9223f5ccdff1166 MD5 · raw file

  1. <?php
  2. class ControllerSaleVoucherTheme extends Controller {
  3. private $error = array();
  4. public function index() {
  5. $this->load->language('sale/voucher_theme');
  6. $this->document->setTitle($this->language->get('heading_title'));
  7. $this->load->model('sale/voucher_theme');
  8. $this->getList();
  9. }
  10. public function insert() {
  11. $this->load->language('sale/voucher_theme');
  12. $this->document->setTitle($this->language->get('heading_title'));
  13. $this->load->model('sale/voucher_theme');
  14. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
  15. $this->model_sale_voucher_theme->addVoucherTheme($this->request->post);
  16. $this->session->data['success'] = $this->language->get('text_success');
  17. $url = '';
  18. if (isset($this->request->get['sort'])) {
  19. $url .= '&sort=' . $this->request->get['sort'];
  20. }
  21. if (isset($this->request->get['order'])) {
  22. $url .= '&order=' . $this->request->get['order'];
  23. }
  24. if (isset($this->request->get['page'])) {
  25. $url .= '&page=' . $this->request->get['page'];
  26. }
  27. $this->redirect($this->url->link('sale/voucher_theme', 'token=' . $this->session->data['token'] . $url, 'SSL'));
  28. }
  29. $this->getForm();
  30. }
  31. public function update() {
  32. $this->load->language('sale/voucher_theme');
  33. $this->document->setTitle($this->language->get('heading_title'));
  34. $this->load->model('sale/voucher_theme');
  35. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
  36. $this->model_sale_voucher_theme->editVoucherTheme($this->request->get['voucher_theme_id'], $this->request->post);
  37. $this->session->data['success'] = $this->language->get('text_success');
  38. $url = '';
  39. if (isset($this->request->get['sort'])) {
  40. $url .= '&sort=' . $this->request->get['sort'];
  41. }
  42. if (isset($this->request->get['order'])) {
  43. $url .= '&order=' . $this->request->get['order'];
  44. }
  45. if (isset($this->request->get['page'])) {
  46. $url .= '&page=' . $this->request->get['page'];
  47. }
  48. $this->redirect($this->url->link('sale/voucher_theme', 'token=' . $this->session->data['token'] . $url, 'SSL'));
  49. }
  50. $this->getForm();
  51. }
  52. public function delete() {
  53. $this->load->language('sale/voucher_theme');
  54. $this->document->setTitle($this->language->get('heading_title'));
  55. $this->load->model('sale/voucher_theme');
  56. if (isset($this->request->post['selected']) && $this->validateDelete()) {
  57. foreach ($this->request->post['selected'] as $voucher_theme_id) {
  58. $this->model_sale_voucher_theme->deleteVoucherTheme($voucher_theme_id);
  59. }
  60. $this->session->data['success'] = $this->language->get('text_success');
  61. $url = '';
  62. if (isset($this->request->get['sort'])) {
  63. $url .= '&sort=' . $this->request->get['sort'];
  64. }
  65. if (isset($this->request->get['order'])) {
  66. $url .= '&order=' . $this->request->get['order'];
  67. }
  68. if (isset($this->request->get['page'])) {
  69. $url .= '&page=' . $this->request->get['page'];
  70. }
  71. $this->redirect($this->url->link('sale/voucher_theme', 'token=' . $this->session->data['token'] . $url, 'SSL'));
  72. }
  73. $this->getList();
  74. }
  75. private function getList() {
  76. if (isset($this->request->get['sort'])) {
  77. $sort = $this->request->get['sort'];
  78. } else {
  79. $sort = 'vtd.name';
  80. }
  81. if (isset($this->request->get['order'])) {
  82. $order = $this->request->get['order'];
  83. } else {
  84. $order = 'ASC';
  85. }
  86. if (isset($this->request->get['page'])) {
  87. $page = $this->request->get['page'];
  88. } else {
  89. $page = 1;
  90. }
  91. $url = '';
  92. if (isset($this->request->get['sort'])) {
  93. $url .= '&sort=' . $this->request->get['sort'];
  94. }
  95. if (isset($this->request->get['order'])) {
  96. $url .= '&order=' . $this->request->get['order'];
  97. }
  98. if (isset($this->request->get['page'])) {
  99. $url .= '&page=' . $this->request->get['page'];
  100. }
  101. $this->data['breadcrumbs'] = array();
  102. $this->data['breadcrumbs'][] = array(
  103. 'text' => $this->language->get('text_home'),
  104. 'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
  105. 'separator' => false
  106. );
  107. $this->data['breadcrumbs'][] = array(
  108. 'text' => $this->language->get('heading_title'),
  109. 'href' => $this->url->link('sale/voucher_theme', 'token=' . $this->session->data['token'] . $url, 'SSL'),
  110. 'separator' => ' :: '
  111. );
  112. $this->data['insert'] = $this->url->link('sale/voucher_theme/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
  113. $this->data['delete'] = $this->url->link('sale/voucher_theme/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');
  114. $this->data['voucher_themes'] = array();
  115. $data = array(
  116. 'sort' => $sort,
  117. 'order' => $order,
  118. 'start' => ($page - 1) * $this->config->get('config_admin_limit'),
  119. 'limit' => $this->config->get('config_admin_limit')
  120. );
  121. $voucher_theme_total = $this->model_sale_voucher_theme->getTotalVoucherThemes();
  122. $results = $this->model_sale_voucher_theme->getVoucherThemes($data);
  123. foreach ($results as $result) {
  124. $action = array();
  125. $action[] = array(
  126. 'text' => $this->language->get('text_edit'),
  127. 'href' => $this->url->link('sale/voucher_theme/update', 'token=' . $this->session->data['token'] . '&voucher_theme_id=' . $result['voucher_theme_id'] . $url, 'SSL')
  128. );
  129. $this->data['voucher_themes'][] = array(
  130. 'voucher_theme_id' => $result['voucher_theme_id'],
  131. 'name' => $result['name'],
  132. 'selected' => isset($this->request->post['selected']) && in_array($result['voucher_theme_id'], $this->request->post['selected']),
  133. 'action' => $action
  134. );
  135. }
  136. $this->data['heading_title'] = $this->language->get('heading_title');
  137. $this->data['text_no_results'] = $this->language->get('text_no_results');
  138. $this->data['column_name'] = $this->language->get('column_name');
  139. $this->data['column_action'] = $this->language->get('column_action');
  140. $this->data['button_insert'] = $this->language->get('button_insert');
  141. $this->data['button_delete'] = $this->language->get('button_delete');
  142. if (isset($this->error['warning'])) {
  143. $this->data['error_warning'] = $this->error['warning'];
  144. } else {
  145. $this->data['error_warning'] = '';
  146. }
  147. if (isset($this->session->data['success'])) {
  148. $this->data['success'] = $this->session->data['success'];
  149. unset($this->session->data['success']);
  150. } else {
  151. $this->data['success'] = '';
  152. }
  153. $url = '';
  154. if ($order == 'ASC') {
  155. $url .= '&order=DESC';
  156. } else {
  157. $url .= '&order=ASC';
  158. }
  159. if (isset($this->request->get['page'])) {
  160. $url .= '&page=' . $this->request->get['page'];
  161. }
  162. $this->data['sort_name'] = $this->url->link('sale/voucher_theme', 'token=' . $this->session->data['token'] . '&sort=name' . $url, 'SSL');
  163. $url = '';
  164. if (isset($this->request->get['sort'])) {
  165. $url .= '&sort=' . $this->request->get['sort'];
  166. }
  167. if (isset($this->request->get['order'])) {
  168. $url .= '&order=' . $this->request->get['order'];
  169. }
  170. $pagination = new Pagination();
  171. $pagination->total = $voucher_theme_total;
  172. $pagination->page = $page;
  173. $pagination->limit = $this->config->get('config_admin_limit');
  174. $pagination->text = $this->language->get('text_pagination');
  175. $pagination->url = $this->url->link('sale/voucher_theme', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
  176. $this->data['pagination'] = $pagination->render();
  177. $this->data['sort'] = $sort;
  178. $this->data['order'] = $order;
  179. $this->template = 'sale/voucher_theme_list.tpl';
  180. $this->children = array(
  181. 'common/header',
  182. 'common/footer',
  183. );
  184. $this->response->setOutput($this->render());
  185. }
  186. private function getForm() {
  187. $this->data['heading_title'] = $this->language->get('heading_title');
  188. $this->data['text_image_manager'] = $this->language->get('text_image_manager');
  189. $this->data['entry_name'] = $this->language->get('entry_name');
  190. $this->data['entry_image'] = $this->language->get('entry_image');
  191. $this->data['button_save'] = $this->language->get('button_save');
  192. $this->data['button_cancel'] = $this->language->get('button_cancel');
  193. if (isset($this->error['warning'])) {
  194. $this->data['error_warning'] = $this->error['warning'];
  195. } else {
  196. $this->data['error_warning'] = '';
  197. }
  198. if (isset($this->error['name'])) {
  199. $this->data['error_name'] = $this->error['name'];
  200. } else {
  201. $this->data['error_name'] = array();
  202. }
  203. if (isset($this->error['image'])) {
  204. $this->data['error_image'] = $this->error['image'];
  205. } else {
  206. $this->data['error_image'] = '';
  207. }
  208. $url = '';
  209. if (isset($this->request->get['sort'])) {
  210. $url .= '&sort=' . $this->request->get['sort'];
  211. }
  212. if (isset($this->request->get['order'])) {
  213. $url .= '&order=' . $this->request->get['order'];
  214. }
  215. if (isset($this->request->get['page'])) {
  216. $url .= '&page=' . $this->request->get['page'];
  217. }
  218. $this->data['breadcrumbs'] = array();
  219. $this->data['breadcrumbs'][] = array(
  220. 'text' => $this->language->get('text_home'),
  221. 'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
  222. 'separator' => false
  223. );
  224. $this->data['breadcrumbs'][] = array(
  225. 'text' => $this->language->get('heading_title'),
  226. 'href' => $this->url->link('sale/voucher_theme', 'token=' . $this->session->data['token'] . $url, 'SSL'),
  227. 'separator' => ' :: '
  228. );
  229. if (!isset($this->request->get['voucher_theme_id'])) {
  230. $this->data['action'] = $this->url->link('sale/voucher_theme/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
  231. } else {
  232. $this->data['action'] = $this->url->link('sale/voucher_theme/update', 'token=' . $this->session->data['token'] . '&voucher_theme_id=' . $this->request->get['voucher_theme_id'] . $url, 'SSL');
  233. }
  234. if (isset($this->request->get['voucher_theme_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
  235. $voucher_theme_info = $this->model_sale_voucher_theme->getVoucherTheme($this->request->get['voucher_theme_id']);
  236. }
  237. $this->data['cancel'] = $this->url->link('sale/voucher_theme', 'token=' . $this->session->data['token'] . $url, 'SSL');
  238. $this->data['token'] = $this->session->data['token'];
  239. $this->load->model('localisation/language');
  240. $this->data['languages'] = $this->model_localisation_language->getLanguages();
  241. if (isset($this->request->post['voucher_theme_description'])) {
  242. $this->data['voucher_theme_description'] = $this->request->post['voucher_theme_description'];
  243. } elseif (isset($this->request->get['voucher_theme_id'])) {
  244. $this->data['voucher_theme_description'] = $this->model_sale_voucher_theme->getVoucherThemeDescriptions($this->request->get['voucher_theme_id']);
  245. } else {
  246. $this->data['voucher_theme_description'] = array();
  247. }
  248. if (isset($this->request->post['image'])) {
  249. $this->data['image'] = $this->request->post['image'];
  250. } elseif (isset($voucher_theme_info)) {
  251. $this->data['image'] = $voucher_theme_info['image'];
  252. } else {
  253. $this->data['image'] = '';
  254. }
  255. $this->load->model('tool/image');
  256. if (isset($voucher_theme_info) && $voucher_theme_info['image'] && file_exists(DIR_IMAGE . $voucher_theme_info['image'])) {
  257. $this->data['preview'] = $this->model_tool_image->resize($voucher_theme_info['image'], 100, 100);
  258. } else {
  259. $this->data['preview'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
  260. }
  261. $this->template = 'sale/voucher_theme_form.tpl';
  262. $this->children = array(
  263. 'common/header',
  264. 'common/footer',
  265. );
  266. $this->response->setOutput($this->render());
  267. }
  268. private function validateForm() {
  269. if (!$this->user->hasPermission('modify', 'sale/voucher_theme')) {
  270. $this->error['warning'] = $this->language->get('error_permission');
  271. }
  272. foreach ($this->request->post['voucher_theme_description'] as $language_id => $value) {
  273. if ((strlen(utf8_decode($value['name'])) < 3) || (strlen(utf8_decode($value['name'])) > 32)) {
  274. $this->error['name'][$language_id] = $this->language->get('error_name');
  275. }
  276. }
  277. if (!$this->request->post['image']) {
  278. $this->error['image'] = $this->language->get('error_image');
  279. }
  280. if (!$this->error) {
  281. return true;
  282. } else {
  283. return false;
  284. }
  285. }
  286. private function validateDelete() {
  287. if (!$this->user->hasPermission('modify', 'sale/voucher_theme')) {
  288. $this->error['warning'] = $this->language->get('error_permission');
  289. }
  290. $this->load->model('sale/voucher');
  291. foreach ($this->request->post['selected'] as $voucher_theme_id) {
  292. $voucher_total = $this->model_sale_voucher->getTotalVouchersByVoucherThemeId($voucher_theme_id);
  293. if ($voucher_total) {
  294. $this->error['warning'] = sprintf($this->language->get('error_voucher'), $voucher_total);
  295. }
  296. }
  297. if (!$this->error) {
  298. return true;
  299. } else {
  300. return false;
  301. }
  302. }
  303. }
  304. ?>