* @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?').''.
'
'.
'
'.'
- '.$this->l('Click "Add new".').'
'.'
- '.$this->l('Fill in the fields and click "Save".').'
- '.$this->l('You need to decide a price range or a weight range for which the new carrier will be available. Under the "Shipping" tab, click either "Price Ranges" or "Weight Ranges".').'
- '.$this->l('Click "Add new".').'
- '.$this->l('Select the name of the carrier and define the price range or the weight range. For example the carrier can be made available for a weight range between 0 and 5kgs. Another carrier will have a range between 5 and 10kgs.').'
- '.$this->l('When you are done, click "Save".').'
- '.$this->l('Click on the "Shipping" tab.').'
- '.$this->l('You need to choose the fees that will be applied for this carrier. At the bottom on the page, in the "Fees" section, select the name of the carrier.').'
- '.$this->l('For each zone, enter a price. Click "Save".').'
- '.$this->l('You\'re set! The new carrier will be displayed to your customers.').'
';
}
public function displayForm($isMainTab = true)
{
global $currentIndex, $cookie;
parent::displayForm();
if (!($obj = $this->loadObject(true)))
return;
$currentLanguage = (int)($cookie->id_lang);
echo '
';
}
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);
}
}