Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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-2013 | |
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 | $userID = $contactID; | |
93 | } | |
94 | ||
95 | if (!$userID) { | |
96 | return; | |
97 | } | |
98 | ||
99 | $log = new CRM_Core_DAO_Log(); | |
100 | $log->id = NULL; | |
101 | ||
102 | if (isset(self::$_processed[$contactID])) { | |
103 | if (isset(self::$_processed[$contactID][$userID])) { | |
104 | $log->id = self::$_processed[$contactID][$userID]; | |
105 | } | |
106 | self::$_processed[$contactID][$userID] = 1; | |
107 | } | |
108 | else { | |
109 | self::$_processed[$contactID] = array($userID => 1); | |
110 | } | |
111 | ||
112 | $logData = "$tableName,$tableID"; | |
113 | if (!$log->id) { | |
114 | $log->entity_table = 'civicrm_contact'; | |
115 | $log->entity_id = $contactID; | |
116 | $log->modified_id = $userID; | |
117 | $log->modified_date = date("YmdHis"); | |
118 | $log->data = $logData; | |
119 | $log->save(); | |
120 | } | |
121 | else { | |
122 | $query = " | |
123 | UPDATE civicrm_log | |
124 | SET data = concat( data, ':$logData' ) | |
125 | WHERE id = {$log->id} | |
126 | "; | |
127 | CRM_Core_DAO::executeQuery($query); | |
128 | } | |
129 | ||
130 | self::$_processed[$contactID][$userID] = $log->id; | |
131 | } | |
132 | ||
133 | /** | |
134 | * Function to get log record count for a Contact | |
135 | * | |
136 | * @param int $contactId Contact ID | |
137 | * | |
138 | * @return int count of log records | |
139 | * @access public | |
140 | * @static | |
141 | */ | |
142 | static function getContactLogCount($contactID) { | |
143 | $query = "SELECT count(*) FROM civicrm_log | |
144 | WHERE civicrm_log.entity_table = 'civicrm_contact' AND civicrm_log.entity_id = {$contactID}"; | |
145 | return CRM_Core_DAO::singleValueQuery($query); | |
146 | } | |
147 | ||
148 | /** | |
149 | * Function for find out whether to use logging schema entries for contact | |
150 | * summary, instead of normal log entries. | |
151 | * | |
152 | * @return int report id of Contact Logging Report (Summary) / false | |
153 | * @access public | |
154 | * @static | |
155 | */ | |
156 | static function useLoggingReport() { | |
157 | // first check if logging is enabled | |
158 | $config = CRM_Core_Config::singleton(); | |
159 | if (!$config->logging) { | |
160 | return FALSE; | |
161 | } | |
162 | ||
163 | $loggingSchema = new CRM_Logging_Schema(); | |
164 | ||
165 | if ($loggingSchema->isEnabled()) { | |
166 | $params = array('report_id' => 'logging/contact/summary'); | |
167 | $instance = array(); | |
168 | CRM_Report_BAO_Instance::retrieve($params, $instance); | |
169 | ||
170 | if (!empty($instance) && | |
171 | (!CRM_Utils_Array::value('permission', $instance) || | |
172 | (CRM_Utils_Array::value('permission', $instance) && CRM_Core_Permission::check($instance['permission'])) | |
173 | ) | |
174 | ) { | |
175 | return $instance['id']; | |
176 | } | |
177 | } | |
178 | ||
179 | return FALSE; | |
180 | } | |
181 | } | |
182 |