* @package MCMS\System\Service */ class InstanceSvc extends InstanceSvcBase { private static $TBL_INST = 'moduleinstances'; private static $TBL_ACL = 'moduleinstanceacls'; /** * Creates a new module instance in the database * * @param integer $siteId * the id of the site * @param string $name * the instance name * @param mixed $module * @param mixed $access */ public static function createInstance($siteId, $name, $module, $access) { $db = System::getInstance()->getDB(); if (class_exists($module)) { $reflect = new \ReflectionClass($module); if(! $reflect->implementsInterface('\\MCMS\\Module\\iModule')) throw new \InvalidArgumentException('Class does not implement iModule'); } else throw new \InvalidArgumentException('Invalid class name'); $db->isTransactionSupported() && $db->startTransaction(); $data = new ModuleInstance(); $data->site_id = $siteId; $data->instance = $name; $data->module = $module; $data->visible = in_array($access, array('none','user','all','acl')) ? $access : 'none'; $db->table(self::$TBL_INST)->insert($data); $mod = new $module(); if ($mod instanceof iInstanceModule) $mod->createInstance($siteId, $name); $db->commit(); } /** * Removes the basic instance info rows of a module from the database. * * @param integer $siteId * the id of the site * @param string $name * the instance name */ public static function removeInstance($siteId, $name) { $db = System::getInstance()->getDB(); $db->isTransactionSupported() && $db->startTransaction(); ACLSvc::clearInstanceACL($siteId, $name); $search = array(); $search['site_id'] = $siteId; $search['instance'] = $name; $r = $db->table(self::$TBL_INST)->select($search); $o = $r->fetchObject(); $r->close(); $module = $o->module; if (class_exists($module)) { $reflect = new \ReflectionClass($module); if($reflect->implementsInterface('\\MCMS\\Module\\iModule')) { $mod = new $module(); if ($mod instanceof iInstanceModule) $mod->removeInstance($siteId, $name); } } $db->table(self::$TBL_INST)->delete($search); $db->isTransactionSupported() && $db->commit(); } }