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