[*] PROJECT : New Module API (part 1)
git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@9790 b9a71923-0436-4b27-9f14-aed3839534dd
This commit is contained in:
+175
-22
@@ -29,6 +29,7 @@ class HookCore extends ObjectModel
|
||||
{
|
||||
/** @var string Name */
|
||||
public $name;
|
||||
public $title;
|
||||
|
||||
protected $fieldsRequired = array('name');
|
||||
protected $fieldsSize = array('name' => 32);
|
||||
@@ -37,12 +38,16 @@ class HookCore extends ObjectModel
|
||||
protected $table = 'hook';
|
||||
protected $identifier = 'id_hook';
|
||||
|
||||
protected static $_hookModulesCache;
|
||||
|
||||
static $preloadModulesFromHooks = null;
|
||||
static $preloadHookAlias = null;
|
||||
|
||||
public function getFields()
|
||||
{
|
||||
$this->validateFields();
|
||||
$fields['name'] = pSQL($this->name);
|
||||
$fields['title'] = pSQL($this->title);
|
||||
return $fields;
|
||||
}
|
||||
|
||||
@@ -95,6 +100,15 @@ class HookCore extends ObjectModel
|
||||
'.($position ? 'WHERE h.`position` = 1' : ''));
|
||||
}
|
||||
|
||||
static public function preloadHookAlias()
|
||||
{
|
||||
if (!is_null(self::$preloadHookAlias))
|
||||
return ;
|
||||
$hookAliasList = Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'hook_alias`');
|
||||
foreach ($hookAliasList as $ha)
|
||||
self::$preloadHookAlias[$ha['alias']] = $ha['name'];
|
||||
}
|
||||
|
||||
static public function preloadModulesFromHooks()
|
||||
{
|
||||
if (!is_null(self::$preloadModulesFromHooks))
|
||||
@@ -138,23 +152,144 @@ class HookCore extends ObjectModel
|
||||
return $list;
|
||||
}
|
||||
|
||||
static public function newOrder($cart, $order, $customer, $currency, $orderStatus)
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Execute modules for specified hook
|
||||
*
|
||||
* @param string $hook_name Hook Name
|
||||
* @param array $hookArgs Parameters for the functions
|
||||
* @return string modules output
|
||||
*/
|
||||
public static function exec($hook_name, $hookArgs = array(), $id_module = NULL)
|
||||
{
|
||||
return Module::hookExec('newOrder', array(
|
||||
'cart' => $cart,
|
||||
'order' => $order,
|
||||
'customer' => $customer,
|
||||
'currency' => $currency,
|
||||
'orderStatus' => $orderStatus));
|
||||
$context = Context::getContext();
|
||||
if ((!empty($id_module) && !Validate::isUnsignedId($id_module)) || !Validate::isHookName($hook_name))
|
||||
die(Tools::displayError());
|
||||
|
||||
self::preloadHookAlias();
|
||||
$hook_name_retro = strtolower($hook_name);
|
||||
if (isset(self::$preloadHookAlias[$hook_name]))
|
||||
$hook_name = self::$preloadHookAlias[$hook_name];
|
||||
|
||||
$live_edit = false;
|
||||
if (!isset($hookArgs['cookie']) || !$hookArgs['cookie'])
|
||||
$hookArgs['cookie'] = $context->cookie;
|
||||
if (!isset($hookArgs['cart']) || !$hookArgs['cart'])
|
||||
$hookArgs['cart'] = $context->cart;
|
||||
$hook_name = strtolower($hook_name);
|
||||
|
||||
if (!isset(self::$_hookModulesCache))
|
||||
{
|
||||
$db = Db::getInstance(_PS_USE_SQL_SLAVE_);
|
||||
$list = $context->shop->getListOfID();
|
||||
if (isset($context->customer) && $context->customer->isLogged())
|
||||
$groups = $context->customer->getGroups();
|
||||
|
||||
$sql = 'SELECT h.`name` as hook, m.`id_module`, h.`id_hook`, m.`name` as module, h.`live_edit`
|
||||
FROM `'._DB_PREFIX_.'module` m
|
||||
LEFT JOIN `'._DB_PREFIX_.'hook_module` hm
|
||||
ON hm.`id_module` = m.`id_module`';
|
||||
if (isset($context->customer) && $context->customer->isLogged())
|
||||
$sql .= '
|
||||
LEFT JOIN `'._DB_PREFIX_.'group_module_restriction` gmr
|
||||
ON gmr.`id_module` = m.`id_module`';
|
||||
$sql .= '
|
||||
LEFT JOIN `'._DB_PREFIX_.'hook` h
|
||||
ON hm.`id_hook` = h.`id_hook`
|
||||
WHERE (SELECT COUNT(*) FROM '._DB_PREFIX_.'module_shop ms WHERE ms.id_module = m.id_module AND ms.id_shop IN('.implode(', ', $list).')) = '.count($list).'
|
||||
AND hm.id_shop IN('.implode(', ', $list).')';
|
||||
if (isset($context->customer) && $context->customer->isLogged())
|
||||
$sql .= '
|
||||
AND (gmr.`authorized` = 1 AND gmr.`id_group` IN('.implode(', ', $groups).'))';
|
||||
$sql .= '
|
||||
GROUP BY hm.id_hook, hm.id_module
|
||||
ORDER BY hm.`position`';
|
||||
$result = $db->executeS($sql, false);
|
||||
self::$_hookModulesCache = array();
|
||||
|
||||
if ($result)
|
||||
while ($row = $db->nextRow())
|
||||
{
|
||||
$row['hook'] = strtolower($row['hook']);
|
||||
if (!isset(self::$_hookModulesCache[$row['hook']]))
|
||||
self::$_hookModulesCache[$row['hook']] = array();
|
||||
self::$_hookModulesCache[$row['hook']][] = array('id_hook' => $row['id_hook'], 'module' => $row['module'], 'id_module' => $row['id_module'], 'live_edit' => $row['live_edit']);
|
||||
}
|
||||
}
|
||||
|
||||
if (!isset(self::$_hookModulesCache[$hook_name]) && !isset(self::$_hookModulesCache[$hook_name_retro]))
|
||||
return;
|
||||
|
||||
$hookModulesCache = array();
|
||||
if (isset(self::$_hookModulesCache[$hook_name]))
|
||||
$hookModulesCache = self::$_hookModulesCache[$hook_name];
|
||||
if (isset(self::$_hookModulesCache[$hook_name_retro]))
|
||||
$hookModulesCache = self::$_hookModulesCache[$hook_name_retro];
|
||||
|
||||
$altern = 0;
|
||||
$output = '';
|
||||
foreach ($hookModulesCache as $array)
|
||||
{
|
||||
if ($id_module && $id_module != $array['id_module'])
|
||||
continue;
|
||||
if (!($moduleInstance = Module::getInstanceByName($array['module'])))
|
||||
continue;
|
||||
|
||||
$exceptions = $moduleInstance->getExceptions($array['id_hook']);
|
||||
if (in_array(Dispatcher::getInstance()->getController(), $exceptions))
|
||||
continue;
|
||||
if (isset($context->employee) && !$moduleInstance->getPermission('view', $context->employee))
|
||||
continue;
|
||||
|
||||
$hook_callable = is_callable(array($moduleInstance, 'hook'.$hook_name));
|
||||
$hook_retro_callable = is_callable(array($moduleInstance, 'hook'.$hook_name_retro));
|
||||
if (($hook_callable || $hook_retro_callable) && Module::preCall($moduleInstance->name))
|
||||
{
|
||||
$hookArgs['altern'] = ++$altern;
|
||||
|
||||
if ($hook_callable)
|
||||
$display = call_user_func(array($moduleInstance, 'hook'.$hook_name), $hookArgs);
|
||||
else if ($hook_retro_callable)
|
||||
$display = call_user_func(array($moduleInstance, 'hook'.$hook_name_retro), $hookArgs);
|
||||
if ($array['live_edit'] && ((Tools::isSubmit('live_edit') && Tools::getValue('ad') && (Tools::getValue('liveToken') == sha1(Tools::getValue('ad')._COOKIE_KEY_)))))
|
||||
{
|
||||
$live_edit = true;
|
||||
$output .= '<script type="text/javascript"> modules_list.push(\''.$moduleInstance->name.'\');</script>
|
||||
<div id="hook_'.$array['id_hook'].'_module_'.$moduleInstance->id.'_moduleName_'.$moduleInstance->name.'"
|
||||
class="dndModule" style="border: 1px dotted red;'.(!strlen($display) ? 'height:50px;' : '').'">
|
||||
<span><img src="'.$moduleInstance->_path.'/logo.gif">'
|
||||
.$moduleInstance->displayName.'<span style="float:right">
|
||||
<a href="#" id="'.$array['id_hook'].'_'.$moduleInstance->id.'" class="moveModule">
|
||||
<img src="'._PS_ADMIN_IMG_.'arrow_out.png"></a>
|
||||
<a href="#" id="'.$array['id_hook'].'_'.$moduleInstance->id.'" class="unregisterHook">
|
||||
<img src="'._PS_ADMIN_IMG_.'delete.gif"></span></a>
|
||||
</span>'.$display.'</div>';
|
||||
}
|
||||
else
|
||||
$output .= $display;
|
||||
}
|
||||
}
|
||||
return ($live_edit ? '<script type="text/javascript">hooks_list.push(\''.$hook_name.'\'); </script><!--<div id="add_'.$hook_name.'" class="add_module_live_edit">
|
||||
<a class="exclusive" href="#">Add a module</a></div>--><div id="'.$hook_name.'" class="dndHook" style="min-height:50px">' : '').$output.($live_edit ? '</div>' : '');
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
static public function updateOrderStatus($newOrderStatusId, $id_order)
|
||||
{
|
||||
$order = new Order((int)($id_order));
|
||||
$newOS = new OrderState((int)($newOrderStatusId), $order->id_lang);
|
||||
|
||||
$return = ((int)($newOS->id) == Configuration::get('PS_OS_PAYMENT')) ? Module::hookExec('paymentConfirm', array('id_order' => (int)($order->id))) : true;
|
||||
$return = Module::hookExec('updateOrderStatus', array('newOrderStatus' => $newOS, 'id_order' => (int)($order->id))) AND $return;
|
||||
$return = ((int)($newOS->id) == Configuration::get('PS_OS_PAYMENT')) ? Hook::exec('paymentConfirm', array('id_order' => (int)($order->id))) : true;
|
||||
$return = Hook::exec('updateOrderStatus', array('newOrderStatus' => $newOS, 'id_order' => (int)($order->id))) AND $return;
|
||||
return $return;
|
||||
}
|
||||
|
||||
@@ -162,7 +297,7 @@ class HookCore extends ObjectModel
|
||||
{
|
||||
$order = new Order((int)($id_order));
|
||||
$newOS = new OrderState((int)($newOrderStatusId), $order->id_lang);
|
||||
$return = Module::hookExec('postUpdateOrderStatus', array('newOrderStatus' => $newOS, 'id_order' => (int)($order->id)));
|
||||
$return = Hook::exec('postUpdateOrderStatus', array('newOrderStatus' => $newOS, 'id_order' => (int)($order->id)));
|
||||
return $return;
|
||||
}
|
||||
|
||||
@@ -172,39 +307,57 @@ class HookCore extends ObjectModel
|
||||
* @param Product
|
||||
* @param Order
|
||||
*/
|
||||
static public function newOrder($cart, $order, $customer, $currency, $orderStatus)
|
||||
{
|
||||
Tools::displayAsDeprecated();
|
||||
return Hook::exec('newOrder', array(
|
||||
'cart' => $cart,
|
||||
'order' => $order,
|
||||
'customer' => $customer,
|
||||
'currency' => $currency,
|
||||
'orderStatus' => $orderStatus));
|
||||
}
|
||||
|
||||
static public function updateQuantity($product, $order = null)
|
||||
{
|
||||
return Module::hookExec('updateQuantity', array('product' => $product, 'order' => $order));
|
||||
Tools::displayAsDeprecated();
|
||||
return Hook::exec('updateQuantity', array('product' => $product, 'order' => $order));
|
||||
}
|
||||
|
||||
static public function productFooter($product, $category)
|
||||
{
|
||||
return Module::hookExec('productFooter', array('product' => $product, 'category' => $category));
|
||||
Tools::displayAsDeprecated();
|
||||
return Hook::exec('productFooter', array('product' => $product, 'category' => $category));
|
||||
}
|
||||
|
||||
static public function productOutOfStock($product)
|
||||
{
|
||||
return Module::hookExec('productOutOfStock', array('product' => $product));
|
||||
Tools::displayAsDeprecated();
|
||||
return Hook::exec('productOutOfStock', array('product' => $product));
|
||||
}
|
||||
|
||||
static public function addProduct($product)
|
||||
{
|
||||
return Module::hookExec('addProduct', array('product' => $product));
|
||||
Tools::displayAsDeprecated();
|
||||
return Hook::exec('addProduct', array('product' => $product));
|
||||
}
|
||||
|
||||
static public function updateProduct($product)
|
||||
{
|
||||
return Module::hookExec('updateProduct', array('product' => $product));
|
||||
Tools::displayAsDeprecated();
|
||||
return Hook::exec('updateProduct', array('product' => $product));
|
||||
}
|
||||
|
||||
static public function deleteProduct($product)
|
||||
{
|
||||
return Module::hookExec('deleteProduct', array('product' => $product));
|
||||
Tools::displayAsDeprecated();
|
||||
return Hook::exec('deleteProduct', array('product' => $product));
|
||||
}
|
||||
|
||||
static public function updateProductAttribute($id_product_attribute)
|
||||
{
|
||||
return Module::hookExec('updateProductAttribute', array('id_product_attribute' => $id_product_attribute));
|
||||
Tools::displayAsDeprecated();
|
||||
return Hook::exec('updateProductAttribute', array('id_product_attribute' => $id_product_attribute));
|
||||
}
|
||||
|
||||
static public function orderConfirmation($id_order)
|
||||
@@ -222,7 +375,7 @@ class HookCore extends ObjectModel
|
||||
$params['objOrder'] = $order;
|
||||
$params['currencyObj'] = $currency;
|
||||
|
||||
return Module::hookExec('orderConfirmation', $params);
|
||||
return Hook::exec('orderConfirmation', $params);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
@@ -243,7 +396,7 @@ class HookCore extends ObjectModel
|
||||
$params['objOrder'] = $order;
|
||||
$params['currencyObj'] = $currency;
|
||||
|
||||
return Module::hookExec('paymentReturn', $params, (int)($id_module));
|
||||
return Hook::exec('paymentReturn', $params, (int)($id_module));
|
||||
}
|
||||
}
|
||||
return false;
|
||||
@@ -253,7 +406,7 @@ class HookCore extends ObjectModel
|
||||
{
|
||||
if (!is_object($pdf) OR !Validate::isUnsignedId($id_order))
|
||||
return false;
|
||||
return Module::hookExec('PDFInvoice', array('pdf' => $pdf, 'id_order' => $id_order));
|
||||
return Hook::exec('PDFInvoice', array('pdf' => $pdf, 'id_order' => $id_order));
|
||||
}
|
||||
|
||||
static public function backBeforePayment($module)
|
||||
@@ -261,14 +414,14 @@ class HookCore extends ObjectModel
|
||||
$params['module'] = strval($module);
|
||||
if (!$params['module'])
|
||||
return false;
|
||||
return Module::hookExec('backBeforePayment', $params);
|
||||
return Hook::exec('backBeforePayment', $params);
|
||||
}
|
||||
|
||||
static public function updateCarrier($id_carrier, $carrier)
|
||||
{
|
||||
if (!Validate::isUnsignedId($id_carrier) OR !is_object($carrier))
|
||||
return false;
|
||||
return Module::hookExec('updateCarrier', array('id_carrier' => $id_carrier, 'carrier' => $carrier));
|
||||
return Hook::exec('updateCarrier', array('id_carrier' => $id_carrier, 'carrier' => $carrier));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user