Skip to content
Browse files

Merge pull request #23 from FlorianLB/master

ticket #237 - Implementation of jPref
  • Loading branch information...
2 parents 9d22ecf + b8200cd commit ce7b54dafba1600463ef4696e32a56e100f41417 @laurentj laurentj committed Feb 15, 2012
Showing with 909 additions and 6 deletions.
  1. +20 −0 build/manifests/jelix-admin-modules.mn
  2. +6 −0 build/manifests/jelix-lib.mn
  3. +1 −0 build/manifests/jelix-scripts.mn
  4. +1 −0 build/manifests/jelix-www.mn
  5. +2 −0 build/manifests/testapp.mn
  6. +23 −0 lib/jelix-admin-modules/jpref_admin/classes/jpref_admin.listener.php
  7. +163 −0 lib/jelix-admin-modules/jpref_admin/controllers/prefs.classic.php
  8. +6 −0 lib/jelix-admin-modules/jpref_admin/events.xml
  9. +26 −0 lib/jelix-admin-modules/jpref_admin/forms/pref.form.xml
  10. +21 −0 lib/jelix-admin-modules/jpref_admin/install/install.php
  11. +26 −0 lib/jelix-admin-modules/jpref_admin/locales/en_EN/admin.UTF-8.properties
  12. +26 −0 lib/jelix-admin-modules/jpref_admin/locales/fr_FR/admin.UTF-8.properties
  13. +16 −0 lib/jelix-admin-modules/jpref_admin/module.xml
  14. +6 −0 lib/jelix-admin-modules/jpref_admin/templates/pref_edit.tpl
  15. +80 −0 lib/jelix-admin-modules/jpref_admin/templates/prefs_index.tpl
  16. +14 −0 lib/jelix-admin-modules/jpref_admin/urls.xml
  17. +1 −0 lib/jelix-scripts/commands-single/createapp.cmd.php
  18. +6 −2 lib/jelix-scripts/commands/initadmin.cmd.php
  19. +27 −0 lib/jelix-scripts/templates/var/config/preferences.ini.php.tpl
  20. +5 −0 lib/jelix-scripts/templates/var/config/profiles.ini.php.tpl
  21. BIN lib/jelix-www/design/images/cog.png
  22. +1 −2 lib/jelix/CREDITS
  23. +2 −2 lib/jelix/init.php
  24. +104 −0 lib/jelix/pref/jPref.class.php
  25. +113 −0 lib/jelix/pref/jPrefItem.class.php
  26. +36 −0 lib/jelix/pref/jPrefItemGroup.class.php
  27. +148 −0 lib/jelix/pref/jPrefManager.class.php
  28. +25 −0 testapp/modules/jelix_tests/tests/jpref.main_api.pu.php
  29. +4 −0 testapp/var/config/profiles.ini.php.dist
View
20 build/manifests/jelix-admin-modules.mn
@@ -79,4 +79,24 @@ cd lib/jelix-admin-modules/jauthdb_admin/templates
password_change.tpl
user_edit.tpl
user_view.tpl
+
+cd lib/jelix-admin-modules/jpref_admin
+* module.xml
+ events.xml
+ urls.xml
+cd lib/jelix-admin-modules/jpref_admin/classes/
+ jpref_admin.listener.php
+cd lib/jelix-admin-modules/jpref_admin/controllers
+ prefs.classic.php
+cd lib/jelix-admin-modules/jpref_admin/forms
+ pref.form.xml
+cd lib/jelix-admin-modules/jpref_admin/install
+ install.php
+cd lib/jelix-admin-modules/jpref_admin/locales/en_EN
+cch admin.UTF-8.properties(admin.%charset%.properties)
+cd lib/jelix-admin-modules/jpref_admin/locales/fr_FR
+cch admin.UTF-8.properties(admin.%charset%.properties)
+cd lib/jelix-admin-modules/jpref_admin/templates
+ prefs_index.tpl
+ pref_edit.tpl
View
6 build/manifests/jelix-lib.mn
@@ -368,6 +368,12 @@ cd lib/jelix/kvdb
* jKVDriver.class.php
jKVDb.class.php
+cd lib/jelix/pref
+ jPref.class.php
+ jPrefItem.class.php
+ jPrefItemGroup.class.php
+ jPrefManager.class.php
+
cd lib/jelix/tpl/
* jTpl.class.php
* jTplCompiler.class.php
View
1 build/manifests/jelix-scripts.mn
@@ -84,6 +84,7 @@ cd lib/jelix-scripts/templates/var/config/
jacl2.coord.ini.php.tpl
installer.ini.php.tpl
profiles.ini.php.tpl
+ preferences.ini.php.tpl
urls.xml.tpl
cd lib/jelix-scripts/templates/var/config/index/
config.ini.php.tpl
View
1 build/manifests/jelix-www.mn
@@ -30,6 +30,7 @@ cd lib/jelix-www/design/images
group.png
rights.png
user.png
+ cog.png
cd lib/jelix-www/js
c tooltip.js
c jforms_light.js
View
2 build/manifests/testapp.mn
@@ -208,6 +208,7 @@ cd testapp/modules/jelix_tests/tests/
core.urls_parsing.html_cli.php
daotests.lib.php
events.simple_test.html_cli.php
+ httpcache.pu.php
installer.lib.php
installer.main.html_cli.php
installer.misc.html_cli.php
@@ -263,6 +264,7 @@ cd testapp/modules/jelix_tests/tests/
jkvdb.file.html_cli.php
jkvdb.redis.html_cli.php
jkvdb.lib.php
+ jpref.main_api.pu.php
jtpl.compiler.html_cli.php
jtpl.expressions_parsing.html_cli.php
jtpl.fetch.html_cli.php
View
23 lib/jelix-admin-modules/jpref_admin/classes/jpref_admin.listener.php
@@ -0,0 +1,23 @@
+<?php
+/**
+* @package jelix_admin_modules
+* @subpackage jpref_admin
+* @author Florian Lonqueu-Brochard
+* @copyright 2012 Florian Lonqueu-Brochard
+* @link http://jelix.org
+* @licence http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
+*/
+
+class jpref_adminListener extends jEventListener{
+
+ /**
+ *
+ */
+ function onmasteradminGetMenuContent ($event) {
+ if (jAcl2::check('auth.users.list')) {
+ $item = new masterAdminMenuItem('pref', jLocale::get('jpref_admin~admin.item.title'), jUrl::get('jpref_admin~prefs:index'), 50, 'system');
+ $item->icon = jApp::config()->urlengine['jelixWWWPath'] . 'design/images/cog.png';
+ $event->add($item);
+ }
+ }
+}
View
163 lib/jelix-admin-modules/jpref_admin/controllers/prefs.classic.php
@@ -0,0 +1,163 @@
+<?php
+/**
+* @package jelix_admin_modules
+* @subpackage jpref_admin
+* @author Florian Lonqueu-Brochard
+* @copyright 2012 Florian Lonqueu-Brochard
+* @link http://jelix.org
+* @licence http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
+*/
+
+class prefsCtrl extends jController {
+
+ public $pluginParams=array(
+ '*' =>array('jacl2.right'=>'jprefs.prefs.list')
+ );
+
+
+ /**
+ *
+ */
+ public function index(){
+ $rep = $this->getResponse('html');
+
+ $tpl = new jTpl();
+ $tpl->assign('prefs',jPrefManager::getAllPreferences());
+ $rep->body->assign('MAIN', $tpl->fetch('prefs_index'));
+
+ return $rep;
+ }
+
+
+ /**
+ *
+ */
+ public function edit(){
+ $rep = $this->getResponse('html');
+ $id = $this->param('id', 0);
+
+
+ $pref = jPrefManager::getPref($id);
+
+ if(!$pref){
+ $rep = $this->getResponse('redirect');
+ $rep->action = 'jpref_admin~prefs:index';
+ return $rep;
+ }
+
+ if(!$pref->isWritable() ){
+ $rep = $this->getResponse('redirect');
+ $rep->action = 'jpref_admin~prefs:index';
+ jMessage::add(jLocale::get('jelix~errors.acl.action.right.needed'), 'error');
+ return $rep;
+ }
+
+ $form = jForms::create('jpref_admin~pref', $id);
+
+ $ctrls = array('integer', 'string', 'boolean', 'decimal');
+ foreach($ctrls as $c){
+ $form->deactivate($c);
+ }
+ $form->deactivate($pref->type, false);
+
+ $label = !empty($pref->locale) ? jLocale::get($pref->locale) : $pref->id;
+ $control = $form->getControl($pref->type);
+ $control->label = $label;
+ if($pref->type != 'boolean')
+ $control->help = jLocale::get('jpref_admin~admin.help.'.$pref->type);
+ if(!empty($pref->value))
+ $form->setData($pref->type, $pref->value);
+
+ $tpl = new jTpl();
+ $tpl->assign('form', $form);
+ $tpl->assign('title', jLocale::get('jpref_admin~admin.pref.edit', array($label)));
+ $tpl->assign('id', $id);
+ $tpl->assign('field', $pref->type);
+ $rep->body->assign('MAIN', $tpl->fetch('pref_edit'));
+
+ return $rep;
+ }
+
+ /**
+ *
+ */
+ public function saveedit(){
+ $rep = $this->getResponse('redirect');
+ $rep->action = 'jpref_admin~prefs:index';
+
+ $id = $this->param('id', 0);
+ $field = $this->param('field');
+
+ $form = jForms::fill('jpref_admin~pref', $id);
+ if(!$form || !$id || !$field){
+ return $rep;
+ }
+
+
+ if(!$form->check()){
+ $form->setErrorOn($field, 'jpref_admin~admin.field.error');
+ $rep->action = 'jpref_admin~prefs:edit';
+ $rep->params = array('id' => $id);
+ return $rep;
+ }
+
+ $data = $form->getData($field);
+ if($field == 'boolean'){
+ if($data == 'false')
+ $data = false;
+ else
+ $data = true;
+ }
+ elseif ($field == 'integer')
+ $data = (int) $data;
+ elseif ($field == 'decimal')
+ $data = (float) $data;
+
+ jPref::set($id, $data);
+
+ jMessage::add(jLocale::get('jpref_admin~admin.message.pref.updated'), 'notice');
+ return $rep;
+ }
+
+
+
+ /**
+ *
+ */
+ public function reset(){
+ $rep = $this->getResponse('redirect');
+ $rep->action = 'jpref_admin~prefs:index';
+
+ $id = $this->param('id', 0);
+
+ $pref = jPrefManager::getPref($id);
+ if(!$id || !$pref || (empty($pref->default_value) && $pref->type != 'boolean')){
+ return $rep;
+ }
+
+ if(!$pref->isWritable() ){
+ jMessage::add(jLocale::get('jelix~errors.acl.action.right.needed'), 'error');
+ return $rep;
+ }
+
+
+ $dvalue = $pref->default_value;
+ if ($pref->type == 'integer')
+ $dvalue = (int) $dvalue;
+ elseif ($pref->type == 'decimal')
+ $dvalue = (float) $dvalue;
+ elseif ($pref->type == 'boolean'){
+ if($dvalue == 'false')
+ $dvalue= false;
+ else if($dvalue == 'true')
+ $dvalue = true;
+ }
+
+ jPref::set($pref->id, $dvalue);
+
+ jMessage::add(jLocale::get('jpref_admin~admin.message.pref.reseted'), 'notice');
+ return $rep;
+
+ }
+}
+
View
6 lib/jelix-admin-modules/jpref_admin/events.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<events xmlns="http://jelix.org/ns/events/1.0">
+ <listener name="jpref_admin">
+ <event name="masteradminGetMenuContent" />
+ </listener>
+</events>
View
26 lib/jelix-admin-modules/jpref_admin/forms/pref.form.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="utf-8"?>
+<form xmlns="http://jelix.org/ns/forms/1.1">
+
+ <input ref="string">
+ <label>.</label>
+ </input>
+
+ <input ref="integer" type="integer">
+ <label>.</label>
+ </input>
+
+ <input ref="decimal" type="decimal">
+ <label>.</label>
+ </input>
+
+ <radiobuttons ref="boolean">
+ <label>.</label>
+ <item value="true" locale="jelix~ui.buttons.yes"/>
+ <item value="false" locale="jelix~ui.buttons.no"/>
+ </radiobuttons>
+
+ <submit ref="submit">
+ <label>Ok</label>
+ </submit>
+
+</form>
View
21 lib/jelix-admin-modules/jpref_admin/install/install.php
@@ -0,0 +1,21 @@
+<?php
+/**
+* @package jelix_admin_modules
+* @subpackage jpref_admin
+* @author Florian Lonqueu-Brochard
+* @copyright 2011 Florian Lonqueu-Brochard
+* @link http://jelix.org
+* @licence http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
+*/
+
+
+class jpref_adminModuleInstaller extends jInstallerModule {
+
+ function install() {
+ if ($this->firstExec('acl2')) {
+ jAcl2DbManager::addSubjectGroup('jprefs.prefs.management', 'jPref~admin.acl.grp.prefs.management');
+ jAcl2DbManager::addSubject('jprefs.prefs.list', 'jPref~admin.acl.prefs.list', 'jprefs.prefs.management');
+ jAcl2DbManager::addRight('admins', 'jprefs.prefs.list'); // for admin group
+ }
+ }
+}
View
26 lib/jelix-admin-modules/jpref_admin/locales/en_EN/admin.UTF-8.properties
@@ -0,0 +1,26 @@
+item.title = Preferences
+
+acl.grp.prefs.management = Preferences management
+acl.prefs.list = Show the preferences list
+
+prefs.list = Preferences list
+pref.edit = Modification of "%s"
+
+group.others = Others
+type.not.allowed = "%s" is a not allowed type for a preference. Allowed types are : "%s"
+no.prefs = There is no preference
+
+ui.pref.name = Name
+ui.pref.value = Value
+ui.pref.edit = Edit
+ui.pref.reset = Reset to
+
+message.cant.edit.pref = You can't edit this preference
+message.pref.updated = The preference has been updated
+message.pref.reseted = The preference has been reseted
+
+help.integer = Integer
+help.decimal = Number with or without comma
+help.string = String
+
+field.error = The field is invalid
View
26 lib/jelix-admin-modules/jpref_admin/locales/fr_FR/admin.UTF-8.properties
@@ -0,0 +1,26 @@
+item.title = Préférences
+
+acl.grp.prefs.management = Gestion des préférences
+acl.prefs.list = Voir la liste des préférences
+
+prefs.list = Liste des préférences
+pref.edit = Modification de "%s"
+
+group.others = Autres
+type.not.allowed = "%s" n'est pas un type autorisé pour une préférence. Les types autorisés sont : "%s"
+no.prefs = Il n'y a aucune préférence
+
+ui.pref.name = Nom
+ui.pref.value = Valeur
+ui.pref.edit = Modifier
+ui.pref.reset = Réinitialiser à
+
+message.cant.edit.pref = Vous ne pouvez pas modifier cette préférence
+message.pref.updated = La préférence a été mise à jour
+message.pref.reseted = La préférence a été réinitialisé
+
+help.integer = Nombre entier
+help.decimal = Nombre avec ou sans virgule
+help.string = Chaîne de caractères
+
+field.error = La saisie est invalide
View
16 lib/jelix-admin-modules/jpref_admin/module.xml
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module xmlns="http://jelix.org/ns/module/1.0">
+ <info id="jpref_admin@jelix.org" name="jpref_admin" createdate="2012-01-21">
+ <version stability="stable" date="2012-01-21">1.0</version>
+ <label lang="en_EN">jPref</label>
+ <description lang="en_EN" />
+ <license URL="http://www.gnu.org/licenses/lgpl.html">GNU Lesser General Public Licence</license>
+ <copyright>2011 Florian Lonqueu-Brochard</copyright>
+ <creator name="Florian Lonqueu-Brochard" email="dev@florianlb.fr" active="true"/>
+ <!--<contributor name="hisname" email="hisemail@yoursite.undefined" active="true" since="" role=""/>-->
+ <homepageURL>http://jelix.org</homepageURL>
+ </info>
+ <dependencies>
+#expand <jelix minversion="1.4pre.*" maxversion="__LIB_VERSION_MAX__"/>-->
+ </dependencies>
+</module>
View
6 lib/jelix-admin-modules/jpref_admin/templates/pref_edit.tpl
@@ -0,0 +1,6 @@
+<h1>{$title}</h1>
+
+{formfull $form, 'jpref_admin~prefs:saveedit', array('id' => $id, 'field' => $field)}
+
+<hr />
+<p><a href="{jurl 'jpref_admin~prefs:index'}" class="crud-link">{@jelix~crud.link.return.to.list@}</a></p>
View
80 lib/jelix-admin-modules/jpref_admin/templates/prefs_index.tpl
@@ -0,0 +1,80 @@
+<h1>{jlocale 'jpref_admin~admin.prefs.list'}</h1>
+
+{if count($prefs) == 0}
+ {jlocale 'jpref_admin~admin.no.prefs'}.
+{else}
+ <table class="records-list">
+ <thead>
+ <tr>
+ <th>{jlocale 'jpref_admin~admin.ui.pref.name'}</th><th>{jlocale 'jpref_admin~admin.ui.pref.value'}</th><th>&nbsp;</th><th>&nbsp;</th>
+ </tr>
+ </thead>
+
+ <tbody>
+ {foreach $prefs as $group}
+ {if count($group->prefs) > 0}
+ <tr>
+ <th colspan="4">
+ <h3>
+ {if $group->locale}
+ {jlocale $group->locale}
+ {else}
+ {$group->id}
+ {/if}
+ </h3>
+ </th>
+ </tr>
+
+ {foreach $group->prefs as $pref}
+ <tr class="{cycle array('odd','even')}">
+ <td>
+ {if !empty($pref->locale)}
+ {jlocale $pref->locale}
+ {else}
+ {$pref->id}
+ {/if}
+ </td>
+ <td>
+ {if $pref->value !== null}
+ {if is_bool($pref->value)}
+ {if $pref->value}
+ {jlocale 'jelix~ui.buttons.yes'}
+ {else}
+ {jlocale 'jelix~ui.buttons.no'}
+ {/if}
+ {else}
+ {$pref->value}
+ {/if}
+ {/if}
+ </td>
+ <td>
+ {if $pref->isWritable()}
+ <a href="{jurl 'jpref_admin~prefs:edit', array('id' => $pref->id)}">Modifier</a>
+ {/if}
+ </td>
+ <td>
+ {if $pref->isWritable() && $pref->default_value !== null && $pref->default_value != $pref->value}
+ <a href="{jurl 'jpref_admin~prefs:reset', array('id' => $pref->id)}">{jlocale 'jpref_admin~admin.ui.pref.reset'}
+ <em>
+ {if is_bool($pref->default_value)}
+ {if $pref->default_value}
+ {jlocale 'jelix~ui.buttons.yes'}
+ {else}
+ {jlocale 'jelix~ui.buttons.no'}
+ {/if}
+ {else}
+ {$pref->default_value}
+ {/if}
+ </em>
+ </a>
+ {/if}
+ </td>
+ </tr>
+ {/foreach}
+
+
+ {/if}
+ {/foreach}
+ </tbody>
+ </table>
+{/if}
View
14 lib/jelix-admin-modules/jpref_admin/urls.xml
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="utf-8"?>
+<suburls xmlns="http://jelix.org/ns/suburls/1.0">
+
+ <url pathinfo="/preferences" action="prefs:index"/>
+
+ <url pathinfo="/pref/edit/:id" action="prefs:edit">
+ <param name="id"/>
+ </url>
+
+ <url pathinfo="/pref/reset/:id" action="prefs:reset">
+ <param name="id"/>
+ </url>
+
+</suburls>
View
1 lib/jelix-scripts/commands-single/createapp.cmd.php
@@ -154,6 +154,7 @@ public function run() {
$this->createFile($appPath.'cmd.php','cmd.php.tpl', $param);
$this->createFile($configPath.'defaultconfig.ini.php', 'var/config/defaultconfig.ini.php.tpl', $param);
$this->createFile($configPath.'profiles.ini.php', 'var/config/profiles.ini.php.tpl', $param);
+ $this->createFile($configPath.'preferences.ini.php', 'var/config/preferences.ini.php.tpl', $param);
$this->createFile($configPath.'urls.xml', 'var/config/urls.xml.tpl', $param);
//$this->createFile(JELIX_APP_CONFIG_PATH.'installer.ini.php', 'var/config/installer.ini.php.tpl', $param);
$this->createFile($configPath.'index/config.ini.php', 'var/config/index/config.ini.php.tpl', $param);
View
8 lib/jelix-scripts/commands/initadmin.cmd.php
@@ -104,9 +104,9 @@ public function run(){
$urlconf = $inifile->getValue($entrypoint, 'simple_urlengine_entrypoints', null, true);
if ($urlconf === null || $urlconf == '') {
// in defaultconfig
- $inifile->setValue($entrypoint, 'jacl2db_admin~*@classic, jauthdb_admin~*@classic, master_admin~*@classic', 'simple_urlengine_entrypoints', null, true);
+ $inifile->setValue($entrypoint, 'jacl2db_admin~*@classic, jauthdb_admin~*@classic, master_admin~*@classic, jpref_admin~*@classic', 'simple_urlengine_entrypoints', null, true);
// in the config of the entry point
- $inifile->setValue($entrypoint, 'jacl2db~*@classic, jauth~*@classic, jacl2db_admin~*@classic, jauthdb_admin~*@classic, master_admin~*@classic', 'simple_urlengine_entrypoints');
+ $inifile->setValue($entrypoint, 'jacl2db~*@classic, jauth~*@classic, jacl2db_admin~*@classic, jauthdb_admin~*@classic, master_admin~*@classic, jpref_admin~*@classic', 'simple_urlengine_entrypoints');
}
else {
$urlconf2 = $inifile->getValue($entrypoint, 'simple_urlengine_entrypoints');
@@ -127,6 +127,8 @@ public function run(){
$urlconf2 .= ',jacl2db~*@classic';
if(strpos($urlconf2, 'jauth~*@classic') === false)
$urlconf2 .= ',jauth~*@classic';
+ if(strpos($urlconf2, 'jpref_admin~*@classic') === false)
+ $urlconf2 .= ',jpref_admin~*@classic';
$inifile->setValue($entrypoint, $urlconf, 'simple_urlengine_entrypoints', null, true);
$inifile->setValue($entrypoint, $urlconf2, 'simple_urlengine_entrypoints');
@@ -182,5 +184,7 @@ public function run(){
$inifile->setValue('jacl2db_admin.access', '0', 'modules');
$inifile->save();
}
+
+ $installer->installModules(array('jpref_admin'), $entrypoint.'.php');
}
}
View
27 lib/jelix-scripts/templates/var/config/preferences.ini.php.tpl
@@ -0,0 +1,27 @@
+;<?php die(''); ?>
+;for security reasons, don't remove or modify the first line
+
+; a preference is declared like this :
+;[pref:my.pref]
+; type of the preference "string" | "integer" | "boolean" | "decimal" (default string)
+;type=
+; locale key that describe the preference
+;locale =
+; identifier of the preference group
+;group =
+; acl_subject of the right required to read the preference in admin panel
+;read_acl_subject =
+; acl_subject of the right required to modify the preference in admin panel
+;write_acl_subject =
+; default value to restore from admin panel
+;default_value =
+
+; a group is declared like this :
+;[group:my.group]
+; locale key for the group
+;locale =
+; order to display in admin panel
+;order =
+
+
+
View
5 lib/jelix-scripts/templates/var/config/profiles.ini.php.tpl
@@ -67,6 +67,11 @@ host = "localhost:11211"
;file_umask =
+;----------------- jKVDb for jPref
+[jkvdb:jpref]
+;driver =
+
+
;----------------- jSoapClient
[jsoapclient]
View
BIN lib/jelix-www/design/images/cog.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
3 lib/jelix/CREDITS
@@ -229,8 +229,7 @@ Florian Lonqueu-Brochard (FlorianLB)
- système de cache http (#4)
- bcrypt algorithm for jAuth
- classe lecture d’un rss/atom distant (#23)
-
-
+ - implementation of jPref (#237)
Vincent Viaud (aka Litchi)
- bug fix in jforms_jquery.js (#1132)
View
4 lib/jelix/init.php
@@ -167,13 +167,13 @@
'Forms'=>JELIX_LIB_PATH.'forms/', 'Event'=>JELIX_LIB_PATH.'events/',
'Tpl'=>JELIX_LIB_PATH.'tpl/', 'Acl'=>JELIX_LIB_PATH.'acl/', 'Controller'=>JELIX_LIB_PATH.'controllers/',
'Auth'=>JELIX_LIB_PATH.'auth/', 'Installer'=>JELIX_LIB_PATH.'installer/',
- 'KV'=>JELIX_LIB_PATH.'kvdb/');
+ 'KV'=>JELIX_LIB_PATH.'kvdb/', 'Pref'=>JELIX_LIB_PATH.'pref/');
/**
* function used by php to try to load an unknown class
*/
function jelix_autoload($class) {
- if(preg_match('/^j(Dao|Tpl|Acl|Event|Db|Controller|Forms|Auth|Installer|KV).*/i', $class, $m)){
+ if(preg_match('/^j(Dao|Tpl|Acl|Event|Db|Controller|Forms|Auth|Installer|KV|Pref).*/i', $class, $m)){
$f=$GLOBALS['gLibPath'][$m[1]].$class.'.class.php';
}elseif(preg_match('/^cDao(?:Record)?_(.+)_Jx_(.+)_Jx_(.+)$/', $class, $m)){
// for DAO which are stored in sessions for example
View
104 lib/jelix/pref/jPref.class.php
@@ -0,0 +1,104 @@
+<?php
+/**
+* @package jelix
+* @subpackage pref
+* @author Florian Lonqueu-Brochard
+* @copyright 2012 Florian Lonqueu-Brochard
+* @link http://jelix.org
+* @licence http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
+*/
+
+
+class jPref{
+
+ protected function __construct (){ }
+
+ protected static $_connection;
+
+ protected static $_prefs;
+
+ protected static $_prefix = 'jpref_';
+
+ protected static function _getConnection(){
+
+ if(!self::$_connection){
+ self::$_connection = jKVDb::getConnection('jpref');
+ }
+ return self::$_connection;
+ }
+
+
+
+ /**
+ * Get a system preference
+ *
+ * @param string $key The corrresponding to the preference
+ * @return mixed La valeur de la préférence
+ */
+ public static function get($key) {
+
+ if(isset(self::$_prefs[$key]))
+ return self::$_prefs[$key];
+
+ $cnx = self::_getConnection();
+ $result = $cnx->get(self::$_prefix.$key);
+
+ if(!$result){
+ self::$_prefs[$key] = null;
+ return null;
+ }
+
+ $type = $result[0];
+ $value = substr($result, 2);
+
+ if($type == 'i')//integer
+ $value = (int) $value;
+ elseif($type == 'b') //boolean
+ $value = (boolean) $value;
+ elseif($type == 'd') // decimal
+ $value = (float) $value;
+
+ self::$_prefs[$key] = $value;
+
+ return $value;
+ }
+
+
+ /**
+ * Set a system preference
+ *
+ * @param string $key The corrresponding to the preference
+ * @param mixed $value La valeur de la préférence
+ */
+ public static function set($key, $value) {
+
+ self::$_prefs[$key] = $value;
+
+ $cnx = self::_getConnection();
+
+ if(is_int($value))
+ $prefix = 'i';
+ elseif(is_bool($value)){
+ $prefix = 'b';
+ if(!$value)
+ $value = '0';
+ }
+ elseif(is_float($value))
+ $prefix = 'd';
+ else
+ $prefix = 's';
+
+ $prefix .= '|';
+
+ $cnx->set(self::$_prefix.$key, $prefix.$value);
+ }
+
+
+ /**
+ * Clear the local cache
+ */
+ public static function clearCache(){
+ self::$_prefs = null;
+ }
+
+}
View
113 lib/jelix/pref/jPrefItem.class.php
@@ -0,0 +1,113 @@
+<?php
+/**
+* @package jelix
+* @subpackage pref
+* @author Florian Lonqueu-Brochard
+* @copyright 2012 Florian Lonqueu-Brochard
+* @link http://jelix.org
+* @licence http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
+*/
+
+class jPrefItem{
+
+ public $id;
+
+ public $type = 'string';
+
+ public $value;
+
+ public $locale;
+
+ public $group;
+
+ public $read_acl_subject;
+
+ public $write_acl_subject;
+
+ public $default_value;
+
+ protected $_writable;
+
+ protected $_readable;
+
+ public static $allowed_types = array('integer', 'decimal', 'string', 'boolean');
+
+ /**
+ * Current user can read this pref
+ */
+ public function isReadable(){
+ return $this->_readable;
+ }
+
+ /**
+ * Current user can write this pref
+ */
+ public function isWritable(){
+ return $this->_writable;
+ }
+
+ /**
+ * Initialise the pref with a node from an ini file
+ */
+ public function setFromIniNode($node_key, $node){
+ $this->id = substr($node_key, 5);
+
+ if(!empty($node['type'])){
+ if(in_array($node['type'], self::$allowed_types))
+ $this->type = $node['type'];
+ else
+ throw new jException('jPref~admin.type.not.allowed', array($node['type'], implode(',',self::$allowed_types)));
+ }
+
+ if(!empty($node['locale']))
+ $this->locale = $node['locale'];
+
+ if(!empty($node['group']))
+ $this->group = $node['group'];
+
+ $this->_readable = empty($node['read_acl_subject']) || jAcl2::check($node['read_acl_subject']);
+
+ $this->_writable = empty($node['write_acl_subject']) || jAcl2::check($node['write_acl_subject']);
+
+ if(!empty($node['default_value']))
+ $this->default_value = $node['default_value'];
+
+ if($this->type == 'boolean'){
+ if($this->default_value == 'true' || $this->default_value == '1')
+ $this->default_value = true;
+ else if($this->default_value == 'false' || (isset($node['default_value']) && $node['default_value'] == '') )
+ $this->default_value = false;
+ }
+ }
+
+ /**
+ * Load the value of the pref via jPref
+ */
+ public function loadValue(){
+ $this->value = jPref::get($this->id);
+ }
+
+ /**
+ * Compare 2 group
+ * @param jPrefAdminGroup $a the first group
+ * @param jPrefAdminGroup $b the second group
+ */
+ public static function compareGroup($a, $b){
+ if(empty($a->order) || empty($b->order)){
+ if(empty($a->order) && empty($b->order))
+ return 0;
+ elseif(empty($a->order))
+ return 1;
+ else
+ return -1;
+ }
+ else if($a->order > $b->order)
+ return 1;
+ else if($a->order < $b->order)
+ return -1;
+ else
+ return 0;
+ }
+
+
+}
View
36 lib/jelix/pref/jPrefItemGroup.class.php
@@ -0,0 +1,36 @@
+<?php
+/**
+* @package jelix
+* @subpackage pref
+* @author Florian Lonqueu-Brochard
+* @copyright 2012 Florian Lonqueu-Brochard
+* @link http://jelix.org
+* @licence http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
+*/
+
+class jPrefItemGroup{
+
+ public $id;
+
+ public $locale;
+
+ public $order;
+
+ /**
+ * @var array of jPrefItem
+ */
+ public $prefs = array();
+
+ /**
+ * Initialise the group with a node from an ini file
+ */
+ public function setFromIniNode($node_key, $node){
+ $this->id = substr($node_key, 6);
+
+ if(!empty($node['locale']))
+ $this->locale = $node['locale'];
+
+ if(!empty($node['order']))
+ $this->order = $node['order'];
+ }
+}
View
148 lib/jelix/pref/jPrefManager.class.php
@@ -0,0 +1,148 @@
+<?php
+/**
+* @package jelix
+* @subpackage pref
+* @author Florian Lonqueu-Brochard
+* @copyright 2012 Florian Lonqueu-Brochard
+* @link http://jelix.org
+* @licence http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
+*/
+
+require_once(JELIX_LIB_PATH.'pref/jPrefItem.class.php');
+require_once(JELIX_LIB_PATH.'pref/jPrefItemGroup.class.php');
+
+class jPrefManager{
+
+ protected static $_ini;
+
+ protected static $_pref_config_file = 'preferences.ini.php';
+
+
+ /**
+ * Add a preference into the preference config
+ *
+ * @param jPrefItem $preference the preference to add
+ */
+ public function addPreference($pref){
+ self::_loadIniModifier();
+
+ $section = 'pref:'.$pref->id;
+
+ self::$_ini->setValue('type', $pref->type, $section);
+ self::$_ini->setValue('locale', $pref->locale, $section);
+ self::$_ini->setValue('group', $pref->group, $section);
+ self::$_ini->setValue('read_acl_subject', $pref->read_acl_subject, $section);
+ self::$_ini->setValue('write_acl_subject', $pref->write_acl_subject, $section);
+ self::$_ini->setValue('default_value', $pref->default_value, $section);
+
+ self::$_ini->save();
+
+ if($pref->value !== null){
+ jPref::set($pref->id, $pref->value);
+ }
+ else if($pref->default_value !== null){
+ jPref::set($pref->id, $pref->default_value);
+ }
+ }
+
+ /**
+ * Add a group of preference into the preference config
+ *
+ * @param jPrefItemGroup $grp the preference group to add
+ */
+ public function addGroup($grp){
+ self::_loadIniModifier();
+
+ $section = 'group:'.$grp->id;
+
+ self::$_ini->setValue('locale', $grp->locale, $section);
+ self::$_ini->setValue('order', $grp->order, $section);
+
+ self::$_ini->save();
+ }
+
+
+ /**
+ *
+ */
+ public static function getAllPreferences($get_prefs_values = true){
+ $preferences = self::_getPrefFile();
+
+ $prefs = array();
+ $nogroup = new jPrefItemGroup();
+ $nogroup->id= '__nogroup';
+ $nogroup-> locale = 'jpref_admin~admin.group.others';
+
+ foreach($preferences as $item_key => $item){
+ if(substr($item_key, 0, 5) == 'group'){
+ $g = new jPrefItemGroup();
+ $g->setFromIniNode($item_key, $item);
+
+ $prefs[$g->id] = $g;
+ }
+ else if(substr($item_key, 0, 4) == 'pref'){
+ $p = new jPrefItem();
+ $p->setFromIniNode($item_key, $item);
+
+ //current user doesnt have rights to read this pref
+ if(!$p->isReadable())
+ continue;
+
+ if($get_prefs_values)
+ $p->loadValue();
+
+ if(!empty($p->group))
+ $prefs[$p->group]->prefs[] = $p;
+ else
+ $nogroup->prefs[] = $p;
+ }
+ }
+ usort($prefs, 'jPrefAdmin::compareGroup');
+ if(count($nogroup->prefs) > 0)
+ $prefs['__nogroup'] = $nogroup;
+ return $prefs;
+ }
+
+ /**
+ *
+ */
+ public static function getPref($pref_id, $get_pref_value = true){
+ $preferences = self::_getPrefFile();
+
+ $item_key = 'pref:'.$pref_id;
+
+ if(isset($preferences[$item_key])){
+ $ini_node = $preferences[$item_key];
+ $p = new jPrefItem();
+ $p->setFromIniNode($item_key, $ini_node);
+
+ //current user doesnt have rights to read this pref
+ if(!$p->isReadable())
+ break;
+
+ if($get_pref_value)
+ $p->loadValue();
+
+ return $p;
+ }
+ else
+ return null;
+ }
+
+
+
+ protected static function _getPrefFile(){
+ return jIniFile::read(jApp::configPath(self::$_pref_config_file));
+ }
+
+
+ protected function _loadIniModifier(){
+ if(!self::$_ini){
+ self::$_ini = new jIniFileModifier(jApp::configPath(self::$_pref_config_file));
+ }
+ return self::$_ini;
+ }
+
+
+
+}
View
25 testapp/modules/jelix_tests/tests/jpref.main_api.pu.php
@@ -0,0 +1,25 @@
+<?php
+
+
+class jpref_main_apiTest extends PHPUnit_Framework_TestCase{
+
+ /**
+ * @covers jPref::set
+ * @covers jPref::get
+ */
+ public function testJPref(){
+ $testArray = array();
+ $testArray[] = array('value' => false, 'key' => 'my.bool.value');
+ $testArray[] = array('value' => 123, 'key' => 'my.int.value');
+ $testArray[] = array('value' => 12.34, 'key' => 'my.float.value');
+ $testArray[] = array('value' => 'test', 'key' => 'my.string.value');
+
+ foreach($testArray as $el){
+ jPref::set($el['key'], $el['value']);
+ jPref::clearCache();
+ $result = jPref::get($el['key']);
+ $this->assertEquals($el['value'], $result);
+ }
+ }
+
+}
View
4 testapp/var/config/profiles.ini.php.dist
@@ -129,6 +129,10 @@ driver = db
table=testkvdb
dbprofile=default
+[jkvdb:jpref]
+driver = db
+table=testkvdb
+dbprofile=default
;----------------- jSoapClient
[jsoapclient]

0 comments on commit ce7b54d

Please sign in to comment.
Something went wrong with that request. Please try again.