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

/admin444/tabs/AdminCarriers.php

http://marocmall.googlecode.com/
PHP | 297 lines | 262 code | 22 blank | 13 comment | 24 complexity | e378b8d8b25d83a6fd75856562502bd6 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Carriers tab for admin panel, AdminCarriers.php
  4. * @category admin
  5. *
  6. * @author PrestaShop <support@prestashop.com>
  7. * @copyright PrestaShop
  8. * @license http://www.opensource.org/licenses/osl-3.0.php Open-source licence 3.0
  9. * @version 1.3
  10. *
  11. */
  12. include_once(PS_ADMIN_DIR.'/../classes/AdminTab.php');
  13. class AdminCarriers extends AdminTab
  14. {
  15. protected $maxImageSize = 30000;
  16. public function __construct()
  17. {
  18. global $cookie;
  19. $this->table = 'carrier';
  20. $this->className = 'Carrier';
  21. $this->lang = true;
  22. $this->edit = true;
  23. $this->delete = true;
  24. $this->deleted = true;
  25. $this->fieldImageSettings = array('name' => 'logo', 'dir' => 's');
  26. $this->fieldsDisplay = array(
  27. 'id_carrier' => array('title' => $this->l('ID'), 'align' => 'center', 'width' => 25),
  28. 'name' => array('title' => $this->l('Name'), 'width' => 100),
  29. 'logo' => array('title' => $this->l('Logo'), 'align' => 'center', 'image' => 's', 'orderby' => false, 'search' => false),
  30. 'delay' => array('title' => $this->l('Delay'), 'width' => 300, 'orderby' => false),
  31. 'active' => array('title' => $this->l('Status'), 'align' => 'center', 'active' => 'status', 'type' => 'bool', 'orderby' => false));
  32. $this->optionTitle = $this->l('Carrier options');
  33. $this->_fieldsOptions = array(
  34. 'PS_CARRIER_DEFAULT' => array('title' => $this->l('Default carrier:'), 'desc' => $this->l('The default carrier used in shop'), 'cast' => 'intval', 'type' => 'select', 'identifier' => 'id_carrier', 'list' => Carrier::getCarriers(intval($cookie->id_lang), true)),
  35. );
  36. parent::__construct();
  37. }
  38. public function displayForm($isMainTab = true)
  39. {
  40. global $currentIndex, $cookie;
  41. parent::displayForm();
  42. $obj = $this->loadObject(true);
  43. $currentLanguage = intval($cookie->id_lang);
  44. echo '
  45. <form action="'.$currentIndex.'&submitAdd'.$this->table.'=1&token='.$this->token.'" method="post" enctype="multipart/form-data">
  46. '.($obj->id ? '<input type="hidden" name="id_'.$this->table.'" value="'.$obj->id.'" />' : '').'
  47. <fieldset class="width3"><legend><img src="../img/admin/delivery.gif" />'.$this->l('Carriers').'</legend>
  48. <label>'.$this->l('Company:').' </label>
  49. <div class="margin-form">
  50. <input type="text" size="25" name="name" value="'.htmlentities($this->getFieldValue($obj, 'name'), ENT_COMPAT, 'UTF-8').'" /> <sup>*</sup>
  51. <span class="hint" name="help_box">'.$this->l('Allowed characters: letters, spaces and').' ().-<span class="hint-pointer">&nbsp;</span></span>
  52. <p class="clear">'.$this->l('Carrier name displayed during checkout').'<br />'.$this->l('With a value of 0, the carrier name will be replaced by the shop name').'</p>
  53. </div>
  54. <label>'.$this->l('Logo:').' </label>
  55. <div class="margin-form">
  56. <input type="file" name="logo" />
  57. <p>'.$this->l('Upload logo from your computer').' (.gif, .jpg, .jpeg '.$this->l('or').' .png)</p>
  58. </div>
  59. <label>'.$this->l('Transit time:').' </label>
  60. <div class="margin-form">';
  61. foreach ($this->_languages as $language)
  62. echo '
  63. <div id="delay_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $this->_defaultFormLanguage ? 'block' : 'none').'; float: left;">
  64. <input type="text" size="41" maxlength="128" name="delay_'.$language['id_lang'].'" value="'.htmlentities($this->getFieldValue($obj, 'delay', intval($language['id_lang'])), ENT_COMPAT, 'UTF-8').'" /> <sup>*</sup>
  65. </div>';
  66. $this->displayFlags($this->_languages, $this->_defaultFormLanguage, 'delay', 'delay');
  67. echo '
  68. <p style="clear: both">'.$this->l('Time taken for product delivery; displayed during checkout').'</p>
  69. </div>
  70. <label>'.$this->l('URL:').' </label>
  71. <div class="margin-form">
  72. <input type="text" size="40" name="url" value="'.htmlentities($this->getFieldValue($obj, 'url'), ENT_COMPAT, 'UTF-8').'" />
  73. <p class="clear">'.$this->l('URL for the tracking number; type \'@\' where the tracking number will appear').'</p>
  74. </div>
  75. <label>'.$this->l('Tax:').'</label>
  76. <div class="margin-form">
  77. <select name="id_tax">
  78. <option value="0"'.(($obj->id AND $obj->id_tax == 0) ? ' selected="selected"' : '').'>'.$this->l('No tax').'</option>';
  79. $tvaList = Tax::getTaxes($currentLanguage);
  80. foreach ($tvaList AS $line)
  81. echo '<option value="'.$line['id_tax'].'"'.(($obj->id AND $obj->id_tax == $line['id_tax']) ? ' selected="selected"' : '').'>'.$line['name'].'</option>';
  82. echo '</select>
  83. <p>'.$this->l('Include tax on carrier, e.g., VAT').'</p>
  84. </div>
  85. <label>'.$this->l('Zone:').'</label>
  86. <div class="margin-form">';
  87. $carrier_zones = $obj->getZones();
  88. $zones = Zone::getZones(false);
  89. foreach ($zones AS $zone)
  90. echo '<input type="checkbox" id="zone_'.$zone['id_zone'].'" name="zone_'.$zone['id_zone'].'" value="true" '.(Tools::getValue('zone_'.$zone['id_zone'], (is_array($carrier_zones) AND in_array(array('id_carrier' => $obj->id, 'id_zone' => $zone['id_zone']), $carrier_zones))) ? ' checked="checked"' : '').'><label class="t" for="zone_'.$zone['id_zone'].'">&nbsp;<b>'.$zone['name'].'</b></label><br />';
  91. echo '<p>'.$this->l('The zone in which this carrier is to be used').'</p>
  92. </div>
  93. <label>'.$this->l('Group access:').'</label>
  94. <div class="margin-form">';
  95. $groups = Group::getGroups(intval($cookie->id_lang));
  96. if (sizeof($groups))
  97. {
  98. echo '
  99. <table cellspacing="0" cellpadding="0" class="table" style="width: 28em;">
  100. <tr>
  101. <th><input type="checkbox" name="checkme" class="noborder" onclick="checkDelBoxes(this.form, \'groupBox[]\', this.checked)"'.(!isset($obj->id) ? 'checked="checked" ' : '').' /></th>
  102. <th>'.$this->l('ID').'</th>
  103. <th>'.$this->l('Group name').'</th>
  104. </tr>';
  105. $irow = 0;
  106. foreach ($groups as $group)
  107. echo '
  108. <tr class="'.($irow++ % 2 ? 'alt_row' : '').'">
  109. <td><input type="checkbox" name="groupBox[]" class="groupBox" id="groupBox_'.$group['id_group'].'" value="'.$group['id_group'].'" '.((Db::getInstance()->getValue('SELECT id_group FROM '._DB_PREFIX_.'carrier_group WHERE id_carrier='.intval($obj->id).' AND id_group='.intval($group['id_group'])) OR (!isset($obj->id))) ? 'checked="checked" ' : '').'/></td>
  110. <td>'.$group['id_group'].'</td>
  111. <td><label for="groupBox_'.$group['id_group'].'" class="t">'.$group['name'].'</label></td>
  112. </tr>';
  113. echo '
  114. </table>
  115. <p style="padding:0px; margin:10px 0px 10px 0px;">'.$this->l('Mark all groups you want to give access to this carrier').'</p>
  116. ';
  117. }
  118. else
  119. echo '<p>'.$this->l('No group created').'</p>';
  120. echo ' </div>
  121. <label>'.$this->l('Status:').' </label>
  122. <div class="margin-form">
  123. <input type="radio" name="active" id="active_on" value="1" '.($this->getFieldValue($obj, 'active') ? 'checked="checked" ' : '').'/>
  124. <label class="t" for="active_on"> <img src="../img/admin/enabled.gif" alt="'.$this->l('Enabled').'" title="'.$this->l('Enabled').'" /></label>
  125. <input type="radio" name="active" id="active_off" value="0" '.(!$this->getFieldValue($obj, 'active') ? 'checked="checked" ' : '').'/>
  126. <label class="t" for="active_off"> <img src="../img/admin/disabled.gif" alt="'.$this->l('Disabled').'" title="'.$this->l('Disabled').'" /></label>
  127. <p>'.$this->l('Include or exclude carrier from list of carriers on Front Office').'</p>
  128. </div>
  129. <label>'.$this->l('Shipping & handling:').' </label>
  130. <div class="margin-form">
  131. <input type="radio" name="shipping_handling" id="shipping_handling_on" value="1" '.($this->getFieldValue($obj, 'shipping_handling') ? 'checked="checked" ' : '').'/>
  132. <label class="t" for="shipping_handling_on"> <img src="../img/admin/enabled.gif" alt="'.$this->l('Enabled').'" title="'.$this->l('Enabled').'" /></label>
  133. <input type="radio" name="shipping_handling" id="shipping_handling_off" value="0" '.(!$this->getFieldValue($obj, 'shipping_handling') ? 'checked="checked" ' : '').'/>
  134. <label class="t" for="shipping_handling_off"> <img src="../img/admin/disabled.gif" alt="'.$this->l('Disabled').'" title="'.$this->l('Disabled').'" /></label>
  135. <p>'.$this->l('Include the shipping & handling costs in carrier price').'</p>
  136. </div>
  137. <label>'.$this->l('Out-of-range behavior:').' </label>
  138. <div class="margin-form">
  139. <select name="range_behavior">
  140. <option value="0"'.(!$this->getFieldValue($obj, 'range_behavior') ? ' selected="selected"' : '').'>'.$this->l('Apply the cost of the highest defined range').'</option>
  141. <option value="1"'.($this->getFieldValue($obj, 'range_behavior') ? ' selected="selected"' : '').'>'.$this->l('Disable carrier').'</option>
  142. </select>
  143. <p>'.$this->l('Out-of-range behavior when none is defined (e.g., when a customer\'s cart weight is superior to the highest range limit)').'</p>
  144. </div>
  145. <label>'.$this->l('Module:').' </label>
  146. <div class="margin-form">
  147. <input type="radio" name="is_module" id="active_on" value="1" '.($this->getFieldValue($obj, 'is_module') ? 'checked="checked" ' : '').'/>
  148. <label class="t" for="active_on"> <img src="../img/admin/enabled.gif" alt="'.$this->l('Enabled').'" title="'.$this->l('Enabled').'" /></label>
  149. <input type="radio" name="is_module" id="active_off" value="0" '.(!$this->getFieldValue($obj, 'is_module') ? 'checked="checked" ' : '').'/>
  150. <label class="t" for="active_off"> <img src="../img/admin/disabled.gif" alt="'.$this->l('Disabled').'" title="'.$this->l('Disabled').'" /></label>
  151. <p>'.$this->l('Mark this carrier as linked to a module').'</p>
  152. </div>
  153. <div class="margin-form">
  154. <input type="submit" value="'.$this->l(' Save ').'" name="submitAdd'.$this->table.'" class="button" />
  155. </div>
  156. <div class="small"><sup>*</sup> '.$this->l('Required field').'</div>
  157. </fieldset>
  158. </form>';
  159. }
  160. public function beforeDelete($object)
  161. {
  162. return $object->isUsed();
  163. }
  164. public function afterDelete($object, $oldId)
  165. {
  166. $object->copyCarrierData(intval($oldId));
  167. }
  168. private function changeGroups($id_carrier, $delete = true)
  169. {
  170. if ($delete)
  171. Db::getInstance()->Execute('DELETE FROM '._DB_PREFIX_.'carrier_group WHERE id_carrier='.intval($id_carrier));
  172. $groups = Db::getInstance()->ExecuteS('SELECT id_group FROM `'._DB_PREFIX_.'group`');
  173. foreach ($groups as $group)
  174. if (in_array($group['id_group'], $_POST['groupBox']))
  175. Db::getInstance()->Execute('INSERT INTO '._DB_PREFIX_.'carrier_group (id_group, id_carrier) VALUES('.intval($group['id_group']).','.intval($id_carrier).')');
  176. }
  177. public function postProcess()
  178. {
  179. global $currentIndex;
  180. if (Tools::getValue('submitAdd'.$this->table))
  181. {
  182. /* Checking fields validity */
  183. $this->validateRules();
  184. if (!sizeof($this->_errors))
  185. {
  186. $id = intval(Tools::getValue('id_'.$this->table));
  187. /* Object update */
  188. if (isset($id) AND !empty($id))
  189. {
  190. if ($this->tabAccess['edit'] === '1')
  191. {
  192. $object = new $this->className($id);
  193. if (Validate::isLoadedObject($object))
  194. {
  195. Db::getInstance()->Execute('DELETE FROM '._DB_PREFIX_.'carrier_group WHERE id_carrier='.intval($id));
  196. $object->deleted = 1;
  197. $object->update();
  198. $objectNew = new $this->className();
  199. $this->copyFromPost($objectNew, $this->table);
  200. $result = $objectNew->add();
  201. if (Validate::isLoadedObject($objectNew))
  202. {
  203. $this->afterDelete($objectNew, $object->id);
  204. Hook::updateCarrier(intval($object->id), $objectNew);
  205. }
  206. $this->changeGroups($objectNew->id);
  207. if (!$result)
  208. $this->_errors[] = Tools::displayError('an error occurred while updating object').' <b>'.$this->table.'</b>';
  209. elseif ($this->postImage($objectNew->id))
  210. {
  211. $this->changeZones($objectNew->id);
  212. Tools::redirectAdmin($currentIndex.'&id_'.$this->table.'='.$object->id.'&conf=4'.'&token='.$this->token);
  213. }
  214. }
  215. else
  216. $this->_errors[] = Tools::displayError('an error occurred while updating object').' <b>'.$this->table.'</b> '.Tools::displayError('(cannot load object)');
  217. }
  218. else
  219. $this->_errors[] = Tools::displayError('You do not have permission to edit anything here.');
  220. }
  221. /* Object creation */
  222. else
  223. {
  224. if ($this->tabAccess['add'] === '1')
  225. {
  226. $object = new $this->className();
  227. $this->copyFromPost($object, $this->table);
  228. if (!$object->add())
  229. $this->_errors[] = Tools::displayError('an error occurred while creating object').' <b>'.$this->table.'</b>';
  230. elseif (($_POST['id_'.$this->table] = $object->id /* voluntary */) AND $this->postImage($object->id) AND $this->_redirect)
  231. {
  232. $this->changeZones($object->id);
  233. $this->changeGroups($object->id);
  234. Tools::redirectAdmin($currentIndex.'&id_'.$this->table.'='.$object->id.'&conf=3'.'&token='.$this->token);
  235. }
  236. }
  237. else
  238. $this->_errors[] = Tools::displayError('You do not have permission to add anything here.');
  239. }
  240. }
  241. }
  242. else
  243. {
  244. if ((Tools::isSubmit('submitDel'.$this->table) && in_array(Configuration::get('PS_CARRIER_DEFAULT'), Tools::getValue('carrierBox')))
  245. OR (isset($_GET['delete'.$this->table]) AND Tools::getValue('id_carrier') == Configuration::get('PS_CARRIER_DEFAULT')))
  246. $this->_errors[] = $this->l('Please set another carrier as default before deleting');
  247. else
  248. parent::postProcess();
  249. }
  250. }
  251. function changeZones($id)
  252. {
  253. $carrier = new $this->className($id);
  254. if (!Validate::isLoadedObject($carrier))
  255. die (Tools::displayError('object cannot be loaded'));
  256. $zones = Zone::getZones(true);
  257. foreach ($zones as $zone)
  258. if (sizeof($carrier->getZone($zone['id_zone'])))
  259. {
  260. if (!isset($_POST['zone_'.$zone['id_zone']]) OR !$_POST['zone_'.$zone['id_zone']])
  261. $carrier->deleteZone($zone['id_zone']);
  262. }
  263. else
  264. if (isset($_POST['zone_'.$zone['id_zone']]) AND $_POST['zone_'.$zone['id_zone']])
  265. $carrier->addZone($zone['id_zone']);
  266. }
  267. public function displayListContent($token = NULL)
  268. {
  269. foreach ($this->_list as $key => $list)
  270. if ($list['name'] == '0')
  271. $this->_list[$key]['name'] = Configuration::get('PS_SHOP_NAME');
  272. parent::displayListContent($token);
  273. }
  274. }
  275. ?>