Merge pull request #4865 from eileenmcnaughton/my-first-factory
[civicrm-core.git] / CRM / PCP / Form / PCP.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * Administer Personal Campaign Pages - Search form
38 */
39 class CRM_PCP_Form_PCP extends CRM_Core_Form {
40
41 public $_context;
42
43 /**
44 * Set variables up before form is built
45 *
46 * @param null
47 *
48 * @return void
49 */
50 public function preProcess() {
51 if ($this->_action & CRM_Core_Action::DELETE) {
52 //check permission for action.
53 if (!CRM_Core_Permission::checkActionPermission('CiviEvent', $this->_action)) {
54 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
55 }
56
57 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
58 $this->_title = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $this->_id, 'title');
59 $this->assign('title', $this->_title);
60 parent::preProcess();
61 }
62
63 if (!$this->_action) {
64 $this->_action = CRM_Utils_Array::value('action', $_GET);
65 $this->_id = CRM_Utils_Array::value('id', $_GET);
66 }
67 else {
68 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
69 }
70
71 //give the context.
72 if (!isset($this->_context)) {
73 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this);
74 }
75
76 $this->assign('context', $this->_context);
77
78 $session = CRM_Core_Session::singleton();
79 $context = $session->popUserContext();
80 $userID = $session->get('userID');
81
82 //do not allow destructive actions without permissions
83 $permission = FALSE;
84 if (CRM_Core_Permission::check('administer CiviCRM') ||
85 ($userID && (CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP',
86 $this->_id,
87 'contact_id'
88 ) == $userID))
89 ) {
90 $permission = TRUE;
91 }
92 if ($permission && $this->_id) {
93
94 $this->_title = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $this->_id, 'title');
95 switch ($this->_action) {
96 case CRM_Core_Action::DELETE:
97 case 'delete':
98 CRM_PCP_BAO_PCP::deleteById($this->_id);
99 CRM_Core_Session::setStatus(ts("The Campaign Page '%1' has been deleted.", array(1 => $this->_title)), ts('Page Deleted'), 'success');
100 break;
101
102 case CRM_Core_Action::DISABLE:
103 case 'disable':
104 CRM_PCP_BAO_PCP::setDisable($this->_id, '0');
105 CRM_Core_Session::setStatus(ts("The Campaign Page '%1' has been disabled.", array(1 => $this->_title)), ts('Page Disabled'), 'success');
106 break;
107
108 case CRM_Core_Action::ENABLE:
109 case 'enable':
110 CRM_PCP_BAO_PCP::setDisable($this->_id, '1');
111 CRM_Core_Session::setStatus(ts("The Campaign Page '%1' has been enabled.", array(1 => $this->_title)), ts('Page Enabled'), 'success');
112 break;
113 }
114
115 if ($context) {
116 CRM_Utils_System::redirect($context);
117 }
118 }
119 }
120
121 /**
122 * Set default values for the form. Note that in edit/view mode
123 * the default values are retrieved from the database
124 *
125 * @param null
126 *
127 * @return array array of default values
128 */
129 public function setDefaultValues() {
130 $defaults = array();
131
132 $pageType = CRM_Utils_Request::retrieve('page_type', 'String', $this);
133 $defaults['page_type'] = !empty($pageType) ? $pageType : '';
134
135 return $defaults;
136 }
137
138 /**
139 * Build the form object
140 *
141 * @param null
142 *
143 * @return void
144 */
145 public function buildQuickForm() {
146 if ($this->_action & CRM_Core_Action::DELETE) {
147 $this->addButtons(array(
148 array(
149 'type' => 'next',
150 'name' => ts('Delete Campaign'),
151 'isDefault' => TRUE,
152 ),
153 array(
154 'type' => 'cancel',
155 'name' => ts('Cancel'),
156 ),
157 )
158 );
159 }
160 else {
161
162 $status = array('' => ts('- select -')) + CRM_Core_OptionGroup::values("pcp_status");
163 $types = array(
164 '' => ts('- select -'),
165 'contribute' => ts('Contribution'),
166 'event' => ts('Event'),
167 );
168 $contribPages = array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::contributionPage();
169 $eventPages = array('' => ts('- select -')) + CRM_Event_PseudoConstant::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )");
170
171 $this->addElement('select', 'status_id', ts('Status'), $status);
172 $this->addElement('select', 'page_type', ts('Source Type'), $types);
173 $this->addElement('select', 'page_id', ts('Contribution Page'), $contribPages);
174 $this->addElement('select', 'event_id', ts('Event Page'), $eventPages);
175 $this->addButtons(array(
176 array(
177 'type' => 'refresh',
178 'name' => ts('Search'),
179 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
180 'isDefault' => TRUE,
181 ),
182 )
183 );
184 parent::buildQuickForm();
185 }
186 }
187
188 /**
189 * Global validation rules for the form
190 *
191 * @param array $fields
192 * Posted values of the form.
193 *
194 * @param $files
195 * @param CRM_Core_Form $form
196 *
197 * @return array list of errors to be posted back to the form
198 * @static
199 */
200 public static function formRule($fields, $files, $form) {
201 }
202
203 /**
204 * Process the form
205 *
206 * @param null
207 *
208 * @return void
209 */
210 public function postProcess() {
211 if ($this->_action & CRM_Core_Action::DELETE) {
212 CRM_PCP_BAO_PCP::deleteById($this->_id);
213 CRM_Core_Session::setStatus(ts("The Campaign Page '%1' has been deleted.", array(1 => $this->_title)), ts('Page Deleted'), 'success');
214 }
215 else {
216 $params = $this->controller->exportValues($this->_name);
217 $parent = $this->controller->getParent();
218
219 if (!empty($params)) {
220 $fields = array('status_id', 'page_id');
221 foreach ($fields as $field) {
222 if (isset($params[$field]) &&
223 !CRM_Utils_System::isNull($params[$field])
224 ) {
225 $parent->set($field, $params[$field]);
226 }
227 else {
228 $parent->set($field, NULL);
229 }
230 }
231 }
232 }
233 }
234 }