INFRA-132 - Remove extra newlines from the bottom of docblocks
[civicrm-core.git] / CRM / PCP / Form / PCPAccount.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 processing a ontribution
38 *
39 */
40 class CRM_PCP_Form_PCPAccount extends CRM_Core_Form {
41
42 /**
43 * Variable defined for Contribution Page Id
44 */
45
46 public $_pageId = NULL;
47 public $_id = NULL;
48 public $_component = NULL;
49
50 /**
51 * Are we in single form mode or wizard mode?
52 *
53 * @var boolean
54 */
55 public $_single;
56
57 /**
58 * The default values for the form
59 *
60 * @var array
61 */
62 protected $_defaults;
63
64
65 public function preProcess() {
66 $session = CRM_Core_Session::singleton();
67 $config = CRM_Core_Config::singleton();
68 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE);
69 $this->_pageId = CRM_Utils_Request::retrieve('pageId', 'Positive', $this);
70 $this->_component = CRM_Utils_Request::retrieve('component', 'String', $this);
71 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
72
73 if (!$this->_pageId && $config->userFramework == 'Joomla' && $config->userFrameworkFrontend) {
74 $this->_pageId = $this->_id;
75 }
76
77 if ($this->_id) {
78 $contactID = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $this->_id, 'contact_id');
79 }
80
81 $this->_contactID = isset($contactID) ? $contactID : $session->get('userID');
82 if (!$this->_pageId) {
83 if (!$this->_id) {
84 $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.');
85 CRM_Core_Error::fatal($msg);
86 }
87 else {
88 $this->_pageId = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $this->_id, 'page_id');
89 }
90 }
91
92 if (!$this->_pageId) {
93 CRM_Core_Error::fatal(ts('Could not find source page id.'));
94 }
95
96 $this->_single = $this->get('single');
97
98 if (!$this->_single) {
99 $this->_single = $session->get('singleForm');
100 }
101
102 $this->set('action', $this->_action);
103 $this->set('page_id', $this->_id);
104 $this->set('component_page_id', $this->_pageId);
105
106 // we do not want to display recently viewed items, so turn off
107 $this->assign('displayRecent', FALSE);
108
109 $this->assign('pcpComponent', $this->_component);
110
111 if ($this->_single) {
112 CRM_Utils_System::setTitle(ts('Update Contact Information'));
113 }
114 }
115
116 /**
117 * @return array
118 */
119 public function setDefaultValues() {
120 $this->_defaults = array();
121 if ($this->_contactID) {
122 foreach ($this->_fields as $name => $dontcare) {
123 $fields[$name] = 1;
124 }
125
126 CRM_Core_BAO_UFGroup::setProfileDefaults($this->_contactID, $fields, $this->_defaults);
127 }
128 //set custom field defaults
129 foreach ($this->_fields as $name => $field) {
130 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) {
131 if (!isset($this->_defaults[$name])) {
132 CRM_Core_BAO_CustomField::setProfileDefaults($customFieldID, $name, $this->_defaults,
133 NULL, CRM_Profile_Form::MODE_REGISTER
134 );
135 }
136 }
137 }
138 return $this->_defaults;
139 }
140
141 /**
142 * Build the form object
143 *
144 * @return void
145 */
146 public function buildQuickForm() {
147 $id = CRM_PCP_BAO_PCP::getSupporterProfileId($this->_pageId, $this->_component);
148 if (CRM_PCP_BAO_PCP::checkEmailProfile($id)) {
149 $this->assign('profileDisplay', TRUE);
150 }
151 $fields = NULL;
152 if ($this->_contactID) {
153 if (CRM_Core_BAO_UFGroup::filterUFGroups($id, $this->_contactID)) {
154 $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD);
155 }
156 $this->addFormRule(array('CRM_PCP_Form_PCPAccount', 'formRule'), $this);
157 }
158 else {
159 CRM_Core_BAO_CMSUser::buildForm($this, $id, TRUE);
160
161 $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD);
162 }
163
164 if ($fields) {
165 $this->assign('fields', $fields);
166 $addCaptcha = FALSE;
167 foreach ($fields as $key => $field) {
168 if (isset($field['data_type']) && $field['data_type'] == 'File') {
169 // ignore file upload fields
170 continue;
171 }
172 CRM_Core_BAO_UFGroup::buildProfile($this, $field, CRM_Profile_Form::MODE_CREATE);
173 $this->_fields[$key] = $field;
174
175 // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor
176 if ($field['add_captcha'] && !$this->_contactID) {
177 $addCaptcha = TRUE;
178 }
179 }
180
181 if ($addCaptcha) {
182 $captcha = &CRM_Utils_ReCAPTCHA::singleton();
183 $captcha->add($this);
184 $this->assign('isCaptcha', TRUE);
185 }
186 }
187
188 if ($this->_component == 'contribute') {
189 $this->assign('campaignName', CRM_Contribute_PseudoConstant::contributionPage($this->_pageId));
190 }
191 elseif ($this->_component == 'event') {
192 $this->assign('campaignName', CRM_Event_PseudoConstant::event($this->_pageId));
193 }
194
195 if ($this->_single) {
196 $button = array(
197 array(
198 'type' => 'next',
199 'name' => ts('Save'),
200 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
201 'isDefault' => TRUE,
202 ),
203 array(
204 'type' => 'cancel',
205 'name' => ts('Cancel'),
206 ),
207 );
208 }
209 else {
210 $button[] = array(
211 'type' => 'next',
212 'name' => ts('Continue'),
213 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
214 'isDefault' => TRUE,
215 );
216 }
217 $this->addFormRule(array('CRM_PCP_Form_PCPAccount', 'formRule'), $this);
218 $this->addButtons($button);
219 }
220
221 /**
222 * Global form rule
223 *
224 * @param array $fields
225 * The input form values.
226 * @param array $files
227 * The uploaded files if any.
228 * @param $self
229 *
230 *
231 * @return true if no errors, else array of errors
232 * @static
233 */
234 public static function formRule($fields, $files, $self) {
235 $errors = array();
236 foreach ($fields as $key => $value) {
237 if (strpos($key, 'email-') !== FALSE && !empty($value)) {
238 $ufContactId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFMatch', $value, 'contact_id', 'uf_name');
239 if ($ufContactId && $ufContactId != $self->_contactID) {
240 $errors[$key] = ts('There is already an user associated with this email address. Please enter different email address.');
241 }
242 }
243 }
244 return empty($errors) ? TRUE : $errors;
245 }
246
247 /**
248 * Process the form submission
249 *
250 *
251 * @return void
252 */
253 public function postProcess() {
254 $params = $this->controller->exportValues($this->getName());
255
256 if (!$this->_contactID && isset($params['cms_create_account'])) {
257 foreach ($params as $key => $value) {
258 if (substr($key, 0, 5) == 'email' && !empty($value)) {
259 list($fieldName, $locTypeId) = CRM_Utils_System::explode('-', $key, 2);
260 $isPrimary = 0;
261 if ($locTypeId == 'Primary') {
262 $locTypeDefault = CRM_Core_BAO_LocationType::getDefault();
263 $locTypeId = NULL;
264 if ($locTypeDefault) {
265 $locTypeId = $locTypeDefault->id;
266 }
267 $isPrimary = 1;
268 }
269
270 $params['email'] = array();
271 $params['email'][1]['email'] = $value;
272 $params['email'][1]['location_type_id'] = $locTypeId;
273 $params['email'][1]['is_primary'] = $isPrimary;
274 }
275 }
276 }
277
278 $dedupeParams = CRM_Dedupe_Finder::formatParams($params, 'Individual');
279 $dedupeParams['check_permission'] = FALSE;
280 $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual', 'Unsupervised');
281 if ($ids) {
282 $this->_contactID = $ids['0'];
283 }
284 $contactID = CRM_Contact_BAO_Contact::createProfileContact($params, $this->_fields, $this->_contactID);
285 $this->set('contactID', $contactID);
286
287 if (!empty($params['email'])) {
288 $params['email'] = $params['email'][1]['email'];
289 }
290
291 CRM_Contribute_BAO_Contribution_Utils::createCMSUser($params, $contactID, 'email');
292 }
293 }