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 | /** | |
19 | * Administer Personal Campaign Pages - Search form | |
20 | */ | |
21 | class CRM_PCP_Form_PCP extends CRM_Core_Form { | |
22 | ||
23 | public $_context; | |
24 | ||
25 | /** | |
fe482240 | 26 | * Set variables up before form is built. |
e22ec653 | 27 | * |
28 | * @throws \CRM_Core_Exception | |
6a488035 TO |
29 | */ |
30 | public function preProcess() { | |
31 | if ($this->_action & CRM_Core_Action::DELETE) { | |
32 | //check permission for action. | |
33 | if (!CRM_Core_Permission::checkActionPermission('CiviEvent', $this->_action)) { | |
e22ec653 | 34 | CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.')); |
6a488035 TO |
35 | } |
36 | ||
37 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); | |
38 | $this->_title = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $this->_id, 'title'); | |
39 | $this->assign('title', $this->_title); | |
40 | parent::preProcess(); | |
41 | } | |
42 | ||
43 | if (!$this->_action) { | |
9c1bc317 CW |
44 | $this->_action = $_GET['action'] ?? NULL; |
45 | $this->_id = $_GET['id'] ?? NULL; | |
6a488035 TO |
46 | } |
47 | else { | |
48 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); | |
49 | } | |
50 | ||
51 | //give the context. | |
52 | if (!isset($this->_context)) { | |
edc80cda | 53 | $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this); |
6a488035 TO |
54 | } |
55 | ||
56 | $this->assign('context', $this->_context); | |
57 | ||
6a488035 TO |
58 | $session = CRM_Core_Session::singleton(); |
59 | $context = $session->popUserContext(); | |
353ffa53 | 60 | $userID = $session->get('userID'); |
6a488035 TO |
61 | |
62 | //do not allow destructive actions without permissions | |
63 | $permission = FALSE; | |
64 | if (CRM_Core_Permission::check('administer CiviCRM') || | |
65 | ($userID && (CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', | |
66 | $this->_id, | |
67 | 'contact_id' | |
68 | ) == $userID)) | |
69 | ) { | |
70 | $permission = TRUE; | |
71 | } | |
72 | if ($permission && $this->_id) { | |
73 | ||
74 | $this->_title = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $this->_id, 'title'); | |
75 | switch ($this->_action) { | |
76 | case CRM_Core_Action::DELETE: | |
77 | case 'delete': | |
78 | CRM_PCP_BAO_PCP::deleteById($this->_id); | |
be2fb01f | 79 | CRM_Core_Session::setStatus(ts("The Campaign Page '%1' has been deleted.", [1 => $this->_title]), ts('Page Deleted'), 'success'); |
6a488035 TO |
80 | break; |
81 | ||
82 | case CRM_Core_Action::DISABLE: | |
83 | case 'disable': | |
84 | CRM_PCP_BAO_PCP::setDisable($this->_id, '0'); | |
be2fb01f | 85 | CRM_Core_Session::setStatus(ts("The Campaign Page '%1' has been disabled.", [1 => $this->_title]), ts('Page Disabled'), 'success'); |
6a488035 TO |
86 | break; |
87 | ||
88 | case CRM_Core_Action::ENABLE: | |
89 | case 'enable': | |
90 | CRM_PCP_BAO_PCP::setDisable($this->_id, '1'); | |
be2fb01f | 91 | CRM_Core_Session::setStatus(ts("The Campaign Page '%1' has been enabled.", [1 => $this->_title]), ts('Page Enabled'), 'success'); |
6a488035 TO |
92 | break; |
93 | } | |
94 | ||
95 | if ($context) { | |
96 | CRM_Utils_System::redirect($context); | |
97 | } | |
98 | } | |
99 | } | |
100 | ||
101 | /** | |
c490a46a | 102 | * Set default values for the form. Note that in edit/view mode |
6a488035 TO |
103 | * the default values are retrieved from the database |
104 | * | |
a6c01b45 CW |
105 | * @return array |
106 | * array of default values | |
6a488035 | 107 | */ |
00be9182 | 108 | public function setDefaultValues() { |
be2fb01f | 109 | $defaults = []; |
6a488035 TO |
110 | |
111 | $pageType = CRM_Utils_Request::retrieve('page_type', 'String', $this); | |
112 | $defaults['page_type'] = !empty($pageType) ? $pageType : ''; | |
113 | ||
114 | return $defaults; | |
115 | } | |
116 | ||
117 | /** | |
fe482240 | 118 | * Build the form object. |
6a488035 TO |
119 | */ |
120 | public function buildQuickForm() { | |
121 | if ($this->_action & CRM_Core_Action::DELETE) { | |
be2fb01f CW |
122 | $this->addButtons([ |
123 | [ | |
6a488035 TO |
124 | 'type' => 'next', |
125 | 'name' => ts('Delete Campaign'), | |
126 | 'isDefault' => TRUE, | |
be2fb01f CW |
127 | ], |
128 | [ | |
6a488035 TO |
129 | 'type' => 'cancel', |
130 | 'name' => ts('Cancel'), | |
be2fb01f | 131 | ], |
c86d4e7c | 132 | ]); |
6a488035 TO |
133 | } |
134 | else { | |
135 | ||
be2fb01f CW |
136 | $status = ['' => ts('- select -')] + CRM_Core_OptionGroup::values("pcp_status"); |
137 | $types = [ | |
2158332a CW |
138 | '' => ts('- select -'), |
139 | 'contribute' => ts('Contribution'), | |
140 | 'event' => ts('Event'), | |
be2fb01f CW |
141 | ]; |
142 | $contribPages = ['' => ts('- select -')] + CRM_Contribute_PseudoConstant::contributionPage(); | |
143 | $eventPages = ['' => ts('- select -')] + CRM_Event_PseudoConstant::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )"); | |
6a488035 TO |
144 | |
145 | $this->addElement('select', 'status_id', ts('Status'), $status); | |
146 | $this->addElement('select', 'page_type', ts('Source Type'), $types); | |
147 | $this->addElement('select', 'page_id', ts('Contribution Page'), $contribPages); | |
148 | $this->addElement('select', 'event_id', ts('Event Page'), $eventPages); | |
be2fb01f CW |
149 | $this->addButtons([ |
150 | [ | |
6a488035 TO |
151 | 'type' => 'refresh', |
152 | 'name' => ts('Search'), | |
153 | 'spacing' => ' ', | |
154 | 'isDefault' => TRUE, | |
be2fb01f | 155 | ], |
c86d4e7c | 156 | ]); |
6a488035 TO |
157 | parent::buildQuickForm(); |
158 | } | |
159 | } | |
160 | ||
161 | /** | |
fe482240 | 162 | * Global validation rules for the form. |
6a488035 | 163 | * |
db95eff6 TO |
164 | * @param array $fields |
165 | * Posted values of the form. | |
ae5ffbb7 | 166 | * @param array $files |
c490a46a | 167 | * @param CRM_Core_Form $form |
da6b46f4 | 168 | * |
ae5ffbb7 | 169 | * @return array|NULL |
a6c01b45 | 170 | * list of errors to be posted back to the form |
6a488035 | 171 | */ |
e049d911 | 172 | public static function formRule($fields, $files, $form) { |
ae5ffbb7 | 173 | return NULL; |
e049d911 | 174 | } |
6a488035 TO |
175 | |
176 | /** | |
fe482240 | 177 | * Process the form. |
6a488035 TO |
178 | */ |
179 | public function postProcess() { | |
180 | if ($this->_action & CRM_Core_Action::DELETE) { | |
181 | CRM_PCP_BAO_PCP::deleteById($this->_id); | |
be2fb01f | 182 | CRM_Core_Session::setStatus(ts("The Campaign Page '%1' has been deleted.", [1 => $this->_title]), ts('Page Deleted'), 'success'); |
6a488035 TO |
183 | } |
184 | else { | |
185 | $params = $this->controller->exportValues($this->_name); | |
186 | $parent = $this->controller->getParent(); | |
187 | ||
188 | if (!empty($params)) { | |
be2fb01f | 189 | $fields = ['status_id', 'page_id']; |
6a488035 TO |
190 | foreach ($fields as $field) { |
191 | if (isset($params[$field]) && | |
192 | !CRM_Utils_System::isNull($params[$field]) | |
193 | ) { | |
194 | $parent->set($field, $params[$field]); | |
195 | } | |
196 | else { | |
197 | $parent->set($field, NULL); | |
198 | } | |
199 | } | |
200 | } | |
201 | } | |
202 | } | |
96025800 | 203 | |
6a488035 | 204 | } |