/reason_4.0/lib/core/classes/filter.php

https://github.com/luthercollege/reason_package · PHP · 221 lines · 158 code · 10 blank · 53 comment · 14 complexity · 541ac9716e6689a34b9b39c4ab8cb36c MD5 · raw file

  1. <?php
  2. /**
  3. * A disco class for Reason admin filter forms
  4. * @package reason
  5. * @subpackage classes
  6. */
  7. /**
  8. * Required Includes
  9. */
  10. include_once('reason_header.php');
  11. reason_include_once( 'classes/entity.php' );
  12. reason_include_once( 'classes/admin/admin_page.php' );
  13. include_once( DISCO_INC . 'disco.php' );
  14. /**
  15. * This class works as a filter for the list_content page. Prints out all the pre stuff
  16. * then creates a form object. Takes all global variables and puts them into hidden
  17. * fields.
  18. * @author Brendon Stanton
  19. * @package reason
  20. */
  21. class filter extends Disco
  22. {
  23. /**
  24. * Default actions, there should only be one which is submit.
  25. * @access private
  26. * @var array
  27. */
  28. var $actions = array( 'submit' => 'Search', 'clear' => 'Clear' );
  29. /**
  30. * The admin page class passed in as a paramater. This way, the form can access
  31. * anything it needs from the class
  32. * @access private
  33. * @var AdminPage
  34. */
  35. var $page;
  36. /**
  37. * Sets page to bet the admin page. Should get called before doing anything else
  38. * @param AdminPage $page The admin page class
  39. * @return void
  40. */
  41. function set_page( &$page) // {{{
  42. {
  43. $this->page =& $page;
  44. } // }}}
  45. /**
  46. * Grabs Any Search fields from the Viewer and sets up display names properly
  47. * @param Viewer $viewer The viewer that we are a part of
  48. * @return void
  49. */
  50. function grab_fields( $viewer )
  51. {
  52. $this->set_form_method('get');
  53. if( !$this->has_filters() ) unset($this->actions['clear']); // remove filter if we have no search fields
  54. $this->add_element( 'search_exact_id' , 'hidden' );
  55. $this->set_value( 'search_exact_id' , true );
  56. $this->add_element( 'refresh_lister_state', 'hidden' );
  57. $this->set_value( 'refresh_lister_state', true );
  58. if( $viewer )
  59. {
  60. $this->get_db_fields( $viewer );
  61. reset( $viewer );
  62. while( list( $field , ) = each( $viewer ) )
  63. {
  64. $key = 'search_' . $field;
  65. //add fields in different ways
  66. if( !empty( $this->fields[ $field ] ) && preg_match( "/^enum\((.*)\)$/" , $this->fields[ $field ][ 'db_type' ] ) )
  67. $this->add_enum_element( $field );
  68. else
  69. $this->add_element( $key , 'text' , array( 'size' => 20 ) );
  70. if( isset( $this->page->request[ $key ] ) AND $this->page->request[ $key ] )
  71. $this->set_value( $key , $this->page->request[ $key ] );
  72. $this->set_display_name( $key , $field );
  73. }
  74. }
  75. foreach( $this->page->module->viewer->request as $key => $value )
  76. {
  77. if(!$this->is_element($key))
  78. {
  79. $this->add_element( $key, 'hidden');
  80. $this->set_value( $key, $value );
  81. }
  82. }
  83. }
  84. /**
  85. * Checks the page to see if any search values have already been submitted
  86. * @return bool
  87. */
  88. function has_filters() // {{{
  89. {
  90. foreach( $_REQUEST AS $k => $r )
  91. {
  92. if( preg_match( '/^search_/' , $k ) && !$this->is_search_type( $k ) && !empty( $r ) )
  93. return true;
  94. }
  95. return false;
  96. } // }}}
  97. /**
  98. * Returns true if the name of a variable is a search type
  99. * @return bool
  100. */
  101. function is_search_type( $name ) // {{{
  102. {
  103. if( preg_match( '/^search_exact_/' , $name ) ) return true;
  104. if( preg_match( '/^search_less_than_/' , $name ) ) return true;
  105. if( preg_match( '/^search_less_than_equal_/' , $name ) ) return true;
  106. if( preg_match( '/^search_greater_than_/' , $name ) ) return true;
  107. if( preg_match( '/^search_greater_than_equal_/' , $name ) ) return true;
  108. return false;
  109. } // }}}
  110. /**
  111. * This adds a field as an enum element. $field must be a field in reason
  112. * @param string $field Name of the enum field
  113. * @return void
  114. */
  115. function add_enum_element( $field ) // {{{
  116. {
  117. $enum_string = $this->fields[ $field ][ 'db_type' ];
  118. preg_match( "/^enum\((.*)\)$/", $enum_string , $matches );
  119. $options = array();
  120. $opts = array();
  121. $t = 'select';
  122. // explode on the commas
  123. $options = explode( ',', $matches[1] );
  124. // get rid of the single quotes at the beginning and end of the string
  125. // MySQL also escapes single quotes with single quotes, so if we see two single quotes, replace those two with one
  126. reset( $options );
  127. while( list( $key,$val ) = each ( $options ) )
  128. $options[ $key ] = str_replace("''","'",substr( $val,1,-1 ));
  129. reset( $options );
  130. while( list( ,$val ) = each( $options ) )
  131. $opts[ $val ] = $val;
  132. $args['options'] = $opts;
  133. $this->add_element( 'search_' . $field , 'select' , $args );
  134. $this->add_element( 'search_exact_' . $field , 'hidden' );
  135. $this->set_value( 'search_exact_' . $field , true );
  136. } // }}}
  137. /**
  138. * Grabs a list of fields associated with the current type in case we need them later.
  139. * Used in add_enum_element( $field ).
  140. * @param Viewer $viewer
  141. */
  142. function get_db_fields( $viewer ) // {{{
  143. {
  144. $n = count( $viewer );
  145. $i = 0;
  146. $in = '( ';
  147. foreach( $viewer AS $key => $t )
  148. {
  149. $i++;
  150. $in .= '"' . $key . '"';
  151. if( $i != $n )
  152. $in .= ', ';
  153. }
  154. $in .= ')';
  155. $d = new DBSelector;
  156. $d->add_table( 'entity' );
  157. $d->add_table( 'field' );
  158. $d->add_table( 'r1' , 'relationship' );
  159. $d->add_table( 'ar1' , 'allowable_relationship' );
  160. $d->add_table( 'r2' , 'relationship' );
  161. $d->add_table( 'ar2' , 'allowable_relationship' );
  162. $d->add_field( 'field' , '*' );
  163. $d->add_field( 'entity' , '*' );
  164. $d->add_relation( 'ar1.name = "type_to_table"' );
  165. $d->add_relation( 'ar2.name = "field_to_entity_table"' );
  166. $d->add_relation( 'r1.type = ar1.id' );
  167. $d->add_relation( 'r2.type = ar2.id' );
  168. $d->add_relation( 'entity.id = field.id' );
  169. $d->add_relation( 'r1.entity_a = ' . $this->page->type_id );
  170. $d->add_relation( 'r1.entity_b = r2.entity_b' );
  171. $d->add_relation( 'r2.entity_a = field.id' );
  172. if( $n > 0 )
  173. $d->add_relation( 'entity.name IN ' . $in );
  174. $fields = $d->run();
  175. $this->fields = array();
  176. foreach( $fields AS $field )
  177. $this->fields[ $field[ 'name' ] ] = $field;
  178. } // }}}
  179. /**
  180. * after all error checking is finish, this finishes the search
  181. * @return void
  182. */
  183. function finish() // {{{
  184. {
  185. if( preg_match( '/clear/' , $this->get_chosen_action() ) )
  186. {
  187. $array = array('page' => false , 'submitted' => '', 'submit' => '', 'clear' => '', 'refresh_lister_state' => '1');
  188. if( !empty( $this->page->request[ 'order_by' ] ) )
  189. $array[ 'order_by' ] = $this->page->request[ 'order_by' ];
  190. if( !empty( $this->page->request[ 'dir' ] ) )
  191. $array[ 'dir' ] = $this->page->request[ 'dir' ];
  192. if( !empty( $this->page->request[ 'lister' ] ) )
  193. $array[ 'lister' ] = $this->page->request[ 'lister' ];
  194. if( !empty( $this->page->request[ 'state' ] ) )
  195. $array[ 'state' ] = $this->page->request[ 'state' ];
  196. $link = unhtmlentities( $this->page->make_link( $array ) );
  197. }
  198. else
  199. {
  200. $link = unhtmlentities( $this->page->make_link( array('page' => false , 'submitted' => '', 'submit' => '', 'clear' => '' ) , true ) );
  201. }
  202. return $link;
  203. } // }}}
  204. }
  205. ?>