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