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