Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
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 | ||
5bd56e68 C |
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 | ||
6a488035 TO |
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 int $contactId Contact ID | |
145 | * | |
146 | * @return int count of log records | |
147 | * @access public | |
148 | * @static | |
149 | */ | |
150 | static function getContactLogCount($contactID) { | |
151 | $query = "SELECT count(*) FROM civicrm_log | |
152 | WHERE civicrm_log.entity_table = 'civicrm_contact' AND civicrm_log.entity_id = {$contactID}"; | |
153 | return CRM_Core_DAO::singleValueQuery($query); | |
154 | } | |
155 | ||
156 | /** | |
157 | * Function for find out whether to use logging schema entries for contact | |
158 | * summary, instead of normal log entries. | |
159 | * | |
160 | * @return int report id of Contact Logging Report (Summary) / false | |
161 | * @access public | |
162 | * @static | |
163 | */ | |
164 | static function useLoggingReport() { | |
165 | // first check if logging is enabled | |
166 | $config = CRM_Core_Config::singleton(); | |
167 | if (!$config->logging) { | |
168 | return FALSE; | |
169 | } | |
170 | ||
171 | $loggingSchema = new CRM_Logging_Schema(); | |
172 | ||
173 | if ($loggingSchema->isEnabled()) { | |
174 | $params = array('report_id' => 'logging/contact/summary'); | |
175 | $instance = array(); | |
0b25329b | 176 | CRM_Report_BAO_ReportInstance::retrieve($params, $instance); |
6a488035 TO |
177 | |
178 | if (!empty($instance) && | |
8cc574cf CW |
179 | (empty($instance['permission']) || |
180 | (!empty($instance['permission']) && CRM_Core_Permission::check($instance['permission'])) | |
6a488035 TO |
181 | ) |
182 | ) { | |
183 | return $instance['id']; | |
184 | } | |
185 | } | |
186 | ||
187 | return FALSE; | |
188 | } | |
189 | } | |
190 |