Files
bio-concept-pharma/web/modules/pssitemap/classes/pssitemapDataClass.php
2019-11-17 19:14:07 +01:00

333 lines
12 KiB
PHP

<?php
/**
* 2007-2018 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2018 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
class PssitemapData
{
/**
* @param id_shop
* @param id_lang
**/
public static function getCategoriesShop($id_shop, $id_lang)
{
$query = "SELECT cat.id_category, cats.name FROM `"._DB_PREFIX_."category` AS cat INNER JOIN `"._DB_PREFIX_."category_lang` AS cats ON(cat.id_category = cats.id_category) WHERE id_shop = ".(int)$id_shop." AND id_lang = ".(int)$id_lang." AND active=1";
return DB::getInstance()->executeS($query);
}
/**
* @param id_shop
* @param id_lang
**/
public static function getProductsShop($id_shop, $id_lang)
{
// todo : get active elements only
$query = "SELECT pl.id_product, p.name, pp.export
FROM `"._DB_PREFIX_."product` AS pl
INNER JOIN `"._DB_PREFIX_."product_lang` AS p ON(pl.id_product = p.id_product)
INNER JOIN `"._DB_PREFIX_."pssitemap_product` AS pp ON(p.id_product = pp.id_element)
WHERE p.id_shop = ".(int)$id_shop."
AND p.id_lang = ".(int)$id_lang."
AND active=1";
return DB::getInstance()->executeS($query);
}
/**
* @param id_shop
* @param id_lang
**/
public static function getCMSPage($id_shop, $id_lang)
{
$query = "SELECT id_cms, meta_title FROM `"._DB_PREFIX_."cms_lang` WHERE id_shop = ".(int)$id_shop." AND id_lang = ".(int)$id_lang;
return DB::getInstance()->executeS($query);
}
/**
* @param id_shop
* @param id_lang
**/
public static function getOtherPages($id_shop, $id_lang)
{
$query = "SELECT id_meta, title from `"._DB_PREFIX_."meta_lang` WHERE `title` <> '' AND id_shop = ".(int)$id_shop." AND id_lang = ".(int)$id_lang;
return DB::getInstance()->executeS($query);
}
/**
* @param id_element
* @param type [Product, CMS, Category or other page]
* @param export (0 or 1)
*/
public static function updateExportElement($id_element, $type, $export)
{
$id_shop = (int)Context::getContext()->shop->id;
$query = 'INSERT INTO `'._DB_PREFIX_.'pssitemap_'.pSQL($type).'` (`id`, `id_element`, `id_shop`, `name`, `export`) VALUES ('.(int)$id_element.', '.(int)$id_element.', '.(int)$id_shop.', "'.pSQL($type).'", '.pSQL($export).') ON DUPLICATE KEY UPDATE `id_element`='.(int)$id_element.', `id_shop`='.(int)$id_shop.', `name`="'.pSQL($type).'" , `export`='.(int)$export.' ';
return Db::getInstance()->Execute($query);
}
/**
* @param id_shop
**/
public static function getCategoryToExport($id_shop)
{
return Db::getInstance()->ExecuteS('SELECT id_element FROM `'._DB_PREFIX_.'pssitemap_category` WHERE export = 1 and id_shop = '.(int)$id_shop);
}
/**
* @param id_shop
**/
public static function getProductToExport($id_shop)
{
return Db::getInstance()->ExecuteS('SELECT id_element FROM `'._DB_PREFIX_.'pssitemap_product` WHERE export = 1 and id_shop = '.(int)$id_shop);
}
/**
* @param id_shop
**/
public static function getCMSToExport($id_shop)
{
return Db::getInstance()->ExecuteS('SELECT id_element FROM `'._DB_PREFIX_.'pssitemap_cms` WHERE export = 1 and id_shop = '.(int)$id_shop);
}
/**
* @param id_shop
**/
public static function getOtherPageToExport($id_shop)
{
return Db::getInstance()->ExecuteS('SELECT id_element FROM `'._DB_PREFIX_.'pssitemap_other_page` WHERE export = 1 and id_shop = '.(int)$id_shop);
}
/**
* @param frequency
* @param priority
* @param id_shop
* @param lang
**/
public function getActiveURLCategory($frequency, $priority, $id_shop, $lang)
{
$link = new Link();
$category_export = $this->getCategoryToExport($id_shop);
$all_urls = array();
foreach ($category_export as $id) {
$category = new Category($id['id_element'], $lang);
$category_url = $link->getCategoryLink($category, null, $lang, null, $id_shop, false);
$all_urls[] = $category_url;
}
$this->generateSitemap($all_urls, $frequency, $priority, "category", $lang);
}
/**
* @param frequency
* @param priority
* @param id_shop
* @param lang
**/
public function getActiveURLProduct($frequency, $priority, $id_shop, $lang)
{
$link = new Link();
$products_export = $this->getProductToExport($id_shop);
$all_urls = array();
$img_url = "";
foreach ($products_export as $products_id) {
$product = new Product((int)$products_id['id_element'], false, (int)$lang, $id_shop);
if(!empty($product)){
$id_img = Product::getCover($products_id['id_element']);
$image = new Image(($id_img['id_image']));
$img_url = _PS_BASE_URL_._THEME_PROD_DIR_.$image->getExistingImgPath().".jpg";
if (Tools::usingSecureMode()) {
$img_url = str_replace("http", "https", $img_url);
}
$url_products = $link->getProductLink($product, null, null, null, $lang, $id_shop, 0, false, false, false, array());
$all_urls[] = array($url_products => $img_url);
}
}
$this->generateSitemap($all_urls, $frequency, $priority, "product", $lang, $img_url);
}
/**
* @param frequency
* @param priority
* @param id_shop
* @param lang
**/
public function getActiveURLCMS($frequency, $priority, $id_shop, $lang)
{
$link = new Link();
$cms_export = $this->getCmsToExport($id_shop);
$all_urls = array();
foreach ($cms_export as $cms_id) {
$cms = new CMS($cms_id['id_element'], $lang);
$url_cms = $link->getCMSLink($cms, null, null, $lang, $id_shop);
$all_urls[] = $url_cms;
}
$this->generateSitemap($all_urls, $frequency, $priority, "cms", $lang);
}
/**
* @param frequency
* @param priority
* @param id_shop
* @param lang
**/
public function getActiveURLOtherPage($frequency, $priority, $id_shop, $lang)
{
$link = new Link();
$other_page_export = $this->getOtherPageToExport($id_shop);
$all_urls = array();
foreach ($other_page_export as $id) {
$otherPage = Db::getInstance()->ExecuteS('SELECT cat.id_meta, cat.page
FROM `'._DB_PREFIX_.'meta` as cat
INNER JOIN `'._DB_PREFIX_.'meta_lang` as cats ON(cat.id_meta = cats.id_meta)
WHERE id_shop = '.(int)$id_shop.'
AND id_lang ='.(int)$lang.'
AND cat.id_meta= '.(int)$id['id_element']);
foreach ($otherPage as $value) {
$otherPage_url = $link->getPageLink($value["page"], null, $lang, null, false, $id_shop, false);
}
$all_urls[] = $otherPage_url;
}
$this->generateSitemap($all_urls, $frequency, $priority, "other_page", $lang);
}
/**
* @param Link
* @param frequency
* @param priority
* @param type
* @param lang
**/
public function generateSitemap($Link, $frequency, $priority, $type, $lang)
{
$id_shop = (int)Context::getContext()->shop->id;
$iso_lang = Language::getIsoById($lang);
$sitemap_name = 'sitemap_'.$iso_lang.'_'.$type.'_'.$id_shop.'.xml';
$root = _PS_ROOT_DIR_. '/'.$sitemap_name;
if (($file = fopen($root, "w"))) {
$sitemapXML = '<?xml version="1.0" encoding="UTF-8"?>' . "\xA";
$sitemapXML .= '<urlset xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'."\n";
fwrite($file, $sitemapXML);
$lastmod=date("Y-m-d")."T".date("H:i:s").date("P");
if ($type == "product") {
foreach ($Link as $innerArray) {
foreach ($innerArray as $product_url => $img_url) {
$product_url = htmlspecialchars($product_url);
$sitemapXML = "<url>\n\t<loc>".$product_url."</loc>\n\t" .
"<lastmod>".$lastmod."</lastmod>\n\t" .
"<priority>".$priority."</priority>\n\t" .
"<changefreq>".$frequency."</changefreq>\n\t";
if (configuration::get('IMG_LINKS_SITEMAP') == 1) {
$sitemapXML .= "<image:image><image:loc>".$img_url."</image:loc>\n</image:image>\n\t";
}
$sitemapXML .= "</url>\n";
fwrite($file, $sitemapXML);
}
}
} elseif (($type == "category") && configuration::get('HOMEPAGE_BUTTON') == 1) {
$count = 0;
$category_export = $this->getCategoryToExport($id_shop);
foreach ($category_export as $id) {
$url_link = htmlspecialchars($Link[$count]);
$category = new Category($id['id_element'], $lang);
if (in_array("Home", array($category->name)) || in_array("Inicio", array($category->name)) || in_array("Accueil", array($category->name))) {
$sitemapXML = "<url>\n\t<loc>".$url_link."</loc>\n\t" .
"<lastmod>".$lastmod."</lastmod>\n\t" .
"<changefreq>".configuration::get('HOMEPAGE_FREQUENCY')."</changefreq>\n\t" .
"<priority>".configuration::get('HOMEPAGE_PRIORITY')."</priority>\n</url>\n";
} else {
$sitemapXML = "<url>\n\t<loc>".$url_link."</loc>\n\t" .
"<lastmod>".$lastmod."</lastmod>\n\t" .
"<changefreq>".$frequency."</changefreq>\n\t" .
"<priority>".$priority."</priority>\n</url>\n";
}
$count++;
fwrite($file, $sitemapXML);
}
} else {
foreach ($Link as $value) {
$value = htmlspecialchars($value);
$sitemapXML = "<url>\n\t<loc>".$value."</loc>\n\t" .
"<lastmod>".$lastmod."</lastmod>\n\t" .
"<changefreq>".$frequency."</changefreq>\n\t" .
"<priority>".$priority."</priority>\n</url>\n";
fwrite($file, $sitemapXML);
}
}
/**END OF SITEMAP FILE**/
$sitemapXML = "</urlset>";
fwrite($file, $sitemapXML);
//close output file
fclose($file);
}
$this->createIndexFile();
}
public function createIndexFile()
{
$module = new Pssitemap();
$id_shop = (int)Context::getContext()->shop->id;
$sitemap_created = $module->getAllSitemap($id_shop);
$filename = _PS_ROOT_DIR_."/"."sitemapIndex_Shop_".$id_shop.".xml";
$lastmod=date("Y-m-d")."T".date("H:i:s").date("P");
$exclude_sitemap = "sitemapIndex";
if (($file = fopen($filename, "w"))) {
$sitemap = '<?xml version="1.0" encoding="UTF-8"?>' . "\xA";
$sitemap .= '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
fwrite($file, $sitemap);
foreach ($sitemap_created as $value) {
if (strpos($value, $exclude_sitemap) == true) {
continue;
}
$sitemap = "<sitemap>\n\t<loc>".$value."</loc>\n\t".
"<lastmod>".$lastmod."</lastmod>\n\t";
$sitemap .= "</sitemap>\n";
fwrite($file, $sitemap);
}
$sitemap = "</sitemapindex>";
fwrite($file, $sitemap);
fclose($file);
}
}
}