INFRA-132 - Civi - PHPStorm cleanup
[civicrm-core.git] / CRM / Contact / Form / Task / Useradd.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 * This class generates form components generic to useradd
30 *
31 */
32class 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 /**
100fef9d 42 * Contact.display_name of contact for whom we are adding user
6a488035
TO
43 *
44 * @var int
6a488035
TO
45 */
46 public $_displayName;
47
48 /**
100fef9d 49 * Primary email of contact for whom we are adding user
6a488035
TO
50 *
51 * @var int
6a488035
TO
52 */
53 public $_email;
54
00be9182 55 public function preProcess() {
6a488035
TO
56 $params = $defaults = $ids = array();
57
58 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
59 $params['id'] = $params['contact_id'] = $this->_contactId;
60 $contact = CRM_Contact_BAO_Contact::retrieve($params, $defaults, $ids);
61 $this->_displayName = $contact->display_name;
62 $this->_email = $contact->email;
63 CRM_Utils_System::setTitle(ts('Create User Record for %1', array(1 => $this->_displayName)));
64 }
65
66 /**
c490a46a 67 * Set default values for the form. Note that in edit/view mode
6a488035
TO
68 * the default values are retrieved from the database
69 *
6a488035 70 *
355ba699 71 * @return void
6a488035 72 */
00be9182 73 public function setDefaultValues() {
6a488035
TO
74 $defaults = array();
75 $defaults['contactID'] = $this->_contactId;
76 $defaults['name'] = $this->_displayName;
77 if (!empty($this->_email)) {
78 $defaults['email'] = $this->_email[1]['email'];
79 }
80
81 return $defaults;
82 }
83
84 /**
c490a46a 85 * Build the form object
6a488035 86 *
355ba699 87 * @return void
6a488035
TO
88 */
89 public function buildQuickForm() {
90 $element = $this->add('text', 'name', ts('Full Name'), array('class' => 'huge'));
91 $element->freeze();
92 $this->add('text', 'cms_name', ts('Username'), array('class' => 'huge'));
93 $this->addRule('cms_name', 'Username is required', 'required');
6a488035
TO
94 $this->add('password', 'cms_pass', ts('Password'), array('class' => 'huge'));
95 $this->add('password', 'cms_confirm_pass', ts('Confirm Password'), array('class' => 'huge'));
96 $this->addRule('cms_pass', 'Password is required', 'required');
97 $this->addRule(array('cms_pass', 'cms_confirm_pass'), 'ERROR: Password mismatch', 'compare');
98 $this->add('text', 'email', ts('Email:'), array('class' => 'huge'))->freeze();
99 $this->add('hidden', 'contactID');
100
101 //add a rule to check username uniqueness
102 $this->addFormRule(array('CRM_Contact_Form_Task_Useradd', 'usernameRule'));
103
104 $this->addButtons(
105 array(
106 array(
107 'type' => 'next',
108 'name' => ts('Add'),
109 'isDefault' => TRUE,
110 ),
111 array(
112 'type' => 'cancel',
113 'name' => ts('Cancel'),
114 ),
115 )
116 );
117 $this->setDefaults($this->setDefaultValues());
118 }
119
120 /**
6a488035 121 *
355ba699 122 * @return void
6a488035
TO
123 */
124 public function postProcess() {
125 // store the submitted values in an array
126 $params = $this->exportValues();
127
128 CRM_Core_BAO_CMSUser::create($params, 'email');
129 CRM_Core_Session::setStatus('', ts('User Added'), 'success');
130 }
131
132 /**
133 * Validation Rule
134 *
135 * @static
136 */
00be9182 137 public static function usernameRule($params) {
6a488035
TO
138 $config = CRM_Core_Config::singleton();
139 $errors = array();
140 $check_params = array(
141 'name' => $params['cms_name'],
142 'mail' => $params['email'],
143 );
144 $config->userSystem->checkUserNameEmailExists($check_params, $errors);
145
146 return empty($errors) ? TRUE : $errors;
147 }
148}