Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
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 | |
43 | * | |
44 | * @var array | |
45 | */ | |
46 | ||
47 | public $_values; | |
48 | ||
49 | /** | |
50 | * context | |
51 | * | |
52 | * @var string | |
53 | */ | |
54 | protected $_context; | |
55 | ||
56 | public function preProcess() { | |
57 | parent::preProcess(); | |
58 | ||
59 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this); | |
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 | ||
69 | $this->_cdType = CRM_Utils_Array::value('type', $_GET); | |
70 | $this->assign('cdType', FALSE); | |
71 | if ($this->_cdType) { | |
72 | $this->assign('cdType', TRUE); | |
73 | return CRM_Custom_Form_CustomData::preProcess($this); | |
74 | } | |
75 | ||
76 | // when custom data is included in this page | |
77 | if (CRM_Utils_Array::value('hidden_custom', $_POST)) { | |
78 | CRM_Custom_Form_CustomData::preProcess($this); | |
79 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
80 | } | |
81 | ||
82 | if ($this->_name != 'Petition') { | |
83 | $url = CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=survey'); | |
84 | CRM_Utils_System::appendBreadCrumb(array(array('title' => ts('Survey Dashboard'), 'url' => $url))); | |
85 | } | |
86 | ||
87 | $this->_values = $this->get('values'); | |
88 | if (!is_array($this->_values)) { | |
89 | $this->_values = array(); | |
90 | if ($this->_surveyId) { | |
91 | $params = array('id' => $this->_surveyId); | |
92 | CRM_Campaign_BAO_Survey::retrieve($params, $this->_values); | |
93 | } | |
94 | $this->set('values', $this->_values); | |
95 | } | |
96 | ||
97 | $this->assign('action', $this->_action); | |
98 | $this->assign('surveyId', $this->_surveyId); | |
99 | // for custom data | |
100 | $this->assign('entityID', $this->_surveyId); | |
101 | } | |
102 | ||
103 | /** | |
104 | * This function sets the default values for the form. Note that in edit/view mode | |
105 | * the default values are retrieved from the database | |
106 | * | |
107 | * @param null | |
108 | * | |
109 | * @return array array of default values | |
110 | * @access public | |
111 | */ | |
112 | function setDefaultValues() { | |
113 | if ($this->_cdType) { | |
114 | return CRM_Custom_Form_CustomData::setDefaultValues($this); | |
115 | } | |
116 | ||
117 | $defaults = $this->_values; | |
118 | ||
119 | if ($this->_surveyId) { | |
120 | ||
121 | if (CRM_Utils_Array::value('result_id', $defaults) && | |
122 | CRM_Utils_Array::value('recontact_interval', $defaults) | |
123 | ) { | |
124 | ||
125 | $resultId = $defaults['result_id']; | |
126 | $recontactInterval = unserialize($defaults['recontact_interval']); | |
127 | ||
128 | unset($defaults['recontact_interval']); | |
129 | $defaults['option_group_id'] = $resultId; | |
130 | } | |
131 | } | |
132 | ||
133 | if (!isset($defaults['is_active'])) { | |
134 | $defaults['is_active'] = 1; | |
135 | } | |
136 | ||
137 | $defaultSurveys = CRM_Campaign_BAO_Survey::getSurveys(TRUE, TRUE); | |
138 | if (!isset($defaults['is_default']) && empty($defaultSurveys)) { | |
139 | $defaults['is_default'] = 1; | |
140 | } | |
141 | ||
142 | return $defaults; | |
143 | } | |
144 | ||
145 | /** | |
146 | * Function to actually build the form | |
147 | * | |
148 | * @param null | |
149 | * | |
150 | * @return void | |
151 | * @access public | |
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 | |
162 | $this->add('select', 'activity_type_id', ts('Activity Type'), array('' => ts('- select -')) + $surveyActivityTypes, TRUE); | |
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 | |
169 | $this->addWysiwyg('instructions', ts('Instructions for interviewers'), array('rows' => 5, 'cols' => 40)); | |
170 | ||
171 | // release frequency | |
172 | $this->add('text', 'release_frequency', ts('Release frequency'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'release_frequency')); | |
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 | /** | |
194 | * Process the form | |
195 | * | |
196 | * @param null | |
197 | * | |
198 | * @return void | |
199 | * @access public | |
200 | */ | |
201 | public function postProcess() { | |
202 | // store the submitted values in an array | |
203 | $params = $this->controller->exportValues($this->_name); | |
204 | ||
205 | $session = CRM_Core_Session::singleton(); | |
206 | ||
207 | $params['last_modified_id'] = $session->get('userID'); | |
208 | $params['last_modified_date'] = date('YmdHis'); | |
209 | ||
210 | if ($this->_surveyId) { | |
211 | $params['id'] = $this->_surveyId; | |
212 | } | |
213 | else { | |
214 | $params['created_id'] = $session->get('userID'); | |
215 | $params['created_date'] = date('YmdHis'); | |
216 | } | |
217 | ||
218 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, 0); | |
219 | $params['is_default'] = CRM_Utils_Array::value('is_default', $params, 0); | |
220 | ||
221 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, | |
222 | $customFields, | |
223 | $this->_surveyId, | |
224 | 'Survey' | |
225 | ); | |
226 | $survey = CRM_Campaign_BAO_Survey::create($params); | |
227 | $this->_surveyId = $survey->id; | |
228 | ||
229 | if (CRM_Utils_Array::value('result_id', $this->_values)) { | |
230 | $query = "SELECT COUNT(*) FROM civicrm_survey WHERE result_id = %1"; | |
231 | $countSurvey = (int)CRM_Core_DAO::singleValueQuery($query, | |
232 | array( | |
233 | 1 => array($this->_values['result_id'], | |
234 | 'Positive', | |
235 | )) | |
236 | ); | |
237 | // delete option group if no any survey is using it. | |
238 | if (!$countSurvey) { | |
239 | CRM_Core_BAO_OptionGroup::del($this->_values['result_id']); | |
240 | } | |
241 | } | |
242 | ||
243 | parent::endPostProcess(); | |
244 | } | |
245 | } | |
246 |