* @since Version 2.0 */ class sysSiteMapModule { public $db; /** * Class constructor */ public function __construct() { $this->db = cDb::getInstance(); } /** * Function to get module records * * @author Mareks Keišs * @category sysSiteMap Module * @param string $where - sql query * @param string $sort - sql query * @param int $offset - db table offset * @param int $limitPerPage - limit per page * @param int $type - type ( cms or pub ); * @return array of data * @version 1.1 */ public function getModuleRecords($where, $sort, $offset = 0, $limitPerPage = 20, $type){ $sSql = " SELECT SQL_CALC_FOUND_ROWS sm.id, sm.p_title, sm.p_created, sm.p_modified, sm.p_enabled, sm.p_visible, sm.p_sitemap, sm.p_autocache, sm.p_2fa, sm.p_login, sm.p_default, COALESCE(sm.p_parent,0) as p_parent, sm.p_order, sm.p_lang_ref_id, sm.p_full_url, ll.lang_code, ll.lang_title, CASE WHEN sm.p_parent > 0 THEN sm2.p_title ELSE '-- ROOT --' END as parent_title, us.user_name FROM site_map sm LEFT JOIN site_languages ll ON sm.p_lang_id = ll.id LEFT JOIN site_users us ON sm.p_modified_user_id = us.id LEFT JOIN site_map sm2 ON sm.p_parent = sm2.id AND sm.p_parent <> 0 WHERE 1 {$where} AND sm.p_type = {$type} ORDER BY {$sort} LIMIT {$offset}, {$limitPerPage} "; $this->db->query($sSql); return $this->db->getArray(); } /** * Function to add or save record data * * @author Mareks Keišs * @category sysSiteMap Module * @param array $data - record data * @param string $dataSql - data sql ( update sql string ); * @return bool record new/old id * @version 1.1 */ public function saveRecordDataById($data, $dataSql){ // Update data with data sql first; if($dataSql){ if(!$this->db->executeMulti($dataSql)) return false; } if(!empty($data) && is_array($data)){ if($id = $this->db->_queryInsertArray('site_map', $data)) return $id; else return false; } return true; } /** * Function to get record data by id * * @author Mareks Keišs * @category sysSiteMap Module * @param int $id - record id * @return array - record data * @version 1.1 */ public function getRecordDataById($id){ return $this->db->_querySelectRow('mod_users_subscription_mails', false, array('id' => $id)); } /** * Function to delete table records form db * * @author Mareks Keišs * @category sysSiteMap Module * @param array $ids - record ids to delete * @return bool true / false * @version 1.1 */ public function deleteRecords($ids){ $this->db->execute("DELETE FROM `site_map` WHERE `id` IN (" . implode(',', $ids) . ")"); $this->db->execute("DELETE FROM `temp_map_links` WHERE `map_id` IN (" . implode(',', $ids) . ")"); $this->db->execute("DELETE FROM `site_map_menus` WHERE `map_id` IN (" . implode(',', $ids) . ")"); return $this->db->execute("DELETE FROM `site_map_modules` WHERE `mod_map_id` IN (" . implode(',', $ids) . ")"); } /** * Function to chk if ref id is the same as id; * * @author Aivars Žukovskis * @category sysSiteMap Module * @param array $ids - record ids * @return bool true / false * @version 1.1 */ public function chkIdsForRefIds($ids){ $sSql = " SELECT id, COALESCE(id = p_lang_ref_id) AS main FROM site_map WHERE id IN (". implode(",", $ids) .") "; $this->db->query($sSql); return $this->db->getFieldBasedArrayFieldsAdv('main', 'id', false); } /** * Function to get ids by reference ids * * @author Aivars Žukovskis * @category sysSiteMap Module * @param array $ids - page ids * @param bool $bSimpleIds - if we need just the ids * @return bool|array * @version 1.1 */ public function getIdsByLangRefIds($ids, $bSimpleIds = false){ $this->db->_querySelect('site_map', array('id', 'p_lang_ref_id'), array('p_lang_ref_id' => $ids), false, false, false); if($bSimpleIds) return $this->db->getKeyValBasedArray('id', 'id'); else return $this->db->getFieldBasedArrayFieldsAdv('p_lang_ref_id', 'id', false); } /** * Function to get ids by reference ids * * @author Aivars Žukovskis * @category sysSiteMap Module * @param string $sSql - sql update * @return int * @version 1.1 */ public function updateRefIds($sSql){ return $this->db->executeMulti($sSql); } /** * Function to get menus * * @author Aivars Žukovskis * @category sysSiteMap Module * @param array $ids - record ids to delete * @return array with data * @version 1.1 */ public function getMenusByMapId($ids){ $sSql = "SELECT menu_id FROM site_map_menus WHERE map_id IN (" . implode(',', $ids) . ")"; $this->db->query($sSql); return $this->db->getKeyValBasedArray('menu_id', 'menu_id'); } /** * Function to change record status in db table * * @author Mareks Keišs * @category sysSiteMap Module * @param array $ids - record ids to affect * @param string $field - field to update for status ENUM * @return bool true / false * @version 1.1 */ public function changeRecordsStatus($ids, $field){ $time = time(); $ids = implode(',', $ids); $status = $this->db->query("UPDATE site_map sm SET {$field} = '1', p_modified = '{$time}' WHERE {$field} = '0' AND id IN ({$ids})"); if(!$status) return $status; $status = $this->db->query("UPDATE site_map sm SET {$field} = '0', p_modified = '{$time}' WHERE {$field} = '1' AND id IN ({$ids}) AND p_modified <> '{$time}'"); return $status; } /** * Function to get all pages for parent use * * @author Aivars Žukovskis * @category sysSiteMapModule Module * @param int $lang_id - language id * @param int $p_type - cms or pub side * @param bool $b - if we need parents for show management * @return array of data * @version 1.1 */ public function getAllPagesUrls($p_type, $lang_id, $b = false){ $sSql = " SELECT sm.id as parent_id, sm.p_title as title, sm.p_full_url as parent_url, sm.p_parent, pp.p_title FROM site_map sm LEFT JOIN site_map pp ON sm.p_parent = pp.id WHERE sm.p_type = {$p_type} AND sm.p_lang_id = {$lang_id} AND sm.p_parent <> 0 "; $this->db->query($sSql); if($b) return $this->db->getFieldBasedArrayFields('parent_id'); else return $this->db->getFieldBasedArrayFields('p_title', 'parent_id'); } /** * Function to get system languages array * * @author Aivars Žukovskis * @category sysSiteMap Module * @param bool $cms - indicates whether its cms languages or pub * @return array - record data * @version 1.1 */ public function getSysLanguages($cms = false){ return $this->db->_querySelect('site_languages', false, array('type' => $cms ? 1 : 0), array('site_languages.order' => 'ASC')); } /** * Function to get reference id by id * * @author Aivars Žukovskis * @category sysSiteMap Module * @param int $id - page id * @return int * @version 1.1 */ public function getRefIdById($id){ return $this->db->_querySelectOne('site_map', array('p_lang_ref_id'), array('id' => $id )); } /** * Function to get module records for edit by id * * @author Aivars Žukovskis * @category sysSiteMapModule Module * @param int $id - id * @param bool $bClone - indicates whether we need data for clone or not * @return array of data * @version 1.1 */ public function getModuleRecordsForEdit($id, $bClone = false){ $select = ""; if(!$bClone){ $select = "sm.id, sm.p_title, sm.p_url, sm.p_lang_ref_id, sm.p_keywords, sm.p_description, sm.p_created, sm.p_modified, us.user_name, sm.p_parent, sm.p_full_url, sm2.p_full_url as parent_url,"; } $sSql = " SELECT {$select} sm.p_type, sm.p_lang_id, sm.p_order, sm.p_redirect_url, sm.p_enabled, sm.p_visible, sm.p_autocache, sm.p_st_cnt, sm.p_2fa, sm.p_404, sm.p_sitemap, sm.p_default, sm.p_login, sm.p_permission, sm.p_menus, smm.mod_use_tmpl FROM site_map sm LEFT JOIN site_users us ON sm.p_modified_user_id = us.id LEFT JOIN site_map_modules smm ON sm.id = smm.mod_map_id AND smm.mod_primary = '1' LEFT JOIN site_map sm2 ON sm.p_parent = sm2.id WHERE sm.p_lang_ref_id = {$id} "; $this->db->query($sSql); return $this->db->getFieldBasedArray('p_lang_id'); } /** * Function to get cms permissions * * @author Aivars Žukovskis * @category sysSiteMapModule Module * @param string $table - db table where to look * @param int $id - id * @return string of permission name * @version 1.1 */ public function getAddedPermissionById($table, $id){ return $this->db->_querySelectOne($table, array('p_name'), array('id' => $id )); } /** * Function to get loaded menus * * @author Aivars Žukovskis * @category sysSiteMapModule Module * @param int $id - id * @param int $menu_type - id * @param int $pubSide - indicates whether its pub side or not * @return array - list of menus for the nod * @version 1.1 */ public function getPageMenus($id, $menu_type, $pubSide){ $sSql = " SELECT sm.id as id, sm.menu_name as v FROM site_map_menus smm LEFT JOIN site_menus sm ON smm.menu_id = sm.id WHERE smm.map_id = {$id} AND sm.menu_blocked = '0' AND smm.menu_type = '{$menu_type}' AND sm.menu_pub_side = '{$pubSide}' "; $this->db->query($sSql); $d = $this->db->getArray(); if(!!$d && is_array($d)) return $d; else return array(); } /** * Function to get used modules for edit by id * * @author Aivars Žukovskis * @category sysSiteMapModule Module * @param array $ids * @return array of data * @version 1.1 */ public function getUsedModulesForEdit($ids){ $sSql = " SELECT smm.mod_map_id, smm.mod_type, smm.mod_code, smm.mod_load_order, smm.mod_call_method FROM site_map_modules smm WHERE smm.mod_map_id IN (". implode(",", $ids) .") "; $this->db->query($sSql); return $this->db->getFieldBasedArrayFields('mod_map_id', 'mod_type', array('mod_code', 'mod_load_order', 'mod_call_method'), false); } /** * Function to get used modules for page by id * * @author Aivars Žukovskis * @category sysSiteMapModule Module * @param int $id * @param array $types * @return array of data * @version 1.1 */ public function getUsedModulesForPageById($id, $types){ $sSql = " SELECT mod_type, mod_code, mod_load_order FROM site_map_modules WHERE mod_map_id = ". $this->db->prepData($id) ." AND mod_type IN (". implode(",", $this->db->prepData($types)) .") "; $this->db->query($sSql); return $this->db->getFieldBasedArrayFieldsAdv('mod_type', array('mod_code', 'mod_load_order'), false); } /** * Function to get all pages by language; * * @author Aivars Žukovskis * @category sysSiteMapModule Module * @param int $langId - lang id * @return array of data page id as key and name as value; * @version 1.1 */ public function getUsedModulesForAllPages($langId){ $sSql = " SELECT id, p_title FROM site_map WHERE p_lang_id = ". $this->db->prepData($langId) ." "; $this->db->query($sSql); return $this->db->getFieldBasedArrayFieldsAdv('id', 'p_title'); } /** * Function to get page full url by id * * @author Aivars Žukovskis * @category sysSiteMapModule Module * @param int $id - page url * @return string * @version 1.1 */ public function getFullUrlById($id){ return $this->db->_querySelectOne('site_map', array('p_full_url'), array('id' => $id )); } /** * Function to get page full url by id * * @author Aivars Žukovskis * @category sysSiteMapModule Module * @param int $id - page url * @param int $langId - selected language * @return array * @version 1.1 */ public function getFullUrlByIdSpecial($id, $langId){ $sSql = " SELECT p_full_url, id FROM site_map WHERE p_lang_ref_id = ( SELECT p_lang_ref_id FROM site_map WHERE id = {$id} ) AND p_lang_id = {$langId} "; $this->db->query($sSql); return $this->db->getRow(); } /** * Function to get page full url with language code by id * * @author Mareks Keiss * @category sysSiteMapModule Module * @param int $id - page url * @param int $lang_id - page language id * @return string * @version 1.1 */ public function getFullUrlWithLangCodeById($id, $lang_id, $bHideDefault = false, $bEndSlash = false){ $bHideDefault = ($bHideDefault ? 1 : 0); $sSql = " SELECT CASE WHEN ll.`default` = '1' AND {$bHideDefault} THEN CONCAT(sm.p_full_url) ELSE CONCAT(sm.p_full_url,ll.lang_code,'/') END as url FROM site_map sm LEFT JOIN site_languages ll ON sm.p_lang_id = ll.id WHERE sm.p_lang_ref_id = {$id} AND sm.p_lang_id = {$lang_id} "; $this->db->query($sSql); $sUrl = $this->db->getOne(); return (strlen($sUrl) && !$bEndSlash ? substr($sUrl, 0, -1) : $sUrl); } /** * Function to get site permissions * * @author Aivars Žukovskis * @param string $table - table name * @return array data * @version 1.1 */ public function getSitePermissions($table){ return $this->db->_querySelect($table, array('id', 'p_name')); } /** * Function to get site menus * * @author Aivars Žukovskis * @param int $pubSide - indicates whether its pub side or not * @return array data * @version 1.1 */ public function getSiteMenus($pubSide){ return $this->db->_querySelect('site_menus', array('id', 'menu_name'), array('menu_blocked' => '0', 'menu_pub_side' => $pubSide)); } /** * Function to get sub url * * @author Aivars Žukovskis * @category sysSiteMap Module * @param int $start_node_id - node id to start creating pre node * @return array with recorded data of parent * @version 1.0 */ public function genSubUrl($start_node_id){ $sSql = " SELECT * FROM site_map WHERE id = '" . $start_node_id . "' AND p_deleted = '0' "; $this->db->query($sSql); return $this->db->getRow(); } /** * Function to get all children ids * * @author Aivars Žukovskis * @category sysSiteMap Module * @param int $parent_id - parent id * @param int $lang_id - language id * @return array with recorded data of children * @version 1.0 */ public function childUrls($parent_id, $lang_id){ $sSql = " SELECT * FROM site_map WHERE p_parent = '" . $parent_id . "' AND p_lang_id = '" . $lang_id . "' AND p_deleted = '0' "; $this->db->query($sSql); return $this->db->getArray(); } /** * Function to update full url * * @author Aivars Žukovskis * @category sysSiteMap Module * @param string $deep_url - full url * @param int $id - node id * @return bool * @version 1.0 */ public function updateFullUrl($deep_url, $id){ return $this->db->_queryUpdate('site_map', array('p_full_url' => $deep_url), array('id' => $id)); } /** * Function to update menu links for map id * * @author Mareks Keišs * @category content Module * @param array $ids - record id * @param array $data - data tu insert * @return bool true / false * @version 1.1 */ public function updateMenuData($ids, $data){ // Delete old menus $this->db->execute("DELETE FROM site_map_menus WHERE map_id IN (" . implode(',', $ids) . ")"); return $data ? $this->db->_queryInsertArray('site_map_menus', $data) : true; } /** * Function to save record data modules * * @author Aivars Žukovskis * @category sysSiteMap Module * @param array $ids - record id for update * @param array $data - record data * @return bool true/false * @version 1.1 */ public function saveRecordDataModuleById($ids, $data){ $this->db->execute("DELETE FROM site_map_modules WHERE mod_map_id IN (" . implode(',', $ids) . ")"); return $this->db->_queryInsertArray('site_map_modules', $data); } /** * Function to get ids to language * * @author Aivars Žukovskis * @category sysSiteMap Module * @param int $t * @return array * @version 1.1 */ public function getIdToLangId($t){ $sSql = " SELECT id, p_lang_id FROM site_map WHERE p_lang_ref_id = {$t} "; $this->db->query($sSql); return $this->db->getFieldBasedArrayFieldsAdv('p_lang_id', 'id'); } /** * Function to get lang ref ids by ids * * @author Aivars Žukovskis * @category sysSiteMap Module * @param array $ids * @return bool|array * @version 1.1 */ public function getLangRefIdById($ids){ $this->db->_querySelect('site_map', array('p_lang_ref_id'), array('id' => $ids), false, false, false); return $this->db->getKeyValBasedArray('p_lang_ref_id', 'p_lang_ref_id'); } /** * Function to update ref id * * @author Aivars Žukovskis * @category sysSiteMap Module * @param array $ids - all record ids * @param int $id - to update reference id by this id * @return bool true/false * @version 1.1 */ public function updateRefId($ids, $id){ $sSql = " UPDATE site_map SET p_lang_ref_id = {$id} WHERE id IN (" . implode(',', $ids) . ") "; return $this->db->query($sSql); } /** * Function to get site menus * * @author Aivars Žukovskis * @param string $oldName - old admin folder name * @param string $newName - new admin folder name * @return bool true * @version 1.1 */ public function replaceLinks($oldName, $newName){ $sSql =" /* UPDATE p_url */ update site_map sm, (select id, REPLACE(ss.p_url,'{$oldName}','{$newName}') as p_url from site_map ss where ss.p_type = '1' and ss.p_url like '{$oldName}') nsm set sm.p_url = nsm.p_url where sm.p_type = '1' and nsm.id = sm.id; /* UPDATE REDIRECT URLS */ update site_map sm, (select id, REPLACE(ss.p_redirect_url,'/{$oldName}/','/{$newName}/') as p_redirect_url from site_map ss where ss.p_type = '1' and ss.p_redirect_url like '/{$oldName}/%') nsm set sm.p_redirect_url = nsm.p_redirect_url where sm.p_type = '1' and nsm.id = sm.id; /* UPDATE LOGIN REDIRECT URLS */ update site_map sm, (select id, REPLACE(ss.p_login_redirect,'/{$oldName}/','/{$newName}/') as p_login_redirect from site_map ss where ss.p_type = '1' and ss.p_login_redirect like '/{$oldName}/%') nsm set sm.p_login_redirect = nsm.p_login_redirect where sm.p_type = '1' and nsm.id = sm.id; /* UPDATE ADMIN BASE URLS */ update site_map sm, (select id, REPLACE(ss.p_full_url,'/{$oldName}/','/{$newName}/') as p_full_url from site_map ss where ss.p_type = '1' and ss.p_full_url like '/{$oldName}/%') nsm set sm.p_full_url = nsm.p_full_url where sm.p_type = '1' and nsm.id = sm.id; /* MANUALLY CLEAR APC CACHE TABLE */ delete from temp_cache_exp; "; return $this->db->executeMulti($sSql); } /** * Function to get site map used modules * * @author Aivars Žukovskis * @param array $type - types to select * @return array data * @version 1.1 */ public function getAllUsedSiteMapModules($type){ $sSql = " SELECT distinct(mod_code) as mcode, mod_type FROM site_map_modules WHERE mod_type IN(". implode(",", $type) .") "; $this->db->query($sSql); return $this->db->getFieldBasedArrayFieldsAdv(array('mod_type', 'mcode'), 'mcode'); } /** * Function to get site map used modules * * @author Aivars Žukovskis * @param string $sTable - table * @param string $sKey - key * @param string $ids - ids to select * @param array $vFields - if needs specific fields * @return array data * @version 1.1 */ public function getDataForExportSql($sTable, $sKey, $ids, $vFields = array()){ $vReturn = false; if($sTable){ $vWhere = false; if(strlen($sKey) && !empty($ids)) $vWhere = array($sKey => $ids); $vReturn = $this->db->_querySelect($sTable, (!empty($vFields) ? $vFields : false), $vWhere); } return $vReturn; } /** * Functions to load/reload site map / site map url links - for array access instead of db * * @author Mareks Keišs * @category Core Module * @param int $mapId - site map id to load links for * @return string token id * @version 1.1 */ public function loadLinks($mapId){ $sSql = " SELECT sml.map_ref_id as `id`, sml.map_base_uri as `base`, sm.p_full_url as `url`, sml.map_lang_id as `lang_id`, sl.`default`, sl.lang_code FROM temp_map_links sml LEFT JOIN site_map sm ON sml.map_link_id = sm.id LEFT JOIN site_languages sl ON sm.p_lang_id = sl.id WHERE sml.map_id = {$mapId} AND sml.map_type = '0' "; if($this->db->query($sSql)){ return $this->db->getFieldBasedArray('id', 'lang_id'); } return false; } /** * Functions to load/reload site map url links - for array access instead of db * * @author Mareks Keišs * @category Core Module * @param int $mapId - site map id to load links for * @return string token id * @version 1.1 */ public function loadUrlLinks($mapId){ $sSql = " SELECT sml.map_ref_id as `id`, sml.map_base_uri as `base`, sm.url_fname as `url`, sml.map_lang_id as `lang_id`, sl.`default`, sl.lang_code FROM temp_map_links sml LEFT JOIN site_map_urls sm ON sml.map_link_id = sm.id LEFT JOIN site_languages sl ON sm.url_lang_id = sl.id WHERE sml.map_id = {$mapId} AND sml.map_type = '1' "; if($this->db->query($sSql)){ return $this->db->getFieldBasedArray('id', 'lang_id'); } return false; } /** * Function to get site map data for XML file by language * * @author Mareks Keišs * @category pubExport * @param int $langId - language id * @return array of data * @version 1.1 */ public function getSiteMapTableData($langId){ $sSql = " SELECT 'weekly' as changefreq, 0.6 as priority, CONCAT(sm.p_full_url, CASE WHEN sl.`default` = '1' THEN '' ELSE sl.lang_code END) as loc, FROM_UNIXTIME(sm.p_modified,'%Y-%m-%dT%H:%i:%s+02:00') as lastmod FROM site_map sm LEFT JOIN site_languages sl ON sl.id = sm.p_lang_id WHERE sm.p_lang_id = {$langId} AND sm.p_type = '0' AND LENGTH(sm.p_full_url) AND sm.p_login = '0' AND sm.p_deleted = '0' AND sm.p_enabled = '1' AND sm.p_visible = '1' "; $this->db->query($sSql); return $this->db->getArray(); } /** * Function to get site map urls data for XML file by language * * @author Mareks Keišs * @category pubExport * @param int $langId - language id * @param int $offset - offset * @param int $limit - limit per page * @return array of data * @version 1.1 */ public function getSiteMapCategoryData($langId, $offset, $limit){ $sSql = " SELECT 'weekly' as changefreq, 0.8 as priority, CONCAT(smu.url_fname, CASE WHEN sl.`default` = '1' THEN '' ELSE sl.lang_code END) as loc, FROM_UNIXTIME(ma.a_modified,'%Y-%m-%dT%H:%i:%s+02:00') as lastmod FROM mod_articles ma LEFT JOIN mod_articles_records mar ON ma.id = mar.a_id LEFT JOIN site_map_urls smu ON smu.id = mar.a_url_alias_id AND mar.a_lang_id = smu.url_lang_id LEFT JOIN site_languages sl ON sl.id = mar.a_lang_id WHERE mar.a_url_alias_id > 0 AND LENGTH(mar.a_link) AND mar.a_lang_id = {$langId} LIMIT {$offset}, {$limit} "; $this->db->query($sSql); return $this->db->getArray(); } /** * Function to get site map urls data for XML file by language * * @author Mareks Keišs * @category pubExport * @param int $langId - language id * @param int $offset - offset * @param int $limit - limit per page * @return array of data * @version 1.1 */ public function getSiteMapProductData($langId, $offset, $limit){ return array(); $sSql = " SELECT 'daily' as changefreq, 1 as priority, CONCAT(smu.url_fname, CASE WHEN sl.`default` = '1' THEN '' ELSE sl.lang_code END) as loc, FROM_UNIXTIME(pp.prod_modified,'%Y-%m-%dT%H:%i:%s+02:00') as lastmod FROM site_map_urls smu LEFT JOIN mod_products pp ON pp.id = smu.url_item_id LEFT JOIN site_languages sl ON sl.id = smu.url_lang_id WHERE pp.prod_deleted = '0' AND pp.prod_disabled = '0' AND smu.url_lang_id = {$langId} AND smu.url_item_id > 0 AND LENGTH(smu.url_fname) LIMIT {$offset}, {$limit} "; $this->db->query($sSql); return $this->db->getArray(); } /** * Function to get all site map ids * * @author Aivars Žukovskis * @category sysSiteMapModule Module * @return array * @version 1.1 */ public function gatherIds(){ $sSql = " SELECT p_lang_ref_id FROM site_map WHERE `p_deleted` = '0' AND `p_type` = ". sysSiteMapController::PUB_TYPE ." AND `p_lang_ref_id` <> ". sysSiteMapController::PUB_SITE_ID ." GROUP BY p_lang_ref_id "; $this->db->query($sSql); return $this->db->getKeyValBasedArray('p_lang_ref_id', 'p_lang_ref_id'); } }