PageRenderTime 67ms CodeModel.GetById 39ms RepoModel.GetById 0ms app.codeStats 0ms

/public/shop/admin/controller/pages/catalog/download.php

https://bitbucket.org/rusbal/club4causes
PHP | 374 lines | 290 code | 60 blank | 24 comment | 40 complexity | dbc3bf495066a3a600d62cf11caaf8b9 MD5 | raw file
  1. <?php
  2. /*------------------------------------------------------------------------------
  3. $Id$
  4. Shopping Cart
  5. http://dev.club4causes.com/shop
  6. Club4Causes v1.0
  7. This source file is subject to Open Software License (OSL 3.0)
  8. License details is bundled with this package in the file LICENSE.txt.
  9. It is also available at this URL:
  10. <http://www.opensource.org/licenses/OSL-3.0>
  11. UPGRADE NOTE:
  12. Do not edit or add to this file if you wish to upgrade Shopping Cart to newer
  13. versions in the future. If you wish to customize Shopping Cart for your
  14. needs please refer to http://dev.club4causes.com/shop for more information.
  15. ------------------------------------------------------------------------------*/
  16. if (! defined ( 'DIR_CORE' ) || !IS_ADMIN) {
  17. header ( 'Location: static_pages/' );
  18. }
  19. class ControllerPagesCatalogDownload extends AController {
  20. private $error = array();
  21. public $data = array();
  22. public function main() {
  23. //init controller data
  24. $this->extensions->hk_InitData($this,__FUNCTION__);
  25. $this->view->assign('success', $this->session->data['success']);
  26. if (isset($this->session->data['success'])) {
  27. unset($this->session->data['success']);
  28. }
  29. $this->document->initBreadcrumb( array (
  30. 'href' => $this->html->getSecureURL('index/home'),
  31. 'text' => $this->language->get('text_home'),
  32. 'separator' => FALSE
  33. ));
  34. $this->document->addBreadcrumb( array (
  35. 'href' => $this->html->getSecureURL('catalog/download'),
  36. 'text' => $this->language->get('heading_title'),
  37. 'separator' => ' :: '
  38. ));
  39. $grid_settings = array(
  40. 'table_id' => 'download_grid',
  41. 'url' => $this->html->getSecureURL('listing_grid/download'),
  42. 'editurl' => $this->html->getSecureURL('listing_grid/download/update'),
  43. 'update_field' => $this->html->getSecureURL('listing_grid/download/update_field'),
  44. 'sortname' => 'name',
  45. 'actions' => array(
  46. 'edit' => array(
  47. 'text' => $this->language->get('text_edit'),
  48. 'href' => $this->html->getSecureURL('catalog/download/update', '&download_id=%ID%')
  49. ),
  50. 'delete' => array(
  51. 'text' => $this->language->get('button_delete'),
  52. )
  53. ),
  54. );
  55. $grid_settings['colNames'] = array(
  56. $this->language->get('column_name'),
  57. $this->language->get('column_remaining'),
  58. );
  59. $grid_settings['colModel'] = array(
  60. array(
  61. 'name' => 'name',
  62. 'index' => 'name',
  63. 'width' => 600,
  64. 'align' => 'center',
  65. ),
  66. array(
  67. 'name' => 'remaining',
  68. 'index' => 'remaining',
  69. 'width' => 200,
  70. 'align' => 'center',
  71. 'search' => false,
  72. ),
  73. );
  74. $grid = $this->dispatch('common/listing_grid', array( $grid_settings ) );
  75. $this->view->assign('listing_grid', $grid->dispatchGetOutput());
  76. $this->view->assign('help_url', $this->gen_help_url('download_listing') );
  77. $this->document->setTitle( $this->language->get('heading_title') );
  78. $this->view->assign( 'insert', $this->html->getSecureURL('catalog/download/insert') );
  79. $this->view->assign('form_language_switch', $this->html->getContentLanguageSwitcher());
  80. $this->view->assign('language_id', $this->session->data['content_language_id']);
  81. $this->processTemplate('pages/catalog/download_list.tpl' );
  82. //update controller data
  83. $this->extensions->hk_UpdateData($this,__FUNCTION__);
  84. }
  85. public function insert() {
  86. //init controller data
  87. $this->extensions->hk_InitData($this,__FUNCTION__);
  88. $this->document->setTitle( $this->language->get('heading_title') );
  89. $this->view->assign('error_warning', $this->error['warning']);
  90. $this->view->assign('success', $this->session->data['success']);
  91. if (isset($this->session->data['success'])) {
  92. unset($this->session->data['success']);
  93. }
  94. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->_validateForm()) {
  95. $data = array();
  96. $this->request->post['download'] = html_entity_decode($this->request->post['download'], ENT_COMPAT, 'UTF-8');
  97. $data['download'] = $this->request->post['download'];
  98. $data['mask'] = $this->request->post['mask'];
  99. $download_id = $this->model_catalog_download->addDownload(array_merge($this->request->post, $data));
  100. $this->session->data['success'] = $this->language->get('text_success');
  101. $this->redirect($this->html->getSecureURL('catalog/download/update', '&download_id=' . $download_id ));
  102. }
  103. $this->_getForm();
  104. //update controller data
  105. $this->extensions->hk_UpdateData($this,__FUNCTION__);
  106. }
  107. public function update() {
  108. //init controller data
  109. $this->extensions->hk_InitData($this,__FUNCTION__);
  110. $this->document->setTitle( $this->language->get('heading_title') );
  111. $this->view->assign('error_warning', $this->error['warning']);
  112. $this->view->assign('success', $this->session->data['success']);
  113. if (isset($this->session->data['success'])) {
  114. unset($this->session->data['success']);
  115. }
  116. if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->_validateForm()) {
  117. $data = array();
  118. $this->request->post['download'] = html_entity_decode($this->request->post['download'], ENT_COMPAT, 'UTF-8');
  119. $data['download'] = $this->request->post['download'];
  120. $data['mask'] = $this->request->post['mask'];
  121. $this->model_catalog_download->editDownload($this->request->get['download_id'], array_merge($this->request->post, $data));
  122. $this->session->data['success'] = $this->language->get('text_success');
  123. $this->redirect($this->html->getSecureURL('catalog/download/update', '&download_id=' . $this->request->get['download_id'] ));
  124. }
  125. $this->_getForm();
  126. //update controller data
  127. $this->extensions->hk_UpdateData($this,__FUNCTION__);
  128. }
  129. private function _getForm() {
  130. if (isset($this->error['warning'])) {
  131. $this->data['error_warning'] = $this->error['warning'];
  132. } else {
  133. $this->data['error_warning'] = '';
  134. }
  135. $this->data['error'] = $this->error;
  136. $this->data['language_id'] = $this->config->get('storefront_language_id');
  137. $this->document->initBreadcrumb( array(
  138. 'href' => $this->html->getSecureURL('index/home'),
  139. 'text' => $this->language->get('text_home'),
  140. 'separator' => FALSE
  141. ));
  142. $this->document->addBreadcrumb( array(
  143. 'href' => $this->html->getSecureURL('catalog/download'),
  144. 'text' => $this->language->get('heading_title'),
  145. 'separator' => ' :: '
  146. ));
  147. $this->data['cancel'] = $this->html->getSecureURL('catalog/download');
  148. if (isset($this->request->get['download_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
  149. $download_info = $this->model_catalog_download->getDownload($this->request->get['download_id']);
  150. }
  151. if (isset($download_info['filename']) ) {
  152. if ( is_file(DIR_RESOURCE.$download_info['filename']) ) {
  153. $rm = new AResource('archive');
  154. $id = $rm->getIdFromHexPath( str_replace($rm->getTypeDir(), '', $download_info['filename']) );
  155. $this->data['download_link'] = $this->html->getSecureURL('common/resource_library/get_resource_preview', '&resource_id='.$id);
  156. }
  157. $this->data['filename'] = $download_info['filename'];
  158. $this->data['mask'] = $download_info['mask'];
  159. } else {
  160. $this->data['filename'] = '';
  161. $this->data['mask'] = '';
  162. }
  163. if (isset($this->request->get['download_id'])) {
  164. $this->data['show_update'] = TRUE;
  165. } else {
  166. $this->data['show_update'] = FALSE;
  167. }
  168. if (isset($this->request->post['download_description'])) {
  169. $this->data['download_description'] = $this->request->post['download_description'];
  170. } elseif (isset($this->request->get['download_id'])) {
  171. $this->data['download_description'] = $this->model_catalog_download->getDownloadDescriptions($this->request->get['download_id']);
  172. } else {
  173. $this->data['download_description'] = array();
  174. }
  175. if (isset($this->request->post['remaining'])) {
  176. $this->data['remaining'] = $this->request->post['remaining'];
  177. } elseif (isset($download_info['remaining'])) {
  178. $this->data['remaining'] = $download_info['remaining'];
  179. } else {
  180. $this->data['remaining'] = 1;
  181. }
  182. if (isset($this->request->post['update'])) {
  183. $this->data['update'] = $this->request->post['update'];
  184. } else {
  185. $this->data['update'] = FALSE;
  186. }
  187. if (!isset($this->request->get['download_id'])) {
  188. $this->data['action'] = $this->html->getSecureURL('catalog/download/insert');
  189. $this->data['heading_title'] = $this->language->get('text_insert') . $this->language->get('text_download');
  190. $this->data['update'] = '';
  191. $form = new AForm('ST');
  192. } else {
  193. $this->data['action'] = $this->html->getSecureURL('catalog/download/update', '&download_id=' . $this->request->get['download_id'] );
  194. $this->data['heading_title'] = $this->language->get('text_edit') . $this->language->get('text_download'). ' - ' . $this->data['download_description'][$this->session->data['content_language_id']]['name'];
  195. $this->data['update'] = $this->html->getSecureURL('listing_grid/download/update_field','&id='.$this->request->get['download_id']);
  196. $form = new AForm('HS');
  197. }
  198. $this->document->addBreadcrumb( array (
  199. 'href' => $this->data['action'],
  200. 'text' => $this->data['heading_title'],
  201. 'separator' => ' :: '
  202. ));
  203. $form->setForm(array(
  204. 'form_name' => 'downloadFrm',
  205. 'update' => $this->data['update'],
  206. ));
  207. $this->data['form']['id'] = 'downloadFrm';
  208. $this->data['form']['form_open'] = $form->getFieldHtml(array(
  209. 'type' => 'form',
  210. 'name' => 'downloadFrm',
  211. 'action' => $this->data['action'],
  212. 'attr' => 'confirm-exit="true"',
  213. ));
  214. $this->data['form']['submit'] = $form->getFieldHtml(array(
  215. 'type' => 'button',
  216. 'name' => 'submit',
  217. 'text' => $this->language->get('button_save'),
  218. 'style' => 'button1',
  219. ));
  220. $this->data['form']['cancel'] = $form->getFieldHtml(array(
  221. 'type' => 'button',
  222. 'name' => 'cancel',
  223. 'text' => $this->language->get('button_cancel'),
  224. 'style' => 'button2',
  225. ));
  226. $this->data['form']['fields']['name'] = $form->getFieldHtml(array(
  227. 'type' => 'input',
  228. 'name' => 'download_description['.$this->session->data['content_language_id'].'][name]',
  229. 'value' => $this->data['download_description'][$this->session->data['content_language_id']]['name'],
  230. 'required' => true,
  231. 'attr' => ' maxlength="64" ',
  232. 'help_url' => $this->gen_help_url('name'),
  233. ));
  234. $this->data['form']['fields']['download'] = $form->getFieldHtml(array(
  235. 'type' => 'hidden',
  236. 'name' => 'download',
  237. 'value' => htmlspecialchars($this->data['filename'], ENT_COMPAT, 'UTF-8'),
  238. ));
  239. $this->data['form']['fields']['mask'] = $form->getFieldHtml(array(
  240. 'type' => 'hidden',
  241. 'name' => 'mask',
  242. 'value' => $this->data['mask'],
  243. ));
  244. $this->data['form']['fields']['remaining'] = $form->getFieldHtml(array(
  245. 'type' => 'input',
  246. 'name' => 'remaining',
  247. 'value' => $this->data['remaining']
  248. ));
  249. if ($this->data['show_update']) {
  250. $this->data['form']['fields']['update'] = $form->getFieldHtml(array(
  251. 'type' => 'checkbox',
  252. 'name' => 'update',
  253. 'value' => $this->data['update'],
  254. 'style' => 'no-save'
  255. ));
  256. }
  257. $this->view->assign('help_url', $this->gen_help_url('download_edit') );
  258. $this->view->batchAssign( $this->data );
  259. $this->view->assign('form_language_switch', $this->html->getContentLanguageSwitcher());
  260. $this->view->assign('language_id', $this->session->data['content_language_id']);
  261. $resources_scripts = $this->dispatch(
  262. 'responses/common/resource_library/get_resources_scripts',
  263. array(
  264. 'object_name' => '',
  265. 'object_id' => '',
  266. 'types' => 'archive',
  267. 'mode' => 'url',
  268. )
  269. );
  270. $this->view->assign('resources_scripts', $resources_scripts->dispatchGetOutput());
  271. $this->view->assign('rl_get_preview', $this->html->getSecureURL('common/resource_library/get_resource_preview'));
  272. $this->processTemplate('pages/catalog/download_form.tpl' );
  273. }
  274. private function _validateForm() {
  275. if (!$this->user->canModify('catalog/download')) {
  276. $this->error['warning'] = $this->language->get('error_permission');
  277. }
  278. foreach ($this->request->post['download_description'] as $language_id => $value) {
  279. if ((mb_strlen($value['name']) < 2) || mb_strlen($value['name']) > 64) {
  280. $this->error['name'] = $this->language->get('error_name');
  281. }
  282. }
  283. if (!$this->request->post['download']) {
  284. $this->error['file'] = $this->language->get('error_file');
  285. }
  286. if (!$this->error) {
  287. return TRUE;
  288. } else {
  289. return FALSE;
  290. }
  291. }
  292. private function _validateDelete() {
  293. if (!$this->user->canModify('catalog/download')) {
  294. $this->error['warning'] = $this->language->get('error_permission');
  295. }
  296. $this->loadModel('catalog/product');
  297. foreach ($this->request->post['selected'] as $download_id) {
  298. $product_total = $this->model_catalog_product->getTotalProductsByDownloadId($download_id);
  299. if ($product_total) {
  300. $this->error['warning'] = sprintf($this->language->get('error_product'), $product_total);
  301. }
  302. }
  303. if (!$this->error) {
  304. return TRUE;
  305. } else {
  306. return FALSE;
  307. }
  308. }
  309. }
  310. ?>