Merge pull request #3223 from lcdservices/CRM-14672
[civicrm-core.git] / CRM / Core / BAO / Log.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * BAO object for crm_log table
38 */
39 class CRM_Core_BAO_Log extends CRM_Core_DAO_Log {
40 static $_processed = NULL;
41
42 static function &lastModified($id, $table = 'civicrm_contact') {
43
44 $log = new CRM_Core_DAO_Log();
45
46 $log->entity_table = $table;
47 $log->entity_id = $id;
48 $log->orderBy('modified_date desc');
49 $log->limit(1);
50 $result = CRM_Core_DAO::$_nullObject;
51 if ($log->find(TRUE)) {
52 list($displayName, $contactImage) = CRM_Contact_BAO_Contact::getDisplayAndImage($log->modified_id);
53 $result = array(
54 'id' => $log->modified_id,
55 'name' => $displayName,
56 'image' => $contactImage,
57 'date' => $log->modified_date,
58 );
59 }
60 return $result;
61 }
62
63 /**
64 * add log to civicrm_log table
65 *
66 * @param array $params array of name-value pairs of log table.
67 *
68 * @static
69 */
70 static function add(&$params) {
71
72 $log = new CRM_Core_DAO_Log();
73 $log->copyValues($params);
74 $log->save();
75 }
76
77 static function register($contactID,
78 $tableName,
79 $tableID,
80 $userID = NULL
81 ) {
82 if (!self::$_processed) {
83 self::$_processed = array();
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] = array($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 * Function to get log record count for a Contact
143 *
144 * @param $contactID
145 *
146 * @internal param int $contactId Contact ID
147 *
148 * @return int count of log records
149 * @access public
150 * @static
151 */
152 static function getContactLogCount($contactID) {
153 $query = "SELECT count(*) FROM civicrm_log
154 WHERE civicrm_log.entity_table = 'civicrm_contact' AND civicrm_log.entity_id = {$contactID}";
155 return CRM_Core_DAO::singleValueQuery($query);
156 }
157
158 /**
159 * Function for find out whether to use logging schema entries for contact
160 * summary, instead of normal log entries.
161 *
162 * @return int report id of Contact Logging Report (Summary) / false
163 * @access public
164 * @static
165 */
166 static function useLoggingReport() {
167 // first check if logging is enabled
168 $config = CRM_Core_Config::singleton();
169 if (!$config->logging) {
170 return FALSE;
171 }
172
173 $loggingSchema = new CRM_Logging_Schema();
174
175 if ($loggingSchema->isEnabled()) {
176 $params = array('report_id' => 'logging/contact/summary');
177 $instance = array();
178 CRM_Report_BAO_ReportInstance::retrieve($params, $instance);
179
180 if (!empty($instance) &&
181 (empty($instance['permission']) ||
182 (!empty($instance['permission']) && CRM_Core_Permission::check($instance['permission']))
183 )
184 ) {
185 return $instance['id'];
186 }
187 }
188
189 return FALSE;
190 }
191 }
192