INFRA-132 - Batch #1
[civicrm-core.git] / CRM / Core / BAO / Log.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 /**
43 * @param int $id
44 * @param string $table
45 *
46 * @return array|null
47 */
48 public static function &lastModified($id, $table = 'civicrm_contact') {
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 /**
70 * Add log to civicrm_log table
71 *
72 * @param array $params
73 * Array of name-value pairs of log table.
74 *
75 */
76 public static function add(&$params) {
77
78 $log = new CRM_Core_DAO_Log();
79 $log->copyValues($params);
80 $log->save();
81 }
82
83 /**
84 * @param int $contactID
85 * @param string $tableName
86 * @param int $tableID
87 * @param int $userID
88 */
89 public static function register(
90 $contactID,
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
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
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) {
135 $log->entity_table = 'civicrm_contact';
136 $log->entity_id = $contactID;
137 $log->modified_id = $userID;
138 $log->modified_date = date("YmdHis");
139 $log->data = $logData;
140 $log->save();
141 }
142 else {
143 $query = "
144 UPDATE 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 /**
155 * Get log record count for a Contact
156 *
157 * @param int $contactID
158 *
159 * @return int
160 * count of log records
161 */
162 public static function getContactLogCount($contactID) {
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 /**
169 * Function for find out whether to use logging schema entries for contact
170 * summary, instead of normal log entries.
171 *
172 * @return int
173 * report id of Contact Logging Report (Summary) / false
174 */
175 public static function useLoggingReport() {
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();
187 CRM_Report_BAO_ReportInstance::retrieve($params, $instance);
188
189 if (!empty($instance) &&
190 (empty($instance['permission']) ||
191 (!empty($instance['permission']) && CRM_Core_Permission::check($instance['permission']))
192 )
193 ) {
194 return $instance['id'];
195 }
196 }
197
198 return FALSE;
199 }
200 }