Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
journal.php
1<?php
3
7
8Loc::loadMessages(__FILE__);
9
10
18{
19 private $exportId;
20 private $type;
21
22
28 public function __construct($exportId, $type)
29 {
30 $this->exportId = $exportId;
31 $this->type = $type;
32 }
33
34
35 public static function getCurrentProcess($exportId)
36 {
37 $resProfiles = ExportProfileTable::getList(array(
38 "filter" => array('=ID' => $exportId),
39 "select" => array("PROCESS"),
40 )
41 );
42 $profile = $resProfiles->fetch();
43
44// if PROCESS empty of set STOP flag - return false
45 if (!empty($profile['PROCESS']) && !(array_key_exists('STOP', $profile['PROCESS']) && $profile['PROCESS']['STOP']))
46 return $profile['PROCESS'];
47 else
48 return false;
49 }
50
51
52 public static function getProgressMessage($exportId, $type)
53 {
54 $type = Feed\Manager::prepareType($type);
55
56 $journal = self::getStatistic($type, $exportId);
57 $count = $journal[$type]['COUNT'] ? $journal[$type]['COUNT'] : 0;
58
59 $detailsMsg = '<p>' . date('G:i - ') . Loc::getMessage('VK_JOURNAL_PROCESSING_COUNT', array("#C1" => $count)) . '</p>';
60 $detailsMsg .= '<p><i>' . Loc::getMessage('VK_JOURNAL_RUNNING_NOTIFY_1') . '<br>' . Loc::getMessage('VK_JOURNAL_RUNNING_NOTIFY_2') . '</i></p>';
61 ob_start();
62 \CAdminMessage::ShowMessage(array(
63 "MESSAGE" => Loc::getMessage("VK_JOURNAL_NOW_EXPORT_" . $type),
64 "DETAILS" =>
65 '<p>' . $detailsMsg . '</p>' .
66 '<input
67 type="button"
68 value="' . Loc::getMessage("VK_JOURNAL_BUTTON_STOP_PROCESS") . '"
69 onclick="if(confirm(\'' . Loc::getMessage("VK_JOURNAL_BUTTON_STOP_PROCESS_ALERT") . '\'))
70 {BX.Sale.VkAdmin.stopProcess(' . $exportId . ');}">',
71 "HTML" => true,
72 "TYPE" => "PROGRESS",
73 ));
74 $res = ob_get_clean();
75
76 return $res;
77 }
78
79 public static function getProgressFinishMessage($ok = true)
80 {
81 $msg = $ok ?
82 Loc::getMessage("VK_JOURNAL_EXPORT_FININSH") :
83 Loc::getMessage("VK_JOURNAL_EXPORT_ABORT");
84 ob_start();
85 \CAdminMessage::ShowMessage(array(
86 "MESSAGE" => $msg,
87 "DETAILS" => '',
88 "TYPE" => "OK")
89 );
90 $res = ob_get_clean();
91
92 return $res;
93 }
94
95
96 public static function getTooMuchTimeExportMessage()
97 {
98 ob_start();
99 \CAdminMessage::ShowMessage(array
100 (
101 "MESSAGE" => Loc::getMessage("VK_JOURNAL_TOO_MUCH_TIMES_NOTIFY_1"),
102 "DETAILS" => Loc::getMessage("VK_JOURNAL_TOO_MUCH_TIMES_NOTIFY_2"),
103 "TYPE" => "ERROR",
104 )
105 );
106 $res = ob_get_clean();
107
108 return $res;
109 }
110
111
112 public static function getCriticalErrorsMessage($exportId, $txt)
113 {
114 ob_start();
115 \CAdminMessage::ShowMessage(array("MESSAGE" => $txt, "HTML" => true, "TYPE" => "ERROR", "DETAILS" => ''));
116 $res = ob_get_clean();
117
118 return $res;
119 }
120
121
122 public static function saveProcessParams($exportId, $type = false, $position = false, $execCount = false)
123 {
124// save only if not set STOP flag
125 if (!self::checkStopProcessFlag($exportId))
126 {
127 $exportId = \EscapePHPString($exportId);
128// if set data - update, if not - clear process
129 $data = array();
130 if ($type !== false && $position !== false && $execCount !== false)
131 $data = array(
132 'TYPE' => $type,
133 'EXEC_COUNT' => $execCount,
134 'START_POSITION' => $position,
135 );
136 $resUpdate = ExportProfileTable::update($exportId, array("PROCESS" => $data));
137
138 return $resUpdate->isSuccess();
139 }
140 else
141 {
142 return false;
143 }
144 }
145
151 public static function clearStopProcessParams($exportId)
152 {
153 $resUpdate = ExportProfileTable::update($exportId, array("PROCESS" => NULL));
154
155 return $resUpdate->isSuccess();
156 }
157
163 public static function stopProcessParams($exportId)
164 {
165 $exportId = \EscapePHPString($exportId);
166 $data = array('STOP' => true);
167 $resUpdate = ExportProfileTable::update($exportId, array("PROCESS" => $data));
168
169 return $resUpdate->isSuccess();
170 }
171
172
173 public static function checkStopProcessFlag($exportId)
174 {
175 $exportId = \EscapePHPString($exportId);
176
177 $resProfiles = ExportProfileTable::getList(array(
178 "filter" => array('=ID' => $exportId),
179 "select" => array("PROCESS"),
180 )
181 );
182 $profile = $resProfiles->fetch();
183
184 return
185 isset($profile['PROCESS']) &&
186 (array_key_exists('STOP', $profile['PROCESS']) && $profile['PROCESS']['STOP']);
187 }
188
189
196 private function getJournal()
197 {
198 $resJournal = ExportProfileTable::getList(array(
199 "filter" => array('=ID' => $this->exportId),
200 "select" => array("JOURNAL"),
201 )
202 );
203 $journal = $resJournal->fetch();
204
205 return $journal["JOURNAL"];
206 }
207
208
216 public function addItemsCount($count)
217 {
218 $journal = $this->getJournal();
219
220// existing journal - increment count
221 if (isset($journal[$this->type]["START"]) && !isset($journal[$this->type]["END"]))
222 {
223 $journal[$this->type]["COUNT"] = intval($journal[$this->type]["COUNT"]) + intval($count);
224 }
225
226// if not set START or set both START and END - it is new item, init them
227 else
228 {
229 $journal[$this->type] = array(
230 "START" => time(),
231 "COUNT" => intval($count),
232 );
233 unset($journal[$this->type]["END"], $journal[$this->type]["ABORT"]);
234 }
235
236 $resUpdate = ExportProfileTable::update($this->exportId, array("JOURNAL" => $journal));
237
238 return $resUpdate->isSuccess();
239 }
240
241
248 public function start()
249 {
250// remove old values and init new journal log for current type
251 $journal = $this->getJournal();
252 $journal[$this->type] = array('START' => time());
253
254 $resUpdate = ExportProfileTable::update($this->exportId, array("JOURNAL" => $journal));
255
256 return $resUpdate->isSuccess();
257 }
258
265 public function end()
266 {
267// finish current type journal
268 $journal = $this->getJournal();
269 $journal[$this->type]["END"] = time();
270
271 $resUpdate = ExportProfileTable::update($this->exportId, array("JOURNAL" => $journal));
272
273 return $resUpdate->isSuccess();
274 }
275
276
277 private static function getStatistic($type, $exportId)
278 {
279 $resJournal = ExportProfileTable::getList(array(
280 "filter" => array('=ID' => $exportId),
281 "select" => array("JOURNAL"),
282 )
283 );
284 $journal = $resJournal->fetch();
285 if (!$journal)
286 return false;
287 else
288 $journal = $journal["JOURNAL"];
289
290 //check if process stopped, but journal not ending
291 return self::getCheckedEndingJournal($type, $exportId, $journal);
292 }
293
294
303 public static function getStatisticText($type, $exportId)
304 {
305 $journal = self::getStatistic($type, $exportId);
306
307 $result = "<h4>" . Loc::getMessage("VK_JOURNAL_" . $type) . "</h4>";
308
309// never running
310 if (!isset($journal[$type]["START"]) && !isset($journal[$type]["END"]))
311 {
312 $result .= "<p>" . Loc::getMessage("VK_JOURNAL_NEVER") . "</p>";
313 }
314
315// export was finished
316 elseif (isset($journal[$type]["END"]))
317 {
318 $result .= "<p>";
319 $result .= Loc::getMessage("VK_JOURNAL_LAST_EXPORT", array(
320 '#D1' => date('d.m.y', $journal[$type]["END"]),
321 '#D2' => date('G:i', $journal[$type]["END"]),
322 ));
323 $result .= isset($journal[$type]["ABORT"]) && $journal[$type]["ABORT"] ?
324 ' ' . Loc::getMessage("VK_JOURNAL_LAST_EXPORT_ABORTED") : '';
325 $result .= "</p>";
326
327// add count items
328 if ($journal[$type]["COUNT"])
329 $result .= "<p>" .
330 Loc::getMessage("VK_JOURNAL_COUNT_WAS", array(
331 '#C1' => $journal[$type]["COUNT"],
332 )) .
333 "</p>";
334 }
335
336// running now
337 elseif (isset($journal[$type]["START"]))
338 {
339 $result .= "<p>" . Loc::getMessage("VK_JOURNAL_EXPORT_NOW") . "</p>";
340
341// add count items
342 if ($journal[$type]["COUNT"])
343 $result .=
344 "<p>" .
345 Loc::getMessage("VK_JOURNAL_COUNT_NOW", array(
346 '#C1' => $journal[$type]["COUNT"],
347 )) .
348 "</p>";
349 }
350
351 return $result;
352 }
353
354
365 private static function getCheckedEndingJournal($type, $exportId, $journal)
366 {
367 $runningProcess = self::getCurrentProcess($exportId);
368 $runningProcessType = Feed\Manager::prepareType($runningProcess['TYPE']);
369
370 if ($runningProcess && $runningProcessType == $type)
371 {
372// if process was running, but not eyt cleared journal
373 if (isset($journal[$type]["START"]) && isset($journal[$type]["END"]))
374 {
375 unset($journal[$type]["END"]);
376 unset($journal[$type]["ABORT"]);
377 $journal[$type]["COUNT"] = 0;
378
379 ExportProfileTable::update($exportId, array("JOURNAL" => $journal));
380 }
381 }
382
383 else
384 {
385// if journal not closed, but process not running, it means that process was stopped manually - close journal
386 if (isset($journal[$type]["START"]) && !isset($journal[$type]["END"]))
387 {
388 $journal[$type]["ABORT"] = true;
389 $journal[$type]["END"] = $journal[$type]["START"];
390
391 ExportProfileTable::update($exportId, array("JOURNAL" => $journal));
392 }
393 }
394
395 return $journal;
396 }
397}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getProgressMessage($exportId, $type)
Definition journal.php:52