| 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 | /** |
| 43 | * @param $id |
| 44 | * @param string $table |
| 45 | * |
| 46 | * @return array|null |
| 47 | */ |
| 48 | 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 array of name-value pairs of log table. |
| 73 | * |
| 74 | * @static |
| 75 | */ |
| 76 | static function add(&$params) { |
| 77 | |
| 78 | $log = new CRM_Core_DAO_Log(); |
| 79 | $log->copyValues($params); |
| 80 | $log->save(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @param $contactID |
| 85 | * @param $tableName |
| 86 | * @param $tableID |
| 87 | * @param null $userID |
| 88 | */ |
| 89 | static function register($contactID, |
| 90 | $tableName, |
| 91 | $tableID, |
| 92 | $userID = NULL |
| 93 | ) { |
| 94 | if (!self::$_processed) { |
| 95 | self::$_processed = array(); |
| 96 | } |
| 97 | |
| 98 | if (!$userID) { |
| 99 | $session = CRM_Core_Session::singleton(); |
| 100 | $userID = $session->get('userID'); |
| 101 | } |
| 102 | |
| 103 | if (!$userID) { |
| 104 | $api_key = CRM_Utils_Request::retrieve('api_key', 'String', $store, FALSE, NULL, 'REQUEST'); |
| 105 | |
| 106 | if ($api_key && strtolower($api_key) != 'null') { |
| 107 | $userID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $api_key, 'id', 'api_key'); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | if (!$userID) { |
| 112 | $userID = $contactID; |
| 113 | } |
| 114 | |
| 115 | if (!$userID) { |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | $log = new CRM_Core_DAO_Log(); |
| 120 | $log->id = NULL; |
| 121 | |
| 122 | if (isset(self::$_processed[$contactID])) { |
| 123 | if (isset(self::$_processed[$contactID][$userID])) { |
| 124 | $log->id = self::$_processed[$contactID][$userID]; |
| 125 | } |
| 126 | self::$_processed[$contactID][$userID] = 1; |
| 127 | } |
| 128 | else { |
| 129 | self::$_processed[$contactID] = array($userID => 1); |
| 130 | } |
| 131 | |
| 132 | $logData = "$tableName,$tableID"; |
| 133 | if (!$log->id) { |
| 134 | $log->entity_table = 'civicrm_contact'; |
| 135 | $log->entity_id = $contactID; |
| 136 | $log->modified_id = $userID; |
| 137 | $log->modified_date = date("YmdHis"); |
| 138 | $log->data = $logData; |
| 139 | $log->save(); |
| 140 | } |
| 141 | else { |
| 142 | $query = " |
| 143 | UPDATE civicrm_log |
| 144 | SET data = concat( data, ':$logData' ) |
| 145 | WHERE id = {$log->id} |
| 146 | "; |
| 147 | CRM_Core_DAO::executeQuery($query); |
| 148 | } |
| 149 | |
| 150 | self::$_processed[$contactID][$userID] = $log->id; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Function to get log record count for a Contact |
| 155 | * |
| 156 | * @param $contactID |
| 157 | * |
| 158 | * @internal param int $contactId Contact ID |
| 159 | * |
| 160 | * @return int count of log records |
| 161 | * @access public |
| 162 | * @static |
| 163 | */ |
| 164 | static function getContactLogCount($contactID) { |
| 165 | $query = "SELECT count(*) FROM civicrm_log |
| 166 | WHERE civicrm_log.entity_table = 'civicrm_contact' AND civicrm_log.entity_id = {$contactID}"; |
| 167 | return CRM_Core_DAO::singleValueQuery($query); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Function for find out whether to use logging schema entries for contact |
| 172 | * summary, instead of normal log entries. |
| 173 | * |
| 174 | * @return int report id of Contact Logging Report (Summary) / false |
| 175 | * @access public |
| 176 | * @static |
| 177 | */ |
| 178 | static function useLoggingReport() { |
| 179 | // first check if logging is enabled |
| 180 | $config = CRM_Core_Config::singleton(); |
| 181 | if (!$config->logging) { |
| 182 | return FALSE; |
| 183 | } |
| 184 | |
| 185 | $loggingSchema = new CRM_Logging_Schema(); |
| 186 | |
| 187 | if ($loggingSchema->isEnabled()) { |
| 188 | $params = array('report_id' => 'logging/contact/summary'); |
| 189 | $instance = array(); |
| 190 | CRM_Report_BAO_ReportInstance::retrieve($params, $instance); |
| 191 | |
| 192 | if (!empty($instance) && |
| 193 | (empty($instance['permission']) || |
| 194 | (!empty($instance['permission']) && CRM_Core_Permission::check($instance['permission'])) |
| 195 | ) |
| 196 | ) { |
| 197 | return $instance['id']; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | return FALSE; |
| 202 | } |
| 203 | } |
| 204 | |