Import from SVN (r45945, r596)
[civicrm-core.git] / CRM / Report / Form / Contact / Log.php
1 <?php
2 // $Id$
3
4 /*
5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
11 | |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
20 | |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
28 */
29
30 /**
31 *
32 * @package CRM
33 * @copyright CiviCRM LLC (c) 2004-2013
34 * $Id$
35 *
36 */
37 class CRM_Report_Form_Contact_Log extends CRM_Report_Form {
38
39 protected $_summary = NULL;
40 function __construct() {
41
42 $this->activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE);
43 asort($this->activityTypes);
44
45 $this->_columns = array(
46 'civicrm_contact' =>
47 array(
48 'dao' => 'CRM_Contact_DAO_Contact',
49 'fields' =>
50 array(
51 'sort_name' =>
52 array('title' => ts('Modified By'),
53 'required' => TRUE,
54 ),
55 'id' =>
56 array(
57 'no_display' => TRUE,
58 'required' => TRUE,
59 ),
60 ),
61 'filters' =>
62 array(
63 'sort_name' =>
64 array('title' => ts('Modified By'),
65 'type' => CRM_Utils_Type::T_STRING,
66 ),
67 ),
68 'grouping' => 'contact-fields',
69 ),
70 'civicrm_contact_touched' =>
71 array(
72 'dao' => 'CRM_Contact_DAO_Contact',
73 'fields' =>
74 array(
75 'sort_name_touched' =>
76 array('title' => ts('Touched Contact'),
77 'name' => 'sort_name',
78 'required' => TRUE,
79 ),
80 'id' =>
81 array(
82 'no_display' => TRUE,
83 'required' => TRUE,
84 ),
85 ),
86 'filters' =>
87 array(
88 'sort_name_touched' =>
89 array('title' => ts('Touched Contact'),
90 'name' => 'sort_name',
91 'type' => CRM_Utils_Type::T_STRING,
92 ),
93 ),
94 'grouping' => 'contact-fields',
95 ),
96 'civicrm_activity' =>
97 array(
98 'dao' => 'CRM_Activity_DAO_Activity',
99 'fields' =>
100 array('id' => array('title' => ts('Activity ID'),
101 'no_display' => TRUE,
102 'required' => TRUE,
103 ),
104 'subject' => array('title' => ts('Touched Activity'),
105 'required' => TRUE,
106 ),
107 'activity_type_id' => array('title' => ts('Activity Type'),
108 'required' => TRUE,
109 ),
110 'source_contact_id' => array(
111 'no_display' => TRUE,
112 'required' => TRUE,
113 ),
114 ),
115 ),
116 'civicrm_log' =>
117 array(
118 'dao' => 'CRM_Core_DAO_Log',
119 'fields' =>
120 array(
121 'modified_date' =>
122 array('title' => ts('Modified Date'),
123 'required' => TRUE,
124 ),
125 'data' =>
126 array('title' => ts('Description'),
127 ),
128 ),
129 'filters' =>
130 array(
131 'modified_date' =>
132 array('title' => ts('Modified Date'),
133 'operatorType' => CRM_Report_Form::OP_DATE,
134 'type' => CRM_Utils_Type::T_DATE,
135 'default' => 'this.week',
136 ),
137 ),
138 ),
139 );
140
141 parent::__construct();
142 }
143
144 function preProcess() {
145 parent::preProcess();
146 }
147
148 function select() {
149 $select = array();
150 $this->_columnHeaders = array();
151 foreach ($this->_columns as $tableName => $table) {
152 if (array_key_exists('fields', $table)) {
153 foreach ($table['fields'] as $fieldName => $field) {
154 if (CRM_Utils_Array::value('required', $field) ||
155 CRM_Utils_Array::value($fieldName, $this->_params['fields'])
156 ) {
157
158 $select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
159 $this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = CRM_Utils_Array::value('type', $field);
160 $this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
161 }
162 }
163 }
164 }
165
166 $this->_select = "SELECT " . implode(', ', $select) . " ";
167 }
168
169 static function formRule($fields, $files, $self) {
170 $errors = $grouping = array();
171 return $errors;
172 }
173
174 function from() {
175 $this->_from = "
176 FROM civicrm_log {$this->_aliases['civicrm_log']}
177 inner join civicrm_contact {$this->_aliases['civicrm_contact']} on {$this->_aliases['civicrm_log']}.modified_id = {$this->_aliases['civicrm_contact']}.id
178 left join civicrm_contact {$this->_aliases['civicrm_contact_touched']} on ({$this->_aliases['civicrm_log']}.entity_table='civicrm_contact' AND {$this->_aliases['civicrm_log']}.entity_id = {$this->_aliases['civicrm_contact_touched']}.id)
179 left join civicrm_activity {$this->_aliases['civicrm_activity']} on ({$this->_aliases['civicrm_log']}.entity_table='civicrm_activity' AND {$this->_aliases['civicrm_log']}.entity_id = {$this->_aliases['civicrm_activity']}.id)
180 ";
181 }
182
183 function where() {
184 $clauses = array();
185 $this->_having = '';
186 foreach ($this->_columns as $tableName => $table) {
187 if (array_key_exists('filters', $table)) {
188 foreach ($table['filters'] as $fieldName => $field) {
189 $clause = NULL;
190 if (CRM_Utils_Array::value('operatorType', $field) & CRM_Report_Form::OP_DATE) {
191 $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params);
192 $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params);
193 $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params);
194
195 $clause = $this->dateClause($field['dbAlias'], $relative, $from, $to);
196 }
197 else {
198 $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params);
199 if ($op) {
200 $clause = $this->whereClause($field,
201 $op,
202 CRM_Utils_Array::value("{$fieldName}_value", $this->_params),
203 CRM_Utils_Array::value("{$fieldName}_min", $this->_params),
204 CRM_Utils_Array::value("{$fieldName}_max", $this->_params)
205 );
206 }
207 }
208
209 if (!empty($clause)) {
210 $clauses[] = $clause;
211 }
212 }
213 }
214 }
215
216 $clauses[] = "({$this->_aliases['civicrm_log']}.entity_table <> 'civicrm_domain')";
217 $this->_where = "WHERE " . implode(' AND ', $clauses);
218 }
219
220 function orderBy() {
221 $this->_orderBy = "
222 ORDER BY {$this->_aliases['civicrm_log']}.modified_date DESC, {$this->_aliases['civicrm_contact']}.sort_name, {$this->_aliases['civicrm_contact_touched']}.sort_name
223 ";
224 }
225
226 function alterDisplay(&$rows) {
227 // custom code to alter rows
228 $entryFound = FALSE;
229 foreach ($rows as $rowNum => $row) {
230 // convert display name to links
231 if (array_key_exists('civicrm_contact_sort_name', $row) &&
232 array_key_exists('civicrm_contact_id', $row)
233 ) {
234 $url = CRM_Utils_System::url('civicrm/contact/view',
235 'reset=1&cid=' . $row['civicrm_contact_id'],
236 $this->_absoluteUrl
237 );
238 $rows[$rowNum]['civicrm_contact_sort_name_link'] = $url;
239 $rows[$rowNum]['civicrm_contact_sort_name_hover'] = ts("View Contact details for this contact.");
240 $entryFound = TRUE;
241 }
242
243 if (array_key_exists('civicrm_contact_touched_sort_name_touched', $row) &&
244 array_key_exists('civicrm_contact_touched_id', $row) &&
245 $row['civicrm_contact_touched_sort_name_touched'] !== ''
246 ) {
247 $url = CRM_Utils_System::url('civicrm/contact/view',
248 'reset=1&cid=' . $row['civicrm_contact_touched_id'],
249 $this->_absoluteUrl
250 );
251 $rows[$rowNum]['civicrm_contact_touched_sort_name_touched_link'] = $url;
252 $rows[$rowNum]['civicrm_contact_touched_sort_name_touched_hover'] = ts("View Contact details for this contact.");
253 $entryFound = TRUE;
254 }
255
256 if (array_key_exists('civicrm_activity_subject', $row) &&
257 array_key_exists('civicrm_activity_id', $row) &&
258 $row['civicrm_activity_subject'] !== ''
259 ) {
260 $url = CRM_Utils_System::url('civicrm/contact/view/activity',
261 'reset=1&action=view&id=' . $row['civicrm_activity_id'] . '&cid=' . $row['civicrm_activity_source_contact_id'] . '&atype=' . $row['civicrm_activity_activity_type_id'],
262 $this->_absoluteUrl
263 );
264 $rows[$rowNum]['civicrm_activity_subject_link'] = $url;
265 $rows[$rowNum]['civicrm_activity_subject_hover'] = ts("View Contact details for this contact.");
266 $entryFound = TRUE;
267 }
268
269 if (array_key_exists('civicrm_activity_activity_type_id', $row)) {
270 if ($value = $row['civicrm_activity_activity_type_id']) {
271 $rows[$rowNum]['civicrm_activity_activity_type_id'] = $this->activityTypes[$value];
272 }
273 $entryFound = TRUE;
274 }
275
276 // skip looking further in rows, if first row itself doesn't
277 // have the column we need
278 if (!$entryFound) {
279 break;
280 }
281 }
282 }
283 }
284