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