* @since Version 2.0 */ class cmsTextManagerModule { public $db; /** @var cmsTextManagerCache */ private $cache; const DEFAULT_LANGUAGE = 7; /** * Class constructor */ public function __construct() { $this->db = cDb::getInstance(); $this->cache = new cmsTextManagerCache(new cApc()); } /** * Function to get module records * * @author Mareks Keišs * @category cmsTextManager 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 $language - Data from witch language * @return array of data * @version 1.1 */ public function getModuleRecords($where, $sort, $offset = 0, $limitPerPage = 20, $language){ $sSql = " SELECT SQL_CALC_FOUND_ROWS ma.*, mar.a_title, SUBSTRING(mar.a_lhtml, 1, 60) as a_lhtml, SUBSTRING(mar.a_shtml, 1, 60) as a_shtml, mc.cl_name FROM mod_articles ma LEFT JOIN mod_articles_records mar ON ma.id = mar.a_id LEFT JOIN mod_classificators mc ON ma.a_type = mc.id WHERE 1 {$where} AND a_lang_id = {$language} 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 cmsTextManager Module * @param int $id - record id for update * @param array $data - record data * @return bool record new/old id * @version 1.1 */ public function saveRecordDataById($id, $data){ if($id){ if(!$this->db->_queryUpdate('mod_articles', $data, array('id' => $id))) return false; }else{ if(!($id = $this->db->_queryInsert('mod_articles', $data))) return false; } return $id; } public function saveRecordTextDataById($id, $data){ $this->db->_queryDelete('mod_articles_records', array('a_id' => $id)); $this->db->_queryInsertArray('mod_articles_records', $data, false, true); } /** * Function to add record image data by id * * @author Aivars Žukovskis * @category cmsTextManager Module * @param int $id - record id for update * @param array $data - record data * @param array $data2 - record data * @param bool $bDeleteImages - to delete old images * @version 1.1 */ public function saveRecordedImg($id, $data, $data2, $bDeleteImages = false){ if($bDeleteImages){ $this->db->_queryDelete('mod_articles_files', array('ref_id' => -1)); $this->db->_queryDelete('mod_articles_files', array('ref_id' => $id)); } if(!!$data) $this->db->_queryInsertArray('mod_articles_files', $data); if(!!$data2) $this->db->_queryInsertArray('mod_articles_files', $data2); } /** * Function to ... * * @author Aivars Žukovskis * @category cmsTextManager Module * @param string $fileLink - file link * @return int new inserted id * @version 1.1 */ public function insertTemporallyFile($fileLink) { return $this->db->_queryInsert('mod_articles_files', [ 'ref_id' => -1, 'file_link' => $fileLink, 'file_disabled' => '0', 'file_order' => 0, 'file_type' => cmsTextManagerConst::IMG_CNT_TYPE, ]); } /** * Function to get record image data by id * * @author Aivars Žukovskis * @category cmsGalleryManager Module * @param int $id - record id * @return array - record data * @version 1.1 */ public function getImagesData($id){ $sSql = " SELECT * FROM mod_articles_files WHERE ref_id = {$id} ORDER BY file_order ASC "; $this->db->query($sSql); return $this->db->getFieldBasedArrayFieldsAdv('file_type', array('id', 'file_link', 'file_disabled', 'file_order', 'file_name'), false); } /** * Function to get record image data by id * * @author Aivars Žukovskis * @category cmsGalleryManager Module * @return array - record data * @version 1.1 */ public function getAllImageData(){ $sSql = " SELECT * FROM mod_articles_files WHERE file_disabled = '0' ORDER BY img_order ASC "; $this->db->query($sSql); return $this->db->getFieldBasedArrayFields('ref_id', 'file_order', 'file_link'); } /** * Function to get Types for articles * * @author Aivars Žukovskis * @category CMS cmsTextManager * @param int/array $type - classificator type id * @param bool/string $groupByType - whether to group by type or not * @return array - if any data found * @version 1.1 */ public function getTypes($type, $groupByType = false){ if(is_array($type)) $type = implode(",", $type); $groupByType ? $groupBy = array('n', 'id') : $groupBy = array('id'); $sSql = " SELECT mc2.id as n, mc.id, mc.cl_name as title FROM mod_classificators mc LEFT JOIN mod_classificators mc2 ON mc.cl_type = mc2.id WHERE mc.cl_type IN ({$type}) AND mc.cl_blocked = '0' ORDER BY mc.cl_order ASC "; $this->db->query($sSql); $data = $this->db->getFieldBasedArrayFieldsAdv($groupBy, array('title')); if($data){ return $data; }else{ return array(); } } /** * Function to get categories * * @author Aivars Žukovskis * @category CMS cmsTextManager * @param array $type - type ids * @param bool $forProd - if we need all categories for products * @return array - if any data found * @version 1.1 */ public function getCategories($type, $forProd = false){ $langId = cmsTextManagerConst::DEFAULT_LANGUAGE; $cacheKeyType = is_array($type) ? $type : [$type]; $cached = $this->cache->get($cacheKeyType, $langId); if ($cached !== false) { return $cached; } $sSql = " SELECT ac.id, ac.a_cat, tt.a_title as title FROM mod_articles ac LEFT JOIN mod_articles_records tt ON tt.a_id = ac.id WHERE ac.a_type IN (". implode(",", $type) .") AND tt.a_lang_id = " . cmsTextManagerConst::DEFAULT_LANGUAGE . " "; $this->db->query($sSql); $data = $this->db->getFieldBasedArrayFieldsAdv('id', array('title', 'a_cat')); $data = ($data ? $data : array()); $this->cache->set($cacheKeyType, $langId, $data); return $data; } /** * Function to get record data by id * * @author Mareks Keišs * @category cmsTextManager Module * @param int $id - record id * @return array - record data * @version 1.1 */ public function getRecordBaseDataById($id){ $sSql = " SELECT id, a_disabled, a_npk, a_created, a_modified, a_type, a_cl_id, a_cat, a_title, a_link, a_date FROM mod_articles WHERE id = {$id} "; $this->db->query($sSql); return $this->db->getRow(); } /** * Function to get record data by id * * @author Mareks Keišs * @category cmsTextManager Module * @param int|array $id - record id * @param mixed $fields - fields to select * @return array - record data * @version 1.1 */ public function getRecordTranslateDataById($id, $fields = false){ if(!$fields) $fields = array('t','l','v','srec','rec'); $sSql = " SELECT rr.a_id as id, rr.a_title as t, rr.a_link as l, rr.a_v as v, rr.a_shtml as srec, rr.a_lhtml as rec, rr.a_lang_id as lang FROM mod_articles_records rr WHERE a_id IN (" . (is_array($id) ? implode(",", $id) : $id) . ") "; $this->db->query($sSql); if(is_array($id)) return $this->db->getFieldBasedArrayFieldsAdv(array('id', 'lang'), $fields); else return $this->db->getFieldBasedArrayFieldsAdv('lang', $fields); } /** * Function to get record data by id * * @author Aivars Žukovskis * @category cmsTextManager Module * @param int $id - record id * @return array - record data * @version 1.1 */ public function getArticleTypeById($id){ $sSql = " SELECT ma.a_type, mar.a_title FROM mod_articles ma LEFT JOIN mod_articles_records mar ON ma.id = mar.a_id WHERE ma.id = {$id} "; $this->db->query($sSql); return $this->db->getRow(); } /** * Function to get record type by ids * * @author Aivars Žukovskis * @category cmsTextManager Module * @param int/array $ids - record ids * @return array - record data * @version 1.1 */ public function getTypeByIds($ids){ $sSql = " SELECT a_type FROM mod_articles WHERE id IN (". implode(",", $ids) .") "; $this->db->query($sSql); return $this->db->getKeyValBasedArray('a_type', 'a_type'); } /** * Function to check if url is unique * * @param string $link - where string * @param int $recordId - record id * @param int $langId - language id * @return array - record data * @version 1.1 */ public function chkUrlSegment($link, $recordId, $langId) { $where = ""; if (!!$recordId) { $where = " AND a_id <> {$recordId}"; } $sSql = " SELECT a_id FROM mod_articles_records WHERE a_link = '{$link}' AND a_lang_id = {$langId} {$where} "; $this->db->query($sSql); return $this->db->getOne(); } /** * Function to delete table record form db * * @author Aivars Žukovskis * @category cmsTextManager Module * @param array $sIds - record id to delete * @return bool true / false * @version 1.1 */ public function deleteRecords($sIds){ $sIds = implode(',', $sIds); // Delete url category links if exists $this->db->execute("DELETE FROM `site_map_urls` WHERE url_item_id IN ({$sIds})"); // Get all linked files for removal & delete from DB $this->db->query(" SELECT f.file_link, COUNT(f.file_link) as c FROM mod_articles_files f WHERE f.file_link IN ( SELECT ff.file_link FROM mod_articles_files ff WHERE ff.ref_id IN ({$sIds}) ) GROUP BY f.file_link HAVING c = 1 ".""); $imagesToDelete = $this->db->getFieldBasedArrayFieldsAdv(false, 'file_link', false); $this->db->execute("DELETE FROM `mod_articles_files` WHERE `ref_id` IN ({$sIds})"); // Delete prod links & prod to cat links $this->db->execute("DELETE FROM `mod_articles_records` WHERE `a_id` IN ({$sIds})"); $this->db->execute("DELETE FROM `mod_articles` WHERE `id` IN ({$sIds})"); return (!!$imagesToDelete ? $imagesToDelete : true); } /** * Function to change record status in db table * * @author Mareks Keišs * @category cmsTextManager 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 mod_articles ma SET {$field} = '1', a_modified = '{$time}' WHERE {$field} = '0' AND id IN ({$ids})"); if(!$status) return $status; $status = $this->db->query("UPDATE mod_articles ma SET {$field} = '0', a_modified = '{$time}' WHERE {$field} = '1' AND id IN ({$ids}) AND a_modified <> '{$time}'"); return $status; } /** * Function to get url alias ref id by id * * @author Aivars Žukovskis * @category cmsTextManager Module * @param int $id - cat/sub cat id * @return int - url alias ref id * @version 1.1 */ public function getAliasRefIdById($id) { return $this->db->_querySelectOne('site_map_urls', ['url_lang_ref_id'], ['url_item_id' => $id]); } public function getDataById(int $id, array $fields = [], int $langId = self::DEFAULT_LANGUAGE) { if (empty($fields)) { $fields = ['a_title']; } return $this->db->_querySelectOne('mod_articles_records', $fields, ['a_id' => $id, 'a_lang_id' => $langId]); } /** * Function to ... * * @author Mareks Keišs * @category cmsTextManager Module * @param array $linkIds - category link ids * @return int new inserted id * @version 1.1 */ public function getModOverWriteUrlData($linkIds = array()){ $linkIds = $this->db->prepData($linkIds); $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_item_id IN (". implode(",", $linkIds) .") ".""; $this->db->query($sSql); return $this->db->getFieldBasedArrayFieldsAdv(array('id', 'lang_id'), array('url', 'map_id')); } }