diff --git a/src/IDF/Gconf.php b/src/IDF/Gconf.php new file mode 100644 index 0000000..4bbec62 --- /dev/null +++ b/src/IDF/Gconf.php @@ -0,0 +1,179 @@ +_a['table'] = 'idf_gconf'; + $this->_a['model'] = __CLASS__; + $this->_a['cols'] = array( + // It is mandatory to have an "id" column. + 'id' => + array( + 'type' => 'Pluf_DB_Field_Sequence', + //It is automatically added. + 'blank' => true, + ), + 'model_class' => + array( + 'type' => 'Pluf_DB_Field_Varchar', + 'blank' => false, + 'size' => 150, + 'verbose' => __('model class'), + ), + 'model_id' => + array( + 'type' => 'Pluf_DB_Field_Integer', + 'blank' => false, + 'verbose' => __('model id'), + ), + 'vkey' => + array( + 'type' => 'Pluf_DB_Field_Varchar', + 'blank' => false, + 'size' => 50, + 'verbose' => __('key'), + ), + 'vdesc' => + array( + 'type' => 'Pluf_DB_Field_Text', + 'blank' => false, + 'verbose' => __('value'), + ), + ); + $this->_a['idx'] = array('model_vkey_idx' => + array( + 'col' => 'model_class, model_id, vkey', + 'type' => 'unique', + ), + ); + $this->f = new IDF_Config_DataProxy($this); + } + + function setModel($model) + { + $this->datacache = null; + $this->_mod = $model; + } + + function initCache() + { + $this->datacache = array(); + $this->dirty = array(); + $sql = new Pluf_SQL('model_class=%s AND model_id=%s', + array($this->_mod->_model, $this->_mod->id)); + foreach ($this->getList(array('filter' => $sql->gen())) as $val) { + $this->datacache[$val->vkey] = $val->vdesc; + $this->dirty[$val->vkey] = $val->id; + } + } + + /** + * FIXME: This is not efficient when setting a large number of + * values in a loop. + */ + function setVal($key, $value) + { + if (!is_null($this->getVal($key, null)) + and $value == $this->getVal($key)) { + return; + } + if (isset($this->dirty[$key])) { + // we get to check if deleted by other process + update + $conf = new IDF_Gconf($this->dirty[$key]); + if ($conf->id == $this->dirty[$key]) { + $conf->vdesc = $value; + $conf->update(); + $this->datacache[$key] = $value; + return; + } + } + // we insert + $conf = new IDF_Gconf(); + $conf->model_class = $this->_mod->_model; + $conf->model_id = $this->_mod->id; + $conf->vkey = $key; + $conf->vdesc = $value; + $conf->create(); + $this->datacache[$key] = $value; + $this->dirty[$key] = $conf->id; + } + + function getVal($key, $default='') + { + if ($this->datacache === null) { + $this->initCache(); + } + return (isset($this->datacache[$key])) ? $this->datacache[$key] : $default; + } + + function delVal($key, $initcache=true) + { + $gconf = new IDF_Gconf(); + $sql = new Pluf_SQL('vkey=%s AND model_class=%s AND model_id=%s', array($key, $this->_mod->_model, $this->_mod->id)); + foreach ($gconf->getList(array('filter' => $sql->gen())) as $c) { + $c->delete(); + } + if ($initcache) { + $this->initCache(); + } + } + + /** + * Drop the conf of a model. + * + * If your model is using this table, just add the following line + * in your preDelete() method: + * + * IDF_Gconf::dropForModel($this) + * + * It will take care of the cleaning. + */ + static public function dropForModel($model) + { + $table = Pluf::factory(__CLASS__)->getSqlTable(); + $sql = new Pluf_SQL('model_class=%s AND model_id=%s', + array($model->_model, $model->id)); + $db = &Pluf::db(); + $db->execute('DELETE FROM '.$table.' WHERE '.$sql->gen()); + } + + static public function dropUser($signal, &$params) + { + self::dropForModel($params['user']); + } +} diff --git a/src/IDF/Migrations/15AddGconf.php b/src/IDF/Migrations/15AddGconf.php new file mode 100644 index 0000000..d783259 --- /dev/null +++ b/src/IDF/Migrations/15AddGconf.php @@ -0,0 +1,53 @@ +model = new $model(); + $schema->createTables(); + } +} + +function IDF_Migrations_15AddGconf_down($params=null) +{ + $models = array( + 'IDF_Gconf', + ); + $db = Pluf::db(); + $schema = new Pluf_DB_Schema($db); + foreach ($models as $model) { + $schema->model = new $model(); + $schema->dropTables(); + } +} \ No newline at end of file diff --git a/src/IDF/Migrations/Backup.php b/src/IDF/Migrations/Backup.php index a90d433..f3db0ab 100644 --- a/src/IDF/Migrations/Backup.php +++ b/src/IDF/Migrations/Backup.php @@ -52,6 +52,7 @@ function IDF_Migrations_Backup_run($folder, $name=null) 'IDF_Key', 'IDF_Scm_Cache_Git', 'IDF_Queue', + 'IDF_Gconf', ); $db = Pluf::db(); // Now, for each table, we dump the content in json, this is a @@ -96,6 +97,7 @@ function IDF_Migrations_Backup_restore($folder, $name) 'IDF_Key', 'IDF_Scm_Cache_Git', 'IDF_Queue', + 'IDF_Gconf', ); $db = Pluf::db(); $schema = new Pluf_DB_Schema($db); diff --git a/src/IDF/Migrations/Install.php b/src/IDF/Migrations/Install.php index a3d42dd..5d5a57c 100644 --- a/src/IDF/Migrations/Install.php +++ b/src/IDF/Migrations/Install.php @@ -49,6 +49,7 @@ function IDF_Migrations_Install_setup($params=null) 'IDF_Key', 'IDF_Scm_Cache_Git', 'IDF_Queue', + 'IDF_Gconf', ); $db = Pluf::db(); $schema = new Pluf_DB_Schema($db); @@ -86,6 +87,7 @@ function IDF_Migrations_Install_teardown($params=null) $perm = Pluf_Permission::getFromString('IDF.project-authorized-user'); if ($perm) $perm->delete(); $models = array( + 'IDF_Gconf', 'IDF_Queue', 'IDF_Scm_Cache_Git', 'IDF_Key',