Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
3819f101 | 35 | * This class generates form components for processing a survey. |
6a488035 TO |
36 | */ |
37 | class CRM_Campaign_Form_Survey_Main extends CRM_Campaign_Form_Survey { | |
38 | ||
f157740d SL |
39 | /** |
40 | * values | |
e70a7fc0 TO |
41 | * |
42 | * @var array | |
f157740d | 43 | * |
e70a7fc0 | 44 | */ |
6a488035 | 45 | |
f157740d | 46 | |
6a488035 TO |
47 | public $_values; |
48 | ||
49 | /** | |
fe482240 | 50 | * Context. |
6a488035 TO |
51 | * |
52 | * @var string | |
53 | */ | |
54 | protected $_context; | |
d5965a37 | 55 | |
6a488035 TO |
56 | public function preProcess() { |
57 | parent::preProcess(); | |
58 | ||
edc80cda | 59 | $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this); |
6a488035 TO |
60 | |
61 | $this->assign('context', $this->_context); | |
62 | ||
63 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this); | |
64 | ||
65 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
66 | CRM_Utils_System::setTitle(ts('Configure Survey') . ' - ' . $this->_surveyTitle); | |
67 | } | |
68 | ||
14c67729 MWMC |
69 | // Add custom data to form |
70 | CRM_Custom_Form_CustomData::addToForm($this); | |
6a488035 TO |
71 | |
72 | if ($this->_name != 'Petition') { | |
73 | $url = CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=survey'); | |
be2fb01f | 74 | CRM_Utils_System::appendBreadCrumb([['title' => ts('Survey Dashboard'), 'url' => $url]]); |
6a488035 TO |
75 | } |
76 | ||
77 | $this->_values = $this->get('values'); | |
78 | if (!is_array($this->_values)) { | |
be2fb01f | 79 | $this->_values = []; |
6a488035 | 80 | if ($this->_surveyId) { |
be2fb01f | 81 | $params = ['id' => $this->_surveyId]; |
6a488035 TO |
82 | CRM_Campaign_BAO_Survey::retrieve($params, $this->_values); |
83 | } | |
84 | $this->set('values', $this->_values); | |
85 | } | |
86 | ||
87 | $this->assign('action', $this->_action); | |
88 | $this->assign('surveyId', $this->_surveyId); | |
6a488035 TO |
89 | } |
90 | ||
91 | /** | |
c490a46a | 92 | * Set default values for the form. Note that in edit/view mode |
6a488035 TO |
93 | * the default values are retrieved from the database |
94 | * | |
a6c01b45 CW |
95 | * @return array |
96 | * array of default values | |
6a488035 | 97 | */ |
00be9182 | 98 | public function setDefaultValues() { |
6a488035 TO |
99 | |
100 | $defaults = $this->_values; | |
101 | ||
102 | if ($this->_surveyId) { | |
103 | ||
8cc574cf | 104 | if (!empty($defaults['result_id']) && !empty($defaults['recontact_interval'])) { |
6a488035 TO |
105 | |
106 | $resultId = $defaults['result_id']; | |
107 | $recontactInterval = unserialize($defaults['recontact_interval']); | |
108 | ||
109 | unset($defaults['recontact_interval']); | |
110 | $defaults['option_group_id'] = $resultId; | |
111 | } | |
112 | } | |
113 | ||
114 | if (!isset($defaults['is_active'])) { | |
115 | $defaults['is_active'] = 1; | |
116 | } | |
117 | ||
118 | $defaultSurveys = CRM_Campaign_BAO_Survey::getSurveys(TRUE, TRUE); | |
119 | if (!isset($defaults['is_default']) && empty($defaultSurveys)) { | |
120 | $defaults['is_default'] = 1; | |
121 | } | |
122 | ||
123 | return $defaults; | |
124 | } | |
125 | ||
126 | /** | |
fe482240 | 127 | * Build the form object. |
6a488035 TO |
128 | */ |
129 | public function buildQuickForm() { | |
6a488035 TO |
130 | $this->add('text', 'title', ts('Title'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'title'), TRUE); |
131 | ||
6a488035 | 132 | // Activity Type id |
be2fb01f | 133 | $this->addSelect('activity_type_id', ['option_url' => 'civicrm/admin/campaign/surveyType'], TRUE); |
6a488035 | 134 | |
f7b32533 | 135 | $this->addEntityRef('campaign_id', ts('Campaign'), [ |
af00ced5 | 136 | 'entity' => 'Campaign', |
f7b32533 | 137 | 'create' => TRUE, |
33ccf435 | 138 | 'select' => ['minimumInputLength' => 0], |
f7b32533 | 139 | ]); |
6a488035 TO |
140 | |
141 | // script / instructions | |
be2fb01f | 142 | $this->add('wysiwyg', 'instructions', ts('Instructions for interviewers'), ['rows' => 5, 'cols' => 40]); |
6a488035 TO |
143 | |
144 | // release frequency | |
9970fd53 | 145 | $this->add('number', 'release_frequency', ts('Release Frequency'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'release_frequency')); |
6a488035 TO |
146 | |
147 | $this->addRule('release_frequency', ts('Release Frequency interval should be a positive number.'), 'positiveInteger'); | |
148 | ||
149 | // max reserved contacts at a time | |
9970fd53 | 150 | $this->add('number', 'default_number_of_contacts', ts('Maximum reserved at one time'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'default_number_of_contacts')); |
6a488035 TO |
151 | $this->addRule('default_number_of_contacts', ts('Maximum reserved at one time should be a positive number'), 'positiveInteger'); |
152 | ||
153 | // total reserved per interviewer | |
9970fd53 | 154 | $this->add('number', 'max_number_of_contacts', ts('Total reserved per interviewer'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'max_number_of_contacts')); |
6a488035 TO |
155 | $this->addRule('max_number_of_contacts', ts('Total reserved contacts should be a positive number'), 'positiveInteger'); |
156 | ||
157 | // is active ? | |
158 | $this->add('checkbox', 'is_active', ts('Active?')); | |
159 | ||
160 | // is default ? | |
161 | $this->add('checkbox', 'is_default', ts('Default?')); | |
162 | ||
163 | parent::buildQuickForm(); | |
164 | } | |
165 | ||
166 | /** | |
fe482240 | 167 | * Process the form. |
6a488035 TO |
168 | */ |
169 | public function postProcess() { | |
170 | // store the submitted values in an array | |
171 | $params = $this->controller->exportValues($this->_name); | |
172 | ||
173 | $session = CRM_Core_Session::singleton(); | |
174 | ||
175 | $params['last_modified_id'] = $session->get('userID'); | |
176 | $params['last_modified_date'] = date('YmdHis'); | |
177 | ||
178 | if ($this->_surveyId) { | |
179 | $params['id'] = $this->_surveyId; | |
180 | } | |
181 | else { | |
182 | $params['created_id'] = $session->get('userID'); | |
183 | $params['created_date'] = date('YmdHis'); | |
184 | } | |
185 | ||
186 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, 0); | |
187 | $params['is_default'] = CRM_Utils_Array::value('is_default', $params, 0); | |
188 | ||
14c67729 MWMC |
189 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $this->getEntityId(), $this->getDefaultEntity()); |
190 | ||
6a488035 TO |
191 | $survey = CRM_Campaign_BAO_Survey::create($params); |
192 | $this->_surveyId = $survey->id; | |
193 | ||
a7488080 | 194 | if (!empty($this->_values['result_id'])) { |
6a488035 | 195 | $query = "SELECT COUNT(*) FROM civicrm_survey WHERE result_id = %1"; |
353ffa53 | 196 | $countSurvey = (int) CRM_Core_DAO::singleValueQuery($query, |
be2fb01f CW |
197 | [ |
198 | 1 => [ | |
353ffa53 | 199 | $this->_values['result_id'], |
6a488035 | 200 | 'Positive', |
be2fb01f CW |
201 | ], |
202 | ] | |
6a488035 TO |
203 | ); |
204 | // delete option group if no any survey is using it. | |
205 | if (!$countSurvey) { | |
206 | CRM_Core_BAO_OptionGroup::del($this->_values['result_id']); | |
207 | } | |
208 | } | |
209 | ||
210 | parent::endPostProcess(); | |
211 | } | |
96025800 | 212 | |
6a488035 | 213 | } |