public function reOrderPositions() { $id_slide = $this->id; $context = Context::getContext(); $id_shop = $context->shop->id; $max = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' SELECT MAX(hss.`position`) as position FROM `' . _DB_PREFIX_ . 'tmsubbanner_slides` hss, `' . _DB_PREFIX_ . 'tmsubbanner` hs WHERE hss.`id_tmsubbanner_slides` = hs.`id_tmsubbanner_slides` AND hs.`id_shop` = ' . (int) $id_shop); if ((int) $max == (int) $id_slide) { return true; } $rows = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' SELECT hss.`position` as position, hss.`id_tmsubbanner_slides` as id_slide FROM `' . _DB_PREFIX_ . 'tmsubbanner_slides` hss LEFT JOIN `' . _DB_PREFIX_ . 'tmsubbanner` hs ON (hss.`id_tmsubbanner_slides` = hs.`id_tmsubbanner_slides`) WHERE hs.`id_shop` = ' . (int) $id_shop . ' AND hss.`position` > ' . (int) $this->position); foreach ($rows as $row) { $current_slide = new subBanner($row['id_slide']); --$current_slide->position; $current_slide->update(); unset($current_slide); } return true; }
private function _postProcess() { $errors = array(); /* Process Slide status */ if (Tools::isSubmit('changeStatus') && Tools::isSubmit('id_slide')) { $slide = new subBanner((int) Tools::getValue('id_slide')); if ($slide->active == 0) { $slide->active = 1; } else { $slide->active = 0; } $res = $slide->update(); $this->clearCache(); $this->_html .= $res ? $this->displayConfirmation($this->l('Configuration updated')) : $this->displayError($this->l('The configuration could not be updated.')); } elseif (Tools::isSubmit('submitSlide')) { /* Sets ID if needed */ if (Tools::getValue('id_slide')) { $slide = new subBanner((int) Tools::getValue('id_slide')); if (!Validate::isLoadedObject($slide)) { $this->_html .= $this->displayError($this->l('Invalid id_slide')); return false; } } else { $slide = new subBanner(); } /* Sets position */ $slide->position = (int) Tools::getValue('position'); /* Sets active */ $slide->active = (int) Tools::getValue('active_slide'); /* Sets each langue fields */ $languages = Language::getLanguages(false); foreach ($languages as $language) { $slide->title[$language['id_lang']] = Tools::getValue('title_' . $language['id_lang']); $slide->url[$language['id_lang']] = Tools::getValue('url_' . $language['id_lang']); /* Uploads image and sets slide */ $type = Tools::strtolower(Tools::substr(strrchr($_FILES['image_' . $language['id_lang']]['name'], '.'), 1)); $imagesize = @getimagesize($_FILES['image_' . $language['id_lang']]['tmp_name']); if (isset($_FILES['image_' . $language['id_lang']]) && isset($_FILES['image_' . $language['id_lang']]['tmp_name']) && !empty($_FILES['image_' . $language['id_lang']]['tmp_name']) && !empty($imagesize) && in_array(Tools::strtolower(Tools::substr(strrchr($imagesize['mime'], '/'), 1)), array('jpg', 'gif', 'jpeg', 'png')) && in_array($type, array('jpg', 'gif', 'jpeg', 'png'))) { $temp_name = tempnam(_PS_TMP_IMG_DIR_, 'PS'); $salt = sha1(microtime()); if ($error = ImageManager::validateUpload($_FILES['image_' . $language['id_lang']])) { $errors[] = $error; } elseif (!$temp_name || !move_uploaded_file($_FILES['image_' . $language['id_lang']]['tmp_name'], $temp_name)) { return false; } elseif (!ImageManager::resize($temp_name, dirname(__FILE__) . '/img/' . Tools::encrypt($_FILES['image_' . $language['id_lang']]['name'] . $salt) . '.' . $type, null, null, $type)) { $errors[] = $this->displayError($this->l('An error occurred during the image upload process.')); } if (isset($temp_name)) { @unlink($temp_name); } $slide->image[$language['id_lang']] = Tools::encrypt($_FILES['image_' . $language['id_lang']]['name'] . $salt) . '.' . $type; } elseif (Tools::getValue('image_old_' . $language['id_lang']) != '') { $slide->image[$language['id_lang']] = Tools::getValue('image_old_' . $language['id_lang']); } } /* Processes if no errors */ if (!$errors) { /* Adds */ if (!Tools::getValue('id_slide')) { if (!$slide->add()) { $errors[] = $this->displayError($this->l('The slide could not be added.')); } } elseif (!$slide->update()) { $errors[] = $this->displayError($this->l('The slide could not be updated.')); } $this->clearCache(); } } elseif (Tools::isSubmit('delete_id_slide')) { $slide = new subBanner((int) Tools::getValue('delete_id_slide')); $res = $slide->delete(); $this->clearCache(); if (!$res) { $this->_html .= $this->displayError('Could not delete.'); } else { Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules', true) . '&conf=1&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name); } } /* Display errors if needed */ if (count($errors)) { $this->_html .= $this->displayError(implode('<br />', $errors)); } elseif (Tools::isSubmit('submitSlide') && Tools::getValue('id_slide')) { Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules', true) . '&conf=4&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name); } elseif (Tools::isSubmit('submitSlide')) { Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules', true) . '&conf=3&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name); } }