scheduling.php 7.87 KB
Newer Older
Prosper De Laure's avatar
Prosper De Laure 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
<?php

/*
 * Copyright (C) 2015 Maarch
 *
 * This file is part of bundle batchProcessing.
 *
 * Bundle batchProcessing 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 3 of the License, or
 * (at your option) any later version.
 *
 * Bundle batchProcessing is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with bundle batchProcessing.  If not, see <http://www.gnu.org/licenses/>.
 */

namespace presentation\maarchRM\Presenter\batchProcessing;

/**
 * search batchProcessing html serializer
 *
 * @package batchProcessing
 * @author Prosper DE LAURE <prosper.delaure@maarch.org>
 */
class scheduling
{
    use \presentation\maarchRM\Presenter\exceptions\exceptionTrait;

    public $view;
    public $json;
    protected $translator;

    /**
     * Constuctor of batchProcessing html serializer
     * @param \dependency\html\Document                    $view       The view
     * @param \dependency\json\JsonObject                  $json       The Json object
     * @param \dependency\localisation\TranslatorInterface $translator The translator service
     */
    public function __construct(\dependency\html\Document $view, \dependency\json\JsonObject $json, \dependency\localisation\TranslatorInterface $translator)
    {
        $this->view = $view;
        $this->json = $json;
        $this->translator = $translator;
        $this->translator->setCatalog('batchProcessing/messages');
    }

    /**
     * get a form to search resource
     * @param array $scheduledTasks Array of scheduled tasks
     *
     * @return string
     */
    public function index($scheduledTasks)
    {
        $this->view->addContentFile("batchProcessing/scheduling/scheduling.html");

        $tasks = \laabs::callService('batchProcessing/scheduling/readTasks');

        foreach ($scheduledTasks as $scheduledTask) {
            $scheduledTask->taskName = $tasks[$scheduledTask->taskId]->description;
            $frequency = explode(';', $scheduledTask->frequency);

            $scheduledTask->startMinutes = $frequency[0];
            $scheduledTask->startHours = $frequency[1];

            if ($frequency[2] != '') {
                $scheduledTask->weekDays = explode(',', $frequency[2]);
            } else {
                $scheduledTask->weekDays = array();
            }

            if ($frequency[3] != '') {
                $scheduledTask->monthDays = explode(',', $frequency[3]);
            } else {
                $scheduledTask->monthDays = array();
            }
            $scheduledTask->monthDaysText = implode(', ', $scheduledTask->monthDays);

            if ($frequency[4] != '') {
                $scheduledTask->month = explode(',', $frequency[4]);
                $translateMonth = [];
                foreach ($scheduledTask->month as $month) {
                    $translateMonth[] = $this->translator->getText(strtoupper($month), "month");
                }
                $scheduledTask->month = $translateMonth;
            } else {
                $scheduledTask->month = array();
            }
            $scheduledTask->monthText = implode(', ', $scheduledTask->month);

            $scheduledTask->frequencyNumber = $frequency[5];
            $scheduledTask->frequencyUnit = $frequency[6];
            $scheduledTask->endMinutes = $frequency[7];
            $scheduledTask->endHours = $frequency[8];
100

Prosper De Laure's avatar
Prosper De Laure committed
101
102
103
            $scheduledTask->json = json_encode($scheduledTask);
        }

104
105
        $accountId = \laabs::getToken("AUTH")->accountId;
        $account = \laabs::callService("auth/userAccount/read_userAccountId_", $accountId);
106
        $hasSecurityLevel = isset(\laabs::configuration('auth')['useSecurityLevel']) ? (bool) \laabs::configuration('auth')['useSecurityLevel'] : false;
107

108
109
110
111
112
113
114
115
        if ($hasSecurityLevel) {
            if (is_null($account->securityLevel)
                || !$account->securityLevel === \bundle\auth\Model\account::SECLEVEL_USER
            ) {
                $isUser = false;
            } else {
                $isUser = true;
            }
116
117
        } else {
            $isUser = true;
118
119
        }

Prosper De Laure's avatar
Prosper De Laure committed
120
121
122
        $this->view->translate();
        $this->view->setSource("tasks", $tasks);
        $this->view->setSource("scheduledTasks", $scheduledTasks);
123
        $this->view->setSource("timezone", date_default_timezone_get());
124
        $this->view->setSource("isUser", $isUser);
Prosper De Laure's avatar
Prosper De Laure committed
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
        $this->view->merge();

        return $this->view->saveHtml();
    }

    //JSON
    /**
     * Serializer JSON for create method
     * @param bool $result
     *
     * @return object JSON object with a status and message parameters
     */
    public function create($result)
    {
        $this->json->status = true;
        $this->json->result = $result;

        $this->json->message = "New scheduling created";
        $this->json->message = $this->translator->getText($this->json->message);

        return $this->json->save();
    }

    /**
     * Serializer JSON for update method
     * @param object $result
     *
     * @return object JSON object with a status and message parameters
     */
    public function update($result)
    {
        $this->json->status = $result;
        $this->json->message = "Scheduling updated";
        $this->json->message = $this->translator->getText($this->json->message);

        return $this->json->save();
    }

    /**
     * Serializer JSON for delete method
     * @param object $result
     *
     * @return object JSON object with a status and message parameters
     */
    public function delete($result)
    {
        $this->json->status = $result;
        $this->json->message = "Scheduling deleted";
        $this->json->message = $this->translator->getText($this->json->message);

        return $this->json->save();
    }

    /**
     * Serializer JSON for execute method
     * @param bool $result
     *
     * @return object JSON object with a status and message parameters
     */
    public function execute($result)
    {
186
187
188
189
190
191
192
        if ($result->status == "error") {
            $this->json->status = false;
            $this->json->message = "An error occurred during execution";
        } else {
            $this->json->status = true;
            $this->json->message = "Task execution triggered";
        }
Prosper De Laure's avatar
Prosper De Laure committed
193
194

        $this->json->message = $this->translator->getText($this->json->message);
195
196
197
198
199
        $result->lastExecution = \laabs::newDateTime($result->lastExecution)->setTimezone(timezone_open(date_default_timezone_get()));
        $result->lastExecution = $result->lastExecution->format("Y-m-d H:i:s P");

        $result->nextExecution = \laabs::newDateTime($result->nextExecution)->setTimezone(timezone_open(date_default_timezone_get()));
        $result->nextExecution = $result->nextExecution->format("Y-m-d H:i:s P");
Prosper De Laure's avatar
Prosper De Laure committed
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
        $this->json->object = $result;

        return $this->json->save();
    }

    /**
     * Serializer JSON for update status
     * @param object $result
     *
     * @return object JSON object with a status and message parameters
     */
    public function changeStatus($result)
    {
        $this->json->status = true;
        $this->json->message = "Status updated";
        $this->json->message = $this->translator->getText($this->json->message);
        $this->json->object = $result;

        return $this->json->save();
    }
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234

    /**
     * List of service accounts available to select for user
     *
     * @param  array $serviceAccounts array of serviceAccounts
     *
     * @return object JSON object
     */
    public function listServiceAccounts($serviceAccounts)
    {
        $this->json->status = true;
        $this->json->serviceAccounts = $serviceAccounts;

        return $this->json->save();
    }
Prosper De Laure's avatar
Prosper De Laure committed
235
}