Person.php 11 KB
Newer Older
Dainis Abols's avatar
Dainis Abols committed
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
<?php

namespace Lu\LuContacts\Models;

use Lu\LuApi\DataSources\LuisContacts;
use Lu\LuContacts\Helpers\DataHelper;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;

/**
 * Class Person
 *
 * @author  Dainis Abols <dainis.abols@lu.lv>
 * @owner   University of Latvia
 * @since   29.06.2018
 *
 * @package Lu\LuContacts\Models
 */
class Person
{
    /**
     * Pre-set uri builder
     *
     * @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
     */
    private $uriBuilder;

    /**
     * Settings
     *
     * @var array
     */
    private $settings;

    /**
     * Person ID
     *
     * @var
     */
    private $personId;

    /**
     * Unit ID
     *
     * @var string
     */
    private $unit_id;

    /**
     * Alphabet constructor.
     *
     * @param null $settings
     */
    public function __construct($settings = null)
    {
        $this->settings = $settings;
    }

    /**
     * Sets uri builder
     *
     * @param \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder $uriBuilder
     */
    public function setUriBuilder(UriBuilder $uriBuilder)
    {
        $this->uriBuilder = $uriBuilder;
    }

    /**
     * Set unit id
     *
     * @param $unit_id
     */
    public function setUnitParentId($unit_id)
    {
        $this->unit_id = $unit_id;
    }

    /**
     * Filter persons array for list view
     *
     * @param $solr_persons
     *
     * @return array
     */
    public function filterPersonList($solr_persons)
    {
        // Pre-define variables and models
        $newPersons = [];
        $sortOrder  = [];

        // Loop and create new persons array
        foreach ($solr_persons as $item) {
            $tmp = [
                'id'       => $item['id'],
                'fullname' => $item['name'].' '.$item['surname'],
                'email'    => $item['email'],
                'unit'     => [],
            ];

            // Add unit info
            if ($item['units']['numFound']) {
                foreach ($item['units']['docs'] as $entry) {
                    $tmp['unitNames'][$entry['id']] = $entry['name'];
                }

                // Use this check, to fetch person id sort
                $sortOrder = $item['units']['docs'][0]['person_ids'];
            }

            // Add position and phone info
            if ($item['_childDocuments_']) {
Dainis Abols's avatar
Dainis Abols committed
112
                $k = 0;
Dainis Abols's avatar
Dainis Abols committed
113
114
115
116
117
118
119
120
121
                foreach ($item['_childDocuments_'] as $children) {
                    $k++;

                    // Add positions
                    if ($children['type'] == 'position') {
                        $tmp['position'][$k] = $children['name'];
                    }

                    if ($children['unit_ids']) {
Dainis Abols's avatar
Dainis Abols committed
122
                        foreach ($children['unit_ids'] as $keyUnitId) {
Dainis Abols's avatar
Dainis Abols committed
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
                            if (key_exists($keyUnitId, $tmp['unitNames']) && !key_exists($keyUnitId, $tmp['unit'])) {
                                $tmp['unit'][$k][] = $tmp['unitNames'][$keyUnitId];
                            }
                        }
                    }
                    $tmp['unit'][$k] = $this->implode($tmp['unit'][$k]);

                    // Add phone numbers
                    if ($children['type'] == 'workplace') {
                        $tmp['phone'][$k] = $this->implode($children['phone']);
                    }
                }
            }
            $tmp['unit'] = $this->implode($tmp['unit']);

            // If uri builder provided, add urls
            if ($this->uriBuilder) {
                $arguments  = [
                    'tx_lucontacts_pi1[action]'   => 'person',
                    'tx_lucontacts_pi1[personId]' => $item['id'],
                ];
                $tmp['url'] = $this->uriBuilder->setArguments($arguments)->buildFrontendUri();
            }

            // Flatten arrays
            $tmp['position'] = $this->implode($tmp['position']);
            $tmp['phone']    = $this->implode($tmp['phone']);

            // Assign with person ID
            $newPersons[$item['id']] = $tmp;
        }

        // Resort by unit person_ids field when unit ID is present
        if ($this->unit_id && $sortOrder) {
            $newPersons = $this->resort($sortOrder, $newPersons);
        }

        return array_values($newPersons);
    }

    /**
     * Implode an array
     *
     * @param        $value
     * @param string $param
     *
     * @return string
     */
    private function implode($value, $param = "<br/>")
    {
        if (is_array($value)) {
            return implode($param, $value);
        } else {
            return $value;
        }
    }

    /**
     * Resort array
     *
     * @param array $sortOrder
     * @param array $newPersons
     *
     * @return array
     */
    private function resort(array $sortOrder, array $newPersons): array
    {
        // Resort by sortOrder array
        $newPersons = array_merge(array_flip($sortOrder), $newPersons);

        // Remove values, that are not anm array
        foreach ($newPersons as $key => $item) {
            if (!is_array($item)) {
                unset($newPersons[$key]);
            }
        }

        // Return new and clean array
        return $newPersons;
    }

    /**
     * Set person ID
     *
     * @param string $personId
     */
    public function setPersonId(string $personId)
    {
        $this->personId = $personId;
    }

    /**
     * Fetch and return user data
     *
     * @return array
     */
    public function getFullData()
    {
        // Pre-set models and values
        $addresses    = [];
        $luisContacts = new LuisContacts();
        $DataHelper   = new DataHelper();

        // Set request
        $jsonData = $luisContacts->getPersonDetails($this->personId);
        $details  = $DataHelper->APIDecode($jsonData);

        // Return if no data received
        if (empty($details['data'])) {
            return [];
        } else {
            $details = reset($details['data']);
        }

        // Set workplace
        $workplaces = [];
        foreach ($details['units']['docs'] as $unit) {
            $fullName = [$unit['name']];
            if (!empty($unit['parent_id'])) {
                $UnitModel = new Unit();
                $fullName  = array_reverse($UnitModel->getRecursiveUnitNames($unit['id']));
            }

            $workplaces[$unit['id']] = [
                'id'       => $unit['id'],
                'name'     => $fullName,
                'position' => '',
            ];
        }

        // Add child document info
        foreach ($details['_childDocuments_'] as $child) {
            switch ($child['type']) {
                case "position":
                    foreach ($child['unit_ids'] as $unitId) {
                        if (empty($workplaces[$unitId]['position'])) {
                            $workplaces[$unitId]['position'] = [];
                        }
                        $workplaces[$unitId]['position'][] = $child['name'];
                    }
                    break;
                case "workplace":
                    $addresses[$child['id']] = [
                        'address' => $child['address'],
                        'room'    => $child['room'],
                        'phone'   => $child['phone'] ? implode(",", $child['phone']) : '',
                    ];
                    break;
            }
        }

        // Reverse workplace position results
        $workplaces[$unitId]['position'] = array_reverse($workplaces[$unitId]['position']);

        // Build the user array
        $user = [
            'name'       => $details['name'].' '.$details['surname'],
            'workplaces' => array_values($workplaces),
            'address'    => array_values($addresses),
            'email'      => $details['email'],
            'html'       => $details['html'],
        ];

        // Add away time
        if (!empty($details['absence_from']) || !empty($details['absence_to'])) {
            $awayTime = [];
            if ($details['absence_from']) {
                $tmpDate = new \DateTime($details['absence_from']);
                $tmpDate->setTimezone(new \DateTimeZone(date_default_timezone_get()));
                $awayTime[] = $tmpDate->format('d.m.Y');
            }
            if ($details['absence_to']) {
                $tmpDate = new \DateTime($details['absence_to']);
                $tmpDate->setTimezone(new \DateTimeZone(date_default_timezone_get()));
                $awayTime[] = $tmpDate->format('d.m.Y');
            }
            if (!empty($awayTime)) {
                $user['away_time'] = implode(" - ", $awayTime);
            }
        }

        // Add consultation
        if (!empty($details['consultation'])) {
            $user['consultation'] = $details['consultation'];
        }

        // Add photo
        if (!empty($details['photo'])) {
            $user['photo'] = $this->setUserPhoto(($details['photo']));
        }

        // Return result
        return [
            'section' => 'person',
            'title'   => 'SOME PERSON NAME',
            'person'  => $user,
            'details' => $details,
        ];
    }

    /**
     * Check if person is under required departeament or structure
     *
     * @param      $personId
     * @param      $unitId
     * @param bool $recursive
     *
     * @return bool
     */
    public function validatePerson($personId, $unitId, $recursive = false): bool
    {
        // Pre-set models and values
        $addresses    = [];
        $luisContacts = new LuisContacts();
        $DataHelper   = new DataHelper();

        // Set request
        $jsonData = $luisContacts->getPersonDetails($personId);
        $details  = $DataHelper->APIDecode($jsonData);

        // Return if no data received
        if (empty($details['data'])) {
            return false;
        } else {
            $details = reset($details['data']);
        }

        // Fetch unit IDs foe person
Dainis Abols's avatar
Dainis Abols committed
351
        foreach ($details['unit_ids'] as $userUnitId) {
Dainis Abols's avatar
Dainis Abols committed
352
            if ($recursive) {
Dainis Abols's avatar
Dainis Abols committed
353
354
                $UnitModel   = new Unit();
                $userUnits[] = array_reverse($UnitModel->getRecursiveUnitIds($userUnitId));
Dainis Abols's avatar
Dainis Abols committed
355
356
357
358
359
            } else {
                $userUnits[] = $userUnitId;
            }
        }

Dainis Abols's avatar
Dainis Abols committed
360
361
362
363
364
        // Flatten into onel evel array
        if (!empty($userUnits)) {
            $userUnits = $this->array_flatten($userUnits);
        }

Dainis Abols's avatar
Dainis Abols committed
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
        // Return result
        return in_array($unitId, $userUnits);
    }

    /**
     * Return output image url
     *
     * @param $image
     *
     * @return string
     */
    private function setUserPhoto($image)
    {
        return '<img src="'.$image.'" />';
    }
Dainis Abols's avatar
Dainis Abols committed
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

    /**
     * Flattens array.
     *
     * @param array $array
     *
     * @return array
     */
    private function array_flatten(array $array)
    {
        $flat  = []; // initialize return array
        $stack = array_values($array); // initialize stack
        while ($stack) // process stack until done
        {
            $value = array_shift($stack);
            if (is_array($value)) // a value to further process
            {
                array_unshift($stack, ...$value);
            } else // a value to take
            {
                $flat[] = $value;
            }
        }

        return $flat;
    }
}