Merge pull request #18286 from sunilpawar/ui_30
[civicrm-core.git] / CRM / Contact / Form / Task / Useradd.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 * This class generates form components generic to useradd.
14 */
15 class 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 /**
25 * Contact.display_name of contact for whom we are adding user
26 *
27 * @var int
28 */
29 public $_displayName;
30
31 /**
32 * Primary email of contact for whom we are adding user.
33 *
34 * @var int
35 */
36 public $_email;
37
38 public function preProcess() {
39 $params = $defaults = $ids = [];
40
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);
44 $this->_displayName = $contact->display_name;
45 $this->_email = $contact->email;
46 CRM_Utils_System::setTitle(ts('Create User Record for %1', [1 => $this->_displayName]));
47 }
48
49 /**
50 * Set default values for the form.
51 */
52 public function setDefaultValues() {
53 $defaults = [];
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 /**
64 * Build the form object.
65 */
66 public function buildQuickForm() {
67 $element = $this->add('text', 'name', ts('Full Name'), ['class' => 'huge']);
68 $element->freeze();
69 $this->add('text', 'cms_name', ts('Username'), ['class' => 'huge']);
70 $this->addRule('cms_name', 'Username is required', 'required');
71 $this->add('password', 'cms_pass', ts('Password'), ['class' => 'huge']);
72 $this->add('password', 'cms_confirm_pass', ts('Confirm Password'), ['class' => 'huge']);
73 $this->addRule('cms_pass', 'Password is required', 'required');
74 $this->addRule(['cms_pass', 'cms_confirm_pass'], 'ERROR: Password mismatch', 'compare');
75 $this->add('text', 'email', ts('Email:'), ['class' => 'huge'])->freeze();
76 $this->addRule('email', 'Email is required', 'required');
77 $this->add('hidden', 'contactID');
78
79 //add a rule to check username uniqueness
80 $this->addFormRule(['CRM_Contact_Form_Task_Useradd', 'usernameRule']);
81
82 $this->addButtons(
83 [
84 [
85 'type' => 'next',
86 'name' => ts('Add'),
87 'isDefault' => TRUE,
88 ],
89 [
90 'type' => 'cancel',
91 'name' => ts('Cancel'),
92 ],
93 ]
94 );
95 $this->setDefaults($this->setDefaultValues());
96 }
97
98 /**
99 * Post process function.
100 */
101 public function postProcess() {
102 // store the submitted values in an array
103 $params = $this->exportValues();
104
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 }
111 }
112
113 /**
114 * Validation Rule.
115 *
116 * @param array $params
117 *
118 * @return array|bool
119 */
120 public static function usernameRule($params) {
121 $config = CRM_Core_Config::singleton();
122 $errors = [];
123 $check_params = [
124 'name' => $params['cms_name'],
125 'mail' => $params['email'],
126 ];
127 $config->userSystem->checkUserNameEmailExists($check_params, $errors);
128
129 return empty($errors) ? TRUE : $errors;
130 }
131
132 }