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