Merge branch 'phpunit-ob-fix' of https://github.com/giant-rabbit/civicrm-core into...
[civicrm-core.git] / CRM / Contact / Form / Task / Useradd.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 */
32 class CRM_Contact_Form_Task_Useradd extends CRM_Core_Form {
33
34 /**
35 * The contact id, used when adding user
36 *
37 * @var int
38 */
39 protected $_contactId;
40
41 /**
42 * Contact.display_name of contact for whom we are adding user
43 *
44 * @var int
45 * @public
46 */
47 public $_displayName;
48
49 /**
50 * Primary email of contact for whom we are adding user
51 *
52 * @var int
53 * @public
54 */
55 public $_email;
56
57 function preProcess() {
58 $params = $defaults = $ids = array();
59
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)));
66 }
67
68 /**
69 * Set default values for the form. Note that in edit/view mode
70 * the default values are retrieved from the database
71 *
72 * @access public
73 *
74 * @return void
75 */
76 function setDefaultValues() {
77 $defaults = array();
78 $defaults['contactID'] = $this->_contactId;
79 $defaults['name'] = $this->_displayName;
80 if (!empty($this->_email)) {
81 $defaults['email'] = $this->_email[1]['email'];
82 }
83
84 return $defaults;
85 }
86
87 /**
88 * Build the form object
89 *
90 * @return void
91 * @access public
92 */
93 public function buildQuickForm() {
94 $element = $this->add('text', 'name', ts('Full Name'), array('class' => 'huge'));
95 $element->freeze();
96 $this->add('text', 'cms_name', ts('Username'), array('class' => 'huge'));
97 $this->addRule('cms_name', 'Username is required', 'required');
98 $this->add('password', 'cms_pass', ts('Password'), array('class' => 'huge'));
99 $this->add('password', 'cms_confirm_pass', ts('Confirm Password'), array('class' => 'huge'));
100 $this->addRule('cms_pass', 'Password is required', 'required');
101 $this->addRule(array('cms_pass', 'cms_confirm_pass'), 'ERROR: Password mismatch', 'compare');
102 $this->add('text', 'email', ts('Email:'), array('class' => 'huge'))->freeze();
103 $this->add('hidden', 'contactID');
104
105 //add a rule to check username uniqueness
106 $this->addFormRule(array('CRM_Contact_Form_Task_Useradd', 'usernameRule'));
107
108 $this->addButtons(
109 array(
110 array(
111 'type' => 'next',
112 'name' => ts('Add'),
113 'isDefault' => TRUE,
114 ),
115 array(
116 'type' => 'cancel',
117 'name' => ts('Cancel'),
118 ),
119 )
120 );
121 $this->setDefaults($this->setDefaultValues());
122 }
123
124 /**
125 * @access public
126 *
127 * @return void
128 */
129 public function postProcess() {
130 // store the submitted values in an array
131 $params = $this->exportValues();
132
133 CRM_Core_BAO_CMSUser::create($params, 'email');
134 CRM_Core_Session::setStatus('', ts('User Added'), 'success');
135 }
136
137 /**
138 * Validation Rule
139 *
140 * @static
141 */
142 static function usernameRule($params) {
143 $config = CRM_Core_Config::singleton();
144 $errors = array();
145 $check_params = array(
146 'name' => $params['cms_name'],
147 'mail' => $params['email'],
148 );
149 $config->userSystem->checkUserNameEmailExists($check_params, $errors);
150
151 return empty($errors) ? TRUE : $errors;
152 }
153 }
154