* @package MCMS\System\Service */ class SiteSvc extends SiteSvcBase { private static $cache; private static function checkCache() { if(empty(self::$cache)) self::$cache = new SimpleCache(); // TODO: optimize } /** * Fetches a list of installed pages * * @param boolean $onlyBase * TRUE if only base sites should be returned, no site variants */ public static function getSites($onlyBase = FALSE) { $db = System::getInstance()->getDB(); $select = $db->select()->from('websites'); if($onlyBase) $select->where([ 'parent_id' => NULL ]); return $db->getObjectList($select, Website::clazz()); } /** * Fetches a website base info * * @param int $id * the website id * @return Website the website model object */ public static function getSite($id) { $db = System::getInstance()->getDB(); $select = $db->select()->from('websites')->where([ 'id' => $id ]); return $db->getObject($select, Website::clazz()); } /** * Fetches a website settings * * @param int $id * the website id */ public static function getSiteSettings($id) { self::checkCache(); if(self::$cache->keyExists('websitesettings_' . $id)) return self::$cache->get('websitesettings_' . $id); $db = System::getInstance()->getDB(); $site = self::getSite($id); $select = $db->select()->from('websitesettings')->where([ 'site_id' => new Parameter() ]); $settings = $db->getArrayListP($select, [ $id ]); $result = array(); $result['online'] = $site->online; foreach($settings as $setting) $result[$setting['key']] = $setting['value']; self::$cache->set('websitesettings_' . $id, $result, 60); return $result; } /** * Set a website setting * * @param int $id * the website id * @param string $key * setting key * @param mixed $value * the setting value */ public static function setSiteSetting($id, $key, $value) { self::checkCache(); $db = System::getInstance()->getDB(); if($key == 'online') { $db->table('websites')->update([ 'online' => $value ], [ 'id' => $id ]); self::$cache->clear(); return; } $db->table('websitesettings')->delete([ 'site_id' => $id, 'instance' => '', 'key' => $key ]); $db->table('websitesettings')->insert([ 'site_id' => $id, 'instance' => '', 'key' => $key, 'value' => $value ]); self::$cache->clear(); } public static function removeSite($id) { self::checkCache(); $db = System::getInstance()->getDB(); $db->table('websitesettings')->delete(['site_id' => $id]); $db->table('websites')->delete(['id' => $id]); self::$cache->clear(); } }