Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-07-14-22-39-05
[civicrm-core.git] / CRM / PCP / Form / PCP.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 * Function to set variables up before form is built
45 *
46 * @param null
47 *
48 * @return void
49 * @access public
50 */
51 public function preProcess() {
52 if ($this->_action & CRM_Core_Action::DELETE) {
53 //check permission for action.
54 if (!CRM_Core_Permission::checkActionPermission('CiviEvent', $this->_action)) {
55 CRM_Core_Error::fatal(ts('You do not have permission to access this page'));
56 }
57
58 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
59 $this->_title = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $this->_id, 'title');
60 $this->assign('title', $this->_title);
61 parent::preProcess();
62 }
63
64 if (!$this->_action) {
65 $this->_action = CRM_Utils_Array::value('action', $_GET);
66 $this->_id = CRM_Utils_Array::value('id', $_GET);
67 }
68 else {
69 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
70 }
71
72 //give the context.
73 if (!isset($this->_context)) {
74 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this);
75 }
76
77 $this->assign('context', $this->_context);
78
79
80 $session = CRM_Core_Session::singleton();
81 $context = $session->popUserContext();
82 $userID = $session->get('userID');
83
84 //do not allow destructive actions without permissions
85 $permission = FALSE;
86 if (CRM_Core_Permission::check('administer CiviCRM') ||
87 ($userID && (CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP',
88 $this->_id,
89 'contact_id'
90 ) == $userID))
91 ) {
92 $permission = TRUE;
93 }
94 if ($permission && $this->_id) {
95
96 $this->_title = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $this->_id, 'title');
97 switch ($this->_action) {
98 case CRM_Core_Action::DELETE:
99 case 'delete':
100 CRM_PCP_BAO_PCP::deleteById($this->_id);
101 CRM_Core_Session::setStatus(ts("The Campaign Page '%1' has been deleted.", array(1 => $this->_title)), ts('Page Deleted'), 'success');
102 break;
103
104 case CRM_Core_Action::DISABLE:
105 case 'disable':
106 CRM_PCP_BAO_PCP::setDisable($this->_id, '0');
107 CRM_Core_Session::setStatus(ts("The Campaign Page '%1' has been disabled.", array(1 => $this->_title)), ts('Page Disabled'), 'success');
108 break;
109
110 case CRM_Core_Action::ENABLE:
111 case 'enable':
112 CRM_PCP_BAO_PCP::setDisable($this->_id, '1');
113 CRM_Core_Session::setStatus(ts("The Campaign Page '%1' has been enabled.", array(1 => $this->_title)), ts('Page Enabled'), 'success');
114 break;
115 }
116
117 if ($context) {
118 CRM_Utils_System::redirect($context);
119 }
120 }
121 }
122
123 /**
124 * This function sets the default values for the form. Note that in edit/view mode
125 * the default values are retrieved from the database
126 *
127 * @param null
128 *
129 * @return array array of default values
130 * @access public
131 */
132 function setDefaultValues() {
133 $defaults = array();
134
135 $pageType = CRM_Utils_Request::retrieve('page_type', 'String', $this);
136 $defaults['page_type'] = !empty($pageType) ? $pageType : '';
137
138 return $defaults;
139 }
140
141 /**
142 * Function to actually build the form
143 *
144 * @param null
145 *
146 * @return void
147 * @access public
148 */
149 public function buildQuickForm() {
150 if ($this->_action & CRM_Core_Action::DELETE) {
151 $this->addButtons(array(
152 array(
153 'type' => 'next',
154 'name' => ts('Delete Campaign'),
155 'isDefault' => TRUE,
156 ),
157 array(
158 'type' => 'cancel',
159 'name' => ts('Cancel'),
160 ),
161 )
162 );
163 }
164 else {
165
166 $status = array('' => ts('- select -')) + CRM_Core_OptionGroup::values("pcp_status");
167 $types = array(
168 '' => ts('- select -'),
169 'contribute' => ts('Contribution'),
170 'event' => ts('Event'),
171 );
172 $contribPages = array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::contributionPage();
173 $eventPages = array('' => ts('- select -')) + CRM_Event_PseudoConstant::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )");
174
175 $this->addElement('select', 'status_id', ts('Status'), $status);
176 $this->addElement('select', 'page_type', ts('Source Type'), $types);
177 $this->addElement('select', 'page_id', ts('Contribution Page'), $contribPages);
178 $this->addElement('select', 'event_id', ts('Event Page'), $eventPages);
179 $this->addButtons(array(
180 array(
181 'type' => 'refresh',
182 'name' => ts('Search'),
183 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
184 'isDefault' => TRUE,
185 ),
186 )
187 );
188 parent::buildQuickForm();
189 }
190 }
191
192 /**
193 * global validation rules for the form
194 *
195 * @param array $fields posted values of the form
196 *
197 * @return array list of errors to be posted back to the form
198 * @static
199 * @access public
200 */
201 static function formRule($fields, $files, $form) {}
202
203 /**
204 * Process the form
205 *
206 * @param null
207 *
208 * @return void
209 * @access public
210 */
211 public function postProcess() {
212 if ($this->_action & CRM_Core_Action::DELETE) {
213 CRM_PCP_BAO_PCP::deleteById($this->_id);
214 CRM_Core_Session::setStatus(ts("The Campaign Page '%1' has been deleted.", array(1 => $this->_title)), ts('Page Deleted'), 'success');
215 }
216 else {
217 $params = $this->controller->exportValues($this->_name);
218 $parent = $this->controller->getParent();
219
220 if (!empty($params)) {
221 $fields = array('status_id', 'page_id');
222 foreach ($fields as $field) {
223 if (isset($params[$field]) &&
224 !CRM_Utils_System::isNull($params[$field])
225 ) {
226 $parent->set($field, $params[$field]);
227 }
228 else {
229 $parent->set($field, NULL);
230 }
231 }
232 }
233 }
234 }
235 }
236