1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
historyservice.php
См. документацию.
1<?php
2
4{
5 protected $useGZipCompression = false;
6
7 public function __construct()
8 {
9 $this->useGZipCompression = \CBPWorkflowTemplateLoader::useGZipCompression();
10 }
11
12 protected function parseFields(&$arFields, $id = 0)
13 {
14 $id = (int)$id;
15 $updateMode = ($id > 0);
16 $addMode = !$updateMode;
17
18 if ($addMode && !is_set($arFields, "DOCUMENT_ID"))
19 throw new CBPArgumentNullException("DOCUMENT_ID");
20
21 if (is_set($arFields, "DOCUMENT_ID") || $addMode)
22 {
23 $arDocumentId = CBPHelper::ParseDocumentId($arFields["DOCUMENT_ID"]);
24 $arFields["MODULE_ID"] = $arDocumentId[0];
25 if ($arFields["MODULE_ID"] == '')
26 $arFields["MODULE_ID"] = false;
27 $arFields["ENTITY"] = $arDocumentId[1];
28 $arFields["DOCUMENT_ID"] = $arDocumentId[2];
29 }
30
31 if (is_set($arFields, "NAME") || $addMode)
32 {
33 $arFields["NAME"] = (string) $arFields["NAME"];
34 if ($arFields["NAME"] == '')
35 throw new CBPArgumentNullException("NAME");
36 }
37
38 if (is_set($arFields, "DOCUMENT"))
39 {
40 if ($arFields["DOCUMENT"] == null)
41 {
42 $arFields["DOCUMENT"] = false;
43 }
44 elseif (is_array($arFields["DOCUMENT"]))
45 {
46 if (count($arFields["DOCUMENT"]) > 0)
47 $arFields["DOCUMENT"] = $this->GetSerializedForm($arFields["DOCUMENT"]);
48 else
49 $arFields["DOCUMENT"] = false;
50 }
51 else
52 {
53 throw new CBPArgumentTypeException("DOCUMENT");
54 }
55 }
56
57 unset($arFields["MODIFIED"]);
58 }
59
60 private function getSerializedForm($arTemplate)
61 {
62 $buffer = serialize($arTemplate);
63 if ($this->useGZipCompression)
64 $buffer = gzcompress($buffer, 9);
65 return $buffer;
66 }
67
68 public static function add($arFields)
69 {
70 $h = new CBPHistoryService();
71 return $h->AddHistory($arFields);
72 }
73
74 public static function update($id, $arFields)
75 {
76 $h = new CBPHistoryService();
77 return $h->UpdateHistory($id, $arFields);
78 }
79
80 private static function generateFilePath($documentId)
81 {
82 $arDocumentId = CBPHelper::ParseDocumentId($documentId);
83
84 $dest = "/bizproc/";
85 if ($arDocumentId[0] <> '')
86 $dest .= preg_replace("/[^a-zA-Z0-9._]/i", "_", $arDocumentId[0]);
87 else
88 $dest .= "NA";
89 $documentIdMD5 = md5($arDocumentId[2]);
90 $dest .= "/".preg_replace("/[^a-zA-Z0-9_]/i", "_", $arDocumentId[1])."/".mb_substr($documentIdMD5, 0, 3)."/".$documentIdMD5;
91
92 return $dest;
93 }
94
95 public function deleteHistory($id, $documentId = null)
96 {
97 global $DB;
98
99 $id = intval($id);
100 if ($id <= 0)
101 throw new Exception("id");
102
103 $arFilter = array("ID" => $id);
104 if ($documentId != null)
105 $arFilter["DOCUMENT_ID"] = $documentId;
106
107 $db = $this->GetHistoryList(
108 array(),
109 $arFilter,
110 false,
111 false,
112 array("ID", "MODULE_ID", "ENTITY", "DOCUMENT_ID")
113 );
114 if ($ar = $db->Fetch())
115 {
116 $deleteFile = true;
117 foreach(GetModuleEvents("bizproc", "OnBeforeDeleteFileFromHistory", true) as $event)
118 {
119 if(ExecuteModuleEventEx($event, array($id, $documentId)) !== true)
120 {
121 $deleteFile = false;
122 break;
123 }
124 }
125
126 if ($deleteFile)
127 {
128 $dest = self::GenerateFilePath($ar["DOCUMENT_ID"]);
129 DeleteDirFilesEx("/".(COption::GetOptionString("main", "upload_dir", "upload")).$dest."/".$ar["ID"]);
130 if(CModule::IncludeModule('clouds'))
132 }
133
134 $DB->Query("DELETE FROM b_bp_history WHERE ID = ".intval($id)." ", true);
135 }
136 }
137
138 public static function delete($id, $documentId = null)
139 {
140 $h = new CBPHistoryService();
141 $h->DeleteHistory($id, $documentId);
142 }
143
144 public function deleteHistoryByDocument($documentId)
145 {
146 global $DB;
147
148 $arDocumentId = CBPHelper::ParseDocumentId($documentId);
149
150 $dest = self::GenerateFilePath($documentId);
151 DeleteDirFilesEx("/".(COption::GetOptionString("main", "upload_dir", "upload")).$dest);
152 if(CModule::IncludeModule('clouds'))
154
155 $DB->Query(
156 "DELETE FROM b_bp_history ".
157 "WHERE DOCUMENT_ID = '".$DB->ForSql($arDocumentId[2])."' ".
158 " AND ENTITY = '".$DB->ForSql($arDocumentId[1])."' ".
159 " AND MODULE_ID ".(($arDocumentId[0] <> '') ? "= '".$DB->ForSql($arDocumentId[0])."'" : "IS NULL")." ",
160 true
161 );
162 }
163
164 public static function deleteByDocument($documentId)
165 {
166 $h = new CBPHistoryService();
167 $h->DeleteHistoryByDocument($documentId);
168 }
169
170 public static function getById($id)
171 {
172 $id = intval($id);
173 if ($id <= 0)
174 throw new CBPArgumentNullException("id");
175
176 $h = new CBPHistoryService();
177 $db = $h->GetHistoryList(array(), array("ID" => $id));
178 return $db->GetNext();
179 }
180
181 public static function getList($arOrder = array("ID" => "DESC"), $arFilter = array(), $arGroupBy = false, $arNavStartParams = false, $arSelectFields = array())
182 {
183 $h = new CBPHistoryService();
184 return $h->GetHistoryList($arOrder, $arFilter, $arGroupBy, $arNavStartParams, $arSelectFields);
185 }
186
187 public static function recoverDocumentFromHistory($id)
188 {
189 $arHistory = self::GetById($id);
190 if (!$arHistory)
191 throw new Exception(str_replace("#ID#", intval($id), GetMessage("BPCGHIST_INVALID_ID")));
192
193 list($moduleId, $entity, $documentId) = CBPHelper::ParseDocumentId($arHistory["DOCUMENT_ID"]);
194
195 if ($moduleId <> '')
196 CModule::IncludeModule($moduleId);
197
198 if (class_exists($entity))
199 return call_user_func_array(array($entity, "RecoverDocumentFromHistory"), array($documentId, $arHistory["DOCUMENT"]));
200
201 return false;
202 }
203
204 public static function prepareFileForHistory($documentId, $arFileId, $historyIndex)
205 {
206 $dest = self::GenerateFilePath($documentId);
207
208 $fileParameterIsArray = true;
209 if (!is_array($arFileId))
210 {
211 $arFileId = array($arFileId);
212 $fileParameterIsArray = false;
213 }
214
215 $result = array();
216
217 foreach ($arFileId as $fileId)
218 {
219 if($ar = CFile::GetFileArray($fileId))
220 {
221 $newFilePath = CFile::CopyFile($fileId, false, $dest."/".$historyIndex."/".$ar["FILE_NAME"]);
222 if ($newFilePath)
223 $result[] = $newFilePath;
224 }
225 }
226
227 if (!$fileParameterIsArray)
228 {
229 if (count($result) > 0)
230 $result = $result[0];
231 else
232 $result = "";
233 }
234
235 return $result;
236 }
237
238 public static function mergeHistory($firstDocumentId, $secondDocumentId)
239 {
240 global $DB;
241
242 $arFirstDocumentId = CBPHelper::ParseDocumentId($firstDocumentId);
243 $arSecondDocumentId = CBPHelper::ParseDocumentId($secondDocumentId);
244
245 $DB->Query(
246 "UPDATE b_bp_history SET ".
247 " DOCUMENT_ID = '".$DB->ForSql($arFirstDocumentId[2])."', ".
248 " ENTITY = '".$DB->ForSql($arFirstDocumentId[1])."', ".
249 " MODULE_ID = '".$DB->ForSql($arFirstDocumentId[0])."' ".
250 "WHERE DOCUMENT_ID = '".$DB->ForSql($arSecondDocumentId[2])."' ".
251 " AND ENTITY = '".$DB->ForSql($arSecondDocumentId[1])."' ".
252 " AND MODULE_ID = '".$DB->ForSql($arSecondDocumentId[0])."' "
253 );
254 }
255
256 public static function migrateDocumentType($oldType, $newType, $workflowTemplateIds)
257 {
258 global $DB;
259
260 $arOldType = CBPHelper::ParseDocumentId($oldType);
261 $arNewType = CBPHelper::ParseDocumentId($newType);
262
263 $DB->Query(
264 "UPDATE b_bp_history SET ".
265 " ENTITY = '".$DB->ForSql($arNewType[1])."', ".
266 " MODULE_ID = '".$DB->ForSql($arNewType[0])."' ".
267 "WHERE ENTITY = '".$DB->ForSql($arOldType[1])."' ".
268 " AND MODULE_ID = '".$DB->ForSql($arOldType[0])."' ".
269 " AND DOCUMENT_ID IN (SELECT t.DOCUMENT_ID FROM b_bp_workflow_state t WHERE t.WORKFLOW_TEMPLATE_ID in (".implode(",", $workflowTemplateIds).") and t.MODULE_ID='".$DB->ForSql($arOldType[0])."' and t.ENTITY='".$DB->ForSql($arOldType[1])."') "
270 );
271 }
272
273 public function addHistory($arFields)
274 {
275 global $DB;
276
277 self::ParseFields($arFields, 0);
278
279 $arInsert = $DB->PrepareInsert("b_bp_history", $arFields);
280
281 $strSql =
282 "INSERT INTO b_bp_history (".$arInsert[0].", MODIFIED) ".
283 "VALUES(".$arInsert[1].", ".$DB->CurrentTimeFunction().")";
284 $DB->Query($strSql);
285
286 $ID = intval($DB->LastID());
287
288 $arEventParams = array(
289 "ID" => $ID,
290 "DOCUMENT_ID" => array($arFields['MODULE_ID'], $arFields['ENTITY'], $arFields['DOCUMENT_ID']),
291 );
292 foreach (GetModuleEvents('bizproc', 'OnAddToHistory', true) as $arEvent)
293 $result = ExecuteModuleEventEx($arEvent, array($arEventParams));
294
295 return $ID;
296 }
297
298 public function updateHistory($id, $arFields)
299 {
300 global $DB;
301
302 $id = intval($id);
303 if ($id <= 0)
304 throw new CBPArgumentNullException("id");
305
306 self::ParseFields($arFields, $id);
307
308 $strUpdate = $DB->PrepareUpdate("b_bp_history", $arFields);
309
310 $strSql =
311 "UPDATE b_bp_history SET ".
312 " ".$strUpdate.", ".
313 " MODIFIED = ".$DB->CurrentTimeFunction()." ".
314 "WHERE ID = ".intval($id)." ";
315 $DB->Query($strSql);
316
317 return $id;
318 }
319
320 public function getHistoryList($arOrder = array("ID" => "DESC"), $arFilter = array(), $arGroupBy = false, $arNavStartParams = false, $arSelectFields = array())
321 {
322 global $DB;
323
324 if (count($arSelectFields) <= 0)
325 $arSelectFields = array("ID", "MODULE_ID", "ENTITY", "DOCUMENT_ID", "NAME", "DOCUMENT", "MODIFIED", "USER_ID");
326
327 if (count(array_intersect($arSelectFields, array("MODULE_ID", "ENTITY", "DOCUMENT_ID"))) > 0)
328 {
329 if (!in_array("MODULE_ID", $arSelectFields))
330 $arSelectFields[] = "MODULE_ID";
331 if (!in_array("ENTITY", $arSelectFields))
332 $arSelectFields[] = "ENTITY";
333 if (!in_array("DOCUMENT_ID", $arSelectFields))
334 $arSelectFields[] = "DOCUMENT_ID";
335 }
336
337 if (array_key_exists("DOCUMENT_ID", $arFilter))
338 {
339 $d = CBPHelper::ParseDocumentId($arFilter["DOCUMENT_ID"]);
340 $arFilter["MODULE_ID"] = $d[0];
341 $arFilter["ENTITY"] = $d[1];
342 $arFilter["DOCUMENT_ID"] = $d[2];
343 }
344
345 static $arFields = array(
346 "ID" => Array("FIELD" => "H.ID", "TYPE" => "int"),
347 "MODULE_ID" => Array("FIELD" => "H.MODULE_ID", "TYPE" => "string"),
348 "ENTITY" => Array("FIELD" => "H.ENTITY", "TYPE" => "string"),
349 "DOCUMENT_ID" => Array("FIELD" => "H.DOCUMENT_ID", "TYPE" => "string"),
350 "NAME" => Array("FIELD" => "H.NAME", "TYPE" => "string"),
351 "DOCUMENT" => Array("FIELD" => "H.DOCUMENT", "TYPE" => "string"),
352 "MODIFIED" => Array("FIELD" => "H.MODIFIED", "TYPE" => "datetime"),
353 "USER_ID" => Array("FIELD" => "H.USER_ID", "TYPE" => "int"),
354
355 "USER_NAME" => Array("FIELD" => "U.NAME", "TYPE" => "string", "FROM" => "INNER JOIN b_user U ON (H.USER_ID = U.ID)"),
356 "USER_LAST_NAME" => Array("FIELD" => "U.LAST_NAME", "TYPE" => "string", "FROM" => "INNER JOIN b_user U ON (H.USER_ID = U.ID)"),
357 "USER_SECOND_NAME" => Array("FIELD" => "U.SECOND_NAME", "TYPE" => "string", "FROM" => "INNER JOIN b_user U ON (H.USER_ID = U.ID)"),
358 "USER_LOGIN" => Array("FIELD" => "U.LOGIN", "TYPE" => "string", "FROM" => "INNER JOIN b_user U ON (H.USER_ID = U.ID)"),
359 );
360
361 $arSqls = CBPHelper::PrepareSql($arFields, $arOrder, $arFilter, $arGroupBy, $arSelectFields);
362
363 $arSqls["SELECT"] = str_replace("%%_DISTINCT_%%", "", $arSqls["SELECT"]);
364
365 if (is_array($arGroupBy) && count($arGroupBy)==0)
366 {
367 $strSql =
368 "SELECT ".$arSqls["SELECT"]." ".
369 "FROM b_bp_history H ".
370 " ".$arSqls["FROM"]." ";
371 if ($arSqls["WHERE"] <> '')
372 $strSql .= "WHERE ".$arSqls["WHERE"]." ";
373 if ($arSqls["GROUPBY"] <> '')
374 $strSql .= "GROUP BY ".$arSqls["GROUPBY"]." ";
375
376 $dbRes = $DB->Query($strSql);
377 if ($arRes = $dbRes->Fetch())
378 return $arRes["CNT"];
379 else
380 return False;
381 }
382
383 $strSql =
384 "SELECT ".$arSqls["SELECT"]." ".
385 "FROM b_bp_history H ".
386 " ".$arSqls["FROM"]." ";
387 if ($arSqls["WHERE"] <> '')
388 $strSql .= "WHERE ".$arSqls["WHERE"]." ";
389 if ($arSqls["GROUPBY"] <> '')
390 $strSql .= "GROUP BY ".$arSqls["GROUPBY"]." ";
391 if ($arSqls["ORDERBY"] <> '')
392 $strSql .= "ORDER BY ".$arSqls["ORDERBY"]." ";
393
394 if (is_array($arNavStartParams) && intval($arNavStartParams["nTopCount"]) <= 0)
395 {
396 $strSql_tmp =
397 "SELECT COUNT('x') as CNT ".
398 "FROM b_bp_history H ".
399 " ".$arSqls["FROM"]." ";
400 if ($arSqls["WHERE"] <> '')
401 $strSql_tmp .= "WHERE ".$arSqls["WHERE"]." ";
402 if ($arSqls["GROUPBY"] <> '')
403 $strSql_tmp .= "GROUP BY ".$arSqls["GROUPBY"]." ";
404
405 $dbRes = $DB->Query($strSql_tmp);
406 $cnt = 0;
407 if ($arSqls["GROUPBY"] == '')
408 {
409 if ($arRes = $dbRes->Fetch())
410 $cnt = $arRes["CNT"];
411 }
412 else
413 {
414 // only for MySQL
415 $cnt = $dbRes->SelectedRowsCount();
416 }
417
418 $dbRes = new CDBResult();
419 $dbRes->NavQuery($strSql, $cnt, $arNavStartParams);
420 }
421 else
422 {
423 if (is_array($arNavStartParams) && intval($arNavStartParams["nTopCount"]) > 0)
424 $strSql .= "LIMIT ".intval($arNavStartParams["nTopCount"]);
425
426 $dbRes = $DB->Query($strSql);
427 }
428
429 $dbRes = new CBPHistoryResult($dbRes, $this->useGZipCompression);
430 return $dbRes;
431 }
432}
433
434class CBPHistoryResult extends CDBResult
435{
436 private $useGZipCompression = false;
437
438 public function __construct($res, $useGZipCompression)
439 {
440 $this->useGZipCompression = $useGZipCompression;
441 parent::__construct($res);
442 }
443
444 private function getFromSerializedForm($value)
445 {
446 if ($value <> '')
447 {
448 if ($this->useGZipCompression)
449 $value = gzuncompress($value);
450
451 $value = unserialize($value, ['allowed_classes' => false]);
452 if (!is_array($value))
453 $value = array();
454 }
455 else
456 {
457 $value = array();
458 }
459 return $value;
460 }
461
462 function fetch()
463 {
464 $res = parent::Fetch();
465
466 if ($res)
467 {
468 if (array_key_exists("DOCUMENT_ID", $res))
469 $res["DOCUMENT_ID"] = array($res["MODULE_ID"], $res["ENTITY"], $res["DOCUMENT_ID"]);
470 if (array_key_exists("DOCUMENT", $res))
471 $res["DOCUMENT"] = $this->GetFromSerializedForm($res["DOCUMENT"]);
472 }
473
474 return $res;
475 }
476}
__construct($res, $useGZipCompression)
Определения historyservice.php:438
fetch()
Определения historyservice.php:462
static migrateDocumentType($oldType, $newType, $workflowTemplateIds)
Определения historyservice.php:256
__construct()
Определения historyservice.php:7
static recoverDocumentFromHistory($id)
Определения historyservice.php:187
deleteHistoryByDocument($documentId)
Определения historyservice.php:144
static getById($id)
Определения historyservice.php:170
$useGZipCompression
Определения historyservice.php:5
static update($id, $arFields)
Определения historyservice.php:74
parseFields(&$arFields, $id=0)
Определения historyservice.php:12
GetHistoryList($arOrder=array("ID"=> "DESC"), $arFilter=array(), $arGroupBy=false, $arNavStartParams=false, $arSelectFields=array())
Определения historyservice.php:59
static prepareFileForHistory($documentId, $arFileId, $historyIndex)
Определения historyservice.php:204
static deleteByDocument($documentId)
Определения historyservice.php:164
static getList($arOrder=array("ID"=> "DESC"), $arFilter=array(), $arGroupBy=false, $arNavStartParams=false, $arSelectFields=array())
Определения historyservice.php:181
updateHistory($id, $arFields)
Определения historyservice.php:298
deleteHistory($id, $documentId=null)
Определения historyservice.php:95
static mergeHistory($firstDocumentId, $secondDocumentId)
Определения historyservice.php:238
addHistory($arFields)
Определения historyservice.php:273
static add($arFields)
Определения historyservice.php:68
getHistoryList($arOrder=array("ID"=> "DESC"), $arFilter=array(), $arGroupBy=false, $arNavStartParams=false, $arSelectFields=array())
Определения historyservice.php:320
static DeleteDirFilesEx($path)
Определения storage.php:946
$arTemplate
Определения component_props.php:26
$arFields
Определения dblapprove.php:5
if(!defined("ADMIN_AJAX_MODE") &&(($_REQUEST["mode"] ?? '') !='excel')) $buffer
Определения epilog_admin_after.php:40
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$res
Определения filter_act.php:7
$result
Определения get_property_values.php:14
if($ajaxMode) $ID
Определения get_user.php:27
$entity
$moduleId
global $DB
Определения cron_frame.php:29
ExecuteModuleEventEx($arEvent, $arParams=[])
Определения tools.php:5214
GetModuleEvents($MODULE_ID, $MESSAGE_ID, $bReturnArray=false)
Определения tools.php:5177
DeleteDirFilesEx($path, $root=null)
Определения tools.php:2823
is_set($a, $k=false)
Определения tools.php:2133
GetMessage($name, $aReplace=null)
Определения tools.php:3397
$value
Определения Param.php:39
$event
Определения prolog_after.php:141
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$ar
Определения options.php:199
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
if(!empty($sellerData)) $dest
Определения pdf.php:818
$arRes
Определения options.php:104
$arFilter
Определения user_search.php:106
$dbRes
Определения yandex_detail.php:168