3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * This class generates form components for processing a survey.
37 class CRM_Campaign_Form_Survey_Main
extends CRM_Campaign_Form_Survey
{
53 public function preProcess() {
56 $this->_context
= CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this);
58 $this->assign('context', $this->_context
);
60 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this);
62 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
63 CRM_Utils_System
::setTitle(ts('Configure Survey') . ' - ' . $this->_surveyTitle
);
66 // Add custom data to form
67 CRM_Custom_Form_CustomData
::addToForm($this);
69 if ($this->_name
!= 'Petition') {
70 $url = CRM_Utils_System
::url('civicrm/campaign', 'reset=1&subPage=survey');
71 CRM_Utils_System
::appendBreadCrumb(array(array('title' => ts('Survey Dashboard'), 'url' => $url)));
74 $this->_values
= $this->get('values');
75 if (!is_array($this->_values
)) {
76 $this->_values
= array();
77 if ($this->_surveyId
) {
78 $params = array('id' => $this->_surveyId
);
79 CRM_Campaign_BAO_Survey
::retrieve($params, $this->_values
);
81 $this->set('values', $this->_values
);
84 $this->assign('action', $this->_action
);
85 $this->assign('surveyId', $this->_surveyId
);
89 * Set default values for the form. Note that in edit/view mode
90 * the default values are retrieved from the database
93 * array of default values
95 public function setDefaultValues() {
97 $defaults = $this->_values
;
99 if ($this->_surveyId
) {
101 if (!empty($defaults['result_id']) && !empty($defaults['recontact_interval'])) {
103 $resultId = $defaults['result_id'];
104 $recontactInterval = unserialize($defaults['recontact_interval']);
106 unset($defaults['recontact_interval']);
107 $defaults['option_group_id'] = $resultId;
111 if (!isset($defaults['is_active'])) {
112 $defaults['is_active'] = 1;
115 $defaultSurveys = CRM_Campaign_BAO_Survey
::getSurveys(TRUE, TRUE);
116 if (!isset($defaults['is_default']) && empty($defaultSurveys)) {
117 $defaults['is_default'] = 1;
124 * Build the form object.
126 public function buildQuickForm() {
127 $this->add('text', 'title', ts('Title'), CRM_Core_DAO
::getAttribute('CRM_Campaign_DAO_Survey', 'title'), TRUE);
130 $this->addSelect('activity_type_id', array('option_url' => 'civicrm/admin/campaign/surveyType'), TRUE);
132 $this->addEntityRef('campaign_id', ts('Campaign'), [
133 'entity' => 'Campaign',
135 'select' => ['minimumInputLength' => 0],
138 // script / instructions
139 $this->add('wysiwyg', 'instructions', ts('Instructions for interviewers'), array('rows' => 5, 'cols' => 40));
142 $this->add('number', 'release_frequency', ts('Release Frequency'), CRM_Core_DAO
::getAttribute('CRM_Campaign_DAO_Survey', 'release_frequency'));
144 $this->addRule('release_frequency', ts('Release Frequency interval should be a positive number.'), 'positiveInteger');
146 // max reserved contacts at a time
147 $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'));
148 $this->addRule('default_number_of_contacts', ts('Maximum reserved at one time should be a positive number'), 'positiveInteger');
150 // total reserved per interviewer
151 $this->add('number', 'max_number_of_contacts', ts('Total reserved per interviewer'), CRM_Core_DAO
::getAttribute('CRM_Campaign_DAO_Survey', 'max_number_of_contacts'));
152 $this->addRule('max_number_of_contacts', ts('Total reserved contacts should be a positive number'), 'positiveInteger');
155 $this->add('checkbox', 'is_active', ts('Active?'));
158 $this->add('checkbox', 'is_default', ts('Default?'));
160 parent
::buildQuickForm();
166 public function postProcess() {
167 // store the submitted values in an array
168 $params = $this->controller
->exportValues($this->_name
);
170 $session = CRM_Core_Session
::singleton();
172 $params['last_modified_id'] = $session->get('userID');
173 $params['last_modified_date'] = date('YmdHis');
175 if ($this->_surveyId
) {
176 $params['id'] = $this->_surveyId
;
179 $params['created_id'] = $session->get('userID');
180 $params['created_date'] = date('YmdHis');
183 $params['is_active'] = CRM_Utils_Array
::value('is_active', $params, 0);
184 $params['is_default'] = CRM_Utils_Array
::value('is_default', $params, 0);
186 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params, $this->getEntityId(), $this->getDefaultEntity());
188 $survey = CRM_Campaign_BAO_Survey
::create($params);
189 $this->_surveyId
= $survey->id
;
191 if (!empty($this->_values
['result_id'])) {
192 $query = "SELECT COUNT(*) FROM civicrm_survey WHERE result_id = %1";
193 $countSurvey = (int) CRM_Core_DAO
::singleValueQuery($query,
196 $this->_values
['result_id'],
201 // delete option group if no any survey is using it.
203 CRM_Core_BAO_OptionGroup
::del($this->_values
['result_id']);
207 parent
::endPostProcess();