PageRenderTime 49ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/resources/classes/class.datagrid_filters.php

https://bitbucket.org/briancastellanos/cse360
PHP | 326 lines | 213 code | 39 blank | 74 comment | 19 complexity | 9b98a60f651ebd39192a2e471139c1f6 MD5 | raw file
  1. <?php
  2. /**
  3. * @version 1.3.1
  4. * @since v1.3.1 2011-11-18 Filters display 100 Characters
  5. * @since v1.3 2010-04-27 Added support for datagrid 4.4
  6. * @since v1.2 2009-12-28 added support for optiongroups
  7. *
  8. *
  9. */
  10. define("FILTER_CONJUNCTION_OR",0);
  11. define("FILTER_CONJUNCTION_AND",1);
  12. define("FILTER_COMPARITIVE_EQUAL_TO",0);
  13. define("FILTER_COMPARITIVE_NOT_EQUAL_TO",1);
  14. define("FILTER_COMPARITIVE_EQ_GREATER_THAN",2);
  15. define("FILTER_COMPARITIVE_GREATER_THAN",3);
  16. define("FILTER_COMPARITIVE_LESS_THAN",4);
  17. define("FILTER_COMPARITIVE_LESS_THAN_EQ_TO",9);
  18. define("FILTER_COMPARITIVE_LIKE_BEFORE",5);
  19. define("FILTER_COMPARITIVE_LIKE_AFTER",6);
  20. define("FILTER_COMPARITIVE_LIKE",7);
  21. define("FILTER_COMPARITIVE_IS",8);
  22. define("FILTER_COMPARITIVE_IS_NOT",10);
  23. /**
  24. * The Datgrid Filter class creates a new text box or drop down list based
  25. * filter that is associated to a datagrid to create a filter WHERE condition for a
  26. * query
  27. *
  28. */
  29. abstract class AbstractDatagridFilter {
  30. /**
  31. * Set the linked field name, or use "`table`.`field` syntax
  32. * @var string
  33. */
  34. public $db_field_name;
  35. public $db_hash;
  36. public $value;
  37. public $comparison;
  38. /**
  39. * The conjution defines what type of conjunction should be used - FILTER_CONJUNCTION_OR or FILTER_CONJUNCTION_AND
  40. *
  41. * @var int
  42. */
  43. public $conjunction;
  44. /**
  45. * Set weather or not add quotes around the value (e.g. '$value') when building the statement.
  46. *
  47. * @var bool
  48. */
  49. public $quote_value;
  50. public $add_slashes;
  51. /**
  52. * Set wheather to skip this field in the dump if there is no value supplied.
  53. *
  54. * @var unknown_type
  55. */
  56. public $skip_on_null=true;
  57. /**
  58. * Instantiate a new datagrid filter. $field_name is the name of the field in the query that you would like to add a filter to
  59. * $conjuction is the
  60. *
  61. * @param string $field_name
  62. * @param int $conjuction
  63. * @param int $comparison_operator
  64. */
  65. public function __construct($field_name,$conjuction,$comparison_operator,$options=null){
  66. $f=explode(".",$field_name);
  67. if(count($f)==1){
  68. $field=str_replace("`","",$f[0]);
  69. $table=null;
  70. }else{
  71. $field=str_replace("`","",$f[1]);
  72. $table=str_replace("`","",$f[0]);
  73. }
  74. $this->conjunction=$conjuction;
  75. $this->setDbField($field,$table);
  76. $this->comparison=$comparison_operator;
  77. $this->quote_value=true;
  78. $this->add_slashes=true;
  79. $this->db_hash=calcPassphrase($this->db_field_name);
  80. if(isset($_REQUEST['dgfilter'][$this->db_hash])){
  81. $this->value=(get_magic_quotes_gpc()?stripslashes($_REQUEST['dgfilter'][$this->db_hash]):$_REQUEST['dgfilter'][$this->db_hash]);
  82. }else{
  83. if($_SESSION['dgfilter'][$this->db_hash]==''){
  84. $this->value='';
  85. }else{
  86. $this->value=$_SESSION['dgfilter'][$this->db_hash];
  87. }
  88. }
  89. $_SESSION['dgfilter'][$this->db_hash]=$this->value;
  90. # $this->value=(get_magic_quotes_gpc()?stripslashes($_REQUEST['dgfilter'][$this->db_hash]):$_REQUEST['dgfilter'][$this->db_hash]);
  91. }
  92. public function setDbField($field,$table=null){
  93. if($field==null){
  94. $bt = debug_backtrace();
  95. if(!stristr($bt[0]['file'],basename(__FILE__))){
  96. $bt = $bt[0];
  97. }else{
  98. $bt = $bt[1];
  99. }
  100. $file = str_replace(DOC_ROOT,"",$bt['file']);
  101. $line = $bt['line'];
  102. $function = $bt['function'];
  103. user_error("DatagridFilter::{$function}() call failed on line {$line} in {$file}. A field name must be provided for the filter. The value provided was: <pre style='display:inline;'>".var_export($field,true)."</pre>",E_USER_WARNING);
  104. }
  105. $field=($table!=null?"`$table`.":"")."`$field`";
  106. $this->db_field_name=$field;
  107. }
  108. function concat_attributes($attributes){
  109. if(!is_array($attributes)){
  110. return false;
  111. }
  112. foreach($attributes as $att=>$value){
  113. $strAttributes .=htmlentities($att,ENT_QUOTES).'="'.htmlentities($value,ENT_QUOTES).'" ';
  114. }
  115. return $strAttributes;
  116. }
  117. }
  118. /**
  119. * Create a new text box filter
  120. *
  121. */
  122. class DatagridTextFilter extends AbstractDatagridFilter{
  123. public function __construct($field_name,$comparison_operator=FILTER_COMPARITIVE_EQUAL_TO,$conjunction=FILTER_CONJUNCTION_OR){
  124. parent::__construct($field_name,$conjunction,$comparison_operator);
  125. }
  126. public function createFilter($attributes=null){
  127. $strAttributes=$this->concat_attributes($attributes);
  128. $txtarea='<input type="text" name="dgfilter['.htmlentities($this->db_hash,ENT_QUOTES).']" value="'.htmlentities($this->value,ENT_QUOTES).'" '.$strAttributes.' />';
  129. return $txtarea;
  130. }
  131. }
  132. /**
  133. * Creates a drop down box filter
  134. *
  135. */
  136. class DatagridListFilter extends AbstractDatagridFilter{
  137. public function __construct($field_name,$comparison_operator=FILTER_COMPARITIVE_EQUAL_TO,$conjunction=FILTER_CONJUNCTION_OR){
  138. parent::__construct($field_name,$conjunction,$comparison_operator);
  139. }
  140. public function createFilter($options,$attributes=null,$isNodeList = false){
  141. $strAttributes=$this->concat_attributes($attributes);
  142. if(!is_array($options))$options=array();
  143. if(!is_array($attributes))$attributes=array();
  144. #$attributes+=array("dojoType"=>"dijit.form.FilteringSelect",'autoComplete'=>"true");
  145. global $form;
  146. if(!$form instanceof Form ){
  147. include_class("form");
  148. $form = new Form(FORM_RETURN_VALUE);
  149. }
  150. $form->max_display = 100;
  151. if($isNodeList){
  152. $cbo = $form->type_select('dgfilter['.htmlentities($this->db_hash,ENT_QUOTES).']',$this->value,$options,$attributes);
  153. }else{
  154. $cbo = $form->select('dgfilter['.htmlentities($this->db_hash,ENT_QUOTES).']',$this->value,$options,$attributes);
  155. }
  156. /* //$strAttributes=$this->concat_attributes($attributes);
  157. $cbo = '<select name="dgfilter['.htmlentities($this->db_hash,ENT_QUOTES).']" '.$strAttributes.'>';
  158. foreach($options as $val=>$dsp){
  159. $cbo.='<option value="'.htmlentities($val,ENT_QUOTES).'"'.($val==$this->value?' selected="selected"':null).'>'.htmlentities($dsp,ENT_QUOTES).'</option>';
  160. }
  161. $cbo.='</select>';*/
  162. if($this->return_mode=="echo"){
  163. echo $cbo;
  164. }
  165. else{
  166. return $cbo;
  167. }
  168. }
  169. }
  170. $__DATAGRID_FILTER_LEVELS=array();
  171. class DatagridFilterLevel{
  172. public $family;
  173. public $name;
  174. public $datagrid_conjunction;
  175. public $value;
  176. /**
  177. * Create a new filter condition level to create a WHERE (condition AND condition OR (condition AND condition)))
  178. * relationship
  179. *
  180. * @param object $filter_obj
  181. * @param object $parent
  182. * @param int $conjunction
  183. */
  184. public function __construct(&$filter_obj,$parent=null){
  185. #,$conjunction=FILTER_CONJUNCTION_OR
  186. global $__DATAGRID_FILTER_LEVELS;
  187. #$this->datagrid_conjunction=$conjunction;
  188. if($parent!==null){
  189. if(!is_object($parent)){
  190. throw new Exception("The parent does not exist");
  191. }else{
  192. $parent->attachChild($this,$filter_obj, $this->family);
  193. }
  194. }else{
  195. $filter_index=count($__DATAGRID_FILTER_LEVELS);
  196. $__DATAGRID_FILTER_LEVELS[$filter_index]=array('filter'=>$filter_obj,"conjunction"=>$conjunction,'children'=>array());
  197. $this->family =& $__DATAGRID_FILTER_LEVELS[$filter_index];
  198. }
  199. }
  200. /**
  201. * Attach a new child level to the current level
  202. *
  203. * @param object $obj
  204. * @param object $filter_obj
  205. * @param array $new_family
  206. */
  207. public function attachChild($obj,&$filter_obj,&$new_family){
  208. $child_index=count($this->family['children']);
  209. $this->family['children'][$child_index]=array('filter'=>$filter_obj,'conjunction'=>$obj->datagrid_conjunction,'children'=>array());
  210. $new_family =& $this->family['children'][$child_index];
  211. }
  212. public function buildStatement(){
  213. global $__DATAGRID_FILTER_LEVELS;
  214. foreach($__DATAGRID_FILTER_LEVELS as $child){
  215. $clause.=buildClause($child);
  216. }
  217. return $clause;
  218. }
  219. }
  220. function buildClause($child){
  221. switch($child['filter']->conjunction){
  222. case FILTER_CONJUNCTION_AND:
  223. $clause=" AND ";
  224. break;
  225. case FILTER_CONJUNCTION_OR:
  226. $clause=" OR ";
  227. break;
  228. }
  229. $clause.=" ({$child['filter']->db_field_name}";
  230. $value=($child['filter']->quote_value && ($child['filter']->comparison < 4) ?"'":"").
  231. ($child['filter']->add_slashes?addslashes($child['filter']->value):$child['filter']->value).
  232. ($child['filter']->quote_value && ($child['filter']->comparison < 4)?"'":"");
  233. switch($child['filter']->comparison){
  234. case FILTER_COMPARITIVE_EQUAL_TO:
  235. $clause.=" = $value";
  236. break;
  237. case FILTER_COMPARITIVE_GREATER_THAN:
  238. $clause.=" > $value";
  239. break;
  240. case FILTER_COMPARITIVE_EQ_GREATER_THAN:
  241. $clause.=" >= $value";
  242. break;
  243. case FILTER_COMPARITIVE_LESS_THAN:
  244. $clause.=" < $value";
  245. break;
  246. case FILTER_COMPARITIVE_LESS_THAN_EQ_TO:
  247. $clause.=" <= $value";
  248. break;
  249. case FILTER_COMPARITIVE_NOT_EQUAL_TO:
  250. $clause.=" <> $value";
  251. break;
  252. case FILTER_COMPARITIVE_LIKE:
  253. $clause.=" LIKE '%$value%'";
  254. break;
  255. case FILTER_COMPARITIVE_LIKE_BEFORE:
  256. $clause.=" LIKE '%$value'";
  257. break;
  258. case FILTER_COMPARITIVE_LIKE_AFTER:
  259. $clause.=" LIKE '$value%'";
  260. break;
  261. case FILTER_COMPARITIVE_IS:
  262. $clause.=" IS $value";
  263. break;
  264. case FILTER_COMPARITIVE_IS_NOT:
  265. $clause.=" IS NOT $value";
  266. break;
  267. default:
  268. $clause .=" $value";
  269. break;
  270. }
  271. if($child['filter']->value=='' && $child['filter']->skip_on_null){
  272. $clause='';
  273. $ending_parenthesis=false;
  274. }
  275. if(count($child['children'])>0){
  276. foreach($child['children'] as $grand_child){
  277. $clause.=buildClause($grand_child);
  278. }
  279. }
  280. if($ending_parenthesis!==false){
  281. $clause.=" )";
  282. }
  283. return $clause;
  284. }
  285. function calcPassphrase($string) {
  286. return strtoupper(str_replace("=","",md5($string)));
  287. }