* @since Version 2.0 */ class sysLanguagesModule { public $db; /** * Class constructor */ public function __construct() { $this->db = cDb::getInstance(DB_HOST, DB_USR, DB_PWD, DB_PORT, DB_NAME); } /** * Function to get module records * * @author Mareks Keišs * @category sysLanguages Module * @param string $where - sql query * @param string $sort - sql query * @param int $offset - db table offset * @param int $limitPerPage - limit per page * @return array of data * @version 1.1 */ public function getModuleRecords($where, $sort, $offset = 0, $limitPerPage = 20){ $sSql = " SELECT SQL_CALC_FOUND_ROWS * FROM site_languages ll WHERE 1 {$where} 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 sysLanguages 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){ // Update type if($data['default']){ $this->db->_queryUpdate('site_languages', array('default' => '0'), array('default' => '1', 'type' => $data['type'])); } if($id){ if(!$this->db->_queryUpdate('site_languages', $data, array('id' => $id))){ return false; } }else{ if(!($id = $this->db->_queryInsert('site_languages', $data))){ return false; } } return $id; } /** * Function to get record data by id * * @author Mareks Keišs * @category sysLanguages Module * @param int $id - record id * @return array - record data * @version 1.1 */ public function getRecordDataById($id){ return $this->db->_querySelectRow('site_languages', false, array('id' => $id)); } /** * Function to delete table records form db * * @author Mareks Keišs * @category sysLanguages Module * @param array $ids - record ids to delete * @return bool true / false * @version 1.1 */ public function deleteRecords($ids){ if($this->chkDefaultExists(false, $ids)){ return $this->db->execute("DELETE FROM `site_languages` WHERE `id` IN (" . implode(',', $ids) . ")"); }else{ cJSON::sendError(); } } /** * Function to delete check if default language exists in same type db * * @author Mareks Keišs * @category sysLanguages Module * @param int $type - type id (1 - CMS, 0 - PUB) * @param mixed $notIn - ids not in * @return bool true / false * @version 1.1 */ public function chkDefaultExists($type, $notIn = 0){ if($type === false){ if(is_array($notIn) && count($notIn)){ $type = $this->db->_querySelectOne('site_languages', array('type'), array('id' => firstE($notIn))); } } $notIn = is_array($notIn)?$notIn:array('1' => $notIn); if($id = $this->db->_querySelectOne('site_languages', array('id'), array('default' => '1', 'type' => $type))){ if(!in_array($id, $notIn)) return true; } return false; } /** * Function to change record status in db table * * @author Mareks Keišs * @category sysLanguages 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_languages ll SET {$field} = '1', lang_modified = '{$time}' WHERE {$field} = '0' AND id IN ({$ids})"); if(!$status) return $status; $status = $this->db->query("UPDATE site_languages ll SET {$field} = '0', lang_modified = '{$time}' WHERE {$field} = '1' AND id IN ({$ids}) AND lang_modified <> '{$time}'"); return $status; } /** * Function to get system languages array * * @author Mareks Keišs * @category sysLanguages Module * @param bool $publicSide - select public side languages or cms side * @param bool $bCmsVisibleOnly - ... * @return array - record data * @version 1.1 */ public function getLanguages($publicSide = true, $bCmsVisibleOnly = false){ $where = array('type' => ($publicSide?0:1)); if($bCmsVisibleOnly !== false && in_array($bCmsVisibleOnly, array('0', '1'))){ $where['lang_cms_e'] = $bCmsVisibleOnly; } return $this->db->_querySelect('site_languages', false, $where, array('site_languages.order' => 'ASC')); } /** * Function to get system language id by code * * @author Mareks Keišs * @category sysLanguages Module * @param string $langCode - lang code to search for * @param bool $bPub - select public side languages or cms side * @return array - record data * @version 1.1 */ public function getLangIdByCode($langCode, $bPub = true){ if($langCode) return $this->db->_querySelectOne('site_languages', array('id'), array('type' => ($bPub ? 0 : 1), 'lang_code' => $langCode)); return false; } /** * Function to get system language data by code * * @author Mareks Keišs * @category sysLanguages Module * @param string $langCode - lang code to search for * @param bool $bIsCms - select cms side languages or cms side * @return array - record data * @version 1.1 */ public function getLangDataByCode($langCode, $bIsCms = true){ if($langCode) return $this->db->_querySelectRow('site_languages', false, array('type' => ($bIsCms ? 1 : 0), 'lang_code' => $langCode)); return false; } /** * Function to load default languages switch menu * * @author Mareks Keišs * @category sysLanguages Module * @param int $id - site map id * @return array - based on lang id * @version 1.1 */ public function getLangSwitchForId($id){ $sSql = " SELECT sm.p_full_url as `url`, sm.p_lang_id, sl.id as `lang_id`, sl.lang_code, sl.lang_title, sl.`default` FROM site_map sm LEFT JOIN site_languages sl ON sl.id = sm.p_lang_id WHERE sm.p_type = '0' AND sm.p_lang_ref_id = {$id} AND sl.lang_blocked = '0' "; if($this->db->query($sSql)){ return $this->db->getFieldBasedArray('lang_id'); } return false; } /** * Function to get site default language * * @author Mareks Keišs * @category cms * @param bool $cms - indicates cms side or public side * @return string - public side language code */ public function getDefaultAppLang($cms = false){ if($lang = $this->db->_querySelectRow('site_languages', false, array( 'type' => ($cms?1:0), 'default' => 1, 'lang_blocked' => 0 ))){ return $lang; } die('No default language marked!'); } /** * Function to get site default language * * @author Mareks Keišs * @category cms * @param int $langId - language id in question * @param bool $bIsCMS * @param bool $bIsDefault * @return bool */ public function updLanguageDefaultMessages($langId, $bIsCMS = false, $bIsDefault = false){ if($langId){ // Get default language $defLanguage = $this->getDefaultAppLang($bIsCMS); $sTable = 'site_pub_msg_translations'; if($bIsCMS) $sTable = 'site_cms_msg_translations'; // Delete all language translations $this->db->_queryDelete($sTable, array('msg_lang_id' => $langId)); // Insert new translations based on APP/CMS default language if($bIsDefault){ $sSql = " INSERT INTO {$sTable} (msg_id, msg_lang_id, msg_translation) SELECT id, {$langId}, msg_message FROM ". ($bIsCMS ? 'site_cms_msg' : 'site_pub_msg') ." "; }else{ $sSql = " INSERT INTO {$sTable} (msg_id, msg_lang_id, msg_translation) SELECT msg_id, {$langId}, msg_translation FROM {$sTable} ss WHERE ss.msg_lang_id = {$defLanguage['id']} "; } $this->db->query($sSql); return true; } return false; } }