Configuration.class.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * Manage application and plugins configurations with key/value pair
  4. *
  5. * **nb:** It's possible to specify namespace in order to distinct global configuration to plugin custom configuration
  6. * @author Idleman
  7. * @category Database
  8. * @license cc by nc sa
  9. */
  10. class Configuration extends SQLiteEntity{
  11. protected $id,$key,$value,$confTab,$namespace;
  12. protected $TABLE_NAME = 'configuration';
  13. protected $CLASS_NAME = 'Configuration';
  14. protected $object_fields =
  15. array(
  16. 'id'=>'key',
  17. 'key'=>'longstring',
  18. 'value'=>'longstring'
  19. );
  20. function __construct(){
  21. parent::__construct();
  22. }
  23. /**
  24. * Get all configurations from database OR session if it was yet loaded
  25. * This function is called at start of program and global var '$conf' is filled with response, so use global $conf instead of call this function.
  26. * #### Example
  27. * ```php
  28. * $confs = Configuration::getAll();
  29. * var_dump($confs);
  30. * ```
  31. * @return array Array of configurations
  32. */
  33. public function getAll(){
  34. if(!isset($_SESSION['configuration'])){
  35. $configurationManager = new Configuration();
  36. $configs = $configurationManager->populate();
  37. $confTab = array();
  38. foreach($configs as $config){
  39. $ns = 'conf';
  40. $key = $config->getKey();
  41. $infos = explode(':',$key);
  42. if(count($infos) ==2){
  43. list($ns,$key) = $infos;
  44. }
  45. $this->confTab[$ns][$key] = $config->getValue();
  46. }
  47. $_SESSION['configuration'] = serialize($this->confTab);
  48. }else{
  49. $this->confTab = unserialize($_SESSION['configuration']);
  50. }
  51. }
  52. /**
  53. * Get configuration value from it key
  54. * #### Example
  55. * ```php
  56. * global $conf; // global var, contain configurations
  57. * echo $conf->get('myConfigKey'); // print myConfigKey value
  58. * ```
  59. * @param string configuration key
  60. * @param string configuration namespace (default is 'conf')
  61. * @return string valeur de la configuration
  62. */
  63. public function get($key,$ns = 'conf'){
  64. return (isset($this->confTab[$ns][$key])?$this->confTab[$ns][$key]:'');
  65. }
  66. /**
  67. * Update or insert configuration value in database with specified key
  68. * #### Example
  69. * ```php
  70. * global $conf; // global var, contain configurations
  71. * echo $conf->put('myNewConfigKey','hello!'); //create configuration myNewConfigKey with value 'hello!'
  72. * echo $conf->put('myNewConfigKey','hello 2!'); //update configuration myNewConfigKey with value 'hello2!'
  73. * ```
  74. * @param string configuration key
  75. * @param string configuration value
  76. * @param string configuration namespace (default is 'conf')
  77. */
  78. public function put($key,$value,$ns = 'conf'){
  79. $configurationManager = new Configuration();
  80. if (isset($this->confTab[$ns][$key])){
  81. $configurationManager->change(array('value'=>$value),array('key'=>$ns.':'.$key));
  82. } else {
  83. $configurationManager->add($key,$value,$ns);
  84. }
  85. $this->confTab[$ns][$key] = $value;
  86. unset($_SESSION['configuration']);
  87. }
  88. /**
  89. * Remove configuration value in database with specified key
  90. * #### Example
  91. * ```php
  92. * global $conf; // global var, contain configurations
  93. * echo $conf->remove('myNewConfigKey'); //delete myNewConfigKey from 'conf' default namespace
  94. * echo $conf->remove('myNewConfigKey','myCustomPluginConfig'); //delete myNewConfigKey from 'myCustomPluginConfig' namespace
  95. * ```
  96. * @param string configuration key
  97. * @param string configuration namespace (default is 'conf')
  98. */
  99. public function remove($key,$ns = 'conf'){
  100. $configurationManager = new Configuration();
  101. if (isset($this->confTab[$ns][$key])){
  102. $configurationManager->delete(array('key'=>$ns.':'.$key));
  103. }
  104. unset($this->confTab[$ns][$key]);
  105. unset($_SESSION['configuration']);
  106. }
  107. private function add($key,$value,$ns = 'conf'){
  108. $config = new Configuration();
  109. $config->setKey($ns.':'.$key);
  110. $config->setValue($value);
  111. $config->save();
  112. $this->confTab[$ns][$key] = $value;
  113. unset($_SESSION['configuration']);
  114. }
  115. /**
  116. * Get current configuration id in database
  117. * @return int configuration id
  118. */
  119. function getId(){
  120. return $this->id;
  121. }
  122. /**
  123. * Get current configuration key
  124. * @return string configuration key
  125. */
  126. function getKey(){
  127. return $this->key;
  128. }
  129. /**
  130. * Set current configuration key
  131. * @param string configuration key
  132. */
  133. function setKey($key){
  134. $this->key = $key;
  135. }
  136. /**
  137. * Get current configuration value
  138. * @return string configuration value
  139. */
  140. function getValue(){
  141. return $this->value;
  142. }
  143. /**
  144. * Set current configuration value
  145. * @param string configuration value
  146. */
  147. function setValue($value){
  148. $this->value = $value;
  149. }
  150. /**
  151. * Set current configuration namespace
  152. * @param string configuration namespace
  153. */
  154. function setNameSpace($ns){
  155. $this->namespace = $ns;
  156. }
  157. /**
  158. * Get current configuration namespace
  159. * @return string configuration namespace
  160. */
  161. function getNameSpace(){
  162. return $this->namespace;
  163. }
  164. }
  165. ?>