commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / CRM / PCP / Form / Campaign.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components for processing a pcp page creati
38 *
39 */
40 class CRM_PCP_Form_Campaign extends CRM_Core_Form {
41 public $_context;
42 public $_component;
43
44 public function preProcess() {
45 // we do not want to display recently viewed items, so turn off
46 $this->assign('displayRecent', FALSE);
47
48 // component null in controller object - fix? dgg
49 // $this->_component = $this->controller->get('component');
50 $this->_component = CRM_Utils_Request::retrieve('component', 'String', $this);
51 $this->assign('component', $this->_component);
52
53 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this);
54 $this->assign('context', $this->_context);
55
56 $this->_pageId = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE);
57 $title = ts('Setup a Personal Campaign Page - Step 2');
58
59 if ($this->_pageId) {
60 $title = ts('Edit Your Personal Campaign Page');
61 }
62
63 CRM_Utils_System::setTitle($title);
64 parent::preProcess();
65 }
66
67 public function setDefaultValues() {
68 $dafaults = array();
69 $dao = new CRM_PCP_DAO_PCP();
70
71 if ($this->_pageId) {
72 $dao->id = $this->_pageId;
73 if ($dao->find(TRUE)) {
74 CRM_Core_DAO::storeValues($dao, $defaults);
75 }
76 // fix the display of the monetary value, CRM-4038
77 if (isset($defaults['goal_amount'])) {
78 $defaults['goal_amount'] = CRM_Utils_Money::format($defaults['goal_amount'], NULL, '%a');
79 }
80
81 $defaults['pcp_title'] = CRM_Utils_Array::value('title', $defaults);
82 $defaults['pcp_intro_text'] = CRM_Utils_Array::value('intro_text', $defaults);
83 }
84
85 if ($this->get('action') & CRM_Core_Action::ADD) {
86 $defaults['is_active'] = 1;
87 $defaults['is_honor_roll'] = 1;
88 $defaults['is_thermometer'] = 1;
89 $defaults['is_notify'] = 1;
90 }
91
92 $this->_contactID = CRM_Utils_Array::value('contact_id', $defaults);
93 $this->_contriPageId = CRM_Utils_Array::value('page_id', $defaults);
94
95 return $defaults;
96 }
97
98 /**
99 * Build the form object.
100 *
101 * @return void
102 */
103 public function buildQuickForm() {
104 $this->add('text', 'pcp_title', ts('Title'), NULL, TRUE);
105 $this->add('textarea', 'pcp_intro_text', ts('Welcome'), NULL, TRUE);
106 $this->add('text', 'goal_amount', ts('Your Goal'), NULL, TRUE);
107 $this->addRule('goal_amount', ts('Goal Amount should be a numeric value'), 'money');
108
109 $attributes = array();
110 if ($this->_component == 'event') {
111 if ($this->get('action') & CRM_Core_Action::ADD) {
112 $attributes = array('value' => ts('Join Us'), 'onClick' => 'select();');
113 }
114 $this->add('text', 'donate_link_text', ts('Sign up Button'), $attributes);
115 }
116 else {
117 if ($this->get('action') & CRM_Core_Action::ADD) {
118 $attributes = array('value' => ts('Donate Now'), 'onClick' => 'select();');
119 }
120 $this->add('text', 'donate_link_text', ts('Donation Button'), $attributes);
121 }
122
123 $attrib = array('rows' => 8, 'cols' => 60);
124 $this->add('textarea', 'page_text', ts('Your Message'), NULL, FALSE);
125
126 $maxAttachments = 1;
127 CRM_Core_BAO_File::buildAttachment($this, 'civicrm_pcp', $this->_pageId, $maxAttachments);
128
129 $this->addElement('checkbox', 'is_thermometer', ts('Progress Bar'));
130 $this->addElement('checkbox', 'is_honor_roll', ts('Honor Roll'), NULL);
131 if ($this->_pageId) {
132 $params = array('id' => $this->_pageId);
133 CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCP', $params, $pcpInfo);
134 $owner_notification_option = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCPBlock', $pcpInfo['pcp_block_id'], 'owner_notify_id');
135 }
136 else {
137 $owner_notification_option = CRM_PCP_BAO_PCP::getOwnerNotificationId($this->controller->get('component_page_id'), $this->_component ? $this->_component : 'contribute');
138 }
139 if ($owner_notification_option == CRM_Core_OptionGroup::getValue('pcp_owner_notify', 'owner_chooses', 'name')) {
140 $this->assign('owner_notification_option', TRUE);
141 $this->addElement('checkbox', 'is_notify', ts('Notify me via email when someone donates to my page'), NULL);
142 }
143
144 $this->addElement('checkbox', 'is_active', ts('Active'));
145 if ($this->_context == 'dashboard') {
146 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/admin/pcp', 'reset=1'));
147 }
148
149 $this->addButtons(
150 array(
151 array(
152 'type' => 'upload',
153 'name' => ts('Save'),
154 'isDefault' => TRUE,
155 ),
156 array(
157 'type' => 'cancel',
158 'name' => ts('Cancel'),
159 ),
160 )
161 );
162 $this->addFormRule(array('CRM_PCP_Form_Campaign', 'formRule'), $this);
163 }
164
165 /**
166 * Global form rule.
167 *
168 * @param array $fields
169 * The input form values.
170 * @param array $files
171 * The uploaded files if any.
172 * @param $self
173 *
174 *
175 * @return bool|array
176 * true if no errors, else array of errors
177 */
178 public static function formRule($fields, $files, $self) {
179 $errors = array();
180 if ($fields['goal_amount'] <= 0) {
181 $errors['goal_amount'] = ts('Goal Amount should be a numeric value greater than zero.');
182 }
183 if (strlen($fields['donate_link_text']) >= 64) {
184 $errors['donate_link_text'] = ts('Button Text must be less than 64 characters.');
185 }
186 return $errors;
187 }
188
189 /**
190 * Process the form submission.
191 *
192 *
193 * @return void
194 */
195 public function postProcess() {
196 $params = $this->controller->exportValues($this->_name);
197 $checkBoxes = array('is_thermometer', 'is_honor_roll', 'is_active', 'is_notify');
198
199 foreach ($checkBoxes as $key) {
200 if (!isset($params[$key])) {
201 $params[$key] = 0;
202 }
203 }
204 $session = CRM_Core_Session::singleton();
205 $contactID = isset($this->_contactID) ? $this->_contactID : $session->get('userID');
206 if (!$contactID) {
207 $contactID = $this->get('contactID');
208 }
209 $params['title'] = $params['pcp_title'];
210 $params['intro_text'] = $params['pcp_intro_text'];
211 $params['contact_id'] = $contactID;
212 $params['page_id'] = $this->get('component_page_id') ? $this->get('component_page_id') : $this->_contriPageId;
213 $params['page_type'] = $this->_component;
214
215 // since we are allowing html input from the user
216 // we also need to purify it, so lets clean it up
217 $htmlFields = array('intro_text', 'page_text', 'title');
218 foreach ($htmlFields as $field) {
219 if (!empty($params[$field])) {
220 $params[$field] = CRM_Utils_String::purifyHTML($params[$field]);
221 }
222 }
223
224 $entity_table = CRM_PCP_BAO_PCP::getPcpEntityTable($params['page_type']);
225
226 $pcpBlock = new CRM_PCP_DAO_PCPBlock();
227 $pcpBlock->entity_table = $entity_table;
228 $pcpBlock->entity_id = $params['page_id'];
229 $pcpBlock->find(TRUE);
230
231 $params['pcp_block_id'] = $pcpBlock->id;
232
233 $params['goal_amount'] = CRM_Utils_Rule::cleanMoney($params['goal_amount']);
234
235 $approval_needed = $pcpBlock->is_approval_needed;
236 $approvalMessage = NULL;
237
238 if ($this->get('action') & CRM_Core_Action::ADD) {
239 $params['status_id'] = $approval_needed ? 1 : 2;
240 $approvalMessage = $approval_needed ? ts('but requires administrator review before you can begin promoting your campaign. You will receive an email confirmation shortly which includes a link to return to this page.') : ts('and is ready to use.');
241 }
242
243 $params['id'] = $this->_pageId;
244
245 $pcp = CRM_PCP_BAO_PCP::add($params, FALSE);
246
247 // add attachments as needed
248 CRM_Core_BAO_File::formatAttachment($params,
249 $params,
250 'civicrm_pcp',
251 $pcp->id
252 );
253
254 $pageStatus = isset($this->_pageId) ? ts('updated') : ts('created');
255 $statusId = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $pcp->id, 'status_id');
256
257 //send notification of PCP create/update.
258 $pcpParams = array('entity_table' => $entity_table, 'entity_id' => $pcp->page_id);
259 $notifyParams = array();
260 $notifyStatus = "";
261 CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCPBlock', $pcpParams, $notifyParams, array('notify_email'));
262
263 if ($emails = $pcpBlock->notify_email) {
264 $this->assign('pcpTitle', $pcp->title);
265
266 if ($this->_pageId) {
267 $this->assign('mode', 'Update');
268 }
269 else {
270 $this->assign('mode', 'Add');
271 }
272 $pcpStatus = CRM_Core_OptionGroup::getLabel('pcp_status', $statusId);
273 $this->assign('pcpStatus', $pcpStatus);
274
275 $this->assign('pcpId', $pcp->id);
276
277 $supporterUrl = CRM_Utils_System::url('civicrm/contact/view',
278 "reset=1&cid={$pcp->contact_id}",
279 TRUE, NULL, FALSE,
280 FALSE
281 );
282 $this->assign('supporterUrl', $supporterUrl);
283 $supporterName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $pcp->contact_id, 'display_name');
284 $this->assign('supporterName', $supporterName);
285
286 if ($this->_component == 'contribute') {
287 $pageUrl = CRM_Utils_System::url('civicrm/contribute/transact',
288 "reset=1&id={$pcpBlock->entity_id}",
289 TRUE, NULL, FALSE,
290 TRUE
291 );
292 $contribPageTitle = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $pcpBlock->entity_id, 'title');
293 }
294 elseif ($this->_component == 'event') {
295 $pageUrl = CRM_Utils_System::url('civicrm/event',
296 "reset=1&id={$pcpBlock->entity_id}",
297 TRUE, NULL, FALSE,
298 TRUE
299 );
300 $contribPageTitle = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $pcpBlock->entity_id, 'title');
301 }
302
303 $this->assign('contribPageUrl', $pageUrl);
304 $this->assign('contribPageTitle', $contribPageTitle);
305
306 $managePCPUrl = CRM_Utils_System::url('civicrm/admin/pcp',
307 "reset=1",
308 TRUE, NULL, FALSE,
309 FALSE
310 );
311 $this->assign('managePCPUrl', $managePCPUrl);
312
313 //get the default domain email address.
314 list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail();
315
316 if (!$domainEmailAddress || $domainEmailAddress == 'info@EXAMPLE.ORG') {
317 $fixUrl = CRM_Utils_System::url('civicrm/admin/domain', 'action=update&reset=1');
318 CRM_Core_Error::fatal(ts('The site administrator needs to enter a valid \'FROM Email Address\' in <a href="%1">Administer CiviCRM &raquo; Communications &raquo; FROM Email Addresses</a>. The email address used may need to be a valid mail account with your email service provider.', array(1 => $fixUrl)));
319 }
320
321 //if more than one email present for PCP notification ,
322 //first email take it as To and other as CC and First email
323 //address should be sent in users email receipt for
324 //support purpose.
325 $emailArray = explode(',', $emails);
326 $to = $emailArray[0];
327 unset($emailArray[0]);
328 $cc = implode(',', $emailArray);
329
330 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate(
331 array(
332 'groupName' => 'msg_tpl_workflow_contribution',
333 'valueName' => 'pcp_notify',
334 'contactId' => $contactID,
335 'from' => "$domainEmailName <$domainEmailAddress>",
336 'toEmail' => $to,
337 'cc' => $cc,
338 )
339 );
340
341 if ($sent) {
342 $notifyStatus = ts('A notification email has been sent to the site administrator.');
343 }
344 }
345
346 CRM_Core_BAO_File::processAttachment($params, 'civicrm_pcp', $pcp->id);
347
348 // send email notification to supporter, if initial setup / add mode.
349 if (!$this->_pageId) {
350 CRM_PCP_BAO_PCP::sendStatusUpdate($pcp->id, $statusId, TRUE, $this->_component);
351 if ($approvalMessage && CRM_Utils_Array::value('status_id', $params) == 1) {
352 $notifyStatus .= ts(' You will receive a second email as soon as the review process is complete.');
353 }
354 }
355
356 //check if pcp created by anonymous user
357 $anonymousPCP = 0;
358 if (!$session->get('userID')) {
359 $anonymousPCP = 1;
360 }
361
362 CRM_Core_Session::setStatus(ts("Your Personal Campaign Page has been %1 %2 %3",
363 array(1 => $pageStatus, 2 => $approvalMessage, 3 => $notifyStatus)
364 ), '', 'info');
365 if (!$this->_pageId) {
366 $session->pushUserContext(CRM_Utils_System::url('civicrm/pcp/info', "reset=1&id={$pcp->id}&ap={$anonymousPCP}"));
367 }
368 elseif ($this->_context == 'dashboard') {
369 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/pcp', 'reset=1'));
370 }
371 }
372
373 }