APIv4 - Add Address::getCoordinates action
[civicrm-core.git] / CRM / Campaign / Form / Survey / Questions.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
3819f101 19 * This class generates form components for processing a survey.
6a488035
TO
20 */
21class CRM_Campaign_Form_Survey_Questions extends CRM_Campaign_Form_Survey {
22
23 /**
3819f101 24 * Set default values for the form.
25 *
26 * Note that in edit/view mode the default values are retrieved from the database.
6a488035 27 *
a6c01b45
CW
28 * @return array
29 * array of default values
6a488035 30 */
00be9182 31 public function setDefaultValues() {
be2fb01f 32 $defaults = [];
6a488035 33
be2fb01f 34 $ufJoinParams = [
6a488035
TO
35 'entity_table' => 'civicrm_survey',
36 'module' => 'CiviCampaign',
37 'entity_id' => $this->_surveyId,
be2fb01f 38 ];
6a488035 39
79d7553f 40 list($defaults['contact_profile_id'], $second)
41 = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
6a488035
TO
42 $defaults['activity_profile_id'] = $second ? array_shift($second) : '';
43
44 return $defaults;
45 }
46
47 /**
fe482240 48 * Build the form object.
6a488035
TO
49 */
50 public function buildQuickForm() {
51 $subTypeId = CRM_Core_DAO::getFieldValue('CRM_Campaign_DAO_Survey', $this->_surveyId, 'activity_type_id');
52 if (!CRM_Core_BAO_CustomGroup::autoCreateByActivityType($subTypeId)) {
5d4fcf54
TO
53 // everything
54 $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE, FALSE);
6a488035 55 // FIXME: Displays weird "/\ Array" message; doesn't work with tabs
342936e2
DL
56 CRM_Core_Session::setStatus(
57 ts(
58 'There are no custom data sets for activity type "%1". To create one, <a href="%2" target="%3">click here</a>.',
be2fb01f 59 [
342936e2 60 1 => $activityTypes[$subTypeId],
aca7613b 61 2 => CRM_Utils_System::url('civicrm/admin/custom/group/edit', 'action=add&reset=1'),
21dfd5f5 62 3 => '_blank',
be2fb01f 63 ]
342936e2
DL
64 )
65 );
6a488035
TO
66 }
67
68 $allowCoreTypes = CRM_Campaign_BAO_Survey::surveyProfileTypes();
be2fb01f
CW
69 $allowSubTypes = [
70 'ActivityType' => [$subTypeId],
71 ];
72 $entities = [
73 ['entity_name' => 'contact_1', 'entity_type' => 'IndividualModel'],
74 ['entity_name' => 'activity_1', 'entity_type' => 'ActivityModel', 'entity_sub_type' => $subTypeId],
75 ];
6a488035
TO
76 $this->addProfileSelector('contact_profile_id', ts('Contact Info'), $allowCoreTypes, $allowSubTypes, $entities);
77 $this->addProfileSelector('activity_profile_id', ts('Questions'), $allowCoreTypes, $allowSubTypes, $entities);
78 // Note: Because this is in a tab, we also preload the schema via CRM_Campaign_Form_Survey::preProcess
79
80 parent::buildQuickForm();
81 }
82
6a488035 83 /**
fe482240 84 * Process the form.
6a488035
TO
85 */
86 public function postProcess() {
87 // store the submitted values in an array
88 $params = $this->controller->exportValues($this->_name);
89
90 // also update the ProfileModule tables
be2fb01f 91 $ufJoinParams = [
6a488035 92 'is_active' => 1,
353ffa53 93 'module' => 'CiviCampaign',
6a488035 94 'entity_table' => 'civicrm_survey',
353ffa53 95 'entity_id' => $this->_surveyId,
be2fb01f 96 ];
6a488035
TO
97
98 // first delete all past entries
99 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams);
100
be2fb01f 101 $uf = [];
6a488035
TO
102 $wt = 2;
103 if (!empty($params['contact_profile_id'])) {
104 $uf[1] = $params['contact_profile_id'];
105 $wt = 1;
106 }
107 if (!empty($params['activity_profile_id'])) {
108 $uf[2] = $params['activity_profile_id'];
109 }
110
111 $uf = array_values($uf);
112 if (!empty($uf)) {
113 foreach ($uf as $weight => $ufGroupId) {
114 $ufJoinParams['weight'] = $weight + $wt;
115 $ufJoinParams['uf_group_id'] = $ufGroupId;
116 CRM_Core_BAO_UFJoin::create($ufJoinParams);
117 unset($ufJoinParams['id']);
118 }
119 }
120
121 parent::endPostProcess();
122 }
96025800 123
6a488035 124}