Merge pull request #22160 from colemanw/navigationPermission
[civicrm-core.git] / CRM / PCP / Form / PCP.php
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 * Administer Personal Campaign Pages - Search form
20 */
21 class CRM_PCP_Form_PCP extends CRM_Core_Form {
22
23 public $_context;
24
25 /**
26 * Set variables up before form is built.
27 *
28 * @throws \CRM_Core_Exception
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)) {
34 CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
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) {
44 $this->_action = $_GET['action'] ?? NULL;
45 $this->_id = $_GET['id'] ?? NULL;
46 }
47 else {
48 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
49 }
50
51 //give the context.
52 if (!isset($this->_context)) {
53 $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
54 }
55
56 $this->assign('context', $this->_context);
57
58 $session = CRM_Core_Session::singleton();
59 $context = $session->popUserContext();
60 $userID = $session->get('userID');
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);
79 CRM_Core_Session::setStatus(ts("The Campaign Page '%1' has been deleted.", [1 => $this->_title]), ts('Page Deleted'), 'success');
80 break;
81
82 case CRM_Core_Action::DISABLE:
83 case 'disable':
84 CRM_PCP_BAO_PCP::setDisable($this->_id, '0');
85 CRM_Core_Session::setStatus(ts("The Campaign Page '%1' has been disabled.", [1 => $this->_title]), ts('Page Disabled'), 'success');
86 break;
87
88 case CRM_Core_Action::ENABLE:
89 case 'enable':
90 CRM_PCP_BAO_PCP::setDisable($this->_id, '1');
91 CRM_Core_Session::setStatus(ts("The Campaign Page '%1' has been enabled.", [1 => $this->_title]), ts('Page Enabled'), 'success');
92 break;
93 }
94
95 if ($context) {
96 CRM_Utils_System::redirect($context);
97 }
98 }
99 }
100
101 /**
102 * Set default values for the form. Note that in edit/view mode
103 * the default values are retrieved from the database
104 *
105 * @return array
106 * array of default values
107 */
108 public function setDefaultValues() {
109 $defaults = [];
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 /**
118 * Build the form object.
119 */
120 public function buildQuickForm() {
121 if ($this->_action & CRM_Core_Action::DELETE) {
122 $this->addButtons([
123 [
124 'type' => 'next',
125 'name' => ts('Delete Campaign'),
126 'isDefault' => TRUE,
127 ],
128 [
129 'type' => 'cancel',
130 'name' => ts('Cancel'),
131 ],
132 ]);
133 }
134 else {
135
136 $status = ['' => ts('- select -')] + CRM_Core_OptionGroup::values("pcp_status");
137 $types = [
138 '' => ts('- select -'),
139 'contribute' => ts('Contribution'),
140 'event' => ts('Event'),
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 )");
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);
149 $this->addButtons([
150 [
151 'type' => 'refresh',
152 'name' => ts('Search'),
153 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
154 'isDefault' => TRUE,
155 ],
156 ]);
157 parent::buildQuickForm();
158 }
159 }
160
161 /**
162 * Global validation rules for the form.
163 *
164 * @param array $fields
165 * Posted values of the form.
166 * @param array $files
167 * @param CRM_Core_Form $form
168 *
169 * @return array|NULL
170 * list of errors to be posted back to the form
171 */
172 public static function formRule($fields, $files, $form) {
173 return NULL;
174 }
175
176 /**
177 * Process the form.
178 */
179 public function postProcess() {
180 if ($this->_action & CRM_Core_Action::DELETE) {
181 CRM_PCP_BAO_PCP::deleteById($this->_id);
182 CRM_Core_Session::setStatus(ts("The Campaign Page '%1' has been deleted.", [1 => $this->_title]), ts('Page Deleted'), 'success');
183 }
184 else {
185 $params = $this->controller->exportValues($this->_name);
186 $parent = $this->controller->getParent();
187
188 if (!empty($params)) {
189 $fields = ['status_id', 'page_id'];
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 }
203
204 }