Commit f0a9c611a830de96bb0599fe75e842fb9cf80ecd

Authored by Brad Shuttleworth
1 parent 9058e6b7

migrate preferences to new forms code.


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@5820 c91229c3-7414-0410-bfa2-8a42b809f60b
preferences.php
@@ -29,116 +29,174 @@ require_once("config/dmsDefaults.php"); @@ -29,116 +29,174 @@ require_once("config/dmsDefaults.php");
29 require_once(KT_LIB_DIR . "/unitmanagement/Unit.inc"); 29 require_once(KT_LIB_DIR . "/unitmanagement/Unit.inc");
30 30
31 require_once(KT_LIB_DIR . "/templating/templating.inc.php"); 31 require_once(KT_LIB_DIR . "/templating/templating.inc.php");
32 -require_once(KT_LIB_DIR . "/templating/kt3template.inc.php");  
33 require_once(KT_LIB_DIR . "/dispatcher.inc.php"); 32 require_once(KT_LIB_DIR . "/dispatcher.inc.php");
34 -  
35 -require_once(KT_LIB_DIR . '/widgets/fieldWidgets.php'); 33 +require_once(KT_LIB_DIR . "/widgets/forms.inc.php");
36 34
37 class PreferencesDispatcher extends KTStandardDispatcher { 35 class PreferencesDispatcher extends KTStandardDispatcher {
38 var $sSection = 'preferences'; 36 var $sSection = 'preferences';
39 37
40 function check() { 38 function check() {
41 - $oConfig =& KTConfig::getSingleton();  
42 - if ($this->oUser->getId() == -2 ||  
43 - ($oConfig->get('user_prefs/restrictPreferences', false) && !Permission::userIsSystemAdministrator($this->oUser->getId()))) {  
44 - return false;  
45 - }  
46 -  
47 - return parent::check(); 39 + $oConfig =& KTConfig::getSingleton();
  40 + if ($this->oUser->getId() == -2 ||
  41 + ($oConfig->get('user_prefs/restrictPreferences', false) && !Permission::userIsSystemAdministrator($this->oUser->getId()))) {
  42 + return false;
  43 + }
  44 + $this->aBreadcrumbs = array(array('action' => 'preferences', 'name' => _kt('Preferences')));
  45 + return parent::check();
48 } 46 }
49 -  
50 - function PreferencesDispatcher() {  
51 - $this->aBreadcrumbs = array(  
52 - array('action' => 'preferences', 'name' => _kt('Preferences')),  
53 - );  
54 - return parent::KTStandardDispatcher(); 47 +
  48 + function form_main() {
  49 + $oForm = new KTForm;
  50 +
  51 + $oForm->setOptions(array(
  52 + 'context' => &$this,
  53 + 'identifier' => 'ktcore.preferences.main',
  54 + 'action' => 'updatePreferences',
  55 + 'fail_action' => 'main',
  56 + 'label' => _kt('Your Details'),
  57 + 'submit_label' => _kt('Update Preferences'),
  58 + 'extraargs' => $this->meldPersistQuery("","", true),
  59 + ));
  60 +
  61 + // widgets
  62 + $oForm->setWidgets(array(
  63 + array('ktcore.widgets.string', array(
  64 + 'label' => _kt('Name'),
  65 + 'description' => _kt('Your full name. This is shown in reports and listings. e.g. <strong>John Smith</strong>'),
  66 + 'required' => true,
  67 + 'name' => 'name',
  68 + 'value' => $this->oUser->getName(),
  69 + 'autocomplete' => false)),
  70 + array('ktcore.widgets.string', array(
  71 + 'label' => _kt('Email Address'),
  72 + 'description' => _kt('Your email address. Notifications and alerts are mailed to this address if <strong>email notifications</strong> is set below. e.g. <strong>jsmith@acme.com</strong>'),
  73 + 'required' => false,
  74 + 'name' => 'email_address',
  75 + 'value' => $this->oUser->getEmail(),
  76 + 'autocomplete' => false)),
  77 + array('ktcore.widgets.boolean', array(
  78 + 'label' => _kt('Email Notifications'),
  79 + 'description' => _kt('If this is specified then the you will receive certain notifications. If it is not set, then you will only see notifications on the <strong>Dashboard</strong>'),
  80 + 'required' => false,
  81 + 'name' => 'email_notifications',
  82 + 'value' => $this->oUser->getEmailNotification(),
  83 + 'autocomplete' => false)),
  84 + ));
  85 +
  86 + $oForm->setValidators(array(
  87 + array('ktcore.validators.string', array(
  88 + 'test' => 'name',
  89 + 'output' => 'name')),
  90 + array('ktcore.validators.emailaddress', array(
  91 + 'test' => 'email_address',
  92 + 'output' => 'email_address')),
  93 + array('ktcore.validators.boolean', array(
  94 + 'test' => 'email_notifications',
  95 + 'output' => 'email_notifications')),
  96 + ));
  97 +
  98 + return $oForm;
  99 +
  100 + }
  101 +
  102 + function form_password() {
  103 + $oForm = new KTForm;
  104 +
  105 + $oForm->setOptions(array(
  106 + 'context' => &$this,
  107 + 'identifier' => 'ktcore.preferences.password',
  108 + 'action' => 'updatePassword',
  109 + 'fail_action' => 'setPassword',
  110 + 'cancel_action' => 'main',
  111 + 'label' => _kt('Change your password'),
  112 + 'submit_label' => _kt('Set pasword'),
  113 + 'extraargs' => $this->meldPersistQuery("","", true),
  114 + ));
  115 +
  116 + // widgets
  117 + $oForm->setWidgets(array(
  118 + array('ktcore.widgets.password', array(
  119 + 'label' => _kt('Password'),
  120 + 'description' => _kt('Specify your new password.'),
  121 + 'confirm_description' => _kt('Confirm the new password you specified above.'),
  122 + 'confirm' => true,
  123 + 'required' => true,
  124 + 'name' => 'new_password',
  125 + 'autocomplete' => false)),
  126 + ));
  127 +
  128 +
  129 + $KTConfig =& KTConfig::getSingleton();
  130 + $minLength = ((int) $KTConfig->get('user_prefs/passwordLength', 6));
  131 +
  132 + $oForm->setValidators(array(
  133 + array('ktcore.validators.string', array(
  134 + 'test' => 'new_password',
  135 + 'min_length' => $minLength,
  136 + 'min_length_warning' => sprintf(_kt("Your password is too short - passwords must be at least %d characters long."), $minLength),
  137 + 'output' => 'password')),
  138 + ));
  139 +
  140 + return $oForm;
  141 +
55 } 142 }
56 143
57 function do_main() { 144 function do_main() {
58 - $this->oPage->setBreadcrumbDetails(_kt("Your Preferences"));  
59 - $this->oPage->title = _kt("Dashboard");  
60 -  
61 -  
62 - $oUser =& $this->oUser;  
63 -  
64 - $aOptions = array('autocomplete' => false);  
65 -  
66 - $edit_fields = array();  
67 - $edit_fields[] = new KTStringWidget(_kt('Name'), _kt('Your full name. This is shown in reports and listings. e.g. <strong>John Smith</strong>'), 'name', $oUser->getName(), $this->oPage, true, null, null, $aOptions);  
68 - $edit_fields[] = new KTStringWidget(_kt('Email Address'), _kt('Your email address. Notifications and alerts are mailed to this address if <strong>email notifications</strong> is set below. e.g. <strong>jsmith@acme.com</strong>'), 'email_address', $oUser->getEmail(), $this->oPage, false, null, null, $aOptions);  
69 - $edit_fields[] = new KTCheckboxWidget(_kt('Email Notifications'), _kt('If this is specified then the you will receive certain notifications. If it is not set, then you will only see notifications on the <strong>Dashboard</strong>'), 'email_notifications', $oUser->getEmailNotification(), $this->oPage, false, null, null, $aOptions);  
70 - $edit_fields[] = new KTStringWidget(_kt('Mobile Number'), _kt('Your mobile phone number. e.g. <strong>+27 99 999 9999</strong>'), 'mobile_number', $oUser->getMobile(), $this->oPage, false, null, null, $aOptions);  
71 -  
72 - $oTemplating =& KTTemplating::getSingleton();  
73 - $oTemplate = $oTemplating->loadTemplate("ktcore/principals/preferences"); 145 + $this->oPage->setBreadcrumbDetails(_kt("Your Preferences"));
  146 + $this->oPage->title = _kt("Dashboard");
  147 + $oUser =& $this->oUser;
  148 +
  149 + $oForm = $this->form_main();
  150 +
  151 + $oTemplating =& KTTemplating::getSingleton();
  152 + $oTemplate = $oTemplating->loadTemplate("ktcore/principals/preferences");
74 $iSourceId = $oUser->getAuthenticationSourceId(); 153 $iSourceId = $oUser->getAuthenticationSourceId();
75 $bChangePassword = true; 154 $bChangePassword = true;
76 if ($iSourceId) { 155 if ($iSourceId) {
77 $bChangePassword = false; 156 $bChangePassword = false;
78 } 157 }
79 - $aTemplateData = array( 158 + $aTemplateData = array(
80 "context" => $this, 159 "context" => $this,
81 - 'edit_fields' => $edit_fields, 160 + 'edit_form' => $oForm,
82 "show_password" => $bChangePassword, 161 "show_password" => $bChangePassword,
83 - );  
84 - return $oTemplate->render($aTemplateData); 162 + );
  163 + return $oTemplate->render($aTemplateData);
85 } 164 }
86 165
87 function do_setPassword() { 166 function do_setPassword() {
88 - $this->oPage->setBreadcrumbDetails(_kt("Your Password"));  
89 - $this->oPage->title = _kt("Dashboard");  
90 -  
91 -  
92 - $oUser =& $this->oUser;  
93 -  
94 - $aOptions = array('autocomplete' => false);  
95 -  
96 - $edit_fields = array();  
97 - $edit_fields[] = new KTPasswordWidget(_kt('Password'), _kt('Specify your new password.'), 'password', null, $this->oPage, true, null, null, $aOptions);  
98 - $edit_fields[] = new KTPasswordWidget(_kt('Confirm Password'), _kt('Confirm the password specified above.'), 'confirm_password', null, $this->oPage, true, null, null, $aOptions);  
99 -  
100 -  
101 - $oTemplating =& KTTemplating::getSingleton();  
102 - $oTemplate = $oTemplating->loadTemplate("ktcore/principals/password");  
103 - $aTemplateData = array( 167 + $this->oPage->setBreadcrumbDetails(_kt("Your Password"));
  168 + $this->oPage->title = _kt("Dashboard");
  169 +
  170 + $oForm = $this->form_password();
  171 +
  172 + $oTemplating =& KTTemplating::getSingleton();
  173 + $oTemplate = $oTemplating->loadTemplate("ktcore/principals/password");
  174 + $aTemplateData = array(
104 "context" => $this, 175 "context" => $this,
105 - 'edit_fields' => $edit_fields,  
106 - );  
107 - return $oTemplate->render($aTemplateData); 176 + 'form' => $oForm,
  177 + );
  178 + return $oTemplate->render($aTemplateData);
108 } 179 }
109 180
110 181
111 182
112 function do_updatePassword() { 183 function do_updatePassword() {
113 -  
114 - $password = KTUtil::arrayGet($_REQUEST, 'password');  
115 - $confirm_password = KTUtil::arrayGet($_REQUEST, 'confirm_password');  
116 -  
117 - if (empty($password)) {  
118 - $this->errorRedirectTo("setPassword", _kt("You must specify a password."));  
119 - } else if ($password !== $confirm_password) {  
120 - $this->errorRedirectTo("setPassword", _kt("The passwords you specified do not match.")); 184 + $oForm = $this->form_password();
  185 + $res = $oForm->validate();
  186 + if (!empty($res['errors'])) {
  187 + $oForm->handleError();
121 } 188 }
122 -  
123 - $KTConfig =& KTConfig::getSingleton();  
124 - $minLength = ((int) $KTConfig->get('user_prefs/passwordLength', 6));  
125 -  
126 - if (strlen($password) < $minLength) {  
127 - $this->errorRedirectTo("setPassword", sprintf(_kt("Your password is too short - passwords must be at least %d characters long."), $minLength));  
128 - }  
129 -  
130 - // FIXME more validation would be useful.  
131 - // validated and ready.. 189 + $res = $res['results'];
  190 +
132 $this->startTransaction(); 191 $this->startTransaction();
133 192
134 $oUser =& $this->oUser; 193 $oUser =& $this->oUser;
135 194
136 195
137 // FIXME this almost certainly has side-effects. do we _really_ want 196 // FIXME this almost certainly has side-effects. do we _really_ want
138 - $oUser->setPassword(md5($password)); // 197 + $oUser->setPassword(md5($res['password'])); //
139 198
140 $res = $oUser->update(); 199 $res = $oUser->update();
141 - //$res = $oUser->doLimitedUpdate(); // ignores a fix blacklist of items.  
142 200
143 201
144 if (PEAR::isError($res) || ($res == false)) { 202 if (PEAR::isError($res) || ($res == false)) {
@@ -155,32 +213,21 @@ class PreferencesDispatcher extends KTStandardDispatcher { @@ -155,32 +213,21 @@ class PreferencesDispatcher extends KTStandardDispatcher {
155 $aErrorOptions = array( 213 $aErrorOptions = array(
156 'redirect_to' => array('main'), 214 'redirect_to' => array('main'),
157 ); 215 );
158 -  
159 - $oUser =& $this->oUser;  
160 216
161 - $name = $this->oValidator->validateString(KTUtil::arrayGet($_REQUEST, 'name'),  
162 - KTUtil::meldOptions($aErrorOptions, array('message' => _kt('You must specify your name.'))));  
163 -  
164 - $email_address = KTUtil::arrayGet($_REQUEST, 'email_address');  
165 - if(strlen(trim($email_address))) {  
166 - $email_address = $this->oValidator->validateEmailAddress(  
167 - $email_address,  
168 - KTUtil::meldOptions($aErrorOptions,  
169 - array('message' => _kt('Invalid email address.')))  
170 - );  
171 - }  
172 -  
173 - $email_notifications = KTUtil::arrayGet($_REQUEST, 'email_notifications', false);  
174 - if ($email_notifications !== false) $email_notifications = true;  
175 - $mobile_number = KTUtil::arrayGet($_REQUEST, 'mobile_number'); 217 + $oForm = $this->form_main();
  218 + $res = $oForm->validate();
  219 + if (!empty($res['errors'])) {
  220 + $oForm->handleError();
  221 + }
176 222
  223 + $res = $res['results'];
177 224
178 $this->startTransaction(); 225 $this->startTransaction();
179 -  
180 - $oUser->setName($name);  
181 - $oUser->setEmail($email_address);  
182 - $oUser->setEmailNotification($email_notifications);  
183 - $oUser->setMobile($mobile_number); 226 + $oUser =& $this->oUser;
  227 + $oUser->setName($res['name']);
  228 + $oUser->setEmail($res['email_address']);
  229 + $oUser->setEmailNotification($res['email_notifications']);
  230 + $oUser->setMobile(null);
184 231
185 232
186 // old system used the very evil store.php. 233 // old system used the very evil store.php.
templates/ktcore/principals/password.smarty
@@ -2,20 +2,4 @@ @@ -2,20 +2,4 @@
2 2
3 <p class="descriptiveText">{i18n}You may change your password by entering it in the fields below. Your system administrator may have defined certain rules (such as minimum password length) that your password must abide by.{/i18n}</p> 3 <p class="descriptiveText">{i18n}You may change your password by entering it in the fields below. Your system administrator may have defined certain rules (such as minimum password length) that your password must abide by.{/i18n}</p>
4 4
5 -<form action="{$smarty.server.PHP_SELF}" method="POST">  
6 -<input type="hidden" name="action" value="updatePassword" />  
7 -  
8 -<fieldset>  
9 - <legend>{i18n}Your Details{/i18n}</legend>  
10 -  
11 - {foreach item=oWidget from=$edit_fields}  
12 - {$oWidget->render()}  
13 - {/foreach}  
14 -  
15 - <div class="form_actions">  
16 - <input type="submit" value="{i18n}Change your password{/i18n}" />  
17 -  
18 - </div>  
19 -</fieldset>  
20 -  
21 -</form> 5 +{$form->render()}
templates/ktcore/principals/preferences.smarty
@@ -2,23 +2,8 @@ @@ -2,23 +2,8 @@
2 2
3 <p class="descriptiveText">{i18n}You may change details about yourself by editing the entries below. Once you have completed the form, click on <strong>Update your details</strong>.{/i18n}</p> 3 <p class="descriptiveText">{i18n}You may change details about yourself by editing the entries below. Once you have completed the form, click on <strong>Update your details</strong>.{/i18n}</p>
4 4
5 -<form action="{$smarty.server.PHP_SELF}" method="POST">  
6 -<input type="hidden" name="action" value="updatePreferences" /> 5 +{$edit_form->render()}
7 6
8 -<fieldset>  
9 - <legend>{i18n}Your Details{/i18n}</legend>  
10 -  
11 - {foreach item=oWidget from=$edit_fields}  
12 - {$oWidget->render()}  
13 - {/foreach}  
14 -  
15 - <div class="form_actions">  
16 - <input type="submit" value="{i18n}Update your details{/i18n}" />  
17 - <!-- FIXME add CSS for secondary actions. -->  
18 {if $show_password} 7 {if $show_password}
19 <p><a href="{addQS}action=setPassword{/addQS}">{i18n}Change your password.{/i18n}</a></p> 8 <p><a href="{addQS}action=setPassword{/addQS}">{i18n}Change your password.{/i18n}</a></p>
20 {/if} 9 {/if}
21 - </div>  
22 -</fieldset>  
23 -  
24 -</form>