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