diff --git a/protected/modules_core/space/migrations/m140507_171527_create_settings_table.php b/protected/modules_core/space/migrations/m140507_171527_create_settings_table.php new file mode 100644 index 0000000000..b495fecd66 --- /dev/null +++ b/protected/modules_core/space/migrations/m140507_171527_create_settings_table.php @@ -0,0 +1,38 @@ +createTable('space_setting', array( + 'id' => 'pk', + 'space_id' => 'int(10)', + 'module_id' => 'varchar(100) DEFAULT NULL', + 'name' => 'varchar(255)', + 'value' => 'varchar(255) DEFAULT NULL', + 'created_at' => 'datetime DEFAULT NULL', + 'created_by' => 'int(11) DEFAULT NULL', + 'updated_at' => 'datetime DEFAULT NULL', + 'updated_by' => 'int(11) DEFAULT NULL', + ), ''); + + $this->createIndex('idx_space_setting', 'space_setting', 'space_id, module_id, name', true); + } + + public function down() { + echo "m140507_171527_create_settings_table does not support migration down.\n"; + return false; + } + + /* + // Use safeUp/safeDown to do migration with transaction + public function safeUp() + { + } + + public function safeDown() + { + } + */ +} diff --git a/protected/modules_core/space/models/Space.php b/protected/modules_core/space/models/Space.php index 029ec98756..8b0c7ad4af 100644 --- a/protected/modules_core/space/models/Space.php +++ b/protected/modules_core/space/models/Space.php @@ -252,11 +252,15 @@ class Space extends HActiveRecordContentContainer implements ISearchable { } /** - * Before Delete of a User - * + * Before deletion of a Space */ protected function beforeDelete() { if (parent::beforeDelete()) { + + foreach (SpaceSetting::model()->findAllByAttributes(array('space_id'=>$this->id)) as $spaceSetting) { + $spaceSetting->delete(); + } + HSearch::getInstance()->deleteModel($this); return true; } diff --git a/protected/modules_core/space/models/SpaceSetting.php b/protected/modules_core/space/models/SpaceSetting.php new file mode 100644 index 0000000000..7aa39d60c3 --- /dev/null +++ b/protected/modules_core/space/models/SpaceSetting.php @@ -0,0 +1,167 @@ + true), + array('module_id', 'length', 'max' => 100), + array('name, value', 'length', 'max' => 255), + array('created_at, updated_at', 'safe'), + ); + } + + /** + * Returns the Cache ID for this SpaceSetting Entry + * + * @return String + */ + public function getCacheId() { + return "SpaceSetting_" . $this->space_id . "_" . $this->name . "_" . $this->module_id; + } + + public function beforeSave() { + Yii::app()->cache->delete($this->getCacheId()); + RuntimeCache::Remove($this->getCacheId()); + + return parent::beforeSave(); + } + + public function beforeDelete() { + Yii::app()->cache->delete($this->getCacheId()); + RuntimeCache::Remove($this->getCacheId()); + + return parent::beforeDelete(); + } + + /** + * Add or update an Space setting + * + * @param type $spaceId + * @param type $name + * @param type $value + * @param type $moduleId + */ + public static function Set($spaceId, $name, $value, $moduleId = "") { + $record = self::GetRecord($spaceId, $name, $moduleId); + $record->value = $value; + $record->name = $name; + $record->module_id = $moduleId; + + if ($moduleId != "") + $record->module_id = $moduleId; + + if ($value == "") { + if (!$record->isNewRecord) + $record->delete(); + } else { + $record->save(); + } + } + + /** + * Returns an Space Setting + * + * @param type $spaceId + * @param type $name + * @param type $moduleId + * @return type + */ + public static function Get($spaceId, $name, $moduleId = "") { + $record = self::GetRecord($spaceId, $name, $moduleId); + return $record->value; + } + + /** + * Returns a settings record by Name and Module Id + * The result is cached. + * + * @param type $spaceId + * @param type $name + * @param type $moduleId + * @return \HSetting + */ + private static function GetRecord($spaceId, $name, $moduleId = "") { + + $cacheId = 'SpaceSetting_' . $spaceId . '_' . $name . '_' . $moduleId; + + // Check if stored in Runtime Cache + if (RuntimeCache::Get($cacheId) !== false) { + return RuntimeCache::Get($cacheId); + } + + // Check if stored in Cache + $cacheValue = Yii::app()->cache->get($cacheId); + if ($cacheValue !== false) { + return $cacheValue; + } + + $condition = ""; + $params = array('name' => $name); + $params = array('space_id' => $spaceId); + + if ($moduleId != "") { + $params['module_id'] = $moduleId; + } else { + $condition = "module_id IS NULL"; + } + + $record = SpaceSetting::model()->findByAttributes($params, $condition); + + if ($record == null) { + $record = new SpaceSetting; + $record->space_id = $spaceId; + $record->module_id = $moduleId; + $record->name = $name; + } else { + $expireTime = 3600; + if ($record->name != 'expireTime' && $record->module_id != "cache") + $expireTime = HSetting::Get('expireTime', 'cache'); + + Yii::app()->cache->set($cacheId, $record, $expireTime); + RuntimeCache::Set($cacheId, $record); + } + + return $record; + } + +} diff --git a/protected/modules_core/user/migrations/m140507_150421_create_settings_table.php b/protected/modules_core/user/migrations/m140507_150421_create_settings_table.php new file mode 100644 index 0000000000..f7b6dc3e79 --- /dev/null +++ b/protected/modules_core/user/migrations/m140507_150421_create_settings_table.php @@ -0,0 +1,38 @@ +createTable('user_setting', array( + 'id' => 'pk', + 'user_id' => 'int(10)', + 'module_id' => 'varchar(100) DEFAULT NULL', + 'name' => 'varchar(255)', + 'value' => 'varchar(255) DEFAULT NULL', + 'created_at' => 'datetime DEFAULT NULL', + 'created_by' => 'int(11) DEFAULT NULL', + 'updated_at' => 'datetime DEFAULT NULL', + 'updated_by' => 'int(11) DEFAULT NULL', + ), ''); + + $this->createIndex('idx_user_setting', 'user_setting', 'user_id, module_id, name', true); + } + + public function down() { + echo "m140507_150421_create_settings_table does not support migration down.\n"; + return false; + } + + /* + // Use safeUp/safeDown to do migration with transaction + public function safeUp() + { + } + + public function safeDown() + { + } + */ +} diff --git a/protected/modules_core/user/models/User.php b/protected/modules_core/user/models/User.php index 03ab7d390b..8866eba47d 100644 --- a/protected/modules_core/user/models/User.php +++ b/protected/modules_core/user/models/User.php @@ -337,10 +337,13 @@ class User extends HActiveRecordContentContainer implements ISearchable { */ public function beforeDelete() { if (parent::beforeDelete()) { + + foreach (UserSetting::model()->findAllByAttributes(array('user_id'=>$this->id)) as $userSetting) { + $userSetting->delete(); + } + HSearch::getInstance()->deleteModel($this); - - return true; } } diff --git a/protected/modules_core/user/models/UserSetting.php b/protected/modules_core/user/models/UserSetting.php new file mode 100644 index 0000000000..a735c93a1a --- /dev/null +++ b/protected/modules_core/user/models/UserSetting.php @@ -0,0 +1,167 @@ + true), + array('module_id', 'length', 'max' => 100), + array('name, value', 'length', 'max' => 255), + array('created_at, updated_at', 'safe'), + ); + } + + /** + * Returns the Cache ID for this UserSetting Entry + * + * @return String + */ + public function getCacheId() { + return "UserSetting_" . $this->user_id . "_" . $this->name . "_" . $this->module_id; + } + + public function beforeSave() { + Yii::app()->cache->delete($this->getCacheId()); + RuntimeCache::Remove($this->getCacheId()); + + return parent::beforeSave(); + } + + public function beforeDelete() { + Yii::app()->cache->delete($this->getCacheId()); + RuntimeCache::Remove($this->getCacheId()); + + return parent::beforeDelete(); + } + + /** + * Add or update an User setting + * + * @param type $userId + * @param type $name + * @param type $value + * @param type $moduleId + */ + public static function Set($userId, $name, $value, $moduleId = "") { + $record = self::GetRecord($userId, $name, $moduleId); + $record->value = $value; + $record->name = $name; + $record->module_id = $moduleId; + + if ($moduleId != "") + $record->module_id = $moduleId; + + if ($value == "") { + if (!$record->isNewRecord) + $record->delete(); + } else { + $record->save(); + } + } + + /** + * Returns an User Setting + * + * @param type $userId + * @param type $name + * @param type $moduleId + * @return type + */ + public static function Get($userId, $name, $moduleId = "") { + $record = self::GetRecord($userId, $name, $moduleId); + return $record->value; + } + + /** + * Returns a settings record by Name and Module Id + * The result is cached. + * + * @param type $userId + * @param type $name + * @param type $moduleId + * @return \HSetting + */ + private static function GetRecord($userId, $name, $moduleId = "") { + + $cacheId = 'UserSetting_' . $userId . '_' . $name . '_' . $moduleId; + + // Check if stored in Runtime Cache + if (RuntimeCache::Get($cacheId) !== false) { + return RuntimeCache::Get($cacheId); + } + + // Check if stored in Cache + $cacheValue = Yii::app()->cache->get($cacheId); + if ($cacheValue !== false) { + return $cacheValue; + } + + $condition = ""; + $params = array('name' => $name); + $params = array('user_id' => $userId); + + if ($moduleId != "") { + $params['module_id'] = $moduleId; + } else { + $condition = "module_id IS NULL"; + } + + $record = UserSetting::model()->findByAttributes($params, $condition); + + if ($record == null) { + $record = new UserSetting; + $record->user_id = $userId; + $record->module_id = $moduleId; + $record->name = $name; + } else { + $expireTime = 3600; + if ($record->name != 'expireTime' && $record->module_id != "cache") + $expireTime = HSetting::Get('expireTime', 'cache'); + + Yii::app()->cache->set($cacheId, $record, $expireTime); + RuntimeCache::Set($cacheId, $record); + } + + return $record; + } + +}