Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | * This function sets the 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 None | |
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 | * Function to actually build the form | |
89 | * | |
90 | * @return None | |
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->addRule('cms_name', 'Enter a valid username', 'minlength', 2); | |
99 | $this->add('password', 'cms_pass', ts('Password'), array('class' => 'huge')); | |
100 | $this->add('password', 'cms_confirm_pass', ts('Confirm Password'), array('class' => 'huge')); | |
101 | $this->addRule('cms_pass', 'Password is required', 'required'); | |
102 | $this->addRule(array('cms_pass', 'cms_confirm_pass'), 'ERROR: Password mismatch', 'compare'); | |
103 | $this->add('text', 'email', ts('Email:'), array('class' => 'huge'))->freeze(); | |
104 | $this->add('hidden', 'contactID'); | |
105 | ||
106 | //add a rule to check username uniqueness | |
107 | $this->addFormRule(array('CRM_Contact_Form_Task_Useradd', 'usernameRule')); | |
108 | ||
109 | $this->addButtons( | |
110 | array( | |
111 | array( | |
112 | 'type' => 'next', | |
113 | 'name' => ts('Add'), | |
114 | 'isDefault' => TRUE, | |
115 | ), | |
116 | array( | |
117 | 'type' => 'cancel', | |
118 | 'name' => ts('Cancel'), | |
119 | ), | |
120 | ) | |
121 | ); | |
122 | $this->setDefaults($this->setDefaultValues()); | |
123 | } | |
124 | ||
125 | /** | |
126 | * | |
127 | * @access public | |
128 | * | |
129 | * @return None | |
130 | */ | |
131 | public function postProcess() { | |
132 | // store the submitted values in an array | |
133 | $params = $this->exportValues(); | |
134 | ||
135 | CRM_Core_BAO_CMSUser::create($params, 'email'); | |
136 | CRM_Core_Session::setStatus('', ts('User Added'), 'success'); | |
137 | } | |
138 | ||
139 | /** | |
140 | * Validation Rule | |
141 | * | |
142 | * @static | |
143 | */ | |
144 | static function usernameRule($params) { | |
145 | $config = CRM_Core_Config::singleton(); | |
146 | $errors = array(); | |
147 | $check_params = array( | |
148 | 'name' => $params['cms_name'], | |
149 | 'mail' => $params['email'], | |
150 | ); | |
151 | $config->userSystem->checkUserNameEmailExists($check_params, $errors); | |
152 | ||
153 | return empty($errors) ? TRUE : $errors; | |
154 | } | |
155 | } | |
156 |