Merge pull request #8494 from kcristiano/CRM-18221
[civicrm-core.git] / CRM / Core / BAO / Log.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
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
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * BAO object for crm_log table
38 */
39class CRM_Core_BAO_Log extends CRM_Core_DAO_Log {
40 static $_processed = NULL;
41
b5c2afd0 42 /**
100fef9d 43 * @param int $id
b5c2afd0
EM
44 * @param string $table
45 *
46 * @return array|null
47 */
00be9182 48 public static function &lastModified($id, $table = 'civicrm_contact') {
6a488035
TO
49
50 $log = new CRM_Core_DAO_Log();
51
52 $log->entity_table = $table;
53 $log->entity_id = $id;
54 $log->orderBy('modified_date desc');
55 $log->limit(1);
56 $result = CRM_Core_DAO::$_nullObject;
57 if ($log->find(TRUE)) {
58 list($displayName, $contactImage) = CRM_Contact_BAO_Contact::getDisplayAndImage($log->modified_id);
59 $result = array(
60 'id' => $log->modified_id,
61 'name' => $displayName,
62 'image' => $contactImage,
63 'date' => $log->modified_date,
64 );
65 }
66 return $result;
67 }
68
69 /**
fe482240 70 * Add log to civicrm_log table.
6a488035 71 *
6a0b768e
TO
72 * @param array $params
73 * Array of name-value pairs of log table.
6a488035 74 *
6a488035 75 */
00be9182 76 public static function add(&$params) {
6a488035
TO
77
78 $log = new CRM_Core_DAO_Log();
79 $log->copyValues($params);
80 $log->save();
81 }
82
b5c2afd0 83 /**
100fef9d
CW
84 * @param int $contactID
85 * @param string $tableName
86 * @param int $tableID
87 * @param int $userID
b5c2afd0 88 */
971d41b1 89 public static function register(
f9f40af3 90 $contactID,
6a488035
TO
91 $tableName,
92 $tableID,
93 $userID = NULL
94 ) {
95 if (!self::$_processed) {
96 self::$_processed = array();
97 }
98
99 if (!$userID) {
100 $session = CRM_Core_Session::singleton();
101 $userID = $session->get('userID');
102 }
103
5bd56e68
C
104 if (!$userID) {
105 $api_key = CRM_Utils_Request::retrieve('api_key', 'String', $store, FALSE, NULL, 'REQUEST');
106
107 if ($api_key && strtolower($api_key) != 'null') {
108 $userID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $api_key, 'id', 'api_key');
109 }
110 }
111
6a488035
TO
112 if (!$userID) {
113 $userID = $contactID;
114 }
115
116 if (!$userID) {
117 return;
118 }
119
120 $log = new CRM_Core_DAO_Log();
121 $log->id = NULL;
122
123 if (isset(self::$_processed[$contactID])) {
124 if (isset(self::$_processed[$contactID][$userID])) {
125 $log->id = self::$_processed[$contactID][$userID];
126 }
127 self::$_processed[$contactID][$userID] = 1;
128 }
129 else {
130 self::$_processed[$contactID] = array($userID => 1);
131 }
132
133 $logData = "$tableName,$tableID";
134 if (!$log->id) {
353ffa53
TO
135 $log->entity_table = 'civicrm_contact';
136 $log->entity_id = $contactID;
137 $log->modified_id = $userID;
6a488035 138 $log->modified_date = date("YmdHis");
353ffa53 139 $log->data = $logData;
6a488035
TO
140 $log->save();
141 }
142 else {
143 $query = "
144UPDATE civicrm_log
145 SET data = concat( data, ':$logData' )
146 WHERE id = {$log->id}
147";
148 CRM_Core_DAO::executeQuery($query);
149 }
150
151 self::$_processed[$contactID][$userID] = $log->id;
152 }
153
154 /**
fe482240 155 * Get log record count for a Contact.
6a488035 156 *
c490a46a 157 * @param int $contactID
6a488035 158 *
a6c01b45
CW
159 * @return int
160 * count of log records
6a488035 161 */
00be9182 162 public static function getContactLogCount($contactID) {
6a488035
TO
163 $query = "SELECT count(*) FROM civicrm_log
164 WHERE civicrm_log.entity_table = 'civicrm_contact' AND civicrm_log.entity_id = {$contactID}";
165 return CRM_Core_DAO::singleValueQuery($query);
166 }
167
168 /**
fe482240 169 * Function for find out whether to use logging schema entries for contact.
6a488035
TO
170 * summary, instead of normal log entries.
171 *
a6c01b45
CW
172 * @return int
173 * report id of Contact Logging Report (Summary) / false
6a488035 174 */
00be9182 175 public static function useLoggingReport() {
6a488035
TO
176 // first check if logging is enabled
177 $config = CRM_Core_Config::singleton();
178 if (!$config->logging) {
179 return FALSE;
180 }
181
182 $loggingSchema = new CRM_Logging_Schema();
183
184 if ($loggingSchema->isEnabled()) {
185 $params = array('report_id' => 'logging/contact/summary');
186 $instance = array();
0b25329b 187 CRM_Report_BAO_ReportInstance::retrieve($params, $instance);
6a488035
TO
188
189 if (!empty($instance) &&
8cc574cf
CW
190 (empty($instance['permission']) ||
191 (!empty($instance['permission']) && CRM_Core_Permission::check($instance['permission']))
6a488035
TO
192 )
193 ) {
194 return $instance['id'];
195 }
196 }
197
198 return FALSE;
199 }
96025800 200
6a488035 201}