//ParentOrderController : Refacto done

This commit is contained in:
mDeflotte committed 2011-09-28 16:14:50 +00:00
1 parent fe96631b7b
commit 68fc08c0f8
1 file changed
+39 -36
+39 -36
View File
@@ -25,8 +25,12 @@
* International Registered Trademark & Property of PrestaShop SA * International Registered Trademark & Property of PrestaShop SA
*/ */
/* Class FreeOrder to use PaymentModule (abstract class, cannot be instancied) */ /**
class FreeOrder extends PaymentModule {} * Class FreeOrder to use PaymentModule (abstract class, cannot be instancied)
*/
class FreeOrder extends PaymentModule
{
}
class ParentOrderControllerCore extends FrontController class ParentOrderControllerCore extends FrontController
{ {
@@ -35,28 +39,28 @@ class ParentOrderControllerCore extends FrontController
public $nbProducts; public $nbProducts;
public function __construct() /**
* Initialize product controller
* @see FrontController::init()
*/
public function init()
{ {
parent::__construct(); parent::init();
/* Disable some cache related bugs on the cart/order */ /* Disable some cache related bugs on the cart/order */
header('Cache-Control: no-cache, must-revalidate'); header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
}
public function init()
{
parent::init();
$this->nbProducts = $this->context->cart->nbProducts(); $this->nbProducts = $this->context->cart->nbProducts();
global $isVirtualCart; global $isVirtualCart;
// Redirect to the good order process // Redirect to the good order process
if (Configuration::get('PS_ORDER_PROCESS_TYPE') == 0 AND Dispatcher::getInstance()->getController() != 'order') if (Configuration::get('PS_ORDER_PROCESS_TYPE') == 0 && Dispatcher::getInstance()->getController() != 'order')
Tools::redirect('index.php?controller=order'); Tools::redirect('index.php?controller=order');
if (Configuration::get('PS_ORDER_PROCESS_TYPE') == 1 AND Dispatcher::getInstance()->getController() != 'orderopc') if (Configuration::get('PS_ORDER_PROCESS_TYPE') == 1 && Dispatcher::getInstance()->getController() != 'orderopc')
{ {
if (isset($_GET['step']) AND $_GET['step'] == 3) if (isset($_GET['step']) && $_GET['step'] == 3)
Tools::redirect('index.php?controller=order-opc&isPaymentStep=true'); Tools::redirect('index.php?controller=order-opc&isPaymentStep=true');
Tools::redirect('index.php?controller=order-opc'); Tools::redirect('index.php?controller=order-opc');
} }
@@ -64,11 +68,11 @@ class ParentOrderControllerCore extends FrontController
if (Configuration::get('PS_CATALOG_MODE')) if (Configuration::get('PS_CATALOG_MODE'))
$this->errors[] = Tools::displayError('This store has not accepted your new order.'); $this->errors[] = Tools::displayError('This store has not accepted your new order.');
if (Tools::isSubmit('submitReorder') AND $id_order = (int)Tools::getValue('id_order')) if (Tools::isSubmit('submitReorder') && $id_order = (int)Tools::getValue('id_order'))
{ {
$oldCart = new Cart(Order::getCartIdStatic($id_order, $this->context->customer->id)); $oldCart = new Cart(Order::getCartIdStatic($id_order, $this->context->customer->id));
$duplication = $oldCart->duplicate(); $duplication = $oldCart->duplicate();
if (!$duplication OR !Validate::isLoadedObject($duplication['cart'])) if (!$duplication || !Validate::isLoadedObject($duplication['cart']))
$this->errors[] = Tools::displayError('Sorry, we cannot renew your order.'); $this->errors[] = Tools::displayError('Sorry, we cannot renew your order.');
else if (!$duplication['success']) else if (!$duplication['success'])
$this->errors[] = Tools::displayError('Missing items - we are unable to renew your order'); $this->errors[] = Tools::displayError('Missing items - we are unable to renew your order');
@@ -99,7 +103,7 @@ class ParentOrderControllerCore extends FrontController
} }
else else
$this->errors[] = Tools::displayError('Voucher name invalid.'); $this->errors[] = Tools::displayError('Voucher name invalid.');
if (!sizeof($this->errors)) if (!count($this->errors))
{ {
$this->context->cart->addDiscount((int)($discount->id)); $this->context->cart->addDiscount((int)($discount->id));
Tools::redirect('index.php?controller=order-opc'); Tools::redirect('index.php?controller=order-opc');
@@ -118,7 +122,7 @@ class ParentOrderControllerCore extends FrontController
/* Is there only virtual product in cart */ /* Is there only virtual product in cart */
if ($isVirtualCart = $this->context->cart->isVirtualCart()) if ($isVirtualCart = $this->context->cart->isVirtualCart())
$this->_setNoCarrier(); $this->setNoCarrier();
} }
$this->context->smarty->assign('back', Tools::safeOutput(Tools::getValue('back'))); $this->context->smarty->assign('back', Tools::safeOutput(Tools::getValue('back')));
@@ -134,18 +138,18 @@ class ParentOrderControllerCore extends FrontController
// Adding JS files // Adding JS files
$this->addJS(_THEME_JS_DIR_.'tools.js'); $this->addJS(_THEME_JS_DIR_.'tools.js');
if ((Configuration::get('PS_ORDER_PROCESS_TYPE') == 0 AND Tools::getValue('step') == 1) OR Configuration::get('PS_ORDER_PROCESS_TYPE') == 1) if ((Configuration::get('PS_ORDER_PROCESS_TYPE') == 0 && Tools::getValue('step') == 1) || Configuration::get('PS_ORDER_PROCESS_TYPE') == 1)
$this->addJS(_THEME_JS_DIR_.'order-address.js'); $this->addJS(_THEME_JS_DIR_.'order-address.js');
$this->addJS(_PS_JS_DIR_.'jquery/jquery.fancybox-1.3.4.js'); $this->addJS(_PS_JS_DIR_.'jquery/jquery.fancybox-1.3.4.js');
if ((int)(Configuration::get('PS_BLOCK_CART_AJAX')) OR Configuration::get('PS_ORDER_PROCESS_TYPE') == 1) if ((int)(Configuration::get('PS_BLOCK_CART_AJAX')) || Configuration::get('PS_ORDER_PROCESS_TYPE') == 1)
{ {
$this->addJS(_THEME_JS_DIR_.'cart-summary.js'); $this->addJS(_THEME_JS_DIR_.'cart-summary.js');
$this->addJS(_PS_JS_DIR_.'jquery/jquery-typewatch.pack.js'); $this->addJS(_PS_JS_DIR_.'jquery/jquery-typewatch.pack.js');
} }
} }
/** /**
* Check if order is free
* @return boolean * @return boolean
*/ */
protected function _checkFreeOrder() protected function _checkFreeOrder()
@@ -204,7 +208,7 @@ class ParentOrderControllerCore extends FrontController
$this->context->cart->gift_message = strip_tags($_POST['gift_message']); $this->context->cart->gift_message = strip_tags($_POST['gift_message']);
} }
if (isset($this->context->customer->id) AND $this->context->customer->id) if (isset($this->context->customer->id) && $this->context->customer->id)
{ {
$address = new Address((int)($this->context->cart->id_address_delivery)); $address = new Address((int)($this->context->cart->id_address_delivery));
if (!($id_zone = Address::getZoneById($address->id))) if (!($id_zone = Address::getZoneById($address->id)))
@@ -213,9 +217,9 @@ class ParentOrderControllerCore extends FrontController
else else
$id_zone = Country::getIdZone((int)Configuration::get('PS_COUNTRY_DEFAULT')); $id_zone = Country::getIdZone((int)Configuration::get('PS_COUNTRY_DEFAULT'));
if (Validate::isInt(Tools::getValue('id_carrier')) AND sizeof(Carrier::checkCarrierZone((int)(Tools::getValue('id_carrier')), (int)($id_zone)))) if (Validate::isInt(Tools::getValue('id_carrier')) && count(Carrier::checkCarrierZone((int)(Tools::getValue('id_carrier')), (int)($id_zone))))
$this->context->cart->id_carrier = (int)(Tools::getValue('id_carrier')); $this->context->cart->id_carrier = (int)(Tools::getValue('id_carrier'));
elseif (!$this->context->cart->isVirtualCart() AND (int)(Tools::getValue('id_carrier')) == 0) else if (!$this->context->cart->isVirtualCart() && (int)(Tools::getValue('id_carrier')) == 0)
$this->errors[] = Tools::displayError('Invalid carrier or no carrier selected'); $this->errors[] = Tools::displayError('Invalid carrier or no carrier selected');
Module::hookExec('processCarrier', array('cart' => $this->context->cart)); Module::hookExec('processCarrier', array('cart' => $this->context->cart));
@@ -233,7 +237,7 @@ class ParentOrderControllerCore extends FrontController
// override customization tax rate with real tax (tax rules) // override customization tax rate with real tax (tax rules)
if ($customizedDatas) if ($customizedDatas)
{ {
foreach($summary['products'] AS &$productUpdate) foreach ($summary['products'] as &$productUpdate)
{ {
$productId = (int)(isset($productUpdate['id_product']) ? $productUpdate['id_product'] : $productUpdate['product_id']); $productId = (int)(isset($productUpdate['id_product']) ? $productUpdate['id_product'] : $productUpdate['product_id']);
$productAttributeId = (int)(isset($productUpdate['id_product_attribute']) ? $productUpdate['id_product_attribute'] : $productUpdate['product_attribute_id']); $productAttributeId = (int)(isset($productUpdate['id_product_attribute']) ? $productUpdate['id_product_attribute'] : $productUpdate['product_attribute_id']);
@@ -257,7 +261,7 @@ class ParentOrderControllerCore extends FrontController
} }
$this->context->smarty->assign('free_ship', $total_free_ship); $this->context->smarty->assign('free_ship', $total_free_ship);
} }
foreach ($summary['products'] AS $key => &$product) foreach ($summary['products'] as $key => &$product)
{ {
$product['quantity'] = $product['cart_quantity'];// for compatibility with 1.2 themes $product['quantity'] = $product['cart_quantity'];// for compatibility with 1.2 themes
$product['price_without_specific_price'] = Product::getPriceStatic($product['id_product'], !Product::getTaxCalculationMethod(), $product['id_product_attribute'], 2); $product['price_without_specific_price'] = Product::getPriceStatic($product['id_product'], !Product::getTaxCalculationMethod(), $product['id_product_attribute'], 2);
@@ -293,7 +297,7 @@ class ParentOrderControllerCore extends FrontController
protected function _assignAddress() protected function _assignAddress()
{ {
//if guest checkout disabled and flag is_guest in cookies is actived //if guest checkout disabled and flag is_guest in cookies is actived
if(Configuration::get('PS_GUEST_CHECKOUT_ENABLED') == 0 AND ((int)$this->context->customer->is_guest != Configuration::get('PS_GUEST_CHECKOUT_ENABLED'))) if (Configuration::get('PS_GUEST_CHECKOUT_ENABLED') == 0 && ((int)$this->context->customer->is_guest != Configuration::get('PS_GUEST_CHECKOUT_ENABLED')))
{ {
$this->context->customer->logout(); $this->context->customer->logout();
Tools::redirect(''); Tools::redirect('');
@@ -324,25 +328,25 @@ class ParentOrderControllerCore extends FrontController
'formatedAddressFieldsValuesList' => $formatedAddressFieldsValuesList)); 'formatedAddressFieldsValuesList' => $formatedAddressFieldsValuesList));
/* Setting default addresses for cart */ /* Setting default addresses for cart */
if ((!isset($this->context->cart->id_address_delivery) OR empty($this->context->cart->id_address_delivery)) AND sizeof($customerAddresses)) if ((!isset($this->context->cart->id_address_delivery) || empty($this->context->cart->id_address_delivery)) && count($customerAddresses))
{ {
$this->context->cart->id_address_delivery = (int)($customerAddresses[0]['id_address']); $this->context->cart->id_address_delivery = (int)($customerAddresses[0]['id_address']);
$update = 1; $update = 1;
} }
if ((!isset($this->context->cart->id_address_invoice) OR empty($this->context->cart->id_address_invoice)) AND sizeof($customerAddresses)) if ((!isset($this->context->cart->id_address_invoice) || empty($this->context->cart->id_address_invoice)) && count($customerAddresses))
{ {
$this->context->cart->id_address_invoice = (int)($customerAddresses[0]['id_address']); $this->context->cart->id_address_invoice = (int)($customerAddresses[0]['id_address']);
$update = 1; $update = 1;
} }
/* Update cart addresses only if needed */ /* Update cart addresses only if needed */
if (isset($update) AND $update) if (isset($update) && $update)
$this->context->cart->update(); $this->context->cart->update();
/* If delivery address is valid in cart, assign it to Smarty */ /* If delivery address is valid in cart, assign it to Smarty */
if (isset($this->context->cart->id_address_delivery)) if (isset($this->context->cart->id_address_delivery))
{ {
$deliveryAddress = new Address((int)($this->context->cart->id_address_delivery)); $deliveryAddress = new Address((int)($this->context->cart->id_address_delivery));
if (Validate::isLoadedObject($deliveryAddress) AND ($deliveryAddress->id_customer == $customer->id)) if (Validate::isLoadedObject($deliveryAddress) && ($deliveryAddress->id_customer == $customer->id))
$this->context->smarty->assign('delivery', $deliveryAddress); $this->context->smarty->assign('delivery', $deliveryAddress);
} }
@@ -350,7 +354,7 @@ class ParentOrderControllerCore extends FrontController
if (isset($this->context->cart->id_address_invoice)) if (isset($this->context->cart->id_address_invoice))
{ {
$invoiceAddress = new Address((int)($this->context->cart->id_address_invoice)); $invoiceAddress = new Address((int)($this->context->cart->id_address_invoice));
if (Validate::isLoadedObject($invoiceAddress) AND ($invoiceAddress->id_customer == $customer->id)) if (Validate::isLoadedObject($invoiceAddress) && ($invoiceAddress->id_customer == $customer->id))
$this->context->smarty->assign('invoice', $invoiceAddress); $this->context->smarty->assign('invoice', $invoiceAddress);
} }
} }
@@ -365,7 +369,7 @@ class ParentOrderControllerCore extends FrontController
$carriers = Carrier::getCarriersForOrder($id_zone, $this->context->customer->getGroups()); $carriers = Carrier::getCarriersForOrder($id_zone, $this->context->customer->getGroups());
$this->context->smarty->assign(array( $this->context->smarty->assign(array(
'checked' => $this->_setDefaultCarrierSelection($carriers), 'checked' => $this->setDefaultCarrierSelection($carriers),
'carriers' => $carriers, 'carriers' => $carriers,
'default_carrier' => (int)(Configuration::get('PS_CARRIER_DEFAULT')) 'default_carrier' => (int)(Configuration::get('PS_CARRIER_DEFAULT'))
)); ));
@@ -413,9 +417,8 @@ class ParentOrderControllerCore extends FrontController
/** /**
* Set id_carrier to 0 (no shipping price) * Set id_carrier to 0 (no shipping price)
*
*/ */
protected function _setNoCarrier() protected function setNoCarrier()
{ {
$this->context->cart->id_carrier = 0; $this->context->cart->id_carrier = 0;
$this->context->cart->update(); $this->context->cart->update();
@@ -427,17 +430,17 @@ class ParentOrderControllerCore extends FrontController
* @param array $carriers * @param array $carriers
* @return number the id of the default carrier * @return number the id of the default carrier
*/ */
protected function _setDefaultCarrierSelection($carriers) protected function setDefaultCarrierSelection($carriers)
{ {
if (sizeof($carriers)) if (count($carriers))
{ {
$defaultCarrierIsPresent = false; $defaultCarrierIsPresent = false;
if ((int)$this->context->cart->id_carrier != 0) if ((int)$this->context->cart->id_carrier != 0)
foreach ($carriers AS $carrier) foreach ($carriers as $carrier)
if ($carrier['id_carrier'] == (int)$this->context->cart->id_carrier) if ($carrier['id_carrier'] == (int)$this->context->cart->id_carrier)
$defaultCarrierIsPresent = true; $defaultCarrierIsPresent = true;
if (!$defaultCarrierIsPresent) if (!$defaultCarrierIsPresent)
foreach ($carriers AS $carrier) foreach ($carriers as $carrier)
if ($carrier['id_carrier'] == (int)Configuration::get('PS_CARRIER_DEFAULT')) if ($carrier['id_carrier'] == (int)Configuration::get('PS_CARRIER_DEFAULT'))
{ {
$defaultCarrierIsPresent = true; $defaultCarrierIsPresent = true;