3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * This class generates form components generic to useradd.
31 class CRM_Contact_Form_Task_Useradd
extends CRM_Core_Form
{
34 * The contact id, used when adding user
38 protected $_contactId;
41 * Contact.display_name of contact for whom we are adding user
48 * Primary email of contact for whom we are adding user.
54 public function preProcess() {
55 $params = $defaults = $ids = array();
57 $this->_contactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this, TRUE);
58 $params['id'] = $params['contact_id'] = $this->_contactId
;
59 $contact = CRM_Contact_BAO_Contact
::retrieve($params, $defaults, $ids);
60 $this->_displayName
= $contact->display_name
;
61 $this->_email
= $contact->email
;
62 CRM_Utils_System
::setTitle(ts('Create User Record for %1', array(1 => $this->_displayName
)));
66 * Set default values for the form.
68 public function setDefaultValues() {
70 $defaults['contactID'] = $this->_contactId
;
71 $defaults['name'] = $this->_displayName
;
72 if (!empty($this->_email
)) {
73 $defaults['email'] = $this->_email
[1]['email'];
80 * Build the form object.
82 public function buildQuickForm() {
83 $element = $this->add('text', 'name', ts('Full Name'), array('class' => 'huge'));
85 $this->add('text', 'cms_name', ts('Username'), array('class' => 'huge'));
86 $this->addRule('cms_name', 'Username is required', 'required');
87 $this->add('password', 'cms_pass', ts('Password'), array('class' => 'huge'));
88 $this->add('password', 'cms_confirm_pass', ts('Confirm Password'), array('class' => 'huge'));
89 $this->addRule('cms_pass', 'Password is required', 'required');
90 $this->addRule(array('cms_pass', 'cms_confirm_pass'), 'ERROR: Password mismatch', 'compare');
91 $this->add('text', 'email', ts('Email:'), array('class' => 'huge'))->freeze();
92 $this->add('hidden', 'contactID');
94 //add a rule to check username uniqueness
95 $this->addFormRule(array('CRM_Contact_Form_Task_Useradd', 'usernameRule'));
106 'name' => ts('Cancel'),
110 $this->setDefaults($this->setDefaultValues());
114 * Post process function.
116 public function postProcess() {
117 // store the submitted values in an array
118 $params = $this->exportValues();
120 CRM_Core_BAO_CMSUser
::create($params, 'email');
121 CRM_Core_Session
::setStatus('', ts('User Added'), 'success');
127 * @param array $params
131 public static function usernameRule($params) {
132 $config = CRM_Core_Config
::singleton();
134 $check_params = array(
135 'name' => $params['cms_name'],
136 'mail' => $params['email'],
138 $config->userSystem
->checkUserNameEmailExists($check_params, $errors);
140 return empty($errors) ?
TRUE : $errors;