Configuration.class.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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
  56. $input = '';
  57. if(!is_array($infos)): ?>
  58. <tr><th colspan="2"><h4 class="m-0"><i class="fas fa-angle-right"></i> <?php echo $infos;?></h4></th></tr>
  59. <?php continue; endif; ?>
  60. <tr>
  61. <th class="align-middle"><?php echo $infos['label']; ?>
  62. <?php if (isset($infos['legend'])): ?>
  63. <small class="text-muted"> - <?php echo $infos['legend']; ?></small>
  64. <?php endif; ?>
  65. </th>
  66. <td class="align-middle position-relative">
  67. <?php $attributes = array();
  68. if(isset($infos['placeholder'])) $attributes[] = 'placeholder="'.$infos['placeholder'].'"';
  69. $attributes['class'] = 'class="form-control"';
  70. $attributes[] = 'id="'.$key.'"';
  71. $attributes['value'] = 'value="'.htmlentities($conf->get($key)).'"';
  72. if(isset($infos['parameters'])){
  73. foreach($infos['parameters'] as $attribute=>$parameter){
  74. if(isset($attributes[$attribute])) continue;
  75. $attributes[$attribute] = $attribute.'="'.$parameter.'"';
  76. }
  77. }
  78. switch($infos['type']){
  79. case 'password':
  80. $attributes['type'] = 'data-type="password"';
  81. $input = '<input type="text" '.implode(' ',$attributes).' >';
  82. break;
  83. case 'dictionnary':
  84. $attributes['type'] = 'data-type="dictionnary"';
  85. $attributes['parameters'] = 'data-slug="'.$infos['slug'].'"';
  86. $input = '<select type="text" '.implode(' ',$attributes).' >';
  87. break;
  88. case 'checkbox':
  89. unset($attributes['class']);
  90. unset($attributes['value']);
  91. $attributes['type'] = 'data-type="checkbox"';
  92. if($conf->get($key)) $attributes['value'] = 'checked="checked"';
  93. $input = '<label class="mb-0 pointer no-select"><input type="checkbox" '.implode(' ',$attributes).' > Activer</label>';
  94. break;
  95. case 'select':
  96. unset($attributes['value']);
  97. $input = '<select '.implode(' ',$attributes).'>';
  98. foreach($infos['values'] as $id=>$label)
  99. $input .= '<option value="'.$id.'" '.($id==$conf->get($key)?'selected="selected"':'').' type="checkbox">'.$label.'</option>';
  100. $input .= '</select>';
  101. break;
  102. case 'user':
  103. $attributes['data-type'] = 'data-type="user"';
  104. $input = '<input type="text" '.implode(' ',$attributes).'>';
  105. break;
  106. case 'rank':
  107. $attributes['data-type'] = 'data-type="user" data-types="rank"';
  108. $input = '<input type="text" '.implode(' ',$attributes).' >';
  109. break;
  110. case 'number':
  111. $input = '<input type="number" onkeydown="return input_number_control(event,true,true);" '.implode(' ',$attributes).' >';
  112. break;
  113. case 'hour':
  114. $input = '<input type="text" data-type="hour" '.implode(' ',$attributes).' >';
  115. break;
  116. case 'date':
  117. $input = '<input type="text" data-type="date" '.implode(' ',$attributes).' >';
  118. break;
  119. case 'textarea':
  120. $input = '<textarea '.implode(' ',$attributes).' >'.htmlentities($conf->get($key)).'</textarea>';
  121. break;
  122. case 'file':
  123. $input = '<input type="file" '.implode(' ',$attributes).' >';
  124. break;
  125. default:
  126. Plugin::callHook('configuration_fields',array(&$input, $infos, $attributes));
  127. if(empty($input)) $input = '<input type="text" '.implode(' ',$attributes).' >';
  128. break;
  129. }
  130. echo $input; ?>
  131. </td>
  132. </tr>
  133. <?php endforeach; ?>
  134. </tbody>
  135. </table>
  136. <?php
  137. }
  138. /**
  139. * Get configuration value from it key
  140. * #### Example
  141. * ```php
  142. * global $conf; // global var, contain configurations
  143. * echo $conf->get('myConfigKey'); // print myConfigKey value
  144. * ```.
  145. * @param string configuration key
  146. * @param string configuration namespace (default is 'conf')
  147. *
  148. * @return string valeur de la configuration
  149. */
  150. public function get($key)
  151. {
  152. return isset($this->confTab[$key]) ? $this->confTab[$key] : '';
  153. }
  154. /**
  155. * Update or insert configuration value in database with specified key
  156. * #### Example
  157. * ```php
  158. * global $conf; // global var, contain configurations
  159. * echo $conf->put('myNewConfigKey','hello!'); //create configuration myNewConfigKey with value 'hello!'
  160. * echo $conf->put('myNewConfigKey','hello 2!'); //update configuration myNewConfigKey with value 'hello2!'
  161. * ```.
  162. *
  163. * @param string configuration key
  164. * @param string configuration value
  165. * @param string configuration namespace (default is 'conf')
  166. */
  167. public function put($key, $value)
  168. {
  169. $secured_value = encrypt($value);
  170. $configurationManager = new self();
  171. if (isset($this->confTab[$key])) {
  172. $configurationManager->change(array('value' => $secured_value), array('key' => $key));
  173. } else {
  174. $configurationManager->add($key, $secured_value);
  175. }
  176. $this->confTab[$key] = $value;
  177. unset($_SESSION['configuration']);
  178. }
  179. /**
  180. * Remove configuration value in database with specified key
  181. * #### Example
  182. * ```php
  183. * global $conf; // global var, contain configurations
  184. * echo $conf->remove('myNewConfigKey'); //delete myNewConfigKey from 'conf' default namespace
  185. * echo $conf->remove('myNewConfigKey','myCustomPluginConfig'); //delete myNewConfigKey from 'myCustomPluginConfig' namespace
  186. * ```.
  187. *
  188. * @param string configuration key
  189. * @param string configuration namespace (default is 'conf')
  190. */
  191. public function add($key, $value)
  192. {
  193. $config = new self();
  194. $config->key = $key;
  195. $config->value = $value;
  196. $config->save();
  197. $this->confTab[$key] = $value;
  198. unset($_SESSION['configuration']);
  199. }
  200. }