Configuration.class.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /*
  3. @nom: Configuration
  4. @auteur: Idleman (idleman@idleman.fr)
  5. @description: Classe de gestion des préférences, fonctionne sur un simple système clé=>valeur avec un cache session pour eviter les requête inutiles
  6. */
  7. class Configuration extends MysqlEntity{
  8. protected $id,$key,$value,$confTab;
  9. protected $TABLE_NAME = 'configuration';
  10. protected $CLASS_NAME = 'Configuration';
  11. protected $object_fields =
  12. array(
  13. 'id'=>'key',
  14. 'key'=>'longstring',
  15. 'value'=>'longstring'
  16. );
  17. function __construct(){
  18. parent::__construct();
  19. }
  20. public function getAll(){
  21. if(!isset($_SESSION['configuration'])){
  22. $configurationManager = new Configuration();
  23. $configs = $configurationManager->populate();
  24. $confTab = array();
  25. foreach($configs as $config){
  26. $this->confTab[$config->getKey()] = $config->getValue();
  27. }
  28. $_SESSION['configuration'] = serialize($this->confTab);
  29. }else{
  30. $this->confTab = unserialize($_SESSION['configuration']);
  31. }
  32. }
  33. public function get($key){
  34. return (isset($this->confTab[$key])?$this->confTab[$key]:'');
  35. }
  36. public function put($key,$value){
  37. $configurationManager = new Configuration();
  38. if (isset($this->confTab[$key])){
  39. $configurationManager->change(array('value'=>$value),array('key'=>$key));
  40. } else {
  41. $configurationManager->add($key,$value);
  42. }
  43. $this->confTab[$key] = $value;
  44. unset($_SESSION['configuration']);
  45. }
  46. public function add($key,$value){
  47. $config = new Configuration();
  48. $config->setKey($key);
  49. $config->setValue($value);
  50. $config->save();
  51. $this->confTab[$key] = $value;
  52. unset($_SESSION['configuration']);
  53. }
  54. function getId(){
  55. return $this->id;
  56. }
  57. function getKey(){
  58. return $this->key;
  59. }
  60. function setKey($key){
  61. $this->key = $key;
  62. }
  63. function getValue(){
  64. return $this->value;
  65. }
  66. function setValue($value){
  67. $this->value = $value;
  68. }
  69. }
  70. ?>