Added UserSetting & SpaceSetting Models to store permanent settings

This commit is contained in:
Lucas Bartholemy 2014-05-07 19:31:30 +02:00
parent 58fd1eca15
commit bc0fbf383d
6 changed files with 421 additions and 4 deletions

View File

@ -0,0 +1,38 @@
<?php
class m140507_171527_create_settings_table extends EDbMigration {
public function up() {
// Create New User Settings Table
$this->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()
{
}
*/
}

View File

@ -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;
}

View File

@ -0,0 +1,167 @@
<?php
/**
* SpaceSettings allows permanent storage of space specific variables.
*
* This is the model class for table "space_setting".
*
* The followings are the available columns in table 'space_setting':
* @property integer $id
* @property integer $space_id
* @property string $module_id
* @property string $name
* @property string $value
* @property string $created_at
* @property integer $created_by
* @property string $updated_at
* @property integer $updated_by
*
* @package humhub.modules_core.space.models
* @since 0.5
* @author Luke
*/
class SpaceSetting extends HActiveRecord {
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return SpaceSetting the static model class
*/
public static function model($className = __CLASS__) {
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName() {
return 'space_setting';
}
/**
* @return array validation rules for model attributes.
*/
public function rules() {
return array(
array('space_id, created_by, updated_by', 'numerical', 'integerOnly' => 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;
}
}

View File

@ -0,0 +1,38 @@
<?php
class m140507_150421_create_settings_table extends EDbMigration {
public function up() {
// Create New User Settings Table
$this->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()
{
}
*/
}

View File

@ -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;
}
}

View File

@ -0,0 +1,167 @@
<?php
/**
* UserSettings allows permanent storage of user specific variables.
*
* This is the model class for table "user_setting".
*
* The followings are the available columns in table 'user_setting':
* @property integer $id
* @property integer $user_id
* @property string $module_id
* @property string $name
* @property string $value
* @property string $created_at
* @property integer $created_by
* @property string $updated_at
* @property integer $updated_by
*
* @package humhub.modules_core.user.models
* @since 0.5
* @author Luke
*/
class UserSetting extends HActiveRecord {
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return UserSetting the static model class
*/
public static function model($className = __CLASS__) {
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName() {
return 'user_setting';
}
/**
* @return array validation rules for model attributes.
*/
public function rules() {
return array(
array('user_id, created_by, updated_by', 'numerical', 'integerOnly' => 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;
}
}