fixed spelling of address on lines 122 and 247
[civicrm-core.git] / CRM / Contact / Form / Task / Useradd.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
f12c6f7d 13 * This class generates form components generic to useradd.
6a488035
TO
14 */
15class CRM_Contact_Form_Task_Useradd extends CRM_Core_Form {
16
17 /**
18 * The contact id, used when adding user
19 *
20 * @var int
21 */
22 protected $_contactId;
23
24 /**
100fef9d 25 * Contact.display_name of contact for whom we are adding user
6a488035
TO
26 *
27 * @var int
6a488035
TO
28 */
29 public $_displayName;
30
31 /**
fe482240 32 * Primary email of contact for whom we are adding user.
6a488035
TO
33 *
34 * @var int
6a488035
TO
35 */
36 public $_email;
37
00be9182 38 public function preProcess() {
be2fb01f 39 $params = $defaults = $ids = [];
6a488035 40
353ffa53
TO
41 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
42 $params['id'] = $params['contact_id'] = $this->_contactId;
43 $contact = CRM_Contact_BAO_Contact::retrieve($params, $defaults, $ids);
6a488035 44 $this->_displayName = $contact->display_name;
353ffa53 45 $this->_email = $contact->email;
be2fb01f 46 CRM_Utils_System::setTitle(ts('Create User Record for %1', [1 => $this->_displayName]));
6a488035
TO
47 }
48
49 /**
f12c6f7d 50 * Set default values for the form.
6a488035 51 */
00be9182 52 public function setDefaultValues() {
be2fb01f 53 $defaults = [];
6a488035
TO
54 $defaults['contactID'] = $this->_contactId;
55 $defaults['name'] = $this->_displayName;
56 if (!empty($this->_email)) {
57 $defaults['email'] = $this->_email[1]['email'];
58 }
59
60 return $defaults;
61 }
62
63 /**
fe482240 64 * Build the form object.
6a488035
TO
65 */
66 public function buildQuickForm() {
be2fb01f 67 $element = $this->add('text', 'name', ts('Full Name'), ['class' => 'huge']);
6a488035 68 $element->freeze();
be2fb01f 69 $this->add('text', 'cms_name', ts('Username'), ['class' => 'huge']);
6a488035 70 $this->addRule('cms_name', 'Username is required', 'required');
be2fb01f
CW
71 $this->add('password', 'cms_pass', ts('Password'), ['class' => 'huge']);
72 $this->add('password', 'cms_confirm_pass', ts('Confirm Password'), ['class' => 'huge']);
6a488035 73 $this->addRule('cms_pass', 'Password is required', 'required');
be2fb01f
CW
74 $this->addRule(['cms_pass', 'cms_confirm_pass'], 'ERROR: Password mismatch', 'compare');
75 $this->add('text', 'email', ts('Email:'), ['class' => 'huge'])->freeze();
454490fc 76 $this->addRule('email', 'Email is required', 'required');
6a488035
TO
77 $this->add('hidden', 'contactID');
78
79 //add a rule to check username uniqueness
be2fb01f 80 $this->addFormRule(['CRM_Contact_Form_Task_Useradd', 'usernameRule']);
6a488035
TO
81
82 $this->addButtons(
be2fb01f
CW
83 [
84 [
6a488035
TO
85 'type' => 'next',
86 'name' => ts('Add'),
87 'isDefault' => TRUE,
be2fb01f
CW
88 ],
89 [
6a488035
TO
90 'type' => 'cancel',
91 'name' => ts('Cancel'),
be2fb01f
CW
92 ],
93 ]
6a488035
TO
94 );
95 $this->setDefaults($this->setDefaultValues());
96 }
97
98 /**
f12c6f7d 99 * Post process function.
6a488035
TO
100 */
101 public function postProcess() {
102 // store the submitted values in an array
103 $params = $this->exportValues();
104
454490fc 105 if (CRM_Core_BAO_CMSUser::create($params, 'email') === FALSE) {
106 CRM_Core_Error::statusBounce(ts('Error creating CMS user account.'));
107 }
108 else {
109 CRM_Core_Session::setStatus(ts('User Added'), '', 'success');
110 }
6a488035
TO
111 }
112
113 /**
fe482240 114 * Validation Rule.
ad37ac8e 115 *
116 * @param array $params
117 *
118 * @return array|bool
6a488035 119 */
00be9182 120 public static function usernameRule($params) {
6a488035 121 $config = CRM_Core_Config::singleton();
be2fb01f
CW
122 $errors = [];
123 $check_params = [
6a488035
TO
124 'name' => $params['cms_name'],
125 'mail' => $params['email'],
be2fb01f 126 ];
6a488035
TO
127 $config->userSystem->checkUserNameEmailExists($check_params, $errors);
128
129 return empty($errors) ? TRUE : $errors;
130 }
96025800 131
6a488035 132}