diff options
author | Tim Almdal <tnalmdal@shaw.ca> | 2008-12-10 19:44:58 +0000 |
---|---|---|
committer | Tim Almdal <tnalmdal@shaw.ca> | 2008-12-10 19:44:58 +0000 |
commit | 7e5935d5325a7bc20fb5603a76a5af4d7df499a9 (patch) | |
tree | 08728a94aefb5df32bb084ee676ec81a3c4cd9c3 | |
parent | 18a6614a11cf39a29f5705edabc710688da357e6 (diff) |
Create a module parameter table. This will be useful if a module wants to store information, but is not enough to warrant a table of its own
-rw-r--r-- | core/helpers/core_installer.php | 10 | ||||
-rw-r--r-- | core/helpers/module.php | 24 | ||||
-rw-r--r-- | core/models/module.php | 1 | ||||
-rw-r--r-- | core/models/parameter.php | 22 | ||||
-rw-r--r-- | core/tests/Parameters_Test.php | 34 |
5 files changed, 91 insertions, 0 deletions
diff --git a/core/helpers/core_installer.php b/core/helpers/core_installer.php index 052b9cb5..95d46597 100644 --- a/core/helpers/core_installer.php +++ b/core/helpers/core_installer.php @@ -82,6 +82,15 @@ class core_installer { UNIQUE KEY(`name`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;"); + $db->query("CREATE TABLE `parameters` ( + `id` int(9) NOT NULL auto_increment, + `module_id` int(9), + `name` char(255) NOT NULL, + `value` text, + PRIMARY KEY (`id`), + UNIQUE KEY(`module_id`, `name`)) + ENGINE=InnoDB DEFAULT CHARSET=utf8;"); + foreach (array("albums", "resizes") as $dir) { @mkdir(VARPATH . $dir); } @@ -115,6 +124,7 @@ class core_installer { $db->query("DROP TABLE IF EXISTS `permissions`;"); $db->query("DROP TABLE IF EXISTS `items`;"); $db->query("DROP TABLE IF EXISTS `modules`;"); + $db->query("DROP TABLE IF EXISTS `parameters`;"); system("/bin/rm -rf " . VARPATH . "albums"); system("/bin/rm -rf " . VARPATH . "resizes"); } diff --git a/core/helpers/module.php b/core/helpers/module.php index c841ca94..cc1823b4 100644 --- a/core/helpers/module.php +++ b/core/helpers/module.php @@ -88,4 +88,28 @@ class module_Core { } catch (Exception $e) { } } + + public function get_parameter($module_name, $name, $default_value=null) { + $module = ORM::factory("module")->where("name", $module_name)->find(); + $parameter = ORM::factory("parameter") + ->where("module_id", $module->id) + ->where("name", $name) + ->find(); + return $parameter->loaded ? $parameter->value : $default_value; + } + + public function set_parameter($module_name, $name, $value) { + $module = ORM::factory("module")->where("name", $module_name)->find(); + $parameter = ORM::factory("parameter") + ->where("module_id", $module->id) + ->where("name", $name) + ->find(); + if (!$parameter->loaded) { + $parameter = ORM::factory("parameter"); + $parameter->module_id = $module->id; + $parameter->name = $name; + } + $parameter->value = $value; + $parameter->save(); + } } diff --git a/core/models/module.php b/core/models/module.php index de4cc43d..5229f42d 100644 --- a/core/models/module.php +++ b/core/models/module.php @@ -18,4 +18,5 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class Module_Model extends ORM { + protected $has_many = array("parameters"); } diff --git a/core/models/parameter.php b/core/models/parameter.php new file mode 100644 index 00000000..9817ff2f --- /dev/null +++ b/core/models/parameter.php @@ -0,0 +1,22 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2008 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ +class Parameter_Model extends ORM { + protected $belongs_to = array("module"); +} diff --git a/core/tests/Parameters_Test.php b/core/tests/Parameters_Test.php new file mode 100644 index 00000000..7f6d33d1 --- /dev/null +++ b/core/tests/Parameters_Test.php @@ -0,0 +1,34 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2008 Bharat Mediratta + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. + */ +class Parameters_Test extends Unit_Test_Case { + public function add_parameter_test() { + module::set_parameter("core", "Parameter", "original value"); + $this->assert_equal("original value", module::get_parameter("core", "Parameter")); + + module::set_parameter("core", "Parameter", "updated value"); + $this->assert_equal("updated value", module::get_parameter("core", "Parameter")); + + module::set_parameter("core", "Parameter2", "new parameter"); + $core = module::get("core"); + + $expected = array("Parameter" => "updated value", "Parameter2" => "new parameter"); + $this->assert_equal($expected, $core->parameters->select_list("name", "value")); + } +}
\ No newline at end of file |