Configuration.class.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * Manage application and plugins configurations with key/value pair.
  4. * **nb:** It's possible to specify namespace in order to distinct global configuration to plugin custom configuration
  5. * @author valentin carruesco
  6. * @category Core
  7. * @license copyright
  8. */
  9. class Configuration extends Entity
  10. {
  11. public $id,$key,$value;
  12. protected $confTab;
  13. protected $fields =
  14. array(
  15. 'id' => 'key',
  16. 'key' => 'longstring',
  17. 'value' => 'longstring',
  18. );
  19. /**
  20. * Get all configurations from database OR session if it was yet loaded
  21. * 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.
  22. * #### Example
  23. * ```php
  24. * $confs = Configuration::getAll();
  25. * var_dump($confs);
  26. * ```.
  27. * @return array Array of configurations
  28. */
  29. public function getAll()
  30. {
  31. if (!isset($_SESSION['configuration'])) {
  32. $configurationManager = new self();
  33. $configs = $configurationManager->loadAll();
  34. $confTab = array();
  35. foreach ($configs as $config) {
  36. $this->confTab[$config->key] = decrypt($config->value);
  37. }
  38. $_SESSION['configuration'] = serialize($this->confTab);
  39. } else {
  40. $this->confTab = unserialize($_SESSION['configuration']);
  41. }
  42. }
  43. //Définit / récupere un set de configurations générique
  44. public static function setting($name,$settings=null){
  45. if(!isset($settings)) return isset($GLOBALS['setting'][$name]) ? $GLOBALS['setting'][$name] : array();
  46. $GLOBALS['setting'][$name] = $settings;
  47. }
  48. //Met en page (tableau html) un set de configuration générique
  49. public static function html($name){
  50. global $conf;
  51. $options = $GLOBALS['setting'][$name]; ?>
  52. <table id="<?php echo $name; ?>-setting-form" class="table table-striped">
  53. <tbody>
  54. <?php foreach($options as $key=>$infos): ?>
  55. <?php if(!is_array($infos)): ?>
  56. <tr><th colspan="2"><h4><i class="fas fa-angle-right"></i> <?php echo $infos;?></h4></th></tr>
  57. <?php continue; endif; ?>
  58. <tr>
  59. <th class="align-middle"><?php echo $infos['label']; ?>
  60. <?php if (isset($infos['legend'])): ?>
  61. <small class="text-muted"> - <?php echo $infos['legend']; ?></small>
  62. <?php endif; ?>
  63. </th>
  64. <td class="align-middle position-relative">
  65. <?php $attributes = array();
  66. if(isset($infos['placeholder'])) $attributes[] = 'placeholder="'.$infos['placeholder'].'"';
  67. $attributes['class'] = 'class="form-control"';
  68. $attributes[] = 'id="'.$key.'"';
  69. $attributes['value'] = 'value="'.$conf->get($key).'"';
  70. if(isset($infos['parameters'])){
  71. foreach($infos['parameters'] as $attribute=>$parameter){
  72. if(isset($attributes[$attribute])) continue;
  73. $attributes[$attribute] = $attribute.'="'.$parameter.'"';
  74. }
  75. }
  76. switch($infos['type']){
  77. case 'password':
  78. $attributes['type'] = 'data-type="password"';
  79. $input = '<input type="text" '.implode(' ',$attributes).' >';
  80. break;
  81. case 'dictionnary':
  82. $attributes['type'] = 'data-type="dictionnary"';
  83. $attributes['parameters'] = 'data-slug="'.$infos['slug'].'"';
  84. $input = '<select type="text" '.implode(' ',$attributes).' >';
  85. break;
  86. case 'checkbox':
  87. unset($attributes['class']);
  88. unset($attributes['value']);
  89. $attributes['type'] = 'data-type="checkbox"';
  90. if($conf->get($key)) $attributes['value'] = 'checked="checked"';
  91. $input = '<label class="mb-0 pointer no-select"><input type="checkbox" '.implode(' ',$attributes).' > Activer</label>';
  92. break;
  93. case 'select':
  94. unset($attributes['value']);
  95. $input = '<select '.implode(' ',$attributes).'>';
  96. foreach($infos['values'] as $id=>$label)
  97. $input .= '<option value="'.$id.'" '.($id==$conf->get($key)?'selected="selected"':'').' type="checkbox">'.$label.'</option>';
  98. $input .= '</select>';
  99. break;
  100. case 'user':
  101. $attributes['data-type'] = 'data-type="user"';
  102. $input = '<input type="text" '.implode(' ',$attributes).'>';
  103. break;
  104. case 'rank':
  105. $attributes['data-type'] = 'data-type="user" data-types="rank"';
  106. $input = '<input type="text" '.implode(' ',$attributes).' >';
  107. break;
  108. case 'number':
  109. $input = '<input type="number" onkeydown="return input_number_control(event,true,true);" '.implode(' ',$attributes).' >';
  110. break;
  111. case 'hour':
  112. $input = '<input type="text" data-type="hour" '.implode(' ',$attributes).' >';
  113. break;
  114. case 'date':
  115. $input = '<input type="text" data-type="date" '.implode(' ',$attributes).' >';
  116. break;
  117. case 'file':
  118. $input = '<input type="file" '.implode(' ',$attributes).' >';
  119. break;
  120. default:
  121. $input = '<input type="text" '.implode(' ',$attributes).' >';
  122. break;
  123. }
  124. echo $input; ?>
  125. </td>
  126. </tr>
  127. <?php endforeach; ?>
  128. </tbody>
  129. </table>
  130. <?php
  131. }
  132. /**
  133. * Get configuration value from it key
  134. * #### Example
  135. * ```php
  136. * global $conf; // global var, contain configurations
  137. * echo $conf->get('myConfigKey'); // print myConfigKey value
  138. * ```.
  139. * @param string configuration key
  140. * @param string configuration namespace (default is 'conf')
  141. *
  142. * @return string valeur de la configuration
  143. */
  144. public function get($key)
  145. {
  146. return isset($this->confTab[$key]) ? $this->confTab[$key] : '';
  147. }
  148. /**
  149. * Update or insert configuration value in database with specified key
  150. * #### Example
  151. * ```php
  152. * global $conf; // global var, contain configurations
  153. * echo $conf->put('myNewConfigKey','hello!'); //create configuration myNewConfigKey with value 'hello!'
  154. * echo $conf->put('myNewConfigKey','hello 2!'); //update configuration myNewConfigKey with value 'hello2!'
  155. * ```.
  156. *
  157. * @param string configuration key
  158. * @param string configuration value
  159. * @param string configuration namespace (default is 'conf')
  160. */
  161. public function put($key, $value)
  162. {
  163. $secured_value = encrypt($value);
  164. $configurationManager = new self();
  165. if (isset($this->confTab[$key])) {
  166. $configurationManager->change(array('value' => $secured_value), array('key' => $key));
  167. } else {
  168. $configurationManager->add($key, $secured_value);
  169. }
  170. $this->confTab[$key] = $value;
  171. unset($_SESSION['configuration']);
  172. }
  173. /**
  174. * Remove configuration value in database with specified key
  175. * #### Example
  176. * ```php
  177. * global $conf; // global var, contain configurations
  178. * echo $conf->remove('myNewConfigKey'); //delete myNewConfigKey from 'conf' default namespace
  179. * echo $conf->remove('myNewConfigKey','myCustomPluginConfig'); //delete myNewConfigKey from 'myCustomPluginConfig' namespace
  180. * ```.
  181. *
  182. * @param string configuration key
  183. * @param string configuration namespace (default is 'conf')
  184. */
  185. public function add($key, $value)
  186. {
  187. $config = new self();
  188. $config->key = $key;
  189. $config->value = $value;
  190. $config->save();
  191. $this->confTab[$key] = $value;
  192. unset($_SESSION['configuration']);
  193. }
  194. }