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