PageRenderTime 99ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/filterbar.php

https://gitlab.com/futuredrive630/wp-event-list
PHP | 316 lines | 277 code | 18 blank | 21 comment | 48 complexity | ae6d290dd66799541dcdb4bc54f36f1e MD5 | raw file
  1. <?php
  2. if(!defined('ABSPATH')) {
  3. exit;
  4. }
  5. require_once( EL_PATH.'includes/db.php' );
  6. require_once( EL_PATH.'includes/categories.php' );
  7. // This class handles the navigation and filter bar
  8. class EL_Filterbar {
  9. private static $instance;
  10. private $db;
  11. private $categories;
  12. public static function &get_instance() {
  13. // Create class instance if required
  14. if( !isset( self::$instance ) ) {
  15. self::$instance = new EL_Filterbar();
  16. }
  17. // Return class instance
  18. return self::$instance;
  19. }
  20. private function __construct() {
  21. $this->db = &EL_Db::get_instance();
  22. $this->categories = &EL_Categories::get_instance();
  23. }
  24. // main function to show the rendered HTML output
  25. public function show($url, &$args) {
  26. $this->parse_args($args);
  27. $out = '
  28. <style type="text/css">
  29. .filterbar { display:table; width:100% }
  30. .filterbar > div { display:table-cell }
  31. </style>
  32. <!--[if lte IE 7]>
  33. <style>.filterbar > div { float:left }</style>
  34. <![endif]-->
  35. <div class="filterbar subsubsub">';
  36. // prepare filterbar-items
  37. //split 3 section (left, center, right) seperated by semicolon
  38. $sections = explode(";", html_entity_decode($args['filterbar_items']));
  39. $section_align = array('left', 'center', 'right');
  40. for($i=0; $i<sizeof($sections) && $i<3; $i++) {
  41. if(strlen($sections[$i]) > 0) {
  42. $out .= '
  43. <div style="text-align:'.$section_align[$i].'">';
  44. //split items in section seperated by comma
  45. $items = explode(",", $sections[$i]);
  46. foreach($items as $item) {
  47. //search for item options
  48. $options = array();
  49. $item_array = explode("(", $item);
  50. if(sizeof($item_array) > 1) {
  51. // options available
  52. $option_array = explode("|", substr($item_array[1],0,-1));
  53. foreach($option_array as $option_text) {
  54. $o = explode("=", $option_text);
  55. $options[$o[0]] = $o[1];
  56. }
  57. }
  58. $item_array = explode("_", $item_array[0]);
  59. switch($item_array[0]) {
  60. case 'years':
  61. $out .= $this->show_years($url, $args, $item_array[1], 'std', $options);
  62. break;
  63. case 'daterange':
  64. $out .= $this->show_daterange($url, $args, $item_array[1], 'std', $options);
  65. break;
  66. case 'cats':
  67. $out .= $this->show_cats($url, $args, $item_array[1], 'std', $options);
  68. break;
  69. case 'months':
  70. $out .= $this->show_months($url, $args, $item_array[1], 'std', $options);
  71. break;
  72. case 'reset':
  73. $out .= $this->show_reset($url, $args, $options);
  74. }
  75. }
  76. $out .= '
  77. </div>';
  78. }
  79. }
  80. $out .= '</div>';
  81. return $out;
  82. }
  83. public function show_years($url, &$args, $type='hlist', $subtype='std', $options=array()) {
  84. $default_options = array (
  85. 'show_all' => 'true',
  86. 'show_upcoming' => 'true',
  87. 'show_past' => 'false',
  88. 'years_order' => 'asc',
  89. );
  90. $options = wp_parse_args($options, $default_options);
  91. // prepare displayed elements
  92. $elements = array();
  93. if('true' == $options['show_all']) {
  94. $elements[] = $this->all_element('date', $type);
  95. }
  96. if('true' == $options['show_upcoming']) {
  97. $elements[] = $this->upcoming_element();
  98. }
  99. if('true' == $options['show_past']) {
  100. $elements[] = $this->past_element();
  101. }
  102. $event_years = $this->db->get_distinct_event_data('substr(`start_date`,1,4)', $args['date_filter'],$args['cat_filter'], $options['years_order']);
  103. foreach($event_years as $entry) {
  104. $elements[] = array('slug'=>$entry->data, 'name'=>$entry->data);
  105. }
  106. // display elements
  107. if('dropdown' === $type) {
  108. return $this->show_dropdown($elements, 'date'.$args['sc_id_for_url'], $subtype, $args['actual_date'], $args['sc_id_for_url']);
  109. }
  110. else {
  111. return $this->show_hlist($elements, $url, 'date'.$args['sc_id_for_url'], $args['actual_date']);
  112. }
  113. }
  114. public function show_months($url, &$args, $type='dropdown', $subtype='std', $options=array()) {
  115. $default_options = array (
  116. 'show_all' => 'false',
  117. 'show_upcoming' => 'false',
  118. 'show_past' => 'false',
  119. 'months_order' => 'asc',
  120. 'date_format' => 'Y-m',
  121. );
  122. $options = wp_parse_args($options, $default_options);
  123. // prepare displayed elements
  124. $elements = array();
  125. if('true' == $options['show_all']) {
  126. $elements[] = $this->all_element('date', $type);
  127. }
  128. if('true' == $options['show_upcoming']) {
  129. $elements[] = $this->upcoming_element();
  130. }
  131. if('true' == $options['show_past']) {
  132. $elements[] = $this->past_element();
  133. }
  134. $event_months = $this->db->get_distinct_event_data('substr(`start_date`,1,7)', $args['date_filter'],$args['cat_filter'], $options['months_order']);
  135. foreach($event_months as $entry) {
  136. list($year, $month) = explode('-', $entry->data);
  137. $elements[] = array('slug' => $entry->data, 'name' => date($options['date_format'], mktime(0,0,0,$month,1,$year)));
  138. }
  139. // display elements
  140. if('hlist' === $type) {
  141. return $this->show_hlist($elements, $url, 'date'.$args['sc_id_for_url'], $args['actual_date']);
  142. }
  143. else {
  144. return $this->show_dropdown($elements, 'date'.$args['sc_id_for_url'], $subtype, $args["actual_date"], $args['sc_id_for_url']);
  145. }
  146. }
  147. public function show_daterange($url, &$args, $type='hlist', $subtype='std', $options) {
  148. // prepare displayed elements
  149. if(isset($options['item_order'])) {
  150. $items = explode('&', $options['item_order']);
  151. }
  152. else {
  153. $items = array('all', 'upcoming', 'past');
  154. }
  155. $elements = array();
  156. foreach($items as $item) {
  157. // show all
  158. switch($item) {
  159. case 'all':
  160. $elements[] = $this->all_element('date'); // Always show short form ... hlist
  161. break;
  162. case 'upcoming':
  163. $elements[] = $this->upcoming_element();
  164. break;
  165. case 'past':
  166. $elements[] = $this->past_element();
  167. }
  168. }
  169. // display elements
  170. if('dropdown' === $type) {
  171. return $this->show_dropdown($elements, 'date'.$args['sc_id_for_url'], $subtype, $args['actual_date'], $args['sc_id_for_url']);
  172. }
  173. else {
  174. return $this->show_hlist($elements, $url, 'date'.$args['sc_id_for_url'], $args['actual_date']);
  175. }
  176. }
  177. public function show_cats($url, &$args, $type='dropdown', $subtype='std', $options=array()) {
  178. $default_options = array (
  179. 'show_all' => 'true',
  180. );
  181. $options = wp_parse_args($options, $default_options);
  182. // prepare displayed elements
  183. $elements = array();
  184. if('true' == $options['show_all']) {
  185. $elements[] = $this->all_element('cat', $type);
  186. }
  187. //prepare required arrays
  188. $cat_array = $this->categories->get_cat_array();
  189. $events_cat_strings = $this->db->get_distinct_event_data('`categories`', $args['date_filter'],$args['cat_filter']);
  190. $events_cat_array = array();
  191. foreach($events_cat_strings as $cat_string) {
  192. $events_cat_array = array_merge($events_cat_array, $this->categories->convert_db_string($cat_string->data, 'slug_array'));
  193. }
  194. $events_cat_array = array_unique($events_cat_array);
  195. //create filtered cat_array
  196. $filtered_cat_array = array();
  197. $required_cats = array();
  198. for($i=count($cat_array)-1; 0<=$i; $i--) { // start from the end to have the childs first and be able to add the parent to the required_cats
  199. if(in_array($cat_array[$i]['slug'], $events_cat_array) || in_array($cat_array[$i]['slug'], $required_cats)) {
  200. array_unshift($filtered_cat_array, $cat_array[$i]); // add the new cat at the beginning (unshift) due to starting at the end in the loop
  201. if('' != $cat_array[$i]['parent']) { // the parent is required to show the categories correctly
  202. $required_cats[] = $cat_array[$i]['parent'];
  203. }
  204. }
  205. }
  206. //create elements array
  207. foreach($filtered_cat_array as $cat) {
  208. $elements[] = array('slug' => $cat['slug'], 'name' => str_pad('', 12*$cat['level'], '&nbsp;', STR_PAD_LEFT).$cat['name']);
  209. }
  210. // display elements
  211. if('hlist' === $type) {
  212. return $this->show_hlist($elements, $url, 'cat'.$args['sc_id_for_url'], $args['actual_cat']);
  213. }
  214. else {
  215. return $this->show_dropdown($elements, 'cat'.$args['sc_id_for_url'], $subtype, $args['actual_cat'], $args['sc_id_for_url']);
  216. }
  217. }
  218. public function show_reset($url, $args, $options) {
  219. $args_to_remove = array('event_id'.$args['sc_id_for_url'],
  220. 'date'.$args['sc_id_for_url'],
  221. 'cat'.$args['sc_id_for_url']);
  222. if(!isset($options['caption'])) {
  223. $options['caption'] = 'Reset';
  224. }
  225. return $this->show_link(remove_query_arg($args_to_remove, $url), $options['caption'], 'link');
  226. }
  227. private function show_hlist($elements, $url, $name, $actual=null) {
  228. $out = '<ul class="hlist">';
  229. foreach($elements as $element) {
  230. $out .= '<li>';
  231. if($actual == $element['slug']) {
  232. $out .= '<strong>'.$element['name'].'</strong>';
  233. }
  234. else {
  235. $out .= $this->show_link(add_query_arg($name, $element['slug'], $url), $element['name']);
  236. }
  237. $out .= '</li>';
  238. }
  239. $out .= '</ul>';
  240. return $out;
  241. }
  242. private function show_dropdown($elements, $name, $subtype='std', $actual=null, $sc_id='') {
  243. $onchange = '';
  244. if('admin' != $subtype) {
  245. wp_register_script('el_filterbar', EL_URL.'includes/js/filterbar.js', null, true);
  246. add_action('wp_footer', array(&$this, 'footer_script'));
  247. $onchange = ' onchange="eventlist_redirect(this.name,this.value,'.$sc_id.')"';
  248. }
  249. $out = '<select class="dropdown" name="'.$name.'"'.$onchange.'>';
  250. foreach($elements as $element) {
  251. $out .= '
  252. <option';
  253. if($element['slug'] == $actual) {
  254. $out .= ' selected="selected"';
  255. }
  256. $out .= ' value="'.$element['slug'].'">'.esc_html($element['name']).'</option>';
  257. }
  258. $out .= '
  259. </select>';
  260. return $out;
  261. }
  262. private function show_link($url, $caption, $class=null) {
  263. $class = (null === $class) ? '' : ' class="'.$class.'"';
  264. return '<a href="'.esc_url($url).'"'.$class.'>'.esc_html($caption).'</a>';
  265. }
  266. private function all_element($list_type='date', $display_type='hlist') {
  267. if('hlist' == $display_type) {
  268. $name = __('All','event-list');
  269. }
  270. else {
  271. $name = ('date' == $list_type) ? __('Show all dates','event-list') : __('Show all categories','event-list');
  272. }
  273. return array('slug' => 'all', 'name' => $name);
  274. }
  275. private function upcoming_element() {
  276. return array('slug' => 'upcoming', 'name' => __('Upcoming','event-list'));
  277. }
  278. private function past_element() {
  279. return array('slug' => 'past', 'name' => __('Past','event-list'));
  280. }
  281. private function parse_args(&$args) {
  282. $defaults = array('date' => null,
  283. 'actual_date' => null,
  284. 'actual_cat' => null,
  285. 'event_id' => null,
  286. 'sc_id_for_url' => '',
  287. );
  288. $args = wp_parse_args($args, $defaults);
  289. if(is_numeric($args['event_id'])) {
  290. $args['actual_date'] = null;
  291. $args['actual_cat'] = null;
  292. };
  293. }
  294. public function footer_script() {
  295. wp_print_scripts('el_filterbar');
  296. }
  297. }
  298. ?>