Files
interblue.pl/modules/cmsproductspro/cmsproductspro.php
2024-10-25 14:16:28 +02:00

978 lines
44 KiB
PHP

<?php
/**
* PrestaShop module created by VEKIA, a guy from official PrestaShop community ;-)
*
* @author VEKIA https://www.prestashop.com/forums/user/132608-vekia/
* @copyright 2010-2017 VEKIA
* @license This program is not free software and you can't resell and redistribute it
*
* CONTACT WITH DEVELOPER
* support@mypresta.eu
*/
use PrestaShop\PrestaShop\Adapter\Image\ImageRetriever;
use PrestaShop\PrestaShop\Adapter\Product\PriceFormatter;
use PrestaShop\PrestaShop\Core\Product\ProductListingPresenter;
use PrestaShop\PrestaShop\Adapter\Product\ProductColorsRetriever;
class cmsproductspro extends Module
{
function __construct()
{
$this->name = 'cmsproductspro';
$this->tab = 'front_office_features';
$this->author = 'MyPresta.eu';
$this->version = '1.3.6';
$this->bootstrap = 1;
$this->mypresta_link = 'https://mypresta.eu/modules/front-office-features/shortcodes.html';
parent::__construct();
$this->displayName = $this->l('Shortcodes');
$this->description = $this->l('This module allows to use shortcodes in any part of your theme, both in .tpl files and also in configuration of any module / feature in shop back office.');
$this->checkforupdates();
}
public function hookactionOutputHTMLBefore($params)
{
$params['html'] = $this->returnContent($params['html']);
}
public function checkforupdates($display_msg = 0, $form = 0)
{
// ---------- //
// ---------- //
// VERSION 12 //
// ---------- //
// ---------- //
$this->mkey = "nlc";
if (@file_exists('../modules/' . $this->name . '/key.php')) {
@require_once('../modules/' . $this->name . '/key.php');
} else {
if (@file_exists(dirname(__FILE__) . $this->name . '/key.php')) {
@require_once(dirname(__FILE__) . $this->name . '/key.php');
} else {
if (@file_exists('modules/' . $this->name . '/key.php')) {
@require_once('modules/' . $this->name . '/key.php');
}
}
}
if ($form == 1) {
return '
<div class="panel" id="fieldset_myprestaupdates" style="margin-top:20px;">
' . ($this->psversion() == 6 || $this->psversion() == 7 ? '<div class="panel-heading"><i class="icon-wrench"></i> ' . $this->l('MyPresta updates') . '</div>' : '') . '
<div class="form-wrapper" style="padding:0px!important;">
<div id="module_block_settings">
<fieldset id="fieldset_module_block_settings">
' . ($this->psversion() == 5 ? '<legend style="">' . $this->l('MyPresta updates') . '</legend>' : '') . '
<form action="' . $_SERVER['REQUEST_URI'] . '" method="post">
<label>' . $this->l('Check updates') . '</label>
<div class="margin-form">' . (Tools::isSubmit('submit_settings_updates_now') ? ($this->inconsistency(0) ? '' : '') . $this->checkforupdates(1) : '') . '
<button style="margin: 0px; top: -3px; position: relative;" type="submit" name="submit_settings_updates_now" class="button btn btn-default" />
<i class="process-icon-update"></i>
' . $this->l('Check now') . '
</button>
</div>
<label>' . $this->l('Updates notifications') . '</label>
<div class="margin-form">
<select name="mypresta_updates">
<option value="-">' . $this->l('-- select --') . '</option>
<option value="1" ' . ((int)(Configuration::get('mypresta_updates') == 1) ? 'selected="selected"' : '') . '>' . $this->l('Enable') . '</option>
<option value="0" ' . ((int)(Configuration::get('mypresta_updates') == 0) ? 'selected="selected"' : '') . '>' . $this->l('Disable') . '</option>
</select>
<p class="clear">' . $this->l('Turn this option on if you want to check MyPresta.eu for module updates automatically. This option will display notification about new versions of this addon.') . '</p>
</div>
<label>' . $this->l('Module page') . '</label>
<div class="margin-form">
<a style="font-size:14px;" href="' . $this->mypresta_link . '" target="_blank">' . $this->displayName . '</a>
<p class="clear">' . $this->l('This is direct link to official addon page, where you can read about changes in the module (changelog)') . '</p>
</div>
<div class="panel-footer">
<button type="submit" name="submit_settings_updates"class="button btn btn-default pull-right" />
<i class="process-icon-save"></i>
' . $this->l('Save') . '
</button>
</div>
</form>
</fieldset>
<style>
#fieldset_myprestaupdates {
display:block;clear:both;
float:inherit!important;
}
</style>
</div>
</div>
</div>';
} else {
if (defined('_PS_ADMIN_DIR_')) {
if (Tools::isSubmit('submit_settings_updates')) {
Configuration::updateValue('mypresta_updates', Tools::getValue('mypresta_updates'));
}
if (Configuration::get('mypresta_updates') != 0 || (bool)Configuration::get('mypresta_updates') == false) {
if (Configuration::get('update_' . $this->name) < (date("U") - 259200)) {
$actual_version = cmsproductsproUpdate::verify($this->name, (isset($this->mkey) ? $this->mkey : 'nokey'), $this->version);
}
if (cmsproductsproUpdate::version($this->version) < cmsproductsproUpdate::version(Configuration::get('updatev_' . $this->name))) {
$this->warning = $this->l('New version available, check http://MyPresta.eu for more informations');
}
}
if ($display_msg == 1) {
if (cmsproductsproUpdate::version($this->version) < cmsproductsproUpdate::version(cmsproductsproUpdate::verify($this->name, (isset($this->mkey) ? $this->mkey : 'nokey'), $this->version))) {
return "<span style='color:red; font-weight:bold; font-size:16px; margin-right:10px;'>" . $this->l('New version available!') . "</span>";
} else {
return "<span style='color:green; font-weight:bold; font-size:16px; margin-right:10px;'>" . $this->l('Module is up to date!') . "</span>";
}
}
}
}
}
public function install()
{
if (parent::install() && $this->registerHook('displayHeader') && $this->registerHook('actionAjaxDieProductControllerdisplayAjaxQuickviewBefore') && $this->registerHook('actionOutputHTMLBefore')) {
return true;
}
}
public function psversion($part = 1)
{
$version = _PS_VERSION_;
$exp = $explode = explode(".", $version);
if ($part == 1) {
return $exp[1];
}
if ($part == 2) {
return $exp[2];
}
if ($part == 3) {
return $exp[3];
}
}
public function inconsistency($ret)
{
return;
}
public function hookdisplayHeader($params)
{
$this->context->controller->addCSS(_THEME_CSS_DIR_ . 'product_list.css', 'all');
$this->context->controller->addCSS($this->_path . 'cmsproductspro.css', 'all');
}
public function getContent()
{
$output = '';
if (Tools::isSubmit('cmsproductspro_hide') && Tools::isSubmit('btnSubmit')) {
Configuration::updateValue('cmsproductspro_hide', Tools::getValue('cmsproductspro_hide'));
$this->context->controller->confirmations[] = $this->l('Successful update');
}
$output = "";
return $output . $this->displayForm();
}
public function displayForm()
{
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->l('Module settings'),
'icon' => 'icon-cubes'
),
'input' => array(
array(
'type' => 'select',
'label' => $this->l('Disable all shortcodes'),
'name' => 'cmsproductspro_hide',
'lang' => false,
'desc' => $this->l('Option when enabled disables all shortcodes - so feeds created with shortcode and shortcodes will be hidden'),
'options' => array(
'query' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('On')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Off')
)
),
'id' => 'value',
'name' => 'label'
)
),
),
'submit' => array(
'title' => $this->l('Save'),
)
),
);
$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;
$helper->identifier = $this->identifier;
$helper->submit_action = 'btnSubmit';
$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
);
$this->context->smarty->assign(array(
'link' => $this->context->link,
));
return $helper->generateForm(array($fields_form)) . $this->checkforupdates(0, 1);
}
public function getConfigFieldsValues()
{
return array(
'cmsproductspro_hide' => Tools::getValue('cmsproductspro_hide', Configuration::get('cmsproductspro_hide')),
);
}
public static function getImagesByID($id_product, $limit = 0)
{
$id_image = Db::getInstance()->ExecuteS('SELECT `id_image` FROM `' . _DB_PREFIX_ . 'image` WHERE cover=1 AND `id_product` = ' . (int)$id_product . ' ORDER BY position ASC LIMIT 0, ' . (int)$limit);
$toReturn = array();
if ( ! $id_image) {
return null;
} else {
foreach ($id_image as $image) {
$toReturn[] = $id_product . '-' . $image['id_image'];
}
}
return $toReturn;
}
public static function getAllProductImagesByID($id_product, $limit = 0)
{
$id_image = Db::getInstance()->ExecuteS('SELECT `id_image` FROM `' . _DB_PREFIX_ . 'image` WHERE `id_product` = ' . (int)$id_product . ' ORDER BY position ASC LIMIT 0, ' . (int)$limit);
$toReturn = array();
if ( ! $id_image) {
return null;
} else {
foreach ($id_image as $image) {
$toReturn[] = $id_product . '-' . $image['id_image'];
}
}
return $toReturn;
}
public function returnProductDescription($id_product = 0)
{
if ($id_product > 0) {
$product = new Product($id_product, false, $this->context->language->id);
return $product->description;
}
return '';
}
public function returnProductDescriptionShort($id_product)
{
if ($id_product > 0) {
$product = new Product($id_product, false, $this->context->language->id);
return $product->description_short;
}
return '';
}
public function returnProduct($id_product)
{
$x = new Product($id_product, true, $this->context->language->id);
if (Validate::isLoadedObject($x)) {
$productss[$id_product] = (array)$x;
$productss[$id_product]['id_product'] = $id_product;
}
$products = Product::getProductsProperties($this->context->language->id, $productss);
$assembler = new ProductAssembler($this->context);
$presenterFactory = new ProductPresenterFactory($this->context);
$presentationSettings = $presenterFactory->getPresentationSettings();
$presenter = new ProductListingPresenter(new ImageRetriever($this->context->link), $this->context->link, new PriceFormatter(), new ProductColorsRetriever(), $this->context->getTranslator());
$products_for_template = [];
foreach ($products as $rawProduct) {
$products_for_template[] = $presenter->present($presentationSettings, $assembler->assembleProduct($rawProduct), $this->context->language);
}
$this->context->smarty->assign('products', $products_for_template);
$this->context->smarty->assign('feedtype', "cmsSingleProductFeed");
return $this->context->smarty->fetch('module:cmsproductspro/products.tpl');
}
public function returnProducts($id_product)
{
$explode_products = explode(",", $id_product);
foreach ($explode_products AS $idp) {
$explode[] = $idp;
foreach ($explode as $tproduct) {
if ($tproduct != '') {
$x = new Product((int)$tproduct, true, $this->context->language->id);
if (Validate::isLoadedObject($x)) {
$productss[$tproduct] = (array)$x;
$productss[$tproduct]['id_product'] = $tproduct;
}
}
}
}
$products = Product::getProductsProperties($this->context->language->id, $productss);
$assembler = new ProductAssembler($this->context);
$presenterFactory = new ProductPresenterFactory($this->context);
$presentationSettings = $presenterFactory->getPresentationSettings();
$presenter = new ProductListingPresenter(new ImageRetriever($this->context->link), $this->context->link, new PriceFormatter(), new ProductColorsRetriever(), $this->context->getTranslator());
$products_for_template = [];
foreach ($products as $rawProduct) {
$products_for_template[] = $presenter->present($presentationSettings, $assembler->assembleProduct($rawProduct), $this->context->language);
}
$this->context->smarty->assign('products', ($this->psversion() == 7 ? $products_for_template : $products));
$this->context->smarty->assign('feedtype', "cmsProductsFeed");
return $this->context->smarty->fetch('module:cmsproductspro/products.tpl');
}
public function returnProductsHpp($block)
{
$hpp = Module::getInstanceByName('hpp');
if ($hpp != false) {
if (method_exists($hpp, 'returnProducts')) {
return $this->displayHpp($hpp->returnProducts($block));
} else {
return $this->noModuleMessage("Homepage Products Pro");
}
} else {
return $this->noModuleMessage("Homepage Products Pro");
}
}
public function returnProductsRpp($block)
{
$rpp = Module::getInstanceByName('ppb');
if ($rpp != false) {
if (method_exists($rpp, 'returnProducts')) {
return $this->displayRpp($rpp->returnProducts($block));
} else {
return $this->noModuleMessage("Related Products Pro");
}
} else {
return $this->noModuleMessage("Related Products Pro");
}
}
public function returnManufacturerProducts($id_manufacturer, $nb, $sort, $sortway)
{
$manufacturerProducts = self::getManufacturerProducts($id_manufacturer, $this->context->language->id, 0, $nb, $sort, $sortway, false, true, true);
if (trim(strtolower($sortway)) == 'random') {
shuffle($manufacturerProducts);
}
$products = Product::getProductsProperties($this->context->language->id, $manufacturerProducts);
if (count($products) > 0) {
$this->context->smarty->assign('products', $this->assembler($products));
$this->context->smarty->assign('feedtype', "cmsProductsFeed");
} else {
$this->context->smarty->assign('feedtype', "noProducts");
}
$contents = $this->context->smarty->fetch('module:cmsproductspro/products.tpl');
return $contents;
}
public function returnSupplierProducts($id_supplier, $nb, $sort, $sortway)
{
$supplierProducts = self::getSupplierProducts($id_supplier, $this->context->language->id, 0, $nb, $sort, $sortway, false, true, true);
if (trim(strtolower($sortway)) == 'random') {
shuffle($supplierProducts);
}
$products = Product::getProductsProperties($this->context->language->id, $supplierProducts);
if (count($products) > 0) {
$this->context->smarty->assign('products', $this->assembler($products));
$this->context->smarty->assign('feedtype', "cmsProductsFeed");
} else {
$this->context->smarty->assign('feedtype', "noProducts");
}
$contents = $this->context->smarty->fetch('module:cmsproductspro/products.tpl');
return $contents;
}
public function returnCategoryProducts($id_category, $nb, $sort, $sortway)
{
$categoryQuery = new Category($id_category);
if ($sort != 'random') {
$categoryProducts = $categoryQuery->getProducts($this->context->language->id, 0, $nb, $sort, $sortway); //grab all the products associated with the specified category id
} else {
$categoryProducts = $categoryQuery->getProducts($this->context->language->id, 0, $nb, null, null, false, true, true, $nb); //grab all the products associated with the specified category id
}
$products = Product::getProductsProperties($this->context->language->id, $categoryProducts);
if (count($products) > 0) {
$this->context->smarty->assign('products', $this->assembler($products));
$this->context->smarty->assign('feedtype', "cmsProductsFeed");
} else {
$this->context->smarty->assign('feedtype', "noProducts");
}
$contents = $this->context->smarty->fetch('module:cmsproductspro/products.tpl');
return $contents;
}
public function returnProductGallery($id_product, $nb = 4, $image_type = 'home_default')
{
$productGallery = new Product($id_product, true, $this->context->language->id);
$pictures = $this->getAllProductImagesByID($id_product, $nb);
$Link = Context::getContext()->link;
$this->context->smarty->assign('feedtype', "gallery");
$this->context->smarty->assign('productGallery', $productGallery);
$this->context->smarty->assign('pictures', $pictures);
$this->context->smarty->assign('link', $Link);
$this->context->smarty->assign('image_type', $image_type);
$contents = $this->context->smarty->fetch('module:cmsproductspro/productGallery.tpl');
return $contents;
}
public function displayRpp($products)
{
if (count($products) <= 0) {
$this->context->smarty->assign('feedtype', "noProducts");
} else {
$this->context->smarty->assign('products', $products);
$this->context->smarty->assign('feedtype', "rppfeed");
}
$contents = $this->context->smarty->fetch(_PS_MODULE_DIR_ . 'cmsproductspro/products.tpl');
return $contents;
}
public function displayHpp($products)
{
if (count($products) <= 0) {
$this->context->smarty->assign('feedtype', "noProducts");
} else {
$this->context->smarty->assign('products', $products);
$this->context->smarty->assign('feedtype', "hppfeed");
}
$contents = $this->context->smarty->fetch(_PS_MODULE_DIR_ . 'cmsproductspro/products.tpl');
return $contents;
}
public function noModuleMessage($module)
{
$this->context->smarty->assign('module', $module);
$this->context->smarty->assign('feedtype', "error");
$contents = $this->context->smarty->fetch(_PS_MODULE_DIR_ . 'cmsproductspro/products.tpl');
return $contents;
}
public function returnManufacturerDetail($id, $what)
{
$manufacturer = new Manufacturer($id, $this->context->language->id);
if (isset($manufacturer->id)) {
if (is_int($manufacturer->id)) {
if ($what == 'link') {
return $this->context->link->getManufacturerLink($manufacturer);
} elseif ($what == 'image_link') {
return $this->context->shop->getBaseURL() . 'img/m/' . $id . '.jpg';
} elseif ($what == 'picture') {
$this->context->smarty->assign('id_manufacturer', $id);
return $this->context->smarty->fetch(_PS_MODULE_DIR_ . 'cmsproductspro/imageManufacturer.tpl');
} else {
if (isset($manufacturer->{$what})) {
return $manufacturer->{$what};
}
}
}
}
}
public static function getManufacturerProducts($id_manufacturer, $id_lang, $p, $n, $order_by = null, $order_way = null, $get_total = false, $active = true, $active_category = true, Context $context = null)
{
if ( ! $context) {
$context = Context::getContext();
}
$front = true;
if ( ! in_array($context->controller->controller_type, array(
'front',
'modulefront'
))) {
$front = false;
}
if ($p < 1) {
$p = 1;
}
if (empty($order_by) || $order_by == 'position') {
$order_by = 'name';
}
if (empty($order_way)) {
$order_way = 'ASC';
}
$groups = FrontController::getCurrentCustomerGroups();
$sql_groups = count($groups) ? 'IN (' . implode(',', $groups) . ')' : '= 1';
/* Return only the number of products */
if ($get_total) {
$sql = '
SELECT p.`id_product`
FROM `' . _DB_PREFIX_ . 'product` p
' . Shop::addSqlAssociation('product', 'p') . '
WHERE p.id_manufacturer = ' . (int)$id_manufacturer . ($active ? ' AND product_shop.`active` = 1' : '') . '
' . ($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '') . '
AND EXISTS (
SELECT 1
FROM `' . _DB_PREFIX_ . 'category_group` cg
LEFT JOIN `' . _DB_PREFIX_ . 'category_product` cp ON (cp.`id_category` = cg.`id_category`)' . ($active_category ? ' INNER JOIN `' . _DB_PREFIX_ . 'category` ca ON cp.`id_category` = ca.`id_category` AND ca.`active` = 1' : '') . '
WHERE p.`id_product` = cp.`id_product` AND cg.`id_group` ' . $sql_groups . '
)';
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
return (int)count($result);
}
if (strpos($order_by, '.') > 0) {
$order_by = explode('.', $order_by);
$order_by = pSQL($order_by[0]) . '.`' . pSQL($order_by[1]) . '`';
}
$alias = '';
if ($order_by == 'price') {
$alias = 'product_shop.';
} elseif ($order_by == 'name') {
$alias = 'pl.';
} elseif ($order_by == 'manufacturer_name') {
$order_by = 'name';
$alias = 'm.';
} elseif ($order_by == 'quantity') {
$alias = 'stock.';
} else {
$alias = 'p.';
}
$sql = 'SELECT p.*, product_shop.*, stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity' . (Combination::isFeatureActive() ? ', product_attribute_shop.minimal_quantity AS product_attribute_minimal_quantity, IFNULL(product_attribute_shop.`id_product_attribute`,0) id_product_attribute' : '') . '
, pl.`description`, pl.`description_short`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`,
pl.`meta_title`, pl.`name`, pl.`available_now`, pl.`available_later`, image_shop.`id_image` id_image, il.`legend`, m.`name` AS manufacturer_name,
DATEDIFF(
product_shop.`date_add`,
DATE_SUB(
"' . date('Y-m-d') . ' 00:00:00",
INTERVAL ' . (Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20) . ' DAY
)
) > 0 AS new' . ' FROM `' . _DB_PREFIX_ . 'product` p
' . Shop::addSqlAssociation('product', 'p') . (Combination::isFeatureActive() ? 'LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute_shop` product_attribute_shop
ON (p.`id_product` = product_attribute_shop.`id_product` AND product_attribute_shop.`default_on` = 1 AND product_attribute_shop.id_shop=' . (int)$context->shop->id . ')' : '') . '
LEFT JOIN `' . _DB_PREFIX_ . 'product_lang` pl
ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = ' . (int)$id_lang . Shop::addSqlRestrictionOnLang('pl') . ')
LEFT JOIN `' . _DB_PREFIX_ . 'image_shop` image_shop
ON (image_shop.`id_product` = p.`id_product` AND image_shop.cover=1 AND image_shop.id_shop=' . (int)$context->shop->id . ')
LEFT JOIN `' . _DB_PREFIX_ . 'image_lang` il
ON (image_shop.`id_image` = il.`id_image` AND il.`id_lang` = ' . (int)$id_lang . ')
LEFT JOIN `' . _DB_PREFIX_ . 'manufacturer` m
ON (m.`id_manufacturer` = p.`id_manufacturer`)
' . Product::sqlStock('p', 0);
if (Group::isFeatureActive() || $active_category) {
$sql .= 'JOIN `' . _DB_PREFIX_ . 'category_product` cp ON (p.id_product = cp.id_product)';
if (Group::isFeatureActive()) {
$sql .= 'JOIN `' . _DB_PREFIX_ . 'category_group` cg ON (cp.`id_category` = cg.`id_category` AND cg.`id_group` ' . $sql_groups . ')';
}
if ($active_category) {
$sql .= 'JOIN `' . _DB_PREFIX_ . 'category` ca ON cp.`id_category` = ca.`id_category` AND ca.`active` = 1';
}
}
$sql .= '
WHERE p.`id_manufacturer` = ' . (int)$id_manufacturer . '
' . ($active ? ' AND product_shop.`active` = 1' : '') . '
' . ($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '') . '
GROUP BY p.id_product
ORDER BY ' . (trim(strtolower($order_way)) == 'random' ? 'RAND() ' : '' . $alias . '`' . bqSQL($order_by) . '` ' . pSQL($order_way)) . '
LIMIT ' . (((int)$p - 1) * (int)$n) . ',' . (int)$n;
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
if ( ! $result) {
return false;
}
if ($order_by == 'price') {
Tools::orderbyPrice($result, $order_way);
}
return Product::getProductsProperties($id_lang, $result);
}
public static function getSupplierProducts($id_supplier, $id_lang, $p, $n, $order_by = null, $order_way = null, $get_total = false, $active = true, $active_category = true)
{
$context = Context::getContext();
$front = true;
if ( ! in_array($context->controller->controller_type, array(
'front',
'modulefront'
))) {
$front = false;
}
if ($p < 1) {
$p = 1;
}
if (empty($order_by) || $order_by == 'position') {
$order_by = 'name';
}
if (empty($order_way)) {
$order_way = 'ASC';
}
$sql_groups = '';
if (Group::isFeatureActive()) {
$groups = FrontController::getCurrentCustomerGroups();
$sql_groups = 'WHERE cg.`id_group` ' . (count($groups) ? 'IN (' . implode(',', $groups) . ')' : '= 1');
}
/* Return only the number of products */
if ($get_total) {
return (int)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
SELECT COUNT(DISTINCT ps.`id_product`)
FROM `' . _DB_PREFIX_ . 'product_supplier` ps
JOIN `' . _DB_PREFIX_ . 'product` p ON (ps.`id_product`= p.`id_product`)
' . Shop::addSqlAssociation('product', 'p') . '
WHERE ps.`id_supplier` = ' . (int)$id_supplier . '
AND ps.id_product_attribute = 0
' . ($active ? ' AND product_shop.`active` = 1' : '') . '
' . ($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '') . '
AND p.`id_product` IN (
SELECT cp.`id_product`
FROM `' . _DB_PREFIX_ . 'category_product` cp
' . (Group::isFeatureActive() ? 'LEFT JOIN `' . _DB_PREFIX_ . 'category_group` cg ON (cp.`id_category` = cg.`id_category`)' : '') . '
' . ($active_category ? ' INNER JOIN `' . _DB_PREFIX_ . 'category` ca ON cp.`id_category` = ca.`id_category` AND ca.`active` = 1' : '') . '
' . $sql_groups . '
)');
}
$nb_days_new_product = Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20;
if (strpos('.', $order_by) > 0) {
$order_by = explode('.', $order_by);
$order_by = pSQL($order_by[0]) . '.`' . pSQL($order_by[1]) . '`';
}
$alias = '';
if (in_array($order_by, array(
'price',
'date_add',
'date_upd'
))) {
$alias = 'product_shop.';
} elseif ($order_by == 'id_product') {
$alias = 'p.';
} elseif ($order_by == 'manufacturer_name') {
$order_by = 'name';
$alias = 'm.';
}
$sql = 'SELECT p.*, product_shop.*, stock.out_of_stock,
IFNULL(stock.quantity, 0) as quantity,
pl.`description`,
pl.`description_short`,
pl.`link_rewrite`,
pl.`meta_description`,
pl.`meta_keywords`,
pl.`meta_title`,
pl.`name`,
image_shop.`id_image` id_image,
il.`legend`,
s.`name` AS supplier_name,
DATEDIFF(p.`date_add`, DATE_SUB("' . date('Y-m-d') . ' 00:00:00", INTERVAL ' . ($nb_days_new_product) . ' DAY)) > 0 AS new,
m.`name` AS manufacturer_name' . (Combination::isFeatureActive() ? ', product_attribute_shop.minimal_quantity AS product_attribute_minimal_quantity, IFNULL(product_attribute_shop.id_product_attribute,0) id_product_attribute' : '') . '
FROM `' . _DB_PREFIX_ . 'product` p
' . Shop::addSqlAssociation('product', 'p') . '
JOIN `' . _DB_PREFIX_ . 'product_supplier` ps ON (ps.id_product = p.id_product
AND ps.id_product_attribute = 0) ' . (Combination::isFeatureActive() ? 'LEFT JOIN `' . _DB_PREFIX_ . 'product_attribute_shop` product_attribute_shop
ON (p.`id_product` = product_attribute_shop.`id_product` AND product_attribute_shop.`default_on` = 1 AND product_attribute_shop.id_shop=' . (int)$context->shop->id . ')' : '') . '
LEFT JOIN `' . _DB_PREFIX_ . 'product_lang` pl ON (p.`id_product` = pl.`id_product`
AND pl.`id_lang` = ' . (int)$id_lang . Shop::addSqlRestrictionOnLang('pl') . ')
LEFT JOIN `' . _DB_PREFIX_ . 'image_shop` image_shop
ON (image_shop.`id_product` = p.`id_product` AND image_shop.cover=1 AND image_shop.id_shop=' . (int)$context->shop->id . ')
LEFT JOIN `' . _DB_PREFIX_ . 'image_lang` il ON (image_shop.`id_image` = il.`id_image`
AND il.`id_lang` = ' . (int)$id_lang . ')
LEFT JOIN `' . _DB_PREFIX_ . 'supplier` s ON s.`id_supplier` = p.`id_supplier`
LEFT JOIN `' . _DB_PREFIX_ . 'manufacturer` m ON m.`id_manufacturer` = p.`id_manufacturer`
' . Product::sqlStock('p', 0);
if (Group::isFeatureActive() || $active_category) {
$sql .= 'JOIN `' . _DB_PREFIX_ . 'category_product` cp ON (p.id_product = cp.id_product)';
if (Group::isFeatureActive()) {
$sql .= 'JOIN `' . _DB_PREFIX_ . 'category_group` cg ON (cp.`id_category` = cg.`id_category` AND cg.`id_group` ' . (count($groups) ? 'IN (' . implode(',', $groups) . ')' : '= 1') . ')';
}
if ($active_category) {
$sql .= 'JOIN `' . _DB_PREFIX_ . 'category` ca ON cp.`id_category` = ca.`id_category` AND ca.`active` = 1';
}
}
$sql .= '
WHERE ps.`id_supplier` = ' . (int)$id_supplier . '
' . ($active ? ' AND product_shop.`active` = 1' : '') . '
' . ($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '') . '
GROUP BY ps.id_product
ORDER BY ' . (trim(strtolower($order_way)) == 'random' ? 'RAND() ' : '' . $alias . '`' . bqSQL($order_by) . '` ' . pSQL($order_way)) . '
LIMIT ' . (((int)$p - 1) * (int)$n) . ',' . (int)$n;
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql, true, false);
if ( ! $result) {
return false;
}
if ($order_by == 'price') {
Tools::orderbyPrice($result, $order_way);
}
return Product::getProductsProperties($id_lang, $result);
}
public function hookactionAjaxDieProductControllerdisplayAjaxQuickviewBefore($params)
{
$html = json_decode($params['value']);
$html->quickview_html = $this->returnContent($html->quickview_html);
die(json_encode($html));
}
public function returnProductsAttribute($id_product)
{
$explode_products = explode(",", $id_product);
foreach ($explode_products AS $idp) {
$explode[] = $idp;
foreach ($explode as $tproduct) {
$tproduct_array = explode('-', $tproduct);
if (isset($tproduct_array[0]) && isset($tproduct_array[1]))
{
if ($tproduct_array[0] != '' && $tproduct_array[1] != '') {
$x = new Product((int)$tproduct_array[0], true, $this->context->language->id);
if (Validate::isLoadedObject($x)) {
$productss[$tproduct] = (array)$x;
$productss[$tproduct]['id_product'] = $tproduct_array[0];
$productss[$tproduct]['id_product_attribute'] = $tproduct_array[1];
}
}
}
}
}
$products = Product::getProductsProperties($this->context->language->id, $productss);
$assembler = new ProductAssembler($this->context);
$presenterFactory = new ProductPresenterFactory($this->context);
$presentationSettings = $presenterFactory->getPresentationSettings();
$presenter = new ProductListingPresenter(new ImageRetriever($this->context->link), $this->context->link, new PriceFormatter(), new ProductColorsRetriever(), $this->context->getTranslator());
$products_for_template = [];
foreach ($products as $rawProduct) {
$products_for_template[] = $presenter->present($presentationSettings, $assembler->assembleProduct($rawProduct), $this->context->language);
}
$this->context->smarty->assign('products', ($this->psversion() == 7 ? $products_for_template : $products));
$this->context->smarty->assign('feedtype', "cmsProductsFeed");
return $this->context->smarty->fetch('module:cmsproductspro/products.tpl');
}
public static function getImagesByIdAttribute($id_product, $limit = 0, $id_product_attribute = 0)
{
$id_image = Db::getInstance()->ExecuteS('SELECT i.`id_image` FROM `' . _DB_PREFIX_ . 'image` AS i
INNER JOIN `' . _DB_PREFIX_ . 'product_attribute_image` AS pai ON (pai.`id_image` = i.`id_image`)
WHERE i.`id_product` = '.$id_product.' AND pai.`id_product_attribute`= '.$id_product_attribute.' ORDER BY i.position ASC LIMIT 0, ' . (int)$limit);
$toReturn = array();
if (!$id_image)
{
return null;
}
else
{
foreach ($id_image as $image)
{
$toReturn[] = $id_product . '-' . $image['id_image'];
}
}
return $toReturn;
}
public function returnContent($contents)
{
if (Tools::getValue('controller') == 'product') {
preg_match_all('/\sdata\-product\=\"\{(.*)\}\"[\s]+role\=/', $contents, $matches);
foreach ($matches[0] as $index => $match) {
$matched = preg_replace('/\[([a-z0-9\-\:\,\_]+)\]/', '', $match);
$contents = str_replace($match, $matched, $contents);
}
}
preg_match_all('/id\=\"product\-details\"\sdata\-product\=\"\{(.*)\}\"/', $contents, $matches);
if (isset($matches[0][0])) {
$matched = preg_replace('/\[([a-z0-9\-\:\,\_]+)\]/', '', $matches[0][0]);
$contents = str_replace($matches[0][0], $matched, $contents);
}
preg_match_all('/\<head\>(.*)\<\/head\>/is', $contents, $matches);
if (isset($matches[0][0])) {
$matched_new = preg_replace('/\[([a-z0-9\-\:\,\_]+)\]/', '', $matches[0][0]);
$contents = str_replace($matches[0][0], $matched_new, $contents);
}
preg_match_all('/\[product\-description\:[(0-9)]+\]/i', $contents, $matches);
foreach ($matches[0] as $index => $match) {
$explode = explode(":", $match);
$contents = str_replace($match, $this->returnProductDescription(str_replace("]", "", $explode[1])), $contents);
}
preg_match_all('/\[productsByAttributes\:[(0-9\-\,)]+\]/i', $contents, $matches);
foreach ($matches[0] as $index => $match)
{
$explode = explode(":", $match);
$contents = str_replace($match, $this->returnProductsAttribute(str_replace("]", "", $explode[1])), $contents);
}
preg_match_all('/\[products\:[(0-9\,)]+\]/i', $contents, $matches);
foreach ($matches[0] as $index => $match) {
$explode = explode(":", $match);
$contents = str_replace($match, $this->returnProducts(str_replace("]", "", $explode[1])), $contents);
}
preg_match_all('/\[product\:[(0-9\,)]+\]/i', $contents, $matches);
foreach ($matches[0] as $index => $match) {
$explode = explode(":", $match);
$contents = str_replace($match, $this->returnProduct(str_replace("]", "", $explode[1])), $contents);
}
preg_match_all('/\[product\-description\-short\:[(0-9\,)]+\]/i', $contents, $matches);
foreach ($matches[0] as $index => $match) {
$explode = explode(":", $match);
$contents = str_replace($match, $this->returnProductDescriptionShort(str_replace("]", "", $explode[1])), $contents);
}
preg_match_all('/\[hpp\:[(0-9)]+\]/i', $contents, $matches);
foreach ($matches[0] as $index => $match) {
$explode = explode(":", $match);
$contents = str_replace($match, $this->returnProductsHpp(str_replace("]", "", $explode[1])), $contents);
}
preg_match_all('/\[rpp\:[(0-9)]+\]/i', $contents, $matches);
foreach ($matches[0] as $index => $match) {
$explode = explode(":", $match);
$contents = str_replace($match, $this->returnProductsRpp(str_replace("]", "", $explode[1])), $contents);
}
preg_match_all('/\[category\:[(0-9)]+\:[(0-9)]+\:[(A-Z\_)]+\:[(A-Z\_)]+\]/i', $contents, $matches);
foreach ($matches[0] as $index => $match) {
$explode = explode(":", $match);
$contents = str_replace($match, $this->returnCategoryProducts($explode[1], str_replace("]", "", $explode[2]), str_replace("]", "", $explode[3]), str_replace("]", "", $explode[4])), $contents);
}
preg_match_all('/\[productgallery\:[(0-9)]+\:[(0-9)]+\:[(A-Z\_\-)]+\]/i', $contents, $matches);
foreach ($matches[0] as $index => $match) {
$explode = explode(":", $match);
$contents = str_replace($match, $this->returnProductGallery(str_replace("]", "", $explode[1]), str_replace("]", "", $explode[2]), str_replace("]", "", $explode[3])), $contents);
}
preg_match_all('/\[manufacturer\:[(0-9)]+\:[(0-9)]+\:[(A-Z\_)]+\:[(A-Z\_)]+\]/i', $contents, $matches);
foreach ($matches[0] as $index => $match) {
$explode = explode(":", $match);
$contents = str_replace($match, $this->returnManufacturerProducts($explode[1], str_replace("]", "", $explode[2]), str_replace("]", "", $explode[3]), str_replace("]", "", $explode[4])), $contents);
}
preg_match_all('/\[manufacturer\:[(0-9)]+\:(short_description|description|name|link|picture|image_link)/i', $contents, $matches);
foreach ($matches[0] as $index => $match) {
$explode = explode(":", $match);
$explode[2] = trim(strtolower($explode[2]));
if ($explode[2] == 'short_description' || $explode[2] == 'description' || $explode[2] == 'name' || $explode[2] == 'link' || $explode[2] == 'image_link' || $explode[2] == 'picture') {
$contents = str_replace($match . ']', $this->returnManufacturerDetail($explode[1], $explode[2]), $contents);
}
}
preg_match_all('/\[supplier\:[(0-9)]+\:[(0-9)]+\:[(A-Z\_)]+\:[(A-Z\_)]+\]/i', $contents, $matches);
foreach ($matches[0] as $index => $match) {
$explode = explode(":", $match);
$contents = str_replace($match, $this->returnSupplierProducts($explode[1], str_replace("]", "", $explode[2]), str_replace("]", "", $explode[3]), str_replace("]", "", $explode[4])), $contents);
}
return $contents;
}
public function assembler($products)
{
$assembler = new ProductAssembler($this->context);
$presenterFactory = new ProductPresenterFactory($this->context);
$presentationSettings = $presenterFactory->getPresentationSettings();
$presenter = new ProductListingPresenter(new ImageRetriever($this->context->link), $this->context->link, new PriceFormatter(), new ProductColorsRetriever(), $this->context->getTranslator());
$products_for_template = [];
foreach ($products as $rawProduct) {
$products_for_template[] = $presenter->present($presentationSettings, $assembler->assembleProduct($rawProduct), $this->context->language);
}
return $products_for_template;
}
}
class cmsproductsproUpdate extends cmsproductspro
{
public static function version($version)
{
$version = (int)str_replace(".", "", $version);
if (strlen($version) == 3) {
$version = (int)$version . "0";
}
if (strlen($version) == 2) {
$version = (int)$version . "00";
}
if (strlen($version) == 1) {
$version = (int)$version . "000";
}
if (strlen($version) == 0) {
$version = (int)$version . "0000";
}
return (int)$version;
}
public static function encrypt($string)
{
return base64_encode($string);
}
public static function verify($module, $key, $version)
{
if (ini_get("allow_url_fopen")) {
if (function_exists("file_get_contents")) {
$actual_version = @file_get_contents('http://dev.mypresta.eu/update/get.php?module=' . $module . "&version=" . self::encrypt($version) . "&lic=$key&u=" . self::encrypt(_PS_BASE_URL_ . __PS_BASE_URI__));
}
}
Configuration::updateValue("update_" . $module, date("U"));
Configuration::updateValue("updatev_" . $module, $actual_version);
return $actual_version;
}
}
?>