Merge pull request #8193 from colemanw/scardinius-issue-CRM-16911
[civicrm-core.git] / CRM / Contact / Form / Task / Useradd.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * This class generates form components generic to useradd.
30 */
31 class 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 /**
41 * Contact.display_name of contact for whom we are adding user
42 *
43 * @var int
44 */
45 public $_displayName;
46
47 /**
48 * Primary email of contact for whom we are adding user.
49 *
50 * @var int
51 */
52 public $_email;
53
54 public function preProcess() {
55 $params = $defaults = $ids = array();
56
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)));
63 }
64
65 /**
66 * Set default values for the form.
67 */
68 public function setDefaultValues() {
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 /**
80 * Build the form object.
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');
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 /**
114 * Post process function.
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 /**
125 * Validation Rule.
126 *
127 * @param array $params
128 *
129 * @return array|bool
130 */
131 public static function usernameRule($params) {
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 }
142
143 }