These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
0 ignored issues
–
show
|
|||
2 | |||
3 | /** |
||
4 | * File holding the SRFFiltered class. |
||
5 | * @author Stephan Gambke |
||
6 | * |
||
7 | */ |
||
8 | |||
9 | $formatDir = dirname( __FILE__ ) . '/'; |
||
10 | |||
11 | $wgAutoloadClasses['SRF_Filtered_Item'] = $formatDir . 'SRF_Filtered_Item.php'; |
||
12 | |||
13 | $wgAutoloadClasses['SRF_Filtered_View'] = $formatDir . 'views/SRF_Filtered_View.php'; |
||
14 | $wgAutoloadClasses['SRF_FV_List'] = $formatDir . 'views/SRF_FV_List.php'; |
||
15 | $wgAutoloadClasses['SRF_FV_Calendar'] = $formatDir . 'views/SRF_FV_Calendar.php'; |
||
16 | $wgAutoloadClasses['SRF_FV_Table'] = $formatDir . 'views/SRF_FV_Table.php'; |
||
17 | |||
18 | $wgAutoloadClasses['SRF_Filtered_Filter'] = $formatDir . 'filters/SRF_Filtered_Filter.php'; |
||
19 | $wgAutoloadClasses['SRF_FF_Value'] = $formatDir . 'filters/SRF_FF_Value.php'; |
||
20 | $wgAutoloadClasses['SRF_FF_Distance'] = $formatDir . 'filters/SRF_FF_Distance.php'; |
||
21 | |||
22 | |||
23 | /** |
||
24 | * Result printer that displays results in switchable views and offers |
||
25 | * client-side (JavaScript based) filtering. |
||
26 | * |
||
27 | * This result printer is ultimately planned to replace exhibit. Currently only |
||
28 | * a list view is available. It is not yet possible to switch between views. |
||
29 | * There is also only the 'value' filter available yet. |
||
30 | * |
||
31 | * Syntax of the #ask call: |
||
32 | * (This is only a syntax example. For currently available features see the |
||
33 | * documentation of the various classes.) |
||
34 | * |
||
35 | * {{#ask:[[SomeCondition]] |
||
36 | * |? SomePrintout |+filter=value, someFutureFilter |+value filter switches=and or, disable, all, none |+someFutureFilter filter option=someOptionValue |
||
37 | * |? SomeOtherPrintout |+filter=value, someOtherFutureFilter |+someOtherFutureFilter filter option=someOptionValue |
||
38 | * |
||
39 | * |format=filtered |
||
40 | * |views=list, someFutureView, someOtherFutureView |
||
41 | * |
||
42 | * |list view type=list |
||
43 | * |list view template=ListItem |
||
44 | * |
||
45 | * |someFutureView view option=someOptionValue |
||
46 | * |
||
47 | * |someOtherFutureView view option=someOptionValue |
||
48 | * |
||
49 | * }} |
||
50 | * |
||
51 | * All format specific parameters are optional, although leaving the 'views' |
||
52 | * parameter empty probably does not make much sense. |
||
53 | * |
||
54 | */ |
||
55 | class SRFFiltered extends SMWResultPrinter { |
||
56 | |||
57 | /** |
||
58 | * The available view types |
||
59 | * @var array of Strings |
||
60 | */ |
||
61 | private $mViewTypes = array( |
||
62 | 'list' => 'SRF_FV_List', |
||
63 | 'calendar' => 'SRF_FV_Calendar', |
||
64 | 'table' => 'SRF_FV_Table' |
||
65 | ); |
||
66 | |||
67 | /** |
||
68 | * The available filter types |
||
69 | * @var array of Strings |
||
70 | */ |
||
71 | private $mFilterTypes = array( |
||
72 | 'value' => 'SRF_FF_Value', |
||
73 | 'distance' => 'SRF_FF_Distance', |
||
74 | ); |
||
75 | |||
76 | private $mViews; |
||
77 | private $mParams; |
||
78 | private $mFiltersOnTop; |
||
79 | |||
80 | public function hasTemplates ( $hasTemplates = null ) { |
||
81 | $ret = $this->hasTemplates; |
||
82 | if ( is_bool( $hasTemplates ) ) { |
||
83 | $this->hasTemplates = $hasTemplates; |
||
84 | } |
||
85 | return $ret; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Get a human readable label for this printer. |
||
90 | * |
||
91 | * @return string |
||
92 | */ |
||
93 | public function getName() { |
||
94 | return wfMessage( 'srf-printername-filtered' )->text(); |
||
95 | } |
||
96 | |||
97 | protected function handleParameters( array $params, $outputmode ) { |
||
98 | parent::handleParameters( $params, $outputmode ); |
||
99 | |||
100 | // // Set in SMWResultPrinter: |
||
101 | // $this->mIntro = $params['intro']; |
||
102 | // $this->mOutro = $params['outro']; |
||
103 | // $this->mSearchlabel = $params['searchlabel'] === false ? null : $params['searchlabel']; |
||
104 | // $this->mLinkFirst = true | false; |
||
105 | // $this->mLinkOthers = true | false; |
||
106 | // $this->mDefault = str_replace( '_', ' ', $params['default'] ); |
||
107 | // $this->mShowHeaders = SMW_HEADERS_HIDE | SMW_HEADERS_PLAIN | SMW_HEADERS_SHOW; |
||
108 | |||
109 | $this->mSearchlabel = null; |
||
110 | |||
111 | $this->mParams = $params; |
||
112 | $this->mViews = explode( ',', $params['views'] ); |
||
113 | $this->mFiltersOnTop = $params['filter position'] === 'top'; |
||
114 | |||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Return serialised results in specified format. |
||
119 | */ |
||
120 | protected function getResultText( SMWQueryResult $res, $outputmode ) { |
||
121 | |||
122 | // collect the query results in an array |
||
123 | $result = array(); |
||
124 | while ( $row = $res->getNext() ) { |
||
125 | $result[uniqid()] = new SRF_Filtered_Item( $row, $this ); |
||
126 | } |
||
127 | |||
128 | $resourceModules = array(); |
||
129 | |||
130 | // prepare filter data for inclusion in HTML and JS |
||
131 | $filterHtml = ''; |
||
132 | $filterHandlers = array(); |
||
133 | $filterData = array(); |
||
134 | |||
135 | foreach ( $res->getPrintRequests() as $printRequest ) { |
||
136 | $filter = $printRequest->getParameter( 'filter' ); |
||
137 | if ( $filter ) { |
||
138 | |||
139 | $filtersForPrintout = array_map( 'trim', explode( ',', $filter ) ); |
||
140 | |||
141 | foreach ( $filtersForPrintout as $filterName ) { |
||
142 | if ( array_key_exists( $filterName, $this->mFilterTypes ) ) { |
||
143 | |||
144 | $filter = new $this->mFilterTypes[$filterName]( $result, $printRequest, $this ); |
||
145 | |||
146 | $resourceModules = $filter->getResourceModules(); |
||
147 | |||
148 | if ( is_array( $resourceModules ) ) { |
||
149 | array_walk( $resourceModules, 'SMWOutputs::requireResource' ); |
||
150 | } elseif ( is_string( $resourceModules ) ) { |
||
151 | SMWOutputs::requireResource( $resourceModules ); |
||
152 | } |
||
153 | |||
154 | $printRequestHash = md5( $printRequest->getHash() ); |
||
155 | $filterHtml .= Html::rawElement( 'div', array( 'class' => "filtered-$filterName $printRequestHash" ), $filter->getResultText() ); |
||
156 | |||
157 | $filterHandlers[$filterName] = null; |
||
158 | $filterData[$filterName][$printRequestHash] = $filter->getJsData(); |
||
159 | |||
160 | } |
||
161 | } |
||
162 | } |
||
163 | } |
||
164 | |||
165 | // wrap filters in a div |
||
166 | $filterHtml = Html::rawElement( 'div', array( 'class' => 'filtered-filters' ), $filterHtml ); |
||
167 | |||
168 | // prepare view data for inclusion in HTML and JS |
||
169 | $viewHtml = ''; |
||
170 | $viewSelectorsHtml = ''; |
||
171 | $viewHandlers = array(); |
||
172 | $viewElements = array(); // will contain the id of the html element to be used by the view |
||
173 | $viewData = array(); |
||
174 | |||
175 | foreach ( $this->mViews as $viewName ) { |
||
176 | |||
177 | // cut off the selector label (if one was specified) from the actual view name |
||
178 | $viewnameComponents = explode('=', $viewName, 2 ); |
||
179 | |||
180 | $viewName = trim( $viewnameComponents[0] ); |
||
181 | |||
182 | if ( array_key_exists( $viewName, $this->mViewTypes ) ) { |
||
183 | |||
184 | // generate unique id |
||
185 | $viewid = uniqid(); |
||
186 | |||
187 | $view = new $this->mViewTypes[$viewName]( $viewid, $result, $this->mParams, $this ); |
||
188 | |||
189 | if ( count( $viewnameComponents ) > 1 ) { |
||
190 | // a selector label was specified in the wiki text |
||
191 | $viewSelectorLabel = trim( $viewnameComponents[1] ); |
||
192 | } else { |
||
193 | // use the default selector label |
||
194 | $viewSelectorLabel = $view->getSelectorLabel(); |
||
195 | } |
||
196 | |||
197 | $resourceModules = $view->getResourceModules(); |
||
198 | |||
199 | if ( is_array( $resourceModules ) ) { |
||
200 | array_walk( $resourceModules, 'SMWOutputs::requireResource' ); |
||
201 | } elseif ( is_string( $resourceModules ) ) { |
||
202 | SMWOutputs::requireResource( $resourceModules ); |
||
203 | } |
||
204 | |||
205 | $viewHtml .= Html::rawElement( 'div', array( 'class' => "filtered-view filtered-$viewName filtered-view-id$viewid" ), $view->getResultText() ); |
||
206 | $viewSelectorsHtml .= Html::rawElement( 'div', array( 'class' => "filtered-view-selector filtered-$viewName filtered-view-id$viewid" ), $viewSelectorLabel ); |
||
207 | |||
208 | $viewHandlers[$viewName] = null; |
||
209 | $viewElements[$viewName][] = $viewid; |
||
210 | $viewData[$viewName] = $view->getJsData(); |
||
211 | } |
||
212 | } |
||
213 | |||
214 | // more than one view? |
||
215 | if ( count( $viewData ) > 1 ) { |
||
216 | // wrap views in a div |
||
217 | $viewHtml = Html::rawElement( 'div', array('class' => 'filtered-views', 'style' => 'display:none'), |
||
218 | Html::rawElement( 'div', array('class' => 'filtered-views-selectors-container'), $viewSelectorsHtml ) . |
||
219 | Html::rawElement( 'div', array('class' => 'filtered-views-container'), $viewHtml ) |
||
220 | ); |
||
221 | } else { |
||
222 | // wrap views in a div |
||
223 | $viewHtml = Html::rawElement( 'div', array('class' => 'filtered-views', 'style' => 'display:none'), |
||
224 | Html::rawElement( 'div', array('class' => 'filtered-views-container'), $viewHtml ) |
||
225 | ); |
||
226 | } |
||
227 | |||
228 | // Define the srf_filtered_values array |
||
229 | SMWOutputs::requireScript( 'srf_filtered_values', Html::inlineScript( |
||
230 | 'srf_filtered_values = {};' |
||
231 | ) |
||
232 | ); |
||
233 | |||
234 | $resultAsArray = array(); |
||
235 | foreach ( $result as $id => $value ) { |
||
236 | $resultAsArray[$id] = $value->getArrayRepresentation(); |
||
237 | } |
||
238 | |||
239 | $id = uniqid(); |
||
240 | SMWOutputs::requireScript( 'srf_filtered_values' . $id, |
||
241 | Html::inlineScript( |
||
242 | 'srf_filtered_values["' . $id . '"] = { "values":' . json_encode( $resultAsArray ) . |
||
243 | ', "data": {' . |
||
244 | ' "viewhandlers" : ' . json_encode( $viewHandlers ) . |
||
245 | ', "viewelements" : ' . json_encode( $viewElements ) . |
||
246 | ', "viewdata" : ' . json_encode( $viewData ) . |
||
247 | ', "filterhandlers" : ' . json_encode( $filterHandlers ) . |
||
248 | ', "filterdata" : ' . json_encode( $filterData ) . |
||
249 | ', "sorthandlers" : ' . json_encode( array() ) . |
||
250 | ', "sorterdata" : ' . json_encode( array() ) . |
||
251 | // ', "sorterhandlers" : ' . json_encode( $sorterHandlers ) . |
||
252 | // ', "sorterdata" : ' . json_encode( $sorterData ) . |
||
253 | '}};' |
||
254 | ) |
||
255 | ); |
||
256 | |||
257 | SMWOutputs::requireResource( 'ext.srf.filtered' ); |
||
258 | |||
259 | // wrap all in a div |
||
260 | if ( $this->mFiltersOnTop ) { |
||
261 | $html = Html::rawElement( 'div', array( 'class' => 'filtered ' . $id ), $filterHtml . $viewHtml ); |
||
262 | } else { |
||
263 | $html = Html::rawElement( 'div', array( 'class' => 'filtered ' . $id ), $viewHtml. $filterHtml ); |
||
264 | } |
||
265 | |||
266 | return $html; |
||
267 | } |
||
268 | |||
269 | |||
270 | /** |
||
271 | * @see SMWResultPrinter::getParamDefinitions |
||
272 | * |
||
273 | * @since 1.8 |
||
274 | * |
||
275 | * @param $definitions array of IParamDefinition |
||
276 | * |
||
277 | * @return array of IParamDefinition|array |
||
278 | */ |
||
279 | public function getParamDefinitions( array $definitions ) { |
||
280 | $params = parent::getParamDefinitions( $definitions ); |
||
281 | |||
282 | $params[] = array( |
||
283 | // 'type' => 'string', |
||
284 | 'name' => 'views', |
||
285 | 'message' => 'srf-paramdesc-filtered-views', |
||
286 | 'default' => '', |
||
287 | // 'islist' => false, |
||
288 | ); |
||
289 | |||
290 | $params[] = array( |
||
291 | // 'type' => 'string', |
||
292 | 'name' => 'filter position', |
||
293 | 'message' => 'srf-paramdesc-filtered-filter-position', |
||
294 | 'default' => 'top', |
||
295 | // 'islist' => false, |
||
296 | ); |
||
297 | |||
298 | /*print_r($this->mViewTypes); |
||
299 | print_r($params); |
||
300 | print_r(get_declared_classes());*/ |
||
301 | foreach ( $this->mViewTypes as $viewType ) { |
||
302 | $params = array_merge( $params, call_user_func( array( $viewType, 'getParameters' ) ) ); |
||
303 | } |
||
304 | |||
305 | return $params; |
||
306 | } |
||
307 | |||
308 | public function getLinker( $firstcol = false, $force = false ) { |
||
309 | return ( $force ) ? $this->mLinker : parent::getLinker( $firstcol ); |
||
310 | } |
||
311 | |||
312 | } |
||
313 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.