PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/prestashop/_/controllers/admin/AdminAttachmentsController.php

https://gitlab.com/A.Julien/sendstockbymail-module-prestashop
PHP | 256 lines | 200 code | 27 blank | 29 comment | 28 complexity | 76f9f217a2c35bfc4a9cfc14eaeb1638 MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2016 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2016 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. /**
  27. * @property Attachment $object
  28. */
  29. class AdminAttachmentsControllerCore extends AdminController
  30. {
  31. public $bootstrap = true ;
  32. protected $product_attachements = array();
  33. public function __construct()
  34. {
  35. $this->table = 'attachment';
  36. $this->className = 'Attachment';
  37. $this->lang = true;
  38. $this->addRowAction('edit');
  39. $this->addRowAction('view');
  40. $this->addRowAction('delete');
  41. $this->_select = 'IFNULL(virtual_product_attachment.products, 0) as products';
  42. $this->_join = 'LEFT JOIN (SELECT id_attachment, COUNT(*) as products FROM '._DB_PREFIX_.'product_attachment GROUP BY id_attachment) AS virtual_product_attachment ON a.id_attachment = virtual_product_attachment.id_attachment';
  43. $this->_use_found_rows = false;
  44. $this->fields_list = array(
  45. 'id_attachment' => array(
  46. 'title' => $this->l('ID'),
  47. 'align' => 'center',
  48. 'class' => 'fixed-width-xs'
  49. ),
  50. 'name' => array(
  51. 'title' => $this->l('Name')
  52. ),
  53. 'file' => array(
  54. 'title' => $this->l('File')
  55. ),
  56. 'file_size' => array(
  57. 'title' => $this->l('Size'),
  58. 'callback' => 'displayHumanReadableSize'
  59. ),
  60. 'products' => array(
  61. 'title' => $this->l('Associated with'),
  62. 'suffix' => $this->l('product(s)'),
  63. 'filter_key' => 'virtual_product_attachment!products',
  64. ),
  65. );
  66. $this->bulk_actions = array(
  67. 'delete' => array(
  68. 'text' => $this->l('Delete selected'),
  69. 'icon' => 'icon-trash',
  70. 'confirm' => $this->l('Delete selected items?')
  71. )
  72. );
  73. parent::__construct();
  74. }
  75. public function setMedia()
  76. {
  77. parent::setMedia();
  78. $this->addJs(_PS_JS_DIR_.'/admin/attachments.js');
  79. Media::addJsDefL('confirm_text', $this->l('This attachment is associated with the following products, do you really want to delete it?', null, true, false));
  80. }
  81. public static function displayHumanReadableSize($size)
  82. {
  83. return Tools::formatBytes($size);
  84. }
  85. public function initPageHeaderToolbar()
  86. {
  87. if (empty($this->display)) {
  88. $this->page_header_toolbar_btn['new_attachment'] = array(
  89. 'href' => self::$currentIndex.'&addattachment&token='.$this->token,
  90. 'desc' => $this->l('Add new attachment', null, null, false),
  91. 'icon' => 'process-icon-new'
  92. );
  93. }
  94. parent::initPageHeaderToolbar();
  95. }
  96. public function renderView()
  97. {
  98. if (($obj = $this->loadObject(true)) && Validate::isLoadedObject($obj)) {
  99. $link = $this->context->link->getPageLink('attachment', true, null, 'id_attachment='.$obj->id);
  100. Tools::redirectLink($link);
  101. }
  102. return $this->displayWarning($this->l('File not found'));
  103. }
  104. public function renderForm()
  105. {
  106. if (($obj = $this->loadObject(true)) && Validate::isLoadedObject($obj)) {
  107. /** @var Attachment $obj */
  108. $link = $this->context->link->getPageLink('attachment', true, null, 'id_attachment='.$obj->id);
  109. if (file_exists(_PS_DOWNLOAD_DIR_.$obj->file)) {
  110. $size = round(filesize(_PS_DOWNLOAD_DIR_.$obj->file) / 1024);
  111. }
  112. }
  113. $this->fields_form = array(
  114. 'legend' => array(
  115. 'title' => $this->l('Attachment'),
  116. 'icon' => 'icon-paper-clip'
  117. ),
  118. 'input' => array(
  119. array(
  120. 'type' => 'text',
  121. 'label' => $this->l('Filename'),
  122. 'name' => 'name',
  123. 'required' => true,
  124. 'lang' => true,
  125. 'col' => 4
  126. ),
  127. array(
  128. 'type' => 'textarea',
  129. 'label' => $this->l('Description'),
  130. 'name' => 'description',
  131. 'lang' => true,
  132. 'col' => 6
  133. ),
  134. array(
  135. 'type' => 'file',
  136. 'file' => isset($link) ? $link : null,
  137. 'size' => isset($size) ? $size : null,
  138. 'label' => $this->l('File'),
  139. 'name' => 'file',
  140. 'required' => true,
  141. 'col' => 6
  142. ),
  143. ),
  144. 'submit' => array(
  145. 'title' => $this->l('Save'),
  146. )
  147. );
  148. return parent::renderForm();
  149. }
  150. public function getList($id_lang, $order_by = null, $order_way = null, $start = 0, $limit = null, $id_lang_shop = false)
  151. {
  152. parent::getList((int)$id_lang, $order_by, $order_way, $start, $limit, $id_lang_shop);
  153. if (count($this->_list)) {
  154. $this->product_attachements = Attachment::getProductAttached((int)$id_lang, $this->_list);
  155. $list_product_list = array();
  156. foreach ($this->_list as $list) {
  157. $product_list = '';
  158. if (isset($this->product_attachements[$list['id_attachment']])) {
  159. foreach ($this->product_attachements[$list['id_attachment']] as $product) {
  160. $product_list .= $product.', ';
  161. }
  162. $product_list = rtrim($product_list, ', ');
  163. }
  164. $list_product_list[$list['id_attachment']] = $product_list;
  165. }
  166. // Assign array in list_action_delete.tpl
  167. $this->tpl_delete_link_vars = array(
  168. 'product_list' => $list_product_list,
  169. 'product_attachements' => $this->product_attachements
  170. );
  171. }
  172. }
  173. public function postProcess()
  174. {
  175. if (_PS_MODE_DEMO_) {
  176. $this->errors[] = Tools::displayError('This functionality has been disabled.');
  177. return;
  178. }
  179. if (Tools::isSubmit('submitAdd'.$this->table)) {
  180. $id = (int)Tools::getValue('id_attachment');
  181. if ($id && $a = new Attachment($id)) {
  182. $_POST['file'] = $a->file;
  183. $_POST['mime'] = $a->mime;
  184. }
  185. if (!count($this->errors)) {
  186. if (isset($_FILES['file']) && is_uploaded_file($_FILES['file']['tmp_name'])) {
  187. if ($_FILES['file']['size'] > (Configuration::get('PS_ATTACHMENT_MAXIMUM_SIZE') * 1024 * 1024)) {
  188. $this->errors[] = sprintf(
  189. $this->l('The file is too large. Maximum size allowed is: %1$d kB. The file you are trying to upload is %2$d kB.'),
  190. (Configuration::get('PS_ATTACHMENT_MAXIMUM_SIZE') * 1024),
  191. number_format(($_FILES['file']['size'] / 1024), 2, '.', '')
  192. );
  193. } else {
  194. do {
  195. $uniqid = sha1(microtime());
  196. } while (file_exists(_PS_DOWNLOAD_DIR_.$uniqid));
  197. if (!move_uploaded_file($_FILES['file']['tmp_name'], _PS_DOWNLOAD_DIR_.$uniqid)) {
  198. $this->errors[] = $this->l('Failed to copy the file.');
  199. }
  200. $_POST['file_name'] = $_FILES['file']['name'];
  201. @unlink($_FILES['file']['tmp_name']);
  202. if (!sizeof($this->errors) && isset($a) && file_exists(_PS_DOWNLOAD_DIR_.$a->file)) {
  203. unlink(_PS_DOWNLOAD_DIR_.$a->file);
  204. }
  205. $_POST['file'] = $uniqid;
  206. $_POST['mime'] = $_FILES['file']['type'];
  207. }
  208. } elseif (array_key_exists('file', $_FILES) && (int)$_FILES['file']['error'] === 1) {
  209. $max_upload = (int)ini_get('upload_max_filesize');
  210. $max_post = (int)ini_get('post_max_size');
  211. $upload_mb = min($max_upload, $max_post);
  212. $this->errors[] = sprintf(
  213. $this->l('The file %1$s exceeds the size allowed by the server. The limit is set to %2$d MB.'),
  214. '<b>'.$_FILES['file']['name'].'</b> ',
  215. '<b>'.$upload_mb.'</b>'
  216. );
  217. } elseif (!isset($a) || (isset($a) && !file_exists(_PS_DOWNLOAD_DIR_.$a->file))) {
  218. $this->errors[] = $this->l('Upload error. Please check your server configurations for the maximum upload size allowed.');
  219. }
  220. }
  221. $this->validateRules();
  222. }
  223. $return = parent::postProcess();
  224. if (!$return && isset($uniqid) && file_exists(_PS_DOWNLOAD_DIR_.$uniqid)) {
  225. unlink(_PS_DOWNLOAD_DIR_.$uniqid);
  226. }
  227. return $return;
  228. }
  229. }