aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/MultiPersonSearch.class.php
blob: af10e6be86980fc5bf6a1744126a89c8733d4637 (plain)
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
<?php
/**
 * MultiPersonSearch.class.php
 *
 * This class provides a GUI-element for searching, adding and removing
 * multiple persons. If JavaScript is enabled the GUI-element is shown
 * as a dialog on the current page. Otherwise the GUI-element is shown
 * on a separate page.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * he License, or (at your option) any later version.
 *
 * @author      Sebastian Hobert <sebastian.hobert@uni-goettingen.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 * @link        http://docs.studip.de/develop/Entwickler/MultiPersonSearch
 */
class MultiPersonSearch {

    private $name;
    private $linkIconPath = "";
    private $linkText = "";
    private $title = "";
    private $description = "";
    private $executeURL;
    private $jsFunction = null;
    private $pageURL = null;
    private $quickfilterIds = [];
    private $defaultSelectableUsersIDs = [];
    private $defaultSelectedUsersIDs = [];
    private $searchObject = null;
    private $additionalHMTL = "";
    private $navigationItem = "";
    private $dataDialogStatus = false;

    /**
     * restores a MultiPersonSearch object.
     *
     * @param string name of the object
     *
     * @return MultiPersonSearch
     */
    public static function load($name)
    {
        $mp = new MultiPersonSearch($name);
        $mp->restoreFromSession();
        return $mp;
    }

    /**
     * returns a MultiPersonSearch object.
     *
     * @param string name of the object
     *
     * @return MultiPersonSearch
     */
    public static function get($name)
    {
        $mp = new MultiPersonSearch($name);
        return $mp;
    }

    /**
     * contsructs a new MultiPersonSearch object.
     *
     * @param string name of the object and html ids
     */
    public function __construct($name)
    {
       $this->name = $name;
       $_SESSION['multipersonsearch'][$this->name]['lastUse'] = time();
       $this->collectGarbage();
       $this->setDefaultValues();

    }

    /**
     * returns the newly added persons. The array will contain all
     * persons which are selected (on the right side of the dialog) but
     * without the defaultSelectedUsers.
     *
     * @return array containing all new persons
     */
    public function getAddedUsers() {
        return $_SESSION['multipersonsearch'][$this->name]['added'] ?? [];
    }

    /**
     * saves the added persons to $_SESSION.
     */
    public function saveAddedUsersToSession() {
        $addedUsers = [];
        foreach (Request::optionArray($this->name . '_selectbox') as $selected) {
            if (!in_array($selected, $_SESSION['multipersonsearch'][$this->name]['defaultSelectedUsersIDs'])) {
                $addedUsers[] = $selected;
            }
        }
        $_SESSION['multipersonsearch'][$this->name]['added'] = $addedUsers;
        $_SESSION['multipersonsearch'][$this->name]['additional'] = Request::optionArray('additional');
    }

    /**
     * returns the removed persons. The array will contain all
     * persons which were selected by default (on the right side of the
     * dialog) and then removed by the user.
     *
     * @return array containing all removed persons
     */
    public function getRemovedUsers() {
        return $_SESSION['multipersonsearch'][$this->name]['removed'];
    }

    /**
     * saves the removed persons to $_SESSION.
     */
    public function saveRemovedUsersToSession() {
        $removedUsers = [];
        foreach ($this->defaultSelectedUsersIDs as $default) {
            if (!in_array($default, Request::optionArray($this->name . '_selectbox'))) {
                $removedUsers[] = $default;
            }
        }
        $_SESSION['multipersonsearch'][$this->name]['removed'] =  $removedUsers;
    }

    /**
     * renders a link to open the multipersonsearch dialog.
     *
     * @param string $with_link_text include link text in output
     */
    public function render($with_link_text = true) {
        $template = $GLOBALS['template_factory']->open('multipersonsearch/link.php');

        $template->set_attribute('linkIconPath', $this->linkIconPath);
        $template->set_attribute('linkText', $with_link_text ? $this->linkText : '');
        $template->set_attribute('title', $this->title);
        $template->set_attribute('name', $this->name);
        $template->set_attribute('description', $this->description);
        $template->set_attribute('executeURL', $this->executeURL);
        $template->set_attribute('jsFunction', $this->jsFunction);
        $this->storeToSession();
        return $template->render();
    }

    /**
     * sets the icon of the link to open the dialog. To hide the icon an
     * empty string can be set.
     *
     * @param string path ot the icon
     *
     * @return MultiPersonSearch
     */
    public function setLinkIconPath($path) {
        $this->linkIconPath = $path;

        return $this;
    }

    /**
     * returns the icon of the link to open the dialog.
     *
     * @return string path ot the icon.
     */
    public function getLinkIconPath() {
        return $this->linkIconPath;
    }

    /**
     * sets the link text of the link to open the dialog. To hide the
     * text an empty string can be set.
     *
     * @param string text of the link
     *
     * @return MultiPersonSearch
     *
     */
    public function setLinkText($text = "") {
        $this->linkText = $text;

        return $this;
    }

    /**
     * returns the link text of the link.
     *
     * @return string text of the link.
     */
    public function getLinkText() {
        return $this->linkText;
    }

    /**
     * sets the action which will handle the added and removed persons after saving the dialog.
     *
     * @param string action
     *
     * @return MultiPersonSearch
     */
    public function setExecuteURL($action) {
        $this->executeURL = $action;

        return $this;
    }

    /**
     * returns the action which will handle the added and removed persons after saving the dialog.
     *
     * @return string action which will handle the form data.
     */
    public function getExecuteURL() {
        return $this->executeURL;
    }

    /**
     * sets a JavaScript-function to be fired when the user has pressed the submit-button.
     * Arguments are:
     * function fireme(id_of_item, text_of_item)
     * example setting: MPS->setJSFunctionOnSubmit('fireme');
     *
     * @param string $function_name the name of the javascript function
     *
     * @return MultiPersonSearch
     */
    public function setJSFunctionOnSubmit($function_name)
    {
        $this->jsFunction = $function_name;
        return $this;
    }

    /**
     * returns a JavaScript-function which should be fired when the user has pressed the submit button.
     *
     * @return string function name
     */
    public function getJSFunctionOnSubmit()
    {
        return $this->jsFunction;
    }

    /**
     * sets the search object.
     *
     * @param SearchType object of type SearchType (e.g. SQLSearch.class.php)
     *
     * @return MultiPersonSearch
     */
    public function setSearchObject($searchType) {
        $this->searchObject = $searchType;

        return $this;
    }

    /**
     * returns the search object.
     *
     * @return SearchType
     */
    public function getSearchObject() {
        return $this->searchObject;
    }

    /**
     * sets html code which will be shown inside the form element.
     *
     * @param string html code
     *
     * @return MultiPersonSearch
     */
    public function setAdditionalHTML($html) {
        $this->additionalHMTL = $html;

        return $this;
    }


    /**
     * enables or disabled data-dialog
     * @param boolean $status
     * @return $this
     */
    public function setDataDialogStatus($status) {
        $this->dataDialogStatus = $status;

        return $this;
    }

    /**
     * returns if data-dialog is enabled or disabled
     * @return bool
     */
    public function getDataDialogStatus() {
        return $this->dataDialogStatus;
    }
    /**
     * returns html code which will be shown inside the form element.
     *
     * @return string html code
     */
    public function getAdditionHTML() {
        return $this->additionalHMTL;
    }

    /**
     * returns an additional option array.
     *
     * @return string html code
     */
    public function getAdditionalOptionArray() {
        return $_SESSION['multipersonsearch'][$this->name]['additional'];
    }

    /**
     * sets the persons which will be shown as selectable by default on
     * the left side of the dialoag.
     *
     * @param array array containing user-ids
     */
    public function setDefaultSelectableUser($userArray) {
        $userArray = array_unique($userArray);
        $this->defaultSelectableUsersIDs = [];
        if (is_array($userArray)) {
            foreach ($userArray as $userId) {
                $this->defaultSelectableUsersIDs[] = $userId;
            }
        }
        return $this;
    }
    /**
     * returns the ids of defaultselectable users.
     *
     * @return array
     */
    public function getDefaultSelectableUsersIDs() {
        return $this->defaultSelectableUsersIDs;
    }

    /**
     * sets the persons which will be shown as selected by default on
     * the right side of the dialoag.
     *
     * @param array array containing user-ids
     */
    public function setDefaultSelectedUser($userArray) {
        $userArray = array_unique($userArray);
        $this->defaultSelectedUsersIDs = [];
        if (is_array($userArray)) {
            foreach ($userArray as $userId) {
                $this->defaultSelectedUsersIDs[]  = $userId;
            }
        }
        return $this;
    }

    /**
     * returns the ids of defaultselected users.
     *
     * @return array
     */
    public function getDefaultSelectedUsersIDs() {
        return $this->defaultSelectedUsersIDs;
    }


    /**
     * sets the title of the dialog.
     *
     * @param string  $title title of the dialog
     *
     * @return MultiPersonSearch
     */
    public function setTitle($title) {
        $this->title = $title;
        return $this;
    }

    /**
     * returns the title.
     *
     * @return string
     */
    public function getTitle() {
        return $this->title;
    }


    /**
     * sets the description of the dialog.
     *
     * @param string  $desc description of the dialog
     *
     * @return MultiPersonSearch
     */
    public function setDescription($desc) {
        $this->description = $desc;
        return $this;
    }

    /**
     * returns the description.
     *
     * @return string
     */
    public function getDescription() {
        return $this->description;
    }

    /**
     * returns the url of the page where the GUI element is added.
     *
     * @return string
     */
    public function getPageUrl() {
        return $this->pageURL;
    }

    /**
     * adds a new quickfilter.
     *
     * @param string $title title of the new quickfilter
     * @param array $userArray containing all user-ids belonging to the quickfilter
     *
     * @return MultiPersonSearch
     */
    public function addQuickfilter($title, $userArray) {
        $users = [];
        $usersIds = [];
        if (is_array($userArray)) {
            foreach ($userArray as $userId) {
                $usersIds[]  = $userId;
            }
        }
        $this->quickfilterIds[(string) $title] = $usersIds;

        return $this;
    }

    /**
     * returns the ids of quickfilters.
     *
     * @return array
     */
    public function getQuickfilterIds()
    {
        return $this->quickfilterIds ?: [];
    }

    /**
     * clears all quickfilters.
     *
     * @return MultiPersonSearch
     */
    public function clearQuickfilters() {
        $this->quickfilterIds = [];

        return $this;
    }

    /**
     * sets the navigation item.
     *
     * @param string  $navigationItem navigation item
     *
     * @return MultiPersonSearch
     */
    public function setNavigationItem($navigationItem) {
        $this->navigationItem = $navigationItem;

        return $this;
    }

    /**
     * returns the navigation item.
     *
     * @return string
     */
    public function getNavigationItem() {
        return $this->navigationItem;
    }

    /**
     * stores the internal data to a session.
     */
    public function storeToSession() {
        $_SESSION['multipersonsearch'][$this->name]['title'] = $this->title;
        $_SESSION['multipersonsearch'][$this->name]['description'] = $this->description;
        $_SESSION['multipersonsearch'][$this->name]['additionalHMTL'] = $this->additionalHMTL;
        $_SESSION['multipersonsearch'][$this->name]['executeURL'] = $this->executeURL;
        $_SESSION['multipersonsearch'][$this->name]['jsFunction'] = $this->jsFunction;
        $_SESSION['multipersonsearch'][$this->name]['pageURL'] = Request::url();
        $_SESSION['multipersonsearch'][$this->name]['defaultSelectableUsersIDs'] = $this->defaultSelectableUsersIDs;
        $_SESSION['multipersonsearch'][$this->name]['defaultSelectedUsersIDs'] = $this->defaultSelectedUsersIDs;
        $_SESSION['multipersonsearch'][$this->name]['quickfilterIds'] = $this->quickfilterIds;
        $_SESSION['multipersonsearch'][$this->name]['searchObject'] = serialize($this->searchObject);
        $_SESSION['multipersonsearch'][$this->name]['navigationItem'] = $this->navigationItem;
        $_SESSION['multipersonsearch'][$this->name]['dataDialogStatus'] = $this->dataDialogStatus;
    }

    /**
     * restores the internal data from a session.
     */
    public function restoreFromSession() {
        if (isset($_SESSION['multipersonsearch'][$this->name])) {
            $this->title = $_SESSION['multipersonsearch'][$this->name]['title'] ?? '';
            $this->description = $_SESSION['multipersonsearch'][$this->name]['description'] ?? '';
            $this->quickfilterIds = $_SESSION['multipersonsearch'][$this->name]['quickfilterIds'] ?? [];
            $this->additionalHMTL = $_SESSION['multipersonsearch'][$this->name]['additionalHMTL'] ?? '';
            $this->executeURL = html_entity_decode($_SESSION['multipersonsearch'][$this->name]['executeURL'] ?? '');
            $this->jsFunction = $_SESSION['multipersonsearch'][$this->name]['jsFunction'] ?? '';
            $this->pageURL = $_SESSION['multipersonsearch'][$this->name]['pageURL'] ?? '';
            $this->defaultSelectableUsersIDs = $_SESSION['multipersonsearch'][$this->name]['defaultSelectableUsersIDs'] ?? [];
            $this->defaultSelectedUsersIDs = $_SESSION['multipersonsearch'][$this->name]['defaultSelectedUsersIDs'] ?? [];
            $this->searchObject = unserialize($_SESSION['multipersonsearch'][$this->name]['searchObject'] ?? null);
            $this->navigationItem = $_SESSION['multipersonsearch'][$this->name]['navigationItem'] ?? null;
            $this->dataDialogStatus = $_SESSION['multipersonsearch'][$this->name]['dataDialogStatus'] ?? '';
        }
    }

    /**
     * clears the session data.
     */
    public function clearSession() {
        unset($_SESSION['multipersonsearch'][$this->name]);
    }

    /**
     * sets default values of the internal variables.
     */
    private function setDefaultValues() {
        $this->title = '';
        $this->description = _('Bitte wählen Sie aus, wen Sie hinzufügen möchten.');
        $this->linkIconPath = Icon::create('add');
    }

    /**
     * clear unused sessions.
     */
    private function collectGarbage() {
        $maxLifeTime = 30; // minutes
        foreach ($_SESSION['multipersonsearch'] as $key=>$value) {
            if (time() - $value['lastUse'] > $maxLifeTime * 60) {
                unset($_SESSION['multipersonsearch'][$key]);
            }
        }
    }

}