Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
usercontentview.php
1<?php
9
17
45class UserContentViewTable extends Entity\DataManager
46{
47 public static function getTableName()
48 {
49 return 'b_sonet_user_content_view';
50 }
51
52 public static function getMap()
53 {
54 $fieldsMap = array(
55 'USER_ID' => array(
56 'data_type' => 'integer',
57 'primary' => true
58 ),
59 'USER' => array(
60 'data_type' => 'Bitrix\Main\UserTable',
61 'reference' => array('=this.USER_ID' => 'ref.ID'),
62 ),
63 'RATING_TYPE_ID' => array(
64 'data_type' => 'string',
65 'primary' => true
66 ),
67 'RATING_ENTITY_ID' => array(
68 'data_type' => 'integer',
69 'primary' => true
70 ),
71 'CONTENT_ID' => array(
72 'data_type' => 'string'
73 ),
74 'DATE_VIEW' => array(
75 'data_type' => 'datetime'
76 ),
77 );
78
79 return $fieldsMap;
80 }
81
82 public static function set($params = array())
83 {
84 static $controllerUser = array();
85
86 $userId = (isset($params['userId']) ? intval($params['userId']) : 0);
87 $typeId = (isset($params['typeId']) ? trim($params['typeId']) : false);
88 $entityId = (isset($params['entityId']) ? intval($params['entityId']) : 0);
89 $save = (isset($params['save']) ? !!$params['save'] : false);
90
91 if (
92 $userId <= 0
93 || empty($typeId)
94 || $entityId <= 0
95 )
96 {
97 throw new SystemException("Invalid input data.");
98 }
99
100 $saved = false;
101
102 if (ModuleManager::isModuleInstalled('bitrix24'))
103 {
104 if (!isset($controllerUser[$userId]))
105 {
106 $res = UserTable::getList(array(
107 'filter' => array(
108 '=ID' => $userId,
109 '=EXTERNAL_AUTH_ID' => '__controller'
110 ),
111 'select' => array('ID')
112 ));
113 if ($res->fetch())
114 {
115 $controllerUser[$userId] = true;
116 }
117 else
118 {
119 $controllerUser[$userId] = false;
120 }
121 }
122
123 if ($controllerUser[$userId])
124 {
125 return array(
126 'success' => true,
127 'savedInDB' => false
128 );
129 }
130 }
131
132 if ($save)
133 {
134 $connection = \Bitrix\Main\Application::getConnection();
135 $helper = $connection->getSqlHelper();
136
137 $nowDate = new SqlExpression($helper->getCurrentDateTimeFunction());
138
139 $insertFields = array(
140 "USER_ID" => $userId,
141 "RATING_TYPE_ID" => $typeId,
142 "RATING_ENTITY_ID" => $entityId,
143 "CONTENT_ID" => $typeId."-".$entityId,
144 "DATE_VIEW" => $nowDate
145 );
146
147 $tableName = static::getTableName();
148 list($prefix, $values) = $helper->prepareInsert($tableName, $insertFields);
149
150 $connection->queryExecute(
151 "INSERT INTO {$tableName} ({$prefix}) VALUES ({$values})
152 ON DUPLICATE KEY UPDATE DATE_VIEW = {$nowDate}"
153 );
154
155 $saved = true;
156 }
157
158 return array(
159 'success' => true,
160 'savedInDB' => $saved
161 );
162 }
163
164 public static function add(array $data)
165 {
166 throw new NotImplementedException("Use set() method of the class.");
167 }
168
169 public static function update($primary, array $data)
170 {
171 throw new NotImplementedException("Use set() method of the class.");
172 }
173}