Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
19 | * BAO object for crm_log table | |
20 | */ | |
21 | class CRM_Core_BAO_Log extends CRM_Core_DAO_Log { | |
518fa0ee | 22 | public static $_processed = NULL; |
6a488035 | 23 | |
b5c2afd0 | 24 | /** |
100fef9d | 25 | * @param int $id |
b5c2afd0 EM |
26 | * @param string $table |
27 | * | |
28 | * @return array|null | |
4c7ef0e6 | 29 | * |
b5c2afd0 | 30 | */ |
00be9182 | 31 | public static function &lastModified($id, $table = 'civicrm_contact') { |
6a488035 TO |
32 | |
33 | $log = new CRM_Core_DAO_Log(); | |
34 | ||
35 | $log->entity_table = $table; | |
36 | $log->entity_id = $id; | |
37 | $log->orderBy('modified_date desc'); | |
38 | $log->limit(1); | |
4c7ef0e6 | 39 | $displayName = $result = $contactImage = NULL; |
6a488035 | 40 | if ($log->find(TRUE)) { |
4c7ef0e6 RT |
41 | if ($log->modified_id) { |
42 | list($displayName, $contactImage) = CRM_Contact_BAO_Contact::getDisplayAndImage($log->modified_id); | |
43 | } | |
be2fb01f | 44 | $result = [ |
6a488035 TO |
45 | 'id' => $log->modified_id, |
46 | 'name' => $displayName, | |
47 | 'image' => $contactImage, | |
48 | 'date' => $log->modified_date, | |
be2fb01f | 49 | ]; |
6a488035 TO |
50 | } |
51 | return $result; | |
52 | } | |
53 | ||
54 | /** | |
fe482240 | 55 | * Add log to civicrm_log table. |
6a488035 | 56 | * |
6a0b768e TO |
57 | * @param array $params |
58 | * Array of name-value pairs of log table. | |
6a488035 | 59 | * |
6a488035 | 60 | */ |
00be9182 | 61 | public static function add(&$params) { |
6a488035 TO |
62 | |
63 | $log = new CRM_Core_DAO_Log(); | |
64 | $log->copyValues($params); | |
65 | $log->save(); | |
66 | } | |
67 | ||
b5c2afd0 | 68 | /** |
100fef9d CW |
69 | * @param int $contactID |
70 | * @param string $tableName | |
71 | * @param int $tableID | |
72 | * @param int $userID | |
b5c2afd0 | 73 | */ |
971d41b1 | 74 | public static function register( |
f9f40af3 | 75 | $contactID, |
6a488035 TO |
76 | $tableName, |
77 | $tableID, | |
78 | $userID = NULL | |
79 | ) { | |
80 | if (!self::$_processed) { | |
be2fb01f | 81 | self::$_processed = []; |
6a488035 TO |
82 | } |
83 | ||
84 | if (!$userID) { | |
85 | $session = CRM_Core_Session::singleton(); | |
86 | $userID = $session->get('userID'); | |
87 | } | |
88 | ||
5bd56e68 C |
89 | if (!$userID) { |
90 | $api_key = CRM_Utils_Request::retrieve('api_key', 'String', $store, FALSE, NULL, 'REQUEST'); | |
91 | ||
92 | if ($api_key && strtolower($api_key) != 'null') { | |
93 | $userID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $api_key, 'id', 'api_key'); | |
94 | } | |
95 | } | |
96 | ||
6a488035 TO |
97 | if (!$userID) { |
98 | $userID = $contactID; | |
99 | } | |
100 | ||
101 | if (!$userID) { | |
102 | return; | |
103 | } | |
104 | ||
105 | $log = new CRM_Core_DAO_Log(); | |
106 | $log->id = NULL; | |
107 | ||
108 | if (isset(self::$_processed[$contactID])) { | |
109 | if (isset(self::$_processed[$contactID][$userID])) { | |
110 | $log->id = self::$_processed[$contactID][$userID]; | |
111 | } | |
112 | self::$_processed[$contactID][$userID] = 1; | |
113 | } | |
114 | else { | |
be2fb01f | 115 | self::$_processed[$contactID] = [$userID => 1]; |
6a488035 TO |
116 | } |
117 | ||
118 | $logData = "$tableName,$tableID"; | |
119 | if (!$log->id) { | |
353ffa53 TO |
120 | $log->entity_table = 'civicrm_contact'; |
121 | $log->entity_id = $contactID; | |
122 | $log->modified_id = $userID; | |
6a488035 | 123 | $log->modified_date = date("YmdHis"); |
353ffa53 | 124 | $log->data = $logData; |
6a488035 TO |
125 | $log->save(); |
126 | } | |
127 | else { | |
128 | $query = " | |
129 | UPDATE civicrm_log | |
130 | SET data = concat( data, ':$logData' ) | |
131 | WHERE id = {$log->id} | |
132 | "; | |
133 | CRM_Core_DAO::executeQuery($query); | |
134 | } | |
135 | ||
136 | self::$_processed[$contactID][$userID] = $log->id; | |
137 | } | |
138 | ||
139 | /** | |
fe482240 | 140 | * Get log record count for a Contact. |
6a488035 | 141 | * |
c490a46a | 142 | * @param int $contactID |
6a488035 | 143 | * |
a6c01b45 CW |
144 | * @return int |
145 | * count of log records | |
6a488035 | 146 | */ |
00be9182 | 147 | public static function getContactLogCount($contactID) { |
6a488035 TO |
148 | $query = "SELECT count(*) FROM civicrm_log |
149 | WHERE civicrm_log.entity_table = 'civicrm_contact' AND civicrm_log.entity_id = {$contactID}"; | |
150 | return CRM_Core_DAO::singleValueQuery($query); | |
151 | } | |
152 | ||
153 | /** | |
344b05bc | 154 | * Get the id of the report to use to display the change log. |
6a488035 | 155 | * |
344b05bc | 156 | * If logging is not enabled a return value of FALSE means to use the |
157 | * basic change log view. | |
158 | * | |
159 | * @return int|FALSE | |
160 | * report id of Contact Logging Report (Summary) | |
6a488035 | 161 | */ |
00be9182 | 162 | public static function useLoggingReport() { |
344b05bc | 163 | if (!\Civi::settings()->get('logging')) { |
6a488035 TO |
164 | return FALSE; |
165 | } | |
166 | ||
167 | $loggingSchema = new CRM_Logging_Schema(); | |
168 | ||
169 | if ($loggingSchema->isEnabled()) { | |
be2fb01f CW |
170 | $params = ['report_id' => 'logging/contact/summary']; |
171 | $instance = []; | |
0b25329b | 172 | CRM_Report_BAO_ReportInstance::retrieve($params, $instance); |
6a488035 TO |
173 | |
174 | if (!empty($instance) && | |
8cc574cf CW |
175 | (empty($instance['permission']) || |
176 | (!empty($instance['permission']) && CRM_Core_Permission::check($instance['permission'])) | |
6a488035 TO |
177 | ) |
178 | ) { | |
179 | return $instance['id']; | |
180 | } | |
181 | } | |
182 | ||
183 | return FALSE; | |
184 | } | |
96025800 | 185 | |
6a488035 | 186 | } |