1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
|
<?php
/**
* @author Peter Thienel <thienel@data-quest.de>
* @license GPL2 or any later version
* @since 3.5
*/
abstract class MVVController extends AuthenticatedController
{
/**
* The maximum number of items listed on a page.
* If the list is longer pagination is displayed.
*
* @var int
*/
public static $items_per_page;
/**
* Array of ids of mvv object found by search action.
*
* @var array
*/
public $search_result = [];
/**
* Holds the last search term.
*
* @var string
*/
public $search_term = '';
/**
* Holds the last id of an mvv object selected in quick search.
*
* @var string
*/
public $search_id = null;
/**
* TRUE if sidebar is already rendered.
*
* @var bool
*/
protected $sidebar_rendered = false;
/**
* The key of an index name used to store values in the session.
* It is the top level key of an multidimensional array that holds all
* values stored in the session by the current controller.
* One part of the key is the name of the controller.
*
* @var string
*/
protected $session_key;
/**
* The second level key of the array that holds values in the session from
* current controller. It is derived from the name of the current
* action normally.
*
* @var string
*/
public $param_suffix = '';
public function before_filter(&$action, &$args)
{
parent::before_filter($action, $args);
if (!static::IsVisible()) {
throw new AccessDeniedException();
}
PageLayout::setTitle(_('Module'));
// Setup flash instance
$this->flash = Trails_Flash::instance();
$this->me = 'mvv';
self::$items_per_page = Config::get()->getValue('ENTRIES_PER_PAGE');
$this->session_key = $this->me . '_' . mb_substr(get_class($this), 0, -10);
}
/**
* Returns a controller based (considers name of action if given)
* suffix for url parameters.
*
* @param string $action The name of the action (optional).
* @return string Suffix for parameters.
*/
public function paramSuffix($action = '')
{
$param_suffix = mb_strtolower(preg_filter(
['/^.*_/', '/Controller$/'], '', get_called_class(), 1));
return $action ? '_' . $param_suffix . '_' . $action : '_' . $param_suffix;
}
/**
* Initialzes the controller (considers name of action if given) based
* parameters for search and bind them to url.
*
* @param string $action The name of the action (optional).
*/
protected function initSearchParams($action = '')
{
$this->search_params_suffix = $this->paramSuffix($action);
$this->search_term = Request::get('search_term' . $this->search_params_suffix, $this->sessGet('search_term'));
URLHelper::bindLinkParam('search_term' . $this->search_params_suffix, $this->search_term);
$this->search_id = Request::option('search_id' . $this->search_params_suffix, $this->sessGet('search_id'));
URLHelper::bindLinkParam('search_id' . $this->search_params_suffix, $this->search_id);
}
/**
* Initialzes the controller (considers name of action if given) based
* parameters for page navigation and bind them to url.
*
* @param string $action The name of the action (optional).
*/
protected function initPageParams($action = null)
{
$this->page_params_suffix = $this->paramSuffix($action);
$action = $action ? '_' . $action : '';
URLHelper::bindLinkParam('page' . $this->page_params_suffix, $this->page);
$this->page = Request::int(
'page' . $this->page_params_suffix,
$this->sessGet('page' . $this->page_params_suffix)
);
$this->page = intval($this->page) > 1 ? $this->page : 1;
$this->sessSet('page' . $this->page_params_suffix, $this->page);
URLHelper::bindLinkParam('sortby' . $this->page_params_suffix, $this->sortby);
$this->sortby = Request::get(
'sortby' . $this->page_params_suffix,
$this->sessGet('sortby' . $this->page_params_suffix)
);
$this->sessSet('sortby' . $this->page_params_suffix, $this->sortby);
URLHelper::bindLinkParam('order' . $this->page_params_suffix, $this->order);
$this->order = Request::get(
'order' . $this->page_params_suffix,
$this->sessGet('order' . $this->page_params_suffix)
);
$this->sessSet('order' . $this->page_params_suffix, $this->order);
}
/**
* Determines the visibility of this controller.
*
* @return bool True if the controller is visible.
*/
protected static function IsVisible()
{
return MVV::isVisible();
}
/**
* Renders a html snippet with a sort link used in table headers.
*
* @param string $action The action called by this link.
* @param string $text The text of the link.
* @param string $field The sort to sort by.
* @param array $attributes Additional url attributes.
* @return string The html snippet.
*/
public function renderSortLink($action, $text, $field, $attributes = null)
{
$template = $this->get_template_factory()->open('shared/sort_link');
$template->set_attributes([
'controller' => $this,
'action' => $action,
'text' => $text,
'field' => $field,
'attributes' => (array) $attributes
]);
return $template->render();
}
/**
* Sets the sidebar with all widgets and set value of sidebar_rendered
* to true.
*
*/
protected function setSidebar()
{
$this->sidebar_rendered = true;
}
/**
* Renders a html snippet containing an url. This url is used by
* java script.
*
* @param string $to A string containing a controller and optionally
* an action. Default is the current controller.
* @param array $params An array with url parameters.
* @return string The html used in templates
*/
public function jsUrl($to = '', $params = [])
{
if ($to === '') {
$to = str_replace('_', '/', mb_substr(mb_strtolower(get_class($this)),
0, -10)) . '/';
}
$to = $this->url_for($to);
list($url, $query) = explode('?', $to);
$url = URLHelper::getUrl($url, $params, true);
$template = $this->get_template_factory()->open('shared/js_url');
$template->set_attributes(['url' => $url]);
return $template->render();
}
/**
* This action is used to show a select box instead of an input field
* if the user has clicked on the magnifier icon of a quicksearch.
*
* @throws Trails_Exception
*/
public function qs_result_action()
{
if (Request::isPost()) {
$this->render_json(self::getQsResult(
Request::option('qs_id'),
Request::get('qs_term')
));
} else {
throw new Trails_Exception(404);
}
}
/**
* Retrieves the result set of quicksearch to show a select box.
*
* @param string $qs_id The id of the quicksearch.
* @param string $qs_term The search term.
* @return null|array The result set.
*/
private static function getQsResult($qs_id, $qs_term)
{
$search = self::getSearch($qs_id);
if ($search) {
$results[] = [
'id' => '',
'name' => '-- ' . _('Bitte wählen') . ' --',
];
foreach ($search->getResults($qs_term) as $result) {
$results[] = [
'id' => $result[0],
'name' => $result[1]
];
}
return $results;
}
return null;
}
/**
* Retrieves a quick search sql object from session by its id
* (md5 of serialized object).
*
* @param string $qs_id The quick search id of the search object.
* @return object A search object.
*/
private static function getSearch($qs_id)
{
$search = null;
if ($qs_id) {
try {
$search = unserialize($_SESSION['QuickSearches'][$qs_id]['object']);
} catch (Exception $e) {
return null;
}
}
return is_object($search) ? $search : null;
}
/**
* Perform the search for mvv objects of type defined by $class_name.
* Uses the findBySearchTerm method with its parameters $search_term and
* $filter. If $search_id is given, only this item will be found.
* Sets info messages with number of hits to page layout.
*
* @see ModuleManagementModel::findBySearchTerm()
* @param string $class_name The name of an mvv object class.
* @param string $search_term The search term.
* @param string $search_id The id of an mvv object selected in quicksearch.
* @param array $filter An array with filter options feeded to search
* function to restrict search result.
*/
protected function do_search($class_name, $search_term = null, $search_id = null, $filter = null)
{
if (!count($this->search_result)) {
$search_id = $search_id ?: $this->search_id;
if ($search_id) {
$found_object = $class_name::find($search_id);
if ($found_object) {
$this->search_result = [$found_object->getId()];
$this->search_term = $found_object->getDisplayName();
if (!$this->search_id) {
PageLayout::postInfo(sprintf(
_('"%s" ausgewählt.'),
htmlReady($found_object->getDisplayName())
));
}
$this->search_id = $search_id;
$this->sessSet('search_id', $this->search_id);
}
} else {
$search_term = $search_term ?: $this->search_term;
$filter = $filter ?: $this->filter;
if ($search_term) {
$this->search_result =
$class_name::findBySearchTerm($search_term, $filter)->pluck('id');
if ($this->current_action === 'search') {
if (count($this->search_result)) {
PageLayout::postInfo(sprintf(
_('%s Treffer für die Suche nach "%s".'),
count($this->search_result),
htmlReady($search_term)
));
$this->search_term = $search_term;
} else {
PageLayout::postInfo(sprintf(
_('Keine Treffer für die Suche nach "%s".'),
htmlReady($search_term)
));
}
unset($this->search_id);
$this->sessRemove('search_id');
}
}
}
}
$this->sessSet('search_term', $this->search_term);
}
/**
* Returns the current search result of the given class. The search result
* is an array of object ids.
*
* @param string The class name of the found objects.
* @return array Array of search results.
*/
protected function getSearchResult($class_name)
{
$this->do_search($class_name);
return $this->search_result;
}
/**
* Deletes the search results stored in $this->search_result for the
* given action.
*
* @param string $action The name of the action that uses the
* particular search.
*/
protected function reset_search($action = '')
{
$this->search_params_suffix = $this->paramSuffix($action);
// reset search
$this->search_result = [];
unset($this->search_term);
URLHelper::removeLinkParam('search_term' . $this->search_params_suffix);
$this->sessRemove('search_term');
unset($this->search_id);
URLHelper::removeLinkParam('search_id' . $this->search_params_suffix);
$this->sessRemove('search_id');
}
/**
* Resets the main page parameters for pagination and sorting for the given
* action.
*
* @param string $action The name of the action that uses the pagination and
* sorting.
*/
protected function reset_page($action = '')
{
$this->page_params_suffix = $this->paramSuffix($action);
// reset page chooser
$this->page = 1;
$this->sessRemove('page' . $this->page_params_suffix);
URLHelper::removeLinkParam('page' . $this->page_params_suffix);
// reset sorting
$this->sortby = '';
$this->sessRemove('sortby' . $this->page_params_suffix);
URLHelper::removeLinkParam('sortby' . $this->page_params_suffix);
$this->order = 'DESC';
$this->sessRemove('order' . $this->page_params_suffix);
URLHelper::removeLinkParam('order' . $this->page_params_suffix);
}
/**
* Stores a value with the given key in the session.
*
* @param string $key The key of the value.
* @param mixed $value The value to store under the given key.
* @return the stored value
*/
protected function sessSet($key, $value)
{
$_SESSION[$this->session_key][$key] = $value;
return $value;
}
/**
* Returns the value of the given key from the session.
*
* @param string $key The key of the value to return.
* @return mixed The value from session with the given key.
*/
protected function sessGet($key, $default = null)
{
return (isset($_SESSION[$this->session_key][$key])
? $_SESSION[$this->session_key][$key]
: $default);
}
/**
* Removes the value with the given key from the session.
*
* @param string|array $keys The key of the value to remove from session.
*/
protected function sessRemove($keys)
{
if (!is_array($keys)) {
$keys = [$keys];
}
foreach ($keys as $key) {
unset($_SESSION[$this->session_key][$key]);
}
}
/**
* Deletes all values from the session used in this controller.
*
*/
protected function sessDelete()
{
unset($_SESSION[$this->session_key]);
}
/**
* This weird WYSIWIG-Editor stores an empty string as an empty diff-element.
* Use this function to check whether the field has no content
* (no input by the user).
*
* @param string $text The text from db to check.
* @return string An empty string, if the content is an empty diff
* or an empty string
*/
public static function trim($text)
{
$text = trim($text);
return preg_match('%\<div.*?\>\</div\>%', $text) ? '' : $text;
}
}
|