Style - Remove @public, @private, @protected
[civicrm-core.git] / CRM / PCP / Form / Event.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 * This class generates form components for PCP
38 *
39 */
40 class CRM_PCP_Form_Event extends CRM_Event_Form_ManageEvent {
41
42 /**
43 * The type of pcp component.
44 *
45 * @var int
46 */
47 public $_component = 'event';
48
49
50 public function preProcess() {
51 parent::preProcess();
52 }
53
54 /**
55 * Set default values for the form.
56 *
57 * @access public
58 *
59 * @return void
60 */
61 public function setDefaultValues() {
62 $defaults = array();
63
64 $defaults = array();
65 if (isset($this->_id)) {
66 $title = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_id, 'title');
67 CRM_Utils_System::setTitle(ts('Personal Campaign Page Settings (%1)', array(1 => $title)));
68
69 $params = array('entity_id' => $this->_id, 'entity_table' => 'civicrm_event');
70 CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCPBlock', $params, $defaults);
71 $defaults['pcp_active'] = CRM_Utils_Array::value('is_active', $defaults);
72 // Assign contribution page ID to pageId for referencing in PCP.hlp - since $id is overwritten there. dgg
73 $this->assign('pageId', $this->_id);
74 }
75
76 if (empty($defaults['id'])) {
77 $defaults['target_entity_type'] = 'event';
78 $defaults['is_approval_needed'] = 1;
79 $defaults['is_tellfriend_enabled'] = 1;
80 $defaults['tellfriend_limit'] = 5;
81 $defaults['link_text'] = ts('Promote this event with a personal campaign page');
82
83 if ($this->_id &&
84 $ccReceipt = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id, 'cc_receipt')) {
85 $defaults['notify_email'] = $ccReceipt;
86 }
87 }
88 return $defaults;
89 }
90
91 /**
92 * Build the form object
93 *
94 * @return void
95 * @access public
96 */
97 public function buildQuickForm() {
98 CRM_PCP_BAO_PCP::buildPCPForm($this);
99
100 $this->addElement('checkbox', 'pcp_active', ts('Enable Personal Campaign Pages? (for this event)'), NULL, array('onclick' => "return showHideByValue('pcp_active',true,'pcpFields','table-row','radio',false);"));
101
102 $this->add('select', 'target_entity_type', ts('Campaign Type'),
103 array('' => ts('- select -'), 'event' => ts('Event'), 'contribute' => ts('Contribution')),
104 NULL, array('onchange' => "return showHideByValue('target_entity_type','contribute','pcpDetailFields','block','select',false);")
105 );
106
107 $this->add('select', 'target_entity_id',
108 ts('Online Contribution Page'),
109 array(
110 '' => ts('- select -')) +
111 CRM_Contribute_PseudoConstant::contributionPage()
112 );
113
114 parent::buildQuickForm();
115
116 // If at least one PCP has been created, don't allow changing the target
117 $pcpBlock = new CRM_PCP_DAO_PCPBlock();
118 $pcpBlock->entity_table = 'civicrm_event';
119 $pcpBlock->entity_id = $this->_id;
120 $pcpBlock->find(TRUE);
121
122 if (!empty($pcpBlock->id) && CRM_PCP_BAO_PCP::getPcpBlockInUse($pcpBlock->id)) {
123 foreach (array(
124 'target_entity_type', 'target_entity_id') as $element_name) {
125 $element = $this->getElement($element_name);
126 $element->freeze();
127 }
128 }
129 $this->addFormRule(array('CRM_PCP_Form_Event', 'formRule'), $this);
130 }
131
132 /**
133 * Validation
134 *
135 * @param array $params (ref.) an assoc array of name/value pairs
136 *
137 * @param $files
138 * @param $self
139 *
140 * @return mixed true or array of errors
141 * @access public
142 * @static
143 */
144 public static function formRule($params, $files, $self) {
145 $errors = array();
146 if (!empty($params['is_active'])) {
147
148 if (!empty($params['is_tellfriend_enabled']) &&
149 (CRM_Utils_Array::value('tellfriend_limit', $params) <= 0)
150 ) {
151 $errors['tellfriend_limit'] = ts('if Tell Friend is enable, Maximum recipients limit should be greater than zero.');
152 }
153 if (empty($params['supporter_profile_id'])) {
154 $errors['supporter_profile_id'] = ts('Supporter profile is a required field.');
155 }
156 else {
157 if (CRM_PCP_BAO_PCP::checkEmailProfile($params['supporter_profile_id'])) {
158 $errors['supporter_profile_id'] = ts('Profile is not configured with Email address.');
159 }
160 }
161
162 if ($emails = CRM_Utils_Array::value('notify_email', $params)) {
163 $emailArray = explode(',', $emails);
164 foreach ($emailArray as $email) {
165 if ($email && !CRM_Utils_Rule::email(trim($email))) {
166 $errors['notify_email'] = ts('A valid Notify Email address must be specified');
167 }
168 }
169 }
170 }
171 return empty($errors) ? TRUE : $errors;
172 }
173
174 /**
175 * Process the form submission
176 *
177 * @access public
178 *
179 * @return void
180 */
181 public function postProcess() {
182 // get the submitted form values.
183 $params = $this->controller->exportValues($this->_name);
184
185 // Source
186 $params['entity_table'] = 'civicrm_event';
187 $params['entity_id'] = $this->_id;
188
189 // Target
190 $params['target_entity_type'] = CRM_Utils_Array::value('target_entity_type', $params, 'event');
191 if ($params['target_entity_type'] == 'event') {
192 $params['target_entity_id'] = $this->_id;
193 }
194 else {
195 $params['target_entity_id'] = CRM_Utils_Array::value('target_entity_id', $params, $this->_id);
196 }
197
198 $dao = new CRM_PCP_DAO_PCPBlock();
199 $dao->entity_table = $params['entity_table'];
200 $dao->entity_id = $this->_id;
201 $dao->find(TRUE);
202 $params['id'] = $dao->id;
203 $params['is_active'] = CRM_Utils_Array::value('pcp_active', $params, FALSE);
204 $params['is_approval_needed'] = CRM_Utils_Array::value('is_approval_needed', $params, FALSE);
205 $params['is_tellfriend_enabled'] = CRM_Utils_Array::value('is_tellfriend_enabled', $params, FALSE);
206
207 $dao = CRM_PCP_BAO_PCP::add($params);
208
209 // Update tab "disabled" css class
210 $this->ajaxResponse['tabValid'] = !empty($params['is_active']);
211
212 parent::endPostProcess();
213 }
214
215 /**
216 * Return a descriptive name for the page, used in wizard header
217 *
218 * @return string
219 * @access public
220 */
221 public function getTitle() {
222 return ts('Enable Personal Campaign Pages');
223 }
224 }
225