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