Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
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 | class CRM_Report_Form_Activity extends CRM_Report_Form { | |
1f220d30 | 36 | protected $_selectAliasesTotal = array(); |
6a488035 TO |
37 | |
38 | protected $_customGroupExtends = array( | |
430ae6dd TO |
39 | 'Activity' |
40 | ); | |
41 | ||
09febbae | 42 | protected $_nonDisplayFields = array(); |
43 | ||
430ae6dd | 44 | function __construct() { |
7116d719 | 45 | // There could be multiple contacts. We not clear on which contact id to display. |
83e74860 DS |
46 | // Lets hide it for now. |
47 | $this->_exposeContactID = FALSE; | |
48 | ||
6a488035 TO |
49 | $config = CRM_Core_Config::singleton(); |
50 | $campaignEnabled = in_array("CiviCampaign", $config->enableComponents); | |
51 | if ($campaignEnabled) { | |
52 | $getCampaigns = CRM_Campaign_BAO_Campaign::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE); | |
53 | $this->activeCampaigns = $getCampaigns['campaigns']; | |
54 | asort($this->activeCampaigns); | |
55 | $this->engagementLevels = CRM_Campaign_PseudoConstant::engagementLevel(); | |
56 | } | |
57 | $this->activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'label', TRUE); | |
58 | asort($this->activityTypes); | |
59 | ||
60 | $this->_columns = array( | |
61 | 'civicrm_contact' => | |
62 | array( | |
63 | 'dao' => 'CRM_Contact_DAO_Contact', | |
64 | 'fields' => | |
65 | array( | |
6a488035 TO |
66 | 'contact_source' => |
67 | array( | |
68 | 'name' => 'sort_name', | |
69 | 'title' => ts('Source Contact Name'), | |
70 | 'alias' => 'civicrm_contact_source', | |
71 | 'no_repeat' => TRUE, | |
72 | ), | |
73 | 'contact_assignee' => | |
74 | array( | |
75 | 'name' => 'sort_name', | |
76 | 'title' => ts('Assignee Contact Name'), | |
77 | 'alias' => 'civicrm_contact_assignee', | |
1f220d30 | 78 | 'dbAlias' => "civicrm_contact_assignee.sort_name", |
6a488035 TO |
79 | 'default' => TRUE, |
80 | ), | |
81 | 'contact_target' => | |
82 | array( | |
83 | 'name' => 'sort_name', | |
84 | 'title' => ts('Target Contact Name'), | |
2f4b4d54 | 85 | 'alias' => 'civicrm_contact_target', |
1f220d30 | 86 | 'dbAlias' => "civicrm_contact_target.sort_name", |
6a488035 TO |
87 | 'default' => TRUE, |
88 | ), | |
482691e1 RN |
89 | 'contact_source_id' => |
90 | array( | |
91 | 'name' => 'id', | |
92 | 'alias' => 'civicrm_contact_source', | |
93 | 'dbAlias' => "civicrm_contact_source.id", | |
94 | 'no_display' => TRUE, | |
95 | 'default' => TRUE, | |
96 | 'required' => TRUE, | |
97 | ), | |
98 | 'contact_assignee_id' => | |
99 | array( | |
100 | 'name' => 'id', | |
101 | 'alias' => 'civicrm_contact_assignee', | |
102 | 'dbAlias' => "civicrm_contact_assignee.id", | |
103 | 'no_display' => TRUE, | |
104 | 'default' => TRUE, | |
105 | 'required' => TRUE, | |
106 | ), | |
107 | 'contact_target_id' => | |
108 | array( | |
109 | 'name' => 'id', | |
110 | 'alias' => 'civicrm_contact_target', | |
111 | 'dbAlias' => "civicrm_contact_target.id", | |
112 | 'no_display' => TRUE, | |
113 | 'default' => TRUE, | |
114 | 'required' => TRUE, | |
115 | ), | |
6a488035 TO |
116 | ), |
117 | 'filters' => | |
118 | array( | |
119 | 'contact_source' => | |
120 | array( | |
121 | 'name' => 'sort_name', | |
122 | 'alias' => 'civicrm_contact_source', | |
123 | 'title' => ts('Source Contact Name'), | |
124 | 'operator' => 'like', | |
125 | 'type' => CRM_Report_Form::OP_STRING, | |
126 | ), | |
127 | 'contact_assignee' => | |
128 | array( | |
129 | 'name' => 'sort_name', | |
130 | 'alias' => 'civicrm_contact_assignee', | |
131 | 'title' => ts('Assignee Contact Name'), | |
132 | 'operator' => 'like', | |
133 | 'type' => CRM_Report_Form::OP_STRING, | |
134 | ), | |
135 | 'contact_target' => | |
136 | array( | |
137 | 'name' => 'sort_name', | |
2f4b4d54 | 138 | 'alias' => 'civicrm_contact_target', |
6a488035 TO |
139 | 'title' => ts('Target Contact Name'), |
140 | 'operator' => 'like', | |
141 | 'type' => CRM_Report_Form::OP_STRING, | |
142 | ), | |
143 | 'current_user' => | |
144 | array( | |
145 | 'name' => 'current_user', | |
146 | 'title' => ts('Limit To Current User'), | |
147 | 'type' => CRM_Utils_Type::T_INT, | |
148 | 'operatorType' => CRM_Report_Form::OP_SELECT, | |
149 | 'options' => array('0' => ts('No'), '1' => ts('Yes')), | |
150 | ), | |
151 | ), | |
152 | 'grouping' => 'contact-fields', | |
153 | ), | |
154 | 'civicrm_email' => | |
155 | array( | |
156 | 'dao' => 'CRM_Core_DAO_Email', | |
157 | 'fields' => | |
158 | array( | |
159 | 'contact_source_email' => | |
160 | array( | |
161 | 'name' => 'email', | |
162 | 'title' => ts('Source Contact Email'), | |
163 | 'alias' => 'civicrm_email_source', | |
164 | ), | |
165 | 'contact_assignee_email' => | |
166 | array( | |
167 | 'name' => 'email', | |
168 | 'title' => ts('Assignee Contact Email'), | |
169 | 'alias' => 'civicrm_email_assignee', | |
170 | ), | |
171 | 'contact_target_email' => | |
172 | array( | |
173 | 'name' => 'email', | |
174 | 'title' => ts('Target Contact Email'), | |
175 | 'alias' => 'civicrm_email_target', | |
176 | ), | |
177 | ), | |
178 | 'order_bys' => | |
179 | array( | |
180 | 'source_contact_email' => | |
181 | array( | |
182 | 'name' => 'email', | |
183 | 'title' => ts('Source Contact Email'), | |
83e74860 | 184 | 'dbAlias' => 'civicrm_email_contact_source_email', |
6a488035 TO |
185 | ), |
186 | ), | |
187 | ), | |
188 | 'civicrm_activity' => | |
189 | array( | |
190 | 'dao' => 'CRM_Activity_DAO_Activity', | |
191 | 'fields' => | |
192 | array( | |
193 | 'id' => | |
194 | array( | |
83e74860 | 195 | 'no_display' => TRUE, |
2f4b4d54 | 196 | 'title' => ts('Activity ID'), |
1f220d30 | 197 | 'required' => TRUE, |
6a488035 TO |
198 | ), |
199 | 'source_record_id' => | |
200 | array( | |
201 | 'no_display' => TRUE, | |
202 | 'required' => TRUE, | |
203 | ), | |
204 | 'activity_type_id' => | |
205 | array('title' => ts('Activity Type'), | |
7116d719 | 206 | 'required' => TRUE, |
6a488035 TO |
207 | 'type' => CRM_Utils_Type::T_STRING, |
208 | ), | |
209 | 'activity_subject' => | |
210 | array('title' => ts('Subject'), | |
211 | 'default' => TRUE, | |
212 | ), | |
6a488035 TO |
213 | 'activity_date_time' => |
214 | array('title' => ts('Activity Date'), | |
7116d719 | 215 | 'required' => TRUE, |
6a488035 TO |
216 | ), |
217 | 'status_id' => | |
218 | array('title' => ts('Activity Status'), | |
219 | 'default' => TRUE, | |
220 | 'type' => CRM_Utils_Type::T_STRING, | |
221 | ), | |
222 | 'duration' => | |
223 | array('title' => ts('Duration'), | |
224 | 'type' => CRM_Utils_Type::T_INT, | |
225 | ), | |
226 | ), | |
227 | 'filters' => | |
228 | array( | |
229 | 'activity_date_time' => | |
230 | array( | |
231 | 'default' => 'this.month', | |
232 | 'operatorType' => CRM_Report_Form::OP_DATE, | |
233 | ), | |
234 | 'activity_subject' => | |
235 | array('title' => ts('Activity Subject')), | |
236 | 'activity_type_id' => | |
237 | array('title' => ts('Activity Type'), | |
238 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
239 | 'options' => $this->activityTypes, | |
240 | ), | |
241 | 'status_id' => | |
242 | array('title' => ts('Activity Status'), | |
243 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
244 | 'options' => CRM_Core_PseudoConstant::activityStatus(), | |
245 | ), | |
246 | ), | |
247 | 'order_bys' => | |
248 | array( | |
6a488035 | 249 | 'activity_date_time' => |
83e74860 | 250 | array('title' => ts('Activity Date'), 'default_weight' => '1', 'dbAlias' => 'civicrm_activity_activity_date_time'), |
6a488035 | 251 | 'activity_type_id' => |
83e74860 | 252 | array('title' => ts('Activity Type'), 'default_weight' => '2', 'dbAlias' => 'civicrm_activity_activity_type_id'), |
6a488035 TO |
253 | ), |
254 | 'grouping' => 'activity-fields', | |
255 | 'alias' => 'activity', | |
256 | ), | |
1f220d30 | 257 | 'civicrm_activity_contact' => |
6a488035 | 258 | array( |
38ba593b | 259 | 'dao' => 'CRM_Activity_DAO_ActivityContact', |
6a488035 TO |
260 | 'fields' => |
261 | array( | |
1f220d30 | 262 | // so we have $this->_alias populated |
6a488035 | 263 | ), |
6a488035 | 264 | ), |
6a488035 TO |
265 | ) + $this->addAddressFields(FALSE, TRUE); |
266 | ||
267 | if ($campaignEnabled) { | |
268 | // Add display column and filter for Survey Results, Campaign and Engagement Index if CiviCampaign is enabled | |
269 | ||
270 | $this->_columns['civicrm_activity']['fields']['result'] = array( | |
271 | 'title' => 'Survey Result', | |
272 | 'default' => 'false', | |
273 | ); | |
274 | $this->_columns['civicrm_activity']['filters']['result'] = array('title' => ts('Survey Result'), | |
275 | 'operator' => 'like', | |
276 | 'type' => CRM_Utils_Type::T_STRING, | |
277 | ); | |
278 | if (!empty($this->activeCampaigns)) { | |
279 | $this->_columns['civicrm_activity']['fields']['campaign_id'] = array( | |
280 | 'title' => 'Campaign', | |
281 | 'default' => 'false', | |
282 | ); | |
283 | $this->_columns['civicrm_activity']['filters']['campaign_id'] = array('title' => ts('Campaign'), | |
284 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
285 | 'options' => $this->activeCampaigns, | |
286 | ); | |
287 | } | |
288 | if (!empty($this->engagementLevels)) { | |
289 | $this->_columns['civicrm_activity']['fields']['engagement_level'] = array( | |
290 | 'title' => 'Engagement Index', | |
291 | 'default' => 'false', | |
292 | ); | |
293 | $this->_columns['civicrm_activity']['filters']['engagement_level'] = array('title' => ts('Engagement Index'), | |
294 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
295 | 'options' => $this->engagementLevels, | |
296 | ); | |
297 | } | |
298 | } | |
299 | $this->_groupFilter = TRUE; | |
300 | $this->_tagFilter = TRUE; | |
301 | parent::__construct(); | |
302 | } | |
303 | ||
1f220d30 | 304 | function select($recordType = NULL) { |
09febbae | 305 | if (!array_key_exists("contact_{$recordType}", $this->_params['fields']) && $recordType != 'final') { |
306 | $this->_nonDisplayFields[] = "civicrm_contact_contact_{$recordType}"; | |
307 | $this->_params['fields']["contact_{$recordType}"] = 1; | |
308 | } | |
1f220d30 | 309 | parent::select(); |
1f220d30 | 310 | |
09febbae | 311 | if ($recordType == 'final' && !empty($this->_nonDisplayFields)) { |
312 | foreach ($this->_nonDisplayFields as $fieldName) { | |
313 | unset($this->_columnHeaders[$fieldName]); | |
314 | } | |
315 | } | |
316 | ||
1f220d30 DS |
317 | if (empty($this->_selectAliasesTotal)) { |
318 | $this->_selectAliasesTotal = $this->_selectAliases; | |
319 | } | |
320 | ||
321 | $removeKeys = array(); | |
322 | if ($recordType == 'target') { | |
323 | foreach ($this->_selectClauses as $key => $clause) { | |
7116d719 | 324 | if (strstr($clause, 'civicrm_contact_assignee.') || |
1f220d30 DS |
325 | strstr($clause, 'civicrm_contact_source.') || |
326 | strstr($clause, 'civicrm_email_assignee.') || | |
327 | strstr($clause, 'civicrm_email_source.') | |
328 | ) { | |
329 | $removeKeys[] = $key; | |
330 | unset($this->_selectClauses[$key]); | |
331 | } | |
332 | } | |
333 | } else if ($recordType == 'assignee') { | |
334 | foreach ($this->_selectClauses as $key => $clause) { | |
7116d719 | 335 | if (strstr($clause, 'civicrm_contact_target.') || |
1f220d30 DS |
336 | strstr($clause, 'civicrm_contact_source.') || |
337 | strstr($clause, 'civicrm_email_target.') || | |
482691e1 | 338 | strstr($clause, 'civicrm_email_source.') |
1f220d30 DS |
339 | ) { |
340 | $removeKeys[] = $key; | |
341 | unset($this->_selectClauses[$key]); | |
342 | } | |
343 | } | |
344 | } else if ($recordType == 'source') { | |
345 | foreach ($this->_selectClauses as $key => $clause) { | |
7116d719 | 346 | if (strstr($clause, 'civicrm_contact_target.') || |
1f220d30 DS |
347 | strstr($clause, 'civicrm_contact_assignee.') || |
348 | strstr($clause, 'civicrm_email_target.') || | |
482691e1 | 349 | strstr($clause, 'civicrm_email_assignee.') |
1f220d30 DS |
350 | ) { |
351 | $removeKeys[] = $key; | |
352 | unset($this->_selectClauses[$key]); | |
353 | } | |
354 | } | |
355 | } else if ($recordType == 'final') { | |
356 | $this->_selectClauses = $this->_selectAliasesTotal; | |
357 | foreach ($this->_selectClauses as $key => $clause) { | |
7116d719 | 358 | if (strstr($clause, 'civicrm_contact_contact_target') || |
1f220d30 DS |
359 | strstr($clause, 'civicrm_contact_contact_assignee') || |
360 | strstr($clause, 'civicrm_contact_contact_source') ) { | |
482691e1 | 361 | $this->_selectClauses[$key] = "GROUP_CONCAT($clause SEPARATOR ';') as $clause"; |
6a488035 TO |
362 | } |
363 | } | |
364 | } | |
365 | ||
1f220d30 DS |
366 | if ($recordType) { |
367 | foreach ($removeKeys as $key) { | |
368 | unset($this->_selectAliases[$key]); | |
369 | } | |
370 | ||
1f220d30 | 371 | $this->_select = "SELECT " . implode(', ', $this->_selectClauses) . " "; |
1f220d30 | 372 | } |
6a488035 TO |
373 | } |
374 | ||
2f4b4d54 | 375 | function from($recordType) { |
e7e657f0 | 376 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
9e74e3ce | 377 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); |
2f4b4d54 DS |
378 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
379 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); | |
6a488035 | 380 | |
2f4b4d54 DS |
381 | if ($recordType == 'target') { |
382 | $this->_from = " | |
383 | FROM civicrm_activity {$this->_aliases['civicrm_activity']} | |
1f220d30 DS |
384 | INNER JOIN civicrm_activity_contact {$this->_aliases['civicrm_activity_contact']} |
385 | ON {$this->_aliases['civicrm_activity']}.id = {$this->_aliases['civicrm_activity_contact']}.activity_id AND | |
386 | {$this->_aliases['civicrm_activity_contact']}.record_type_id = {$targetID} | |
2f4b4d54 | 387 | INNER JOIN civicrm_contact civicrm_contact_target |
1f220d30 | 388 | ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_contact_target.id |
2f4b4d54 DS |
389 | {$this->_aclFrom}"; |
390 | ||
391 | if ($this->isTableSelected('civicrm_email')) { | |
392 | $this->_from .= " | |
1f220d30 DS |
393 | LEFT JOIN civicrm_email civicrm_email_target |
394 | ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_email_target.contact_id AND | |
395 | civicrm_email_target.is_primary = 1"; | |
2f4b4d54 | 396 | } |
1f220d30 | 397 | $this->_aliases['civicrm_contact'] = 'civicrm_contact_target'; |
2f4b4d54 DS |
398 | } |
399 | ||
400 | if ($recordType == 'assignee') { | |
401 | $this->_from = " | |
402 | FROM civicrm_activity {$this->_aliases['civicrm_activity']} | |
1f220d30 DS |
403 | INNER JOIN civicrm_activity_contact {$this->_aliases['civicrm_activity_contact']} |
404 | ON {$this->_aliases['civicrm_activity']}.id = {$this->_aliases['civicrm_activity_contact']}.activity_id AND | |
405 | {$this->_aliases['civicrm_activity_contact']}.record_type_id = {$assigneeID} | |
2f4b4d54 | 406 | INNER JOIN civicrm_contact civicrm_contact_assignee |
1f220d30 | 407 | ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_contact_assignee.id |
2f4b4d54 DS |
408 | {$this->_aclFrom}"; |
409 | ||
410 | if ($this->isTableSelected('civicrm_email')) { | |
411 | $this->_from .= " | |
1f220d30 DS |
412 | LEFT JOIN civicrm_email civicrm_email_assignee |
413 | ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_email_assignee.contact_id AND | |
414 | civicrm_email_assignee.is_primary = 1"; | |
2f4b4d54 | 415 | } |
482691e1 | 416 | $this->_aliases['civicrm_contact'] = 'civicrm_contact_assignee'; |
2f4b4d54 DS |
417 | } |
418 | ||
419 | if ($recordType == 'source') { | |
420 | $this->_from = " | |
421 | FROM civicrm_activity {$this->_aliases['civicrm_activity']} | |
1f220d30 DS |
422 | INNER JOIN civicrm_activity_contact {$this->_aliases['civicrm_activity_contact']} |
423 | ON {$this->_aliases['civicrm_activity']}.id = {$this->_aliases['civicrm_activity_contact']}.activity_id AND | |
424 | {$this->_aliases['civicrm_activity_contact']}.record_type_id = {$sourceID} | |
2f4b4d54 | 425 | INNER JOIN civicrm_contact civicrm_contact_source |
1f220d30 | 426 | ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_contact_source.id |
2f4b4d54 DS |
427 | {$this->_aclFrom}"; |
428 | ||
429 | if ($this->isTableSelected('civicrm_email')) { | |
430 | $this->_from .= " | |
1f220d30 DS |
431 | LEFT JOIN civicrm_email civicrm_email_source |
432 | ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_email_source.contact_id AND | |
433 | civicrm_email_source.is_primary = 1"; | |
2f4b4d54 | 434 | } |
482691e1 | 435 | $this->_aliases['civicrm_contact'] = 'civicrm_contact_source'; |
6a488035 | 436 | } |
482691e1 | 437 | $this->addAddressFromClause(); |
6a488035 TO |
438 | } |
439 | ||
b6cab294 | 440 | function where($recordType = NULL) { |
2f4b4d54 | 441 | $this->_where = " WHERE {$this->_aliases['civicrm_activity']}.is_test = 0 AND |
6a488035 TO |
442 | {$this->_aliases['civicrm_activity']}.is_deleted = 0 AND |
443 | {$this->_aliases['civicrm_activity']}.is_current_revision = 1"; | |
444 | ||
445 | $clauses = array(); | |
446 | foreach ($this->_columns as $tableName => $table) { | |
447 | if (array_key_exists('filters', $table)) { | |
448 | ||
449 | foreach ($table['filters'] as $fieldName => $field) { | |
450 | $clause = NULL; | |
7116d719 | 451 | if ($fieldName != 'contact_' . $recordType && |
452 | (strstr($fieldName, '_target') || | |
453 | strstr($fieldName, '_assignee') || | |
454 | strstr($fieldName, '_source') | |
455 | ) | |
456 | ) { | |
7116d719 | 457 | continue; |
458 | } | |
6a488035 TO |
459 | if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) { |
460 | $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params); | |
461 | $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params); | |
462 | $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params); | |
463 | ||
464 | $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']); | |
465 | } | |
466 | else { | |
467 | $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params); | |
389cfee7 | 468 | if ($op && ($op != 'nnll' || $op != 'nll')) { |
6a488035 TO |
469 | $clause = $this->whereClause($field, |
470 | $op, | |
471 | CRM_Utils_Array::value("{$fieldName}_value", $this->_params), | |
472 | CRM_Utils_Array::value("{$fieldName}_min", $this->_params), | |
473 | CRM_Utils_Array::value("{$fieldName}_max", $this->_params) | |
474 | ); | |
475 | } | |
476 | } | |
477 | ||
478 | if ($field['name'] == 'current_user') { | |
479 | if (CRM_Utils_Array::value("{$fieldName}_value", $this->_params) == 1) { | |
480 | // get current user | |
481 | $session = CRM_Core_Session::singleton(); | |
482 | if ($contactID = $session->get('userID')) { | |
389cfee7 | 483 | $clause = "{$this->_aliases['civicrm_activity_contact']}.activity_id IN |
484 | (SELECT activity_id FROM civicrm_activity_contact WHERE contact_id = {$contactID})"; | |
6a488035 TO |
485 | } |
486 | else { | |
487 | $clause = NULL; | |
488 | } | |
489 | } | |
490 | else { | |
491 | $clause = NULL; | |
492 | } | |
493 | } | |
494 | if (!empty($clause)) { | |
495 | $clauses[] = $clause; | |
496 | } | |
497 | } | |
498 | } | |
499 | } | |
500 | ||
501 | if (empty($clauses)) { | |
b6cab294 | 502 | $this->_where .= " "; |
6a488035 TO |
503 | } |
504 | else { | |
b6cab294 | 505 | $this->_where .= " AND " . implode(' AND ', $clauses); |
6a488035 TO |
506 | } |
507 | ||
508 | if ($this->_aclWhere) { | |
509 | $this->_where .= " AND {$this->_aclWhere} "; | |
510 | } | |
511 | } | |
512 | ||
513 | function groupBy() { | |
514 | $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_activity']}.id"; | |
515 | } | |
516 | ||
517 | function buildACLClause($tableAlias = 'contact_a') { | |
2a3c101f | 518 | //override for ACL( Since Contact may be source |
6a488035 TO |
519 | //contact/assignee or target also it may be null ) |
520 | ||
521 | if (CRM_Core_Permission::check('view all contacts')) { | |
522 | $this->_aclFrom = $this->_aclWhere = NULL; | |
523 | return; | |
524 | } | |
525 | ||
526 | $session = CRM_Core_Session::singleton(); | |
527 | $contactID = $session->get('userID'); | |
528 | if (!$contactID) { | |
529 | $contactID = 0; | |
530 | } | |
531 | $contactID = CRM_Utils_Type::escape($contactID, 'Integer'); | |
532 | ||
533 | CRM_Contact_BAO_Contact_Permission::cache($contactID); | |
534 | $clauses = array(); | |
535 | foreach ($tableAlias as $k => $alias) { | |
536 | $clauses[] = " INNER JOIN civicrm_acl_contact_cache aclContactCache_{$k} ON ( {$alias}.id = aclContactCache_{$k}.contact_id OR {$alias}.id IS NULL ) AND aclContactCache_{$k}.user_id = $contactID "; | |
537 | } | |
538 | ||
539 | $this->_aclFrom = implode(" ", $clauses); | |
540 | $this->_aclWhere = NULL; | |
541 | } | |
542 | ||
9a2f601e | 543 | function add2group($groupID) { |
544 | if (CRM_Utils_Array::value("contact_target_op", $this->_params) == 'nll') { | |
545 | CRM_Core_Error::fatal(ts('Current filter criteria didn\'t have any target contact to add to group')); | |
546 | } | |
547 | ||
548 | $query = "{$this->_select} | |
549 | FROM civireport_activity_temp_target tar | |
550 | GROUP BY civicrm_activity_id {$this->_having} {$this->_orderBy}"; | |
551 | $select = 'AS addtogroup_contact_id'; | |
552 | $query = str_ireplace('AS civicrm_contact_contact_target_id', $select, $query); | |
553 | $dao = CRM_Core_DAO::executeQuery($query); | |
554 | ||
555 | $contactIDs = array(); | |
556 | // Add resulting contacts to group | |
557 | while ($dao->fetch()) { | |
558 | if ($dao->addtogroup_contact_id) { | |
559 | $contact_id = explode(';', $dao->addtogroup_contact_id); | |
560 | if ($contact_id[0]) { | |
7bb16323 | 561 | $contactIDs[$contact_id[0]] = $contact_id[0]; |
9a2f601e | 562 | } |
563 | } | |
564 | } | |
565 | ||
566 | if ( !empty($contactIDs) ) { | |
567 | CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID); | |
568 | CRM_Core_Session::setStatus(ts("Listed contact(s) have been added to the selected group."), ts('Contacts Added'), 'success'); | |
569 | } | |
570 | else { | |
571 | CRM_Core_Session::setStatus(ts("The listed records(s) cannot be added to the group.")); | |
572 | } | |
573 | } | |
574 | ||
6a488035 | 575 | function postProcess() { |
83e74860 | 576 | $this->buildACLClause(array('civicrm_contact_source', 'civicrm_contact_target', 'civicrm_contact_assignee')); |
2f4b4d54 DS |
577 | $this->beginPostProcess(); |
578 | ||
389cfee7 | 579 | //Assign those recordtype to array which have filter operator as 'Is not empty' or 'Is empty' |
580 | $nullFilters = array(); | |
b6cab294 | 581 | foreach (array('target', 'source', 'assignee') as $type) { |
8cc574cf | 582 | if (CRM_Utils_Array::value("contact_{$type}_op", $this->_params) == 'nnll' || !empty($this->_params["contact_{$type}_value"])) { |
389cfee7 | 583 | $nullFilters[] = " civicrm_contact_contact_{$type} IS NOT NULL "; |
584 | } | |
585 | else if (CRM_Utils_Array::value("contact_{$type}_op", $this->_params) == 'nll') { | |
586 | $nullFilters[] = " civicrm_contact_contact_{$type} IS NULL "; | |
b6cab294 | 587 | } |
588 | } | |
589 | ||
389cfee7 | 590 | // 1. fill temp table with target results |
1f220d30 | 591 | $this->select('target'); |
2f4b4d54 | 592 | $this->from('target'); |
5b1ba2b8 | 593 | $this->customDataFrom(); |
389cfee7 | 594 | $this->where('target'); |
1f220d30 | 595 | $insertCols = implode(',', $this->_selectAliases); |
7116d719 | 596 | $tempQuery = "CREATE TEMPORARY TABLE civireport_activity_temp_target CHARACTER SET utf8 COLLATE utf8_unicode_ci AS |
482691e1 | 597 | {$this->_select} {$this->_from} {$this->_where} "; |
2f4b4d54 | 598 | CRM_Core_DAO::executeQuery($tempQuery); |
2f4b4d54 | 599 | |
1f220d30 DS |
600 | // 2. add new columns to hold assignee and source results |
601 | // fixme: add when required | |
602 | $tempQuery = " | |
603 | ALTER TABLE civireport_activity_temp_target | |
604 | ADD COLUMN civicrm_contact_contact_assignee VARCHAR(128), | |
605 | ADD COLUMN civicrm_contact_contact_source VARCHAR(128), | |
7116d719 | 606 | ADD COLUMN civicrm_contact_contact_assignee_id VARCHAR(128), |
482691e1 | 607 | ADD COLUMN civicrm_contact_contact_source_id VARCHAR(128), |
1f220d30 DS |
608 | ADD COLUMN civicrm_email_contact_assignee_email VARCHAR(128), |
609 | ADD COLUMN civicrm_email_contact_source_email VARCHAR(128)"; | |
610 | CRM_Core_DAO::executeQuery($tempQuery); | |
611 | ||
612 | // 3. fill temp table with assignee results | |
389cfee7 | 613 | $this->select('assignee'); |
614 | $this->from('assignee'); | |
615 | $this->customDataFrom(); | |
616 | $this->where('assignee'); | |
617 | $insertCols = implode(',', $this->_selectAliases); | |
618 | $tempQuery = "INSERT INTO civireport_activity_temp_target ({$insertCols}) | |
1f220d30 | 619 | {$this->_select} |
83e74860 | 620 | {$this->_from} {$this->_where}"; |
389cfee7 | 621 | CRM_Core_DAO::executeQuery($tempQuery); |
2f4b4d54 | 622 | |
1f220d30 | 623 | // 4. fill temp table with source results |
389cfee7 | 624 | $this->select('source'); |
625 | $this->from('source'); | |
626 | $this->customDataFrom(); | |
627 | $this->where('source'); | |
628 | $insertCols = implode(',', $this->_selectAliases); | |
629 | $tempQuery = "INSERT INTO civireport_activity_temp_target ({$insertCols}) | |
1f220d30 | 630 | {$this->_select} |
83e74860 | 631 | {$this->_from} {$this->_where}"; |
389cfee7 | 632 | CRM_Core_DAO::executeQuery($tempQuery); |
2f4b4d54 | 633 | |
1f220d30 | 634 | // 5. show final result set from temp table |
2f4b4d54 | 635 | $rows = array(); |
1f220d30 | 636 | $this->select('final'); |
389cfee7 | 637 | $this->_having = ""; |
638 | if (!empty($nullFilters)) { | |
639 | $this->_having = "HAVING " . implode(' AND ', $nullFilters); | |
640 | } | |
83e74860 | 641 | $this->orderBy(); |
1f220d30 | 642 | $this->limit(); |
7116d719 | 643 | $sql = "{$this->_select} |
644 | FROM civireport_activity_temp_target tar | |
389cfee7 | 645 | GROUP BY civicrm_activity_id {$this->_having} {$this->_orderBy} {$this->_limit}"; |
2f4b4d54 DS |
646 | $this->buildRows($sql, $rows); |
647 | ||
648 | // format result set. | |
1f220d30 | 649 | $this->formatDisplay($rows); |
2f4b4d54 DS |
650 | |
651 | // assign variables to templates | |
652 | $this->doTemplateAssignment($rows); | |
653 | ||
654 | // do print / pdf / instance stuff if needed | |
655 | $this->endPostProcess($rows); | |
6a488035 TO |
656 | } |
657 | ||
658 | function alterDisplay(&$rows) { | |
659 | // custom code to alter rows | |
660 | ||
661 | $entryFound = FALSE; | |
662 | $activityType = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE); | |
663 | $activityStatus = CRM_Core_PseudoConstant::activityStatus(); | |
664 | $viewLinks = FALSE; | |
f7ed6ab4 | 665 | $seperator = CRM_Core_DAO::VALUE_SEPARATOR; |
6a488035 TO |
666 | $context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'report'); |
667 | ||
668 | if (CRM_Core_Permission::check('access CiviCRM')) { | |
669 | $viewLinks = TRUE; | |
670 | $onHover = ts('View Contact Summary for this Contact'); | |
671 | $onHoverAct = ts('View Activity Record'); | |
672 | } | |
6a488035 | 673 | |
38ba593b | 674 | foreach ($rows as $rowNum => $row) { |
482691e1 RN |
675 | if (array_key_exists('civicrm_contact_contact_source', $row)) { |
676 | if ($value = $row['civicrm_contact_contact_source_id']) { | |
677 | if ($viewLinks) { | |
678 | $url = CRM_Utils_System::url("civicrm/contact/view", | |
679 | 'reset=1&cid=' . $value, | |
680 | $this->_absoluteUrl | |
681 | ); | |
682 | $rows[$rowNum]['civicrm_contact_contact_source_link'] = $url; | |
683 | $rows[$rowNum]['civicrm_contact_contact_source_hover'] = $onHover; | |
684 | } | |
685 | $entryFound = TRUE; | |
686 | } | |
687 | } | |
688 | ||
689 | if (array_key_exists('civicrm_contact_contact_assignee', $row)) { | |
690 | $assigneeNames = explode(';', $row['civicrm_contact_contact_assignee']); | |
691 | if ($value = $row['civicrm_contact_contact_assignee_id']) { | |
692 | $assigneeContactIds = explode(';', $value); | |
693 | $link = array(); | |
694 | if ($viewLinks) { | |
695 | foreach ($assigneeContactIds as $id => $value) { | |
696 | if (isset($value) && isset($assigneeNames[$id])) { | |
697 | $url = CRM_Utils_System::url("civicrm/contact/view", | |
698 | 'reset=1&cid=' . $value, | |
699 | $this->_absoluteUrl | |
700 | ); | |
701 | $link[] = "<a title='" . $onHover . "' href='" . $url . "'>{$assigneeNames[$id]}</a>"; | |
702 | } | |
703 | } | |
704 | $rows[$rowNum]['civicrm_contact_contact_assignee'] = implode('; ', $link); | |
705 | } | |
706 | $entryFound = TRUE; | |
707 | } | |
708 | } | |
709 | ||
710 | if (array_key_exists('civicrm_contact_contact_target', $row)) { | |
711 | $targetNames = explode(';', $row['civicrm_contact_contact_target']); | |
712 | if ($value = $row['civicrm_contact_contact_target_id']) { | |
713 | $targetContactIds = explode(';', $value); | |
714 | $link = array(); | |
715 | if ($viewLinks) { | |
716 | foreach ($targetContactIds as $id => $value) { | |
717 | if (isset($value) && isset($targetNames[$id])) { | |
718 | $url = CRM_Utils_System::url("civicrm/contact/view", | |
719 | 'reset=1&cid=' . $value, | |
720 | $this->_absoluteUrl | |
721 | ); | |
722 | $link[] = "<a title='" . $onHover . "' href='" . $url . "'>{$targetNames[$id]}</a>"; | |
723 | } | |
724 | } | |
725 | $rows[$rowNum]['civicrm_contact_contact_target'] = implode('; ', $link); | |
726 | } | |
727 | $entryFound = TRUE; | |
728 | } | |
729 | } | |
730 | ||
6a488035 TO |
731 | if (array_key_exists('civicrm_activity_activity_type_id', $row)) { |
732 | if ($value = $row['civicrm_activity_activity_type_id']) { | |
733 | $rows[$rowNum]['civicrm_activity_activity_type_id'] = $activityType[$value]; | |
482691e1 RN |
734 | if ($viewLinks) { |
735 | // Check for target contact id(s) and use the first contact id in that list for view activity link if found, | |
736 | // else use source contact id | |
737 | if (!empty($rows[$rowNum]['civicrm_contact_contact_target_id'])) { | |
738 | $targets = explode(';', $rows[$rowNum]['civicrm_contact_contact_target_id']); | |
739 | $cid = $targets[0]; | |
740 | } | |
741 | else { | |
742 | $cid = $rows[$rowNum]['civicrm_contact_contact_source_id']; | |
743 | } | |
744 | ||
745 | $actionLinks = CRM_Activity_Selector_Activity::actionLinks($row['civicrm_activity_activity_type_id'], | |
746 | CRM_Utils_Array::value('civicrm_activity_source_record_id', $rows[$rowNum]), | |
747 | FALSE, | |
748 | $rows[$rowNum]['civicrm_activity_id'] | |
749 | ); | |
750 | ||
751 | $linkValues = array( | |
752 | 'id' => $rows[$rowNum]['civicrm_activity_id'], | |
753 | 'cid' => $cid, | |
754 | 'cxt' => $context, | |
755 | ); | |
756 | $url = CRM_Utils_System::url($actionLinks[CRM_Core_Action::VIEW]['url'], | |
757 | CRM_Core_Action::replace($actionLinks[CRM_Core_Action::VIEW]['qs'], $linkValues), TRUE | |
758 | ); | |
759 | $rows[$rowNum]['civicrm_activity_activity_type_id_link'] = $url; | |
760 | $rows[$rowNum]['civicrm_activity_activity_type_id_hover'] = $onHoverAct; | |
761 | } | |
6a488035 TO |
762 | $entryFound = TRUE; |
763 | } | |
764 | } | |
765 | ||
766 | if (array_key_exists('civicrm_activity_status_id', $row)) { | |
767 | if ($value = $row['civicrm_activity_status_id']) { | |
768 | $rows[$rowNum]['civicrm_activity_status_id'] = $activityStatus[$value]; | |
769 | $entryFound = TRUE; | |
770 | } | |
771 | } | |
772 | ||
773 | if (array_key_exists('civicrm_activity_campaign_id', $row)) { | |
774 | if ($value = $row['civicrm_activity_campaign_id']) { | |
775 | $rows[$rowNum]['civicrm_activity_campaign_id'] = $this->activeCampaigns[$value]; | |
776 | $entryFound = TRUE; | |
777 | } | |
778 | } | |
779 | ||
780 | if (array_key_exists('civicrm_activity_engagement_level', $row)) { | |
781 | if ($value = $row['civicrm_activity_engagement_level']) { | |
782 | $rows[$rowNum]['civicrm_activity_engagement_level'] = $this->engagementLevels[$value]; | |
783 | $entryFound = TRUE; | |
784 | } | |
785 | } | |
786 | ||
787 | if (array_key_exists('civicrm_activity_activity_date_time', $row) && array_key_exists('civicrm_activity_status_id', $row)) { | |
788 | if (CRM_Utils_Date::overdue($rows[$rowNum]['civicrm_activity_activity_date_time']) && | |
789 | $activityStatus[$row['civicrm_activity_status_id']] != 'Completed' | |
790 | ) { | |
791 | $rows[$rowNum]['class'] = "status-overdue"; | |
792 | $entryFound = TRUE; | |
793 | } | |
794 | } | |
795 | ||
482691e1 RN |
796 | $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'activity', 'List all activities for this ') ? TRUE : $entryFound; |
797 | ||
6a488035 TO |
798 | if (!$entryFound) { |
799 | break; | |
800 | } | |
801 | } | |
802 | } | |
6a488035 TO |
803 | } |
804 |