basvandertogt Posted November 26, 2014 Share Posted November 26, 2014 I'm building a simple ModuleAdminController to manage my faq items. However when i change the position of a item, the change is not saved to the database. I don't get any error, it says: "Update successful". What am i doing wrong? <?php if (defined('__PS_VERSION_')) { exit('Restricted Access!!!'); } class AdminBlockfaqController extends ModuleAdminController { public function __construct() { $this->table = 'faq'; $this->list_simple_header = FALSE; $this->className = 'FaqModel'; $this->position_identifier = 'id_faq'; $this->lang = TRUE; $this->bootstrap = TRUE; $this->addRowAction('edit'); $this->addRowAction('delete'); $this->bulk_actions = array( 'delete' => array( 'text' => $this->l('Delete selected'), 'confirm' => $this->l('Delete selected items?') ) ); $this->fields_list = array( 'id_faq' => array( 'title' => $this->l('ID'), 'align' => 'center', 'width' => 25 ), 'question' => array( 'title' => $this->l('Question'), 'width' => 'auto' ), 'answer' => array( 'title' => $this->l('Answer'), 'width' => 'auto' ), 'id_category' => array( 'title' => $this->l('Category'), 'width' => 'auto' ), 'active' => array( 'title' => $this->l('Status'), 'width' => 'auto', 'active' => 'status' ), 'position' => array( 'title' => $this->l('Position'), 'width' => '70', 'align' => 'center', 'position' => 'position', ), ); parent::__construct(); } public function renderForm() { $sql = new DbQuery(); $sql->select('*'); $sql->from('faq_category', 'c'); $sql->innerJoin('faq_category_lang', 'l', 'c.id_faq_category = l.id_faq_category AND l.id_lang = '.(int)$this->context->language->id); $sql->where('c.active = 1'); $sql->orderBy('position'); $categoriesArray = Db::getInstance()->executeS($sql); $this->fields_form = array( 'tinymce' => TRUE, 'legend' => array( 'title' => $this->l('Faq'), // 'icon' => 'icon-envelope-alt' ), 'input' => array( array( 'type' => 'text', 'label' => $this->l('Question'), 'name' => 'question', 'size' => 33, 'lang' => TRUE, 'required' => TRUE, ), array( 'type' => 'textarea', 'label' => $this->l('Answer'), 'name' => 'answer', 'size' => 33, 'lang' => TRUE, 'required' => TRUE, 'autoload_rte' => 'rte' ), array( 'type' => 'select', 'label' => $this->l('Category'), 'name' => 'id_category', 'size' => 1, 'lang' => FALSE, 'required' => TRUE, 'options' => array( 'query' => $categoriesArray, 'id' => 'id_faq_category', 'name' => 'title' ), ), array( 'type' => 'switch', 'label' => $this->l('Active'), 'name' => 'active', 'required' => FALSE, 'is_bool' => TRUE, 'values' => array( array( 'id' => 'active_on', 'value' => 1, 'label' => $this->l('Enabled') ), array( 'id' => 'active_off', 'value' => 0, 'label' => $this->l('Disabled') ) ), ), ), 'submit' => array( 'title' => $this->l('Save') ) ); return parent::renderForm(); } } Link to comment Share on other sites More sharing options...
basvandertogt Posted February 24, 2016 Author Share Posted February 24, 2016 Long time ago with no answers so i will answer this myself :-) I added this in the controller: public function ajaxProcessUpdatePositions() { $way = (int)Tools::getValue('way'); $id_quicklinks = (int)Tools::getValue('id'); $positions = Tools::getValue('faq_category'); if (is_array($positions)) foreach ($positions as $position => $value) { $pos = explode('_', $value); if (isset($pos[2]) && (int)$pos[2] === $id_quicklinks) { if (isset($position) && $this->updatePosition($way, $position, $id_quicklinks)) echo 'ok position '.(int)$position.' for id '.(int)$pos[1].'\r\n'; else echo '{"hasError" : true, "errors" : "Can not update id '.(int)$id_quicklinks.' to position '.(int)$position.' "}'; break; } } } public function updatePosition($way, $position, $id) { if (!$res = Db::getInstance()->executeS(' SELECT `id_faq_category`, `position` FROM `'._DB_PREFIX_.'faq_category` ORDER BY `position` ASC' )) return false; foreach ($res as $quicklinks) if ((int)$quicklinks['id_faq_category'] == (int)$id) $moved_quicklinks = $quicklinks; if (!isset($moved_quicklinks) || !isset($position)) return false; // var_dump($moved_quicklinks['position']); // < and > statements rather than BETWEEN operator // since BETWEEN is treated differently according to databases return (Db::getInstance()->execute(' UPDATE `'._DB_PREFIX_.'faq_category` SET `position`= `position` '.($way ? '- 1' : '+ 1').' WHERE `position` '.($way ? '> '.(int)$moved_quicklinks['position'].' AND `position` <= '.(int)$position : '< '.(int)$moved_quicklinks['position'].' AND `position` >= '.(int)$position.' ')) && Db::getInstance()->execute(' UPDATE `'._DB_PREFIX_.'faq_category` SET `position` = '.(int)$position.' WHERE `id_faq_category` = '.(int)$moved_quicklinks['id_faq_category'])); } Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now