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