PageRenderTime 68ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/controllers/admin/AdminCarrierWizardController.php

https://github.com/netplayer/PrestaShop
PHP | 939 lines | 799 code | 108 blank | 32 comment | 107 complexity | ddf568642b7e58cfa4dc5bd7b19bed5d MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /*
  3. * 2007-2014 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-2014 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 AdminCarrierWizardControllerCore extends AdminController
  27. {
  28. protected $wizard_access;
  29. public function __construct()
  30. {
  31. $this->bootstrap = true;
  32. $this->display = 'view';
  33. $this->table = 'carrier';
  34. $this->identifier = 'id_carrier';
  35. $this->className = 'Carrier';
  36. $this->lang = false;
  37. $this->deleted = true;
  38. $this->step_number = 0;
  39. $this->multishop_context = Shop::CONTEXT_ALL;
  40. $this->context = Context::getContext();
  41. $this->fieldImageSettings = array(
  42. 'name' => 'logo',
  43. 'dir' => 's'
  44. );
  45. parent::__construct();
  46. $this->tabAccess = Profile::getProfileAccess($this->context->employee->id_profile, Tab::getIdFromClassName('AdminCarriers'));
  47. }
  48. public function setMedia()
  49. {
  50. parent::setMedia();
  51. $this->addJqueryPlugin('smartWizard');
  52. $this->addJqueryPlugin('typewatch');
  53. $this->addJs(_PS_JS_DIR_.'admin_carrier_wizard.js');
  54. }
  55. public function initWizard()
  56. {
  57. $this->wizard_steps = array(
  58. 'name' => 'carrier_wizard',
  59. 'steps' => array(
  60. array(
  61. 'title' => $this->l('General settings'),
  62. ),
  63. array(
  64. 'title' => $this->l('Shipping locations and costs'),
  65. ),
  66. array(
  67. 'title' => $this->l('Size, weight, and group access'),
  68. ),
  69. array(
  70. 'title' => $this->l('Summary'),
  71. ),
  72. ));
  73. if (Shop::isFeatureActive())
  74. {
  75. $multistore_step = array(
  76. array(
  77. 'title' => $this->l('MultiStore'),
  78. )
  79. );
  80. array_splice($this->wizard_steps['steps'], 1, 0, $multistore_step);
  81. }
  82. }
  83. public function renderView()
  84. {
  85. $this->initWizard();
  86. if (Tools::getValue('id_carrier') && $this->tabAccess['edit'])
  87. $carrier = $this->loadObject();
  88. elseif ($this->tabAccess['add'])
  89. $carrier = new Carrier();
  90. if ((!$this->tabAccess['edit'] && Tools::getValue('id_carrier')) || (!$this->tabAccess['add'] && !Tools::getValue('id_carrier')))
  91. {
  92. $this->errors[] = Tools::displayError('You do not have permission to use this wizard.');
  93. return ;
  94. }
  95. $currency = new Currency(Configuration::get('PS_CURRENCY_DEFAULT'));
  96. $this->tpl_view_vars = array(
  97. 'currency_sign' => $currency->sign,
  98. 'PS_WEIGHT_UNIT' => Configuration::get('PS_WEIGHT_UNIT'),
  99. 'enableAllSteps' => Validate::isLoadedObject($carrier),
  100. 'wizard_steps' => $this->wizard_steps,
  101. 'validate_url' => $this->context->link->getAdminLink('AdminCarrierWizard'),
  102. 'carrierlist_url' => $this->context->link->getAdminLink('AdminCarriers').'&conf='.((int)Validate::isLoadedObject($carrier) ? 4 : 3),
  103. 'multistore_enable' => Shop::isFeatureActive(),
  104. 'wizard_contents' => array(
  105. 'contents' => array(
  106. 0 => $this->renderStepOne($carrier),
  107. 1 => $this->renderStepThree($carrier),
  108. 2 => $this->renderStepFour($carrier),
  109. 3 => $this->renderStepFive($carrier),
  110. )),
  111. 'labels' => array('next' => $this->l('Next'), 'previous' => $this->l('Previous'), 'finish' => $this->l('Finish'))
  112. );
  113. if (Shop::isFeatureActive())
  114. array_splice($this->tpl_view_vars['wizard_contents']['contents'], 1, 0, array(0 => $this->renderStepTwo($carrier)));
  115. $this->context->smarty->assign(array(
  116. 'carrier_logo' => (Validate::isLoadedObject($carrier) && file_exists(_PS_SHIP_IMG_DIR_.$carrier->id.'.jpg') ? _THEME_SHIP_DIR_.$carrier->id.'.jpg' : false),
  117. ));
  118. $this->context->smarty->assign(array(
  119. 'logo_content' => $this->createTemplate('logo.tpl')->fetch()
  120. ));
  121. $this->addjQueryPlugin(array('ajaxfileupload'));
  122. return parent::renderView();
  123. }
  124. public function initBreadcrumbs($tab_id = null,$tabs = null)
  125. {
  126. if (Tools::getValue('id_carrier'))
  127. $this->display = 'edit';
  128. else
  129. $this->display = 'add';
  130. parent::initBreadcrumbs((int)Tab::getIdFromClassName('AdminCarriers'));
  131. $this->display = 'view';
  132. }
  133. public function initPageHeaderToolbar()
  134. {
  135. parent::initPageHeaderToolbar();
  136. $this->page_header_toolbar_btn['cancel'] = array(
  137. 'href' => $this->context->link->getAdminLink('AdminCarriers'),
  138. 'desc' => $this->l('Cancel', null, null, false)
  139. );
  140. }
  141. public function renderStepOne($carrier)
  142. {
  143. $this->fields_form = array(
  144. 'form' => array(
  145. 'id_form' => 'step_carrier_general',
  146. 'input' => array(
  147. array(
  148. 'type' => 'text',
  149. 'label' => $this->l('Carrier name'),
  150. 'name' => 'name',
  151. 'required' => true,
  152. 'hint' => array(
  153. sprintf($this->l('Allowed characters: letters, spaces and "%s".'), '().-'),
  154. $this->l('The carrier\'s name will be displayed during checkout.'),
  155. $this->l('For in-store pickup, enter 0 to replace the carrier name with your shop name.')
  156. )
  157. ),
  158. array(
  159. 'type' => 'text',
  160. 'label' => $this->l('Transit time'),
  161. 'name' => 'delay',
  162. 'lang' => true,
  163. 'required' => true,
  164. 'maxlength' => 128,
  165. 'hint' => $this->l('The estimated delivery time will be displayed during checkout.')
  166. ),
  167. array(
  168. 'type' => 'text',
  169. 'label' => $this->l('Speed grade'),
  170. 'name' => 'grade',
  171. 'required' => false,
  172. 'size' => 1,
  173. 'hint' => $this->l('Enter "0" for a longest shipping delay, or "9" for the shortest shipping delay.')
  174. ),
  175. array(
  176. 'type' => 'logo',
  177. 'label' => $this->l('Logo'),
  178. 'name' => 'logo'
  179. ),
  180. array(
  181. 'type' => 'text',
  182. 'label' => $this->l('Tracking URL'),
  183. 'name' => 'url',
  184. 'hint' => $this->l('Delivery tracking URL: Type \'@\' where the tracking number should appear. It will be automatically replaced by the tracking number.')
  185. ),
  186. )),
  187. );
  188. $tpl_vars = array('max_image_size' => (int)Configuration::get('PS_PRODUCT_PICTURE_MAX_SIZE') / 1024 / 1024);
  189. $fields_value = $this->getStepOneFieldsValues($carrier);
  190. return $this->renderGenericForm(array('form' => $this->fields_form), $fields_value, $tpl_vars);
  191. }
  192. public function renderStepTwo($carrier)
  193. {
  194. $this->fields_form = array(
  195. 'form' => array(
  196. 'id_form' => 'step_carrier_shops',
  197. 'input' => array(
  198. array(
  199. 'type' => 'shop',
  200. 'label' => $this->l('Shop association'),
  201. 'name' => 'checkBoxShopAsso',
  202. ),
  203. ))
  204. );
  205. $fields_value = $this->getStepTwoFieldsValues($carrier);
  206. return $this->renderGenericForm(array('form' => $this->fields_form), $fields_value);
  207. }
  208. public function renderStepThree($carrier)
  209. {
  210. $this->fields_form = array(
  211. 'form' => array(
  212. 'id_form' => 'step_carrier_ranges',
  213. 'input' => array(
  214. array(
  215. 'type' => 'switch',
  216. 'label' => $this->l('Add handling costs'),
  217. 'name' => 'shipping_handling',
  218. 'required' => false,
  219. 'class' => 't',
  220. 'is_bool' => true,
  221. 'values' => array(
  222. array(
  223. 'id' => 'shipping_handling_on',
  224. 'value' => 1,
  225. 'label' => $this->l('Enabled')
  226. ),
  227. array(
  228. 'id' => 'shipping_handling_off',
  229. 'value' => 0,
  230. 'label' => $this->l('Disabled')
  231. )
  232. ),
  233. 'hint' => $this->l('Include the handling costs (as set in Shipping > Preferences) in the final carrier price.')
  234. ),
  235. array(
  236. 'type' => 'switch',
  237. 'label' => $this->l('Free shipping'),
  238. 'name' => 'is_free',
  239. 'required' => false,
  240. 'class' => 't',
  241. 'values' => array(
  242. array(
  243. 'id' => 'is_free_on',
  244. 'value' => 1,
  245. 'label' => '<img src="../img/admin/disabled.gif" alt="'.$this->l('No').'" title="'.$this->l('No').'" />'
  246. ),
  247. array(
  248. 'id' => 'is_free_off',
  249. 'value' => 0,
  250. 'label' => '<img src="../img/admin/enabled.gif" alt="'.$this->l('Yes').'" title="'.$this->l('Yes').'" />'
  251. )
  252. ),
  253. ),
  254. array(
  255. 'type' => 'radio',
  256. 'label' => $this->l('Billing'),
  257. 'name' => 'shipping_method',
  258. 'required' => false,
  259. 'class' => 't',
  260. 'br' => true,
  261. 'values' => array(
  262. array(
  263. 'id' => 'billing_price',
  264. 'value' => Carrier::SHIPPING_METHOD_PRICE,
  265. 'label' => $this->l('According to total price.')
  266. ),
  267. array(
  268. 'id' => 'billing_weight',
  269. 'value' => Carrier::SHIPPING_METHOD_WEIGHT,
  270. 'label' => $this->l('According to total weight.')
  271. )
  272. )
  273. ),
  274. array(
  275. 'type' => 'select',
  276. 'label' => $this->l('Tax'),
  277. 'name' => 'id_tax_rules_group',
  278. 'options' => array(
  279. 'query' => TaxRulesGroup::getTaxRulesGroups(true),
  280. 'id' => 'id_tax_rules_group',
  281. 'name' => 'name',
  282. 'default' => array(
  283. 'label' => $this->l('No tax'),
  284. 'value' => 0
  285. )
  286. )
  287. ),
  288. array(
  289. 'type' => 'select',
  290. 'label' => $this->l('Out-of-range behavior'),
  291. 'name' => 'range_behavior',
  292. 'options' => array(
  293. 'query' => array(
  294. array(
  295. 'id' => 0,
  296. 'name' => $this->l('Apply the cost of the highest defined range')
  297. ),
  298. array(
  299. 'id' => 1,
  300. 'name' => $this->l('Disable carrier')
  301. )
  302. ),
  303. 'id' => 'id',
  304. 'name' => 'name'
  305. ),
  306. 'hint' => $this->l('Out-of-range behavior occurs when no defined range matches the customer\'s cart (e.g. when the weight of the cart is greater than the highest weight limit defined by the weight ranges).')
  307. ),
  308. array(
  309. 'type' => 'zone',
  310. 'name' => 'zones'
  311. )
  312. ),
  313. ));
  314. $tpl_vars = array();
  315. $tpl_vars['PS_WEIGHT_UNIT'] = Configuration::get('PS_WEIGHT_UNIT');
  316. $currency = new Currency(Configuration::get('PS_CURRENCY_DEFAULT'));
  317. $tpl_vars['currency_sign'] = $currency->sign;
  318. $fields_value = $this->getStepThreeFieldsValues($carrier);
  319. $this->getTplRangesVarsAndValues($carrier, $tpl_vars, $fields_value);
  320. return $this->renderGenericForm(array('form' => $this->fields_form), $fields_value, $tpl_vars);
  321. }
  322. public function renderStepFour($carrier)
  323. {
  324. $this->fields_form = array(
  325. 'form' => array(
  326. 'id_form' => 'step_carrier_conf',
  327. 'input' => array(
  328. array(
  329. 'type' => 'text',
  330. 'label' => sprintf($this->l('Maximum package height (%s)'), Configuration::get('PS_DIMENSION_UNIT')),
  331. 'name' => 'max_height',
  332. 'required' => false,
  333. 'hint' => $this->l('Maximum height managed by this carrier. Set the value to "0", or leave this field blank to ignore.').' '.$this->l('The value must be an integer.')
  334. ),
  335. array(
  336. 'type' => 'text',
  337. 'label' => sprintf($this->l('Maximum package width (%s)'), Configuration::get('PS_DIMENSION_UNIT')),
  338. 'name' => 'max_width',
  339. 'required' => false,
  340. 'hint' => $this->l('Maximum width managed by this carrier. Set the value to "0", or leave this field blank to ignore.').' '.$this->l('The value must be an integer.')
  341. ),
  342. array(
  343. 'type' => 'text',
  344. 'label' => sprintf($this->l('Maximum package depth (%s)'), Configuration::get('PS_DIMENSION_UNIT')),
  345. 'name' => 'max_depth',
  346. 'required' => false,
  347. 'hint' => $this->l('Maximum depth managed by this carrier. Set the value to "0", or leave this field blank to ignore.').' '.$this->l('The value must be an integer.')
  348. ),
  349. array(
  350. 'type' => 'text',
  351. 'label' => sprintf($this->l('Maximum package weight (%s)'), Configuration::get('PS_WEIGHT_UNIT')),
  352. 'name' => 'max_weight',
  353. 'required' => false,
  354. 'hint' => $this->l('Maximum weight managed by this carrier. Set the value to "0", or leave this field blank to ignore.')
  355. ),
  356. array(
  357. 'type' => 'group',
  358. 'label' => $this->l('Group access'),
  359. 'name' => 'groupBox',
  360. 'values' => Group::getGroups(Context::getContext()->language->id),
  361. 'hint' => $this->l('Mark the groups that are allowed access to this carrier.')
  362. )
  363. )
  364. ));
  365. $fields_value = $this->getStepFourFieldsValues($carrier);
  366. // Added values of object Group
  367. $carrier_groups = $carrier->getGroups();
  368. $carrier_groups_ids = array();
  369. if (is_array($carrier_groups))
  370. foreach ($carrier_groups as $carrier_group)
  371. $carrier_groups_ids[] = $carrier_group['id_group'];
  372. $groups = Group::getGroups($this->context->language->id);
  373. foreach ($groups as $group)
  374. $fields_value['groupBox_'.$group['id_group']] = Tools::getValue('groupBox_'.$group['id_group'], (in_array($group['id_group'], $carrier_groups_ids) || empty($carrier_groups_ids) && !$carrier->id));
  375. return $this->renderGenericForm(array('form' => $this->fields_form), $fields_value);
  376. }
  377. public function renderStepFive($carrier)
  378. {
  379. $this->fields_form = array(
  380. 'form' => array(
  381. 'id_form' => 'step_carrier_summary',
  382. 'input' => array(
  383. array(
  384. 'type' => 'switch',
  385. 'label' => $this->l('Enabled'),
  386. 'name' => 'active',
  387. 'required' => false,
  388. 'class' => 't',
  389. 'is_bool' => true,
  390. 'values' => array(
  391. array(
  392. 'id' => 'active_on',
  393. 'value' => 1
  394. ),
  395. array(
  396. 'id' => 'active_off',
  397. 'value' => 0
  398. )
  399. ),
  400. 'hint' => $this->l('Enable the carrier in the Front Office.')
  401. )
  402. )
  403. ));
  404. $template = $this->createTemplate('controllers/carrier_wizard/summary.tpl');
  405. $fields_value = $this->getStepFiveFieldsValues($carrier);
  406. $active_form = $this->renderGenericForm(array('form' => $this->fields_form), $fields_value);
  407. $active_form = str_replace(array('<fieldset id="fieldset_form">', '</fieldset>'), '', $active_form);
  408. $template->assign('active_form', $active_form);
  409. return $template->fetch('controllers/carrier_wizard/summary.tpl');
  410. }
  411. protected function getTplRangesVarsAndValues($carrier, &$tpl_vars, &$fields_value)
  412. {
  413. $tpl_vars['zones'] = Zone::getZones(false);
  414. $carrier_zones = $carrier->getZones();
  415. $carrier_zones_ids = array();
  416. if (is_array($carrier_zones))
  417. foreach ($carrier_zones as $carrier_zone)
  418. $carrier_zones_ids[] = $carrier_zone['id_zone'];
  419. $range_table = $carrier->getRangeTable();
  420. $shipping_method = $carrier->getShippingMethod();
  421. $zones = Zone::getZones(false);
  422. foreach ($zones as $zone)
  423. $fields_value['zones'][$zone['id_zone']] = Tools::getValue('zone_'.$zone['id_zone'], (in_array($zone['id_zone'], $carrier_zones_ids)));
  424. if ($shipping_method == Carrier::SHIPPING_METHOD_FREE)
  425. {
  426. $range_obj = $carrier->getRangeObject($carrier->shipping_method);
  427. $price_by_range = array();
  428. }
  429. else
  430. {
  431. $range_obj = $carrier->getRangeObject();
  432. $price_by_range = Carrier::getDeliveryPriceByRanges($range_table, (int)$carrier->id);
  433. }
  434. foreach ($price_by_range as $price)
  435. $tpl_vars['price_by_range'][$price['id_'.$range_table]][$price['id_zone']] = $price['price'];
  436. $tmp_range = $range_obj->getRanges((int)$carrier->id);
  437. $tpl_vars['ranges'] = array();
  438. if ($shipping_method != Carrier::SHIPPING_METHOD_FREE)
  439. foreach ($tmp_range as $id => $range)
  440. {
  441. $tpl_vars['ranges'][$range['id_'.$range_table]] = $range;
  442. $tpl_vars['ranges'][$range['id_'.$range_table]]['id_range'] = $range['id_'.$range_table];
  443. }
  444. // init blank range
  445. if (!count($tpl_vars['ranges']))
  446. $tpl_vars['ranges'][] = array('id_range' => 0, 'delimiter1' => 0, 'delimiter2' => 0);
  447. }
  448. public function renderGenericForm($fields_form, $fields_value, $tpl_vars = array())
  449. {
  450. $helper = new HelperForm();
  451. $helper->show_toolbar = false;
  452. $helper->table = $this->table;
  453. $lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
  454. $helper->default_form_language = $lang->id;
  455. $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
  456. $this->fields_form = array();
  457. $helper->id = (int)Tools::getValue('id_carrier');
  458. $helper->identifier = $this->identifier;
  459. $helper->tpl_vars = array_merge(array(
  460. 'fields_value' => $fields_value,
  461. 'languages' => $this->getLanguages(),
  462. 'id_language' => $this->context->language->id
  463. ), $tpl_vars);
  464. $helper->override_folder = 'carrier_wizard/';
  465. return $helper->generateForm($fields_form);
  466. }
  467. public function getStepOneFieldsValues($carrier)
  468. {
  469. return array(
  470. 'id_carrier' => $this->getFieldValue($carrier, 'id_carrier'),
  471. 'name' => $this->getFieldValue($carrier, 'name'),
  472. 'delay' => $this->getFieldValue($carrier, 'delay'),
  473. 'grade' => $this->getFieldValue($carrier, 'grade'),
  474. 'url' => $this->getFieldValue($carrier, 'url'),
  475. );
  476. }
  477. public function getStepTwoFieldsValues($carrier)
  478. {
  479. return array('shop' => $this->getFieldValue($carrier, 'shop'));
  480. }
  481. public function getStepThreeFieldsValues($carrier)
  482. {
  483. $id_tax_rules_group = (is_object($this->object) && !$this->object->id) ? Carrier::getIdTaxRulesGroupMostUsed() : $this->getFieldValue($carrier, 'id_tax_rules_group');
  484. $shipping_handling = (is_object($this->object) && !$this->object->id) ? 0 : $this->getFieldValue($carrier, 'shipping_handling');
  485. return array(
  486. 'is_free' => $this->getFieldValue($carrier, 'is_free'),
  487. 'id_tax_rules_group' => (int)$id_tax_rules_group,
  488. 'shipping_handling' => $shipping_handling,
  489. 'shipping_method' => $this->getFieldValue($carrier, 'shipping_method'),
  490. 'range_behavior' => $this->getFieldValue($carrier, 'range_behavior'),
  491. 'zones' => $this->getFieldValue($carrier, 'zones'),
  492. );
  493. }
  494. public function getStepFourFieldsValues($carrier)
  495. {
  496. return array(
  497. 'range_behavior' => $this->getFieldValue($carrier, 'range_behavior'),
  498. 'max_height' => $this->getFieldValue($carrier, 'max_height'),
  499. 'max_width' => $this->getFieldValue($carrier, 'max_width'),
  500. 'max_depth' => $this->getFieldValue($carrier, 'max_depth'),
  501. 'max_weight' => $this->getFieldValue($carrier, 'max_weight'),
  502. 'group' => $this->getFieldValue($carrier, 'group'),
  503. );
  504. }
  505. public function getStepFiveFieldsValues($carrier)
  506. {
  507. return array('active' => $this->getFieldValue($carrier, 'active'));
  508. }
  509. public function ajaxProcessChangeRanges()
  510. {
  511. if ((Validate::isLoadedObject($this->object) && !$this->tabAccess['edit']) || !$this->tabAccess['add'])
  512. {
  513. $this->errors[] = Tools::displayError('You do not have permission to use this wizard.');
  514. return;
  515. }
  516. if ((!(int)$shipping_method = Tools::getValue('shipping_method')) || !in_array($shipping_method, array(Carrier::SHIPPING_METHOD_PRICE, Carrier::SHIPPING_METHOD_WEIGHT)))
  517. return ;
  518. $carrier = $this->loadObject(true);
  519. $carrier->shipping_method = $shipping_method;
  520. $tpl_vars = array();
  521. $fields_value = $this->getStepThreeFieldsValues($carrier);
  522. $this->getTplRangesVarsAndValues($carrier, $tpl_vars, $fields_value);
  523. $template = $this->createTemplate('controllers/carrier_wizard/helpers/form/form_ranges.tpl');
  524. $template->assign($tpl_vars);
  525. $template->assign('change_ranges', 1);
  526. $template->assign('fields_value', $fields_value);
  527. $template->assign('input', array('type' => 'zone', 'name' => 'zones' ));
  528. $currency = new Currency(Configuration::get('PS_CURRENCY_DEFAULT'));
  529. $template->assign('currency_sign', $currency->sign);
  530. $template->assign('PS_WEIGHT_UNIT', Configuration::get('PS_WEIGHT_UNIT'));
  531. die($template->fetch());
  532. }
  533. protected function validateForm($die = true)
  534. {
  535. $step_number = (int)Tools::getValue('step_number');
  536. $return = array('has_error' => false);
  537. if (!$this->tabAccess['edit'])
  538. $this->errors[] = Tools::displayError('You do not have permission to use this wizard.');
  539. else
  540. {
  541. if (Shop::isFeatureActive() && $step_number == 2)
  542. {
  543. if (!Tools::getValue('checkBoxShopAsso_carrier'))
  544. {
  545. $return['has_error'] = true;
  546. $return['errors'][] = $this->l('You must choose at least one shop or group shop.');
  547. }
  548. }
  549. else
  550. $this->validateRules();
  551. }
  552. if (count($this->errors))
  553. {
  554. $return['has_error'] = true;
  555. $return['errors'] = $this->errors;
  556. }
  557. if (count($this->errors) || $die)
  558. die(Tools::jsonEncode($return));
  559. }
  560. public function ajaxProcessValidateStep()
  561. {
  562. $this->validateForm(true);
  563. }
  564. public function processRanges($id_carrier)
  565. {
  566. if (!$this->tabAccess['edit'] || !$this->tabAccess['add'])
  567. {
  568. $this->errors[] = Tools::displayError('You do not have permission to use this wizard.');
  569. return;
  570. }
  571. $carrier = new Carrier((int)$id_carrier);
  572. if (!Validate::isLoadedObject($carrier))
  573. return false;
  574. $range_inf = Tools::getValue('range_inf');
  575. $range_sup = Tools::getValue('range_sup');
  576. $range_type = Tools::getValue('shipping_method');
  577. $fees = Tools::getValue('fees');
  578. $carrier->deleteDeliveryPrice($carrier->getRangeTable());
  579. if ($range_type != Carrier::SHIPPING_METHOD_FREE)
  580. {
  581. foreach ($range_inf as $key => $delimiter1)
  582. {
  583. if (!isset($range_sup[$key]))
  584. continue;
  585. $add_range = true;
  586. if ($range_type == Carrier::SHIPPING_METHOD_WEIGHT)
  587. {
  588. if (!RangeWeight::rangeExist((int)$carrier->id, (float)$delimiter1, (float)$range_sup[$key]))
  589. $range = new RangeWeight();
  590. else
  591. {
  592. $range = new RangeWeight((int)$key);
  593. $add_range = false;
  594. }
  595. }
  596. if ($range_type == Carrier::SHIPPING_METHOD_PRICE)
  597. {
  598. if (!RangePrice::rangeExist((int)$carrier->id, (float)$delimiter1, (float)$range_sup[$key]))
  599. $range = new RangePrice();
  600. else
  601. {
  602. $range = new RangePrice((int)$key);
  603. $add_range = false;
  604. }
  605. }
  606. if ($add_range)
  607. {
  608. $range->id_carrier = (int)$carrier->id;
  609. $range->delimiter1 = (float)$delimiter1;
  610. $range->delimiter2 = (float)$range_sup[$key];
  611. $range->save();
  612. }
  613. if (!Validate::isLoadedObject($range))
  614. return false;
  615. $price_list = array();
  616. if (is_array($fees) && count($fees))
  617. {
  618. foreach ($fees as $id_zone => $fee)
  619. {
  620. $price_list[] = array(
  621. 'id_range_price' => ($range_type == Carrier::SHIPPING_METHOD_PRICE ? (int)$range->id : null),
  622. 'id_range_weight' => ($range_type == Carrier::SHIPPING_METHOD_WEIGHT ? (int)$range->id : null),
  623. 'id_carrier' => (int)$carrier->id,
  624. 'id_zone' => (int)$id_zone,
  625. 'price' => isset($fee[$key]) ? (float)$fee[$key] : 0,
  626. );
  627. }
  628. }
  629. if (count($price_list) && !$carrier->addDeliveryPrice($price_list, true))
  630. return false;
  631. }
  632. }
  633. return true;
  634. }
  635. public function ajaxProcessUploadLogo()
  636. {
  637. if (!$this->tabAccess['edit'])
  638. die('<return result="error" message="'.Tools::displayError('You do not have permission to use this wizard.').'" />');
  639. $allowedExtensions = array('jpeg', 'gif', 'png', 'jpg');
  640. $logo = (isset($_FILES['carrier_logo_input']) ? $_FILES['carrier_logo_input'] : false);
  641. if ($logo && !empty($logo['tmp_name']) && $logo['tmp_name'] != 'none'
  642. && (!isset($logo['error']) || !$logo['error'])
  643. && preg_match('/\.(jpe?g|gif|png)$/', $logo['name'])
  644. && is_uploaded_file($logo['tmp_name'])
  645. && ImageManager::isRealImage($logo['tmp_name'], $logo['type']))
  646. {
  647. $file = $logo['tmp_name'];
  648. do $tmp_name = uniqid().'.jpg';
  649. while (file_exists(_PS_TMP_IMG_DIR_.$tmp_name));
  650. if (!ImageManager::resize($file, _PS_TMP_IMG_DIR_.$tmp_name))
  651. die('<return result="error" message="Impossible to resize the image into '.Tools::safeOutput(_PS_TMP_IMG_DIR_).'" />');
  652. @unlink($file);
  653. die('<return result="success" message="'.Tools::safeOutput(_PS_TMP_IMG_.$tmp_name).'" />');
  654. }
  655. else
  656. die('<return result="error" message="Cannot upload file" />');
  657. }
  658. public function ajaxProcessFinishStep()
  659. {
  660. $return = array('has_error' => false);
  661. if (!$this->tabAccess['edit'])
  662. $return = array(
  663. 'has_error' => true,
  664. $return['errors'][] = Tools::displayError('You do not have permission to use this wizard.')
  665. );
  666. else
  667. {
  668. $this->validateForm(false);
  669. if ($id_carrier = Tools::getValue('id_carrier'))
  670. {
  671. $current_carrier = new Carrier((int)$id_carrier);
  672. // if update we duplicate current Carrier
  673. $new_carrier = $current_carrier->duplicateObject();
  674. if (Validate::isLoadedObject($new_carrier))
  675. {
  676. // Set flag deteled to true for historization
  677. $current_carrier->deleted = true;
  678. $current_carrier->update();
  679. // Fill the new carrier object
  680. $this->copyFromPost($new_carrier, $this->table);
  681. $new_carrier->position = $current_carrier->position;
  682. $new_carrier->update();
  683. $this->updateAssoShop((int)$new_carrier->id);
  684. $this->duplicateLogo((int)$new_carrier->id, (int)$current_carrier->id);
  685. $this->changeGroups((int)$new_carrier->id);
  686. //Copy default carrier
  687. if (Configuration::get('PS_CARRIER_DEFAULT') == $current_carrier->id)
  688. Configuration::updateValue('PS_CARRIER_DEFAULT', (int)$new_carrier->id);
  689. // Call of hooks
  690. Hook::exec('actionCarrierUpdate', array(
  691. 'id_carrier' => (int)$current_carrier->id,
  692. 'carrier' => $new_carrier
  693. ));
  694. $this->postImage($new_carrier->id);
  695. $this->changeZones($new_carrier->id);
  696. $new_carrier->setTaxRulesGroup((int)Tools::getValue('id_tax_rules_group'));
  697. $carrier = $new_carrier;
  698. }
  699. }
  700. else
  701. {
  702. $carrier = new Carrier();
  703. $this->copyFromPost($carrier, $this->table);
  704. if (!$carrier->add())
  705. {
  706. $return['has_error'] = true;
  707. $return['errors'][] = $this->l('An error occurred while saving this carrier.');
  708. }
  709. }
  710. if ($carrier->is_free)
  711. {
  712. //if carrier is free delete shipping cost
  713. $carrier->deleteDeliveryPrice('range_weight');
  714. $carrier->deleteDeliveryPrice('range_price');
  715. }
  716. if (Validate::isLoadedObject($carrier))
  717. {
  718. if (!$this->changeGroups((int)$carrier->id))
  719. {
  720. $return['has_error'] = true;
  721. $return['errors'][] = $this->l('An error occurred while saving carrier groups.');
  722. }
  723. if (!$this->changeZones((int)$carrier->id))
  724. {
  725. $return['has_error'] = true;
  726. $return['errors'][] = $this->l('An error occurred while saving carrier zones.');
  727. }
  728. if (!$carrier->is_free)
  729. if (!$this->processRanges((int)$carrier->id))
  730. {
  731. $return['has_error'] = true;
  732. $return['errors'][] = $this->l('An error occurred while saving carrier ranges.');
  733. }
  734. if (Shop::isFeatureActive() && !$this->updateAssoShop((int)$carrier->id))
  735. {
  736. $return['has_error'] = true;
  737. $return['errors'][] = $this->l('An error occurred while saving associations of shops.');
  738. }
  739. if (!$carrier->setTaxRulesGroup((int)Tools::getValue('id_tax_rules_group')))
  740. {
  741. $return['has_error'] = true;
  742. $return['errors'][] = $this->l('An error occurred while saving the tax rules group.');
  743. }
  744. if (Tools::getValue('logo'))
  745. {
  746. if (Tools::getValue('logo') == 'null' && file_exists(_PS_SHIP_IMG_DIR_.$carrier->id.'.jpg'))
  747. unlink(_PS_SHIP_IMG_DIR_.$carrier->id.'.jpg');
  748. else
  749. {
  750. $logo = basename(Tools::getValue('logo'));
  751. if (!file_exists(_PS_TMP_IMG_DIR_.$logo) || !copy(_PS_TMP_IMG_DIR_.$logo, _PS_SHIP_IMG_DIR_.$carrier->id.'.jpg'))
  752. {
  753. $return['has_error'] = true;
  754. $return['errors'][] = $this->l('An error occurred while saving carrier logo.');
  755. }
  756. }
  757. }
  758. $return['id_carrier'] = $carrier->id;
  759. }
  760. }
  761. die(Tools::jsonEncode($return));
  762. }
  763. protected function changeGroups($id_carrier, $delete = true)
  764. {
  765. $carrier = new Carrier((int)$id_carrier);
  766. if (!Validate::isLoadedObject($carrier))
  767. return false;
  768. return $carrier->setGroups(Tools::getValue('groupBox'));
  769. }
  770. public function changeZones($id)
  771. {
  772. $return = true;
  773. $carrier = new Carrier($id);
  774. if (!Validate::isLoadedObject($carrier))
  775. die (Tools::displayError('The object cannot be loaded.'));
  776. $zones = Zone::getZones(false);
  777. foreach ($zones as $zone)
  778. if (count($carrier->getZone($zone['id_zone'])))
  779. {
  780. if (!isset($_POST['zone_'.$zone['id_zone']]) || !$_POST['zone_'.$zone['id_zone']])
  781. $return &= $carrier->deleteZone((int)$zone['id_zone']);
  782. }
  783. else
  784. if (isset($_POST['zone_'.$zone['id_zone']]) && $_POST['zone_'.$zone['id_zone']])
  785. $return &= $carrier->addZone((int)$zone['id_zone']);
  786. return $return;
  787. }
  788. public function getValidationRules()
  789. {
  790. $step_number = (int)Tools::getValue('step_number');
  791. if (!$step_number)
  792. return;
  793. if ($step_number == 4 && !Shop::isFeatureActive() || $step_number == 5 && Shop::isFeatureActive())
  794. return array('fields' => array());
  795. $step_fields = array(
  796. 1 => array('name', 'delay', 'grade', 'url'),
  797. 2 => array('is_free', 'id_tax_rules_group', 'shipping_handling', 'shipping_method', 'range_behavior'),
  798. 3 => array('range_behavior', 'max_height', 'max_width', 'max_depth', 'max_weight'),
  799. 4 => array(),
  800. );
  801. if (Shop::isFeatureActive())
  802. {
  803. $tmp = $step_fields;
  804. $step_fields = array_slice($tmp, 0, 1, true) + array(2 => array('shop'));
  805. $step_fields[3] = $tmp[2];
  806. $step_fields[4] = $tmp[3];
  807. }
  808. $definition = ObjectModel::getDefinition('Carrier');
  809. foreach ($definition['fields'] as $field => $def)
  810. if (is_array($step_fields[$step_number]) && !in_array($field, $step_fields[$step_number]))
  811. unset($definition['fields'][$field]);
  812. return $definition;
  813. }
  814. public static function displayFieldName($field)
  815. {
  816. return $field;
  817. }
  818. public function duplicateLogo($new_id, $old_id)
  819. {
  820. $old_logo = _PS_SHIP_IMG_DIR_.'/'.(int)$old_id.'.jpg';
  821. if (file_exists($old_logo))
  822. copy($old_logo, _PS_SHIP_IMG_DIR_.'/'.(int)$new_id.'.jpg');
  823. $old_tmp_logo = _PS_TMP_IMG_DIR_.'/carrier_mini_'.(int)$old_id.'.jpg';
  824. if (file_exists($old_tmp_logo))
  825. {
  826. if (!isset($_FILES['logo']))
  827. copy($old_tmp_logo, _PS_TMP_IMG_DIR_.'/carrier_mini_'.$new_id.'.jpg');
  828. unlink($old_tmp_logo);
  829. }
  830. }
  831. }