Filter.class.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. class Filter extends Entity{
  3. public $id,$report,$label,$slug,$type,$default,$sort,$datasource;
  4. public $TABLE_NAME = 'statistic_filter';
  5. public $fields = array(
  6. 'id'=>'key',
  7. 'label'=>'string',
  8. 'slug'=>'string',
  9. 'report'=>'int',
  10. 'type' => 'string',
  11. 'default' => 'string',
  12. 'datasource' => 'longstring',
  13. 'sort' => 'int'
  14. );
  15. public static function getMetaTerms(){
  16. global $myUser;
  17. return array(
  18. 'date.year' => array('value' => date('Y') ,'description' => 'Année courante'),
  19. 'date.month' => array('value' => date('m'),'description' => 'Mois courant'),
  20. 'date.day' => array('value' => date('d'),'description' => 'Jour courant'),
  21. 'date.hour' => array('value' => date('H'),'description' => 'heure courante'),
  22. 'date.minut' => array('value' => date('i'),'description' => 'Minutes courantes'),
  23. 'date.second' => array('value' => date('s'),'description' => 'Secondes courantes'),
  24. 'user.login' => array('value' => $myUser->login,'description' => 'Identifiant utilisateur courant')
  25. );
  26. }
  27. //Vérification des acces sur le widget courant ou sur le rapport du widget courant
  28. public function check_access($crud,$user = null){
  29. global $myUser,$myFirm;
  30. if(!isset($user)) $user = $myUser;
  31. //pour avoir 'lacces, l'utilisateur doit être le créateur OU avoir access au rapport OU avoir acces au widget
  32. return $user->login == $this->creator ||
  33. $user->can('statistic_report',$crud,$this->report) ||
  34. $user->can('statistic_filter',$crud,$this->id);
  35. }
  36. public function toHtml(){
  37. $readony = $this->check_access('edit') ? '': ' readonly="readonly" ';
  38. $html = '<label title="'.$this->getSlug().'">'.$this->label.'</label>';
  39. $default = $this->default;
  40. $meta = self::getMetaTerms();
  41. $default = preg_replace_callback('|{{(.*)([\-\+]*)([0-9]*)}}|U', function($matches) use($meta) {
  42. $t = $matches[0];
  43. if(isset($meta[$matches[1]])) $t = $meta[$matches[1]]['value'];
  44. if(!empty($matches[2]) && !empty($matches[3])){
  45. if($matches[2]=='+') $t+=$matches[3];
  46. if($matches[2]=='-') $t-=$matches[3];
  47. }
  48. return $t;
  49. }, $default);
  50. switch($this->type){
  51. case 'list' :
  52. $html .= '<select class="form-control" '.$readony.' data-slug="'.$this->getSlug().'">';
  53. if(!empty($this->datasource)){
  54. list($type,$other) = explode(':', $this->datasource);
  55. switch($type){
  56. case 'plain':
  57. $items = explode("\n", $this->datasource);
  58. array_shift($items);
  59. foreach($items as $item){
  60. list($key,$value) = explode(':', $item);
  61. $html .= '<option value="'.$key.'" '.($default==$key ? 'selected="selected"': '').'>'.$value.'</option>';
  62. }
  63. break;
  64. case 'query':
  65. $results = self::staticQuery($other);
  66. foreach($results->fetchAll() as $line){
  67. $html .= '<option value="'.$line[0].'" '.($default==$line[0] ? 'selected="selected"': '').'>'.$line[1].'</option>';
  68. }
  69. break;
  70. case 'slug':
  71. foreach(Dictionary::childs(array("slug"=>$other)) as $line){
  72. $html .= '<option value="'.$line->id.'" '.($default==$line->slug ? 'selected="selected"': '').'>'.$line->label.'</option>';
  73. }
  74. break;
  75. }
  76. }
  77. $html .= '</select>';
  78. break;
  79. case 'date' :
  80. $default = explode('/',$default);
  81. //s'assure que les mois/jours sont sur deux nombres
  82. foreach ($default as $i=>$value) {
  83. if( ($i==0 || $i==1) && strlen($value)==1 ) $default[$i] = str_pad($value, 2,'0',STR_PAD_LEFT);
  84. }
  85. $default = implode('/',$default);
  86. $html .= '<input data-type="date" '.$readony.' value="'.$default.'" class="form-control" type="text" placeholder="jj/mm/aaaa" data-slug="'.$this->getSlug().'">';
  87. break;
  88. case 'hour' :
  89. $html .= '<input data-type="hour" '.$readony.' value="'.$default.'" class="form-control" type="text" placeholder="08:00" data-slug="'.$this->getSlug().'">';
  90. break;
  91. case 'user' :
  92. $html .= '<input class="form-control" '.$readony.' data-type="user" value="'.$default.'" type="text" data-slug="'.$this->getSlug().'">';
  93. break;
  94. case 'year' :
  95. $html .= '<input class="form-control" '.$readony.' value="'.$default.'" type="number" data-slug="'.$this->getSlug().'">';
  96. break;
  97. case 'month' :
  98. $months = array("Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Août","Septembre","Octobre","Novembre","Décembre");
  99. $html .= '<select class="form-control" '.$readony.' data-slug="'.$this->getSlug().'">';
  100. foreach($months as $idx => $month){
  101. $key = $idx+1;
  102. $html .= '<option value="'.$key.'" '.($default==$key ? 'selected="selected"': '').'>'.$month.'</option>';
  103. }
  104. $html .= '</select>';
  105. break;
  106. case 'number' :
  107. $html .= '<input class="form-control" '.$readony.' value="'.$default.'" type="number" data-slug="'.$this->getSlug().'">';
  108. break;
  109. case 'boolean' :
  110. $html .= '<input class="form-control" '.$readony.' '.($default=='1'?'checked="checked"':'').' type="checkbox" data-slug="'.$this->getSlug().'">';
  111. break;
  112. default :
  113. $html .= '<input class="form-control" '.$readony.' value="'.$default.'" type="text" data-slug="'.$this->getSlug().'">';
  114. break;
  115. }
  116. return $html;
  117. }
  118. public function getSlug(){
  119. return slugify($this->label);
  120. }
  121. public static function types(){
  122. return array(
  123. 'text' => array('label'=>'Texte','datasource'=>false),
  124. 'list' => array('label'=>'Liste','datasource'=>true),
  125. 'date' => array('label'=>'Date','placeholder'=>'jj/mm/aaaaa','datasource'=>false),
  126. 'hour' => array('label'=>'Heure','placeholder'=>'08:00','datasource'=>false),
  127. 'user' => array('label'=>'Utilisateur','placeholder'=>'login.utilisateur','datasource'=>false),
  128. 'number' => array('label'=>'Nombre','placeholder'=>'ex: 12','datasource'=>false),
  129. 'year' => array('label'=>'Année','placeholder'=>'ex: 2009','datasource'=>false),
  130. 'month' => array('label'=>'Mois','placeholder'=>'ex: 01','datasource'=>false),
  131. 'boolean' => array('label'=>'Vrai/Faux','placeholder'=>'0 ou 1','datasource'=>false),
  132. );
  133. }
  134. }
  135. ?>