Merge pull request #4606 from johanv/CRM-15636-price_set_event_and_contribution
[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 */
56 public $_single;
57
58 /**
59 * The default values for the form
60 *
61 * @var array
62 */
63 protected $_defaults;
64
65
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);
73
74 if (!$this->_pageId && $config->userFramework == 'Joomla' && $config->userFrameworkFrontend) {
75 $this->_pageId = $this->_id;
76 }
77
78 if ($this->_id) {
79 $contactID = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $this->_id, 'contact_id');
80 }
81
82 $this->_contactID = isset($contactID) ? $contactID : $session->get('userID');
83 if (!$this->_pageId) {
84 if (!$this->_id) {
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);
87 }
88 else {
89 $this->_pageId = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $this->_id, 'page_id');
90 }
91 }
92
93 if (!$this->_pageId) {
94 CRM_Core_Error::fatal(ts('Could not find source page id.'));
95 }
96
97 $this->_single = $this->get('single');
98
99 if (!$this->_single) {
100 $this->_single = $session->get('singleForm');
101 }
102
103 $this->set('action', $this->_action);
104 $this->set('page_id', $this->_id);
105 $this->set('component_page_id', $this->_pageId);
106
107 // we do not want to display recently viewed items, so turn off
108 $this->assign('displayRecent', FALSE);
109
110 $this->assign('pcpComponent', $this->_component);
111
112 if ($this->_single) {
113 CRM_Utils_System::setTitle(ts('Update Contact Information'));
114 }
115 }
116
117 /**
118 * @return array
119 */
120 public function setDefaultValues() {
121 $this->_defaults = array();
122 if ($this->_contactID) {
123 foreach ($this->_fields as $name => $dontcare) {
124 $fields[$name] = 1;
125 }
126
127 CRM_Core_BAO_UFGroup::setProfileDefaults($this->_contactID, $fields, $this->_defaults);
128 }
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
135 );
136 }
137 }
138 }
139 return $this->_defaults;
140 }
141
142 /**
143 * Build the form object
144 *
145 * @return void
146 */
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);
151 }
152 $fields = NULL;
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);
156 }
157 $this->addFormRule(array('CRM_PCP_Form_PCPAccount', 'formRule'), $this);
158 }
159 else {
160 CRM_Core_BAO_CMSUser::buildForm($this, $id, TRUE);
161
162 $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD);
163 }
164
165 if ($fields) {
166 $this->assign('fields', $fields);
167 $addCaptcha = FALSE;
168 foreach ($fields as $key => $field) {
169 if (isset($field['data_type']) && $field['data_type'] == 'File') {
170 // ignore file upload fields
171 continue;
172 }
173 CRM_Core_BAO_UFGroup::buildProfile($this, $field, CRM_Profile_Form::MODE_CREATE);
174 $this->_fields[$key] = $field;
175
176 // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor
177 if ($field['add_captcha'] && !$this->_contactID) {
178 $addCaptcha = TRUE;
179 }
180 }
181
182 if ($addCaptcha) {
183 $captcha = &CRM_Utils_ReCAPTCHA::singleton();
184 $captcha->add($this);
185 $this->assign('isCaptcha', TRUE);
186 }
187 }
188
189
190 if ($this->_component == 'contribute') {
191 $this->assign('campaignName', CRM_Contribute_PseudoConstant::contributionPage($this->_pageId));
192 }
193 elseif ($this->_component == 'event') {
194 $this->assign('campaignName', CRM_Event_PseudoConstant::event($this->_pageId));
195 }
196
197 if ($this->_single) {
198 $button = array(
199 array('type' => 'next',
200 'name' => ts('Save'),
201 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
202 'isDefault' => TRUE,
203 ),
204 array(
205 'type' => 'cancel',
206 'name' => ts('Cancel'),
207 ),
208 );
209 }
210 else {
211 $button[] = array(
212 'type' => 'next',
213 'name' => ts('Continue >>'),
214 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
215 'isDefault' => TRUE,
216 );
217 }
218 $this->addFormRule(array('CRM_PCP_Form_PCPAccount', 'formRule'), $this);
219 $this->addButtons($button);
220 }
221
222 /**
223 * Global form rule
224 *
225 * @param array $fields the input form values
226 * @param array $files the uploaded files if any
227 * @param $self
228 *
229 *
230 * @return true if no errors, else array of errors
231 * @static
232 */
233 public static function formRule($fields, $files, $self) {
234 $errors = array();
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.');
240 }
241 }
242 }
243 return empty($errors) ? TRUE : $errors;
244 }
245
246 /**
247 * Process the form submission
248 *
249 *
250 * @return void
251 */
252 public function postProcess() {
253 $params = $this->controller->exportValues($this->getName());
254
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);
259 $isPrimary = 0;
260 if ($locTypeId == 'Primary') {
261 $locTypeDefault = CRM_Core_BAO_LocationType::getDefault();
262 $locTypeId = NULL;
263 if ($locTypeDefault) {
264 $locTypeId = $locTypeDefault->id;
265 }
266 $isPrimary = 1;
267 }
268
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;
273 }
274 }
275 }
276
277 $dedupeParams = CRM_Dedupe_Finder::formatParams($params, 'Individual');
278 $dedupeParams['check_permission'] = FALSE;
279 $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual', 'Unsupervised');
280 if ($ids) {
281 $this->_contactID = $ids['0'];
282 }
283 $contactID = CRM_Contact_BAO_Contact::createProfileContact($params, $this->_fields, $this->_contactID);
284 $this->set('contactID', $contactID);
285
286 if (!empty($params['email'])) {
287 $params['email'] = $params['email'][1]['email'];
288 }
289
290 CRM_Contribute_BAO_Contribution_Utils::createCMSUser($params, $contactID, 'email');
291 }
292 }