Merge pull request #20096 from larssandergreen/copy-template_type-when-cloning-mailing
[civicrm-core.git] / CRM / PCP / Form / PCPAccount.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class generates form components for processing a contribution.
20 */
21 class CRM_PCP_Form_PCPAccount extends CRM_Core_Form {
22
23 /**
24 * Variable defined for Contribution Page Id.
25 * @var int
26 */
27 public $_pageId = NULL;
28 public $_id = NULL;
29 public $_component = NULL;
30
31 /**
32 * Are we in single form mode or wizard mode?
33 *
34 * @var bool
35 */
36 public $_single;
37
38 /**
39 * Get the active UFGroups (profiles) on this form
40 * Many forms load one or more UFGroups (profiles).
41 * This provides a standard function to retrieve the IDs of those profiles from the form
42 * so that you can implement things such as "is is_captcha field set on any of the active profiles on this form?"
43 *
44 * NOT SUPPORTED FOR USE OUTSIDE CORE EXTENSIONS - Added for reCAPTCHA core extension.
45 *
46 * @return array
47 */
48 public function getUFGroupIDs() {
49 $ufGroupIDs = [];
50 if (!empty($this->_pageId)) {
51 $ufGroupIDs[] = CRM_PCP_BAO_PCP::getSupporterProfileId($this->_pageId, $this->_component);
52 }
53 return $ufGroupIDs;
54 }
55
56 public function preProcess() {
57 $session = CRM_Core_Session::singleton();
58 $config = CRM_Core_Config::singleton();
59 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE);
60 $this->_pageId = CRM_Utils_Request::retrieve('pageId', 'Positive', $this);
61 $this->_component = CRM_Utils_Request::retrieve('component', 'String', $this);
62 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
63
64 if (!$this->_pageId && $config->userFramework == 'Joomla' && $config->userFrameworkFrontend) {
65 $this->_pageId = $this->_id;
66 }
67
68 if ($this->_id) {
69 $contactID = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $this->_id, 'contact_id');
70 }
71
72 $this->_contactID = $contactID ?? CRM_Core_Session::getLoggedInContactID();
73 if (!$this->_pageId) {
74 if (!$this->_id) {
75 $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.');
76 CRM_Core_Error::statusBounce($msg);
77 }
78 else {
79 $this->_pageId = CRM_Core_DAO::getFieldValue('CRM_PCP_DAO_PCP', $this->_id, 'page_id');
80 }
81 }
82
83 if (!$this->_pageId) {
84 CRM_Core_Error::statusBounce(ts('Could not find source page id.'));
85 }
86
87 $this->_single = $this->get('single');
88
89 if (!$this->_single) {
90 $this->_single = $session->get('singleForm');
91 }
92
93 $this->set('action', $this->_action);
94 $this->set('page_id', $this->_id);
95 $this->set('component_page_id', $this->_pageId);
96
97 // we do not want to display recently viewed items, so turn off
98 $this->assign('displayRecent', FALSE);
99
100 $this->assign('pcpComponent', $this->_component);
101
102 if ($this->_single) {
103 CRM_Utils_System::setTitle(ts('Update Contact Information'));
104 }
105 }
106
107 /**
108 * @return array
109 */
110 public function setDefaultValues() {
111 $this->_defaults = [];
112 if ($this->_contactID) {
113 foreach ($this->_fields as $name => $dontcare) {
114 $fields[$name] = 1;
115 }
116
117 CRM_Core_BAO_UFGroup::setProfileDefaults($this->_contactID, $fields, $this->_defaults);
118 }
119 //set custom field defaults
120 foreach ($this->_fields as $name => $field) {
121 if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($name)) {
122 if (!isset($this->_defaults[$name])) {
123 CRM_Core_BAO_CustomField::setProfileDefaults($customFieldID, $name, $this->_defaults,
124 NULL, CRM_Profile_Form::MODE_REGISTER
125 );
126 }
127 }
128 }
129 return $this->_defaults;
130 }
131
132 /**
133 * Build the form object.
134 *
135 * @return void
136 */
137 public function buildQuickForm() {
138 $ufGroupID = CRM_PCP_BAO_PCP::getSupporterProfileId($this->_pageId, $this->_component);
139 if (CRM_PCP_BAO_PCP::checkEmailProfile($ufGroupID)) {
140 $this->assign('profileDisplay', TRUE);
141 }
142 $fields = NULL;
143 if ($this->_contactID) {
144 if (CRM_Core_BAO_UFGroup::filterUFGroups($ufGroupID, $this->_contactID)) {
145 $fields = CRM_Core_BAO_UFGroup::getFields($ufGroupID, FALSE, CRM_Core_Action::ADD);
146 }
147 $this->addFormRule(['CRM_PCP_Form_PCPAccount', 'formRule'], $this);
148 }
149 else {
150 CRM_Core_BAO_CMSUser::buildForm($this, $ufGroupID, TRUE);
151
152 $fields = CRM_Core_BAO_UFGroup::getFields($ufGroupID, FALSE, CRM_Core_Action::ADD);
153 }
154
155 if ($fields) {
156 $this->assign('fields', $fields);
157 $addCaptcha = FALSE;
158 foreach ($fields as $key => $field) {
159 if (isset($field['data_type']) && $field['data_type'] == 'File') {
160 // ignore file upload fields
161 continue;
162 }
163 CRM_Core_BAO_UFGroup::buildProfile($this, $field, CRM_Profile_Form::MODE_CREATE);
164 $this->_fields[$key] = $field;
165
166 // CRM-11316 Is ReCAPTCHA enabled for this profile AND is this an anonymous visitor
167 if ($field['add_captcha'] && !$this->_contactID) {
168 $addCaptcha = TRUE;
169 }
170 }
171
172 if ($addCaptcha) {
173 CRM_Utils_ReCAPTCHA::enableCaptchaOnForm($this);
174 }
175 }
176
177 if ($this->_component == 'contribute') {
178 $this->assign('campaignName', CRM_Contribute_PseudoConstant::contributionPage($this->_pageId));
179 }
180 elseif ($this->_component == 'event') {
181 $this->assign('campaignName', CRM_Event_PseudoConstant::event($this->_pageId));
182 }
183
184 if ($this->_single) {
185 $button = [
186 [
187 'type' => 'next',
188 'name' => ts('Save'),
189 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
190 'isDefault' => TRUE,
191 ],
192 [
193 'type' => 'cancel',
194 'name' => ts('Cancel'),
195 ],
196 ];
197 }
198 else {
199 $button[] = [
200 'type' => 'next',
201 'name' => ts('Continue'),
202 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
203 'isDefault' => TRUE,
204 ];
205 }
206 $this->addFormRule(['CRM_PCP_Form_PCPAccount', 'formRule'], $this);
207 $this->addButtons($button);
208 }
209
210 /**
211 * Global form rule.
212 *
213 * @param array $fields
214 * The input form values.
215 * @param array $files
216 * The uploaded files if any.
217 * @param $self
218 *
219 *
220 * @return bool|array
221 * true if no errors, else array of errors
222 */
223 public static function formRule($fields, $files, $self) {
224 $errors = [];
225 foreach ($fields as $key => $value) {
226 if (strpos($key, 'email-') !== FALSE && !empty($value)) {
227 $ufContactId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFMatch', $value, 'contact_id', 'uf_name');
228 if ($ufContactId && $ufContactId != $self->_contactID) {
229 $errors[$key] = ts('There is already an user associated with this email address. Please enter different email address.');
230 }
231 }
232 }
233 return empty($errors) ? TRUE : $errors;
234 }
235
236 /**
237 * Process the form submission.
238 *
239 *
240 * @return void
241 */
242 public function postProcess() {
243 $params = $this->controller->exportValues($this->getName());
244
245 if (!$this->_contactID && isset($params['cms_create_account'])) {
246 foreach ($params as $key => $value) {
247 if (substr($key, 0, 5) == 'email' && !empty($value)) {
248 list($fieldName, $locTypeId) = CRM_Utils_System::explode('-', $key, 2);
249 $isPrimary = 0;
250 if ($locTypeId == 'Primary') {
251 $locTypeDefault = CRM_Core_BAO_LocationType::getDefault();
252 $locTypeId = NULL;
253 if ($locTypeDefault) {
254 $locTypeId = $locTypeDefault->id;
255 }
256 $isPrimary = 1;
257 }
258
259 $params['email'] = [];
260 $params['email'][1]['email'] = $value;
261 $params['email'][1]['location_type_id'] = $locTypeId;
262 $params['email'][1]['is_primary'] = $isPrimary;
263 }
264 }
265 }
266
267 $this->_contactID = CRM_Contact_BAO_Contact::getFirstDuplicateContact($params, 'Individual', 'Unsupervised', [], FALSE);
268
269 $contactID = CRM_Contact_BAO_Contact::createProfileContact($params, $this->_fields, $this->_contactID);
270 $this->set('contactID', $contactID);
271
272 if (!empty($params['email'])) {
273 $params['email'] = $params['email'][1]['email'];
274 }
275
276 CRM_Contribute_BAO_Contribution_Utils::createCMSUser($params, $contactID, 'email');
277 }
278
279 }