Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | */ |
33 | class CRM_Report_Form_Activity extends CRM_Report_Form { | |
1f220d30 | 34 | protected $_selectAliasesTotal = array(); |
6a488035 TO |
35 | |
36 | protected $_customGroupExtends = array( | |
21dfd5f5 | 37 | 'Activity', |
430ae6dd TO |
38 | ); |
39 | ||
09febbae | 40 | protected $_nonDisplayFields = array(); |
41 | ||
74cf4551 | 42 | /** |
ab432335 | 43 | * Class constructor. |
74cf4551 | 44 | */ |
00be9182 | 45 | public function __construct() { |
7116d719 | 46 | // There could be multiple contacts. We not clear on which contact id to display. |
83e74860 DS |
47 | // Lets hide it for now. |
48 | $this->_exposeContactID = FALSE; | |
49 | ||
6a488035 TO |
50 | $config = CRM_Core_Config::singleton(); |
51 | $campaignEnabled = in_array("CiviCampaign", $config->enableComponents); | |
d6c3d40f | 52 | $caseEnabled = in_array("CiviCase", $config->enableComponents); |
6a488035 TO |
53 | if ($campaignEnabled) { |
54 | $getCampaigns = CRM_Campaign_BAO_Campaign::getPermissionedCampaigns(NULL, NULL, TRUE, FALSE, TRUE); | |
55 | $this->activeCampaigns = $getCampaigns['campaigns']; | |
56 | asort($this->activeCampaigns); | |
57 | $this->engagementLevels = CRM_Campaign_PseudoConstant::engagementLevel(); | |
58 | } | |
6a488035 | 59 | |
1023b3bc | 60 | $components = CRM_Core_Component::getEnabledComponents(); |
61 | foreach ($components as $componentName => $componentInfo) { | |
62 | if (CRM_Core_Permission::check("access $componentName")) { | |
63 | $accessAllowed[] = $componentInfo->componentID; | |
64 | } | |
65 | } | |
66 | ||
67 | $include = ''; | |
68 | if (!empty($accessAllowed)) { | |
69 | $include = 'OR v.component_id IN (' . implode(', ', $accessAllowed) . ')'; | |
70 | } | |
71 | $condition = " AND ( v.component_id IS NULL {$include} )"; | |
72 | $this->activityTypes = CRM_Core_OptionGroup::values('activity_type', FALSE, FALSE, FALSE, $condition); | |
73 | asort($this->activityTypes); | |
6a488035 | 74 | $this->_columns = array( |
af9b09df TO |
75 | 'civicrm_contact' => array( |
76 | 'dao' => 'CRM_Contact_DAO_Contact', | |
77 | 'fields' => array( | |
78 | 'contact_source' => array( | |
79 | 'name' => 'sort_name', | |
8c0ff2a6 | 80 | 'title' => ts('Source Name'), |
af9b09df TO |
81 | 'alias' => 'civicrm_contact_source', |
82 | 'no_repeat' => TRUE, | |
6a488035 | 83 | ), |
af9b09df TO |
84 | 'contact_assignee' => array( |
85 | 'name' => 'sort_name', | |
8c0ff2a6 | 86 | 'title' => ts('Assignee Name'), |
af9b09df TO |
87 | 'alias' => 'civicrm_contact_assignee', |
88 | 'dbAlias' => "civicrm_contact_assignee.sort_name", | |
89 | 'default' => TRUE, | |
90 | ), | |
91 | 'contact_target' => array( | |
92 | 'name' => 'sort_name', | |
8c0ff2a6 | 93 | 'title' => ts('Target Name'), |
af9b09df TO |
94 | 'alias' => 'civicrm_contact_target', |
95 | 'dbAlias' => "civicrm_contact_target.sort_name", | |
96 | 'default' => TRUE, | |
97 | ), | |
98 | 'contact_source_id' => array( | |
99 | 'name' => 'id', | |
100 | 'alias' => 'civicrm_contact_source', | |
101 | 'dbAlias' => "civicrm_contact_source.id", | |
102 | 'no_display' => TRUE, | |
103 | 'default' => TRUE, | |
104 | 'required' => TRUE, | |
105 | ), | |
106 | 'contact_assignee_id' => array( | |
107 | 'name' => 'id', | |
108 | 'alias' => 'civicrm_contact_assignee', | |
109 | 'dbAlias' => "civicrm_contact_assignee.id", | |
110 | 'no_display' => TRUE, | |
111 | 'default' => TRUE, | |
112 | 'required' => TRUE, | |
113 | ), | |
114 | 'contact_target_id' => array( | |
115 | 'name' => 'id', | |
116 | 'alias' => 'civicrm_contact_target', | |
117 | 'dbAlias' => "civicrm_contact_target.id", | |
118 | 'no_display' => TRUE, | |
119 | 'default' => TRUE, | |
120 | 'required' => TRUE, | |
482691e1 | 121 | ), |
6a488035 | 122 | ), |
af9b09df TO |
123 | 'filters' => array( |
124 | 'contact_source' => array( | |
125 | 'name' => 'sort_name', | |
126 | 'alias' => 'civicrm_contact_source', | |
8c0ff2a6 | 127 | 'title' => ts('Source Name'), |
af9b09df TO |
128 | 'operator' => 'like', |
129 | 'type' => CRM_Report_Form::OP_STRING, | |
6a488035 | 130 | ), |
af9b09df TO |
131 | 'contact_assignee' => array( |
132 | 'name' => 'sort_name', | |
133 | 'alias' => 'civicrm_contact_assignee', | |
8c0ff2a6 | 134 | 'title' => ts('Assignee Name'), |
af9b09df TO |
135 | 'operator' => 'like', |
136 | 'type' => CRM_Report_Form::OP_STRING, | |
137 | ), | |
138 | 'contact_target' => array( | |
139 | 'name' => 'sort_name', | |
140 | 'alias' => 'civicrm_contact_target', | |
8c0ff2a6 | 141 | 'title' => ts('Target Name'), |
af9b09df TO |
142 | 'operator' => 'like', |
143 | 'type' => CRM_Report_Form::OP_STRING, | |
144 | ), | |
145 | 'current_user' => array( | |
146 | 'name' => 'current_user', | |
147 | 'title' => ts('Limit To Current User'), | |
148 | 'type' => CRM_Utils_Type::T_INT, | |
149 | 'operatorType' => CRM_Report_Form::OP_SELECT, | |
150 | 'options' => array('0' => ts('No'), '1' => ts('Yes')), | |
6a488035 TO |
151 | ), |
152 | ), | |
af9b09df TO |
153 | 'grouping' => 'contact-fields', |
154 | ), | |
155 | 'civicrm_email' => array( | |
156 | 'dao' => 'CRM_Core_DAO_Email', | |
157 | 'fields' => array( | |
158 | 'contact_source_email' => array( | |
159 | 'name' => 'email', | |
8c0ff2a6 | 160 | 'title' => ts('Source Email'), |
af9b09df | 161 | 'alias' => 'civicrm_email_source', |
6a488035 | 162 | ), |
af9b09df TO |
163 | 'contact_assignee_email' => array( |
164 | 'name' => 'email', | |
8c0ff2a6 | 165 | 'title' => ts('Assignee Email'), |
af9b09df | 166 | 'alias' => 'civicrm_email_assignee', |
6a488035 | 167 | ), |
af9b09df TO |
168 | 'contact_target_email' => array( |
169 | 'name' => 'email', | |
8c0ff2a6 | 170 | 'title' => ts('Target Email'), |
af9b09df | 171 | 'alias' => 'civicrm_email_target', |
6a488035 TO |
172 | ), |
173 | ), | |
af9b09df TO |
174 | 'order_bys' => array( |
175 | 'source_contact_email' => array( | |
176 | 'name' => 'email', | |
8c0ff2a6 | 177 | 'title' => ts('Source Email'), |
af9b09df | 178 | 'dbAlias' => 'civicrm_email_contact_source_email', |
6a488035 TO |
179 | ), |
180 | ), | |
af9b09df | 181 | ), |
8c0ff2a6 | 182 | 'civicrm_phone' => array( |
183 | 'dao' => 'CRM_Core_DAO_Phone', | |
184 | 'fields' => array( | |
185 | 'contact_source_phone' => array( | |
186 | 'name' => 'phone', | |
187 | 'title' => ts('Source Phone'), | |
188 | 'alias' => 'civicrm_phone_source', | |
189 | ), | |
190 | 'contact_assignee_phone' => array( | |
191 | 'name' => 'phone', | |
192 | 'title' => ts('Assignee Phone'), | |
193 | 'alias' => 'civicrm_phone_assignee', | |
194 | ), | |
195 | 'contact_target_phone' => array( | |
196 | 'name' => 'phone', | |
2afe8e0c | 197 | 'title' => ts('Target Phone'), |
8c0ff2a6 | 198 | 'alias' => 'civicrm_phone_target', |
199 | ), | |
200 | ), | |
201 | ), | |
af9b09df TO |
202 | 'civicrm_activity' => array( |
203 | 'dao' => 'CRM_Activity_DAO_Activity', | |
204 | 'fields' => array( | |
205 | 'id' => array( | |
206 | 'no_display' => TRUE, | |
207 | 'title' => ts('Activity ID'), | |
208 | 'required' => TRUE, | |
209 | ), | |
210 | 'source_record_id' => array( | |
211 | 'no_display' => TRUE, | |
212 | 'required' => TRUE, | |
213 | ), | |
214 | 'activity_type_id' => array( | |
215 | 'title' => ts('Activity Type'), | |
216 | 'required' => TRUE, | |
217 | 'type' => CRM_Utils_Type::T_STRING, | |
218 | ), | |
219 | 'activity_subject' => array( | |
220 | 'title' => ts('Subject'), | |
221 | 'default' => TRUE, | |
222 | ), | |
223 | 'activity_date_time' => array( | |
224 | 'title' => ts('Activity Date'), | |
225 | 'required' => TRUE, | |
226 | ), | |
227 | 'status_id' => array( | |
228 | 'title' => ts('Activity Status'), | |
229 | 'default' => TRUE, | |
230 | 'type' => CRM_Utils_Type::T_STRING, | |
231 | ), | |
232 | 'duration' => array( | |
233 | 'title' => ts('Duration'), | |
234 | 'type' => CRM_Utils_Type::T_INT, | |
667abe3a | 235 | ), |
1efec7ff DG |
236 | 'details' => array( |
237 | 'title' => ts('Activity Details'), | |
af9b09df | 238 | ), |
6a488035 | 239 | ), |
1efec7ff DG |
240 | 'filters' => array( |
241 | 'activity_date_time' => array( | |
1a6c8513 | 242 | 'default' => 'this.month', |
6a488035 TO |
243 | 'operatorType' => CRM_Report_Form::OP_DATE, |
244 | ), | |
af9b09df TO |
245 | 'activity_subject' => array('title' => ts('Activity Subject')), |
246 | 'activity_type_id' => array( | |
247 | 'title' => ts('Activity Type'), | |
248 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, | |
249 | 'options' => $this->activityTypes, | |
250 | ), | |
251 | 'status_id' => array( | |
252 | 'title' => ts('Activity Status'), | |
8ee006e7 | 253 | 'type' => CRM_Utils_Type::T_STRING, |
af9b09df TO |
254 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
255 | 'options' => CRM_Core_PseudoConstant::activityStatus(), | |
256 | ), | |
1efec7ff DG |
257 | 'details' => array( |
258 | 'title' => ts('Activity Details'), | |
259 | 'type' => CRM_Utils_Type::T_TEXT, | |
af9b09df TO |
260 | ), |
261 | ), | |
262 | 'order_bys' => array( | |
263 | 'activity_date_time' => array( | |
264 | 'title' => ts('Activity Date'), | |
265 | 'default_weight' => '1', | |
266 | 'dbAlias' => 'civicrm_activity_activity_date_time', | |
267 | ), | |
268 | 'activity_type_id' => array( | |
269 | 'title' => ts('Activity Type'), | |
270 | 'default_weight' => '2', | |
56bbb442 | 271 | 'dbAlias' => 'field(civicrm_activity_activity_type_id, ' . implode(', ', array_keys($this->activityTypes)) . ')', |
af9b09df TO |
272 | ), |
273 | ), | |
274 | 'grouping' => 'activity-fields', | |
275 | 'alias' => 'activity', | |
276 | ), | |
56bbb442 | 277 | // Hack to get $this->_alias populated for the table. |
af9b09df TO |
278 | 'civicrm_activity_contact' => array( |
279 | 'dao' => 'CRM_Activity_DAO_ActivityContact', | |
a0375fa3 | 280 | 'fields' => array(), |
af9b09df TO |
281 | ), |
282 | ) + $this->addressFields(TRUE); | |
6a488035 | 283 | |
d6c3d40f | 284 | if ($caseEnabled && CRM_Core_Permission::check('access all cases and activities')) { |
285 | $this->activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE); | |
286 | $this->_columns['civicrm_activity']['filters']['activity_type_id']['options'] = $this->activityTypes; | |
287 | $this->_columns['civicrm_activity']['filters']['include_case_activities'] = array( | |
288 | 'name' => 'include_case_activities', | |
289 | 'title' => ts('Include Case Activities'), | |
290 | 'type' => CRM_Utils_Type::T_INT, | |
291 | 'operatorType' => CRM_Report_Form::OP_SELECT, | |
292 | 'options' => array('0' => ts('No'), '1' => ts('Yes')), | |
293 | ); | |
294 | } | |
295 | ||
6a488035 TO |
296 | if ($campaignEnabled) { |
297 | // Add display column and filter for Survey Results, Campaign and Engagement Index if CiviCampaign is enabled | |
298 | ||
299 | $this->_columns['civicrm_activity']['fields']['result'] = array( | |
300 | 'title' => 'Survey Result', | |
301 | 'default' => 'false', | |
302 | ); | |
9d72cede EM |
303 | $this->_columns['civicrm_activity']['filters']['result'] = array( |
304 | 'title' => ts('Survey Result'), | |
6a488035 TO |
305 | 'operator' => 'like', |
306 | 'type' => CRM_Utils_Type::T_STRING, | |
307 | ); | |
308 | if (!empty($this->activeCampaigns)) { | |
309 | $this->_columns['civicrm_activity']['fields']['campaign_id'] = array( | |
310 | 'title' => 'Campaign', | |
311 | 'default' => 'false', | |
312 | ); | |
9d72cede EM |
313 | $this->_columns['civicrm_activity']['filters']['campaign_id'] = array( |
314 | 'title' => ts('Campaign'), | |
6a488035 TO |
315 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
316 | 'options' => $this->activeCampaigns, | |
317 | ); | |
318 | } | |
319 | if (!empty($this->engagementLevels)) { | |
320 | $this->_columns['civicrm_activity']['fields']['engagement_level'] = array( | |
321 | 'title' => 'Engagement Index', | |
322 | 'default' => 'false', | |
323 | ); | |
9d72cede EM |
324 | $this->_columns['civicrm_activity']['filters']['engagement_level'] = array( |
325 | 'title' => ts('Engagement Index'), | |
6a488035 TO |
326 | 'operatorType' => CRM_Report_Form::OP_MULTISELECT, |
327 | 'options' => $this->engagementLevels, | |
328 | ); | |
329 | } | |
330 | } | |
331 | $this->_groupFilter = TRUE; | |
332 | $this->_tagFilter = TRUE; | |
ed795723 | 333 | $this->_tagFilterTable = 'civicrm_activity'; |
6a488035 TO |
334 | parent::__construct(); |
335 | } | |
336 | ||
be2e0c6a TO |
337 | /** |
338 | * Adding address fields with dbAlias for order clause. | |
ab432335 EM |
339 | * |
340 | * @param bool $orderBy | |
341 | * | |
a6c01b45 | 342 | * @return array |
ab432335 | 343 | * Address fields |
667abe3a | 344 | */ |
00be9182 | 345 | public function addressFields($orderBy = FALSE) { |
667abe3a | 346 | $address = parent::addAddressFields(FALSE, TRUE); |
347 | if ($orderBy) { | |
348 | foreach ($address['civicrm_address']['order_bys'] as $fieldName => $field) { | |
349 | $address['civicrm_address']['order_bys'][$fieldName]['dbAlias'] = "civicrm_address_{$fieldName}"; | |
350 | } | |
351 | } | |
352 | return $address; | |
353 | } | |
354 | ||
74cf4551 | 355 | /** |
a0375fa3 | 356 | * Build select clause. |
357 | * | |
74cf4551 EM |
358 | * @param null $recordType |
359 | */ | |
00be9182 | 360 | public function select($recordType = NULL) { |
9d72cede EM |
361 | if (!array_key_exists("contact_{$recordType}", $this->_params['fields']) && |
362 | $recordType != 'final' | |
363 | ) { | |
09febbae | 364 | $this->_nonDisplayFields[] = "civicrm_contact_contact_{$recordType}"; |
09febbae | 365 | } |
1f220d30 | 366 | parent::select(); |
1f220d30 | 367 | |
09febbae | 368 | if ($recordType == 'final' && !empty($this->_nonDisplayFields)) { |
369 | foreach ($this->_nonDisplayFields as $fieldName) { | |
370 | unset($this->_columnHeaders[$fieldName]); | |
371 | } | |
372 | } | |
373 | ||
1f220d30 DS |
374 | if (empty($this->_selectAliasesTotal)) { |
375 | $this->_selectAliasesTotal = $this->_selectAliases; | |
376 | } | |
377 | ||
378 | $removeKeys = array(); | |
379 | if ($recordType == 'target') { | |
380 | foreach ($this->_selectClauses as $key => $clause) { | |
7116d719 | 381 | if (strstr($clause, 'civicrm_contact_assignee.') || |
1f220d30 DS |
382 | strstr($clause, 'civicrm_contact_source.') || |
383 | strstr($clause, 'civicrm_email_assignee.') || | |
8c0ff2a6 | 384 | strstr($clause, 'civicrm_email_source.') || |
385 | strstr($clause, 'civicrm_phone_assignee.') || | |
386 | strstr($clause, 'civicrm_phone_source.') | |
1f220d30 DS |
387 | ) { |
388 | $removeKeys[] = $key; | |
389 | unset($this->_selectClauses[$key]); | |
390 | } | |
391 | } | |
9d72cede | 392 | } |
4c9b6178 | 393 | elseif ($recordType == 'assignee') { |
1f220d30 | 394 | foreach ($this->_selectClauses as $key => $clause) { |
7116d719 | 395 | if (strstr($clause, 'civicrm_contact_target.') || |
1f220d30 DS |
396 | strstr($clause, 'civicrm_contact_source.') || |
397 | strstr($clause, 'civicrm_email_target.') || | |
8c0ff2a6 | 398 | strstr($clause, 'civicrm_email_source.') || |
399 | strstr($clause, 'civicrm_phone_target.') || | |
400 | strstr($clause, 'civicrm_phone_source.') | |
1f220d30 DS |
401 | ) { |
402 | $removeKeys[] = $key; | |
403 | unset($this->_selectClauses[$key]); | |
404 | } | |
405 | } | |
9d72cede | 406 | } |
4c9b6178 | 407 | elseif ($recordType == 'source') { |
1f220d30 | 408 | foreach ($this->_selectClauses as $key => $clause) { |
7116d719 | 409 | if (strstr($clause, 'civicrm_contact_target.') || |
1f220d30 DS |
410 | strstr($clause, 'civicrm_contact_assignee.') || |
411 | strstr($clause, 'civicrm_email_target.') || | |
8c0ff2a6 | 412 | strstr($clause, 'civicrm_email_assignee.') || |
413 | strstr($clause, 'civicrm_phone_target.') || | |
414 | strstr($clause, 'civicrm_phone_assignee.') | |
1f220d30 DS |
415 | ) { |
416 | $removeKeys[] = $key; | |
417 | unset($this->_selectClauses[$key]); | |
418 | } | |
419 | } | |
9d72cede | 420 | } |
4c9b6178 | 421 | elseif ($recordType == 'final') { |
1f220d30 DS |
422 | $this->_selectClauses = $this->_selectAliasesTotal; |
423 | foreach ($this->_selectClauses as $key => $clause) { | |
7116d719 | 424 | if (strstr($clause, 'civicrm_contact_contact_target') || |
1f220d30 | 425 | strstr($clause, 'civicrm_contact_contact_assignee') || |
8c0ff2a6 | 426 | strstr($clause, 'civicrm_contact_contact_source') || |
427 | strstr($clause, 'civicrm_phone_contact_source_phone') || | |
428 | strstr($clause, 'civicrm_phone_contact_assignee_phone') || | |
429 | strstr($clause, 'civicrm_email_contact_source_email') || | |
430 | strstr($clause, 'civicrm_email_contact_assignee_email') || | |
431 | strstr($clause, 'civicrm_email_contact_target_email') || | |
432 | strstr($clause, 'civicrm_phone_contact_target_phone') | |
9d72cede | 433 | ) { |
8c0ff2a6 | 434 | $this->_selectClauses[$key] = "GROUP_CONCAT($clause SEPARATOR '; ') as $clause"; |
6a488035 TO |
435 | } |
436 | } | |
437 | } | |
438 | ||
1f220d30 DS |
439 | if ($recordType) { |
440 | foreach ($removeKeys as $key) { | |
441 | unset($this->_selectAliases[$key]); | |
442 | } | |
443 | ||
667abe3a | 444 | if ($recordType != 'final') { |
445 | foreach ($this->_columns['civicrm_address']['order_bys'] as $fieldName => $field) { | |
446 | $orderByFld = $this->_columns['civicrm_address']['order_bys'][$fieldName]; | |
447 | $fldInfo = $this->_columns['civicrm_address']['fields'][$fieldName]; | |
448 | $this->_selectAliases[] = $orderByFld['dbAlias']; | |
449 | $this->_selectClauses[] = "{$fldInfo['dbAlias']} as {$orderByFld['dbAlias']}"; | |
450 | } | |
667abe3a | 451 | $this->_selectAliases = array_unique($this->_selectAliases); |
452 | $this->_selectClauses = array_unique($this->_selectClauses); | |
453 | } | |
1f220d30 | 454 | $this->_select = "SELECT " . implode(', ', $this->_selectClauses) . " "; |
1f220d30 | 455 | } |
6a488035 TO |
456 | } |
457 | ||
74cf4551 | 458 | /** |
a0375fa3 | 459 | * Build from clause. |
460 | * | |
461 | * @param string $recordType | |
74cf4551 | 462 | */ |
00be9182 | 463 | public function from($recordType) { |
e7e657f0 | 464 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
667abe3a | 465 | $activityTypeId = CRM_Core_DAO::getFieldValue("CRM_Core_DAO_OptionGroup", 'activity_type', 'id', 'name'); |
9e74e3ce | 466 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); |
9d72cede EM |
467 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
468 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); | |
6a488035 | 469 | |
2f4b4d54 DS |
470 | if ($recordType == 'target') { |
471 | $this->_from = " | |
472 | FROM civicrm_activity {$this->_aliases['civicrm_activity']} | |
1f220d30 DS |
473 | INNER JOIN civicrm_activity_contact {$this->_aliases['civicrm_activity_contact']} |
474 | ON {$this->_aliases['civicrm_activity']}.id = {$this->_aliases['civicrm_activity_contact']}.activity_id AND | |
475 | {$this->_aliases['civicrm_activity_contact']}.record_type_id = {$targetID} | |
2f4b4d54 | 476 | INNER JOIN civicrm_contact civicrm_contact_target |
1f220d30 | 477 | ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_contact_target.id |
2f4b4d54 DS |
478 | {$this->_aclFrom}"; |
479 | ||
480 | if ($this->isTableSelected('civicrm_email')) { | |
481 | $this->_from .= " | |
1f220d30 DS |
482 | LEFT JOIN civicrm_email civicrm_email_target |
483 | ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_email_target.contact_id AND | |
484 | civicrm_email_target.is_primary = 1"; | |
2f4b4d54 | 485 | } |
8c0ff2a6 | 486 | |
487 | if ($this->isTableSelected('civicrm_phone')) { | |
488 | $this->_from .= " | |
489 | LEFT JOIN civicrm_phone civicrm_phone_target | |
490 | ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_phone_target.contact_id AND | |
491 | civicrm_phone_target.is_primary = 1 "; | |
492 | } | |
1f220d30 | 493 | $this->_aliases['civicrm_contact'] = 'civicrm_contact_target'; |
2f4b4d54 DS |
494 | } |
495 | ||
496 | if ($recordType == 'assignee') { | |
497 | $this->_from = " | |
498 | FROM civicrm_activity {$this->_aliases['civicrm_activity']} | |
1f220d30 DS |
499 | INNER JOIN civicrm_activity_contact {$this->_aliases['civicrm_activity_contact']} |
500 | ON {$this->_aliases['civicrm_activity']}.id = {$this->_aliases['civicrm_activity_contact']}.activity_id AND | |
501 | {$this->_aliases['civicrm_activity_contact']}.record_type_id = {$assigneeID} | |
2f4b4d54 | 502 | INNER JOIN civicrm_contact civicrm_contact_assignee |
1f220d30 | 503 | ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_contact_assignee.id |
2f4b4d54 DS |
504 | {$this->_aclFrom}"; |
505 | ||
506 | if ($this->isTableSelected('civicrm_email')) { | |
507 | $this->_from .= " | |
1f220d30 DS |
508 | LEFT JOIN civicrm_email civicrm_email_assignee |
509 | ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_email_assignee.contact_id AND | |
510 | civicrm_email_assignee.is_primary = 1"; | |
2f4b4d54 | 511 | } |
8c0ff2a6 | 512 | if ($this->isTableSelected('civicrm_phone')) { |
513 | $this->_from .= " | |
514 | LEFT JOIN civicrm_phone civicrm_phone_assignee | |
515 | ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_phone_assignee.contact_id AND | |
516 | civicrm_phone_assignee.is_primary = 1 "; | |
517 | } | |
482691e1 | 518 | $this->_aliases['civicrm_contact'] = 'civicrm_contact_assignee'; |
2f4b4d54 DS |
519 | } |
520 | ||
521 | if ($recordType == 'source') { | |
522 | $this->_from = " | |
523 | FROM civicrm_activity {$this->_aliases['civicrm_activity']} | |
1f220d30 DS |
524 | INNER JOIN civicrm_activity_contact {$this->_aliases['civicrm_activity_contact']} |
525 | ON {$this->_aliases['civicrm_activity']}.id = {$this->_aliases['civicrm_activity_contact']}.activity_id AND | |
526 | {$this->_aliases['civicrm_activity_contact']}.record_type_id = {$sourceID} | |
2f4b4d54 | 527 | INNER JOIN civicrm_contact civicrm_contact_source |
1f220d30 | 528 | ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_contact_source.id |
2f4b4d54 DS |
529 | {$this->_aclFrom}"; |
530 | ||
531 | if ($this->isTableSelected('civicrm_email')) { | |
532 | $this->_from .= " | |
1f220d30 DS |
533 | LEFT JOIN civicrm_email civicrm_email_source |
534 | ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_email_source.contact_id AND | |
535 | civicrm_email_source.is_primary = 1"; | |
2f4b4d54 | 536 | } |
8c0ff2a6 | 537 | if ($this->isTableSelected('civicrm_phone')) { |
538 | $this->_from .= " | |
539 | LEFT JOIN civicrm_phone civicrm_phone_source | |
540 | ON {$this->_aliases['civicrm_activity_contact']}.contact_id = civicrm_phone_source.contact_id AND | |
541 | civicrm_phone_source.is_primary = 1 "; | |
542 | } | |
482691e1 | 543 | $this->_aliases['civicrm_contact'] = 'civicrm_contact_source'; |
6a488035 | 544 | } |
56bbb442 | 545 | |
482691e1 | 546 | $this->addAddressFromClause(); |
6a488035 TO |
547 | } |
548 | ||
74cf4551 | 549 | /** |
a0375fa3 | 550 | * Build where clause. |
551 | * | |
552 | * @param string $recordType | |
74cf4551 | 553 | */ |
00be9182 | 554 | public function where($recordType = NULL) { |
2f4b4d54 | 555 | $this->_where = " WHERE {$this->_aliases['civicrm_activity']}.is_test = 0 AND |
6a488035 TO |
556 | {$this->_aliases['civicrm_activity']}.is_deleted = 0 AND |
557 | {$this->_aliases['civicrm_activity']}.is_current_revision = 1"; | |
558 | ||
559 | $clauses = array(); | |
560 | foreach ($this->_columns as $tableName => $table) { | |
561 | if (array_key_exists('filters', $table)) { | |
562 | ||
563 | foreach ($table['filters'] as $fieldName => $field) { | |
564 | $clause = NULL; | |
7116d719 | 565 | if ($fieldName != 'contact_' . $recordType && |
566 | (strstr($fieldName, '_target') || | |
567 | strstr($fieldName, '_assignee') || | |
568 | strstr($fieldName, '_source') | |
569 | ) | |
570 | ) { | |
7116d719 | 571 | continue; |
572 | } | |
6a488035 TO |
573 | if (CRM_Utils_Array::value('type', $field) & CRM_Utils_Type::T_DATE) { |
574 | $relative = CRM_Utils_Array::value("{$fieldName}_relative", $this->_params); | |
9d72cede EM |
575 | $from = CRM_Utils_Array::value("{$fieldName}_from", $this->_params); |
576 | $to = CRM_Utils_Array::value("{$fieldName}_to", $this->_params); | |
6a488035 TO |
577 | |
578 | $clause = $this->dateClause($field['name'], $relative, $from, $to, $field['type']); | |
579 | } | |
580 | else { | |
581 | $op = CRM_Utils_Array::value("{$fieldName}_op", $this->_params); | |
389cfee7 | 582 | if ($op && ($op != 'nnll' || $op != 'nll')) { |
6a488035 TO |
583 | $clause = $this->whereClause($field, |
584 | $op, | |
585 | CRM_Utils_Array::value("{$fieldName}_value", $this->_params), | |
586 | CRM_Utils_Array::value("{$fieldName}_min", $this->_params), | |
587 | CRM_Utils_Array::value("{$fieldName}_max", $this->_params) | |
588 | ); | |
d6c3d40f | 589 | if ($field['name'] == 'include_case_activities') { |
590 | $clause = NULL; | |
591 | } | |
9d72cede EM |
592 | if ($fieldName == 'activity_type_id' && |
593 | empty($this->_params['activity_type_id_value']) | |
594 | ) { | |
355b8a20 | 595 | if (empty($this->_params['include_case_activities_value'])) { |
596 | $this->activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'label', TRUE); | |
597 | } | |
1023b3bc | 598 | $actTypes = array_flip($this->activityTypes); |
6c552737 | 599 | $clause = "( {$this->_aliases['civicrm_activity']}.activity_type_id IN (" . |
9d72cede | 600 | implode(',', $actTypes) . ") )"; |
42b3be07 | 601 | } |
6a488035 TO |
602 | } |
603 | } | |
604 | ||
605 | if ($field['name'] == 'current_user') { | |
9d72cede EM |
606 | if (CRM_Utils_Array::value("{$fieldName}_value", $this->_params) == |
607 | 1 | |
608 | ) { | |
6a488035 TO |
609 | // get current user |
610 | $session = CRM_Core_Session::singleton(); | |
611 | if ($contactID = $session->get('userID')) { | |
389cfee7 | 612 | $clause = "{$this->_aliases['civicrm_activity_contact']}.activity_id IN |
613 | (SELECT activity_id FROM civicrm_activity_contact WHERE contact_id = {$contactID})"; | |
6a488035 TO |
614 | } |
615 | else { | |
616 | $clause = NULL; | |
617 | } | |
618 | } | |
619 | else { | |
620 | $clause = NULL; | |
621 | } | |
622 | } | |
623 | if (!empty($clause)) { | |
624 | $clauses[] = $clause; | |
625 | } | |
626 | } | |
627 | } | |
628 | } | |
629 | ||
630 | if (empty($clauses)) { | |
b6cab294 | 631 | $this->_where .= " "; |
6a488035 TO |
632 | } |
633 | else { | |
b6cab294 | 634 | $this->_where .= " AND " . implode(' AND ', $clauses); |
6a488035 TO |
635 | } |
636 | ||
637 | if ($this->_aclWhere) { | |
638 | $this->_where .= " AND {$this->_aclWhere} "; | |
639 | } | |
640 | } | |
641 | ||
a0375fa3 | 642 | /** |
643 | * Override group by function. | |
644 | */ | |
00be9182 | 645 | public function groupBy() { |
6a488035 TO |
646 | $this->_groupBy = "GROUP BY {$this->_aliases['civicrm_activity']}.id"; |
647 | } | |
648 | ||
74cf4551 | 649 | /** |
a0375fa3 | 650 | * Build ACL clause. |
651 | * | |
74cf4551 EM |
652 | * @param string $tableAlias |
653 | */ | |
00be9182 | 654 | public function buildACLClause($tableAlias = 'contact_a') { |
2a3c101f | 655 | //override for ACL( Since Contact may be source |
6a488035 TO |
656 | //contact/assignee or target also it may be null ) |
657 | ||
658 | if (CRM_Core_Permission::check('view all contacts')) { | |
659 | $this->_aclFrom = $this->_aclWhere = NULL; | |
660 | return; | |
661 | } | |
662 | ||
663 | $session = CRM_Core_Session::singleton(); | |
664 | $contactID = $session->get('userID'); | |
665 | if (!$contactID) { | |
666 | $contactID = 0; | |
667 | } | |
668 | $contactID = CRM_Utils_Type::escape($contactID, 'Integer'); | |
669 | ||
670 | CRM_Contact_BAO_Contact_Permission::cache($contactID); | |
671 | $clauses = array(); | |
672 | foreach ($tableAlias as $k => $alias) { | |
673 | $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 "; | |
674 | } | |
675 | ||
676 | $this->_aclFrom = implode(" ", $clauses); | |
677 | $this->_aclWhere = NULL; | |
678 | } | |
679 | ||
74cf4551 | 680 | /** |
100fef9d | 681 | * @param int $groupID |
74cf4551 EM |
682 | * |
683 | * @throws Exception | |
684 | */ | |
00be9182 | 685 | public function add2group($groupID) { |
9a2f601e | 686 | if (CRM_Utils_Array::value("contact_target_op", $this->_params) == 'nll') { |
687 | CRM_Core_Error::fatal(ts('Current filter criteria didn\'t have any target contact to add to group')); | |
688 | } | |
689 | ||
7f6464a0 JM |
690 | $new_select = 'AS addtogroup_contact_id'; |
691 | $select = str_ireplace('AS civicrm_contact_contact_target_id', $new_select, $this->_select); | |
692 | $new_having = ' addtogroup_contact_id'; | |
693 | $having = str_ireplace(' civicrm_contact_contact_target_id', $new_having, $this->_having); | |
694 | $query = "$select | |
9a2f601e | 695 | FROM civireport_activity_temp_target tar |
7f6464a0 | 696 | GROUP BY civicrm_activity_id $having {$this->_orderBy}"; |
9a2f601e | 697 | $select = 'AS addtogroup_contact_id'; |
698 | $query = str_ireplace('AS civicrm_contact_contact_target_id', $select, $query); | |
699 | $dao = CRM_Core_DAO::executeQuery($query); | |
700 | ||
701 | $contactIDs = array(); | |
702 | // Add resulting contacts to group | |
703 | while ($dao->fetch()) { | |
704 | if ($dao->addtogroup_contact_id) { | |
705 | $contact_id = explode(';', $dao->addtogroup_contact_id); | |
706 | if ($contact_id[0]) { | |
7bb16323 | 707 | $contactIDs[$contact_id[0]] = $contact_id[0]; |
9a2f601e | 708 | } |
709 | } | |
710 | } | |
711 | ||
9d72cede | 712 | if (!empty($contactIDs)) { |
9a2f601e | 713 | CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID); |
714 | CRM_Core_Session::setStatus(ts("Listed contact(s) have been added to the selected group."), ts('Contacts Added'), 'success'); | |
715 | } | |
716 | else { | |
717 | CRM_Core_Session::setStatus(ts("The listed records(s) cannot be added to the group.")); | |
718 | } | |
719 | } | |
720 | ||
d6c3d40f | 721 | /** |
722 | * @param $fields | |
723 | * @param $files | |
724 | * @param $self | |
725 | * | |
726 | * @return array | |
727 | */ | |
728 | public static function formRule($fields, $files, $self) { | |
729 | $errors = array(); | |
355b8a20 | 730 | $config = CRM_Core_Config::singleton(); |
731 | if (in_array("CiviCase", $config->enableComponents)) { | |
732 | $componentId = CRM_Core_Component::getComponentID('CiviCase'); | |
733 | $caseActivityTypes = CRM_Core_OptionGroup::values('activity_type', TRUE, FALSE, FALSE, " AND v.component_id={$componentId}"); | |
734 | if (!empty($fields['activity_type_id_value']) && is_array($fields['activity_type_id_value']) && empty($fields['include_case_activities_value'])) { | |
735 | foreach ($fields['activity_type_id_value'] as $activityTypeId) { | |
736 | if (in_array($activityTypeId, $caseActivityTypes)) { | |
737 | $errors['fields'] = ts("Please enable 'Include Case Activities' to filter with Case Activity types."); | |
738 | } | |
d6c3d40f | 739 | } |
740 | } | |
741 | } | |
742 | return $errors; | |
743 | } | |
744 | ||
00be9182 | 745 | public function postProcess() { |
1a6c8513 KJ |
746 | //reset value of activity_date |
747 | if (!empty($this->_force)) { | |
748 | $this->_formValues["activity_date_time_relative"] = NULL; | |
749 | } | |
2f4b4d54 DS |
750 | $this->beginPostProcess(); |
751 | ||
389cfee7 | 752 | //Assign those recordtype to array which have filter operator as 'Is not empty' or 'Is empty' |
753 | $nullFilters = array(); | |
b6cab294 | 754 | foreach (array('target', 'source', 'assignee') as $type) { |
9d72cede EM |
755 | if (CRM_Utils_Array::value("contact_{$type}_op", $this->_params) == |
756 | 'nnll' || !empty($this->_params["contact_{$type}_value"]) | |
757 | ) { | |
3ae89fd7 | 758 | $nullFilters[] = " civicrm_contact_contact_{$type}_id IS NOT NULL "; |
389cfee7 | 759 | } |
4c9b6178 | 760 | elseif (CRM_Utils_Array::value("contact_{$type}_op", $this->_params) == |
9d72cede EM |
761 | 'nll' |
762 | ) { | |
3ae89fd7 | 763 | $nullFilters[] = " civicrm_contact_contact_{$type}_id IS NULL "; |
b6cab294 | 764 | } |
765 | } | |
766 | ||
9d72cede | 767 | // 1. fill temp table with target results |
6e5e50a9 | 768 | $this->buildACLClause(array('civicrm_contact_target')); |
1f220d30 | 769 | $this->select('target'); |
2f4b4d54 | 770 | $this->from('target'); |
5b1ba2b8 | 771 | $this->customDataFrom(); |
389cfee7 | 772 | $this->where('target'); |
1f220d30 | 773 | $insertCols = implode(',', $this->_selectAliases); |
9d72cede | 774 | $tempQuery = "CREATE TEMPORARY TABLE civireport_activity_temp_target CHARACTER SET utf8 COLLATE utf8_unicode_ci AS |
482691e1 | 775 | {$this->_select} {$this->_from} {$this->_where} "; |
2f4b4d54 | 776 | CRM_Core_DAO::executeQuery($tempQuery); |
2f4b4d54 | 777 | |
1f220d30 DS |
778 | // 2. add new columns to hold assignee and source results |
779 | // fixme: add when required | |
780 | $tempQuery = " | |
781 | ALTER TABLE civireport_activity_temp_target | |
e7b3275b | 782 | MODIFY COLUMN civicrm_contact_contact_target_id VARCHAR(128), |
1f220d30 DS |
783 | ADD COLUMN civicrm_contact_contact_assignee VARCHAR(128), |
784 | ADD COLUMN civicrm_contact_contact_source VARCHAR(128), | |
7116d719 | 785 | ADD COLUMN civicrm_contact_contact_assignee_id VARCHAR(128), |
482691e1 | 786 | ADD COLUMN civicrm_contact_contact_source_id VARCHAR(128), |
8c0ff2a6 | 787 | ADD COLUMN civicrm_phone_contact_assignee_phone VARCHAR(128), |
788 | ADD COLUMN civicrm_phone_contact_source_phone VARCHAR(128), | |
1f220d30 DS |
789 | ADD COLUMN civicrm_email_contact_assignee_email VARCHAR(128), |
790 | ADD COLUMN civicrm_email_contact_source_email VARCHAR(128)"; | |
791 | CRM_Core_DAO::executeQuery($tempQuery); | |
792 | ||
793 | // 3. fill temp table with assignee results | |
6e5e50a9 | 794 | $this->buildACLClause(array('civicrm_contact_assignee')); |
389cfee7 | 795 | $this->select('assignee'); |
796 | $this->from('assignee'); | |
797 | $this->customDataFrom(); | |
798 | $this->where('assignee'); | |
799 | $insertCols = implode(',', $this->_selectAliases); | |
9d72cede | 800 | $tempQuery = "INSERT INTO civireport_activity_temp_target ({$insertCols}) |
1f220d30 | 801 | {$this->_select} |
83e74860 | 802 | {$this->_from} {$this->_where}"; |
389cfee7 | 803 | CRM_Core_DAO::executeQuery($tempQuery); |
2f4b4d54 | 804 | |
1f220d30 | 805 | // 4. fill temp table with source results |
6e5e50a9 | 806 | $this->buildACLClause(array('civicrm_contact_source')); |
389cfee7 | 807 | $this->select('source'); |
808 | $this->from('source'); | |
809 | $this->customDataFrom(); | |
810 | $this->where('source'); | |
811 | $insertCols = implode(',', $this->_selectAliases); | |
9d72cede | 812 | $tempQuery = "INSERT INTO civireport_activity_temp_target ({$insertCols}) |
1f220d30 | 813 | {$this->_select} |
83e74860 | 814 | {$this->_from} {$this->_where}"; |
389cfee7 | 815 | CRM_Core_DAO::executeQuery($tempQuery); |
2f4b4d54 | 816 | |
1f220d30 | 817 | // 5. show final result set from temp table |
2f4b4d54 | 818 | $rows = array(); |
1f220d30 | 819 | $this->select('final'); |
389cfee7 | 820 | $this->_having = ""; |
821 | if (!empty($nullFilters)) { | |
822 | $this->_having = "HAVING " . implode(' AND ', $nullFilters); | |
823 | } | |
83e74860 | 824 | $this->orderBy(); |
ecf1e543 | 825 | foreach ($this->_sections as $alias => $section) { |
826 | if (!empty($section) && $section['name'] == 'activity_date_time') { | |
827 | $this->alterSectionHeaderForDateTime('civireport_activity_temp_target', $section['tplField']); | |
828 | } | |
e06df9af | 829 | } |
1f220d30 | 830 | $this->limit(); |
7116d719 | 831 | $sql = "{$this->_select} |
832 | FROM civireport_activity_temp_target tar | |
389cfee7 | 833 | GROUP BY civicrm_activity_id {$this->_having} {$this->_orderBy} {$this->_limit}"; |
2f4b4d54 DS |
834 | $this->buildRows($sql, $rows); |
835 | ||
836 | // format result set. | |
1f220d30 | 837 | $this->formatDisplay($rows); |
2f4b4d54 DS |
838 | |
839 | // assign variables to templates | |
840 | $this->doTemplateAssignment($rows); | |
841 | ||
842 | // do print / pdf / instance stuff if needed | |
843 | $this->endPostProcess($rows); | |
6a488035 TO |
844 | } |
845 | ||
74cf4551 | 846 | /** |
4b62bc4f EM |
847 | * Alter display of rows. |
848 | * | |
849 | * Iterate through the rows retrieved via SQL and make changes for display purposes, | |
850 | * such as rendering contacts as links. | |
851 | * | |
852 | * @param array $rows | |
853 | * Rows generated by SQL, with an array for each row. | |
74cf4551 | 854 | */ |
00be9182 | 855 | public function alterDisplay(&$rows) { |
9d72cede EM |
856 | $entryFound = FALSE; |
857 | $activityType = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE); | |
6a488035 | 858 | $activityStatus = CRM_Core_PseudoConstant::activityStatus(); |
9d72cede | 859 | $viewLinks = FALSE; |
9d72cede EM |
860 | $context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'report'); |
861 | $actUrl = ''; | |
6a488035 TO |
862 | |
863 | if (CRM_Core_Permission::check('access CiviCRM')) { | |
9d72cede EM |
864 | $viewLinks = TRUE; |
865 | $onHover = ts('View Contact Summary for this Contact'); | |
6a488035 TO |
866 | $onHoverAct = ts('View Activity Record'); |
867 | } | |
38ba593b | 868 | foreach ($rows as $rowNum => $row) { |
1efec7ff | 869 | // if we have an activity type, format the View Activity link for use in various columns |
9d72cede EM |
870 | if ($viewLinks && |
871 | array_key_exists('civicrm_activity_activity_type_id', $row) | |
872 | ) { | |
1efec7ff DG |
873 | // Check for target contact id(s) and use the first contact id in that list for view activity link if found, |
874 | // else use source contact id | |
875 | if (!empty($rows[$rowNum]['civicrm_contact_contact_target_id'])) { | |
876 | $targets = explode(';', $rows[$rowNum]['civicrm_contact_contact_target_id']); | |
877 | $cid = $targets[0]; | |
878 | } | |
879 | else { | |
880 | $cid = $rows[$rowNum]['civicrm_contact_contact_source_id']; | |
881 | } | |
882 | ||
883 | $actActionLinks = CRM_Activity_Selector_Activity::actionLinks($row['civicrm_activity_activity_type_id'], | |
884 | CRM_Utils_Array::value('civicrm_activity_source_record_id', $rows[$rowNum]), | |
885 | FALSE, | |
886 | $rows[$rowNum]['civicrm_activity_id'] | |
887 | ); | |
888 | ||
889 | $actLinkValues = array( | |
890 | 'id' => $rows[$rowNum]['civicrm_activity_id'], | |
891 | 'cid' => $cid, | |
892 | 'cxt' => $context, | |
893 | ); | |
894 | $actUrl = CRM_Utils_System::url($actActionLinks[CRM_Core_Action::VIEW]['url'], | |
895 | CRM_Core_Action::replace($actActionLinks[CRM_Core_Action::VIEW]['qs'], $actLinkValues), TRUE | |
896 | ); | |
897 | } | |
898 | ||
482691e1 RN |
899 | if (array_key_exists('civicrm_contact_contact_source', $row)) { |
900 | if ($value = $row['civicrm_contact_contact_source_id']) { | |
901 | if ($viewLinks) { | |
902 | $url = CRM_Utils_System::url("civicrm/contact/view", | |
903 | 'reset=1&cid=' . $value, | |
904 | $this->_absoluteUrl | |
905 | ); | |
906 | $rows[$rowNum]['civicrm_contact_contact_source_link'] = $url; | |
907 | $rows[$rowNum]['civicrm_contact_contact_source_hover'] = $onHover; | |
908 | } | |
909 | $entryFound = TRUE; | |
910 | } | |
911 | } | |
912 | ||
913 | if (array_key_exists('civicrm_contact_contact_assignee', $row)) { | |
914 | $assigneeNames = explode(';', $row['civicrm_contact_contact_assignee']); | |
915 | if ($value = $row['civicrm_contact_contact_assignee_id']) { | |
916 | $assigneeContactIds = explode(';', $value); | |
917 | $link = array(); | |
918 | if ($viewLinks) { | |
919 | foreach ($assigneeContactIds as $id => $value) { | |
920 | if (isset($value) && isset($assigneeNames[$id])) { | |
921 | $url = CRM_Utils_System::url("civicrm/contact/view", | |
922 | 'reset=1&cid=' . $value, | |
923 | $this->_absoluteUrl | |
924 | ); | |
9d72cede EM |
925 | $link[] = "<a title='" . $onHover . "' href='" . $url . |
926 | "'>{$assigneeNames[$id]}</a>"; | |
482691e1 RN |
927 | } |
928 | } | |
929 | $rows[$rowNum]['civicrm_contact_contact_assignee'] = implode('; ', $link); | |
930 | } | |
931 | $entryFound = TRUE; | |
932 | } | |
933 | } | |
934 | ||
935 | if (array_key_exists('civicrm_contact_contact_target', $row)) { | |
936 | $targetNames = explode(';', $row['civicrm_contact_contact_target']); | |
937 | if ($value = $row['civicrm_contact_contact_target_id']) { | |
938 | $targetContactIds = explode(';', $value); | |
939 | $link = array(); | |
940 | if ($viewLinks) { | |
941 | foreach ($targetContactIds as $id => $value) { | |
942 | if (isset($value) && isset($targetNames[$id])) { | |
943 | $url = CRM_Utils_System::url("civicrm/contact/view", | |
944 | 'reset=1&cid=' . $value, | |
945 | $this->_absoluteUrl | |
946 | ); | |
9d72cede EM |
947 | $link[] = "<a title='" . $onHover . "' href='" . $url . |
948 | "'>{$targetNames[$id]}</a>"; | |
482691e1 RN |
949 | } |
950 | } | |
951 | $rows[$rowNum]['civicrm_contact_contact_target'] = implode('; ', $link); | |
952 | } | |
953 | $entryFound = TRUE; | |
954 | } | |
955 | } | |
956 | ||
6a488035 TO |
957 | if (array_key_exists('civicrm_activity_activity_type_id', $row)) { |
958 | if ($value = $row['civicrm_activity_activity_type_id']) { | |
959 | $rows[$rowNum]['civicrm_activity_activity_type_id'] = $activityType[$value]; | |
482691e1 | 960 | if ($viewLinks) { |
1efec7ff | 961 | $rows[$rowNum]['civicrm_activity_activity_type_id_link'] = $actUrl; |
482691e1 RN |
962 | $rows[$rowNum]['civicrm_activity_activity_type_id_hover'] = $onHoverAct; |
963 | } | |
6a488035 TO |
964 | $entryFound = TRUE; |
965 | } | |
966 | } | |
967 | ||
968 | if (array_key_exists('civicrm_activity_status_id', $row)) { | |
969 | if ($value = $row['civicrm_activity_status_id']) { | |
970 | $rows[$rowNum]['civicrm_activity_status_id'] = $activityStatus[$value]; | |
971 | $entryFound = TRUE; | |
972 | } | |
973 | } | |
974 | ||
34e0bdb6 | 975 | if (array_key_exists('civicrm_activity_details', $row) && $this->_outputMode == 'html') { |
1efec7ff DG |
976 | if ($value = $row['civicrm_activity_details']) { |
977 | $fullDetails = $rows[$rowNum]['civicrm_activity_details']; | |
978 | $rows[$rowNum]['civicrm_activity_details'] = substr($fullDetails, 0, strrpos(substr($fullDetails, 0, 80), ' ')); | |
979 | if ($actUrl) { | |
6e5e50a9 | 980 | $rows[$rowNum]['civicrm_activity_details'] .= " <a href='{$actUrl}' title='{$onHoverAct}'>(more)</a>"; |
1efec7ff DG |
981 | } |
982 | $entryFound = TRUE; | |
983 | } | |
984 | } | |
985 | ||
6a488035 TO |
986 | if (array_key_exists('civicrm_activity_campaign_id', $row)) { |
987 | if ($value = $row['civicrm_activity_campaign_id']) { | |
988 | $rows[$rowNum]['civicrm_activity_campaign_id'] = $this->activeCampaigns[$value]; | |
989 | $entryFound = TRUE; | |
990 | } | |
991 | } | |
992 | ||
993 | if (array_key_exists('civicrm_activity_engagement_level', $row)) { | |
994 | if ($value = $row['civicrm_activity_engagement_level']) { | |
995 | $rows[$rowNum]['civicrm_activity_engagement_level'] = $this->engagementLevels[$value]; | |
996 | $entryFound = TRUE; | |
997 | } | |
998 | } | |
999 | ||
9d72cede EM |
1000 | if (array_key_exists('civicrm_activity_activity_date_time', $row) && |
1001 | array_key_exists('civicrm_activity_status_id', $row) | |
1002 | ) { | |
6a488035 TO |
1003 | if (CRM_Utils_Date::overdue($rows[$rowNum]['civicrm_activity_activity_date_time']) && |
1004 | $activityStatus[$row['civicrm_activity_status_id']] != 'Completed' | |
1005 | ) { | |
1006 | $rows[$rowNum]['class'] = "status-overdue"; | |
1007 | $entryFound = TRUE; | |
1008 | } | |
1009 | } | |
1010 | ||
482691e1 RN |
1011 | $entryFound = $this->alterDisplayAddressFields($row, $rows, $rowNum, 'activity', 'List all activities for this ') ? TRUE : $entryFound; |
1012 | ||
6a488035 TO |
1013 | if (!$entryFound) { |
1014 | break; | |
1015 | } | |
1016 | } | |
1017 | } | |
667abe3a | 1018 | |
00be9182 | 1019 | public function sectionTotals() { |
667abe3a | 1020 | // Reports using order_bys with sections must populate $this->_selectAliases in select() method. |
1021 | if (empty($this->_selectAliases)) { | |
1022 | return; | |
1023 | } | |
1024 | ||
1025 | if (!empty($this->_sections)) { | |
1026 | // pull section aliases out of $this->_sections | |
1027 | $sectionAliases = array_keys($this->_sections); | |
1028 | ||
1029 | $ifnulls = array(); | |
1030 | foreach (array_merge($sectionAliases, $this->_selectAliases) as $alias) { | |
1031 | $ifnulls[] = "ifnull($alias, '') as $alias"; | |
1032 | } | |
1033 | ||
1034 | $query = "select " . implode(", ", $ifnulls) . | |
e13b3d8e | 1035 | ", count(DISTINCT civicrm_activity_id) as ct from civireport_activity_temp_target group by " . |
667abe3a | 1036 | implode(", ", $sectionAliases); |
1037 | ||
1038 | // initialize array of total counts | |
1039 | $totals = array(); | |
1040 | $dao = CRM_Core_DAO::executeQuery($query); | |
1041 | while ($dao->fetch()) { | |
1042 | // let $this->_alterDisplay translate any integer ids to human-readable values. | |
1043 | $rows[0] = $dao->toArray(); | |
1044 | $this->alterDisplay($rows); | |
1045 | $row = $rows[0]; | |
1046 | ||
1047 | // add totals for all permutations of section values | |
1048 | $values = array(); | |
1049 | $i = 1; | |
1050 | $aliasCount = count($sectionAliases); | |
1051 | foreach ($sectionAliases as $alias) { | |
1052 | $values[] = $row[$alias]; | |
1053 | $key = implode(CRM_Core_DAO::VALUE_SEPARATOR, $values); | |
1054 | if ($i == $aliasCount) { | |
1055 | // the last alias is the lowest-level section header; use count as-is | |
1056 | $totals[$key] = $dao->ct; | |
1057 | } | |
1058 | else { | |
1059 | // other aliases are higher level; roll count into their total | |
1060 | $totals[$key] += $dao->ct; | |
1061 | } | |
1062 | } | |
1063 | } | |
1064 | $this->assign('sectionTotals', $totals); | |
1065 | } | |
1066 | } | |
96025800 | 1067 | |
6a488035 | 1068 | } |