maison_alexandra/www/system-brs-core/library/cMapUrls/cMapUrlsModule.php

320 lines
11 KiB
PHP

<?php
namespace BRSystems\Core\Library\cMapUrls;
use BRSystems\Core\Library\cDb\cDb;
/**
* Map urls Module
*
* @category Map Urls Module
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @since Version 1.0
*/
class cMapUrlsModule
{
public $db;
/**
* Class constructor
*/
public function __construct() {
$this->db = cDb::getInstance(DB_HOST, DB_USR, DB_PWD, DB_PORT, DB_NAME);
}
/**
* Function to get site_map url data property(ies) by id
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category Map Urls Module
* @param int $id - map id
* @param string|bool $propName - property name
* @param bool $bWithLangRefs - get all linked lang refs as well
* @return mixed - property data
* @version 1.0
*/
public function getSiteMapDataById($id, $propName = false, $bWithLangRefs = false){
if($propName){
if($bWithLangRefs){
$sSql = "
SELECT
sm.id as map_id,
sm.p_full_url as url,
sm.p_lang_id as lang_id
FROM
site_map sm
WHERE
sm.p_lang_ref_id = (SELECT s.p_lang_ref_id FROM site_map s WHERE s.id = {$id})
";
$this->db->query($sSql);
return $this->db->getFieldBasedArrayFieldsAdv('lang_id', array('url', 'map_id'));
}else{
return $this->db->_querySelectOne('site_map', array($propName), array('id' => $id));
}
}
return false;
}
/**
* Function to get site_map url data array with properties by multiple ids
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category Map Urls Module
* @param int $ids - map id's
* @return mixed - property data
* @version 1.0
*/
public function getSiteMapDataArrayById($ids){
if(!!$ids) {
$ids = $this->db->prepData($ids);
$sSql = "
SELECT
sm.p_lang_ref_id as id,
sm.id as map_id,
sm.p_full_url as url,
sm.p_lang_id as lang_id
FROM
site_map sm
WHERE
sm.p_lang_ref_id = (SELECT s.p_lang_ref_id FROM site_map s WHERE s.id IN (" . implode(',', $ids) . "))
";
$this->db->query($sSql);
return $this->db->getFieldBasedArrayFieldsAdv(array('id','lang_id'), array('url', 'map_id'));
}
return false;
}
/**
* Function to get site_map_urls url data property(ies) by id
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category Map Urls Module
* @param int $id - map url id
* @param string|bool $propName - property name
* @param bool $bWithLangRefs - get all linked lang refs as well
* @return mixed - property data
* @version 1.0
*/
public function getSiteMapUrlsDataById($id, $propName = false, $bWithLangRefs = false){
if($propName){
if($bWithLangRefs){
$sSql = "
SELECT
sm.url_fname as url,
sm.url_lang_id as lang_id,
sm.url_alias_id as map_id
FROM
site_map_urls sm
WHERE
sm.url_lang_ref_id = (SELECT s.url_lang_ref_id FROM site_map_urls s WHERE s.id = {$id})
";
$this->db->query($sSql);
return $this->db->getFieldBasedArrayFieldsAdv('lang_id', array('url', 'map_id'));
}else{
return $this->db->_querySelectOne('site_map_urls', array($propName), array('id' => $id));
}
}
return false;
}
/**
* Function to get site_map_urls url data property(ies) by id
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category Map Urls Module
* @param int $ids - map url id's
* @return mixed - property data
* @version 1.0
*/
public function getSiteMapUrlsDataArrayById($ids){
if(!!$ids){
$ids = $this->db->prepData($ids);
$sSql = "
SELECT
sm.url_lang_ref_id as id,
sm.url_fname as url,
sm.url_lang_id as lang_id,
sm.url_alias_id as map_id
FROM
site_map_urls sm
WHERE
sm.url_lang_ref_id = (SELECT s.url_lang_ref_id FROM site_map_urls s WHERE s.id IN (" . implode(',', $ids) . "))
";
$this->db->query($sSql);
return $this->db->getFieldBasedArrayFieldsAdv(array('id', 'lang_id'), array('url', 'map_id'));
}
return false;
}
/**
* Function to get site_map_urls by item id
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category Map Urls Module
* @param int $itemId - map urls item id
* @param int $urlChCount - url chunk count before before substring cutting of products
* @return mixed - property data
* @version 1.0
*/
public function getAllSiteMapUrlsByItemId($itemId, $urlChCount = 4)
{
$itemId = $this->db->prepData($itemId);
if (empty($itemId))
return false;
$sSql = "
SELECT
sm.id,
CONCAT(substring_index(sm.url_fname,'/', {$urlChCount}),'/') as url,
sm.url_lang_id as lang_id,
sm.url_lang_ref_id as r_id
FROM
site_map_urls sm
WHERE
sm.url_item_id = {$itemId}
";
$this->db->query($sSql);
return $this->db->getFieldBasedArrayFieldsAdv(['lang_id', 'url'], ['id', 'r_id']);
}
/**
* Function to add / update site map urls table
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category Map Urls Module
* @param int|bool $mapUrlsId - map id to update
* @param array $updateData - map data to update
* @param array|bool $where - update clause
* @return int - url id
*/
public function saveMapUrlsData($mapUrlsId = false, $updateData, $where = false){
if($mapUrlsId){
if(!$where){
$where = array('id' => $mapUrlsId);
}
$this->db->_queryUpdate('site_map_urls', $updateData, $where);
}else{
$mapUrlsId = $this->db->_queryInsert('site_map_urls', $updateData);
}
return $mapUrlsId;
}
/**
* Function to add / update site map urls table
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category Map Urls Module
* @param int|bool $mapUrlId - url id
* @param string $newUrlFName - new map url
* @param int|bool $langId - lang id
* @return int - url id
*/
public function updateChildUrlsByParentId($mapUrlId = false, $newUrlFName = '', $langId = false){
if($mapUrlId && $langId && !!$newUrlFName){
$oldUrl = $this->db->_querySelectOne('site_map_urls', array('url_fname'), array('id' => $mapUrlId, 'url_lang_id' => $langId));
if(!!$oldUrl){
$sSql = "
UPDATE site_map_urls sm,
(
SELECT
id,
REPLACE(ss.url_fname,'{$oldUrl}','{$newUrlFName}') AS n_url_fname
FROM
site_map_urls ss
WHERE
ss.url_lang_id = {$langId}
AND ss.url_fname LIKE '{$oldUrl}%'
AND ss.id <> {$mapUrlId}
) nsm
SET sm.url_fname = nsm.n_url_fname
WHERE sm.url_lang_id = {$langId} AND nsm.id = sm.id;
";
return $this->db->query($sSql);
}
}
return false;
}
/**
* Function to delete old aliases
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category Map Urls Module
* @param int|bool $ids - ids to delete
* @param string|bool $sUrlType - url type
* @return mixed
*/
public function deleteAliasesById($ids, $sUrlType){
if(!!$ids && is_array($ids) && $sUrlType)
return $this->db->_queryDelete('site_map_urls', array('id' => $ids, 'url_type' => $sUrlType));
return false;
}
/**
* Function to get both site_map & site_map_urls link occurrences in tables
*
* @author Mareks Keišs <mareks.keiss@brsystems.lv>
* @category Map Urls Module
* @param string|array $links - map url(s) to find
* @return bool|array - status or found data
* @version 1.0
*/
public function getAllUrlOccurrencesInBothTables($links){
if(!empty($links)){
$links = "'" . (is_array($links) ? implode("','", $links) : $links) . "'";
$sSql = "
SELECT
0 as type,
sm.id,
sm.p_lang_ref_id as lang_ref_id,
sm.p_full_url as url
FROM
site_map sm
WHERE
sm.p_lang_ref_id IN (
SELECT smr.p_lang_ref_id FROM site_map smr WHERE smr.p_full_url IN ({$links})
)
UNION ALL
SELECT
1 as type,
sm.id,
sm.url_lang_ref_id as lang_ref_id,
sm.url_fname as url
FROM
site_map_urls sm
WHERE
sm.url_lang_ref_id IN (
SELECT smr.url_lang_ref_id FROM site_map_urls smr WHERE smr.url_fname IN ({$links})
)
";
$this->db->query($sSql);
return $this->db->getFieldBasedArrayFieldsAdv(array('type', 'lang_ref_id', 'id'), 'url');
}
return false;
}
}