PageRenderTime 26ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/admin5513/autoupgrade/latest/prestashop/controllers/admin/AdminAttachmentsController.php

https://bitbucket.org/marketing_alfatec/colette
PHP | 182 lines | 143 code | 14 blank | 25 comment | 17 complexity | 63fce379a32436e964649d076c534106 MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2013 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-2013 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. class AdminAttachmentsControllerCore extends AdminController
  27. {
  28. protected $product_attachements = array();
  29. public function __construct()
  30. {
  31. $this->table = 'attachment';
  32. $this->className = 'Attachment';
  33. $this->lang = true;
  34. $this->addRowAction('edit');
  35. $this->addRowAction('delete');
  36. $this->fields_list = array(
  37. 'id_attachment' => array(
  38. 'title' => $this->l('ID'),
  39. 'align' => 'center',
  40. 'width' => 25
  41. ),
  42. 'name' => array(
  43. 'title' => $this->l('Name')
  44. ),
  45. 'file' => array(
  46. 'title' => $this->l('File')
  47. )
  48. );
  49. parent::__construct();
  50. }
  51. public function renderForm()
  52. {
  53. $this->fields_form = array(
  54. 'legend' => array(
  55. 'title' => $this->l('Attachment'),
  56. 'image' => '../img/t/AdminAttachments.gif'
  57. ),
  58. 'input' => array(
  59. array(
  60. 'type' => 'text',
  61. 'label' => $this->l('Filename:'),
  62. 'name' => 'name',
  63. 'size' => 33,
  64. 'required' => true,
  65. 'lang' => true,
  66. ),
  67. array(
  68. 'type' => 'textarea',
  69. 'label' => $this->l('Description:'),
  70. 'name' => 'description',
  71. 'cols' => 40,
  72. 'rows' => 10,
  73. 'lang' => true,
  74. ),
  75. array(
  76. 'type' => 'file',
  77. 'label' => $this->l('File:'),
  78. 'name' => 'file',
  79. 'desc' => $this->l('Upload a file from your computer.')
  80. ),
  81. ),
  82. 'submit' => array(
  83. 'title' => $this->l('Save '),
  84. 'class' => 'button'
  85. )
  86. );
  87. return parent::renderForm();
  88. }
  89. public function getList($id_lang, $order_by = null, $order_way = null, $start = 0, $limit = null, $id_lang_shop = false)
  90. {
  91. parent::getList((int)$id_lang, $order_by, $order_way, $start, $limit, $id_lang_shop);
  92. if (count($this->_list))
  93. {
  94. $this->product_attachements = Attachment::getProductAttached((int)$id_lang, $this->_list);
  95. $list_product_list = array();
  96. foreach ($this->_list as $list)
  97. {
  98. $product_list = '';
  99. if (isset($this->product_attachements[$list['id_attachment']]))
  100. {
  101. foreach ($this->product_attachements[$list['id_attachment']] as $product)
  102. $product_list .= $product.', ';
  103. }
  104. $list_product_list[$list['id_attachment']] = $product_list;
  105. }
  106. // Assign array in list_action_delete.tpl
  107. $this->tpl_delete_link_vars = array(
  108. 'product_list' => $list_product_list,
  109. 'product_attachements' => $this->product_attachements
  110. );
  111. }
  112. }
  113. public function postProcess()
  114. {
  115. if (_PS_MODE_DEMO_)
  116. {
  117. $this->errors[] = Tools::displayError('This functionality has been disabled.');
  118. return;
  119. }
  120. if (Tools::isSubmit('submitAdd'.$this->table))
  121. {
  122. $id = (int)Tools::getValue('id_attachment');
  123. if ($id && $a = new Attachment($id))
  124. {
  125. $_POST['file'] = $a->file;
  126. $_POST['mime'] = $a->mime;
  127. }
  128. if (!count($this->errors))
  129. {
  130. if (isset($_FILES['file']) && is_uploaded_file($_FILES['file']['tmp_name']))
  131. {
  132. if ($_FILES['file']['size'] > (Configuration::get('PS_ATTACHMENT_MAXIMUM_SIZE') * 1024 * 1024))
  133. $this->errors[] = sprintf(
  134. $this->l('The file is too large. Maximum size allowed is: %1$d kB. The file you\'re trying to upload is: %2$d kB.'),
  135. (Configuration::get('PS_ATTACHMENT_MAXIMUM_SIZE') * 1024),
  136. number_format(($_FILES['file']['size'] / 1024), 2, '.', '')
  137. );
  138. else
  139. {
  140. do $uniqid = sha1(microtime());
  141. while (file_exists(_PS_DOWNLOAD_DIR_.$uniqid));
  142. if (!copy($_FILES['file']['tmp_name'], _PS_DOWNLOAD_DIR_.$uniqid))
  143. $this->errors[] = $this->l('Failed to copy the file.');
  144. $_POST['file_name'] = $_FILES['file']['name'];
  145. @unlink($_FILES['file']['tmp_name']);
  146. $_POST['file'] = $uniqid;
  147. $_POST['mime'] = $_FILES['file']['type'];
  148. }
  149. }
  150. else if (array_key_exists('file', $_FILES) && (int)$_FILES['file']['error'] === 1)
  151. {
  152. $max_upload = (int)ini_get('upload_max_filesize');
  153. $max_post = (int)ini_get('post_max_size');
  154. $upload_mb = min($max_upload, $max_post);
  155. $this->errors[] = sprintf(
  156. $this->l('The file %1$s exceeds the size allowed by the server. The limit is set to %2$d MB.'),
  157. '<b>'.$_FILES['file']['name'].'</b> ',
  158. '<b>'.$upload_mb.'</b>'
  159. );
  160. }
  161. else if (!empty($_FILES['file']['tmp_name']))
  162. $this->errors[] = $this->l('Upload error. Please check your server configurations for the maximum upload size allowed.');
  163. }
  164. $this->validateRules();
  165. }
  166. return parent::postProcess();
  167. }
  168. }