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