Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
userprofilehistory.php
1<?php
2
10namespace Bitrix\Main;
11
14
34{
35 use Data\Internal\DeleteByFilterTrait;
36
37 const TYPE_ADD = 1;
38 const TYPE_UPDATE = 2;
39 const TYPE_DELETE = 3;
40
41 public static function getTableName()
42 {
43 return 'b_user_profile_history';
44 }
45
46 public static function getMap()
47 {
48 return array(
49 new Entity\IntegerField("ID", array(
50 'primary' => true,
51 'autocomplete' => true,
52 )),
53 new Entity\IntegerField("USER_ID", array(
54 'required' => true,
55 )),
56 new Entity\IntegerField("EVENT_TYPE"),
57 new Entity\DatetimeField("DATE_INSERT", array(
58 'default_value' => function(){return new Type\DateTime();}
59 )),
60 new Entity\StringField("REMOTE_ADDR"),
61 new Entity\TextField('USER_AGENT'),
62 new Entity\TextField('REQUEST_URI'),
63 new Entity\IntegerField("UPDATED_BY_ID"),
64 );
65 }
66
74 public static function addHistory($userId, $type, array $before = null, array $after = null)
75 {
76 global $USER;
77
78 $server = Context::getCurrent()->getServer();
79 $request = Context::getCurrent()->getRequest();
80
81 $url = preg_replace("/(&?sessid=[0-9a-z]+)/", "", $request->getDecodedUri());
82 $remoteAddr = $server->get("REMOTE_ADDR");
83 $userAgent = $server->get("HTTP_USER_AGENT");
84 $updatedBy = (is_object($USER) && ($USER->GetID() > 0)? $USER->GetID() : null);
85
86 $changedFields = array();
87 if(is_array($before) && is_array($after))
88 {
89 //we shouldn't display some values
90 static $hiddenFields = array("PASSWORD" => 1, "CHECKWORD" => 1, "CONFIRM_CODE" => 1);
91 static $ignoredFields = array("TIMESTAMP_X" => 1);
92
93 foreach($after as $field => $value)
94 {
95 if(isset($ignoredFields[$field]))
96 {
97 continue;
98 }
99
100 if($before[$field] <> $value)
101 {
102 $data = array(
103 "before" => (isset($hiddenFields[$field])? "***" : $before[$field]),
104 "after" => (isset($hiddenFields[$field])? "***" : $value)
105 );
106
107 $changedFields[] = array(
108 "FIELD" => $field,
109 "DATA" => $data,
110 );
111 }
112 }
113 }
114
115 $result = null;
116
117 if(!empty($changedFields) || $type <> self::TYPE_UPDATE)
118 {
119 $result = static::add(array(
120 "USER_ID" => $userId,
121 "EVENT_TYPE" => $type,
122 "REMOTE_ADDR" => $remoteAddr,
123 "USER_AGENT" => $userAgent,
124 "REQUEST_URI" => $url,
125 "UPDATED_BY_ID" => $updatedBy,
126 ));
127 }
128
129 if(!empty($changedFields) && $result->isSuccess())
130 {
131 foreach($changedFields as $value)
132 {
133 UserProfileRecordTable::add(array(
134 "HISTORY_ID" => $result->getId(),
135 "FIELD" => $value["FIELD"],
136 "DATA" => $value["DATA"],
137 ));
138 }
139 }
140
141 return $result;
142 }
143
144 public static function deleteByUser($userId)
145 {
146 static::deleteByFilter(["=USER_ID" => $userId]);
147 }
148
149 protected static function onBeforeDeleteByFilter(string $where)
150 {
152 }
153}
static addHistory($userId, $type, array $before=null, array $after=null)