<?php

class PositionController
{
    /**
     * @@param array $positions
     */	
    public static function savePositions($positions)
    {
        $id_shop = (int)Tools::getValue('id_shop');
        if (!$id_shop)
            $id_shop = Context::getContext()->shop->id;

        $res = true;
        $position = $positions['current'];
        foreach ($position as $hook_name => $hook_modules)
        {
            // return id of existed or newly added hook
            if (!$id_hook = Hook::registerDesignerHook($hook_name))
                continue;
            // clears positions if all modules were removed from hook
            if (sizeof($hook_modules) < 1)
                PositionController::clearHook($id_hook, $id_shop);

            $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 ($hook_modules as $module)
            {
                $id = explode('_', $module);
                if ((sizeof($id) > 5) && ($id[0] == 'hook')) // hook_7_module_21_moduleName_blocksearch
                {
                    $id_module = $id[3];
                    if (!in_array($id_module, $ids))
                    {
                        $ids[] = (int)$id_module;
                        $value .= '('.(int)$id_module.', '.(int)$id_shop.', '.(int)$id_hook.', '.(int)$i.'),';
                    }
                }
                $i++;
            }
            
            // drop all previous hooked modules
            // if value with new positions is not empty
            // when Designer navigates between pages
            // TODO: modules can be deleted from hook
            if (!empty($value))
            {
                $res &= PositionController::clearHook($id_hook, $id_shop);
                            
                $value = rtrim($value, ',');
                $res &= Db::getInstance()->execute('INSERT INTO  `'._DB_PREFIX_.'hook_module`
                    (id_module, id_shop, id_hook, position)
                    VALUES '.$value);
            }
        }
        
        return $res;
        
        //die('{"hasError" : false, "errors" : ""}');
        // For Test
        //$positions = '{"displayLeftColumn":["hook_7_module_21_moduleName_blocksearch","hook_7_module_7_moduleName_blockcms","hook_7_module_8_moduleName_blockcontact","hook_7_module_122_moduleName_blockrss","hook_7_module_24_moduleName_blockspecials","hook_7_module_14_moduleName_blockmyaccount","hook_7_module_3_moduleName_blockadvertising","hook_7_module_6_moduleName_blockcategories"],"displayHome":[]}';
        //$positions = '{"displayLeftColumn":["hook_7_module_7_moduleName_blockcms","hook_7_module_21_moduleName_blocksearch","hook_7_module_8_moduleName_blockcontact","hook_7_module_122_moduleName_blockrss","hook_7_module_24_moduleName_blockspecials","hook_7_module_14_moduleName_blockmyaccount","hook_7_module_3_moduleName_blockadvertising","hook_7_module_6_moduleName_blockcategories"]}';
        //self::saveHook(json_decode($positions, true));
    }

    private static function clearHook($id_hook, $id_shop)
    {
        $sql = 'DELETE FROM `'._DB_PREFIX_.'hook_module`
            WHERE `id_hook` =  '.(int)$id_hook.'
            AND id_shop = '.(int)$id_shop;
        return Db::getInstance()->execute($sql);
    }
}
