Query.php 23.1 KB
Newer Older
totten's avatar
totten committed
1 2 3
<?php
/*
 +--------------------------------------------------------------------+
yashodha's avatar
yashodha committed
4
 | CiviCRM version 4.6                                                |
totten's avatar
totten committed
5
 +--------------------------------------------------------------------+
kurund's avatar
kurund committed
6
 | Copyright CiviCRM LLC (c) 2004-2014                                |
totten's avatar
totten committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License and the CiviCRM Licensing Exception along                  |
 | with this program; if not, contact CiviCRM LLC                     |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
26
 */
totten's avatar
totten committed
27 28 29 30

/**
 *
 * @package CRM
kurund's avatar
kurund committed
31
 * @copyright CiviCRM LLC (c) 2004-2014
totten's avatar
totten committed
32 33 34 35 36 37
 * $Id$
 *
 */
class CRM_Activity_BAO_Query {

  /**
colemanw's avatar
colemanw committed
38
   * Build select for Case
totten's avatar
totten committed
39
   *
40 41
   * @param $query
   *
totten's avatar
totten committed
42 43
   * @return void
   */
44
  public static function select(&$query) {
45
    if (!empty($query->_returnProperties['activity_id'])) {
totten's avatar
totten committed
46 47 48 49 50
      $query->_select['activity_id'] = "civicrm_activity.id as activity_id";
      $query->_element['activity_id'] = 1;
      $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
    }

51
    if (!empty($query->_returnProperties['activity_type_id'])) {
52
      $query->_select['activity_type_id'] = "activity_type.value as activity_type_id";
totten's avatar
totten committed
53 54 55 56 57 58 59
      $query->_element['activity_type_id'] = 1;
      $query->_tables['civicrm_activity'] = 1;
      $query->_tables['activity_type'] = 1;
      $query->_whereTables['civicrm_activity'] = 1;
      $query->_whereTables['activity_type'] = 1;
    }

60
    if (!empty($query->_returnProperties['activity_type'])) {
61
      $query->_select['activity_type'] = "activity_type.label as activity_type";
totten's avatar
totten committed
62 63 64 65 66 67 68
      $query->_element['activity_type'] = 1;
      $query->_tables['civicrm_activity'] = 1;
      $query->_tables['activity_type'] = 1;
      $query->_whereTables['civicrm_activity'] = 1;
      $query->_whereTables['activity_type'] = 1;
    }

69
    if (!empty($query->_returnProperties['activity_subject'])) {
totten's avatar
totten committed
70 71 72 73 74
      $query->_select['activity_subject'] = "civicrm_activity.subject as activity_subject";
      $query->_element['activity_subject'] = 1;
      $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
    }

75
    if (!empty($query->_returnProperties['activity_date_time'])) {
totten's avatar
totten committed
76 77 78 79 80
      $query->_select['activity_date_time'] = "civicrm_activity.activity_date_time as activity_date_time";
      $query->_element['activity_date_time'] = 1;
      $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
    }

81
    if (!empty($query->_returnProperties['activity_status_id'])) {
totten's avatar
totten committed
82 83 84 85 86 87 88 89
      $query->_select['activity_status_id'] = "activity_status.value as activity_status_id";
      $query->_element['activity_status_id'] = 1;
      $query->_tables['civicrm_activity'] = 1;
      $query->_tables['activity_status'] = 1;
      $query->_whereTables['civicrm_activity'] = 1;
      $query->_whereTables['activity_status'] = 1;
    }

90
    if (!empty($query->_returnProperties['activity_status'])) {
91 92
      $query->_select['activity_status'] = "activity_status.label as activity_status,
      civicrm_activity.status_id as status_id";
totten's avatar
totten committed
93 94 95 96 97 98 99
      $query->_element['activity_status'] = 1;
      $query->_tables['civicrm_activity'] = 1;
      $query->_tables['activity_status'] = 1;
      $query->_whereTables['civicrm_activity'] = 1;
      $query->_whereTables['activity_status'] = 1;
    }

100
    if (!empty($query->_returnProperties['activity_duration'])) {
totten's avatar
totten committed
101 102 103 104 105
      $query->_select['activity_duration'] = "civicrm_activity.duration as activity_duration";
      $query->_element['activity_duration'] = 1;
      $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
    }

106
    if (!empty($query->_returnProperties['activity_location'])) {
totten's avatar
totten committed
107 108 109 110 111
      $query->_select['activity_location'] = "civicrm_activity.location as activity_location";
      $query->_element['activity_location'] = 1;
      $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
    }

112
    if (!empty($query->_returnProperties['activity_details'])) {
totten's avatar
totten committed
113 114 115 116 117
      $query->_select['activity_details'] = "civicrm_activity.details as activity_details";
      $query->_element['activity_details'] = 1;
      $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
    }

118
    if (!empty($query->_returnProperties['source_record_id'])) {
totten's avatar
totten committed
119 120 121 122 123
      $query->_select['source_record_id'] = "civicrm_activity.source_record_id as source_record_id";
      $query->_element['source_record_id'] = 1;
      $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
    }

124
    if (!empty($query->_returnProperties['activity_is_test'])) {
totten's avatar
totten committed
125 126 127 128 129
      $query->_select['activity_is_test'] = "civicrm_activity.is_test as activity_is_test";
      $query->_element['activity_is_test'] = 1;
      $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
    }

130
    if (!empty($query->_returnProperties['activity_campaign_id'])) {
totten's avatar
totten committed
131 132 133 134 135
      $query->_select['activity_campaign_id'] = 'civicrm_activity.campaign_id as activity_campaign_id';
      $query->_element['activity_campaign_id'] = 1;
      $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
    }

136
    if (!empty($query->_returnProperties['activity_engagement_level'])) {
totten's avatar
totten committed
137 138 139 140 141
      $query->_select['activity_engagement_level'] = 'civicrm_activity.engagement_level as activity_engagement_level';
      $query->_element['activity_engagement_level'] = 1;
      $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
    }

142
    if (!empty($query->_returnProperties['source_contact'])) {
143
      $query->_select['source_contact'] = 'source_contact.sort_name as source_contact';
totten's avatar
totten committed
144 145 146
      $query->_element['source_contact'] = 1;
      $query->_tables['source_contact'] = $query->_whereTables['source_contact'] = 1;
    }
147

148
    if (!empty($query->_returnProperties['activity_result'])) {
149 150 151 152
      $query->_select['activity_result'] = 'civicrm_activity.result as activity_result';
      $query->_element['result'] = 1;
      $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
    }
153

154 155 156 157 158
    if (CRM_Utils_Array::value('parent_id', $query->_returnProperties)) {
      $query->_tables['parent_id'] = 1;
      $query->_whereTables['parent_id'] = 1;
      $query->_element['parent_id'] = 1;
    }
totten's avatar
totten committed
159 160 161 162 163 164
  }

  /**
   * Given a list of conditions in query generate the required
   * where clause
   *
Eileen McNaughton's avatar
Eileen McNaughton committed
165 166
   * @param $query
   *
totten's avatar
totten committed
167 168
   * @return void
   */
169
  public static function where(&$query) {
totten's avatar
totten committed
170 171 172 173 174
    foreach (array_keys($query->_params) as $id) {
      if (substr($query->_params[$id][0], 0, 9) == 'activity_') {
        if ($query->_mode == CRM_Contact_BAO_QUERY::MODE_CONTACTS) {
          $query->_useDistinct = TRUE;
        }
175
        $query->_params[$id][3];
totten's avatar
totten committed
176 177 178 179 180 181
        self::whereClauseSingle($query->_params[$id], $query);
      }
    }
  }

  /**
colemanw's avatar
colemanw committed
182
   * Where clause for a single field
totten's avatar
totten committed
183
   *
Eileen McNaughton's avatar
Eileen McNaughton committed
184 185
   * @param $values
   * @param $query
totten's avatar
totten committed
186 187
   * @return void
   */
188
  public static function whereClauseSingle(&$values, &$query) {
189
    list($name, $op, $value, $grouping) = $values;
totten's avatar
totten committed
190

monishdeb's avatar
monishdeb committed
191
    $fields = CRM_Activity_BAO_Activity::exportableFields();
totten's avatar
totten committed
192 193 194 195 196 197 198
    $query->_tables['civicrm_activity'] = $query->_whereTables['civicrm_activity'] = 1;
    if ($query->_mode & CRM_Contact_BAO_Query::MODE_ACTIVITY) {
      $query->_skipDeleteClause = TRUE;
    }

    switch ($name) {
      case 'activity_type_id':
monishdeb's avatar
monishdeb committed
199 200 201 202 203 204 205
      case 'activity_status_id':
      case 'activity_engagement_level':
      case 'activity_subject':
      case 'activity_id':
        $qillName = $name;
        if (in_array($name, array('activity_engagement_level', 'activity_id'))) {
          $name = $qillName = str_replace('activity_', '', $name);
totten's avatar
totten committed
206
        }
monishdeb's avatar
monishdeb committed
207 208 209 210 211 212
        if (in_array($name, array('activity_status_id', 'activity_subject'))) {
          $name = str_replace('activity_', '', $name);
          $qillName = str_replace('_id', '', $qillName);
        }
        $dataType = !empty($fields[$qillName]['type']) ? CRM_Utils_Type::typeToString($fields[$qillName]['type']) : 'String';

monishdeb's avatar
monishdeb committed
213
        $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_activity.$name", $op, $value, $dataType);
monishdeb's avatar
monishdeb committed
214 215
        list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Activity_DAO_Activity', $name, $value, $op);
        $query->_qill[$grouping][] = ts('%1 %2 %3', array(1 => $fields[$qillName]['title'], 2 => $op, 3 => $value));
totten's avatar
totten committed
216 217
        break;

monishdeb's avatar
monishdeb committed
218 219 220 221 222 223 224 225
      case 'activity_type':
      case 'activity_status':
        $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("$name.label", $op, $value, 'String');
        list($op, $value) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Activity_DAO_Activity', $name, $value, $op);
        $query->_qill[$grouping][] = ts('%1 %2 %3', array(1 => $fields[$name]['title'], 2 => $op, 3 => $value));
        $query->_tables[$name] = $query->_whereTables[$name] = 1;
        break;

totten's avatar
totten committed
226 227 228 229 230
      case 'activity_survey_id':
        if (!$value) {
          break;
        }
        $value = CRM_Utils_Type::escape($value, 'Integer');
231
        $query->_where[$grouping][] = " civicrm_activity.source_record_id = $value";
totten's avatar
totten committed
232 233 234 235 236
        $query->_qill[$grouping][] = ts('Survey') . ' - ' . CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $value, 'title');
        break;

      case 'activity_role':
        CRM_Contact_BAO_Query::$_activityRole = $values[2];
237
        $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
yashodha's avatar
yashodha committed
238 239 240
        $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
        $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
        $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
241

242
        if ($values[2]) {
totten's avatar
totten committed
243
          $query->_tables['civicrm_activity_contact'] = $query->_whereTables['civicrm_activity_contact'] = 1;
244
          if ($values[2] == 1) {
yashodha's avatar
yashodha committed
245
            $query->_where[$grouping][] = " civicrm_activity_contact.record_type_id = $sourceID";
246 247
            $query->_qill[$grouping][] = ts('Activity created by');
          }
248
          elseif ($values[2] == 2) {
yashodha's avatar
yashodha committed
249
            $query->_where[$grouping][] = " civicrm_activity_contact.record_type_id = $assigneeID";
250 251
            $query->_qill[$grouping][] = ts('Activity assigned to');
          }
252
          elseif ($values[2] == 3) {
yashodha's avatar
yashodha committed
253
            $query->_where[$grouping][] = " civicrm_activity_contact.record_type_id = $targetID";
254 255
            $query->_qill[$grouping][] = ts('Activity targeted to');
          }
totten's avatar
totten committed
256 257 258 259
        }
        break;

      case 'activity_test':
260
        // We don't want to include all tests for sql OR CRM-7827
261
        if (!$value || $query->getOperator() != 'OR') {
totten's avatar
totten committed
262
          $query->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("civicrm_activity.is_test", $op, $value, "Boolean");
263 264 265
          if ($value) {
            $query->_qill[$grouping][] = ts('Activity is a Test');
          }
totten's avatar
totten committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
        }
        break;

      case 'activity_date':
      case 'activity_date_low':
      case 'activity_date_high':
        $query->dateQueryBuilder($values,
          'civicrm_activity', 'activity_date', 'activity_date_time', ts('Activity Date')
        );
        break;

      case 'activity_taglist':
        $taglist = $value;
        $value = array();
        foreach ($taglist as $val) {
          if ($val) {
            $val = explode(',', $val);
            foreach ($val as $tId) {
              if (is_numeric($tId)) {
                $value[$tId] = 1;
              }
            }
          }
        }
290

totten's avatar
totten committed
291 292
      case 'activity_tags':
        $value = array_keys($value);
293
        $activityTags = CRM_Core_PseudoConstant::get('CRM_Core_DAO_EntityTag', 'tag_id', array('onlyActive' => FALSE));
totten's avatar
totten committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313

        $names = array();
        if (is_array($value)) {
          foreach ($value as $k => $v) {
            $names[] = $activityTags[$v];
          }
        }
        $query->_where[$grouping][] = "civicrm_activity_tag.tag_id IN (" . implode(",", $value) . ")";
        $query->_qill[$grouping][] = ts('Activity Tag %1', array(1 => $op)) . ' ' . implode(' ' . ts('OR') . ' ', $names);
        $query->_tables['civicrm_activity_tag'] = $query->_whereTables['civicrm_activity_tag'] = 1;
        break;

      case 'activity_campaign_id':
        $campParams = array(
          'op' => $op,
          'campaign' => $value,
          'grouping' => $grouping,
          'tableName' => 'civicrm_activity',
        );
        CRM_Campaign_BAO_Query::componentSearchClause($campParams, $query);
314
        break;
totten's avatar
totten committed
315

316
      case 'activity_result':
317
        if (is_array($value)) {
318
          $safe = NULL;
totten's avatar
totten committed
319
          while (list(, $k) = each($value)) {
320 321 322 323 324 325
            $safe[] = "'" . CRM_Utils_Type::escape($k, 'String') . "'";
          }
          $query->_where[$grouping][] = "civicrm_activity.result IN (" . implode(',', $safe) . ")";
          $query->_qill[$grouping][] = ts("Activity Result - %1", array(1 => implode(' or ', $safe)));
        }
        break;
326 327 328 329 330 331 332 333 334 335 336

      case 'parent_id':
        if ($value == 1) {
          $query->_where[$grouping][] = "parent_id.parent_id IS NOT NULL";
          $query->_qill[$grouping][] = ts('Activities which have Followup Activities');
        }
        elseif ($value == 2) {
          $query->_where[$grouping][] = "parent_id.parent_id IS NULL";
          $query->_qill[$grouping][] = ts('Activities without Followup Activities');
        }
        break;
337

338 339 340 341 342 343 344
      case 'followup_parent_id':
        if ($value == 1) {
          $query->_where[$grouping][] = "civicrm_activity.parent_id IS NOT NULL";
          $query->_qill[$grouping][] = ts('Activities which are Followup Activities');
        }
        elseif ($value == 2) {
          $query->_where[$grouping][] = "civicrm_activity.parent_id IS NULL";
totten's avatar
totten committed
345
          $query->_qill[$grouping][] = ts('Activities which are not Followup Activities');
346 347
        }
        break;
totten's avatar
totten committed
348 349 350
    }
  }

351
  /**
colemanw's avatar
colemanw committed
352
   * @param string $name
353 354 355 356 357
   * @param $mode
   * @param $side
   *
   * @return null|string
   */
358
  public static function from($name, $mode, $side) {
totten's avatar
totten committed
359 360 361
    $from = NULL;
    switch ($name) {
      case 'civicrm_activity':
yashodha's avatar
yashodha committed
362
        //CRM-7480 we are going to civicrm_activity table either
totten's avatar
totten committed
363 364 365
        //from civicrm_activity_target or civicrm_activity_assignment.
        //as component specific activities does not have entry in
        //activity target table so lets consider civicrm_activity_assignment.
366 367 368 369 370
        $from .= " INNER JOIN civicrm_activity_contact
                      ON ( civicrm_activity_contact.contact_id = contact_a.id ) ";
        $from .= " INNER JOIN civicrm_activity
                      ON ( civicrm_activity.id = civicrm_activity_contact.activity_id
                      AND civicrm_activity.is_deleted = 0 AND civicrm_activity.is_current_revision = 1 )";
totten's avatar
totten committed
371 372 373 374

        break;

      case 'activity_status':
lobo's avatar
lobo committed
375
        $from .= " $side JOIN civicrm_option_group option_group_activity_status ON (option_group_activity_status.name = 'activity_status')";
totten's avatar
totten committed
376 377 378 379 380
        $from .= " $side JOIN civicrm_option_value activity_status ON (civicrm_activity.status_id = activity_status.value
                               AND option_group_activity_status.id = activity_status.option_group_id ) ";
        break;

      case 'activity_type':
lobo's avatar
lobo committed
381
        $from .= " $side JOIN civicrm_option_group option_group_activity_type ON (option_group_activity_type.name = 'activity_type')";
totten's avatar
totten committed
382 383 384 385 386 387 388 389 390
        $from .= " $side JOIN civicrm_option_value activity_type ON (civicrm_activity.activity_type_id = activity_type.value
                               AND option_group_activity_type.id = activity_type.option_group_id ) ";
        break;

      case 'civicrm_activity_tag':
        $from .= " $side JOIN civicrm_entity_tag as civicrm_activity_tag ON ( civicrm_activity_tag.entity_table = 'civicrm_activity' AND civicrm_activity_tag.entity_id = civicrm_activity.id ) ";
        break;

      case 'source_contact':
391 392 393 394 395 396
        $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
        $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
        $from = "
        LEFT JOIN civicrm_activity_contact ac
                      ON ( ac.activity_id = civicrm_activity_contact.activity_id AND ac.record_type_id = {$sourceID})
        INNER JOIN civicrm_contact source_contact ON (ac.contact_id = source_contact.id)";
totten's avatar
totten committed
397
        break;
398

399 400 401
      case 'parent_id':
        $from = "$side JOIN civicrm_activity AS parent_id ON civicrm_activity.id = parent_id.parent_id";
        break;
totten's avatar
totten committed
402 403 404 405 406 407
    }

    return $from;
  }

  /**
colemanw's avatar
colemanw committed
408
   * Getter for the qill object
totten's avatar
totten committed
409 410 411
   *
   * @return string
   */
412
  public function qill() {
totten's avatar
totten committed
413 414 415 416
    return (isset($this->_qill)) ? $this->_qill : "";
  }

  /**
colemanw's avatar
colemanw committed
417
   * Add all the elements shared between case activity search and advanced search
totten's avatar
totten committed
418 419
   *
   *
colemanw's avatar
colemanw committed
420
   * @param CRM_Core_Form $form
totten's avatar
totten committed
421 422
   * @return void
   */
423
  public static function buildSearchForm(&$form) {
totten's avatar
totten committed
424
    $activityOptions = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE);
monishdeb's avatar
monishdeb committed
425 426 427
    $form->addSelect('activity_type_id',
      array('entity' => 'activity', 'label' => 'Activity Type(s)', 'multiple' => 'multiple', 'option_url' => NULL, 'placeholder' => ts('- any -'))
    );
totten's avatar
totten committed
428 429

    CRM_Core_Form_Date::buildDateRange($form, 'activity_date', 1, '_low', '_high', ts('From'), FALSE, FALSE);
430 431 432 433 434 435
    $followUpActivity = array(
      1 => ts('Yes'),
      2 => ts('No'),
    );
    $form->addRadio('parent_id', NULL, $followUpActivity, array('allowClear' => TRUE));
    $form->addRadio('followup_parent_id', NULL, $followUpActivity, array('allowClear' => TRUE));
436
    $activityRoles = array(
437
      3 => ts('With'),
438
      2 => ts('Assigned to'),
439
      1 => ts('Added by'),
440
    );
441
    $form->addRadio('activity_role', NULL, $activityRoles, array('allowClear' => TRUE));
442
    $form->setDefaults(array('activity_role' => 3));
monishdeb's avatar
monishdeb committed
443 444 445 446 447
    $activityStatus = CRM_Core_PseudoConstant::get('CRM_Activity_DAO_Activity', 'status_id', array('flip' => 1, 'labelColumn' => 'name'));
    $form->addSelect('status_id',
      array('entity' => 'activity', 'multiple' => 'multiple', 'option_url' => NULL, 'placeholder' => ts('- any -'))
    );
    $form->setDefaults(array('status_id' => array($activityStatus['Completed'], $activityStatus['Scheduled'])));
totten's avatar
totten committed
448 449 450 451 452 453 454 455 456 457 458 459
    $form->addElement('text', 'activity_subject', ts('Subject'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name'));
    $form->addYesNo('activity_test', ts('Activity is a Test?'));
    $activity_tags = CRM_Core_BAO_Tag::getTags('civicrm_activity');
    if ($activity_tags) {
      foreach ($activity_tags as $tagID => $tagName) {
        $form->_tagElement = &$form->addElement('checkbox', "activity_tags[$tagID]",
          NULL, $tagName
        );
      }
    }

    $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_activity');
colemanw's avatar
colemanw committed
460
    CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_activity', NULL, TRUE, TRUE);
totten's avatar
totten committed
461

462
    $surveys = CRM_Campaign_BAO_Survey::getSurveys(TRUE, FALSE, FALSE, TRUE);
pratikshad's avatar
pratikshad committed
463 464 465 466 467 468
    if ($surveys) {
      $form->add('select', 'activity_survey_id', ts('Survey / Petition'),
                 array('' => ts('- none -')) + $surveys, FALSE,
                 array('class' => 'crm-select2')
                 );
    }
totten's avatar
totten committed
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
    $extends = array('Activity');
    $groupDetails = CRM_Core_BAO_CustomGroup::getGroupDetail(NULL, TRUE, $extends);
    if ($groupDetails) {
      $form->assign('activityGroupTree', $groupDetails);
      foreach ($groupDetails as $group) {
        foreach ($group['fields'] as $field) {
          $fieldId = $field['id'];
          $elementName = 'custom_' . $fieldId;
          CRM_Core_BAO_CustomField::addQuickFormElement($form, $elementName, $fieldId, FALSE, FALSE, TRUE);
        }
      }
    }

    CRM_Campaign_BAO_Campaign::addCampaignInComponentSearch($form, 'activity_campaign_id');

    //add engagement level CRM-7775
    $buildEngagementLevel = FALSE;
486
    $buildSurveyResult = FALSE;
totten's avatar
totten committed
487 488 489 490
    if (CRM_Campaign_BAO_Campaign::isCampaignEnable() &&
      CRM_Campaign_BAO_Campaign::accessCampaign()
    ) {
      $buildEngagementLevel = TRUE;
491
      $form->addSelect('activity_engagement_level', array('entity' => 'activity', 'context' => 'search'));
totten's avatar
totten committed
492

493
      // Add survey result field.
totten's avatar
totten committed
494
      $optionGroups = CRM_Campaign_BAO_Survey::getResultSets('name');
495
      $resultOptions = array();
496 497
      foreach ($optionGroups as $gid => $name) {
        if ($name) {
498 499 500
          $value = array();
          $value = CRM_Core_OptionGroup::values($name);
          if (!empty($value)) {
totten's avatar
totten committed
501
            while (list($k, $v) = each($value)) {
502 503 504 505 506 507 508
              $resultOptions[$v] = $v;
            }
          }
        }
      }
      // If no survey result options have been created, don't build
      // the field to avoid clutter.
509
      if (count($resultOptions) > 0) {
510 511 512 513
        $buildSurveyResult = TRUE;
        asort($resultOptions);
        $form->add('select', 'activity_result', ts("Survey Result"),
          $resultOptions, FALSE,
514
          array('id' => 'activity_result', 'multiple' => 'multiple', 'class' => 'crm-select2')
515 516 517
        );
      }
    }
518

totten's avatar
totten committed
519
    $form->assign('buildEngagementLevel', $buildEngagementLevel);
520
    $form->assign('buildSurveyResult', $buildSurveyResult);
totten's avatar
totten committed
521 522 523
    $form->setDefaults(array('activity_test' => 0));
  }

524 525 526 527 528 529
  /**
   * @param $mode
   * @param bool $includeCustomFields
   *
   * @return array|null
   */
530
  public static function defaultReturnProperties($mode, $includeCustomFields = TRUE) {
totten's avatar
totten committed
531 532 533 534 535 536 537 538 539
    $properties = NULL;
    if ($mode & CRM_Contact_BAO_Query::MODE_ACTIVITY) {
      $properties = array(
        'activity_id' => 1,
        'contact_type' => 1,
        'contact_sub_type' => 1,
        'sort_name' => 1,
        'display_name' => 1,
        'activity_type' => 1,
Tyrell Cook's avatar
Tyrell Cook committed
540
        'activity_type_id' => 1,
totten's avatar
totten committed
541 542 543 544 545 546
        'activity_subject' => 1,
        'activity_date_time' => 1,
        'activity_duration' => 1,
        'activity_location' => 1,
        'activity_details' => 1,
        'activity_status' => 1,
547
        'source_contact' => 1,
totten's avatar
totten committed
548 549 550
        'source_record_id' => 1,
        'activity_is_test' => 1,
        'activity_campaign_id' => 1,
551
        'result' => 1,
totten's avatar
totten committed
552
        'activity_engagement_level' => 1,
553
        'parent_id' => 1,
totten's avatar
totten committed
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
      );

      if ($includeCustomFields) {
        // also get all the custom activity properties
        $fields = CRM_Core_BAO_CustomField::getFieldsForImport('Activity');
        if (!empty($fields)) {
          foreach ($fields as $name => $dontCare) {
            $properties[$name] = 1;
          }
        }
      }
    }

    return $properties;
  }
569

totten's avatar
totten committed
570
}