1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8//this script may only be included - so its better to die if called directly.
9if (strpos($_SERVER['SCRIPT_NAME'], basename(__FILE__)) !== false) {
10	header('location: index.php');
11	exit;
12}
13
14/**
15 * Class Table_Code_WidgetOptionsFilter
16 *
17 * Creates the code for the filter widget options portion of the Tablesorter jQuery code
18 *
19 * @package Tiki
20 * @subpackage Table
21 * @uses Table_Code_WidgetOptions
22 */
23class Table_Code_WidgetOptionsFilter extends Table_Code_WidgetOptions
24{
25
26	protected function getOptionArray()
27	{
28		if (parent::$filters) {
29			$wof[] = 'filter_cssFilter : \'form-control-sm\'';
30			//allows for different label versus value in dropdowns
31			$wof[] = 'filter_selectSourceSeparator : \'|\'';
32			//server side filtering
33			if (parent::$ajax) {
34				$wof[] = 'filter_serversideFiltering : true';
35			}
36			//hide filters
37			if (isset(parent::$s['filters']['hide']) && parent::$s['filters']['hide'] === true) {
38				$wof[] = 'filter_hideFilters : true';
39			}
40			//filter reset
41			if (isset(parent::$s['filters']['type']) && parent::$s['filters']['type'] === 'reset') {
42				$wof[] = 'filter_reset : \'button#' . parent::$s['filters']['reset']['id'] . '\'';
43			}
44			//filter_functions and filter_formatter
45			if (parent::$filtercol) {
46				$ffunc = [];
47				$fsource = [];
48				$fform = [];
49				foreach (parent::$s['columns'] as $col => $info) {
50					$info = ! empty($info['filter']) ? $info['filter'] : [];
51					$colpointer = parent::$usecolselector ? (string) '\'' . $col . '\'' : (int) $col;
52					if (! empty($info['type'])) {
53						switch ($info['type']) {
54							case 'dropdown':
55								$o = [];
56								if (array_key_exists('options', $info)) {
57									foreach ($info['options'] as $key => $val) {
58										$label = addcslashes(is_numeric($key) ? $val : $key, "'/");
59										if (parent::$ajax) {
60											$o[] = '\'' . $label . '\' : function() {}';
61										} else {
62											$o[] = '\'' . $label . '\' : function(e, n, f, i) { return /' . $val
63													. '/.test(e);}';
64										}
65									}
66									if (count($o) > 0) {
67										$options = $this->iterate($o, '{', $this->nt4 . '}', $this->nt5, '', ',');
68										$ffunc[] = $colpointer . ' : ' . $options;
69									}
70								} elseif (! parent::$ajax) {
71									$ffunc[] = $colpointer . ' : {
72					\'(empty)\': function( e, n, f, i, $r, c, data ) {
73						if( typeof e === "Object" && typeof n === "Object" && typeof f === "undefined" ) {
74							// v2.22.0 compatibility
75							c = e;
76							data = n;
77						}
78						return ( "" + data.exact ) === "";
79					}
80				}';
81									$fsource[] = $colpointer . ' : function( table, column, onlyAvail ) {'
82										. $this->nt4 . 'var array = $.tablesorter.filter.getOptions(table, column, onlyAvail);'
83										. $this->nt4 . 'array = array.join(\',\').split(/\s*,\s*/);'
84										. ( array_key_exists('empty', $info) ?
85											$this->nt4 . 'array.push({value: \'(empty)\', text: ' . json_encode($info['empty']) . '});'
86											: '' )
87										. $this->nt4 . 'return array;'
88									. $this->nt3 . '}';
89								}
90								break;
91							case 'range':
92								$min = isset($info['from']) ? $info['from'] : 0;
93								$max = isset($info['to']) ? $info['to'] : 100;
94								$valtohead = isset($info['style']) && $info['style'] == 'popup' ? 'false' : 'true';
95								$fform[] = $colpointer . ' : function($cell, indx){return $.tablesorter.filterFormatter.uiRange('
96										. '$cell, indx, {values: [' . $min . ', ' . $max . '], min: ' . $min . ', max: ' . $max
97										. ', delayed: false, valueToHeader: ' . $valtohead . ', exactMatch: true});}';
98								break;
99							case 'date':
100								$fm = isset($info['from']) ? $info['from'] : '';
101								$to = isset($info['to']) ? $info['to'] : '';
102								$format = isset($info['format']) ? $info['format'] : 'yy-mm-dd';
103								$fform[] = $colpointer . ' : function($cell, indx){return $.tablesorter.filterFormatter.uiDatepicker('
104										. '$cell, indx, {from: \'' . $fm . '\', to: \'' . $to . '\', dateFormat: \'' . $format
105										. '\', changeMonth: true, changeYear: true});}';
106								break;
107						}
108					}
109				}
110				unset($col, $info);
111				if (is_array($ffunc)) {
112					$wof[] = $this->iterate($ffunc, 'filter_functions : {', $this->nt3 . '}', $this->nt4, '');
113				}
114				if (is_array($fsource)) {
115					$wof[] = $this->iterate($fsource, 'filter_selectSource : {', $this->nt3 . '}', $this->nt4, '');
116				}
117				if (is_array($fform)) {
118					$wof[] = $this->iterate($fform, 'filter_formatter : {', $this->nt3 . '}', $this->nt4, '');
119				}
120			}
121			if (count($wof) > 0) {
122				return $wof;
123			} else {
124				return false;
125			}
126		} else {
127			return false;
128		}
129	}
130}
131