(NFC) (dev/core#878) Simplify copyright header (templates/*)
[civicrm-core.git] / CRM / Core / BAO / Log.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
f299f7db 6 | Copyright CiviCRM LLC (c) 2004-2020 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
ca5cec67 31 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
32 */
33
34/**
35 * BAO object for crm_log table
36 */
37class CRM_Core_BAO_Log extends CRM_Core_DAO_Log {
518fa0ee 38 public static $_processed = NULL;
6a488035 39
b5c2afd0 40 /**
100fef9d 41 * @param int $id
b5c2afd0
EM
42 * @param string $table
43 *
44 * @return array|null
4c7ef0e6 45 *
b5c2afd0 46 */
00be9182 47 public static function &lastModified($id, $table = 'civicrm_contact') {
6a488035
TO
48
49 $log = new CRM_Core_DAO_Log();
50
51 $log->entity_table = $table;
52 $log->entity_id = $id;
53 $log->orderBy('modified_date desc');
54 $log->limit(1);
4c7ef0e6 55 $displayName = $result = $contactImage = NULL;
6a488035 56 if ($log->find(TRUE)) {
4c7ef0e6
RT
57 if ($log->modified_id) {
58 list($displayName, $contactImage) = CRM_Contact_BAO_Contact::getDisplayAndImage($log->modified_id);
59 }
be2fb01f 60 $result = [
6a488035
TO
61 'id' => $log->modified_id,
62 'name' => $displayName,
63 'image' => $contactImage,
64 'date' => $log->modified_date,
be2fb01f 65 ];
6a488035
TO
66 }
67 return $result;
68 }
69
70 /**
fe482240 71 * Add log to civicrm_log table.
6a488035 72 *
6a0b768e
TO
73 * @param array $params
74 * Array of name-value pairs of log table.
6a488035 75 *
6a488035 76 */
00be9182 77 public static function add(&$params) {
6a488035
TO
78
79 $log = new CRM_Core_DAO_Log();
80 $log->copyValues($params);
81 $log->save();
82 }
83
b5c2afd0 84 /**
100fef9d
CW
85 * @param int $contactID
86 * @param string $tableName
87 * @param int $tableID
88 * @param int $userID
b5c2afd0 89 */
971d41b1 90 public static function register(
f9f40af3 91 $contactID,
6a488035
TO
92 $tableName,
93 $tableID,
94 $userID = NULL
95 ) {
96 if (!self::$_processed) {
be2fb01f 97 self::$_processed = [];
6a488035
TO
98 }
99
100 if (!$userID) {
101 $session = CRM_Core_Session::singleton();
102 $userID = $session->get('userID');
103 }
104
5bd56e68
C
105 if (!$userID) {
106 $api_key = CRM_Utils_Request::retrieve('api_key', 'String', $store, FALSE, NULL, 'REQUEST');
107
108 if ($api_key && strtolower($api_key) != 'null') {
109 $userID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $api_key, 'id', 'api_key');
110 }
111 }
112
6a488035
TO
113 if (!$userID) {
114 $userID = $contactID;
115 }
116
117 if (!$userID) {
118 return;
119 }
120
121 $log = new CRM_Core_DAO_Log();
122 $log->id = NULL;
123
124 if (isset(self::$_processed[$contactID])) {
125 if (isset(self::$_processed[$contactID][$userID])) {
126 $log->id = self::$_processed[$contactID][$userID];
127 }
128 self::$_processed[$contactID][$userID] = 1;
129 }
130 else {
be2fb01f 131 self::$_processed[$contactID] = [$userID => 1];
6a488035
TO
132 }
133
134 $logData = "$tableName,$tableID";
135 if (!$log->id) {
353ffa53
TO
136 $log->entity_table = 'civicrm_contact';
137 $log->entity_id = $contactID;
138 $log->modified_id = $userID;
6a488035 139 $log->modified_date = date("YmdHis");
353ffa53 140 $log->data = $logData;
6a488035
TO
141 $log->save();
142 }
143 else {
144 $query = "
145UPDATE civicrm_log
146 SET data = concat( data, ':$logData' )
147 WHERE id = {$log->id}
148";
149 CRM_Core_DAO::executeQuery($query);
150 }
151
152 self::$_processed[$contactID][$userID] = $log->id;
153 }
154
155 /**
fe482240 156 * Get log record count for a Contact.
6a488035 157 *
c490a46a 158 * @param int $contactID
6a488035 159 *
a6c01b45
CW
160 * @return int
161 * count of log records
6a488035 162 */
00be9182 163 public static function getContactLogCount($contactID) {
6a488035
TO
164 $query = "SELECT count(*) FROM civicrm_log
165 WHERE civicrm_log.entity_table = 'civicrm_contact' AND civicrm_log.entity_id = {$contactID}";
166 return CRM_Core_DAO::singleValueQuery($query);
167 }
168
169 /**
344b05bc 170 * Get the id of the report to use to display the change log.
6a488035 171 *
344b05bc 172 * If logging is not enabled a return value of FALSE means to use the
173 * basic change log view.
174 *
175 * @return int|FALSE
176 * report id of Contact Logging Report (Summary)
6a488035 177 */
00be9182 178 public static function useLoggingReport() {
344b05bc 179 if (!\Civi::settings()->get('logging')) {
6a488035
TO
180 return FALSE;
181 }
182
183 $loggingSchema = new CRM_Logging_Schema();
184
185 if ($loggingSchema->isEnabled()) {
be2fb01f
CW
186 $params = ['report_id' => 'logging/contact/summary'];
187 $instance = [];
0b25329b 188 CRM_Report_BAO_ReportInstance::retrieve($params, $instance);
6a488035
TO
189
190 if (!empty($instance) &&
8cc574cf
CW
191 (empty($instance['permission']) ||
192 (!empty($instance['permission']) && CRM_Core_Permission::check($instance['permission']))
6a488035
TO
193 )
194 ) {
195 return $instance['id'];
196 }
197 }
198
199 return FALSE;
200 }
96025800 201
6a488035 202}