Merge pull request #16802 from colemanw/coalesce
[civicrm-core.git] / CRM / Campaign / Form / Survey.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 extends CRM_Core_Form {
22
23 /**
fe482240 24 * The id of the object being edited.
6a488035
TO
25 *
26 * @var int
27 */
28 protected $_surveyId;
29
30 /**
fe482240 31 * Action.
6a488035
TO
32 *
33 * @var int
34 */
3a936dab 35 public $_action;
6a488035
TO
36
37 /**
fe482240 38 * SurveyTitle.
6a488035
TO
39 *
40 * @var string
41 */
42 protected $_surveyTitle;
43
14c67729
MWMC
44 /**
45 * Explicitly declare the entity api name.
46 */
47 public function getDefaultEntity() {
48 return 'Survey';
49 }
50
51 /**
52 * Get the entity id being edited.
53 *
54 * @return int|null
55 */
56 public function getEntityId() {
57 return $this->_surveyId;
58 }
59
6a488035
TO
60 public function preProcess() {
61 if (!CRM_Campaign_BAO_Campaign::accessCampaign()) {
62 CRM_Utils_System::permissionDenied();
63 }
64
353ffa53 65 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add', 'REQUEST');
6a488035
TO
66 $this->_surveyId = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE);
67
68 if ($this->_surveyId) {
69 $this->_single = TRUE;
70
be2fb01f 71 $params = ['id' => $this->_surveyId];
6a488035
TO
72 CRM_Campaign_BAO_Survey::retrieve($params, $surveyInfo);
73 $this->_surveyTitle = $surveyInfo['title'];
74 $this->assign('surveyTitle', $this->_surveyTitle);
be2fb01f 75 CRM_Utils_System::setTitle(ts('Configure Survey - %1', [1 => $this->_surveyTitle]));
6a488035
TO
76 }
77
78 $this->assign('action', $this->_action);
79 $this->assign('surveyId', $this->_surveyId);
80
14c67729
MWMC
81 // Add custom data to form
82 CRM_Custom_Form_CustomData::addToForm($this);
98c54bff 83
6a488035
TO
84 // CRM-11480, CRM-11682
85 // Preload libraries required by the "Questions" tab
86 CRM_UF_Page_ProfileEditor::registerProfileScripts();
be2fb01f 87 CRM_UF_Page_ProfileEditor::registerSchemas(['IndividualModel', 'ActivityModel']);
6a488035
TO
88
89 CRM_Campaign_Form_Survey_TabHeader::build($this);
90 }
91
92 /**
fe482240 93 * Build the form object.
6a488035
TO
94 */
95 public function buildQuickForm() {
96 $session = CRM_Core_Session::singleton();
97 if ($this->_surveyId) {
be2fb01f
CW
98 $buttons = [
99 [
6a488035
TO
100 'type' => 'upload',
101 'name' => ts('Save'),
102 'isDefault' => TRUE,
be2fb01f
CW
103 ],
104 [
6a488035
TO
105 'type' => 'upload',
106 'name' => ts('Save and Done'),
107 'subName' => 'done',
be2fb01f
CW
108 ],
109 [
6a488035
TO
110 'type' => 'upload',
111 'name' => ts('Save and Next'),
112 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
113 'subName' => 'next',
be2fb01f
CW
114 ],
115 ];
6a488035
TO
116 }
117 else {
be2fb01f
CW
118 $buttons = [
119 [
6a488035 120 'type' => 'upload',
f212d37d 121 'name' => ts('Continue'),
6a488035
TO
122 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
123 'isDefault' => TRUE,
be2fb01f
CW
124 ],
125 ];
6a488035 126 }
be2fb01f 127 $buttons[] = [
28a04ea9 128 'type' => 'cancel',
129 'name' => ts('Cancel'),
be2fb01f 130 ];
6a488035
TO
131 $this->addButtons($buttons);
132
133 $url = CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=survey');
134 $session->replaceUserContext($url);
135 }
136
00be9182 137 public function endPostProcess() {
6a488035
TO
138 // make submit buttons keep the current working tab opened.
139 if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) {
140 $tabTitle = $className = CRM_Utils_String::getClassName($this->_name);
141 if ($tabTitle == 'Main') {
142 $tabTitle = 'Main settings';
143 }
353ffa53 144 $subPage = strtolower($className);
be2fb01f 145 CRM_Core_Session::setStatus(ts("'%1' have been saved.", [1 => $tabTitle]), ts('Saved'), 'success');
6a488035
TO
146
147 $this->postProcessHook();
148
28a04ea9 149 if ($this->_action & CRM_Core_Action::ADD) {
6a488035 150 CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/survey/configure/questions",
353ffa53 151 "action=update&reset=1&id={$this->_surveyId}"));
28a04ea9 152 }
6a488035
TO
153 if ($this->controller->getButtonName('submit') == "_qf_{$className}_upload_done") {
154 CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=survey'));
155 }
4c9b6178 156 elseif ($this->controller->getButtonName('submit') == "_qf_{$className}_upload_next") {
6a488035
TO
157 $subPage = CRM_Campaign_Form_Survey_TabHeader::getNextTab($this);
158 CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/survey/configure/{$subPage}",
353ffa53 159 "action=update&reset=1&id={$this->_surveyId}"));
6a488035
TO
160 }
161 else {
162 CRM_Utils_System::redirect(CRM_Utils_System::url("civicrm/survey/configure/{$subPage}",
353ffa53 163 "action=update&reset=1&id={$this->_surveyId}"));
6a488035
TO
164 }
165 }
166 }
167
30c4e065
EM
168 /**
169 * @return string
170 */
00be9182 171 public function getTemplateFileName() {
481a74f4 172 if ($this->controller->getPrint() || $this->getVar('_surveyId') <= 0) {
6a488035
TO
173 return parent::getTemplateFileName();
174 }
175 else {
176 // hack lets suppress the form rendering for now
177 self::$_template->assign('isForm', FALSE);
178 return 'CRM/Campaign/Form/Survey/Tab.tpl';
179 }
180 }
96025800 181
6a488035 182}