Configuration.class.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 MIT
  8. */
  9. class Configuration extends Entity {
  10. public $id,$key,$value;
  11. public $confTab;
  12. public $entityLabel = 'Configuration';
  13. protected $fields = array(
  14. 'id' => array('label'=>'Identifiant','type'=>'key'),
  15. 'key' => array('label'=>'Clée','type'=>'longstring'),
  16. 'value' => array('label'=>'Valeur','type'=>'longstring')
  17. );
  18. /**
  19. * Get all configurations from database OR session if it was yet loaded
  20. * 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.
  21. * #### Example
  22. * ```php
  23. * $confs = Configuration::getAll();
  24. * var_dump($confs);
  25. * ```.
  26. * @return array Array of configurations
  27. */
  28. public function getAll() {
  29. if (!isset($_SESSION['configuration'])) {
  30. $configurationManager = new self();
  31. $configs = $configurationManager->loadAll();
  32. $this->confTab = array();
  33. foreach ($configs as $config)
  34. $this->confTab[$config->key] = decrypt($config->value);
  35. $_SESSION['configuration'] = serialize($this->confTab);
  36. } else {
  37. $this->confTab = unserialize($_SESSION['configuration']);
  38. }
  39. }
  40. //Définit / récupere un set de configurations générique
  41. public static function setting($name,$settings=null){
  42. if(!isset($settings)) return isset($GLOBALS['setting'][$name]) ? $GLOBALS['setting'][$name] : array();
  43. $GLOBALS['setting'][$name] = $settings;
  44. }
  45. //Met en page (tableau html) un set de configuration générique
  46. public static function html($name){
  47. global $conf;
  48. $options = isset($GLOBALS['setting'][$name]) ? $GLOBALS['setting'][$name] : array(); ?>
  49. <table id="<?php echo $name; ?>-setting-form" class="table table-striped <?php echo $name; ?>-setting-form">
  50. <tbody>
  51. <?php foreach($options as $key=>$infos):
  52. if(!is_array($infos)): ?>
  53. <tr><th colspan="2" class="bg-secondary text-light py-2 align-middle"><h4 class="m-0"><i class="fas fa-angle-right"></i> <?php echo $infos;?></h4></th></tr>
  54. <?php continue; endif;
  55. $confValue = $conf->get($key);
  56. $infos['attributes'] = empty($infos['attributes']) ? array() : $infos['attributes'];
  57. if(isset($confValue)) $infos['value'] = htmlentities($confValue);
  58. //retro compatibilité parameters / attributes
  59. if(isset($infos['parameters'])){
  60. foreach($infos['parameters'] as $attribute=>$parameter){
  61. if(isset($infos['attributes'][$attribute])) continue;
  62. $infos['attributes'][$attribute] = $attribute.'="'.$parameter.'"';
  63. }
  64. }
  65. $infos['id'] = $key;
  66. if(!empty( $infos['placeholder'])) $infos['placeholder'] = '"'. $infos['placeholder'].'"';
  67. if(!empty( $infos['placeholder'])) $infos['attributes']['placeholder'] = $infos['placeholder'] ;
  68. $field = FieldType::toHtml($infos,null,array('allowCustomLabel'=>false));
  69. ?>
  70. <tr class="<?php echo $key; ?>">
  71. <th class="align-middle position-relative"><?php echo $field['label']; ?>
  72. <?php if (!empty($field['legend'])): ?>
  73. <small class="text-muted"> - <?php echo $field['legend']; ?></small>
  74. <?php endif; ?>
  75. </th>
  76. <td class="align-middle position-relative">
  77. <?php
  78. if($infos['type']=='dropzone' && isset($infos['attributes']['documents'])){
  79. $infos['attributes']['value'] = array($infos['attributes']['documents']);
  80. unset($infos['attributes']['documents']);
  81. }
  82. echo $field['input'];
  83. ?>
  84. </td>
  85. </tr>
  86. <?php endforeach; ?>
  87. </tbody>
  88. </table>
  89. <?php
  90. }
  91. /**
  92. * Get configuration value from it key
  93. * #### Example
  94. * ```php
  95. * global $conf; // global var, contain configurations
  96. * echo $conf->get('myConfigKey'); // print myConfigKey value
  97. * ```.
  98. * @param string configuration key
  99. * @param string configuration namespace (default is 'conf')
  100. *
  101. * @return string valeur de la configuration
  102. */
  103. public function get($key) {
  104. return isset($this->confTab[$key]) ? htmlspecialchars_decode($this->confTab[$key]) : '';
  105. }
  106. /**
  107. * Update or insert configuration value in database with specified key
  108. * #### Example
  109. * ```php
  110. * global $conf; // global var, contain configurations
  111. * echo $conf->put('myNewConfigKey','hello!'); //create configuration myNewConfigKey with value 'hello!'
  112. * echo $conf->put('myNewConfigKey','hello 2!'); //update configuration myNewConfigKey with value 'hello2!'
  113. * ```.
  114. *
  115. * @param string configuration key
  116. * @param string configuration value
  117. * @param string configuration namespace (default is 'conf')
  118. */
  119. public function put($key, $value) {
  120. $secured_value = encrypt($value);
  121. $configurationManager = new self();
  122. if (isset($this->confTab[$key])) {
  123. $configurationManager->change(array('value' => $secured_value), array('key' => $key));
  124. } else {
  125. $configurationManager->add($key, $secured_value);
  126. }
  127. $this->confTab[$key] = $value;
  128. unset($_SESSION['configuration']);
  129. }
  130. /**
  131. * Remove configuration value in database with specified key
  132. * #### Example
  133. * ```php
  134. * global $conf; // global var, contain configurations
  135. * echo $conf->remove('myNewConfigKey'); //delete myNewConfigKey from 'conf' default namespace
  136. * echo $conf->remove('myNewConfigKey','myCustomPluginConfig'); //delete myNewConfigKey from 'myCustomPluginConfig' namespace
  137. * ```.
  138. *
  139. * @param string configuration key
  140. * @param string configuration namespace (default is 'conf')
  141. */
  142. public function add($key, $value) {
  143. $config = new self();
  144. $config->key = $key;
  145. $config->value = $value;
  146. $config->save();
  147. $this->confTab[$key] = $value;
  148. unset($_SESSION['configuration']);
  149. }
  150. }