Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
79.45% covered (warning)
79.45%
58 / 73
n/a
0 / 0
CRAP
n/a
0 / 0
1<?php
2
3/**
4 * @file
5 * Place action: load product details by objids (productnfo).
6 */
7
8declare(strict_types=1);
9
10require_once __DIR__.'/../../Legacy/common/session.php';
11require_once __DIR__.'/../../Legacy/model/synchloader.php';
12
13$datamodel = json_decode('{
14    "data": {
15        "PRODUCT": [
16            { "direction": "<", "target": "TICKETPRODUCT", "by": "product", "reason": "requested", "sql": "and TICKETPRODUCT.status = \'AC\'" }
17        ],
18        "TICKETPRODUCT": [
19            { "direction": ">", "target": "TICKET", "by": "ticket", "reason": null },
20            { "direction": ">", "target": "PRODUCT", "by": "product", "reason": null },
21            { "direction": "<", "target": "TICKETOPTION", "by": "product", "reason": "options" }
22        ],
23        "TICKETOPTION": [
24            { "direction": ">", "target": "TICKETPRODUCT", "by": "product", "reason": null },
25            { "direction": ">", "target": "PRODUCTOPT", "by": "prodopt", "reason": null }
26        ],
27        "TICKETOFFER": [
28            { "direction": ">", "target": "OFFER", "by": "offer", "reason": null }
29        ],
30        "TICKETEXTRA": [
31            { "direction": ">", "target": "EXTRA", "by": "extra", "reason": null }
32        ]
33    }
34}', true);
35
36/**
37 * Action callable: load product details by objids (POST body).
38 *
39 * @param string $body JSON array of product objids.
40 * @param array<string, mixed> $query Query params: device, session, place, dateini, dateend.
41 * @param \ConxHelper $conx Connection helper.
42 * @param \Psr\Log\LoggerInterface $logger Logger.
43 * @param \UppServices\SessionService $sessionService Session service for check.
44 * @return array{output: string, contentType: string}
45 */
46return function (string $body, array $query, \ConxHelper $conx, \Psr\Log\LoggerInterface $logger, \UppServices\SessionService $sessionService): array {
47    $retval = \ApplicationError::Success;
48    $updates = [];
49
50    if (!$body) {
51        $logger->error("No post data provided.");
52        $retval = \ApplicationError::Parameters;
53    }
54
55    if (\success($retval)) {
56        $products = json_decode($body, true);
57        $logger->debug("DUMP OF POST DATA:".PHP_EOL.print_r($products, true).PHP_EOL);
58        if ($products === false) {
59            $logger->error("Invalid JSON format in post request data");
60            $retval = \ApplicationError::Parameters;
61        } else {
62            $_objids = implode(', ', array_map('intval', $products));
63        }
64    }
65
66    if (\success($retval)) {
67        $device = $query['device'] ?? null;
68        if (!$device) {
69            $logger->error("No device provided on URL. Operation cancelled.");
70            $retval = \ApplicationError::Parameters;
71        }
72    }
73
74    if (\success($retval)) {
75        $session = $query['session'] ?? null;
76        if (!$session) {
77            $logger->error("No session provided on URL. Operation cancelled.");
78            $retval = \ApplicationError::Parameters;
79        } else {
80            $userid = null;
81            $sessionObjid = null;
82            $retval = $sessionService->checkSession($conx, (string) $session, (string) $device, $userid, $sessionObjid);
83        }
84    }
85
86    if (\success($retval)) {
87        $place = $query['place'] ?? null;
88        if ($place === null || $place === '') {
89            $logger->error("Mandatory argument 'place' not provided in url parameters.");
90            $retval = \ApplicationError::Parameters;
91        }
92    }
93
94    $ini = null;
95    $end = null;
96    if (\success($retval)) {
97        $ini = $query['dateini'] ?? null;
98        if ($ini === null) {
99            $logger->error("Mandatory argument 'dateini' not provided in url parameters.");
100            $retval = \ApplicationError::Parameters;
101        }
102    }
103    if (\success($retval)) {
104        $end = $query['dateend'] ?? null;
105        if ($end === null) {
106            $logger->error("Mandatory argument 'dateend' not provided in url parameters.");
107            $retval = \ApplicationError::Parameters;
108        }
109    }
110
111    if (\success($retval)) {
112        $conx->tenant = $place;
113    }
114
115    if (\success($retval) && $conx->tenant === null) {
116        $logger->error("Connection to place database failed.");
117        $retval = \ApplicationError::Database;
118    }
119
120    $results = [];
121    if (\success($retval)) {
122        $GLOBALS['session'] = $session;
123        $datamodelData = $datamodel['data'] ?? null;
124        if ($datamodelData) {
125            $logger->info("(".$session.") Loading ticket products for products: [".$_objids."]");
126            $retval = \load_updates($conx, $datamodelData, 'PRODUCT', $products, 0, $results);
127        }
128    }
129
130    if (\success($retval)) {
131        $iniTs = is_numeric($ini) ? (int) $ini : strtotime((string) $ini);
132        $endTs = is_numeric($end) ? (int) $end : strtotime((string) $end);
133        foreach ($results as &$result) {
134            $udt = strtotime($result['updated']);
135            if ($udt >= $iniTs && $udt <= $endTs) {
136                $updates[] = &$result;
137            }
138        }
139        unset($result);
140    }
141
142    $result = [
143        "errorcode" => $retval,
144        "updates" => \success($retval) ? $updates : null,
145    ];
146    return ['output' => json_encode($result), 'contentType' => 'application/json; charset=utf-8'];
147};