3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
32 class CRM_Contact_Form_Task_Useradd
extends CRM_Core_Form
{
35 * The contact id, used when adding user
39 protected $_contactId;
42 * contact.display_name of contact for whom we are adding user
50 * primary email of contact for whom we are adding user
57 function preProcess() {
58 $params = $defaults = $ids = array();
60 $this->_contactId
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this, TRUE);
61 $params['id'] = $params['contact_id'] = $this->_contactId
;
62 $contact = CRM_Contact_BAO_Contact
::retrieve($params, $defaults, $ids);
63 $this->_displayName
= $contact->display_name
;
64 $this->_email
= $contact->email
;
65 CRM_Utils_System
::setTitle(ts('Create User Record for %1', array(1 => $this->_displayName
)));
69 * This function sets the default values for the form. Note that in edit/view mode
70 * the default values are retrieved from the database
76 function setDefaultValues() {
78 $defaults['contactID'] = $this->_contactId
;
79 $defaults['name'] = $this->_displayName
;
80 if (!empty($this->_email
)) {
81 $defaults['email'] = $this->_email
[1]['email'];
88 * Function to actually build the form
93 public function buildQuickForm() {
94 $element = $this->add('text', 'name', ts('Full Name'), array('class' => 'huge'));
96 $this->add('text', 'cms_name', ts('Username'), array('class' => 'huge'));
97 $this->addRule('cms_name', 'Username is required', 'required');
98 $this->addRule('cms_name', 'Enter a valid username', 'minlength', 2);
99 $this->add('password', 'cms_pass', ts('Password'), array('class' => 'huge'));
100 $this->add('password', 'cms_confirm_pass', ts('Confirm Password'), array('class' => 'huge'));
101 $this->addRule('cms_pass', 'Password is required', 'required');
102 $this->addRule(array('cms_pass', 'cms_confirm_pass'), 'ERROR: Password mismatch', 'compare');
103 $this->add('text', 'email', ts('Email:'), array('class' => 'huge'))->freeze();
104 $this->add('hidden', 'contactID');
106 //add a rule to check username uniqueness
107 $this->addFormRule(array('CRM_Contact_Form_Task_Useradd', 'usernameRule'));
118 'name' => ts('Cancel'),
122 $this->setDefaults($this->setDefaultValues());
131 public function postProcess() {
132 // store the submitted values in an array
133 $params = $this->exportValues();
135 CRM_Core_BAO_CMSUser
::create($params, 'email');
136 CRM_Core_Session
::setStatus('', ts('User Added'), 'success');
144 static function usernameRule($params) {
145 $config = CRM_Core_Config
::singleton();
147 $check_params = array(
148 'name' => $params['cms_name'],
149 'mail' => $params['email'],
151 $config->userSystem
->checkUserNameEmailExists($check_params, $errors);
153 return empty($errors) ?
TRUE : $errors;