Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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-2013 | |
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 | ||
119 | function setDefaultValues() { | |
71fc6ea4 | 120 | $this->_defaults = array(); |
f49b8be4 RN |
121 | if ($this->_contactID) { |
122 | foreach ($this->_fields as $name => $dontcare) { | |
123 | $fields[$name] = 1; | |
124 | } | |
2efcf0c2 | 125 | |
f49b8be4 | 126 | CRM_Core_BAO_UFGroup::setProfileDefaults($this->_contactID, $fields, $this->_defaults); |
6a488035 TO |
127 | } |
128 | $stateCountryMap = array(); | |
6a488035 TO |
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 | if ((substr($name, 0, 14) === 'state_province') || (substr($name, 0, 7) === 'country') || (substr($name, 0, 6) === 'county')) { | |
139 | list($fieldName, $index) = CRM_Utils_System::explode('-', $name, 2); | |
140 | if (!array_key_exists($index, $stateCountryMap)) { | |
141 | $stateCountryMap[$index] = array(); | |
142 | } | |
143 | $stateCountryMap[$index][$fieldName] = $name; | |
144 | } | |
145 | ||
25482510 | 146 | // also take care of state country widget |
6a488035 TO |
147 | if (!empty($stateCountryMap)) { |
148 | CRM_Core_BAO_Address::addStateCountryMap($stateCountryMap, $this->_defaults); | |
149 | } | |
150 | } | |
25482510 | 151 | // now fix all state country selectors |
6a488035 TO |
152 | CRM_Core_BAO_Address::fixAllStateSelects($this, $this->_defaults); |
153 | return $this->_defaults; | |
154 | } | |
155 | ||
156 | /** | |
157 | * Function to build the form | |
158 | * | |
159 | * @return None | |
160 | * @access public | |
161 | */ | |
162 | public function buildQuickForm() { | |
163 | $id = CRM_PCP_BAO_PCP::getSupporterProfileId($this->_pageId, $this->_component); | |
164 | if (CRM_PCP_BAO_PCP::checkEmailProfile($id)) { | |
165 | $this->assign('profileDisplay', TRUE); | |
166 | } | |
167 | $fields = NULL; | |
168 | if ($this->_contactID) { | |
169 | if (CRM_Core_BAO_UFGroup::filterUFGroups($id, $this->_contactID)) { | |
170 | $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD); | |
171 | } | |
172 | $this->addFormRule(array('CRM_PCP_Form_PCPAccount', 'formRule'), $this); | |
173 | } | |
174 | else { | |
175 | CRM_Core_BAO_CMSUser::buildForm($this, $id, TRUE); | |
176 | ||
177 | $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD); | |
178 | } | |
179 | ||
180 | if ($fields) { | |
181 | $this->assign('fields', $fields); | |
182 | $addCaptcha = FALSE; | |
183 | foreach ($fields as $key => $field) { | |
184 | if (isset($field['data_type']) && $field['data_type'] == 'File') { | |
185 | // ignore file upload fields | |
186 | continue; | |
187 | } | |
188 | CRM_Core_BAO_UFGroup::buildProfile($this, $field, CRM_Profile_Form::MODE_CREATE); | |
189 | $this->_fields[$key] = $field; | |
71fc6ea4 DG |
190 | |
191 | // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor | |
192 | if ($field['add_captcha'] && !$this->_contactID) { | |
6a488035 TO |
193 | $addCaptcha = TRUE; |
194 | } | |
195 | } | |
196 | ||
197 | if ($addCaptcha) { | |
198 | $captcha = &CRM_Utils_ReCAPTCHA::singleton(); | |
199 | $captcha->add($this); | |
200 | $this->assign('isCaptcha', TRUE); | |
201 | } | |
202 | } | |
203 | ||
204 | ||
205 | if ($this->_component == 'contribute') { | |
206 | $this->assign('campaignName', CRM_Contribute_PseudoConstant::contributionPage($this->_pageId)); | |
207 | } | |
208 | elseif ($this->_component == 'event') { | |
209 | $this->assign('campaignName', CRM_Event_PseudoConstant::event($this->_pageId)); | |
210 | } | |
211 | ||
212 | if ($this->_single) { | |
213 | $button = array( | |
214 | array('type' => 'next', | |
215 | 'name' => ts('Save'), | |
216 | 'spacing' => ' ', | |
217 | 'isDefault' => TRUE, | |
218 | ), | |
219 | array( | |
220 | 'type' => 'cancel', | |
221 | 'name' => ts('Cancel'), | |
222 | ), | |
223 | ); | |
224 | } | |
225 | else { | |
226 | $button[] = array( | |
227 | 'type' => 'next', | |
228 | 'name' => ts('Continue >>'), | |
229 | 'spacing' => ' ', | |
230 | 'isDefault' => TRUE, | |
231 | ); | |
232 | } | |
233 | $this->addFormRule(array('CRM_PCP_Form_PCPAccount', 'formRule'), $this); | |
234 | $this->addButtons($button); | |
235 | } | |
236 | ||
237 | /** | |
238 | * global form rule | |
239 | * | |
240 | * @param array $fields the input form values | |
241 | * @param array $files the uploaded files if any | |
242 | * @param array $options additional user data | |
243 | * | |
244 | * @return true if no errors, else array of errors | |
245 | * @access public | |
246 | * @static | |
247 | */ | |
248 | static function formRule($fields, $files, $self) { | |
249 | $errors = array(); | |
250 | foreach ($fields as $key => $value) { | |
251 | if (strpos($key, 'email-') !== FALSE && !empty($value)) { | |
252 | $ufContactId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFMatch', $value, 'contact_id', 'uf_name'); | |
253 | if ($ufContactId && $ufContactId != $self->_contactID) { | |
254 | $errors[$key] = ts('There is already an user associated with this email address. Please enter different email address.'); | |
255 | } | |
256 | } | |
257 | } | |
258 | return empty($errors) ? TRUE : $errors; | |
259 | } | |
260 | ||
261 | /** | |
262 | * Function to process the form | |
263 | * | |
264 | * @access public | |
265 | * | |
266 | * @return None | |
267 | */ | |
268 | public function postProcess() { | |
269 | $params = $this->controller->exportValues($this->getName()); | |
270 | ||
271 | if (!$this->_contactID && isset($params['cms_create_account'])) { | |
272 | foreach ($params as $key => $value) { | |
273 | if (substr($key, 0, 5) == 'email' && !empty($value)) { | |
274 | list($fieldName, $locTypeId) = CRM_Utils_System::explode('-', $key, 2); | |
275 | $isPrimary = 0; | |
276 | if ($locTypeId == 'Primary') { | |
277 | $locTypeDefault = CRM_Core_BAO_LocationType::getDefault(); | |
278 | $locTypeId = NULL; | |
279 | if ($locTypeDefault) { | |
280 | $locTypeId = $locTypeDefault->id; | |
281 | } | |
282 | $isPrimary = 1; | |
283 | } | |
284 | ||
285 | $params['email'] = array(); | |
286 | $params['email'][1]['email'] = $value; | |
287 | $params['email'][1]['location_type_id'] = $locTypeId; | |
288 | $params['email'][1]['is_primary'] = $isPrimary; | |
289 | } | |
290 | } | |
291 | } | |
292 | ||
293 | $dedupeParams = CRM_Dedupe_Finder::formatParams($params, 'Individual'); | |
294 | $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual', 'Unsupervised'); | |
295 | if ($ids) { | |
296 | $this->_contactID = $ids['0']; | |
297 | } | |
9d9922e7 | 298 | $contactID = CRM_Contact_BAO_Contact::createProfileContact($params, $this->_fields, $this->_contactID); |
6a488035 TO |
299 | $this->set('contactID', $contactID); |
300 | ||
301 | if (!empty($params['email'])) { | |
302 | $params['email'] = $params['email'][1]['email']; | |
303 | } | |
304 | ||
305 | CRM_Contribute_BAO_Contribution_Utils::createCMSUser($params, $contactID, 'email'); | |
306 | } | |
307 | } | |
308 |