PageRenderTime 42ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/admin444/tabs/AdminShipping.php

http://marocmall.googlecode.com/
PHP | 262 lines | 218 code | 26 blank | 18 comment | 25 complexity | 12d6866fd1fc78a3fbe782978ccf3f5a MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Shipping tab for admin panel, AdminShipping.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 AdminShipping extends AdminTab
  14. {
  15. private $_fieldsHandling;
  16. public function __construct()
  17. {
  18. $this->table = 'delivery';
  19. $currency = new Currency(Configuration::get('PS_CURRENCY_DEFAULT'));
  20. $this->_fieldsHandling = array(
  21. 'PS_SHIPPING_HANDLING' => array('title' => $this->l('Handling charges'), 'suffix' => $currency, 'validation' => 'isPrice', 'cast' => 'floatval'),
  22. 'PS_SHIPPING_FREE_PRICE' => array('title' => $this->l('Free shipping starts at'), 'suffix' => $currency, 'validation' => 'isPrice', 'cast' => 'floatval'),
  23. 'PS_SHIPPING_FREE_WEIGHT' => array('title' => $this->l('Free shipping starts at'), 'suffix' => Configuration::get('PS_WEIGHT_UNIT'), 'validation' => 'isUnsignedFloat', 'cast' => 'floatval'),
  24. 'PS_SHIPPING_METHOD' => array('title' => $this->l('Billing'), 'validation' => 'isBool', 'cast' => 'intval'));
  25. parent::__construct();
  26. }
  27. public function postProcess()
  28. {
  29. global $currentIndex;
  30. /* Handling settings */
  31. if (isset($_POST['submitHandling'.$this->table]))
  32. {
  33. if ($this->tabAccess['edit'] === '1')
  34. {
  35. /* Check required fields */
  36. foreach ($this->_fieldsHandling AS $field => $values)
  37. if (($value = Tools::getValue($field)) == false AND (string)$value != '0')
  38. $this->_errors[] = Tools::displayError('field').' <b>'.$values['title'].'</b> '.Tools::displayError('is required');
  39. /* Check field validity */
  40. foreach ($this->_fieldsHandling AS $field => $values)
  41. if (Tools::getValue($field))
  42. {
  43. $function = $values['validation'];
  44. if (!Validate::$function(Tools::getValue($field)))
  45. $this->_errors[] = Tools::displayError('field').' <b>'.$values['title'].'</b> '.Tools::displayError('is invalid');
  46. }
  47. /* Update configuration values */
  48. if (!sizeof($this->_errors))
  49. {
  50. foreach ($this->_fieldsHandling AS $field => $values)
  51. {
  52. $function = $values['cast'];
  53. Configuration::updateValue($field, call_user_func($function, Tools::getValue($field)));
  54. }
  55. Tools::redirectAdmin($currentIndex.'&conf=6'.'&token='.$this->token);
  56. }
  57. }
  58. else
  59. $this->_errors[] = Tools::displayError('You do not have permission to edit anything here.');
  60. }
  61. /* Shipping fees */
  62. elseif (isset($_POST['submitFees'.$this->table]))
  63. {
  64. if ($this->tabAccess['edit'] === '1')
  65. {
  66. if (($id_carrier = intval(Tools::getValue('id_carrier'))) AND $id_carrier == ($id_carrier2 = intval(Tools::getValue('id_carrier2'))))
  67. {
  68. $carrier = new Carrier($id_carrier);
  69. if (Validate::isLoadedObject($carrier))
  70. {
  71. /* Get configuration values */
  72. $shipping_method = intval(Configuration::get('PS_SHIPPING_METHOD'));
  73. $rangeTable = $shipping_method ? 'range_weight' : 'range_price';
  74. $carrier->deleteDeliveryPrice($rangeTable);
  75. /* Build prices list */
  76. $priceList = '';
  77. foreach ($_POST AS $key => $value)
  78. if (strstr($key, 'fees_'))
  79. {
  80. $tmpArray = explode('_', $key);
  81. $priceList .= '('.($shipping_method == 0 ? intval($tmpArray[2]) : 'NULL').',
  82. '.($shipping_method == 1 ? intval($tmpArray[2]) : 'NULL').', '.$carrier->id.',
  83. '.intval($tmpArray[1]).', '.number_format(abs($value), 2, '.', '').'),';
  84. unset($tmpArray);
  85. }
  86. $priceList = rtrim($priceList, ',');
  87. /* Update delivery prices */
  88. $carrier->addDeliveryPrice($priceList);
  89. Tools::redirectAdmin($currentIndex.'&conf=6'.'&token='.$this->token);
  90. }
  91. else
  92. $this->_errors[] = Tools::displayError('an error occurred while updating fees (cannot load carrier object)');
  93. }
  94. elseif (isset($id_carrier2))
  95. {
  96. $_POST['id_carrier'] = $id_carrier2;
  97. }
  98. else
  99. $this->_errors[] = Tools::displayError('an error occurred while updating fees (cannot load carrier object)');
  100. }
  101. else
  102. $this->_errors[] = Tools::displayError('You do not have permission to edit anything here.');
  103. }
  104. }
  105. public function display()
  106. {
  107. $this->displayFormHandling();
  108. $this->displayFormFees();
  109. }
  110. public function displayFormHandling()
  111. {
  112. global $currentIndex;
  113. $confKeys = $this->_fieldsHandling;
  114. foreach ($confKeys AS $key => $confKey)
  115. $getConf[] = $key;
  116. $confValues = Configuration::getMultiple($getConf);
  117. unset($confKeys['PS_SHIPPING_METHOD']);
  118. echo '
  119. <form action="'.$currentIndex.'&submitHandling'.$this->table.'=1&token='.$this->token.'" method="post" class="width2">
  120. <fieldset>
  121. <legend><img src="../img/admin/delivery.gif" />'.$this->l('Handling').'</legend>';
  122. foreach ($confKeys AS $key => $confKey)
  123. {
  124. $postValue = Tools::getValue($key);
  125. $sign_left = (is_object($confKey['suffix']) ? $confKey['suffix']->getSign('left') : '');
  126. $sign_right = (is_object($confKey['suffix']) ? $confKey['suffix']->getSign('right') : (is_string($confKey['suffix']) ? '&nbsp;'.$confKey['suffix'] : ''));
  127. echo '
  128. <label class="clear">'.$confKey['title'].':</label>
  129. <div class="margin-form">';
  130. echo $sign_left;
  131. echo '<input size="6" type="text" name="'.$key.'" value="'.(($postValue != false OR (string)$postValue == '0') ? $postValue : $confValues[$key]).'" />';
  132. echo $sign_right.' '.($key == 'PS_SHIPPING_HANDLING' ? $this->l('(tax excl.)') : '');
  133. echo '</div>';
  134. }
  135. echo '
  136. <div class="margin-form" style="margin-top: 20px;">
  137. <input type="submit" value="'.$this->l(' Save ').'" name="submitHandling'.$this->table.'" class="button" />
  138. </div>
  139. <p style="font-weight: bold; font-size: 11px;">'.$this->l('Tips:').'</p>
  140. <ul style="list-style-type: disc; font-size: 11px; color:#7F7F7F; margin-left: 30px; line-height: 20px;">
  141. <li>'.$this->l('If you set these parameters to 0, they will be disabled').'</li>
  142. <li>'.$this->l('Coupons are not taken into account when calculating free shipping').'</li>
  143. </ul>
  144. </fieldset>
  145. <br />
  146. <fieldset>
  147. <legend><img src="../img/admin/money.gif" />'.$this->l('Billing').'</legend>
  148. <label class="clear">'.$this->l('Choice of range:').' </label>
  149. <div class="margin-form">
  150. <input type="radio" name="PS_SHIPPING_METHOD" value="0" id="total_price"
  151. '.((isset($confValues['PS_SHIPPING_METHOD']) AND $confValues['PS_SHIPPING_METHOD'] == 0) ? 'checked="checked"' : '').'/>
  152. <label class="t" for="total_price"> '.$this->l('According to total price').'</label><br />
  153. <input type="radio" name="PS_SHIPPING_METHOD" value="1" id="total_weight"
  154. '.((!isset($confValues['PS_SHIPPING_METHOD']) OR $confValues['PS_SHIPPING_METHOD'] == 1) ? 'checked="checked"' : '').'/>
  155. <label class="t" for="total_weight"> '.$this->l('According to total weight').'</label>
  156. </div>
  157. <div class="margin-form">
  158. <input type="submit" value="'.$this->l(' Save ').'" name="submitHandling'.$this->table.'" class="button" />
  159. </div>
  160. </fieldset>
  161. </form>';
  162. }
  163. public function displayFormFees()
  164. {
  165. global $currentIndex;
  166. echo '<br /><br />
  167. <h2>'.$this->l('Fees by carrier, geographical zone, and ranges').'</h2>
  168. <form action="'.$currentIndex.'&token='.$this->token.'" id="fees" name="fees" method="post" class="width2">
  169. <fieldset>
  170. <legend><img src="../img/admin/delivery.gif" />'.$this->l('Fees').'</legend>
  171. <b>'.$this->l('Carrier:').' </b>
  172. <select name="id_carrier2" onchange="document.fees.submit();">';
  173. $carriers = Carrier::getCarriers(intval(Configuration::get('PS_LANG_DEFAULT')));
  174. $id_carrier = Tools::getValue('id_carrier') ? intval(Tools::getValue('id_carrier')) : intval($carriers[0]['id_carrier']);
  175. $carrierSelected = new Carrier($id_carrier);
  176. foreach ($carriers AS $carrier)
  177. echo '<option value="'.intval($carrier['id_carrier']).'"'.(($carrier['id_carrier'] == $id_carrier) ? ' selected="selected"' : '').'>'.$carrier['name'].'</option>';
  178. echo '
  179. </select><br />
  180. <table class="table space" cellpadding="0" cellspacing="0">
  181. <tr>
  182. <th>'.$this->l('Zone / Range').'</th>';
  183. $shipping_method = intval(Configuration::get('PS_SHIPPING_METHOD'));
  184. $currency = new Currency(Configuration::get('PS_CURRENCY_DEFAULT'));
  185. $rangeObj = $shipping_method ? new RangeWeight() : new RangePrice();
  186. $rangeTable = $shipping_method ? 'range_weight' : 'range_price';
  187. $rangeIdentifier = 'id_'.$rangeTable;
  188. $ranges = $rangeObj->getRanges($id_carrier);
  189. $suffix = $shipping_method ? Configuration::get('PS_WEIGHT_UNIT') : $currency->sign;
  190. $delivery = Carrier::getDeliveryPriceByRanges($rangeTable, $id_carrier);
  191. foreach ($delivery AS $deliv)
  192. $deliveryArray[$deliv['id_zone']][$deliv['id_carrier']][$deliv[$rangeIdentifier]] = $deliv['price'];
  193. foreach ($ranges AS $range)
  194. echo '<th style="font-size: 11px;">'.floatval($range['delimiter1']).$suffix.' '.$this->l('to').' '.floatval($range['delimiter2']).$suffix.'</th>';
  195. echo '</tr>';
  196. $zones = Zone::getZones(true);
  197. if (sizeof($ranges))
  198. foreach ($zones AS $zone)
  199. {
  200. if (!$carrierSelected->getZone($zone['id_zone']))
  201. continue ;
  202. echo '
  203. <tr>
  204. <th style="height: 30px;">'.$zone['name'].'</th>';
  205. foreach ($ranges AS $range)
  206. {
  207. if (isset($deliveryArray[$zone['id_zone']][$id_carrier][$range[$rangeIdentifier]]))
  208. $price = $deliveryArray[$zone['id_zone']][$id_carrier][$range[$rangeIdentifier]];
  209. else
  210. $price = '0.00';
  211. echo '<td class="center">'.$currency->getSign('left').'<input type="text" name="fees_'.$zone['id_zone'].'_'.$range[$rangeIdentifier].'" value="'.$price.'" style="width: 45px;" />'.$currency->getSign('right').'</td>';
  212. }
  213. echo '
  214. </tr>';
  215. }
  216. echo '
  217. <tr>
  218. <td colspan="'.(sizeof($ranges) + 1).'" class="center" style="border-bottom: none; height: 40px;">
  219. <input type="hidden" name="submitFees'.$this->table.'" value="1" />
  220. ';
  221. if (sizeof($ranges))
  222. echo ' <input type="submit" value="'.$this->l(' Save ').'" class="button" />';
  223. else
  224. echo $this->l('No ranges set for this carrier');
  225. echo '
  226. </td>
  227. </tr>';
  228. echo '
  229. </table>
  230. <p>'.$this->l('Prices are tax excluded.').'</p>
  231. </fieldset>
  232. <input type="hidden" name="id_carrier" value="'.$id_carrier.'" />
  233. </form>';
  234. }
  235. }
  236. ?>