SummarySheetController.php 57.8 KB
Newer Older
Damien's avatar
Damien committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php

/**
* Copyright Maarch since 2008 under licence GPLv3.
* See LICENCE.txt file at the root folder for more details.
* This file is part of Maarch software.
*
*/

/**
* @brief Summary Sheet Controller
* @author dev@maarch.org
*/

namespace Resource\controllers;

17
use Attachment\models\AttachmentModel;
18
use Contact\controllers\ContactController;
19
use CustomField\models\CustomFieldModel;
20
use Docserver\models\DocserverModel;
Damien's avatar
Damien committed
21
use Endroid\QrCode\QrCode;
Damien's avatar
Damien committed
22
23
use Entity\models\EntityModel;
use Entity\models\ListInstanceModel;
24
use ExternalSignatoryBook\controllers\MaarchParapheurController;
25
use Group\controllers\PrivilegeController;
26
use History\models\HistoryModel;
27
use IndexingModel\models\IndexingModelFieldModel;
Damien's avatar
Damien committed
28
29
use Note\models\NoteEntityModel;
use Note\models\NoteModel;
30
use Parameter\models\ParameterModel;
Damien's avatar
Damien committed
31
32
use Priority\models\PriorityModel;
use Resource\models\ResModel;
Damien's avatar
Damien committed
33
use Respect\Validation\Validator;
Damien's avatar
Damien committed
34
35
36
37
38
39
40
41
42
43
44
use setasign\Fpdi\Tcpdf\Fpdi;
use Slim\Http\Request;
use Slim\Http\Response;
use SrcCore\models\CoreConfigModel;
use SrcCore\models\TextFormatModel;
use SrcCore\models\ValidatorModel;
use Status\models\StatusModel;
use User\models\UserModel;

class SummarySheetController
{
45
    public function createList(Request $request, Response $response)
Damien's avatar
Damien committed
46
    {
47
        set_time_limit(240);
Damien's avatar
Damien committed
48

Damien's avatar
Damien committed
49
        $bodyData = $request->getParsedBody();
Damien's avatar
Damien committed
50

Damien's avatar
Damien committed
51
        if (!Validator::arrayType()->notEmpty()->validate($bodyData['resources'])) {
52
            return $response->withStatus(403)->withJson(['errors' => 'Resources is not set or empty']);
53
        } elseif (!ResController::hasRightByResId(['resId' => $bodyData['resources'], 'userId' => $GLOBALS['id']])) {
54
55
            return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
        }
Damien's avatar
Damien committed
56

57
58
        $units = empty($bodyData['units']) ? [] : $bodyData['units'];
        $bodyData['resources'] = array_slice($bodyData['resources'], 0, 500);
59

60
        $order = '';
Damien's avatar
Damien committed
61
        foreach ($bodyData['resources'] as $key => $resId) {
62
            $order .= "WHEN {$resId} THEN {$key} ";
Damien's avatar
Damien committed
63
        }
64
        $order .= 'END';
Damien's avatar
Damien committed
65

66
67
68
        $orderTable = 'CASE res_id ' . $order;
        $resourcesByModelIds = ResModel::get([
            'select'  => ["string_agg(cast(res_id as text), ',' order by {$orderTable}) as res_ids", 'model_id'],
69
70
71
            'where'   => ['res_id in (?)'],
            'data'    => [$bodyData['resources']],
            'groupBy' => ['model_id']
Damien's avatar
Damien committed
72
73
74
75
76
        ]);

        $pdf = new Fpdi('P', 'pt');
        $pdf->setPrintHeader(false);

77
78
        $order = 'CASE res_view_letterbox.res_id ' . $order;

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
        foreach ($resourcesByModelIds as $resourcesByModelId) {
            $resourcesIdsByModel = $resourcesByModelId['res_ids'];
            $resourcesIdsByModel = explode(',', $resourcesIdsByModel);

            $indexingFields   = IndexingModelFieldModel::get([
                'select' => ['identifier', 'unit'],
                'where'  => ['model_id = ?'],
                'data'   => [$resourcesByModelId['model_id']]
            ]);
            $fieldsIdentifier = array_column($indexingFields, 'identifier');

            $select = ['res_id', 'subject', 'alt_identifier'];
            foreach ($units as $unit) {
                $unit = (array)$unit;
                if ($unit['unit'] == 'primaryInformations') {
                    $information = [
                        'documentDate' => 'doc_date',
                        'arrivalDate'  => 'admission_date',
                        'initiator'    => 'initiator'
                    ];
99
                    $select = array_merge($select, ['type_label', 'creation_date', 'typist']);
100
101
102
103
104
105
106
107
108
109
110

                    foreach ($information as $key => $item) {
                        if (in_array($key, $fieldsIdentifier)) {
                            $select[] = $item;
                        }
                    }
                } elseif ($unit['unit'] == 'secondaryInformations') {
                    $information = [
                        'priority'         => 'priority',
                        'processLimitDate' => 'process_limit_date',
                    ];
111
                    $select = array_merge($select, ['category_id', 'status', 'retention_frozen', 'binding']);
112
113
114
115
116
117

                    foreach ($information as $key => $item) {
                        if (in_array($key, $fieldsIdentifier)) {
                            $select[] = $item;
                        }
                    }
118
119
                } elseif ($unit['unit'] == 'systemTechnicalFields') {
                    $select = array_merge($select, ['format', 'fingerprint', 'filesize', 'creation_date', 'filename', 'docserver_id', 'path', 'typist']);
120
121
122
123
124
125
                } elseif ($unit['unit'] == 'diffusionList') {
                    if (in_array('destination', $fieldsIdentifier)) {
                        $select[] = 'destination';
                    }
                }
            }
126

127
128
129
130
131
132
            $resources = ResModel::getOnView([
                'select'  => $select,
                'where'   => ['res_view_letterbox.res_id in (?)'],
                'data'    => [$resourcesIdsByModel],
                'orderBy' => [$order]
            ]);
133

134
135
136
137
138
139
            $resourcesIds = array_column($resources, 'res_id');

            // Data for resources
            $data = SummarySheetController::prepareData(['units' => $units, 'resourcesIds' => $resourcesIds]);

            foreach ($resources as $resource) {
140
141
                SummarySheetController::createSummarySheet($pdf, [
                    'resource'         => $resource, 'units' => $units,
142
                    'login'            => $GLOBALS['login'],
143
144
145
                    'data'             => $data,
                    'fieldsIdentifier' => $fieldsIdentifier
                ]);
146
            }
147
148
        }

149
150
151
        $fileContent = $pdf->Output('', 'S');
        $finfo    = new \finfo(FILEINFO_MIME_TYPE);
        $mimeType = $finfo->buffer($fileContent);
152

153
154
155
156
157
        $queryParams = $request->getQueryParams();
        if (!empty($queryParams['mode']) && $queryParams['mode'] == 'base64') {
            return $response->withJson(['encodedDocument' => base64_encode($fileContent), 'mimeType' => $mimeType]);
        }

158
159
160
161
162
163
        $response->write($fileContent);
        $response = $response->withAddedHeader('Content-Disposition', "inline; filename=maarch.pdf");

        return $response->withHeader('Content-Type', $mimeType);
    }

164
    public static function createSummarySheet(Fpdi $pdf, array $args)
Damien's avatar
Damien committed
165
    {
Damien's avatar
Damien committed
166
        ValidatorModel::notEmpty($args, ['resource', 'login']);
167
        ValidatorModel::arrayType($args, ['resource', 'units', 'data', 'fieldsIdentifier']);
Damien's avatar
Damien committed
168
169
        ValidatorModel::stringType($args, ['login']);

170
171
172
173
        $resource         = $args['resource'];
        $units            = $args['units'];
        $fieldsIdentifier = $args['fieldsIdentifier'];

Damien's avatar
Damien committed
174
175

        $pdf->AddPage();
176
        $dimensions     = $pdf->getPageDimensions();
Damien's avatar
Damien committed
177
        $widthNoMargins = $dimensions['w'] - $dimensions['rm'] - $dimensions['lm'];
178
179
180
181
182
183
184
        $bottomHeight   = $dimensions['h'] - $dimensions['bm'];

        $widthMultiCell = $widthNoMargins / 10 * 4.5;
        $widthCell      = $widthNoMargins / 10;
        $widthNotes     = $widthNoMargins / 2;
        $specialWidth   = $widthNoMargins / 4;
        $widthAssignee  = $widthNoMargins / 6;
Damien's avatar
Damien committed
185
186

        $appName = CoreConfigModel::getApplicationName();
187
        $pdf->SetFont('', '', 8);
188
        $pdf->Cell(0, 20, mb_strimwidth($appName, 0, 40, '...', 'utf8') . " / " . date('d-m-Y'), 0, 2, 'L', false);
189
190
        $pdf->SetY($pdf->GetY() - 20);

Damien's avatar
Damien committed
191
192
        $pdf->SetFont('', 'B', 12);
        $pdf->Cell(0, 20, _SUMMARY_SHEET, 0, 2, 'C', false);
193

Damien's avatar
Damien committed
194
195
        $pdf->SetFont('', '', 8);
        $pdf->Cell(0, 1, $resource['alt_identifier'], 0, 2, 'C', false);
Damien's avatar
Damien committed
196

197
198
        $subject = str_replace("\n", ' ', $resource['subject']);

199
200
201
202
        $hasQrcode = in_array('qrcode', array_column($units, 'unit'));
        if ($hasQrcode) {
            $pdf->SetY($pdf->GetY() + 30);
        }
Damien's avatar
Damien committed
203
204
        $pdf->SetY($pdf->GetY() + 15);
        $pdf->SetFont('', 'B', 16);
205
        $pdf->MultiCell(0, 1, $subject, 1, 'C', false);
Damien's avatar
Damien committed
206

207
208
209
210
        if ($hasQrcode) {
            $pdf->SetY($pdf->GetY() - 20);
        }

211
212
        foreach ($units as $key => $unit) {
            $units[$key] = (array)$unit;
213
            $unit        = (array)$unit;
Damien's avatar
Damien committed
214
            if ($unit['unit'] == 'qrcode') {
215
216
217
                $parameter = ParameterModel::getById(['select' => ['param_value_int'], 'id' => 'QrCodePrefix']);
                $prefix = '';
                if ($parameter['param_value_int'] == 1) {
218
                    $prefix = 'MAARCH_';
219
220
                }
                $qrCode = new QrCode($prefix . $resource['res_id']);
Damien's avatar
Damien committed
221
                $qrCode->setSize(400);
222
                $qrCode->setMargin(25);
223
                $pdf->Image('@'.$qrCode->writeString(), 485, 10, 90, 90);
Damien's avatar
Damien committed
224
225
            }
        }
226
        foreach ($units as $key => $unit) {
Damien's avatar
Damien committed
227
            if ($unit['unit'] == 'primaryInformations') {
228
229
230
231
232
233
                $admissionDate = null;
                if (in_array('arrivalDate', $fieldsIdentifier)) {
                    $admissionDate = TextFormatModel::formatDate($resource['admission_date'], 'd-m-Y');
                    $admissionDate = empty($admissionDate) ? '<i>' . _UNDEFINED . '</i>' : "<b>{$admissionDate}</b>";
                }

234
235
                $creationdate  = TextFormatModel::formatDate($resource['creation_date'], 'd-m-Y');
                $creationdate  = empty($creationdate) ? '<i>'._UNDEFINED.'</i>' : "<b>{$creationdate}</b>";
236
237
238
239
240
241
242

                $docDate = null;
                if (in_array('documentDate', $fieldsIdentifier)) {
                    $docDate = TextFormatModel::formatDate($resource['doc_date'], 'd-m-Y');
                    $docDate = empty($docDate) ? '<i>' . _UNDEFINED . '</i>' : "<b>{$docDate}</b>";
                }

243
244
245
                if (!empty($resource['initiator'])) {
                    $initiator = EntityModel::getByEntityId(['entityId' => $resource['initiator'], 'select' => ['short_label']]);
                }
Damien's avatar
Damien committed
246
                $initiatorEntity = empty($initiator) ? '' : "({$initiator['short_label']})";
247

Damien's avatar
Damien committed
248
                $typist          = UserModel::getLabelledUserById(['id' => $resource['typist']]);
249
                $doctype         = empty($resource['type_label']) ? '<i>'._UNDEFINED.'</i>' : "<b>{$resource['type_label']}</b>";
Damien's avatar
Damien committed
250
251
252
253
254

                $pdf->SetY($pdf->GetY() + 40);
                if (($pdf->GetY() + 77) > $bottomHeight) {
                    $pdf->AddPage();
                }
255
256
257
258
259
                if (!($key == 0 || ($key == 1 && $units[0]['unit'] == 'qrcode'))) {
                    $pdf->SetFont('', 'B', 11);
                    $pdf->Cell(0, 15, $unit['label'], 0, 2, 'L', false);
                    $pdf->SetY($pdf->GetY() + 2);
                }
Damien's avatar
Damien committed
260
261

                $pdf->SetFont('', '', 10);
262

263
264
                $pdf->MultiCell($widthMultiCell, 15, _CREATED . " : {$creationdate}", 0, 'L', false, 0, '', '', true, 0, true);

265
266
267
268
269
270
271
272
273
274
275
                if (isset($docDate)) {
                    $pdf->Cell($widthCell, 15, '', 0, 0, 'L', false);
                    $pdf->MultiCell($widthMultiCell, 15, _DOC_DATE . " : {$docDate}", 0, 'L', false, 1, '', '', true, 0, true);
                } else {
                    $pdf->Cell($widthCell, 15, '', 0, 1, 'L', false);
                }

                if (isset($admissionDate)) {
                    $pdf->MultiCell($widthMultiCell, 15, _ADMISSION_DATE . " : {$admissionDate}", 0, 'L', false, 1, '', '', true, 0, true);
                }

276
277
                $pdf->MultiCell($widthMultiCell * 2, 15, _TYPIST . " : <b>{$typist} {$initiatorEntity}</b>", 0, 'L', false, 1, '', '', true, 0, true);

278
                $pdf->MultiCell($widthMultiCell * 2, 15, _DOCTYPE . " : {$doctype}", 0, 'L', false, 0, '', '', true, 0, true);
279
                $pdf->Cell($widthCell, 15, '', 0, 0, 'L', false);
Damien's avatar
Damien committed
280
281
            } elseif ($unit['unit'] == 'secondaryInformations') {
                $category = ResModel::getCategoryLabel(['categoryId' => $resource['category_id']]);
282
                $category = empty($category) ? '<i>'._UNDEFINED.'</i>' : "<b>{$category}</b>";
283

284
285
286
                if (!empty($resource['status'])) {
                    $status = StatusModel::getById(['id' => $resource['status'], 'select' => ['label_status']]);
                }
287
288
                $status = empty($status['label_status']) ? '<i>' . _UNDEFINED . '</i>' : "<b>{$status['label_status']}</b>";

289
290
291
292
293
294
295
296
                $retentionRuleFrozen = empty($resource['retention_frozen']) ? '<b>' . _NO . '</b>' : '<b>' . _YES . '</b>';

                if (!isset($resource['binding'])) {
                    $binding = '<i>' . _UNDEFINED . '</i>';
                } else {
                    $binding = empty($resource['binding']) ? '<b>' . _NO . '</b>' : '<b>' . _YES . '</b>';
                }

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
                $priority = null;
                if (in_array('priority', $fieldsIdentifier)) {
                    $priority = '';
                    if (!empty($resource['priority'])) {
                        $priority = PriorityModel::getById(['id' => $resource['priority'], 'select' => ['label']]);
                    }
                    $priority = empty($priority['label']) ? '<i>' . _UNDEFINED . '</i>' : "<b>{$priority['label']}</b>";
                }

                $processLimitDate = null;
                if (in_array('processLimitDate', $fieldsIdentifier)) {
                    $processLimitDate = TextFormatModel::formatDate($resource['process_limit_date'], 'd-m-Y');
                    $processLimitDate = empty($processLimitDate) ? '<i>' . _UNDEFINED . '</i>' : "<b>{$processLimitDate}</b>";
                }

                // Custom fields
                $customFieldsValues = ResModel::get([
                    'select' => ['custom_fields'],
                    'where' => ['res_id = ?'],
                    'data' => [$resource['res_id']]
                ]);
                // Get all the ids of the custom fields in the model
                $customFieldsIds = [];
                foreach ($fieldsIdentifier as $item) {
                    if (strpos($item, 'indexingCustomField_') !== false) {
                        $customFieldsIds[] = explode('_', $item)[1];
                    }
                }

                if (!empty($customFieldsIds)) {
                    // get the label of the custom fields
                    $customFields = CustomFieldModel::get([
329
                        'select' => ['id', 'label', 'values', 'type'],
330
331
332
333
                        'where'  => ['id in (?)'],
                        'data'   => [$customFieldsIds]
                    ]);

334
                    $customFieldsRawValues = array_column($customFields, 'values', 'id');
335
                    $customFieldsRawTypes = array_column($customFields, 'type', 'id');
336
                    $customFields = array_column($customFields, 'label', 'id');
337
338
339

                    $customFieldsValues = $customFieldsValues[0]['custom_fields'] ?? null;
                    $customFieldsValues = json_decode($customFieldsValues, true);
Damien's avatar
Damien committed
340
341
342
                }

                $pdf->SetY($pdf->GetY() + 40);
Damien's avatar
Damien committed
343
                if (($pdf->GetY() + 57) > $bottomHeight) {
Damien's avatar
Damien committed
344
345
346
347
348
349
350
                    $pdf->AddPage();
                }
                $pdf->SetFont('', 'B', 11);
                $pdf->Cell(0, 15, $unit['label'], 0, 2, 'L', false);
                $pdf->SetY($pdf->GetY() + 2);

                $pdf->SetFont('', '', 10);
351
                $pdf->MultiCell($widthNotes, 30, _CATEGORY . " : {$category}", 1, 'L', false, 0, '', '', true, 0, true);
352

353
                $pdf->MultiCell($widthNotes, 30, _STATUS . " : {$status}", 1, 'L', false, 1, '', '', true, 0, true);
354

355
356
357
                $pdf->MultiCell($widthNotes, 30, _RETENTION_RULE_FROZEN . " : {$retentionRuleFrozen}", 1, 'L', false, 0, '', '', true, 0, true);
                $pdf->MultiCell($widthNotes, 30, _BINDING_DOCUMENT . " : {$binding}", 1, 'L', false, 1, '', '', true, 0, true);

358
359
360
361
362
363
364
                $nextLine = 1;
                if (isset($priority)) {
                    $nextLine = isset($processLimitDate) || !empty($customFieldsIds) ? 0 : 1;
                    $pdf->MultiCell($widthNotes, 30, _PRIORITY . " : {$priority}", 1, 'L', false, $nextLine, '', '', true, 0, true);
                }
                if (isset($processLimitDate)) {
                    $nextLine = !empty($customFieldsIds) && $nextLine == 0 ? 1 : 0;
365
                    $pdf->MultiCell($widthNotes, 30, _PROCESS_LIMIT_DATE . " : {$processLimitDate}", 1, 'L', false, $nextLine, '', '', true, 0, true);
366
367
368
                }

                if (!empty($customFieldsIds)) {
369
370
371
                    $fieldsType = CustomFieldModel::get(['select' => ['type', 'id'], 'where' => ['id in (?)'], 'data' => [$customFieldsIds]]);
                    $fieldsType = array_column($fieldsType, 'type', 'id');

372
373
                    foreach ($customFieldsIds as $customFieldsId) {
                        $label = $customFields[$customFieldsId];
374
                        $rawValues = json_decode($customFieldsRawValues[$customFieldsId], true);
375
                        if (!empty($rawValues['table']) && in_array($customFieldsRawTypes[$customFieldsId], ['radio', 'select', 'checkbox'])) {
376
377
378
                            if (!empty($resource['res_id'])) {
                                $rawValues['resId'] = $resource['res_id'];
                            }
379
380
381
382
383
384
385
386
387
388
389
                            $rawValues = CustomFieldModel::getValuesSQL($rawValues);

                            $rawValues = array_column($rawValues, 'label', 'key');
                            if (is_array($customFieldsValues[$customFieldsId])) {
                                foreach ($customFieldsValues[$customFieldsId] as $key => $value) {
                                    $customFieldsValues[$customFieldsId][$key] = $rawValues[$value];
                                }
                            } else {
                                $customFieldsValues[$customFieldsId] = $rawValues[$customFieldsValues[$customFieldsId]];
                            }
                        }
390
                        if (is_array($customFieldsValues[$customFieldsId])) {
391
                            $customValue = "";
392
                            if (!empty($customFieldsValues[$customFieldsId])) {
393
                                if ($fieldsType[$customFieldsId] == 'banAutocomplete') {
394
                                    $customValue = "{$customFieldsValues[$customFieldsId][0]['addressNumber']} {$customFieldsValues[$customFieldsId][0]['addressStreet']} {$customFieldsValues[$customFieldsId][0]['addressTown']} ({$customFieldsValues[$customFieldsId][0]['addressPostcode']})";
395
396
397
                                    if (!empty($customFieldsValues[$customFieldsId][0]['sector'])) {
                                        $customValue .= " - {$customFieldsValues[$customFieldsId][0]['sector']}";
                                    }
398
399
400
                                } elseif ($fieldsType[$customFieldsId] == 'contact') {
                                    $customValues = ContactController::getContactCustomField(['contacts' => $customFieldsValues[$customFieldsId]]);
                                    $customValue = count($customValues) > 2 ? count($customValues) . ' ' . _CONTACTS : implode(", ", $customValues);
401
402
403
                                    if (count($customValues) < 3) {
                                        $pdf->SetFont('', '', 8);
                                    }
404
405
406
407
408
                                } else {
                                    $customValue = implode(',', $customFieldsValues[$customFieldsId]);
                                }
                            }
                            $value = !empty($customValue) ? '<b>' . $customValue . '</b>' : '<i>' . _UNDEFINED . '</i>';
409
                        } else {
410
                            $value = $customFieldsValues[$customFieldsId] ? '<b>' . $customFieldsValues[$customFieldsId] . '</b>' : '<i>' . _UNDEFINED . '</i>';
411
412
                        }

413
414
                        $nextLine = ($nextLine + 1) % 2;
                        $pdf->MultiCell($widthNotes*2, 30, $label . " : {$value}", 1, 'L', false, 1, '', '', true, 0, true, true);
415
                        $pdf->SetFont('', '', 10);
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
            } elseif ($unit['unit'] == 'systemTechnicalFields') {
                if (PrivilegeController::hasPrivilege(['privilegeId' => 'view_technical_infos', 'userId' => $GLOBALS['id']])) {
                    if (!empty($resource['docserver_id'])) {
                        $docserver = DocserverModel::getByDocserverId(['docserverId' => $resource['docserver_id'], 'select' => ['path_template']]);
                        $docserverPathFile = $docserver['path_template'] . $resource['path'];
                        $docserverPathFile = str_replace('//', '/', $docserverPathFile);
                        $docserverPathFile = str_replace('#', '/', $docserverPathFile);
                    }
            
                    $typistLabel  = UserModel::getLabelledUserById(['id' => $resource['typist']]);
                    $fulltextInfo = ResModel::getById(['select' => ['fulltext_result'], 'resId' => $resource['res_id']]);

                    $pdf->SetY($pdf->GetY() + 40);
                    if (($pdf->GetY() + 57) > $bottomHeight) {
                        $pdf->AddPage();
                    }
                    $pdf->SetFont('', 'B', 11);
                    $pdf->Cell(0, 15, $unit['label'], 0, 2, 'L', false);
                    $pdf->SetY($pdf->GetY() + 2);
    
                    $pdf->SetFont('', '', 10);
                    $pdf->MultiCell($widthNotes, 30, _TYPIST . " : {$typistLabel}", 1, 'L', false, 0, '', '', true, 0, true);

                    $creationDate = TextFormatModel::formatDate($resource['creation_date'], 'd-m-Y');
                    $pdf->MultiCell($widthNotes, 30, _CREATION_DATE . " : {$creationDate}", 1, 'L', false, 1, '', '', true, 0, true);
    
                    $nextLine = 1;
                    if (!empty($resource['filesize'])) {
                        $resource['filesize'] = StoreController::getFormattedSizeFromBytes(['size' => $resource['filesize']]);
                        $nextLine = ($nextLine + 1) % 2;
                        $pdf->MultiCell($widthNotes, 30, _SIZE . " : {$resource['filesize']}", 1, 'L', false, $nextLine, '', '', true, 0, true);
                    }
                    if (!empty($resource['format'])) {
                        $resource['format'] = strtoupper($resource['format']);
                        $nextLine = ($nextLine + 1) % 2;
                        $pdf->MultiCell($widthNotes, 30, _FORMAT . " : {$resource['format']}", 1, 'L', false, $nextLine, '', '', true, 0, true);
                    }
                    if (!empty($resource['filename'])) {
                        $nextLine = ($nextLine + 1) % 2;
                        $pdf->MultiCell($widthNotes, 30, _FILENAME . " : {$resource['filename']}", 1, 'L', false, $nextLine, '', '', true, 0, true);
                    }
                    if (!empty($docserverPathFile)) {
                        $nextLine = ($nextLine + 1) % 2;
                        $pdf->MultiCell($widthNotes, 30, _DOCSERVER_PATH_FILE . " : {$docserverPathFile}", 1, 'L', false, $nextLine, '', '', true, 0, true);
                    }
                    if (!empty($resource['fingerprint'])) {
                        $pdf->SetFont('', '', 8);
                        $nextLine = ($nextLine + 1) % 2;
                        $pdf->MultiCell($widthNotes, 30, _FINGERPRINT . " : {$resource['fingerprint']}", 1, 'L', false, $nextLine, '', '', true, 0, true);
                        $pdf->SetFont('', '', 10);
                    }
                    if (!empty($fulltextInfo['fulltext_result'])) {
                        $nextLine = ($nextLine + 1) % 2;
                        $fulltextResult = $fulltextInfo['fulltext_result'] == 'SUCCESS' ? _SUCCESS : _ERROR;
                        $pdf->MultiCell($widthNotes, 30, _FULLTEXT . " : {$fulltextResult}", 1, 'L', false, $nextLine, '', '', true, 0, true);
                    }
                }
            } elseif ($unit['unit'] == 'customTechnicalFields') {
                if (PrivilegeController::hasPrivilege(['privilegeId' => 'view_technical_infos', 'userId' => $GLOBALS['id']])) {
                    $customFieldsValues = ResModel::get([
                        'select' => ['custom_fields'],
                        'where'  => ['res_id = ?'],
                        'data'   => [$resource['res_id']]
                    ]);
                    // Get all the ids of technical custom fields
                    $customFields    = CustomFieldModel::get(['where' => ['mode = ?'], 'data' => ['technical'], 'orderBy' => ['label']]);
                    $customFieldsIds = array_column($customFields, 'id');
    
                    if (!empty($customFieldsIds)) {
                        $customFieldsRawValues = array_column($customFields, 'values', 'id');
                        $customFieldsRawTypes = array_column($customFields, 'type', 'id');
                        $customFields = array_column($customFields, 'label', 'id');
    
                        $customFieldsValues = $customFieldsValues[0]['custom_fields'] ?? null;
                        $customFieldsValues = json_decode($customFieldsValues, true);
                    }
    
                    $pdf->SetY($pdf->GetY() + 40);
                    if (($pdf->GetY() + 57) > $bottomHeight) {
                        $pdf->AddPage();
                    }
                    $pdf->SetFont('', 'B', 11);
                    $pdf->Cell(0, 15, $unit['label'], 0, 2, 'L', false);
                    $pdf->SetY($pdf->GetY() + 2);
                    $pdf->SetFont('', '', 10);
    
                    $nextLine = 1;
                    if (!empty($customFieldsIds)) {
                        $fieldsType = CustomFieldModel::get(['select' => ['type', 'id'], 'where' => ['id in (?)'], 'data' => [$customFieldsIds]]);
                        $fieldsType = array_column($fieldsType, 'type', 'id');
    
                        foreach ($customFieldsIds as $customFieldsId) {
                            $label = $customFields[$customFieldsId];
                            $rawValues = json_decode($customFieldsRawValues[$customFieldsId], true);
                            if (!empty($rawValues['table']) && in_array($customFieldsRawTypes[$customFieldsId], ['radio', 'select', 'checkbox'])) {
                                if (!empty($resource['res_id'])) {
                                    $rawValues['resId'] = $resource['res_id'];
                                }
                                $rawValues = CustomFieldModel::getValuesSQL($rawValues);
    
                                $rawValues = array_column($rawValues, 'label', 'key');
                                if (is_array($customFieldsValues[$customFieldsId])) {
                                    foreach ($customFieldsValues[$customFieldsId] as $key => $value) {
                                        $customFieldsValues[$customFieldsId][$key] = $rawValues[$value];
                                    }
                                } else {
                                    $customFieldsValues[$customFieldsId] = $rawValues[$customFieldsValues[$customFieldsId]];
                                }
                            }
                            if (is_array($customFieldsValues[$customFieldsId])) {
                                $customValue = "";
                                if (!empty($customFieldsValues[$customFieldsId])) {
                                    if ($fieldsType[$customFieldsId] == 'banAutocomplete') {
                                        $customValue = "{$customFieldsValues[$customFieldsId][0]['addressNumber']} {$customFieldsValues[$customFieldsId][0]['addressStreet']} {$customFieldsValues[$customFieldsId][0]['addressTown']} ({$customFieldsValues[$customFieldsId][0]['addressPostcode']})";
532
533
534
                                        if (!empty($customFieldsValues[$customFieldsId][0]['sector'])) {
                                            $customValue .= " - {$customFieldsValues[$customFieldsId][0]['sector']}";
                                        }
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
                                    } elseif ($fieldsType[$customFieldsId] == 'contact') {
                                        $customValues = ContactController::getContactCustomField(['contacts' => $customFieldsValues[$customFieldsId]]);
                                        $customValue = count($customValues) > 2 ? count($customValues) . ' ' . _CONTACTS : implode(", ", $customValues);
                                        if (count($customValues) < 3) {
                                            $pdf->SetFont('', '', 8);
                                        }
                                    } else {
                                        $customValue = implode(',', $customFieldsValues[$customFieldsId]);
                                    }
                                }
                                $value = !empty($customValue) ? '<b>' . $customValue . '</b>' : '<i>' . _UNDEFINED . '</i>';
                            } else {
                                $value = $customFieldsValues[$customFieldsId] ? '<b>' . $customFieldsValues[$customFieldsId] . '</b>' : '<i>' . _UNDEFINED . '</i>';
                            }
    
                            $nextLine = ($nextLine + 1) % 2;
                            $pdf->MultiCell($widthNotes, 30, $label . " : {$value}", 1, 'L', false, $nextLine, '', '', true, 0, true);
                            $pdf->SetFont('', '', 10);
                        }
                    }
                }
556
            } elseif ($unit['unit'] == 'senderRecipientInformations') {
557
558
559
560
561
562
563
564
565
566
                $senders = null;
                if (in_array('senders', $fieldsIdentifier)) {
                    $senders = ContactController::getFormattedContacts([
                        'resId' => $resource['res_id'],
                        'mode'  => 'sender'
                    ]);
                    if (!empty($senders) && count($senders) > 2) {
                        $nbSenders = count($senders);
                        $senders = [];
                        $senders[0] = $nbSenders . ' ' . _CONTACTS;
567
568
                    } elseif (empty($senders)) {
                        $senders = [''];
569
570
                    }
                }
571

572
573
574
575
576
577
578
579
580
581
                $recipients = null;
                if (in_array('recipients', $fieldsIdentifier)) {
                    $recipients = ContactController::getFormattedContacts([
                        'resId' => $resource['res_id'],
                        'mode'  => 'recipient'
                    ]);
                    if (!empty($recipients) && count($recipients) > 2) {
                        $nbRecipients = count($recipients);
                        $recipients = [];
                        $recipients[0] = $nbRecipients . ' ' . _CONTACTS;
582
583
                    } elseif (empty($recipients)) {
                        $recipients = [''];
584
                    }
585
                }
586
587
588
589

                // If senders and recipients are both null, they are not part of the model so we continue to the next unit
                if ($senders === null && $recipients === null) {
                    continue;
590
                }
591

592
593
594
595
596
597
598
                $pdf->SetY($pdf->GetY() + 40);
                if (($pdf->GetY() + 57) > $bottomHeight) {
                    $pdf->AddPage();
                }
                $pdf->SetFont('', 'B', 11);
                $pdf->Cell(0, 15, $unit['label'], 0, 2, 'L', false);
                $pdf->SetY($pdf->GetY() + 2);
599

600
                $pdf->SetFont('', '', 10);
601
602
603
604
605
606
607
608
609

                $correspondents = [];
                if ($senders !== null && $recipients !== null) {
                    if (empty($senders[0]) && empty($recipients[0])) {
                        $correspondents = [null, null];
                    } else {
                        for ($i = 0; !empty($senders[$i]) || !empty($recipients[$i]); $i++) {
                            $correspondents[] = $senders[$i] ?? null;
                            $correspondents[] = $recipients[$i] ?? null;
610
                        }
611
                    }
612

613
614
615
                    $pdf->Cell($widthMultiCell, 15, _SENDERS, 1, 0, 'C', false);
                    $pdf->Cell($widthCell, 15, '', 0, 0, 'C', false);
                    $pdf->Cell($widthMultiCell, 15, _RECIPIENTS, 1, 1, 'C', false);
Florian Azizian's avatar
Florian Azizian committed
616
                } elseif ($senders !== null && $recipients === null) {
617
                    $correspondents = $senders;
618

619
                    $pdf->Cell($widthMultiCell, 15, _SENDERS, 1, 1, 'C', false);
Florian Azizian's avatar
Florian Azizian committed
620
                } elseif ($senders === null && $recipients !== null) {
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
                    $correspondents = $recipients;

                    $pdf->Cell($widthMultiCell, 15, _RECIPIENTS, 1, 1, 'C', false);
                }

                // allow to skip an element in the senders or recipients column if we already printed UNDEFINED once
                $columnUndefined = [false, false];
                $nextLine = 1;
                foreach ($correspondents as $correspondent) {
                    // if senders and recipients are not null, nextLine alternate between 0 and 1, otherwise its always 1
                    if ($senders !== null && $recipients !== null) {
                        $nextLine = ($nextLine + 1) % 2;

                        if ($columnUndefined[$nextLine]) {
                            $pdf->MultiCell($widthMultiCell, 40, '', 0, 'L', false, 0, '', '', true, 0, true);
                            $pdf->MultiCell($widthCell, 40, '', 0, 'L', false, $nextLine, '', '', true, 0, true);
                            continue;
638
                        }
639
640
641
642
643
644
645
646
647
648
649
650
651
                    } else {
                        $nextLine = 1;
                    }

                    if (empty($correspondent)) {
                        $columnUndefined[$nextLine] = true;
                        $pdf->MultiCell($widthMultiCell, 40, _UNDEFINED, 1, 'L', false, $nextLine, '', '', true, 0, true);
                    } else {
                        $pdf->MultiCell($widthMultiCell, 40, empty($correspondent) ? '' : $correspondent, empty($correspondent) ? 0 : 1, 'L', false, $nextLine, '', '', true, 0, true);
                    }

                    if ($nextLine == 0) {
                        $pdf->MultiCell($widthCell, 40, '', 0, 'L', false, 0, '', '', true, 0, true);
652
653
                    }
                }
Damien's avatar
Damien committed
654
            } elseif ($unit['unit'] == 'diffusionList') {
655
656
657
                $assignee    = '';
                $destination = '';
                $found       = false;
658
659
660
                $roles       = EntityModel::getRoles();
                $rolesItems  = [];
                $nbItems     = 0;
Damien's avatar
Damien committed
661
                foreach ($args['data']['listInstances'] as $listKey => $listInstance) {
662
663
664
665
666
                    if ($found && $listInstance['res_id'] != $resource['res_id']) {
                        break;
                    } elseif ($listInstance['res_id'] == $resource['res_id']) {
                        $item = '';
                        if ($listInstance['item_type'] == 'user_id') {
667
                            $user   = UserModel::getById(['id' => $listInstance['item_id'], 'select' => ['id', 'firstname', 'lastname']]);
668
                            $entity = UserModel::getPrimaryEntityById(['id' => $user['id'], 'select' => ['entities.entity_label']]);
669
670

                            if ($listInstance['item_mode'] == 'dest') {
671
                                $item = $user['firstname'] . ' ' . $user['lastname'];
672
                            } else {
673
                                $item = "{$user['firstname']} {$user['lastname']} ({$entity['entity_label']})";
674
                            }
675
                        } elseif ($listInstance['item_type'] == 'entity_id') {
676
677
                            $item   = $listInstance['item_id'];
                            $entity = EntityModel::getById(['id' => $listInstance['item_id'], 'select' => ['short_label', 'entity_id']]);
678
                            if (!empty($entity)) {
679
                                $item = "{$entity['short_label']} ({$entity['entity_id']})";
680
                            }
Damien's avatar
Damien committed
681
                        }
682
683
684
                        if ($listInstance['item_mode'] == 'dest') {
                            $assignee = $item;
                        } else {
685
686
687
688
689
690
691
692
                            foreach ($roles as $role) {
                                if ($listInstance['item_mode'] == $role['id'] || ($listInstance['item_mode'] == 'cc' && $role['id'] == 'copy')) {
                                    $rolesItems[$role['id']]['item'][] = $item;
                                    $rolesItems[$role['id']]['label'] = $role['label'];
                                    $nbItems++;
                                    continue;
                                }
                            }
693
                        }
Damien's avatar
Damien committed
694
                        unset($args['data']['listInstances'][$listKey]);
695
                        $found = true;
Damien's avatar
Damien committed
696
697
                    }
                }
698
699
700

                // Sort keys to be in the same order defined in the roles.xml file
                $rolesIDs = array_column($roles, 'id');
701
                $tmp      = [];
702
703
704
705
706
707
708
                foreach ($rolesIDs as $key) {
                    if (!empty($rolesItems[$key])) {
                        $tmp[$key] = $rolesItems[$key];
                    }
                }
                $rolesItems = $tmp;

709
710
711
                if (!empty($resource['destination'])) {
                    $destination = EntityModel::getByEntityId(['entityId' => $resource['destination'], 'select' => ['short_label']]);
                }
Damien's avatar
Damien committed
712
                $destinationEntity = empty($destination) ? '' : "({$destination['short_label']})";
Damien's avatar
Damien committed
713

714
715
716
                if (empty($assignee)) {
                    $assignee = _UNDEFINED;
                }
Damien's avatar
Damien committed
717
                $pdf->SetY($pdf->GetY() + 40);
718
                if (($pdf->GetY() + 37 + $nbItems * 20) > $bottomHeight) {
Damien's avatar
Damien committed
719
720
721
722
723
724
725
                    $pdf->AddPage();
                }
                $pdf->SetFont('', 'B', 11);
                $pdf->Cell(0, 15, $unit['label'], 0, 2, 'L', false);
                $pdf->SetY($pdf->GetY() + 2);

                $pdf->SetFont('', '', 10);
726
                $pdf->MultiCell($widthAssignee, 20, _ASSIGNEE, 1, 'C', false, 0, '', '', true, 0, false, true, 20, 'M');
727
                $pdf->SetFont('', 'B', 10);
728
                $pdf->Cell($widthAssignee * 5, 20, "- {$assignee} {$destinationEntity}", 1, 1, 'L', false);
729
730

                foreach ($rolesItems as $rolesItem) {
731
                    $pdf->SetFont('', '', 10);
732
733
734
735
736
737
738
739
                    $pdf->MultiCell($widthAssignee, count($rolesItem['item']) * 20, $rolesItem['label'], 1, 'C', false, 0, '', '', true, 0, false, true, count($rolesItem['item']) * 20, 'M');

                    $nbItems = count($rolesItem['item']);
                    $i = 0;
                    foreach ($rolesItem['item'] as $item) {
                        $nextLine = $i == ($nbItems - 1) ? 1 : 2;
                        $pdf->Cell($widthAssignee * 5, 20, "- {$item}", 1, $nextLine, 'L', false);
                        $i++;
Damien's avatar
Damien committed
740
741
742
743
                    }
                }
            } elseif ($unit['unit'] == 'visaWorkflow') {
                $users = [];
744
                $found = false;
745

Damien's avatar
Damien committed
746
                foreach ($args['data']['listInstancesVisa'] as $listKey => $listInstance) {
747
748
749
                    if ($found && $listInstance['res_id'] != $resource['res_id']) {
                        break;
                    } elseif ($listInstance['res_id'] == $resource['res_id']) {
750
                        if (!empty($listInstance['process_date']) && $listInstance['process_comment'] != _INTERRUPTED_WORKFLOW) {
751
752
753
754
                            $mode = ($listInstance['signatory'] ? _SIGNATORY : _VISA_USER_MIN);
                        } else {
                            $mode = ($listInstance['requested_signature'] ? _SIGNATORY : _VISA_USER_MIN);
                        }
755
                        $userLabel = UserModel::getLabelledUserById(['id' => $listInstance['item_id']]);
756
757
758

                        $delegate = !empty($listInstance['delegate']) ? UserModel::getLabelledUserById(['id' => $listInstance['delegate']]) : '';
                        if (!empty($delegate)) {
759
                            $mode .= ', ' . _INSTEAD_OF . ' ' . $userLabel;
760
                            $userLabel = $delegate . " ({$mode})";
761
                        } else {
762
763
764
765
766
767
768
769
770
                            $userLabel .= " ({$mode})";
                        }

                        if (!empty($listInstance['process_date'])) {
                            if (empty($listInstance['process_comment'])) {
                                $userLabel .= ',' .  ($listInstance['signatory'] ? _SIGNED : _VALIDATED);
                            } else {
                                $userLabel .= ', ' . $listInstance['process_comment'];
                            }
771
772
                        }

773
                        $users[] = [
774
                            'user'  => $userLabel,
775
776
                            'date'  => TextFormatModel::formatDate($listInstance['process_date']),
                        ];
Damien's avatar
Damien committed
777
                        unset($args['data']['listInstancesVisa'][$listKey]);
778
779
                        $found = true;
                    }
Damien's avatar
Damien committed
780
781
782
783
784
785
786
787
788
789
790
791
                }

                if (!empty($users)) {
                    $pdf->SetY($pdf->GetY() + 40);
                    if (($pdf->GetY() + 37 + count($users) * 20) > $bottomHeight) {
                        $pdf->AddPage();
                    }
                    $pdf->SetFont('', 'B', 11);
                    $pdf->Cell(0, 15, $unit['label'], 0, 2, 'L', false);
                    $pdf->SetY($pdf->GetY() + 2);

                    $pdf->SetFont('', '', 10);
792
793
                    $pdf->Cell($specialWidth * 3, 20, _USERS, 1, 0, 'L', false);
                    $pdf->Cell($specialWidth, 20, _ACTION_DATE, 1, 1, 'L', false);
794
                    foreach ($users as $keyUser => $user) {
795
                        $pdf->MultiCell($specialWidth * 3, 20, $keyUser + 1 . ". {$user['user']}", 1, 'L', false, 0, '', '', true, 0, false, true, 20, 'M', true);
796
                        $pdf->Cell($specialWidth, 20, $user['date'], 1, 1, 'L', false);
797
798
799
800
                    }
                }
            } elseif ($unit['unit'] == 'opinionWorkflow') {
                $users = [];
801
                $found = false;
Damien's avatar
Damien committed
802
                foreach ($args['data']['listInstancesOpinion'] as $listKey => $listInstance) {
803
804
805
                    if ($found && $listInstance['res_id'] != $resource['res_id']) {
                        break;
                    } elseif ($listInstance['res_id'] == $resource['res_id']) {
806
807
808
                        $user = UserModel::getLabelledUserById(['id' => $listInstance['item_id']]);
                        $entity = UserModel::getPrimaryEntityById(['id' => $listInstance['item_id'], 'select' => ['entities.entity_label']]);

809
810
                        $entityLabel = $entity['entity_label'];
                        $userLabel = $user;
811
812
813
                        $delegate = !empty($listInstance['delegate']) ? UserModel::getLabelledUserById(['id' => $listInstance['delegate']]) : '';

                        if (!empty($delegate)) {
814
815
816
817
                            $entityLabel .= ', ' .  _INSTEAD_OF . ' ' . $userLabel;
                            $userLabel = $delegate . " (" . $entityLabel . ")";
                        } else {
                            $userLabel .= " (" . $entityLabel . ")";
818
                        }
819

820
                        $users[] = [
821
                            'user'  => $userLabel,
822
                            'date'  => TextFormatModel::formatDate($listInstance['process_date'])
823
                        ];
Damien's avatar
Damien committed
824
                        unset($args['data']['listInstancesOpinion'][$listKey]);
825
826
                        $found = true;
                    }
827
828
829
830
831
832
833
834
835
836
837
838
                }

                if (!empty($users)) {
                    $pdf->SetY($pdf->GetY() + 40);
                    if (($pdf->GetY() + 37 + count($users) * 20) > $bottomHeight) {
                        $pdf->AddPage();
                    }
                    $pdf->SetFont('', 'B', 11);
                    $pdf->Cell(0, 15, $unit['label'], 0, 2, 'L', false);
                    $pdf->SetY($pdf->GetY() + 2);

                    $pdf->SetFont('', '', 10);
839
840
                    $pdf->Cell($specialWidth * 3, 20, _USERS, 1, 0, 'L', false);
                    $pdf->Cell($specialWidth, 20, _ACTION_DATE, 1, 1, 'L', false);
841
                    foreach ($users as $keyUser => $user) {
842
843
                        $pdf->Cell($specialWidth * 3, 20, $keyUser + 1 . ". {$user['user']}", 1, 0, 'L', false);
                        $pdf->Cell($specialWidth, 20, $user['date'], 1, 1, 'L', false);
Damien's avatar
Damien committed
844
845
846
847
                    }
                }
            } elseif ($unit['unit'] == 'notes') {
                $notes = [];
848
                $found = false;
849
                $user = UserModel::getByLogin(['select' => ['id'], 'login' => $args['login']]);
Damien's avatar
Damien committed
850
                foreach ($args['data']['notes'] as $noteKey => $rawNote) {
851
852
853
854
                    if ($found && $rawNote['identifier'] != $resource['res_id']) {
                        break;
                    } elseif ($rawNote['identifier'] == $resource['res_id']) {
                        $allowed = false;
855
                        if ($rawNote['user_id'] == $user['id']) {
856
857
858
859
860
861
862
863
864
                            $allowed = true;
                        } else {
                            $noteEntities = NoteEntityModel::get(['select' => ['item_id'], 'where' => ['note_id = ?'], 'data' => [$rawNote['id']]]);
                            if (!empty($noteEntities)) {
                                foreach ($noteEntities as $noteEntity) {
                                    if (in_array($noteEntity['item_id'], $args['data']['userEntities'])) {
                                        $allowed = true;
                                        break;
                                    }
Damien's avatar
Damien committed
865
                                }
866
867
                            } else {
                                $allowed = true;
Damien's avatar
Damien committed
868
869
                            }
                        }
870
871
                        if ($allowed) {
                            $notes[] = [
872
                                'user'  => UserModel::getLabelledUserById(['id' => $rawNote['user_id']]),
Damien's avatar
Damien committed
873
                                'date'  => TextFormatModel::formatDate($rawNote['creation_date']),
874
                                'note'  => str_replace('←', '<=', $rawNote['note_text'])
875
876
                            ];
                        }
Damien's avatar
Damien committed
877
                        unset($args['data']['notes'][$noteKey]);
878
                        $found = true;
Damien's avatar
Damien committed
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
                    }
                }

                if (!empty($notes)) {
                    $pdf->SetY($pdf->GetY() + 40);
                    if (($pdf->GetY() + 80) > $bottomHeight) {
                        $pdf->AddPage();
                    }

                    $pdf->SetFont('', 'B', 11);
                    $pdf->Cell(0, 15, $unit['label'], 0, 2, 'L', false);

                    $pdf->SetY($pdf->GetY() + 2);
                    $pdf->SetFont('', '', 10);

                    foreach ($notes as $note) {
                        if (($pdf->GetY() + 65) > $bottomHeight) {
                            $pdf->AddPage();
                        }
898
                        $pdf->SetFont('', 'B', 10);
899
                        $pdf->Cell($widthNotes, 20, $note['user'], 1, 0, 'L', false);
900
                        $pdf->SetFont('', '', 10);
901
                        $pdf->Cell($widthNotes, 20, $note['date'], 1, 1, 'L', false);
Damien's avatar
Damien committed
902
903
904
905
                        $pdf->MultiCell(0, 40, $note['note'], 1, 'L', false);
                        $pdf->SetY($pdf->GetY() + 5);
                    }
                }
906
            } elseif ($unit['unit'] == 'freeField') {
Damien's avatar
Damien committed
907
908
909
910
911
912
913
914
915
                $pdf->SetY($pdf->GetY() + 40);
                if (($pdf->GetY() + 77) > $bottomHeight) {
                    $pdf->AddPage();
                }
                $pdf->SetFont('', 'B', 11);
                $pdf->Cell(0, 15, $unit['label'], 0, 2, 'L', false);

                $pdf->SetY($pdf->GetY() + 2);
                $pdf->Cell(0, 60, '', 1, 2, 'L', false);
916
            } elseif ($unit['unit'] == 'trafficRecords') {
917
918
919
920
                $pdf->SetY($pdf->GetY() + 30);

                $parameter = ParameterModel::getById(['select' => ['param_value_string'], 'id' => 'traffic_record_summary_sheet']);

921
922
                $pdf2 = new Fpdi('P', 'pt');
                $pdf2->setPrintHeader(false);
923
924
925
926
                $pdf2->AddPage();
                $pdf2->writeHTMLCell($widthNoMargins + $dimensions['lm'], 0, $widthNoMargins + $dimensions['lm'], 0, $parameter['param_value_string'], 0, 1, 0, true, 'C', true);
                $height = 10 - ($pdf2->GetY());
                if (($pdf->GetY() + abs($height)) > $bottomHeight) {
927
928
                    $pdf->AddPage();
                }
929
                unset($pdf2);
930

931
932
933
934
                $pdf->SetFont('', 'B', 11);
                $pdf->Cell(0, 15, $unit['label'], 0, 2, 'L', false);
                $pdf->SetFont('', '', 9);

935
                $pdf->writeHTMLCell($widthNoMargins + $dimensions['lm'], 0, $dimensions['lm'] - 2, $pdf->GetY(), $parameter['param_value_string']);
936
                $pdf->SetY($pdf->GetY() + abs($height));
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
            } elseif ($unit['unit'] == 'visaWorkflowMaarchParapheur') {
                $loadedXml = CoreConfigModel::getXmlLoaded(['path' => 'modules/visa/xml/remoteSignatoryBooks.xml']);
                if (empty($loadedXml)) {
                    continue;
                }

                $config = ['data' => []];
                foreach ($loadedXml->signatoryBook as $value) {
                    if ($value->id == "maarchParapheur") {
                        $config['data']['url'] = rtrim($value->url, '/');
                        $config['data']['userId'] = (string)$value->userId;
                        $config['data']['password'] = (string)$value->password;
                        break;
                    }
                }

953
954
955
956
                if (empty($config['data'])) {
                    continue;
                }

957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
                $mainDocument = ResModel::getById([
                    'resId' => $resource['res_id'],
                    'select' => ["external_id->>'signatureBookId' as external_id", 'alt_identifier', 'subject']
                ]);

                $documents = [];
                if (!empty($mainDocument['external_id'])) {
                    $documents[] = [
                        'id'    => $mainDocument['external_id'],
                        'label' => _MAIN_DOCUMENT . ' - ' . $mainDocument['alt_identifier'] . ' - ' . $mainDocument['subject']
                    ];
                }

                $attachments = AttachmentModel::get([
                    'select' => ['res_id', "external_id->>'signatureBookId' as external_id", 'title', 'identifier'],
                    'where'  => ["external_id->>'signatureBookId' IS NOT NULL", "external_id->>'signatureBookId' != ''", 'res_id_master = ?'],
                    'data'   => [$resource['res_id']]
                ]);

                foreach ($attachments as $attachment) {
                    $documents[] = [
                        'id'    => $attachment['external_id'],
                        'label' => _ATTACHMENT . ' - ' . $attachment['identifier'] . ' - ' . $attachment['title']
                    ];
                }

983
984
985
986
                if (empty($documents)) {
                    continue;
                }

987
988
989
990
991
992
993
994
995
996
997
998
                $pdf->SetY($pdf->GetY() + 40);
                if (($pdf->GetY() + 37) > $bottomHeight) {
                    $pdf->AddPage();
                }
                $pdf->SetFont('', 'B', 11);
                $pdf->Cell(0, 15, $unit['label'], 0, 2, 'L', false);
                $pdf->SetY($pdf->GetY() + 2);

                foreach ($documents as $document) {
                    $workflow = MaarchParapheurController::getDocumentWorkflow(['config' => $config, 'documentId' => $document['id']]);

                    $users = [];
999
1000
                    if (!empty($workflow)) {
                        foreach ($workflow as $item) {
For faster browsing, not all history is shown. View entire blame