* @copyright 2007-2014 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
if (!defined('_PS_VERSION_'))
exit;
include_once(dirname(__FILE__).'/classes/StBlogRelatedArticlesClass.php');
include_once(_PS_MODULE_DIR_.'stthemeeditor/classes/BaseSlider.php');
class StBlogRelatedArticles extends BaseSlider
{
public $_prefix_st = 'ST_B_RELATED_';
public $_prefix_stsn = 'STSN_B_RELATED_';
protected $sort_by = array(
1 => array('id' =>1 , 'name' => 'Date add: Desc', 'orderBy'=>'date_add', 'orderWay'=>'DESC'),
2 => array('id' =>2 , 'name' => 'Date add: Asc', 'orderBy'=>'date_add', 'orderWay'=>'ASC'),
3 => array('id' =>3 , 'name' => 'Date update: Desc', 'orderBy'=>'date_udp', 'orderWay'=>'DESC'),
4 => array('id' =>4 , 'name' => 'Date update: Asc', 'orderBy'=>'date_udp', 'orderWay'=>'ASC'),
5 => array('id' =>5 , 'name' => 'Position: Asc', 'orderBy'=>'position', 'orderWay'=>'DESC'),
6 => array('id' =>6 , 'name' => 'Position ID: Desc', 'orderBy'=>'position', 'orderWay'=>'ASC'),
7 => array('id' =>7 , 'name' => 'Blog ID: Asc', 'orderBy'=>'id_st_blog', 'orderWay'=>'DESC'),
8 => array('id' =>8 , 'name' => 'Blog ID: Desc', 'orderBy'=>'id_st_blog', 'orderWay'=>'ASC'),
);
private $templateFile = array();
function __construct()
{
$this->name = 'stblogrelatedarticles';
$this->version = '1.0';
$this->displayName = $this->getTranslator()->trans('Blog Module - Related articles', array(), 'Modules.Stblog.Admin');
$this->description = $this->getTranslator()->trans('Add related articles on blog artice pages.', array(), 'Modules.Stblog.Admin');
parent::__construct();
Shop::addTableAssociation('st_blog', array('type' => 'shop'));
$this->templateFile = array(
'module:stthemeeditor/views/templates/slider/header.tpl',
'module:stthemeeditor/views/templates/slider/homepage.tpl',
'module:stblog/views/templates/slider/footer.tpl',
);
}
protected function initTabNames()
{
$this->_tabs = array(
array('id' => '0', 'name' => $this->getTranslator()->trans('General settings', array(), 'Admin.Theme.Panda')),
array('id' => '1,4', 'name' => $this->getTranslator()->trans('Slider', array(), 'Admin.Theme.Panda')),
array('id' => '2', 'name' => $this->getTranslator()->trans('Left or right column', array(), 'Admin.Theme.Panda')),
array('id' => '3', 'name' => $this->getTranslator()->trans('Hooks', array(), 'Admin.Theme.Panda')),
);
}
protected function initHookArray()
{
$this->_hooks = array(
'Hooks' => array(
array(
'id' => 'displayFooterProduct',
'val' => '1',
'name' => $this->getTranslator()->trans('displayFooterProduct', array(), 'Admin.Theme.Panda')
),
array(
'id' => 'displayStBlogArticleFooter',
'val' => '1',
'name' => $this->getTranslator()->trans('displayStBlogArticleFooter', array(), 'Admin.Theme.Panda')
),
),
'Column' => array(
array(
'id' => 'displayLeftColumn',
'val' => '1',
'name' => $this->getTranslator()->trans('Left column except the produt page', array(), 'Admin.Theme.Panda')
),
array(
'id' => 'displayRightColumn',
'val' => '1',
'name' => $this->getTranslator()->trans('Right column except the produt page', array(), 'Admin.Theme.Panda')
),
array(
'id' => 'displayLeftColumnProduct',
'val' => '1',
'name' => $this->getTranslator()->trans('Left column on the product page only', array(), 'Admin.Theme.Panda')
),
array(
'id' => 'displayRightColumnProduct',
'val' => '1',
'name' => $this->getTranslator()->trans('Right column on the product page only', array(), 'Admin.Theme.Panda')
),
array(
'id' => 'displayProductRightColumn',
'val' => '1',
'name' => $this->getTranslator()->trans('Product right column', array(), 'Admin.Theme.Panda')
),
array(
'id' => 'displayStBlogLeftColumn',
'val' => '1',
'name' => $this->getTranslator()->trans('displayStBlogLeftColumn', array(), 'Admin.Theme.Panda')
),
array(
'id' => 'displayStBlogRightColumn',
'val' => '1',
'name' => $this->getTranslator()->trans('displayStBlogRightColumn', array(), 'Admin.Theme.Panda')
),
)
);
}
function install()
{
if (!parent::install()
|| !$this->installDB()
|| !$this->registerHook('actionObjectStBlogClassAddAfter')
|| !$this->registerHook('actionObjectStBlogClassUpdateAfter')
|| !$this->registerHook('actionObjectStBlogClassDeleteAfter')
|| !$this->registerHook('actionAdminStBlogFormModifier')
|| !$this->registerHook('displayStBlogArticleFooter')
|| !Configuration::updateValue($this->_prefix_st.'BY_TAG', 1)
|| !Configuration::updateValue($this->_prefix_st.'IMAGE_TYPE', 'medium')
)
return false;
$this->clearSliderCache();
return true;
}
private function installDB()
{
$return = (bool)Db::getInstance()->execute('
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'st_blog_related_articles` (
`id_st_blog_1` int(10) unsigned NOT NULL DEFAULT 0,
`id_st_blog_2` int(10) unsigned NOT NULL DEFAULT 0,
PRIMARY KEY (`id_st_blog_1`,`id_st_blog_2`)
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8 ;');
return $return;
}
private function uninstallDB()
{
return Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'st_blog_related_articles`');
}
public function uninstall()
{
$this->clearSliderCache();
if (!parent::uninstall() ||
!$this->uninstallDB())
return false;
return true;
}
public function getContent()
{
if (Tools::getValue('act') == 'gsbra' && Tools::getValue('ajax')==1)
{
if(!$q = Tools::getValue('q'))
die;
if(!$id_st_blog = Tools::getValue('id_st_blog'))
die;
$excludeIds = Tools::getValue('excludeIds');
$result = Db::getInstance()->executeS('
SELECT b.`id_st_blog`,bl.`name`
FROM `'._DB_PREFIX_.'st_blog` b
LEFT JOIN `'._DB_PREFIX_.'st_blog_lang` bl
ON (b.`id_st_blog` = bl.`id_st_blog`
AND bl.`id_lang`='.(int)$this->context->language->id.')
'.Shop::addSqlAssociation('st_blog', 'b').'
WHERE bl.`name` LIKE \'%'.pSQL($q).'%\'
AND b.`active` = 1
AND b.`id_st_blog` != '.(int)$id_st_blog.'
'.($excludeIds ? 'AND b.`id_st_blog` NOT IN('.$excludeIds.')' : '').'
');
foreach ($result AS $value)
echo trim($value['name']).'|'.(int)($value['id_st_blog'])."\n";
die;
}
$this->initHookArray();
$this->initTabNames();
$this->context->controller->addCSS($this->_path.'views/css/admin.css');
$this->context->controller->addJS($this->_path.'views/js/admin.js');
parent::getContent();
$helper = $this->initForm();
$this->smarty->assign(array(
'bo_tabs' => $this->_tabs,
'bo_tab_content' => $helper->generateForm($this->fields_form),
));
return $this->_html.$this->display(__FILE__, 'bo_tab_layout.tpl');
}
public function initFieldsForm()
{
$fields = parent::getFormFields();
$fields['home_slider']['display_sd']['values'] = array_merge($fields['home_slider']['display_sd']['values'],
array(
array(
'id' => 'display_sd_short',
'value' => 3,
'label' => $this->getTranslator()->trans('Yes, 120 characters', array(), 'Admin.Theme.Panda')),
array(
'id' => 'display_sd_4',
'value' => 4,
'label' => $this->getTranslator()->trans('Content, 120 characters', array(), 'Admin.Theme.Panda')),
array(
'id' => 'display_sd_5',
'value' => 5,
'label' => $this->getTranslator()->trans('Content, 220 characters', array(), 'Admin.Theme.Panda')),
array(
'id' => 'display_sd_6',
'value' => 6,
'label' => $this->getTranslator()->trans('Content, about 5 lines', array(), 'Admin.Theme.Panda')),
array(
'id' => 'display_sd_7',
'value' => 7,
'label' => $this->getTranslator()->trans('Content, about 10 lines', array(), 'Admin.Theme.Panda'))
));
$this->fields_form[0]['form'] = array(
'legend' => array(
'title' => $this->getTranslator()->trans('General settings', array(), 'Admin.Theme.Panda'),
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'switch',
'label' => $this->getTranslator()->trans('Automatically generate related articles(using tags):', array(), 'Admin.Theme.Panda'),
'name' => 'by_tag',
'is_bool' => true,
'default_value' => 1,
'values' => array(
array(
'id' => 'by_tag_on',
'value' => 1,
'label' => $this->getTranslator()->trans('Yes', array(), 'Admin.Theme.Panda')),
array(
'id' => 'by_tag_off',
'value' => 0,
'label' => $this->getTranslator()->trans('No', array(), 'Admin.Theme.Panda')),
),
'validation' => 'isBool',
),
),
'submit' => array(
'title' => $this->getTranslator()->trans('Save all', array(), 'Admin.Theme.Panda'),
),
);
$image_types_arr = array();
$imagesTypes = StBlogImageClass::getDefImageTypes();
foreach ($imagesTypes[1] as $k => $imageType) {
if (!isset($imageType[0]) || !$imageType[0] || !isset($imageType[1]) || !$imageType[1]) {
continue;
}
$image_types_arr[$k] = array('id' => $k, 'name' => $k.'('.$imageType[0].' x '.$imageType[1].')');
}
$fields['home_slider']['image_type']['options']['query'] = $image_types_arr;
$fields['home_slider']['image_type']['options']['default'] = array(
'value' => '',
'label' => $this->getTranslator()->trans('--', array(), 'Admin.Theme.Panda'),
);
$option = array(
'spacing' => (int)Configuration::get($this->_prefix_st.'SPACING_BETWEEN'),
'per_lg' => (int)Configuration::get($this->_prefix_stsn.'PRO_PER_LG'),
'per_xl' => (int)Configuration::get($this->_prefix_stsn.'PRO_PER_XL'),
'per_xxl' => (int)Configuration::get($this->_prefix_stsn.'PRO_PER_XXL'),
'page' => 'module-stblog-article',
);
$fields['home_slider']['image_type']['desc'] = $this->calcImageWidth($option);
$fields['home_slider']['grid']['label'] = $this->getTranslator()->trans('How to display articles', array(), 'Admin.Theme.Panda');
$fields['home_slider']['nbr']['label'] = $this->getTranslator()->trans('Define the number of articles to be displayed:', array(), 'Admin.Theme.Panda');
$fields['home_slider']['spacing_between']['label'] = $this->getTranslator()->trans('Spacing between articles:', array(), 'Admin.Theme.Panda');
$fields['home_slider']['display_sd']['label'] = $this->getTranslator()->trans('Display articles short description:', array(), 'Admin.Theme.Panda');
$fields['home_slider']['link_hover_color']['label'] = $this->getTranslator()->trans('Article name hover color:', array(), 'Admin.Theme.Panda');
unset($fields['home_slider']['price_color']);
$this->fields_form[1]['form'] = array(
'legend' => array(
'title' => $this->getTranslator()->trans('Slide settings', array(), 'Admin.Theme.Panda'),
'icon' => 'icon-cogs'
),
'input' => $fields['home_slider'],
'submit' => array(
'title' => $this->getTranslator()->trans('Save all', array(), 'Admin.Theme.Panda'),
)
);
$this->fields_form[4]['form'] = array(
'legend' => array(
'title' => $this->getTranslator()->trans('Style settings', array(), 'Admin.Theme.Panda'),
'icon' => 'icon-cogs'
),
'input' => $fields['setting'],
'submit' => array(
'title' => $this->getTranslator()->trans('Save all', array(), 'Admin.Theme.Panda'),
)
);
$fields['column']['display_pro_col']['label'] = $this->getTranslator()->trans('How to display articles:', array(), 'Admin.Theme.Panda');
$fields['column']['nbr_col']['label'] = $this->getTranslator()->trans('Define the number of articles to be displayed:', array(), 'Admin.Theme.Panda');
$fields['column']['items_col']['label'] = $this->getTranslator()->trans('How many articles per view on compact slider:', array(), 'Admin.Theme.Panda');
$this->fields_form[2]['form'] = array(
'legend' => array(
'title' => $this->getTranslator()->trans('Column Slide Settings', array(), 'Admin.Theme.Panda'),
'icon' => 'icon-cogs'
),
'input' => $fields['column'],
'submit' => array(
'title' => $this->getTranslator()->trans('Save all', array(), 'Admin.Theme.Panda'),
)
);
$this->fields_form[3]['form'] = array(
'legend' => array(
'title' => $this->getTranslator()->trans('Hook manager', array(), 'Admin.Theme.Panda'),
'icon' => 'icon-cogs'
),
'description' => $this->getTranslator()->trans('Check the hook that you would like this module to display on.', array(), 'Admin.Theme.Panda').'
'.$this->getTranslator()->trans('Click here to see hook position', array(), 'Admin.Theme.Panda').'.',
'input' => $fields['hook'],
'submit' => array(
'title' => $this->getTranslator()->trans('Save all', array(), 'Admin.Theme.Panda')
),
);
}
public function hookActionAdminStBlogFormModifier($params)
{
if(!$id_st_blog = Tools::getValue('id_st_blog'))
return false;
$fields_form['form'] = array(
'legend' => array(
'title' => 'Related articles',
'icon' => 'icon-cogs'
),
'input' => array(
'relatedarticles' => array(
'type' => 'text',
'label' => $this->getTranslator()->trans('Related articles:', array(), 'Modules.Stblog.Admin'),
'name' => 'relatedarticles',
'autocomplete' => false,
'class' => 'fixed-width-xxl',
'desc' => $this->getTranslator()->trans('Begin typing the first letters of the artilce name, then select the article from the drop-down list.', array(), 'Modules.Stblog.Admin'),
),
),
'buttons' => array(
array(
'title' => $this->getTranslator()->trans('Save all', array(), 'Admin.Theme.Panda'),
'class' => 'btn btn-default pull-right',
'icon' => 'process-icon-save',
'type' => 'submit'
)
),
'submit' => array(
'title' => $this->getTranslator()->trans('Save and stay', array(), 'Admin.Actions'),
'stay' => true
),
);
$js = '';
$html = '';
foreach(StBlogRelatedArticlesClass::getRelatedArticlesLight((int)$this->context->language->id,(int)$id_st_blog) AS $value)
{
$html .= '