[REF] Move handling of form elements back to the Form
[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->add('hidden', 'contactID');
77
78 //add a rule to check username uniqueness
79 $this->addFormRule(['CRM_Contact_Form_Task_Useradd', 'usernameRule']);
80
81 $this->addButtons(
82 [
83 [
84 'type' => 'next',
85 'name' => ts('Add'),
86 'isDefault' => TRUE,
87 ],
88 [
89 'type' => 'cancel',
90 'name' => ts('Cancel'),
91 ],
92 ]
93 );
94 $this->setDefaults($this->setDefaultValues());
95 }
96
97 /**
98 * Post process function.
99 */
100 public function postProcess() {
101 // store the submitted values in an array
102 $params = $this->exportValues();
103
104 CRM_Core_BAO_CMSUser::create($params, 'email');
105 CRM_Core_Session::setStatus('', ts('User Added'), 'success');
106 }
107
108 /**
109 * Validation Rule.
110 *
111 * @param array $params
112 *
113 * @return array|bool
114 */
115 public static function usernameRule($params) {
116 $config = CRM_Core_Config::singleton();
117 $errors = [];
118 $check_params = [
119 'name' => $params['cms_name'],
120 'mail' => $params['email'],
121 ];
122 $config->userSystem->checkUserNameEmailExists($check_params, $errors);
123
124 return empty($errors) ? TRUE : $errors;
125 }
126
127 }