Merge pull request #7047 from johanv/CRM-17430-dont_change_domain_version_1st_attempt
[civicrm-core.git] / CRM / Contact / Form / Task / Useradd.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
f12c6f7d 29 * This class generates form components generic to useradd.
6a488035
TO
30 */
31class CRM_Contact_Form_Task_Useradd extends CRM_Core_Form {
32
33 /**
34 * The contact id, used when adding user
35 *
36 * @var int
37 */
38 protected $_contactId;
39
40 /**
100fef9d 41 * Contact.display_name of contact for whom we are adding user
6a488035
TO
42 *
43 * @var int
6a488035
TO
44 */
45 public $_displayName;
46
47 /**
fe482240 48 * Primary email of contact for whom we are adding user.
6a488035
TO
49 *
50 * @var int
6a488035
TO
51 */
52 public $_email;
53
00be9182 54 public function preProcess() {
6a488035
TO
55 $params = $defaults = $ids = array();
56
353ffa53
TO
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);
6a488035 60 $this->_displayName = $contact->display_name;
353ffa53 61 $this->_email = $contact->email;
6a488035
TO
62 CRM_Utils_System::setTitle(ts('Create User Record for %1', array(1 => $this->_displayName)));
63 }
64
65 /**
f12c6f7d 66 * Set default values for the form.
6a488035 67 */
00be9182 68 public function setDefaultValues() {
6a488035
TO
69 $defaults = array();
70 $defaults['contactID'] = $this->_contactId;
71 $defaults['name'] = $this->_displayName;
72 if (!empty($this->_email)) {
73 $defaults['email'] = $this->_email[1]['email'];
74 }
75
76 return $defaults;
77 }
78
79 /**
fe482240 80 * Build the form object.
6a488035
TO
81 */
82 public function buildQuickForm() {
83 $element = $this->add('text', 'name', ts('Full Name'), array('class' => 'huge'));
84 $element->freeze();
85 $this->add('text', 'cms_name', ts('Username'), array('class' => 'huge'));
86 $this->addRule('cms_name', 'Username is required', 'required');
6a488035
TO
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');
93
94 //add a rule to check username uniqueness
95 $this->addFormRule(array('CRM_Contact_Form_Task_Useradd', 'usernameRule'));
96
97 $this->addButtons(
98 array(
99 array(
100 'type' => 'next',
101 'name' => ts('Add'),
102 'isDefault' => TRUE,
103 ),
104 array(
105 'type' => 'cancel',
106 'name' => ts('Cancel'),
107 ),
108 )
109 );
110 $this->setDefaults($this->setDefaultValues());
111 }
112
113 /**
f12c6f7d 114 * Post process function.
6a488035
TO
115 */
116 public function postProcess() {
117 // store the submitted values in an array
118 $params = $this->exportValues();
119
120 CRM_Core_BAO_CMSUser::create($params, 'email');
121 CRM_Core_Session::setStatus('', ts('User Added'), 'success');
122 }
123
124 /**
fe482240 125 * Validation Rule.
ad37ac8e 126 *
127 * @param array $params
128 *
129 * @return array|bool
6a488035 130 */
00be9182 131 public static function usernameRule($params) {
6a488035
TO
132 $config = CRM_Core_Config::singleton();
133 $errors = array();
134 $check_params = array(
135 'name' => $params['cms_name'],
136 'mail' => $params['email'],
137 );
138 $config->userSystem->checkUserNameEmailExists($check_params, $errors);
139
140 return empty($errors) ? TRUE : $errors;
141 }
96025800 142
6a488035 143}