Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
3819f101 | 19 | * This class generates form components for Option Group. |
6a488035 TO |
20 | */ |
21 | class CRM_Campaign_Form_SurveyType extends CRM_Admin_Form { | |
22 | protected $_gid; | |
23 | ||
24 | /** | |
25 | * The option group name | |
26 | * | |
27 | * @var string | |
6a488035 TO |
28 | */ |
29 | protected $_gName; | |
30 | ||
6a488035 | 31 | /** |
100fef9d | 32 | * Action |
6a488035 TO |
33 | * |
34 | * @var int | |
35 | */ | |
3a936dab | 36 | public $_action; |
6a488035 TO |
37 | |
38 | /** | |
fe482240 | 39 | * Set variables up before form is built. |
beb414cc | 40 | * |
41 | * @throws \CRM_Core_Exception | |
95ea96be | 42 | */ |
28a04ea9 | 43 | public function preProcess() { |
6a488035 TO |
44 | $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this); |
45 | ||
46 | if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::DELETE)) { | |
47 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE); | |
48 | $this->assign('id', $this->_id); | |
49 | } | |
50 | $this->assign('action', $this->_action); | |
51 | $this->assign('id', $this->_id); | |
52 | ||
53 | $this->_BAOName = 'CRM_Core_BAO_OptionValue'; | |
353ffa53 TO |
54 | $this->_gName = 'activity_type'; |
55 | $this->_gid = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $this->_gName, 'id', 'name'); | |
6a488035 TO |
56 | |
57 | $session = CRM_Core_Session::singleton(); | |
58 | $url = CRM_Utils_System::url('civicrm/admin/campaign/surveyType', 'reset=1'); | |
59 | $session->pushUserContext($url); | |
60 | ||
2bf24361 | 61 | if ($this->_id && CRM_Core_OptionGroup::isDomainOptionGroup($this->_gName)) { |
6a488035 TO |
62 | $domainID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id, 'domain_id', 'id'); |
63 | if (CRM_Core_Config::domainID() != $domainID) { | |
beb414cc | 64 | CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.')); |
6a488035 TO |
65 | } |
66 | } | |
67 | } | |
68 | ||
69 | /** | |
c490a46a | 70 | * Set default values for the form. |
6a488035 TO |
71 | * the default values are retrieved from the database. |
72 | * | |
a6c01b45 CW |
73 | * @return array |
74 | * array of default values | |
6a488035 | 75 | */ |
00be9182 | 76 | public function setDefaultValues() { |
6a488035 TO |
77 | $defaults = parent::setDefaultValues(); |
78 | ||
79 | if (!isset($defaults['weight']) || !$defaults['weight']) { | |
be2fb01f | 80 | $fieldValues = ['option_group_id' => $this->_gid]; |
6a488035 TO |
81 | $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', $fieldValues); |
82 | } | |
83 | ||
84 | return $defaults; | |
85 | } | |
86 | ||
87 | /** | |
fe482240 | 88 | * Build the form object. |
6a488035 TO |
89 | */ |
90 | public function buildQuickForm() { | |
91 | parent::buildQuickForm(); | |
92 | if ($this->_action & CRM_Core_Action::DELETE) { | |
93 | return; | |
94 | } | |
95 | ||
96 | $this->applyFilter('__ALL__', 'trim'); | |
97 | $this->add('text', 'label', ts('Title'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'label'), TRUE); | |
98 | ||
5d51a2f9 | 99 | $this->add('wysiwyg', 'description', |
6a488035 TO |
100 | ts('Description'), |
101 | CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'description') | |
102 | ); | |
103 | ||
6a488035 TO |
104 | $this->add('checkbox', 'is_active', ts('Enabled?')); |
105 | ||
106 | if ($this->_action == CRM_Core_Action::UPDATE && | |
107 | CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $this->_id, 'is_reserved') | |
108 | ) { | |
be2fb01f | 109 | $this->freeze(['label', 'is_active']); |
6a488035 | 110 | } |
f20d2b0d | 111 | $this->add('number', 'weight', ts('Order'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'weight'), TRUE); |
6a488035 TO |
112 | |
113 | $this->assign('id', $this->_id); | |
114 | } | |
115 | ||
116 | /** | |
fe482240 | 117 | * Process the form submission. |
6a488035 TO |
118 | */ |
119 | public function postProcess() { | |
120 | ||
121 | if ($this->_action & CRM_Core_Action::DELETE) { | |
be2fb01f | 122 | $fieldValues = ['option_group_id' => $this->_gid]; |
6a488035 TO |
123 | $wt = CRM_Utils_Weight::delWeight('CRM_Core_DAO_OptionValue', $this->_id, $fieldValues); |
124 | ||
125 | if (CRM_Core_BAO_OptionValue::del($this->_id)) { | |
126 | CRM_Core_Session::setStatus(ts('Selected Survey type has been deleted.'), ts('Record Deleted'), 'success'); | |
127 | } | |
128 | } | |
129 | else { | |
be2fb01f | 130 | $params = $ids = []; |
6a488035 TO |
131 | $params = $this->exportValues(); |
132 | ||
133 | // set db value of filter in params if filter is non editable | |
134 | if ($this->_id && !array_key_exists('filter', $params)) { | |
135 | $params['filter'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id, 'filter', 'id'); | |
136 | } | |
137 | ||
6a488035 | 138 | $params['component_id'] = CRM_Core_Component::getComponentID('CiviCampaign'); |
ff625280 | 139 | $optionValue = CRM_Core_OptionValue::addOptionValue($params, $this->_gName, $this->_action, $this->_id); |
6a488035 | 140 | |
be2fb01f | 141 | CRM_Core_Session::setStatus(ts('The Survey type \'%1\' has been saved.', [1 => $optionValue->label]), ts('Saved'), 'success'); |
6a488035 TO |
142 | } |
143 | } | |
96025800 | 144 | |
6a488035 | 145 | } |