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