';
foreach ($languages as $language)
{
$this->_html .= '
';
$this->_html .= '
';
/* Sets image as hidden in case it does not change */
if ($slide && $slide->image[$language['id_lang']])
$this->_html .= '
';
/* Display image */
if ($slide && $slide->image[$language['id_lang']])
$this->_html .= '

';
$this->_html .= '
';
}
$this->_html .= $this->displayFlags($languages, $defaultLanguage, $divLangName, 'image', true);
/* End Fieldset Upload */
$this->_html .= '
';
/* Fieldset edit/add */
$this->_html .= '
';
}
private function _postValidation()
{
$errors = array();
/* Validation for Slider configuration */
if (Tools::isSubmit('submitSlider'))
{
if (!Validate::isInt(Tools::getValue('HOMESLIDER_SPEED')) || !Validate::isInt(Tools::getValue('HOMESLIDER_PAUSE')) ||
!Validate::isInt(Tools::getValue('HOMESLIDER_WIDTH')) || !Validate::isInt(Tools::getValue('HOMESLIDER_HEIGHT')))
$errors[] = $this->l('Invalid values');
} /* Validation for status */
else if (Tools::isSubmit('changeStatus'))
{
if (!Validate::isInt(Tools::getValue('id_slide')))
$errors[] = $this->l('Invalid slide');
}
/* Validation for Slide */
else if (Tools::isSubmit('submitSlide'))
{
/* Checks state (active) */
if (!Validate::isInt(Tools::getValue('active_slide')) || (Tools::getValue('active_slide') != 0 && Tools::getValue('active_slide') != 1))
$errors[] = $this->l('Invalid slide state');
/* Checks position */
if (!Validate::isInt(Tools::getValue('position')) || (Tools::getValue('position') < 0))
$errors[] = $this->l('Invalid slide position');
/* If edit : checks id_slide */
if (Tools::isSubmit('id_slide'))
{
if (!Validate::isInt(Tools::getValue('id_slide')) && !$this->slideExists(Tools::getValue('id_slide')))
$errors[] = $this->l('Invalid id_slide');
}
/* Checks title/url/legend/description/image */
$languages = Language::getLanguages(false);
foreach ($languages as $language)
{
if (strlen(Tools::getValue('title_'.$language['id_lang'])) > 40)
$errors[] = $this->l('Title is too long');
if (strlen(Tools::getValue('legend_'.$language['id_lang'])) > 40)
$errors[] = $this->l('Legend is too long');
if (strlen(Tools::getValue('url_'.$language['id_lang'])) > 200)
$errors[] = $this->l('URL is too long');
if (strlen(Tools::getValue('description_'.$language['id_lang'])) > 400)
$errors[] = $this->l('Description is too long');
if (strlen(Tools::getValue('url_'.$language['id_lang'])) > 0 && !Validate::isUrl(Tools::getValue('url_'.$language['id_lang'])))
$errors[] = $this->l('URL format is not correct');
if (Tools::getValue('image_'.$language['id_lang']) != null && !Validate::isFileName(Tools::getValue('image_'.$language['id_lang'])))
$errors[] = $this->l('Invalid filename');
if (Tools::getValue('image_old_'.$language['id_lang']) != null && !Validate::isFileName(Tools::getValue('image_old_'.$language['id_lang'])))
$errors[] = $this->l('Invalid filename');
}
/* Checks title/url/legend/description for default lang */
$defaultLanguage = (int)Configuration::get('PS_LANG_DEFAULT');
if (strlen(Tools::getValue('title_'.$defaultLanguage)) == 0)
$errors[] = $this->l('Title is not set');
if (strlen(Tools::getValue('legend_'.$defaultLanguage)) == 0)
$errors[] = $this->l('Legend is not set');
if (strlen(Tools::getValue('url_'.$defaultLanguage)) == 0)
$errors[] = $this->l('URL is not set');
if (Tools::getValue('image_'.$defaultLanguage) == '' && !Validate::isFileName(Tools::getValue('image_'.$defaultLanguage)) && !Tools::getValue('image_old_'.defaultLanguage))
$errors[] = $this->l('Image is not set');
if (Tools::getValue('image_old_'.$defaultLanguage) && !Validate::isFileName(Tools::getValue('image_old_'.$defaultLanguage)))
$errors[] = $this->l('Image is not set');
} /* Validation for deletion */
else if (Tools::isSubmit('delete_id_slide') && (!Validate::isInt(Tools::getValue('delete_id_slide')) || !$this->slideExists((int)Tools::getValue('delete_id_slide'))))
$errors[] = $this->l('Invalid id_slide');
/* Display errors if needed */
if (count($errors))
{
$this->_html .= $this->displayError(implode('
', $errors));
return false;
}
/* Returns if validation is ok */
return true;
}
private function _postProcess()
{
$errors = array();
/* Processes Slider */
if (Tools::isSubmit('submitSlider'))
{
$res = Configuration::updateValue('HOMESLIDER_WIDTH', (int)Tools::getValue('HOMESLIDER_WIDTH'));
$res &= Configuration::updateValue('HOMESLIDER_HEIGHT', (int)Tools::getValue('HOMESLIDER_HEIGHT'));
$res &= Configuration::updateValue('HOMESLIDER_SPEED', (int)Tools::getValue('HOMESLIDER_SPEED'));
$res &= Configuration::updateValue('HOMESLIDER_PAUSE', (int)Tools::getValue('HOMESLIDER_PAUSE'));
if (!$res)
$errors .= $this->displayError($this->l('Configuration could not be updated'));
$this->_html = $this->displayConfirmation($this->l('Configuration updated'));
} /* Process Slide status */
else if (Tools::isSubmit('changeStatus') && Tools::isSubmit('id_slide'))
{
$slide = new HomeSlide((int)Tools::getValue('id_slide'));
$slide->active = (int)($slide->active == 0 ? 1 : 0);
$res = $slide->update();
$this->_html = ($res ? $this->displayConfirmation($this->l('Configuration updated')) : $this->displayErro($this->l('Configuration could not be updated')));
}
/* Processes Slide */
else if (Tools::isSubmit('submitSlide'))
{
/* Sets ID if needed */
if (Tools::getValue('id_slide'))
{
$slide = new HomeSlide((int)Tools::getValue('id_slide'));
if (!Validate::isLoadedObject($slide))
{
$this->_html = $this->displayError($this->l('Invalid id_slide'));
return;
}
}
else
$slide = new HomeSlide();
/* 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)
{
if (Tools::getValue('title_'.$language['id_lang']) != '')
$slide->title[$language['id_lang']] = pSQL(Tools::getValue('title_'.$language['id_lang']));
if (Tools::getValue('url_'.$language['id_lang']) != '')
$slide->url[$language['id_lang']] = pSQL(Tools::getValue('url_'.$language['id_lang']));
if (Tools::getValue('legend_'.$language['id_lang']) != '')
$slide->legend[$language['id_lang']] = pSQL(Tools::getValue('legend_'.$language['id_lang']));
if (Tools::getValue('description_'.$language['id_lang']) != '')
$slide->description[$language['id_lang']] = pSQL(Tools::getValue('description_'.$language['id_lang']));
/* Uploads image and sets slide */
if (isset($_FILES['image_'.$language['id_lang']]) && isset($_FILES['image_'.$language['id_lang']]['tmp_name']) && !empty($_FILES['image_'.$language['id_lang']]['tmp_name']))
{
if ($error = checkImage($_FILES['image_'.$language['id_lang']]))
$errors .= $error;
else if (!$tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS') || !move_uploaded_file($_FILES['image_'.$language['id_lang']]['tmp_name'], $tmpName))
return false;
else if (!imageResize($tmpName, dirname(__FILE__).'/images/'.Tools::encrypt($_FILES['image_'.$language['id_lang']]['name']).'.jpg'))
$errors .= $this->displayError($this->l('An error occurred during the image upload.'));
if (isset($tmpName))
unlink($tmpName);
$slide->image[$language['id_lang']] = pSQL(Tools::encrypt($_FILES['image_'.($language['id_lang'])]['name']).'.jpg');
}
if (Tools::getValue('image_old_'.$language['id_lang']) != '')
$slide->image[$language['id_lang']] = pSQL(Tools::getValue('image_old_'.$language['id_lang']));
}
/* Adds */
if (!Tools::getValue('id_slide'))
{
if (!$slide->add())
$errors .= $this->displayError($this->l('Slide could not be added'));
} /* Update */
else if (!$slide->update())
$errors .= $this->displayError($this->l('Slide could not be updated'));
} /* Deletes */
else if (Tools::isSubmit('delete_id_slide'))
{
$slide = new HomeSlide((int)Tools::getValue('delete_id_slide'));
$res = $slide->delete();
if (!$res)
$this->_html .= $this->displayError('Could not delete');
$this->_html = $this->displayConfirmation($this->l('Slide deleted'));
}
/* Display errors if needed */
if (count($errors))
$this->_html .= $this->displayError(implode('
', $errors));
else if (Tools::isSubmit('submitSlide') && Tools::getValue('id_slide'))
$this->_html .= $this->displayConfirmation($this->l('Slide updated'));
else if (Tools::isSubmit('submitSlide'))
$this->_html .= $this->displayConfirmation($this->l('Slide added'));
}
public function hookHome()
{
$slider = array(
'width' => Configuration::get('HOMESLIDER_WIDTH'),
'height' => Configuration::get('HOMESLIDER_HEIGHT'),
'speed' => Configuration::get('HOMESLIDER_SPEED'),
'pause' => Configuration::get('HOMESLIDER_PAUSE')
);
$slides = $this->getSlides(true);
if (!$slides)
return;
$this->context->smarty->assign('homeslider_slides', $slides);
$this->context->smarty->assign('homeslider', $slider);
return $this->display(__FILE__, 'homeslider.tpl');
}
public function hookHeader()
{
if (!$this->getSlides(true))
return;
$this->context->controller->addJS(_PS_JS_DIR_.'jquery/jquery-ui-1.8.10.custom.min.js');
$this->context->controller->addJS($this->_path.'js/jquery.bxSlider.min.js');
$this->context->controller->addCSS($this->_path.'bx_styles.css');
$this->context->controller->addJS($this->_path.'js/homeslider.js');
}
public function hookBackOfficeTop()
{
/* Style & js for fieldset 'slides configuration' */
$html = '
';
return $html;
}
public function getNextPosition()
{
$row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('
SELECT MAX(hss.`position`) AS `next_position`
FROM `'._DB_PREFIX_.'homeslider_slides` hss, `'._DB_PREFIX_.'homeslider` hs
WHERE hss.`id_slide` = hs.`id_slide` AND hs.`id_shop` = '.(int)$this->context->shop->getId()
);
return (++$row['next_position']);
}
public function getSlides($active = null)
{
$this->context = Context::getContext();
$idShop = $this->context->shop->getID();
$idLang = $this->context->language->id;
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT hs.`id_slide` AS id_slide, hssl.`image` as image, hss.`position` AS position, hss.`active` as active, hssl.`title` as title, hssl.`url` as url, hssl.`legend` as legend
FROM `'._DB_PREFIX_.'homeslider` hs, `'._DB_PREFIX_.'homeslider_slides` hss, `'._DB_PREFIX_.'homeslider_slides_lang` hssl
WHERE hs.`id_shop` = '.(int)$idShop.((int)$idShop != 0 ? ' OR hs.`id_shop` = 0' : '').' AND hs.`id_slide` = hss.`id_slide` AND hss.`id_slide` = hssl.`id_slide` AND hs.`id_slide` = hssl.`id_slide`
AND hssl.`id_lang` = '.(int)$idLang.($active ? ' AND hss.`active` = 1' : '').'
ORDER BY hss.`position`
');
}
public function displayStatus($id_slide, $active)
{
$title = ((int)$active == 0 ? $this->l('Disabled') : $this->l('Enabled'));
$img = ((int)$active == 0 ? 'disabled.gif' : 'enabled.gif');
$html = '

';
return $html;
}
public function slideExists($id_slide)
{
$req = 'SELECT hs.`id_slide`
FROM `'._DB_PREFIX_.'homeslider` hs
WHERE hs.`id_slide` = '.(int)$id_slide;
$row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($req);
return ($row);
}
}