Merge pull request #17531 from colemanw/customIcon
[civicrm-core.git] / CRM / Campaign / Form / Survey / Questions.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class generates form components for processing a survey.
20 */
21 class CRM_Campaign_Form_Survey_Questions extends CRM_Campaign_Form_Survey {
22
23 /**
24 * Set default values for the form.
25 *
26 * Note that in edit/view mode the default values are retrieved from the database.
27 *
28 * @return array
29 * array of default values
30 */
31 public function setDefaultValues() {
32 $defaults = [];
33
34 $ufJoinParams = [
35 'entity_table' => 'civicrm_survey',
36 'module' => 'CiviCampaign',
37 'entity_id' => $this->_surveyId,
38 ];
39
40 list($defaults['contact_profile_id'], $second)
41 = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
42 $defaults['activity_profile_id'] = $second ? array_shift($second) : '';
43
44 return $defaults;
45 }
46
47 /**
48 * Build the form object.
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)) {
53 // everything
54 $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE, FALSE);
55 // FIXME: Displays weird "/\ Array" message; doesn't work with tabs
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>.',
59 [
60 1 => $activityTypes[$subTypeId],
61 2 => CRM_Utils_System::url('civicrm/admin/custom/group', 'action=add&reset=1'),
62 3 => '_blank',
63 ]
64 )
65 );
66 }
67
68 $allowCoreTypes = CRM_Campaign_BAO_Survey::surveyProfileTypes();
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 ];
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
83 /**
84 * Process the form.
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
91 $ufJoinParams = [
92 'is_active' => 1,
93 'module' => 'CiviCampaign',
94 'entity_table' => 'civicrm_survey',
95 'entity_id' => $this->_surveyId,
96 ];
97
98 // first delete all past entries
99 CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams);
100
101 $uf = [];
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 }
123
124 }