Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-07-14-22-39-05
[civicrm-core.git] / CRM / PCP / Form / Campaign.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 * 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 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 }
90
91 $this->_contactID = CRM_Utils_Array::value('contact_id', $defaults);
92 $this->_contriPageId = CRM_Utils_Array::value('page_id', $defaults);
93
94 return $defaults;
95 }
96
97 /**
98 * Function to build the form
99 *
100 * @return None
101 * @access public
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 $this->addElement('checkbox', 'is_active', ts('Active'));
132
133 $this->addButtons(
134 array(
135 array(
136 'type' => 'upload',
137 'name' => ts('Save'),
138 'isDefault' => TRUE,
139 ),
140 array(
141 'type' => 'cancel',
142 'name' => ts('Cancel'),
143 ),
144 )
145 );
146 $this->addFormRule(array('CRM_PCP_Form_Campaign', 'formRule'), $this);
147 }
148
149 /**
150 * global form rule
151 *
152 * @param array $fields the input form values
153 * @param array $files the uploaded files if any
154 * @param array $options additional user data
155 *
156 * @return true if no errors, else array of errors
157 * @access public
158 * @static
159 */
160 static function formRule($fields, $files, $self) {
161 $errors = array();
162 if ($fields['goal_amount'] <= 0) {
163 $errors['goal_amount'] = ts('Goal Amount should be a numeric value greater than zero.');
164 }
165 if (strlen($fields['donate_link_text']) >= 64) {
166 $errors['donate_link_text'] = ts('Button Text must be less than 64 characters.');
167 }
168 if (isset($files['attachFile_1']) &&
169 CRM_Utils_Array::value('tmp_name', $files['attachFile_1'])
170 ) {
171 list($width, $height) = getimagesize($files['attachFile_1']['tmp_name']);
172 if ($width > 360 || $height > 360) {
173 $errors['attachFile_1'] = ts('Your picture or image file cannot be larger than 360 x 360 pixels in size.') . ' ' . ts("The dimensions of the image you have selected are %1 x %2.", array(1 => $width, 2 => $height)) . ' ' . ts('Please shrink or crop the file or find another smaller image and try again.');
174 }
175 }
176 return $errors;
177 }
178
179 /**
180 * Function to process the form
181 *
182 * @access public
183 *
184 * @return None
185 */
186 public function postProcess() {
187 $params = $this->controller->exportValues( $this->_name );
188 $checkBoxes = array('is_thermometer', 'is_honor_roll', 'is_active');
189
190 foreach ($checkBoxes as $key) {
191 if (!isset($params[$key])) {
192 $params[$key] = 0;
193 }
194 }
195 $session = CRM_Core_Session::singleton();
196 $contactID = isset($this->_contactID) ? $this->_contactID : $session->get('userID');
197 if (!$contactID) {
198 $contactID = $this->get('contactID');
199 }
200 $params['title'] = $params['pcp_title'];
201 $params['intro_text'] = $params['pcp_intro_text'];
202 $params['contact_id'] = $contactID;
203 $params['page_id'] = $this->get('component_page_id') ? $this->get('component_page_id') : $this->_contriPageId;
204 $params['page_type'] = $this->_component;
205
206 // since we are allowing html input from the user
207 // we also need to purify it, so lets clean it up
208 $htmlFields = array( 'intro_text', 'page_text', 'title' );
209 foreach ( $htmlFields as $field ) {
210 if ( ! empty($params[$field]) ) {
211 $params[$field] = CRM_Utils_String::purifyHTML($params[$field]);
212 }
213 }
214
215 $entity_table = CRM_PCP_BAO_PCP::getPcpEntityTable($params['page_type']);
216
217 $pcpBlock = new CRM_PCP_DAO_PCPBlock();
218 $pcpBlock->entity_table = $entity_table;
219 $pcpBlock->entity_id = $params['page_id'];
220 $pcpBlock->find(TRUE);
221
222 $params['pcp_block_id'] = $pcpBlock->id;
223
224 $params['goal_amount'] = CRM_Utils_Rule::cleanMoney($params['goal_amount']);
225
226 $approval_needed = $pcpBlock->is_approval_needed;
227 $approvalMessage = NULL;
228
229 if ($this->get('action') & CRM_Core_Action::ADD) {
230 $params['status_id'] = $approval_needed ? 1 : 2;
231 $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.');
232 }
233
234 $params['id'] = $this->_pageId;
235
236 $pcp = CRM_PCP_BAO_PCP::add($params, FALSE);
237
238
239 // add attachments as needed
240 CRM_Core_BAO_File::formatAttachment($params,
241 $params,
242 'civicrm_pcp',
243 $pcp->id
244 );
245
246 $pageStatus = isset($this->_pageId) ? ts('updated') : ts('created');
247 $statusId = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $pcp->id, 'status_id');
248
249 //send notification of PCP create/update.
250 $pcpParams = array('entity_table' => $entity_table, 'entity_id' => $pcp->page_id);
251 $notifyParams = array();
252 $notifyStatus = "";
253 CRM_Core_DAO::commonRetrieve('CRM_PCP_DAO_PCPBlock', $pcpParams, $notifyParams, array('notify_email'));
254
255 if ($emails = $pcpBlock->notify_email) {
256 $this->assign('pcpTitle', $pcp->title);
257
258 if ($this->_pageId) {
259 $this->assign('mode', 'Update');
260 }
261 else {
262 $this->assign('mode', 'Add');
263 }
264 $pcpStatus = CRM_Core_OptionGroup::getLabel('pcp_status', $statusId);
265 $this->assign('pcpStatus', $pcpStatus);
266
267 $this->assign('pcpId', $pcp->id);
268
269 $supporterUrl = CRM_Utils_System::url('civicrm/contact/view',
270 "reset=1&cid={$pcp->contact_id}",
271 TRUE, NULL, FALSE,
272 FALSE
273 );
274 $this->assign('supporterUrl', $supporterUrl);
275 $supporterName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $pcp->contact_id, 'display_name');
276 $this->assign('supporterName', $supporterName);
277
278
279 if ($this->_component == 'contribute') {
280 $pageUrl = CRM_Utils_System::url('civicrm/contribute/transact',
281 "reset=1&id={$pcpBlock->entity_id}",
282 TRUE, NULL, FALSE,
283 TRUE
284 );
285 $contribPageTitle = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $pcpBlock->entity_id, 'title');
286 }
287 elseif ($this->_component == 'event') {
288 $pageUrl = CRM_Utils_System::url('civicrm/event',
289 "reset=1&id={$pcpBlock->entity_id}",
290 TRUE, NULL, FALSE,
291 TRUE
292 );
293 $contribPageTitle = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $pcpBlock->entity_id, 'title');
294 }
295
296 $this->assign('contribPageUrl', $pageUrl);
297 $this->assign('contribPageTitle', $contribPageTitle);
298
299 $managePCPUrl = CRM_Utils_System::url('civicrm/admin/pcp',
300 "reset=1",
301 TRUE, NULL, FALSE,
302 FALSE
303 );
304 $this->assign('managePCPUrl', $managePCPUrl);
305
306 //get the default domain email address.
307 list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail();
308
309 if (!$domainEmailAddress || $domainEmailAddress == 'info@EXAMPLE.ORG') {
310 $fixUrl = CRM_Utils_System::url('civicrm/admin/domain', 'action=update&reset=1');
311 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)));
312 }
313
314 //if more than one email present for PCP notification ,
315 //first email take it as To and other as CC and First email
316 //address should be sent in users email receipt for
317 //support purpose.
318 $emailArray = explode(',', $emails);
319 $to = $emailArray[0];
320 unset($emailArray[0]);
321 $cc = implode(',', $emailArray);
322
323 list($sent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplates::sendTemplate(
324 array(
325 'groupName' => 'msg_tpl_workflow_contribution',
326 'valueName' => 'pcp_notify',
327 'contactId' => $contactID,
328 'from' => "$domainEmailName <$domainEmailAddress>",
329 'toEmail' => $to,
330 'cc' => $cc,
331 )
332 );
333
334 if ($sent) {
335 $notifyStatus = ts('A notification email has been sent to the site administrator.');
336 }
337 }
338
339 CRM_Core_BAO_File::processAttachment($params, 'civicrm_pcp', $pcp->id);
340
341 // send email notification to supporter, if initial setup / add mode.
342 if (!$this->_pageId) {
343 CRM_PCP_BAO_PCP::sendStatusUpdate($pcp->id, $statusId, TRUE, $this->_component);
344 if ($approvalMessage && CRM_Utils_Array::value('status_id', $params) == 1) {
345 $notifyStatus .= ts(' You will receive a second email as soon as the review process is complete.');
346 }
347 }
348
349 //check if pcp created by anonymous user
350 $anonymousPCP = 0;
351 if (!$session->get('userID')) {
352 $anonymousPCP = 1;
353 }
354
355 CRM_Core_Session::setStatus(ts("Your Personal Campaign Page has been %1 %2 %3",
356 array(1 => $pageStatus, 2 => $approvalMessage, 3 => $notifyStatus)
357 ), '', 'info');
358 if (!$this->_pageId) {
359 $session->pushUserContext(CRM_Utils_System::url('civicrm/pcp/info', "reset=1&id={$pcp->id}&ap={$anonymousPCP}"));
360 }
361 elseif ($this->_context == 'dashboard') {
362 $session->pushUserContext(CRM_Utils_System::url('civicrm/admin/pcp', 'reset=1'));
363 }
364 }
365 }
366