configuration.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. require_once(dirname(dirname(__FILE__))."/header.php");
  3. function get_conf($key){
  4. $res = query("SELECT c.value FROM configuration c WHERE c.key = '%s'",array($key));
  5. if(!$res || $res->num_rows != 1 ){
  6. return false;
  7. }
  8. $res = $res->fetch_assoc();
  9. return $res['value'];
  10. }
  11. function set_conf($key,$val){
  12. return query("UPDATE configuration c SET c.value = '%s' WHERE c.key = '%s'",array($val,$key));
  13. }
  14. function get_conf_type($key){
  15. $res = query("SELECT c.type FROM configuration c WHERE c.key = '%s'",array($key));
  16. if($res && $row = $res->fetch_assoc()){
  17. return $row['type'];
  18. }
  19. return 'string';
  20. }
  21. function get_conf_values($key,$labels=false){
  22. $ret = array();
  23. switch(get_conf_type($key)){
  24. case 'list':
  25. $res = query("SELECT cl.value,cl.label FROM configuration_lists cl WHERE cl.key='%s'",array($key));
  26. if($res){
  27. while($row = $res->fetch_assoc()){
  28. if(!$labels){
  29. $row = $row['value'];
  30. }
  31. array_push($ret,$row);
  32. }
  33. }else{
  34. array_push($ret,get_conf($key));
  35. }
  36. break;
  37. case 'lookup':
  38. $res = query("SELECT cl.table,cl.column,cl.label_column,cl.enabled_column FROM configuration_lookups cl WHERE cl.key='%s'",array($key));
  39. if($res && $res->num_rows == 1){
  40. $lookup = $res->fetch_assoc();
  41. if(isset($lookup['enabled_column']) && !is_null($lookup['enabled_column']) && $lookup['enabled_column'] != ''){
  42. $eq = 1;
  43. if(substr($lookup['enabled_column'],0,1) == '!'){
  44. $eq = 0;
  45. $lookup['enabled_column'] = substr($lookup['enabled_column'],1);
  46. }
  47. $res = query("SELECT t.%s AS value, t.%s AS label FROM %s t WHERE t.%s = {$eq}",array($lookup['column'],$lookup['label_column'],$lookup['table'],$lookup['enabled_column']));
  48. }else{
  49. $res = query("SELECT t.%s AS value, t.%s AS label FROM %s t",array($lookup['column'],$lookup['label_column'],$lookup['table']));
  50. }
  51. if($res){
  52. while($row = $res->fetch_assoc()){
  53. if(!$labels){
  54. $row = $row['value'];
  55. }
  56. array_push($ret,$row);
  57. }
  58. }else{
  59. array_push($ret,get_conf($key));
  60. }
  61. }else{
  62. array_push($ret,get_conf($key));
  63. }
  64. break;
  65. }
  66. return $ret;
  67. }
  68. function get_conf_list(){
  69. $conf = array();
  70. $res = query("SELECT c.key,c.description,c.value,c.type FROM configuration c");
  71. if($res){
  72. while($row = $res->fetch_assoc()){
  73. $item = array(
  74. 'key'=>$row['key'],
  75. 'type'=>get_conf_type($row['key']),
  76. 'label'=>isset($row['description'])?$row['description']:''
  77. );
  78. $item['value'] = $row['value'];
  79. if(!isset($item['value'])){
  80. $item['value'] = '';
  81. }
  82. if(isset($item['type'])){
  83. switch($item['type']){
  84. case 'list':case 'lookup':
  85. $item['type'] = 'select';
  86. $values = get_conf_values($item['key'],true);
  87. $item['values'] = array();
  88. foreach($values as $value){
  89. if(isset($item['value']) && $value['value'] == $item['value']){
  90. $value['attributes'] = array(
  91. 'selected'=>'selected'
  92. );
  93. }
  94. array_push($item['values'],$value);
  95. }
  96. break;
  97. }
  98. array_push($conf,$item);
  99. }
  100. }
  101. }
  102. return $conf;
  103. }
  104. function render_configuration_table(){
  105. $items = array(
  106. array(
  107. 'name'=>'action',
  108. 'type'=>'hidden',
  109. 'value'=>'config'
  110. )
  111. );
  112. $config = get_conf_list();
  113. foreach($config as $k => $conf){
  114. switch($conf['type']){
  115. case 'select':
  116. $item = array(
  117. 'name'=>$conf['key'],
  118. 'values'=>$conf['values'],
  119. 'label'=>$conf['label'],
  120. 'type'=>'select'
  121. );
  122. break;
  123. default:
  124. $item = array(
  125. 'name'=>$conf['key'],
  126. 'value'=>$conf['value'],
  127. 'label'=>$conf['label'],
  128. 'type'=>$conf['type']
  129. );
  130. }
  131. array_push($items,$item);
  132. }
  133. return get_form_html('configuration',$items,'Save');
  134. }
  135. ?>