* @copyright 2007-2011 PrestaShop SA * @version Release: $Revision: 7541 $ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * International Registered Trademark & Property of PrestaShop SA */ include_once(PS_ADMIN_DIR.'/../classes/AdminTab.php'); class AdminCarriers extends AdminTab { protected $maxImageSize = 30000; public function __construct() { global $cookie; $this->table = 'carrier'; $this->className = 'Carrier'; $this->lang = true; $this->edit = true; $this->delete = true; $this->deleted = true; $this->fieldImageSettings = array('name' => 'logo', 'dir' => 's'); $this->fieldsDisplay = array( 'id_carrier' => array('title' => $this->l('ID'), 'align' => 'center', 'width' => 25), 'name' => array('title' => $this->l('Name'), 'width' => 100), 'logo' => array('title' => $this->l('Logo'), 'align' => 'center', 'image' => 's', 'orderby' => false, 'search' => false), 'delay' => array('title' => $this->l('Delay'), 'width' => 300, 'orderby' => false), 'active' => array('title' => $this->l('Status'), 'align' => 'center', 'active' => 'status', 'type' => 'bool', 'orderby' => false), 'is_free' => array('title' => $this->l('Is Free'), 'align' => 'center', 'icon' => array(0 => 'disabled.gif', 1 => 'enabled.gif', 'default' => 'disabled.gif'), 'type' => 'bool', 'orderby' => false)); $this->optionTitle = $this->l('Carrier options'); $this->_fieldsOptions = array( '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((int)(Configuration::get('PS_LANG_DEFAULT')), true , false,false, NULL, ALL_CARRIERS)), ); parent::__construct(); } public function displayTop() { echo '
'. ' '.$this->l('How to create a new carrier?').''. '
'. '

'; } public function displayForm($isMainTab = true) { global $currentIndex, $cookie; parent::displayForm(); if (!($obj = $this->loadObject(true))) return; $currentLanguage = (int)($cookie->id_lang); echo '
'.($obj->id ? '' : '').'
'.$this->l('Carriers').'
* '.$this->l('Allowed characters: letters, spaces and').' ().- 

'.$this->l('Carrier name displayed during checkout').'
'.$this->l('With a value of 0, the carrier name will be replaced by the shop name').'

'.$this->l('Upload logo from your computer').' (.gif, .jpg, .jpeg '.$this->l('or').' .png)

'; foreach ($this->_languages as $language) echo '
*
'; $this->displayFlags($this->_languages, $this->_defaultFormLanguage, 'delay', 'delay'); echo '

'.$this->l('Time taken for product delivery; displayed during checkout').'

'.$this->l('URL for the tracking number; type \'@\' where the tracking number will appear').'

'; $carrier_zones = $obj->getZones(); $carrier_zones_ids = array(); if (is_array($carrier_zones)) foreach($carrier_zones as $carrier_zone) $carrier_zones_ids[] = $carrier_zone['id_zone']; $zones = Zone::getZones(false); foreach ($zones AS $zone) echo '
'; echo '

'.$this->l('The zone in which this carrier is to be used').'

'; $groups = Group::getGroups((int)($cookie->id_lang)); if (sizeof($groups)) { echo ' '; $irow = 0; foreach ($groups as $group) echo ' '; echo '
id) ? 'checked="checked" ' : '').' /> '.$this->l('ID').' '.$this->l('Group name').'
getValue('SELECT id_group FROM '._DB_PREFIX_.'carrier_group WHERE id_carrier='.(int)($obj->id).' AND id_group='.(int)($group['id_group'])) OR (!isset($obj->id))) ? 'checked="checked" ' : '').'/> '.$group['id_group'].'

'.$this->l('Mark all groups you want to give access to this carrier').'

'; } else echo '

'.$this->l('No group created').'

'; echo '
getFieldValue($obj, 'active') ? 'checked="checked" ' : '').'/> getFieldValue($obj, 'active') ? 'checked="checked" ' : '').'/>

'.$this->l('Include or exclude carrier from list of carriers on Front Office').'

getFieldValue($obj, 'is_free') ? 'checked="checked" ' : '').'/> getFieldValue($obj, 'is_free') ? 'checked="checked" ' : '').'/>

'.$this->l('Apply shipping costs and additional shipping costs by products in carrier price').'

getFieldValue($obj, 'shipping_handling') ? 'checked="checked" ' : '').'/> getFieldValue($obj, 'shipping_handling') ? 'checked="checked" ' : '').'/>

'.$this->l('Include the shipping & handling costs in carrier price').'

getFieldValue($obj, 'shipping_method') == Carrier::SHIPPING_METHOD_DEFAULT ? 'checked="checked" ' : '').'/>
getFieldValue($obj, 'shipping_method') == Carrier::SHIPPING_METHOD_PRICE ? 'checked="checked" ' : '').'/>
getFieldValue($obj, 'shipping_method') == Carrier::SHIPPING_METHOD_WEIGHT ? 'checked="checked" ' : '').'/>

'.$this->l('Out-of-range behavior when none is defined (e.g., when a customer\'s cart weight is greater than the highest range limit)').'

'; if ($this->getFieldValue($obj, 'is_module')) { echo '

- '. $this->l('This carrier is bound to this module ').' => '.$this->getFieldValue($obj, 'external_module_name').'

'; if ($this->getFieldValue($obj, 'shipping_external')) { echo '

- '.$this->l('The shipping costs are calculated outside of your shop').'

'; } if ($this->getFieldValue($obj, 'need_range')) { echo '

- '.$this->l('This carrier uses PrestaShop range to calculate shipping costs').'

'; } echo '
'; } echo '
* '.$this->l('Required field').'
'; } public function beforeDelete($object) { return $object->isUsed(); } public function afterDelete($object, $oldId) { $object->copyCarrierData((int)($oldId)); } private function changeGroups($id_carrier, $delete = true) { if ($delete) Db::getInstance()->Execute('DELETE FROM '._DB_PREFIX_.'carrier_group WHERE id_carrier='.(int)($id_carrier)); $groups = Db::getInstance()->ExecuteS('SELECT id_group FROM `'._DB_PREFIX_.'group`'); foreach ($groups as $group) if (in_array($group['id_group'], $_POST['groupBox'])) Db::getInstance()->Execute('INSERT INTO '._DB_PREFIX_.'carrier_group (id_group, id_carrier) VALUES('.(int)($group['id_group']).','.(int)($id_carrier).')'); } public function postProcess() { global $currentIndex; if (Tools::getValue('submitAdd'.$this->table)) { /* Checking fields validity */ $this->validateRules(); if (!sizeof($this->_errors)) { $id = (int)(Tools::getValue('id_'.$this->table)); /* Object update */ if (isset($id) AND !empty($id)) { if ($this->tabAccess['edit'] === '1') { $object = new $this->className($id); if (Validate::isLoadedObject($object)) { Db::getInstance()->Execute('DELETE FROM '._DB_PREFIX_.'carrier_group WHERE id_carrier='.(int)($id)); $object->deleted = 1; $object->update(); $objectNew = new $this->className(); $this->copyFromPost($objectNew, $this->table); $result = $objectNew->add(); if (Validate::isLoadedObject($objectNew)) { $this->afterDelete($objectNew, $object->id); Hook::updateCarrier((int)($object->id), $objectNew); } $this->changeGroups($objectNew->id); if (!$result) $this->_errors[] = Tools::displayError('An error occurred while updating object.').' '.$this->table.''; elseif ($this->postImage($objectNew->id)) { $this->changeZones($objectNew->id); Tools::redirectAdmin($currentIndex.'&id_'.$this->table.'='.$object->id.'&conf=4'.'&token='.$this->token); } } else $this->_errors[] = Tools::displayError('An error occurred while updating object.').' '.$this->table.' '.Tools::displayError('(cannot load object)'); } else $this->_errors[] = Tools::displayError('You do not have permission to edit here.'); } /* Object creation */ else { if ($this->tabAccess['add'] === '1') { $object = new $this->className(); $this->copyFromPost($object, $this->table); if (!$object->add()) $this->_errors[] = Tools::displayError('An error occurred while creating object.').' '.$this->table.''; elseif (($_POST['id_'.$this->table] = $object->id /* voluntary */) AND $this->postImage($object->id) AND $this->_redirect) { $this->changeZones($object->id); $this->changeGroups($object->id); Tools::redirectAdmin($currentIndex.'&id_'.$this->table.'='.$object->id.'&conf=3'.'&token='.$this->token); } } else $this->_errors[] = Tools::displayError('You do not have permission to add here.'); } } } else { if ((Tools::isSubmit('submitDel'.$this->table) && in_array(Configuration::get('PS_CARRIER_DEFAULT'), Tools::getValue('carrierBox'))) OR (isset($_GET['delete'.$this->table]) AND Tools::getValue('id_carrier') == Configuration::get('PS_CARRIER_DEFAULT'))) $this->_errors[] = $this->l('Please set another carrier as default before deleting'); else parent::postProcess(); } } function changeZones($id) { $carrier = new $this->className($id); if (!Validate::isLoadedObject($carrier)) die (Tools::displayError('Object cannot be loaded')); $zones = Zone::getZones(true); foreach ($zones as $zone) if (sizeof($carrier->getZone($zone['id_zone']))) { if (!isset($_POST['zone_'.$zone['id_zone']]) OR !$_POST['zone_'.$zone['id_zone']]) $carrier->deleteZone($zone['id_zone']); } else if (isset($_POST['zone_'.$zone['id_zone']]) AND $_POST['zone_'.$zone['id_zone']]) $carrier->addZone($zone['id_zone']); } public function displayListContent($token = NULL) { foreach ($this->_list as $key => $list) if ($list['name'] == '0') $this->_list[$key]['name'] = Configuration::get('PS_SHOP_NAME'); parent::displayListContent($token); } }