Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class generates form components for processing a survey | |
38 | * | |
39 | */ | |
40 | class CRM_Campaign_Form_Survey_Main extends CRM_Campaign_Form_Survey { | |
41 | ||
42 | /* values | |
e70a7fc0 TO |
43 | * |
44 | * @var array | |
45 | */ | |
6a488035 TO |
46 | |
47 | public $_values; | |
48 | ||
49 | /** | |
fe482240 | 50 | * Context. |
6a488035 TO |
51 | * |
52 | * @var string | |
53 | */ | |
54 | protected $_context; | |
d5965a37 | 55 | |
6e62b28c TM |
56 | /** |
57 | * Explicitly declare the entity api name. | |
58 | */ | |
59 | public function getDefaultEntity() { | |
60 | return 'Survey'; | |
61 | } | |
6a488035 TO |
62 | |
63 | public function preProcess() { | |
64 | parent::preProcess(); | |
65 | ||
66 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this); | |
67 | ||
68 | $this->assign('context', $this->_context); | |
69 | ||
70 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this); | |
71 | ||
72 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
73 | CRM_Utils_System::setTitle(ts('Configure Survey') . ' - ' . $this->_surveyTitle); | |
74 | } | |
75 | ||
76 | $this->_cdType = CRM_Utils_Array::value('type', $_GET); | |
77 | $this->assign('cdType', FALSE); | |
78 | if ($this->_cdType) { | |
79 | $this->assign('cdType', TRUE); | |
80 | return CRM_Custom_Form_CustomData::preProcess($this); | |
81 | } | |
82 | ||
83 | // when custom data is included in this page | |
a7488080 | 84 | if (!empty($_POST['hidden_custom'])) { |
6a488035 TO |
85 | CRM_Custom_Form_CustomData::preProcess($this); |
86 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
87 | } | |
88 | ||
89 | if ($this->_name != 'Petition') { | |
90 | $url = CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=survey'); | |
91 | CRM_Utils_System::appendBreadCrumb(array(array('title' => ts('Survey Dashboard'), 'url' => $url))); | |
92 | } | |
93 | ||
94 | $this->_values = $this->get('values'); | |
95 | if (!is_array($this->_values)) { | |
96 | $this->_values = array(); | |
97 | if ($this->_surveyId) { | |
98 | $params = array('id' => $this->_surveyId); | |
99 | CRM_Campaign_BAO_Survey::retrieve($params, $this->_values); | |
100 | } | |
101 | $this->set('values', $this->_values); | |
102 | } | |
103 | ||
104 | $this->assign('action', $this->_action); | |
105 | $this->assign('surveyId', $this->_surveyId); | |
106 | // for custom data | |
107 | $this->assign('entityID', $this->_surveyId); | |
108 | } | |
109 | ||
110 | /** | |
c490a46a | 111 | * Set default values for the form. Note that in edit/view mode |
6a488035 TO |
112 | * the default values are retrieved from the database |
113 | * | |
a6c01b45 CW |
114 | * @return array |
115 | * array of default values | |
6a488035 | 116 | */ |
00be9182 | 117 | public function setDefaultValues() { |
6a488035 TO |
118 | if ($this->_cdType) { |
119 | return CRM_Custom_Form_CustomData::setDefaultValues($this); | |
120 | } | |
121 | ||
122 | $defaults = $this->_values; | |
123 | ||
124 | if ($this->_surveyId) { | |
125 | ||
8cc574cf | 126 | if (!empty($defaults['result_id']) && !empty($defaults['recontact_interval'])) { |
6a488035 TO |
127 | |
128 | $resultId = $defaults['result_id']; | |
129 | $recontactInterval = unserialize($defaults['recontact_interval']); | |
130 | ||
131 | unset($defaults['recontact_interval']); | |
132 | $defaults['option_group_id'] = $resultId; | |
133 | } | |
134 | } | |
135 | ||
136 | if (!isset($defaults['is_active'])) { | |
137 | $defaults['is_active'] = 1; | |
138 | } | |
139 | ||
140 | $defaultSurveys = CRM_Campaign_BAO_Survey::getSurveys(TRUE, TRUE); | |
141 | if (!isset($defaults['is_default']) && empty($defaultSurveys)) { | |
142 | $defaults['is_default'] = 1; | |
143 | } | |
144 | ||
145 | return $defaults; | |
146 | } | |
147 | ||
148 | /** | |
fe482240 | 149 | * Build the form object. |
6a488035 | 150 | * |
6a488035 | 151 | * @return void |
6a488035 TO |
152 | */ |
153 | public function buildQuickForm() { | |
154 | if ($this->_cdType) { | |
155 | return CRM_Custom_Form_CustomData::buildQuickForm($this); | |
156 | } | |
157 | ||
158 | $this->add('text', 'title', ts('Title'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'title'), TRUE); | |
159 | ||
160 | $surveyActivityTypes = CRM_Campaign_BAO_Survey::getSurveyActivityType(); | |
161 | // Activity Type id | |
4c17373d | 162 | $this->addSelect('activity_type_id', array('option_url' => 'civicrm/admin/campaign/surveyType'), TRUE); |
6a488035 TO |
163 | |
164 | // Campaign id | |
165 | $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns(CRM_Utils_Array::value('campaign_id', $this->_values)); | |
166 | $this->add('select', 'campaign_id', ts('Campaign'), array('' => ts('- select -')) + $campaigns); | |
167 | ||
168 | // script / instructions | |
5d51a2f9 | 169 | $this->add('wysiwyg', 'instructions', ts('Instructions for interviewers'), array('rows' => 5, 'cols' => 40)); |
6a488035 TO |
170 | |
171 | // release frequency | |
e71ee30d | 172 | $this->add('text', 'release_frequency', ts('Release Frequency'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'release_frequency')); |
6a488035 TO |
173 | |
174 | $this->addRule('release_frequency', ts('Release Frequency interval should be a positive number.'), 'positiveInteger'); | |
175 | ||
176 | // max reserved contacts at a time | |
177 | $this->add('text', 'default_number_of_contacts', ts('Maximum reserved at one time'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'default_number_of_contacts')); | |
178 | $this->addRule('default_number_of_contacts', ts('Maximum reserved at one time should be a positive number'), 'positiveInteger'); | |
179 | ||
180 | // total reserved per interviewer | |
181 | $this->add('text', 'max_number_of_contacts', ts('Total reserved per interviewer'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'max_number_of_contacts')); | |
182 | $this->addRule('max_number_of_contacts', ts('Total reserved contacts should be a positive number'), 'positiveInteger'); | |
183 | ||
184 | // is active ? | |
185 | $this->add('checkbox', 'is_active', ts('Active?')); | |
186 | ||
187 | // is default ? | |
188 | $this->add('checkbox', 'is_default', ts('Default?')); | |
189 | ||
190 | parent::buildQuickForm(); | |
191 | } | |
192 | ||
193 | /** | |
fe482240 | 194 | * Process the form. |
6a488035 | 195 | * |
6a488035 | 196 | * @return void |
6a488035 TO |
197 | */ |
198 | public function postProcess() { | |
199 | // store the submitted values in an array | |
200 | $params = $this->controller->exportValues($this->_name); | |
201 | ||
202 | $session = CRM_Core_Session::singleton(); | |
203 | ||
204 | $params['last_modified_id'] = $session->get('userID'); | |
205 | $params['last_modified_date'] = date('YmdHis'); | |
206 | ||
207 | if ($this->_surveyId) { | |
208 | $params['id'] = $this->_surveyId; | |
209 | } | |
210 | else { | |
211 | $params['created_id'] = $session->get('userID'); | |
212 | $params['created_date'] = date('YmdHis'); | |
213 | } | |
214 | ||
215 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, 0); | |
216 | $params['is_default'] = CRM_Utils_Array::value('is_default', $params, 0); | |
217 | ||
218 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, | |
219 | $customFields, | |
220 | $this->_surveyId, | |
221 | 'Survey' | |
222 | ); | |
223 | $survey = CRM_Campaign_BAO_Survey::create($params); | |
224 | $this->_surveyId = $survey->id; | |
225 | ||
a7488080 | 226 | if (!empty($this->_values['result_id'])) { |
6a488035 | 227 | $query = "SELECT COUNT(*) FROM civicrm_survey WHERE result_id = %1"; |
353ffa53 | 228 | $countSurvey = (int) CRM_Core_DAO::singleValueQuery($query, |
6a488035 | 229 | array( |
353ffa53 TO |
230 | 1 => array( |
231 | $this->_values['result_id'], | |
6a488035 | 232 | 'Positive', |
acb1052e | 233 | ), |
353ffa53 | 234 | ) |
6a488035 TO |
235 | ); |
236 | // delete option group if no any survey is using it. | |
237 | if (!$countSurvey) { | |
238 | CRM_Core_BAO_OptionGroup::del($this->_values['result_id']); | |
239 | } | |
240 | } | |
241 | ||
242 | parent::endPostProcess(); | |
243 | } | |
96025800 | 244 | |
6a488035 | 245 | } |