// ajax.php clean and bug fixes

This commit is contained in:
rGaillard
2012-06-15 13:22:07 +00:00
parent 4c555b4e6d
commit 54675df666
20 changed files with 383 additions and 328 deletions
+75 -42
View File
@@ -157,57 +157,90 @@ class AdminCmsContentControllerCore extends AdminController
public function ajaxProcessUpdateCmsPositions()
{
$id_cms = (int)Tools::getValue('id_cms');
$id_category = (int)Tools::getValue('id_cms_category');
$way = (int)Tools::getValue('way');
$positions = Tools::getValue('cms');
if (is_array($positions))
foreach ($positions as $key => $value)
{
$pos = explode('_', $value);
if ((isset($pos[1]) && isset($pos[2])) && ($pos[1] == $id_category && $pos[2] == $id_cms))
{
$position = $key;
break;
}
}
$cms = new CMS($id_cms);
if (Validate::isLoadedObject($cms))
if ($this->tabAccess['edit'] === '1')
{
if (isset($position) && $cms->updatePosition($way, $position))
die(true);
$id_cms = (int)Tools::getValue('id_cms');
$id_category = (int)Tools::getValue('id_cms_category');
$way = (int)Tools::getValue('way');
$positions = Tools::getValue('cms');
if (is_array($positions))
foreach ($positions as $key => $value)
{
$pos = explode('_', $value);
if ((isset($pos[1]) && isset($pos[2])) && ($pos[1] == $id_category && $pos[2] == $id_cms))
{
$position = $key;
break;
}
}
$cms = new CMS($id_cms);
if (Validate::isLoadedObject($cms))
{
if (isset($position) && $cms->updatePosition($way, $position))
die(true);
else
die('{"hasError" : true, "errors" : "Can not update cms position"}');
}
else
die('{"hasError" : true, "errors" : "Can not update cms position"}');
die('{"hasError" : true, "errors" : "This cms can not be loaded"}');
}
else
die('{"hasError" : true, "errors" : "This cms can not be loaded"}');
}
public function ajaxProcessUpdateCmsCategoriesPositions()
{
$id_cms_category_to_move = (int)Tools::getValue('id_cms_category_to_move');
$id_cms_category_parent = (int)Tools::getValue('id_cms_category_parent');
$way = (int)Tools::getValue('way');
$positions = Tools::getValue('cms_category');
if (is_array($positions))
foreach ($positions as $key => $value)
{
$pos = explode('_', $value);
if ((isset($pos[1]) && isset($pos[2])) && ($pos[1] == $id_cms_category_parent && $pos[2] == $id_cms_category_to_move))
{
$position = $key;
break;
}
}
$cms_category = new CMSCategory($id_cms_category_to_move);
if (Validate::isLoadedObject($cms_category))
if ($this->tabAccess['edit'] === '1')
{
if (isset($position) && $cms_category->updatePosition($way, $position))
die(true);
$id_cms_category_to_move = (int)Tools::getValue('id_cms_category_to_move');
$id_cms_category_parent = (int)Tools::getValue('id_cms_category_parent');
$way = (int)Tools::getValue('way');
$positions = Tools::getValue('cms_category');
if (is_array($positions))
foreach ($positions as $key => $value)
{
$pos = explode('_', $value);
if ((isset($pos[1]) && isset($pos[2])) && ($pos[1] == $id_cms_category_parent && $pos[2] == $id_cms_category_to_move))
{
$position = $key;
break;
}
}
$cms_category = new CMSCategory($id_cms_category_to_move);
if (Validate::isLoadedObject($cms_category))
{
if (isset($position) && $cms_category->updatePosition($way, $position))
die(true);
else
die('{"hasError" : true, "errors" : "Can not update cms categories position"}');
}
else
die('{"hasError" : true, "errors" : "Can not update cms categories position"}');
die('{"hasError" : true, "errors" : "This cms category can not be loaded"}');
}
else
die('{"hasError" : true, "errors" : "This cms category can not be loaded"}');
}
public function ajaxProcessPublishCMS()
{
if ($this->tabAccess['edit'] === '1')
{
if ($id_cms = (int)Tools::getValue('id_cms'))
{
$bo_cms_url = dirname($_SERVER['PHP_SELF']).'/index.php?tab=AdminCmsContent&id_cms='.(int)$id_cms.'&updatecms&token='.$this->token;
if (Tools::getValue('redirect'))
die($bo_cms_url);
$cms = new CMS((int)(Tools::getValue('id_cms')));
if (!Validate::isLoadedObject($cms))
die('error: invalid id');
$cms->active = 1;
if ($cms->save())
die($bo_cms_url);
else
die('error: saving');
}
else
die ('error: parameters');
}
}
}
+2 -4
View File
@@ -234,7 +234,7 @@ class AdminCmsControllerCore extends AdminController
{
$admin_dir = dirname($_SERVER['PHP_SELF']);
$admin_dir = substr($admin_dir, strrpos($admin_dir, '/') + 1);
$redir .= '?adtoken='.Tools::encrypt('PreviewCMS'.$cms->id).'&ad='.$admin_dir;
$redir .= '?adtoken='.Tools::getAdminTokenLite('AdminCmsContent').'&ad='.$admin_dir.'&id_employee='.(int)$this->context->employee->id;
}
Tools::redirectAdmin($redir);
}
@@ -312,9 +312,7 @@ class AdminCmsControllerCore extends AdminController
{
$admin_dir = dirname($_SERVER['PHP_SELF']);
$admin_dir = substr($admin_dir, strrpos($admin_dir, '/') + 1);
$token = Tools::encrypt('PreviewCMS'.$cms->id);
$preview_url .= $cms->active ? '' : '&adtoken='.$token.'&ad='.$admin_dir;
$preview_url .= $cms->active ? '' : '&adtoken='.Tools::getAdminTokenLite('AdminCmsContent').'&ad='.$admin_dir.'&id_employee='.(int)$this->context->employee->id;
}
Tools::redirectAdmin($preview_url);
}
@@ -882,6 +882,28 @@ class AdminCustomersControllerCore extends AdminController
$this->content = Tools::jsonEncode($to_return);
}
/**
* Uodate the customer note
*
* @return void
*/
public function ajaxProcessUpdateCustomerNote()
{
if ($this->tabAccess['edit'] === '1')
{
$note = Tools::htmlentitiesDecodeUTF8(Tools::getValue('note'));
$customer = new Customer((int)Tools::getValue('id_customer'));
if (!Validate::isLoadedObject($customer))
die ('error:update');
if (!empty($note) && !Validate::isCleanHtml($note))
die ('error:validation');
$customer->note = $note;
if (!$customer->update())
die ('error:update');
die('ok');
}
}
}
@@ -270,7 +270,12 @@ class AdminModulesPositionsControllerCore extends AdminController
'desc' => $this->l('Transplant a module')
);
$live_edit_params = array('live_edit' => true, 'ad' => $admin_dir, 'liveToken' => sha1($admin_dir._COOKIE_KEY_));
$live_edit_params = array(
'live_edit' => true,
'ad' => $admin_dir,
'liveToken' => $this->token,
'id_employee' => (int)$this->context->employee->id
);
$this->context->smarty->assign(array(
'show_toolbar' => true,
@@ -398,18 +403,143 @@ class AdminModulesPositionsControllerCore extends AdminController
public function ajaxProcessUpdatePositions()
{
$id_module = (int)(Tools::getValue('id_module'));
$id_hook = (int)(Tools::getValue('id_hook'));
$way = (int)(Tools::getValue('way'));
$positions = Tools::getValue(strval($id_hook));
$position = (is_array($positions)) ? array_search($id_hook.'_'.$id_module, $positions) : null;
$module = Module::getInstanceById($id_module);
if (Validate::isLoadedObject($module))
if ($module->updatePosition($id_hook, $way, $position))
die(true);
if ($this->tabAccess['edit'] === '1')
{
$id_module = (int)(Tools::getValue('id_module'));
$id_hook = (int)(Tools::getValue('id_hook'));
$way = (int)(Tools::getValue('way'));
$positions = Tools::getValue(strval($id_hook));
$position = (is_array($positions)) ? array_search($id_hook.'_'.$id_module, $positions) : null;
$module = Module::getInstanceById($id_module);
if (Validate::isLoadedObject($module))
if ($module->updatePosition($id_hook, $way, $position))
die(true);
else
die('{"hasError" : true, "errors" : "Can not update module position"}');
else
die('{"hasError" : true, "errors" : "Can not update module position"}');
else
die('{"hasError" : true, "errors" : "This module can not be loaded"}');
die('{"hasError" : true, "errors" : "This module can not be loaded"}');
}
}
public function ajaxProcessGetHookableList()
{
if ($this->tabAccess['view'] === '1')
{
/* PrestaShop demo mode */
if (_PS_MODE_DEMO_)
die('{"hasError" : true, "errors" : ["Live Edit : This functionnality has been disabled"]}');
if (!count(Tools::getValue('hooks_list')))
die('{"hasError" : true, "errors" : ["Live Edit : no module on this page"]}');
$modules_list = Tools::getValue('modules_list');
$hooks_list = Tools::getValue('hooks_list');
$hookableList = array();
foreach ($modules_list as $module)
{
$module = trim($module);
if (!$module)
continue;
if (!Validate::isModuleName($module))
die('{"hasError" : true, "errors" : ["Live Edit : module is invalid"]}');
$moduleInstance = Module::getInstanceByName($module);
foreach ($hooks_list as $hook_name)
{
$hook_name = trim($hook_name);
if (!$hook_name)
continue;
if (!array_key_exists($hook_name, $hookableList))
$hookableList[$hook_name] = array();
if ($moduleInstance->isHookableOn($hook_name))
array_push($hookableList[$hook_name], str_replace('_', '-', $module));
}
}
$hookableList['hasError'] = false;
die(Tools::jsonEncode($hookableList));
}
}
public function ajaxProcessGetHookableModuleList()
{
if ($this->tabAccess['view'] === '1')
{
/* PrestaShop demo mode */
if (_PS_MODE_DEMO_)
die('{"hasError" : true, "errors" : ["Live Edit : This functionnality has been disabled"]}');
/* PrestaShop demo mode*/
$hook_name = Tools::getValue('hook');
$hookableModulesList = array();
$modules = Db::getInstance()->executeS('SELECT id_module, name FROM `'._DB_PREFIX_.'module` ');
foreach ($modules as $module)
{
if (!Validate::isModuleName($module['name']))
continue;
if (file_exists(_PS_MODULE_DIR_.$module['name'].'/'.$module['name'].'.php'))
{
include_once(_PS_MODULE_DIR_.$module['name'].'/'.$module['name'].'.php');
$mod = new $module['name']();
if ($mod->isHookableOn($hook_name))
$hookableModulesList[] = array('id' => (int)$mod->id, 'name' => $mod->displayName, 'display' => Hook::exec($hook_name, array(), (int)$mod->id));
}
}
die(Tools::jsonEncode($hookableModulesList));
}
}
public function ajaxProcessSaveHook()
{
if ($this->tabAccess['edit'] === '1')
{
/* PrestaShop demo mode */
if (_PS_MODE_DEMO_)
die('{"hasError" : true, "errors" : ["Live Edit : This functionnality has been disabled"]}');
$hooks_list = explode(',', Tools::getValue('hooks_list'));
$id_shop = (int)Tools::getValue('id_shop');
if (!$id_shop)
$id_shop = Context::getContext()->shop->id;
$res = true;
$hookableList = array();
// $_POST['hook'] is an array of id_module
$hooks_list = Tools::getValue('hook');
foreach ($hooks_list as $id_hook => $modules)
{
// 1st, drop all previous hooked modules
$sql = 'DELETE FROM `'._DB_PREFIX_.'hook_module`
WHERE `id_hook` = '.(int)$id_hook.'
AND id_shop = '.(int)$id_shop;
$res &= Db::getInstance()->execute($sql);
$i = 1;
$value = '';
$ids = array();
// then prepare sql query to rehook all chosen modules(id_module, id_shop, id_hook, position)
// position is i (autoincremented)
foreach ($modules as $id_module)
{
if (!in_array($id_module, $ids))
{
$ids[] = (int)$id_module;
$value .= '('.(int)$id_module.', '.(int)$id_shop.', '.(int)$id_hook.', '.(int)$i.'),';
}
$i++;
}
$value = rtrim($value, ',');
$res &= Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'hook_module`
(id_module, id_shop, id_hook, position)
VALUES '.$value);
}
if ($res)
$hasError = true;
else
$hasError = false;
die('{"hasError" : false, "errors" : ""}');
}
}
}
+68 -39
View File
@@ -1504,15 +1504,14 @@ class AdminProductsControllerCore extends AdminController
null,
Context::getContext()->shop->id,
0,
true
(bool)Configuration::get('PS_REWRITING_SETTINGS')
);
if (!$this->object->active)
{
$admin_dir = dirname($_SERVER['PHP_SELF']);
$admin_dir = substr($admin_dir, strrpos($admin_dir, '/') + 1);
$token = Tools::encrypt('PreviewProduct'.$this->object->id);
$preview_url .= '&adtoken='.$token.'&ad='.$admin_dir;
$preview_url .= '&adtoken='.$this->token.'&ad='.$admin_dir.'&id_employee='.(int)$this->context->employee->id;
}
$this->redirect_after = $preview_url;
@@ -1625,19 +1624,18 @@ class AdminProductsControllerCore extends AdminController
null,
Context::getContext()->shop->id,
0,
true
(bool)Configuration::get('PS_REWRITING_SETTINGS')
);
if (!$object->active)
{
$admin_dir = dirname($_SERVER['PHP_SELF']);
$admin_dir = substr($admin_dir, strrpos($admin_dir, '/') + 1);
$token = Tools::encrypt('PreviewProduct'.$object->id);
if (strpos($preview_url, '?') === false)
$preview_url .= '?';
else
$preview_url .= '&';
$preview_url .= 'adtoken='.$token.'&ad='.$admin_dir;
$preview_url .= 'adtoken='.$this->token.'&ad='.$admin_dir.'&id_employee='.(int)$this->context->employee->id;
}
$this->redirect_after = $preview_url;
}
@@ -2331,16 +2329,15 @@ class AdminProductsControllerCore extends AdminController
null,
Context::getContext()->shop->id,
0,
true
$is_rewrite_active
);
if (!$product->active)
{
$admin_dir = dirname($_SERVER['PHP_SELF']);
$admin_dir = substr($admin_dir, strrpos($admin_dir, '/') + 1);
$token = Tools::encrypt('PreviewProduct'.$product->id);
$preview_url .= $product->active ? '' : '&adtoken='.$token.'&ad='.$admin_dir;
$preview_url .= $product->active ? '' : '&adtoken='.$this->token.'&ad='.$admin_dir.'&id_employee='.(int)$this->context->employee->id;
}
}
return $preview_url;
@@ -3976,44 +3973,76 @@ class AdminProductsControllerCore extends AdminController
public function ajaxProcessCheckProductName()
{
$search = Tools::getValue('q');
$id_lang = Tools::getValue('id_lang');
$limit = Tools::getValue('limit');
$result = Db::getInstance()->executeS('
SELECT DISTINCT pl.`name`, p.`id_product`, pl.`id_shop`
FROM `'._DB_PREFIX_.'product` p
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
ON (pl.`id_product` = p.`id_product` AND pl.`id_lang` = '.(int)$id_lang.')
WHERE pl.`name` LIKE "%'.pSQL($search).'%"
GROUP BY pl.`id_product`
LIMIT '.(int)$limit);
die(Tools::jsonEncode($result));
if ($this->tabAccess['view'] === '1')
{
$search = Tools::getValue('q');
$id_lang = Tools::getValue('id_lang');
$limit = Tools::getValue('limit');
$result = Db::getInstance()->executeS('
SELECT DISTINCT pl.`name`, p.`id_product`, pl.`id_shop`
FROM `'._DB_PREFIX_.'product` p
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
ON (pl.`id_product` = p.`id_product` AND pl.`id_lang` = '.(int)$id_lang.')
WHERE pl.`name` LIKE "%'.pSQL($search).'%"
GROUP BY pl.`id_product`
LIMIT '.(int)$limit);
die(Tools::jsonEncode($result));
}
}
public function ajaxProcessUpdatePositions()
{
$way = (int)(Tools::getValue('way'));
$id_product = (int)(Tools::getValue('id_product'));
$id_category = (int)(Tools::getValue('id_category'));
$positions = Tools::getValue('product');
if ($this->tabAccess['edit'] === '1')
{
$way = (int)(Tools::getValue('way'));
$id_product = (int)(Tools::getValue('id_product'));
$id_category = (int)(Tools::getValue('id_category'));
$positions = Tools::getValue('product');
if (is_array($positions))
foreach ($positions as $position => $value)
{
$pos = explode('_', $value);
if ((isset($pos[1]) && isset($pos[2])) && ($pos[1] == $id_category && (int)$pos[2] === $id_product))
if (is_array($positions))
foreach ($positions as $position => $value)
{
if ($product = new Product((int)$pos[2]))
if (isset($position) && $product->updatePosition($way, $position))
echo 'ok position '.(int)$position.' for product '.(int)$pos[2].'\r\n';
else
echo '{"hasError" : true, "errors" : "Can not update product '.(int)$id_product.' to position '.(int)$position.' "}';
else
echo '{"hasError" : true, "errors" : "This product ('.(int)$id_product.') can t be loaded"}';
$pos = explode('_', $value);
break;
if ((isset($pos[1]) && isset($pos[2])) && ($pos[1] == $id_category && (int)$pos[2] === $id_product))
{
if ($product = new Product((int)$pos[2]))
if (isset($position) && $product->updatePosition($way, $position))
echo 'ok position '.(int)$position.' for product '.(int)$pos[2].'\r\n';
else
echo '{"hasError" : true, "errors" : "Can not update product '.(int)$id_product.' to position '.(int)$position.' "}';
else
echo '{"hasError" : true, "errors" : "This product ('.(int)$id_product.') can t be loaded"}';
break;
}
}
}
}
public function ajaxProcessPublishProduct()
{
if ($this->tabAccess['edit'] === '1')
{
if ($id_product = (int)Tools::getValue('id_product'))
{
$id_tab_catalog = (int)(Tab::getIdFromClassName('AdminProducts'));
$bo_product_url = dirname($_SERVER['PHP_SELF']).'/index.php?tab=AdminProducts&id_product='.$id_product.'&updateproduct&token='.$this->token;
if (Tools::getValue('redirect'))
die($bo_product_url);
$product = new Product((int)$id_product);
if (!Validate::isLoadedObject($product))
die('error: invalid id');
$product->active = 1;
if ($product->save())
die($bo_product_url);
else
die('error: saving');
}
}
}
}