Merge pull request #686 from totten/mktest-mail
[civicrm-core.git] / CRM / PCP / Form / PCPAccount.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
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
59 public function preProcess() {
60 $session = CRM_Core_Session::singleton();
61 $config = CRM_Core_Config::singleton();
62 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE);
63 $this->_pageId = CRM_Utils_Request::retrieve('pageId', 'Positive', $this);
64 $this->_component = CRM_Utils_Request::retrieve('component', 'String', $this);
65 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
66
67 if (!$this->_pageId && $config->userFramework == 'Joomla' && $config->userFrameworkFrontend) {
68 $this->_pageId = $this->_id;
69 }
70
71 if ($this->_id) {
72 $contactID = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $this->_id, 'contact_id');
73 }
74
75 $this->_contactID = isset($contactID) ? $contactID : $session->get('userID');
76 if (!$this->_pageId) {
77 if (!$this->_id) {
78 $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.');
79 CRM_Core_Error::fatal($msg);
80 }
81 else {
82 $this->_pageId = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $this->_id, 'page_id');
83 }
84 }
85
86 if (!$this->_pageId) {
87 CRM_Core_Error::fatal(ts('Could not find source page id.'));
88 }
89
90 $this->_single = $this->get('single');
91
92 if (!$this->_single) {
93 $this->_single = $session->get('singleForm');
94 }
95
96 $this->set('action', $this->_action);
97 $this->set('page_id', $this->_id);
98 $this->set('component_page_id', $this->_pageId);
99
100 // we do not want to display recently viewed items, so turn off
101 $this->assign('displayRecent', FALSE);
102
103 $this->assign('pcpComponent', $this->_component);
104
105 if ($this->_single) {
106 CRM_Utils_System::setTitle(ts('Update Contact Information'));
107 }
108 }
109
110 function setDefaultValues() {
111 if (!$this->_contactID) {
112 return;
113 }
114 $stateCountryMap = array();
115 foreach ($this->_fields as $name => $dontcare) {
116 $fields[$name] = 1;
117 }
118
119 CRM_Core_BAO_UFGroup::setProfileDefaults($this->_contactID, $fields, $this->_defaults);
120
121 //set custom field defaults
122 foreach ($this->_fields as $name => $field) {
123 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) {
124 if (!isset($this->_defaults[$name])) {
125 CRM_Core_BAO_CustomField::setProfileDefaults($customFieldID, $name, $this->_defaults,
126 NULL, CRM_Profile_Form::MODE_REGISTER
127 );
128 }
129 }
130 if ((substr($name, 0, 14) === 'state_province') || (substr($name, 0, 7) === 'country') || (substr($name, 0, 6) === 'county')) {
131 list($fieldName, $index) = CRM_Utils_System::explode('-', $name, 2);
132 if (!array_key_exists($index, $stateCountryMap)) {
133 $stateCountryMap[$index] = array();
134 }
135 $stateCountryMap[$index][$fieldName] = $name;
136 }
137
138 // also take care of state country widget
139 if (!empty($stateCountryMap)) {
140 CRM_Core_BAO_Address::addStateCountryMap($stateCountryMap, $this->_defaults);
141 }
142 }
143 // now fix all state country selectors
144 CRM_Core_BAO_Address::fixAllStateSelects($this, $this->_defaults);
145 return $this->_defaults;
146 }
147
148 /**
149 * Function to build the form
150 *
151 * @return None
152 * @access public
153 */
154 public function buildQuickForm() {
155 $id = CRM_PCP_BAO_PCP::getSupporterProfileId($this->_pageId, $this->_component);
156 if (CRM_PCP_BAO_PCP::checkEmailProfile($id)) {
157 $this->assign('profileDisplay', TRUE);
158 }
159 $fields = NULL;
160 if ($this->_contactID) {
161 if (CRM_Core_BAO_UFGroup::filterUFGroups($id, $this->_contactID)) {
162 $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD);
163 }
164 $this->addFormRule(array('CRM_PCP_Form_PCPAccount', 'formRule'), $this);
165 }
166 else {
167 CRM_Core_BAO_CMSUser::buildForm($this, $id, TRUE);
168
169 $fields = CRM_Core_BAO_UFGroup::getFields($id, FALSE, CRM_Core_Action::ADD);
170 }
171
172 if ($fields) {
173 $this->assign('fields', $fields);
174 $addCaptcha = FALSE;
175 foreach ($fields as $key => $field) {
176 if (isset($field['data_type']) && $field['data_type'] == 'File') {
177 // ignore file upload fields
178 continue;
179 }
180 CRM_Core_BAO_UFGroup::buildProfile($this, $field, CRM_Profile_Form::MODE_CREATE);
181 $this->_fields[$key] = $field;
182 if ($field['add_captcha']) {
183 $addCaptcha = TRUE;
184 }
185 }
186
187 if ($addCaptcha) {
188 $captcha = &CRM_Utils_ReCAPTCHA::singleton();
189 $captcha->add($this);
190 $this->assign('isCaptcha', TRUE);
191 }
192 }
193
194
195 if ($this->_component == 'contribute') {
196 $this->assign('campaignName', CRM_Contribute_PseudoConstant::contributionPage($this->_pageId));
197 }
198 elseif ($this->_component == 'event') {
199 $this->assign('campaignName', CRM_Event_PseudoConstant::event($this->_pageId));
200 }
201
202 if ($this->_single) {
203 $button = array(
204 array('type' => 'next',
205 'name' => ts('Save'),
206 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
207 'isDefault' => TRUE,
208 ),
209 array(
210 'type' => 'cancel',
211 'name' => ts('Cancel'),
212 ),
213 );
214 }
215 else {
216 $button[] = array(
217 'type' => 'next',
218 'name' => ts('Continue >>'),
219 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
220 'isDefault' => TRUE,
221 );
222 }
223 $this->addFormRule(array('CRM_PCP_Form_PCPAccount', 'formRule'), $this);
224 $this->addButtons($button);
225 }
226
227 /**
228 * global form rule
229 *
230 * @param array $fields the input form values
231 * @param array $files the uploaded files if any
232 * @param array $options additional user data
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 *
256 * @return None
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');
284 $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual', 'Unsupervised');
285 if ($ids) {
286 $this->_contactID = $ids['0'];
287 }
288 $contactID = &CRM_Contact_BAO_Contact::createProfileContact($params, $this->_fields, $this->_contactID);
289 $this->set('contactID', $contactID);
290
291 if (!empty($params['email'])) {
292 $params['email'] = $params['email'][1]['email'];
293 }
294
295 CRM_Contribute_BAO_Contribution_Utils::createCMSUser($params, $contactID, 'email');
296 }
297 }
298