PageRenderTime 39ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/controllers/admin/AdminCurrenciesController.php

https://gitlab.com/staging06/myproject
PHP | 364 lines | 298 code | 27 blank | 39 comment | 30 complexity | c477aefb1e43611a2e4b75e3768b1617 MD5 | raw file
  1. <?php
  2. /*
  3. * 2007-2015 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-2015 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 Currency $object
  28. */
  29. class AdminCurrenciesControllerCore extends AdminController
  30. {
  31. public function __construct()
  32. {
  33. $this->bootstrap = true;
  34. $this->table = 'currency';
  35. $this->className = 'Currency';
  36. $this->lang = false;
  37. $this->fields_list = array(
  38. 'id_currency' => array('title' => $this->l('ID'), 'align' => 'center', 'class' => 'fixed-width-xs'),
  39. 'name' => array('title' => $this->l('Currency')),
  40. 'iso_code' => array('title' => $this->l('ISO code'), 'align' => 'center', 'class' => 'fixed-width-xs'),
  41. 'iso_code_num' => array('title' => $this->l('ISO code number'), 'align' => 'center', 'class' => 'fixed-width-xs'),
  42. 'sign' => array('title' => $this->l('Symbol'), 'width' => 20, 'align' => 'center', 'orderby' => false, 'search' => false, 'class' => 'fixed-width-xs'),
  43. 'conversion_rate' => array('title' => $this->l('Exchange rate'), 'type' => 'float', 'align' => 'center', 'width' => 130, 'search' => false, 'filter_key' => 'currency_shop!conversion_rate'),
  44. 'active' => array('title' => $this->l('Enabled'), 'width' => 25, 'align' => 'center', 'active' => 'status', 'type' => 'bool', 'orderby' => false, 'class' => 'fixed-width-sm'),
  45. );
  46. $this->bulk_actions = array(
  47. 'delete' => array(
  48. 'text' => $this->l('Delete selected'),
  49. 'confirm' => $this->l('Delete selected items?'),
  50. 'icon' => 'icon-trash'
  51. )
  52. );
  53. $this->fields_options = array(
  54. 'change' => array(
  55. 'title' => $this->l('Currency rates'),
  56. 'image' => '../img/admin/exchangesrate.gif',
  57. 'description' => $this->l('Use PrestaShop\'s webservice to update your currency\'s exchange rates. However, please use caution: rates are provided as-is.'),
  58. 'submit' => array(
  59. 'title' => $this->l('Update currency rates'),
  60. 'name' => 'SubmitExchangesRates'
  61. )
  62. ),
  63. 'cron' => array(
  64. 'title' => $this->l('Automatically update currency rates'),
  65. 'image' => '../img/admin/tab-tools.gif',
  66. 'info' => '<div class="alert alert-block"><p>'.$this->l('Use PrestaShop\'s webservice to update your currency exchange rates. However, please use caution: rates are provided as-is.').'<br/>'.$this->l('You can place the following URL in your crontab file, or you can click it yourself regularly:').'</p>
  67. <p><strong><a href="'.Tools::getShopDomain(true, true).__PS_BASE_URI__.basename(_PS_ADMIN_DIR_).'/cron_currency_rates.php?secure_key='.md5(_COOKIE_KEY_.Configuration::get('PS_SHOP_NAME')).'" onclick="return !window.open($(this).attr(\'href\'));">'.Tools::getShopDomain(true, true).__PS_BASE_URI__.basename(_PS_ADMIN_DIR_).'/cron_currency_rates.php?secure_key='.md5(_COOKIE_KEY_.Configuration::get('PS_SHOP_NAME')).'</a></strong></p></div>',
  68. )
  69. );
  70. parent::__construct();
  71. $this->_select .= 'currency_shop.conversion_rate conversion_rate';
  72. $this->_join .= Shop::addSqlAssociation('currency', 'a');
  73. $this->_group .= 'GROUP BY a.id_currency';
  74. }
  75. public function renderList()
  76. {
  77. $this->addRowAction('edit');
  78. $this->addRowAction('delete');
  79. $this->_where = 'AND a.`deleted` = 0';
  80. return parent::renderList();
  81. }
  82. public function renderForm()
  83. {
  84. $this->fields_form = array(
  85. 'legend' => array(
  86. 'title' => $this->l('Currencies'),
  87. 'icon' => 'icon-money'
  88. ),
  89. 'input' => array(
  90. array(
  91. 'type' => 'text',
  92. 'label' => $this->l('Currency name'),
  93. 'name' => 'name',
  94. 'size' => 30,
  95. 'maxlength' => 32,
  96. 'required' => true,
  97. 'hint' => $this->l('Only letters and the minus character are allowed.')
  98. ),
  99. array(
  100. 'type' => 'text',
  101. 'label' => $this->l('ISO code'),
  102. 'name' => 'iso_code',
  103. 'maxlength' => 32,
  104. 'required' => true,
  105. 'hint' => $this->l('ISO code (e.g. USD for Dollars, EUR for Euros, etc.).')
  106. ),
  107. array(
  108. 'type' => 'text',
  109. 'label' => $this->l('Numeric ISO code'),
  110. 'name' => 'iso_code_num',
  111. 'maxlength' => 32,
  112. 'required' => true,
  113. 'hint' => $this->l('Numeric ISO code (e.g. 840 for Dollars, 978 for Euros, etc.).')
  114. ),
  115. array(
  116. 'type' => 'text',
  117. 'label' => $this->l('Symbol'),
  118. 'name' => 'sign',
  119. 'maxlength' => 8,
  120. 'required' => true,
  121. 'hint' => $this->l('Will appear in front office (e.g. $, &euro;, etc.)')
  122. ),
  123. array(
  124. 'type' => 'text',
  125. 'label' => $this->l('Exchange rate'),
  126. 'name' => 'conversion_rate',
  127. 'maxlength' => 11,
  128. 'required' => true,
  129. 'hint' => $this->l('Exchange rates are calculated from one unit of your shop\'s default currency. For example, if the default currency is euros and your chosen currency is dollars, type "1.20" (1&euro; = $1.20).')
  130. ),
  131. array(
  132. 'type' => 'select',
  133. 'label' => $this->l('Currency format'),
  134. 'name' => 'format',
  135. 'maxlength' => 11,
  136. 'required' => true,
  137. 'hint' =>$this->l('Applies to all prices (e.g. $1,240.15).'),
  138. 'options' => array(
  139. 'query' => array(
  140. array('key' => 1, 'name' => 'X0,000.00 ('.$this->l('Such as with Dollars').')'),
  141. array('key' => 2, 'name' => '0 000,00X ('.$this->l('Such as with Euros').')'),
  142. array('key' => 3, 'name' => 'X0.000,00'),
  143. array('key' => 4, 'name' => '0,000.00X'),
  144. array('key' => 5, 'name' => '0\'000.00X') // Added for the switzerland currency
  145. ),
  146. 'name' => 'name',
  147. 'id' => 'key'
  148. )
  149. ),
  150. array(
  151. 'type' => 'switch',
  152. 'label' => $this->l('Decimals'),
  153. 'name' => 'decimals',
  154. 'required' => false,
  155. 'is_bool' => true,
  156. 'hint' => $this->l('Display decimals in prices.'),
  157. 'values' => array(
  158. array(
  159. 'id' => 'decimals_on',
  160. 'value' => 1,
  161. 'label' => $this->l('Enabled')
  162. ),
  163. array(
  164. 'id' => 'decimals_off',
  165. 'value' => 0,
  166. 'label' => $this->l('Disabled')
  167. )
  168. ),
  169. ),
  170. array(
  171. 'type' => 'switch',
  172. 'label' => $this->l('Spacing'),
  173. 'name' => 'blank',
  174. 'required' => false,
  175. 'is_bool' => true,
  176. 'hint' => $this->l('Include a space between symbol and price (e.g. $1,240.15 -> $ 1,240.15).'),
  177. 'values' => array(
  178. array(
  179. 'id' => 'blank_on',
  180. 'value' => 1,
  181. 'label' => $this->l('Enabled')
  182. ),
  183. array(
  184. 'id' => 'blank_off',
  185. 'value' => 0,
  186. 'label' => $this->l('Disabled')
  187. )
  188. ),
  189. ),
  190. array(
  191. 'type' => 'switch',
  192. 'label' => $this->l('Enable'),
  193. 'name' => 'active',
  194. 'required' => false,
  195. 'is_bool' => true,
  196. 'values' => array(
  197. array(
  198. 'id' => 'active_on',
  199. 'value' => 1,
  200. 'label' => $this->l('Enabled')
  201. ),
  202. array(
  203. 'id' => 'active_off',
  204. 'value' => 0,
  205. 'label' => $this->l('Disabled')
  206. )
  207. )
  208. )
  209. )
  210. );
  211. if (Shop::isFeatureActive()) {
  212. $this->fields_form['input'][] = array(
  213. 'type' => 'shop',
  214. 'label' => $this->l('Shop association'),
  215. 'name' => 'checkBoxShopAsso',
  216. );
  217. }
  218. $this->fields_form['submit'] = array(
  219. 'title' => $this->l('Save'),
  220. );
  221. return parent::renderForm();
  222. }
  223. protected function checkDeletion($object)
  224. {
  225. if (Validate::isLoadedObject($object)) {
  226. if ($object->id == Configuration::get('PS_CURRENCY_DEFAULT')) {
  227. $this->errors[] = $this->l('You cannot delete the default currency');
  228. } else {
  229. return true;
  230. }
  231. } else {
  232. $this->errors[] = Tools::displayError('An error occurred while deleting the object.').'
  233. <b>'.$this->table.'</b> '.Tools::displayError('(cannot load object)');
  234. }
  235. return false;
  236. }
  237. protected function checkDisableStatus($object)
  238. {
  239. if (Validate::isLoadedObject($object)) {
  240. if ($object->active && $object->id == Configuration::get('PS_CURRENCY_DEFAULT')) {
  241. $this->errors[] = $this->l('You cannot disable the default currency');
  242. } else {
  243. return true;
  244. }
  245. } else {
  246. $this->errors[] = Tools::displayError('An error occurred while updating the status for an object.').'
  247. <b>'.$this->table.'</b> '.Tools::displayError('(cannot load object)');
  248. }
  249. return false;
  250. }
  251. /**
  252. * @see AdminController::processDelete()
  253. */
  254. public function processDelete()
  255. {
  256. $object = $this->loadObject();
  257. if (!$this->checkDeletion($object)) {
  258. return false;
  259. }
  260. return parent::processDelete();
  261. }
  262. protected function processBulkDelete()
  263. {
  264. if (is_array($this->boxes) && !empty($this->boxes)) {
  265. foreach ($this->boxes as $id_currency) {
  266. $object = new Currency((int)$id_currency);
  267. if (!$this->checkDeletion($object)) {
  268. return false;
  269. }
  270. }
  271. }
  272. return parent::processBulkDelete();
  273. }
  274. /**
  275. * @see AdminController::processStatus()
  276. */
  277. public function processStatus()
  278. {
  279. $object = $this->loadObject();
  280. if (!$this->checkDisableStatus($object)) {
  281. return false;
  282. }
  283. return parent::processStatus();
  284. }
  285. protected function processBulkDisableSelection()
  286. {
  287. if (is_array($this->boxes) && !empty($this->boxes)) {
  288. foreach ($this->boxes as $id_currency) {
  289. $object = new Currency((int)$id_currency);
  290. if (!$this->checkDisableStatus($object)) {
  291. return false;
  292. }
  293. }
  294. }
  295. return parent::processBulkDisableSelection();
  296. }
  297. /**
  298. * Update currency exchange rates
  299. */
  300. public function processExchangeRates()
  301. {
  302. if (!$this->errors = Currency::refreshCurrencies()) {
  303. Tools::redirectAdmin(self::$currentIndex.'&conf=6&token='.$this->token);
  304. }
  305. }
  306. /**
  307. * @see AdminController::initProcess()
  308. */
  309. public function initProcess()
  310. {
  311. if (Tools::isSubmit('SubmitExchangesRates')) {
  312. if ($this->tabAccess['edit'] === '1') {
  313. $this->action = 'exchangeRates';
  314. } else {
  315. $this->errors[] = Tools::displayError('You do not have permission to edit this.');
  316. }
  317. }
  318. if (Tools::isSubmit('submitAddcurrency') && !Tools::getValue('id_currency') && Currency::exists(Tools::getValue('iso_code'), Tools::getValue('iso_code_num'))) {
  319. $this->errors[] = Tools::displayError('This currency already exists.');
  320. }
  321. if (Tools::isSubmit('submitAddcurrency') && (float)Tools::getValue('conversion_rate') <= 0) {
  322. $this->errors[] = Tools::displayError('The currency conversion rate can not be equal to 0.');
  323. }
  324. parent::initProcess();
  325. }
  326. public function initPageHeaderToolbar()
  327. {
  328. if (empty($this->display)) {
  329. $this->page_header_toolbar_btn['new_currency'] = array(
  330. 'href' => self::$currentIndex.'&addcurrency&token='.$this->token,
  331. 'desc' => $this->l('Add new currency', null, null, false),
  332. 'icon' => 'process-icon-new'
  333. );
  334. }
  335. parent::initPageHeaderToolbar();
  336. }
  337. }