Jump to content

[PS 1.7.7.x] UpdateCategoryPositionHandler


ps8modules

Recommended Posts

My code (NOT WORK):

class CustomCategoryListController extends ModuleAdminController
{
    public $selected_categories = array();

    protected $position_identifier = 'id_category';
	
	public function __construct()
    {
        parent::__construct();
        
        $this->context = Context::getContext();
		$this->bootstrap = true;
		$this->id_lang = Configuration::get('PS_LANG_DEFAULT');
		$this->secure_key = Tools::getValue('secure_key');

        $this->table = 'category_lang';

        $this->id_parent_category =  Tools::getValue('category_parent', '2');   

        $this->explicitSelect = false;
        $this->allow_export = false;
        $this->deleted = false;
        $this->lang = false;
        $this->identifier = 'id_category';
        $this->_default_pagination = 100;
        $this->bulk_actions = array();

        $this->_defaultOrderBy = 'b.position';
        $this->_defaultOrderWay = 'ASC';
        $this->_select = " CONCAT(a.id_category,'|', a.name) as c_name, b.*";
        $this->_join = " LEFT JOIN "._DB_PREFIX_."category b ON (a.id_category = b.id_category)";
        $this->_where = " AND b.id_parent = ".$this->id_parent_category. " AND a.id_lang = ".$this->id_lang;
        $this->_group = ' GROUP BY a.id_category';

        print($this->id_parent_category);
    
        $this->addRowAction('Edit');
    
        $this->fields_list = [
            'id_category' => ['title' => $this->l('[ID]'), 'remove_onclick' => true, 'class' => 'fixed-width-xs no-cursor', 'havingFilter' => true, 'filter_key' => 'a!id_category', 'callback' => 'renderClickIdCategory',],
            'c_name' => ['title' => $this->l('Name'), 'remove_onclick' => true, 'class' => 'no-cursor', 'havingFilter' => true, 'filter_key' => 'a!name', 'callback' => 'renderClickCategoryName',],
            'description' => ['title' => $this->l('Title'), 'remove_onclick' => true, 'class' => 'no-cursor', 'havingFilter' => true, 'filter_key' => 'a!popis', 'callback' => 'renderTitle',],
            'position' => ['title' => $this->l('Position'), 'position' => 'position', 'class' => 'fixed-width-xs no-cursor center', 'orderby' => false, 'ajax' => true, 'search' => false,],
            'active' => ['title' => $this->l('Active'), 'type' => 'bool', 'remove_onclick' => true, 'class' => 'no-cursor center', 'filter_key' => 'b!active', 'callback' => 'renderActive',],
        ];

	}

    public function ajaxProcessUpdatePositions()
    {
        $way = (int)Tools::getValue('way');
        $positions = Tools::getValue('category');
        $parentCategoryId = $this->id_parent_category;

        $test = array();

        foreach ($positions as $key => $value) {
            $pos = explode('_', $value);

            $test[] = array('new_position' => $key, 'id_parent_category' => $parentCategoryId, 'id_category' => $pos[2]);
            
        }

        $this->updatePositions($test);

    }

    public function updatePositions($res)
    {
        foreach ($res as $pos){
            Db::getInstance()->execute('UPDATE '._DB_PREFIX_.'category SET position = '.$pos['new_position'].' WHERE id_category = '.$pos['id_category'].' AND id_parent = '.$pos['id_parent_category']);
            Db::getInstance()->execute('UPDATE '._DB_PREFIX_.'category_shop SET position = '.$pos['new_position'].' WHERE id_category = '.$pos['id_category']);
        }  
    }
...
...
...
}

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...