';
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, $id_lang_default, $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 */
elseif (Tools::isSubmit('changeStatus'))
{
if (!Validate::isInt(Tools::getValue('id_slide')))
$errors[] = $this->l('Invalid slide');
}
/* Validation for Slide */
elseif (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'))
{
//d(var_dump(Tools::getValue('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 (Tools::strlen(Tools::getValue('title_'.$language['id_lang'])) > 255)
$errors[] = $this->l('The title is too long.');
if (Tools::strlen(Tools::getValue('legend_'.$language['id_lang'])) > 255)
$errors[] = $this->l('The legend is too long.');
if (Tools::strlen(Tools::getValue('url_'.$language['id_lang'])) > 255)
$errors[] = $this->l('The URL is too long.');
if (Tools::strlen(Tools::getValue('description_'.$language['id_lang'])) > 4000)
$errors[] = $this->l('The description is too long.');
if (Tools::strlen(Tools::getValue('url_'.$language['id_lang'])) > 0 && !Validate::isUrl(Tools::getValue('url_'.$language['id_lang'])))
$errors[] = $this->l('The 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 */
$id_lang_default = (int)Configuration::get('PS_LANG_DEFAULT');
if (Tools::strlen(Tools::getValue('title_'.$id_lang_default)) == 0)
$errors[] = $this->l('The title is not set.');
if (Tools::strlen(Tools::getValue('legend_'.$id_lang_default)) == 0)
$errors[] = $this->l('The legend is not set.');
if (Tools::strlen(Tools::getValue('url_'.$id_lang_default)) == 0)
$errors[] = $this->l('The URL is not set.');
if (!Tools::isSubmit('has_picture') && (!isset($_FILES['image_'.$id_lang_default]) || empty($_FILES['image_'.$id_lang_default]['tmp_name'])))
$errors[] = $this->l('The image is not set.');
if (Tools::getValue('image_old_'.$id_lang_default) && !Validate::isFileName(Tools::getValue('image_old_'.$id_lang_default)))
$errors[] = $this->l('The image is not set.');
} /* Validation for deletion */
elseif (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'));
$res &= Configuration::updateValue('HOMESLIDER_LOOP', (int)Tools::getValue('HOMESLIDER_LOOP'));
$this->clearCache();
if (!$res)
$errors[] = $this->displayError($this->l('The configuration could not be updated.'));
$this->_html .= $this->displayConfirmation($this->l('Configuration updated'));
} /* Process Slide status */
elseif (Tools::isSubmit('changeStatus') && Tools::isSubmit('id_slide'))
{
$slide = new HomeSlide((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.')));
}
/* Processes Slide */
elseif (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)
{
$slide->title[$language['id_lang']] = Tools::getValue('title_'.$language['id_lang']);
$slide->url[$language['id_lang']] = Tools::getValue('url_'.$language['id_lang']);
$slide->legend[$language['id_lang']] = Tools::getValue('legend_'.$language['id_lang']);
$slide->description[$language['id_lang']] = Tools::getValue('description_'.$language['id_lang']);
/* Uploads image and sets slide */
$type = strtolower(substr(strrchr($_FILES['image_'.$language['id_lang']]['name'], '.'), 1));
$imagesize = array();
$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(strtolower(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__).'/images/'.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.'));
}
/* Update */
elseif (!$slide->update())
$errors[] = $this->displayError($this->l('The slide could not be updated.'));
$this->clearCache();
}
} /* Deletes */
elseif (Tools::isSubmit('delete_id_slide'))
{
$slide = new HomeSlide((int)Tools::getValue('delete_id_slide'));
$res = $slide->delete();
$this->clearCache();
if (!$res)
$this->_html .= $this->displayError('Could not delete');
else
$this->_html .= $this->displayConfirmation($this->l('Slide deleted'));
}
/* Display errors if needed */
if (count($errors))
$this->_html .= $this->displayError(implode('
', $errors));
elseif (Tools::isSubmit('submitSlide') && Tools::getValue('id_slide'))
$this->_html .= $this->displayConfirmation($this->l('Slide updated'));
elseif (Tools::isSubmit('submitSlide'))
$this->_html .= $this->displayConfirmation($this->l('Slide added'));
}
private function _prepareHook()
{
if (!$this->isCached('homeslider.tpl', $this->getCacheId()))
{
$slider = array(
'width' => Configuration::get('HOMESLIDER_WIDTH'),
'height' => Configuration::get('HOMESLIDER_HEIGHT'),
'speed' => Configuration::get('HOMESLIDER_SPEED'),
'pause' => Configuration::get('HOMESLIDER_PAUSE'),
'loop' => Configuration::get('HOMESLIDER_LOOP'),
);
$slides = $this->getSlides(true);
if (!$slides)
return false;
$this->smarty->assign('homeslider_slides', $slides);
$this->smarty->assign('homeslider', $slider);
}
return true;
}
public function hookdisplayHeader($params)
{
$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 hookdisplayTop($params)
{
return $this->hookdisplayTopColumn();
}
public function hookdisplayTopColumn($params)
{
if ($this->context->smarty->tpl_vars['page_name'] != 'index' || $this->context->getMobileDevice() != false)
return false;
if(!$this->_prepareHook())
return false;
return $this->display(__FILE__, 'homeslider.tpl', $this->getCacheId());
}
public function getCacheId($name = null)
{
if ($name === null && isset($this->context->smarty->tpl_vars['page_name']))
return parent::getCacheId($this->context->smarty->tpl_vars['page_name']->value);
return parent::getCacheId($name);
}
public function clearCache()
{
$this->_clearCache('homeslider.tpl');
}
public function hookActionShopDataDuplication($params)
{
Db::getInstance()->execute('
INSERT IGNORE INTO '._DB_PREFIX_.'homeslider (id_homeslider_slides, id_shop)
SELECT id_homeslider_slides, '.(int)$params['new_id_shop'].'
FROM '._DB_PREFIX_.'homeslider
WHERE id_shop = '.(int)$params['old_id_shop']);
$this->clearCache();
}
public function headerHTML()
{
if (Tools::getValue('controller') != 'AdminModules' && Tools::getValue('configure') != $this->name)
return;
$this->context->controller->addJqueryUI('ui.sortable');
/* 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_homeslider_slides` = hs.`id_homeslider_slides` AND hs.`id_shop` = '.(int)$this->context->shop->id
);
return (++$row['next_position']);
}
public function getSlides($active = null)
{
$this->context = Context::getContext();
$id_shop = $this->context->shop->id;
$id_lang = $this->context->language->id;
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT hs.`id_homeslider_slides` as id_slide,
hssl.`image`,
hss.`position`,
hss.`active`,
hssl.`title`,
hssl.`url`,
hssl.`legend`,
hssl.`description`
FROM '._DB_PREFIX_.'homeslider hs
LEFT JOIN '._DB_PREFIX_.'homeslider_slides hss ON (hs.id_homeslider_slides = hss.id_homeslider_slides)
LEFT JOIN '._DB_PREFIX_.'homeslider_slides_lang hssl ON (hss.id_homeslider_slides = hssl.id_homeslider_slides)
WHERE (id_shop = '.(int)$id_shop.')
AND hssl.id_lang = '.(int)$id_lang.
($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_homeslider_slides` as id_slide
FROM `'._DB_PREFIX_.'homeslider` hs
WHERE hs.`id_homeslider_slides` = '.(int)$id_slide;
$row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($req);
return ($row);
}
public function renderList()
{
$slides = $this->getSlides();
foreach ($slides as $key => $slide)
$slides[$key]['status'] = $this->displayStatus($slide['id_slide'], $slide['active']);
$this->context->smarty->assign(array(
'link' => $this->context->link,
'slides' => $slides,
));
return $this->display(__FILE__, 'list.tpl');
}
public function renderAddForm()
{
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->l('Slide informations'),
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'file_lang',
'label' => $this->l('Select a file:'),
'name' => 'image',
'lang' => true,
),
array(
'type' => 'text',
'label' => $this->l('Title:'),
'name' => 'title',
'lang' => true,
),
array(
'type' => 'text',
'label' => $this->l('URL:'),
'name' => 'url',
'lang' => true,
),
array(
'type' => 'text',
'label' => $this->l('Legend:'),
'name' => 'legend',
'lang' => true,
),
array(
'type' => 'textarea',
'label' => $this->l('Description:'),
'name' => 'description',
'lang' => true,
),
array(
'type' => 'switch',
'label' => $this->l('Active'),
'name' => 'active_slide',
'is_bool' => true,
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Yes')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('No')
)
),
),
),
'submit' => array(
'title' => $this->l('Save'),
'class' => 'btn btn-default')
),
);
if (Tools::isSubmit('id_slide') && $this->slideExists((int)Tools::getValue('id_slide')))
{
$slide = new HomeSlide((int)Tools::getValue('id_slide'));
$fields_form['form']['input'][] = array('type' => 'hidden', 'name' => 'id_slide');
$has_picture = true;
foreach (Language::getLanguages(false) as $lang)
if (!isset($slide->image[$lang['id_lang']]))
$has_picture &= false;
if ($has_picture)
$fields_form['form']['input'][] = array('type' => 'hidden', 'name' => 'has_picture');
}
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
$helper->default_form_language = $lang->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$this->fields_form = array();
$helper->module = $this;
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitSlide';
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$language = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
$helper->tpl_vars = array(
'base_url' => $this->context->shop->getBaseURL(),
'language' => array(
'id_lang' => $language->id,
'iso_code' => $language->iso_code
),
'fields_value' => $this->getAddFieldsValues(),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id
);
$helper->override_folder = '/';
return $helper->generateForm(array($fields_form));
}
public function renderForm()
{
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->l('Settings'),
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Height:'),
'name' => 'HOMESLIDER_HEIGHT',
'suffix' => 'px'
),
array(
'type' => 'text',
'label' => $this->l('Width:'),
'name' => 'HOMESLIDER_WIDTH',
'suffix' => 'px'
),
array(
'type' => 'text',
'label' => $this->l('Speed:'),
'name' => 'HOMESLIDER_SPEED',
'suffix' => 'ms'
),
array(
'type' => 'text',
'label' => $this->l('Pause:'),
'name' => 'HOMESLIDER_PAUSE',
'suffix' => 'ms'
),
array(
'type' => 'switch',
'label' => $this->l('Loop:'),
'name' => 'HOMESLIDER_LOOP',
'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'),
'class' => 'btn btn-default')
),
);
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
$helper->default_form_language = $lang->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$this->fields_form = array();
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitSlider';
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = array(
'fields_value' => $this->getConfigFieldsValues(),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id
);
return $helper->generateForm(array($fields_form));
}
public function getConfigFieldsValues()
{
return array(
'HOMESLIDER_HEIGHT' => Tools::getValue('HOMESLIDER_HEIGHT', Configuration::get('HOMESLIDER_HEIGHT')),
'HOMESLIDER_WIDTH' => Tools::getValue('HOMESLIDER_WIDTH', Configuration::get('HOMESLIDER_WIDTH')),
'HOMESLIDER_SPEED' => Tools::getValue('HOMESLIDER_SPEED', Configuration::get('HOMESLIDER_SPEED')),
'HOMESLIDER_PAUSE' => Tools::getValue('HOMESLIDER_PAUSE', Configuration::get('HOMESLIDER_PAUSE')),
'HOMESLIDER_LOOP' => Tools::getValue('HOMESLIDER_LOOP', Configuration::get('HOMESLIDER_LOOP')),
);
}
public function getAddFieldsValues()
{
$fields = array();
if (Tools::isSubmit('id_slide') && $this->slideExists((int)Tools::getValue('id_slide')))
{
$slide = new HomeSlide((int)Tools::getValue('id_slide'));
$fields['id_slide'] = (int)Tools::getValue('id_slide', $slide->id);
}
else
$slide = new HomeSlide();
$fields['active_slide'] = Tools::getValue('active_slide', $slide->active);
$fields['has_picture'] = true;
$languages = Language::getLanguages(false);
foreach ($languages as $lang)
{
$fields['image'][$lang['id_lang']] = Tools::getValue('image_'.(int)$lang['id_lang']);
$fields['title'][$lang['id_lang']] = Tools::getValue('title_'.(int)$lang['id_lang'], $slide->title[$lang['id_lang']]);
$fields['url'][$lang['id_lang']] = Tools::getValue('url_'.(int)$lang['id_lang'], $slide->url[$lang['id_lang']]);
$fields['legend'][$lang['id_lang']] = Tools::getValue('legend_'.(int)$lang['id_lang'], $slide->legend[$lang['id_lang']]);
$fields['description'][$lang['id_lang']] = Tools::getValue('description_'.(int)$lang['id_lang'], $slide->description[$lang['id_lang']]);
}
return $fields;
}
}