3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * This class generates form components generic to useradd.
15 class CRM_Contact_Form_Task_Useradd
extends CRM_Core_Form
{
18 * The contact id, used when adding user
22 protected $_contactId;
25 * Contact.display_name of contact for whom we are adding user
32 * Primary email of contact for whom we are adding user.
38 public function preProcess() {
39 $params = $defaults = $ids = [];
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
]));
50 * Set default values for the form.
52 public function setDefaultValues() {
54 $defaults['contactID'] = $this->_contactId
;
55 $defaults['name'] = $this->_displayName
;
56 if (!empty($this->_email
)) {
57 $defaults['email'] = $this->_email
[1]['email'];
64 * Build the form object.
66 public function buildQuickForm() {
67 $element = $this->add('text', 'name', ts('Full Name'), ['class' => 'huge']);
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');
79 //add a rule to check username uniqueness
80 $this->addFormRule(['CRM_Contact_Form_Task_Useradd', 'usernameRule']);
91 'name' => ts('Cancel'),
95 $this->setDefaults($this->setDefaultValues());
99 * Post process function.
101 public function postProcess() {
102 // store the submitted values in an array
103 $params = $this->exportValues();
105 if (CRM_Core_BAO_CMSUser
::create($params, 'email') === FALSE) {
106 CRM_Core_Error
::statusBounce(ts('Error creating CMS user account.'));
109 CRM_Core_Session
::setStatus(ts('User Added'), '', 'success');
116 * @param array $params
120 public static function usernameRule($params) {
121 $config = CRM_Core_Config
::singleton();
124 'name' => $params['cms_name'],
125 'mail' => $params['email'],
127 $config->userSystem
->checkUserNameEmailExists($check_params, $errors);
129 return empty($errors) ?
TRUE : $errors;