Questions.php 5.01 KB
Newer Older
totten's avatar
totten committed
1 2 3
<?php
/*
 +--------------------------------------------------------------------+
totten's avatar
totten committed
4
 | CiviCRM version 5                                                  |
totten's avatar
totten committed
5
 +--------------------------------------------------------------------+
Seamus Lee's avatar
Seamus Lee committed
6
 | Copyright CiviCRM LLC (c) 2004-2019                                |
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
Seamus Lee's avatar
Seamus Lee committed
31
 * @copyright CiviCRM LLC (c) 2004-2019
totten's avatar
totten committed
32 33 34
 */

/**
eileenmcnaugton's avatar
eileenmcnaugton committed
35
 * This class generates form components for processing a survey.
totten's avatar
totten committed
36 37 38 39
 */
class CRM_Campaign_Form_Survey_Questions extends CRM_Campaign_Form_Survey {

  /**
eileenmcnaugton's avatar
eileenmcnaugton committed
40 41 42
   * Set default values for the form.
   *
   * Note that in edit/view mode the default values are retrieved from the database.
totten's avatar
totten committed
43
   *
44 45
   * @return array
   *   array of default values
totten's avatar
totten committed
46
   */
47
  public function setDefaultValues() {
48
    $defaults = [];
totten's avatar
totten committed
49

50
    $ufJoinParams = [
totten's avatar
totten committed
51 52 53
      'entity_table' => 'civicrm_survey',
      'module' => 'CiviCampaign',
      'entity_id' => $this->_surveyId,
54
    ];
totten's avatar
totten committed
55

kurund's avatar
kurund committed
56 57
    list($defaults['contact_profile_id'], $second)
      = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
totten's avatar
totten committed
58 59 60 61 62 63
    $defaults['activity_profile_id'] = $second ? array_shift($second) : '';

    return $defaults;
  }

  /**
64
   * Build the form object.
totten's avatar
totten committed
65 66 67 68
   */
  public function buildQuickForm() {
    $subTypeId = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $this->_surveyId, 'activity_type_id');
    if (!CRM_Core_BAO_CustomGroup::autoCreateByActivityType($subTypeId)) {
69 70
      // everything
      $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE, FALSE);
totten's avatar
totten committed
71
      // FIXME: Displays weird "/\ Array" message; doesn't work with tabs
lobo's avatar
lobo committed
72 73 74
      CRM_Core_Session::setStatus(
        ts(
          'There are no custom data sets for activity type "%1". To create one, <a href="%2" target="%3">click here</a>.',
75
          [
lobo's avatar
lobo committed
76 77
            1 => $activityTypes[$subTypeId],
            2 => CRM_Utils_System::url('civicrm/admin/custom/group', 'action=add&reset=1'),
78
            3 => '_blank',
79
          ]
lobo's avatar
lobo committed
80 81
        )
      );
totten's avatar
totten committed
82 83 84
    }

    $allowCoreTypes = CRM_Campaign_BAO_Survey::surveyProfileTypes();
85 86 87 88 89 90 91
    $allowSubTypes = [
      'ActivityType' => [$subTypeId],
    ];
    $entities = [
      ['entity_name' => 'contact_1', 'entity_type' => 'IndividualModel'],
      ['entity_name' => 'activity_1', 'entity_type' => 'ActivityModel', 'entity_sub_type' => $subTypeId],
    ];
totten's avatar
totten committed
92 93 94 95 96 97 98 99 100
    $this->addProfileSelector('contact_profile_id', ts('Contact Info'), $allowCoreTypes, $allowSubTypes, $entities);
    $this->addProfileSelector('activity_profile_id', ts('Questions'), $allowCoreTypes, $allowSubTypes, $entities);
    // Note: Because this is in a tab, we also preload the schema via CRM_Campaign_Form_Survey::preProcess

    parent::buildQuickForm();
  }


  /**
101
   * Process the form.
totten's avatar
totten committed
102 103 104 105 106 107
   */
  public function postProcess() {
    // store the submitted values in an array
    $params = $this->controller->exportValues($this->_name);

    // also update the ProfileModule tables
108
    $ufJoinParams = [
totten's avatar
totten committed
109
      'is_active' => 1,
totten's avatar
totten committed
110
      'module' => 'CiviCampaign',
totten's avatar
totten committed
111
      'entity_table' => 'civicrm_survey',
totten's avatar
totten committed
112
      'entity_id' => $this->_surveyId,
113
    ];
totten's avatar
totten committed
114 115 116 117

    // first delete all past entries
    CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams);

118
    $uf = [];
totten's avatar
totten committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    $wt = 2;
    if (!empty($params['contact_profile_id'])) {
      $uf[1] = $params['contact_profile_id'];
      $wt = 1;
    }
    if (!empty($params['activity_profile_id'])) {
      $uf[2] = $params['activity_profile_id'];
    }

    $uf = array_values($uf);
    if (!empty($uf)) {
      foreach ($uf as $weight => $ufGroupId) {
        $ufJoinParams['weight'] = $weight + $wt;
        $ufJoinParams['uf_group_id'] = $ufGroupId;
        CRM_Core_BAO_UFJoin::create($ufJoinParams);
        unset($ufJoinParams['id']);
      }
    }

    parent::endPostProcess();
  }
140

totten's avatar
totten committed
141
}