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