3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class generates form components for processing a ontribution
40 class CRM_PCP_Form_PCPAccount
extends CRM_Core_Form
{
43 *Variable defined for Contribution Page Id
47 public $_pageId = NULL;
49 public $_component = NULL;
52 * Are we in single form mode or wizard mode?
59 * The default values for the form
66 public function preProcess() {
67 $session = CRM_Core_Session
::singleton();
68 $config = CRM_Core_Config
::singleton();
69 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE);
70 $this->_pageId
= CRM_Utils_Request
::retrieve('pageId', 'Positive', $this);
71 $this->_component
= CRM_Utils_Request
::retrieve('component', 'String', $this);
72 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
74 if (!$this->_pageId
&& $config->userFramework
== 'Joomla' && $config->userFrameworkFrontend
) {
75 $this->_pageId
= $this->_id
;
79 $contactID = CRM_Core_DAO
::getFieldValue('CRM_PCP_DAO_PCP', $this->_id
, 'contact_id');
82 $this->_contactID
= isset($contactID) ?
$contactID : $session->get('userID');
83 if (!$this->_pageId
) {
85 $msg = ts('We can\'t load the requested web page due to an incomplete link. This can be caused by using your browser\'s Back button or by using an incomplete or invalid link.');
86 CRM_Core_Error
::fatal($msg);
89 $this->_pageId
= CRM_Core_DAO
::getFieldValue('CRM_PCP_DAO_PCP', $this->_id
, 'page_id');
93 if (!$this->_pageId
) {
94 CRM_Core_Error
::fatal(ts('Could not find source page id.'));
97 $this->_single
= $this->get('single');
99 if (!$this->_single
) {
100 $this->_single
= $session->get('singleForm');
103 $this->set('action', $this->_action
);
104 $this->set('page_id', $this->_id
);
105 $this->set('component_page_id', $this->_pageId
);
107 // we do not want to display recently viewed items, so turn off
108 $this->assign('displayRecent', FALSE);
110 $this->assign('pcpComponent', $this->_component
);
112 if ($this->_single
) {
113 CRM_Utils_System
::setTitle(ts('Update Contact Information'));
120 public function setDefaultValues() {
121 $this->_defaults
= array();
122 if ($this->_contactID
) {
123 foreach ($this->_fields
as $name => $dontcare) {
127 CRM_Core_BAO_UFGroup
::setProfileDefaults($this->_contactID
, $fields, $this->_defaults
);
129 //set custom field defaults
130 foreach ($this->_fields
as $name => $field) {
131 if ($customFieldID = CRM_Core_BAO_CustomField
::getKeyID($name)) {
132 if (!isset($this->_defaults
[$name])) {
133 CRM_Core_BAO_CustomField
::setProfileDefaults($customFieldID, $name, $this->_defaults
,
134 NULL, CRM_Profile_Form
::MODE_REGISTER
139 return $this->_defaults
;
143 * Build the form object
147 public function buildQuickForm() {
148 $id = CRM_PCP_BAO_PCP
::getSupporterProfileId($this->_pageId
, $this->_component
);
149 if (CRM_PCP_BAO_PCP
::checkEmailProfile($id)) {
150 $this->assign('profileDisplay', TRUE);
153 if ($this->_contactID
) {
154 if (CRM_Core_BAO_UFGroup
::filterUFGroups($id, $this->_contactID
)) {
155 $fields = CRM_Core_BAO_UFGroup
::getFields($id, FALSE, CRM_Core_Action
::ADD
);
157 $this->addFormRule(array('CRM_PCP_Form_PCPAccount', 'formRule'), $this);
160 CRM_Core_BAO_CMSUser
::buildForm($this, $id, TRUE);
162 $fields = CRM_Core_BAO_UFGroup
::getFields($id, FALSE, CRM_Core_Action
::ADD
);
166 $this->assign('fields', $fields);
168 foreach ($fields as $key => $field) {
169 if (isset($field['data_type']) && $field['data_type'] == 'File') {
170 // ignore file upload fields
173 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, CRM_Profile_Form
::MODE_CREATE
);
174 $this->_fields
[$key] = $field;
176 // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor
177 if ($field['add_captcha'] && !$this->_contactID
) {
183 $captcha = &CRM_Utils_ReCAPTCHA
::singleton();
184 $captcha->add($this);
185 $this->assign('isCaptcha', TRUE);
190 if ($this->_component
== 'contribute') {
191 $this->assign('campaignName', CRM_Contribute_PseudoConstant
::contributionPage($this->_pageId
));
193 elseif ($this->_component
== 'event') {
194 $this->assign('campaignName', CRM_Event_PseudoConstant
::event($this->_pageId
));
197 if ($this->_single
) {
199 array('type' => 'next',
200 'name' => ts('Save'),
201 'spacing' => ' ',
206 'name' => ts('Cancel'),
213 'name' => ts('Continue >>'),
214 'spacing' => ' ',
218 $this->addFormRule(array('CRM_PCP_Form_PCPAccount', 'formRule'), $this);
219 $this->addButtons($button);
225 * @param array $fields the input form values
226 * @param array $files the uploaded files if any
230 * @return true if no errors, else array of errors
233 public static function formRule($fields, $files, $self) {
235 foreach ($fields as $key => $value) {
236 if (strpos($key, 'email-') !== FALSE && !empty($value)) {
237 $ufContactId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFMatch', $value, 'contact_id', 'uf_name');
238 if ($ufContactId && $ufContactId != $self->_contactID
) {
239 $errors[$key] = ts('There is already an user associated with this email address. Please enter different email address.');
243 return empty($errors) ?
TRUE : $errors;
247 * Process the form submission
252 public function postProcess() {
253 $params = $this->controller
->exportValues($this->getName());
255 if (!$this->_contactID
&& isset($params['cms_create_account'])) {
256 foreach ($params as $key => $value) {
257 if (substr($key, 0, 5) == 'email' && !empty($value)) {
258 list($fieldName, $locTypeId) = CRM_Utils_System
::explode('-', $key, 2);
260 if ($locTypeId == 'Primary') {
261 $locTypeDefault = CRM_Core_BAO_LocationType
::getDefault();
263 if ($locTypeDefault) {
264 $locTypeId = $locTypeDefault->id
;
269 $params['email'] = array();
270 $params['email'][1]['email'] = $value;
271 $params['email'][1]['location_type_id'] = $locTypeId;
272 $params['email'][1]['is_primary'] = $isPrimary;
277 $dedupeParams = CRM_Dedupe_Finder
::formatParams($params, 'Individual');
278 $dedupeParams['check_permission'] = FALSE;
279 $ids = CRM_Dedupe_Finder
::dupesByParams($dedupeParams, 'Individual', 'Unsupervised');
281 $this->_contactID
= $ids['0'];
283 $contactID = CRM_Contact_BAO_Contact
::createProfileContact($params, $this->_fields
, $this->_contactID
);
284 $this->set('contactID', $contactID);
286 if (!empty($params['email'])) {
287 $params['email'] = $params['email'][1]['email'];
290 CRM_Contribute_BAO_Contribution_Utils
::createCMSUser($params, $contactID, 'email');