commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / CRM / Contact / Form / Task / Useradd.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 */
46 public $_displayName;
47
48 /**
49 * Primary email of contact for whom we are adding user.
50 *
51 * @var int
52 */
53 public $_email;
54
55 public function preProcess() {
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 /**
67 * Set default values for the form. Note that in edit/view mode
68 * the default values are retrieved from the database
69 *
70 *
71 * @return void
72 */
73 public function setDefaultValues() {
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 /**
85 * Build the form object.
86 *
87 * @return void
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');
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 /**
121 *
122 * @return void
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 */
136 public static function usernameRule($params) {
137 $config = CRM_Core_Config::singleton();
138 $errors = array();
139 $check_params = array(
140 'name' => $params['cms_name'],
141 'mail' => $params['email'],
142 );
143 $config->userSystem->checkUserNameEmailExists($check_params, $errors);
144
145 return empty($errors) ? TRUE : $errors;
146 }
147
148 }