_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE); $params['id'] = $params['contact_id'] = $this->_contactId; $contact = CRM_Contact_BAO_Contact::retrieve($params, $defaults, $ids); $this->_displayName = $contact->display_name; $this->_email = $contact->email; CRM_Utils_System::setTitle(ts('Create User Record for %1', array(1 => $this->_displayName))); } /** * Set default values for the form. Note that in edit/view mode * the default values are retrieved from the database * * @access public * * @return void */ function setDefaultValues() { $defaults = array(); $defaults['contactID'] = $this->_contactId; $defaults['name'] = $this->_displayName; if (!empty($this->_email)) { $defaults['email'] = $this->_email[1]['email']; } return $defaults; } /** * Build the form object * * @return void * @access public */ public function buildQuickForm() { $element = $this->add('text', 'name', ts('Full Name'), array('class' => 'huge')); $element->freeze(); $this->add('text', 'cms_name', ts('Username'), array('class' => 'huge')); $this->addRule('cms_name', 'Username is required', 'required'); $this->add('password', 'cms_pass', ts('Password'), array('class' => 'huge')); $this->add('password', 'cms_confirm_pass', ts('Confirm Password'), array('class' => 'huge')); $this->addRule('cms_pass', 'Password is required', 'required'); $this->addRule(array('cms_pass', 'cms_confirm_pass'), 'ERROR: Password mismatch', 'compare'); $this->add('text', 'email', ts('Email:'), array('class' => 'huge'))->freeze(); $this->add('hidden', 'contactID'); //add a rule to check username uniqueness $this->addFormRule(array('CRM_Contact_Form_Task_Useradd', 'usernameRule')); $this->addButtons( array( array( 'type' => 'next', 'name' => ts('Add'), 'isDefault' => TRUE, ), array( 'type' => 'cancel', 'name' => ts('Cancel'), ), ) ); $this->setDefaults($this->setDefaultValues()); } /** * @access public * * @return void */ public function postProcess() { // store the submitted values in an array $params = $this->exportValues(); CRM_Core_BAO_CMSUser::create($params, 'email'); CRM_Core_Session::setStatus('', ts('User Added'), 'success'); } /** * Validation Rule * * @static */ static function usernameRule($params) { $config = CRM_Core_Config::singleton(); $errors = array(); $check_params = array( 'name' => $params['cms_name'], 'mail' => $params['email'], ); $config->userSystem->checkUserNameEmailExists($check_params, $errors); return empty($errors) ? TRUE : $errors; } }