Merge pull request #16526 from eileenmcnaughton/valid_calls
[civicrm-core.git] / CRM / Core / BAO / Log.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * BAO object for crm_log table
20 */
21 class CRM_Core_BAO_Log extends CRM_Core_DAO_Log {
22 public static $_processed = NULL;
23
24 /**
25 * @param int $id
26 * @param string $table
27 *
28 * @return array|null
29 *
30 */
31 public static function &lastModified($id, $table = 'civicrm_contact') {
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);
39 $displayName = $result = $contactImage = NULL;
40 if ($log->find(TRUE)) {
41 if ($log->modified_id) {
42 list($displayName, $contactImage) = CRM_Contact_BAO_Contact::getDisplayAndImage($log->modified_id);
43 }
44 $result = [
45 'id' => $log->modified_id,
46 'name' => $displayName,
47 'image' => $contactImage,
48 'date' => $log->modified_date,
49 ];
50 }
51 return $result;
52 }
53
54 /**
55 * Add log to civicrm_log table.
56 *
57 * @param array $params
58 * Array of name-value pairs of log table.
59 *
60 */
61 public static function add(&$params) {
62
63 $log = new CRM_Core_DAO_Log();
64 $log->copyValues($params);
65 $log->save();
66 }
67
68 /**
69 * @param int $contactID
70 * @param string $tableName
71 * @param int $tableID
72 * @param int $userID
73 *
74 * @throws \CRM_Core_Exception
75 */
76 public static function register(
77 $contactID,
78 $tableName,
79 $tableID,
80 $userID = NULL
81 ) {
82 if (!self::$_processed) {
83 self::$_processed = [];
84 }
85
86 if (!$userID) {
87 $session = CRM_Core_Session::singleton();
88 $userID = $session->get('userID');
89 }
90
91 if (!$userID) {
92 $api_key = CRM_Utils_Request::retrieve('api_key', 'String', $store, FALSE, NULL, 'REQUEST');
93
94 if ($api_key && strtolower($api_key) != 'null') {
95 $userID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $api_key, 'id', 'api_key');
96 }
97 }
98
99 if (!$userID) {
100 $userID = $contactID;
101 }
102
103 if (!$userID) {
104 return;
105 }
106
107 $log = new CRM_Core_DAO_Log();
108 $log->id = NULL;
109
110 if (isset(self::$_processed[$contactID])) {
111 if (isset(self::$_processed[$contactID][$userID])) {
112 $log->id = self::$_processed[$contactID][$userID];
113 }
114 self::$_processed[$contactID][$userID] = 1;
115 }
116 else {
117 self::$_processed[$contactID] = [$userID => 1];
118 }
119
120 $logData = "$tableName,$tableID";
121 if (!$log->id) {
122 $log->entity_table = 'civicrm_contact';
123 $log->entity_id = $contactID;
124 $log->modified_id = $userID;
125 $log->modified_date = date("YmdHis");
126 $log->data = $logData;
127 $log->save();
128 }
129 else {
130 $query = "
131 UPDATE civicrm_log
132 SET data = concat( data, ':$logData' )
133 WHERE id = {$log->id}
134 ";
135 CRM_Core_DAO::executeQuery($query);
136 }
137
138 self::$_processed[$contactID][$userID] = $log->id;
139 }
140
141 /**
142 * Get log record count for a Contact.
143 *
144 * @param int $contactID
145 *
146 * @return int
147 * count of log records
148 */
149 public static function getContactLogCount($contactID) {
150 $query = "SELECT count(*) FROM civicrm_log
151 WHERE civicrm_log.entity_table = 'civicrm_contact' AND civicrm_log.entity_id = {$contactID}";
152 return CRM_Core_DAO::singleValueQuery($query);
153 }
154
155 /**
156 * Get the id of the report to use to display the change log.
157 *
158 * If logging is not enabled a return value of FALSE means to use the
159 * basic change log view.
160 *
161 * @return int|FALSE
162 * report id of Contact Logging Report (Summary)
163 */
164 public static function useLoggingReport() {
165 if (!\Civi::settings()->get('logging')) {
166 return FALSE;
167 }
168
169 $loggingSchema = new CRM_Logging_Schema();
170
171 if ($loggingSchema->isEnabled()) {
172 $params = ['report_id' => 'logging/contact/summary'];
173 $instance = [];
174 CRM_Report_BAO_ReportInstance::retrieve($params, $instance);
175
176 if (!empty($instance) &&
177 (empty($instance['permission']) ||
178 (!empty($instance['permission']) && CRM_Core_Permission::check($instance['permission']))
179 )
180 ) {
181 return $instance['id'];
182 }
183 }
184
185 return FALSE;
186 }
187
188 }