1 //<script type="text/javascript">
5 * A generic Login Dialog..... - only one needed in theory!?!?
7 * Fires XComponent builder on success...
10 * username,password, lang = for login actions.
11 * check = 1 for periodic checking that sesion is valid.
12 * passwordRequest = email request password
13 * logout = 1 = to logout
15 * Affects: (this id="????" elements)
16 * loading (removed) (used to indicate application is loading)
17 * loading-mask (hides) (used to hide application when it's building loading)
23 * Myapp.login = Roo.Login({
37 * Ext.apply(_T, _T[lang]);
41 Roo.Login = function(cfg)
48 Roo.each(this.items, function(e) {
51 Roo.Document.on('ready', function() {
55 Roo.Login.superclass.constructor.call(this);
59 Roo.extend(Roo.Login, Roo.LayoutDialog, {
62 * @cfg {String} method
63 * Method used to query for login details.
69 * URL to query login data. - eg. baseURL + '/Login.php'
75 * The user data - if user.id < 0 then login will be bypassed. (used for inital setup situation.
81 onLoad : function() // called on page load...
85 if (Roo.get('loading')) { // clear any loading indicator..
86 Roo.get('loading').remove();
89 this.switchLang('en'); // set the language to english..
92 success: function(response, opts) { // check successfull...
94 var res = Pman.processResponse(response);
96 if (!res.success) { // error!
98 //console.log('call failure');
99 return this.failure(response,opts);
102 if (!res.data.id) { // id=0 == login failure.
103 return this.show(true);
107 //console.log(success);
108 this.fillAuth(res.data);
110 Roo.XComponent.build();
118 check: function(cfg) // called every so often to refresh cookie etc..
120 if (cfg.again) { // could be undefined..
127 if ( this.checkFails > 4) {
128 Roo.MessageBox.alert("Error",
129 "Error getting authentication status. - try reloading, or wait a while", function() {
130 _this.sending = false;
135 _this.check.defer(10000, _this, [ cfg ]); // check in 10 secs.
146 success: cfg.success || this.success,
147 failure : cfg.failure || this.failure,
157 window.onbeforeunload = function() { }; // false does not work for IE..
166 failure : function() {
167 Roo.MessageBox.alert("Error", "Error logging out. - continuing anyway.", function() {
168 document.location = baseURL + '?ts=' + Math.random();
172 success : function() {
173 Pman.Login.authUserId = -1;
174 Pman.Login.checkFails =0;
176 document.location = baseURL + '?ts=' + Math.random();
185 success : function(response, opts) // check successfull...
187 this.sending = false;
188 var res = Pman.processResponse(response);
190 return this.failure(response, opts);
192 if (!res.data || !res.data.id) {
193 return this.failure(response,opts);
196 this.fillAuth(res.data);
203 failure : function (response, opts) // called if login 'check' fails.. (causes re-check)
206 this.sending = false;
207 var res = Pman.processResponse(response);
209 if ( Pman.Login.checkFails > 2) {
211 Roo.MessageBox.alert("Error", res.errorMsg ? res.errorMsg :
212 "Error getting authentication status. - try reloading");
215 opts.callCfg.again = true;
216 this.check.defer(1000, this, [ opts.callCfg ]);
222 fillAuth: function(au) {
223 this.startAuthCheck();
224 this.authUserId = au.id;
226 this.lastChecked = new Date();
227 this.fireEvent('refreshed', au);
228 //Pman.Tab.FaxQueue.newMaxId(au.faxMax);
229 //Pman.Tab.FaxTab.setTitle(au.faxNumPending);
231 //this.switchLang(Roo.state.Manager.get('Pman.Login.lang', 'en'));
232 Roo.state.Manager.set( this.realm + 'lang' , au.lang);
233 this.switchLang(au.lang);
236 // open system... - -on setyp..
237 if (this.authUserId < 0) {
238 Roo.MessageBox.alert("Warning",
239 "This is an open system - please set up a admin user with a password.");
242 //Pman.onload(); // which should do nothing if it's a re-auth result...
247 startAuthCheck : function() // starter for timeout checking..
249 if (Pman.Login.intervalID) { // timer already in place...
253 Pman.Login.intervalID = window.setInterval(function() {
254 Pman.Login.check(false);
255 }, 120000); // every 120 secs = 2mins..
272 center: { // needed??
275 // tabPosition: 'top',
278 alwaysShowTabs: false
282 rendered : function(dlg) {
283 this.form = this.items[0];
284 this.form.dialog = dlg;
285 this.buttons[0].form = this.form;
286 this.buttons[0].dialog = dlg
287 this.buttons[1].form = this.form;
288 this.buttons[1].dialog = dlg;
290 // logoprefix comes from base config.
291 //Pman.Login.form.el.createChild({
293 // src: rootURL + '/Pman/'+appNameShort + '/templates/images/logo.gif',
294 // style: 'margin-bottom: 10px;'
303 //this.resizeToLogo.defer(1000,this);
304 //var sz = Roo.get(Pman.Login.form.el.query('img')[0]).getSize();
306 // this.resizeToLogo.defer(1000,this);
309 var w = Ext.lib.Dom.getViewWidth() - 100;
310 var h = Ext.lib.Dom.getViewHeight() - 100;
311 this.resizeTo(Math.max(350, Math.min(sz.width + 30, w)),Math.min(sz.height+200, h));
318 if (this.user.id < 0) { // used for inital setup situations.
322 if (this.intervalID) {
324 window.clearInterval(this.intervalID);
325 this.intervalID = false;
329 if (Roo.get('loading')) {
330 Roo.get('loading').remove();
332 if (Roo.get('loading-mask')) {
333 Roo.get('loading-mask').hide();
336 //incomming._node = tnode;
338 //this.dialog.modal = !modal;
339 //this.dialog.show();
340 this.dialog.el.unmask();
343 this.form.setValues({
344 'username' : Roo.state.Manager.get(this.realm + '.username', ''),
345 'lang' : Roo.state.Manager.get(this.realm + '.lang', 'en')
348 Pman.Login.switchLang(Roo.state.Manager.get(this.realm + '.lang', ''));
349 if (this.form.findField('username').getValue().length > 0 ){
350 this.form.findField('password').focus();
352 this.form.findField('username').focus();
360 xtype : 'ContentPanel',
371 style : 'margin : 10px;',
373 actionfailed : function(f, act) {
374 // form can return { errors: .... }
376 //act.result.errors // invalid form element list...
377 //act.result.errorMsg// invalid form element list...
379 this.dialog.el.unmask();
380 Roo.MessageBox.alert("Error", act.result.errorMsg ? act.result.errorMsg :
381 "Login failed - communication error - try again.");
384 actioncomplete: function(re, act) {
386 Roo.state.Manager.set(
387 this.dialog.realm + '.' + this.dialog.params.username,
388 this.form.findField(this.dialog.params.username).getValue()
390 Roo.state.Manager.set(
391 this.dialog.realm + '.lang',
392 Pman.Login.form.findField('lang').getValue()
395 this.dialog.fillAuth(act.result.data);
399 if (Roo.get('loading-mask')) {
400 Roo.get('loading-mask').show();
402 Roo.XComponent.build();
412 fieldLabel: "Email Address",
415 autoCreate : {tag: "input", type: "text", size: "20"}
420 fieldLabel: "Password",
421 inputType: 'password',
424 autoCreate : {tag: "input", type: "text", size: "20"},
426 specialkey : function(e,ev) {
427 if (ev.keyCode == 13) {
428 this.form.dialog.el.mask("Logging in");
429 this.form.doAction('submit', {
430 url: this.form.dialog.url,
431 method: this.form.dialog.method,
440 fieldLabel: "Language",
443 xtype : 'SimpleStore',
444 fields: ['lang', 'ldisp'],
447 [ 'zh_HK' , '\u7E41\u4E2D' ],
448 [ 'zh_CN', '\u7C21\u4E2D' ]
455 displayField:'ldisp',
459 triggerAction: 'all',
460 emptyText:'Select a Language...',
463 select : function(cb, rec, ix) {
464 this.form.switchLang(rec.data.lang);
478 text : "Forgot Password",
482 var n = this.form.findField('username').getValue();
484 Roo.MessageBox.alert("Error", "Fill in your email address");
492 method: this.dialog.method,
493 success: function(response, opts) { // check successfull...
495 var res = Pman.processResponse(response);
496 if (!res.success) { // error!
497 Roo.MessageBox.alert("Error" ,
498 res.errorMsg ? res.errorMsg : "Problem Requesting Password Reset");
501 Roo.MessageBox.alert("Notice" ,
502 "Please check you email for the Password Reset message");
504 failure : function() {
505 Roo.MessageBox.alert("Error" , "Problem Requesting Password Reset");
518 this.dialog.el.mask("Logging in");
519 this.form.doAction('submit', {
521 method: this.dialog.method
530 Pman.Login = new Roo.util.Observable({
543 authUser: { id : false },
553 intervalID : false, /// the login refresher...
564 switchLang : function (lang) {
568 if (typeof(_T.en) == 'undefined') {
570 Roo.apply(_T.en, _T);
573 if (typeof(_T[lang]) == 'undefined') {
574 Roo.MessageBox.alert("Sorry", "Language not available yet (" + lang +')');
579 Roo.apply(_T, _T[lang]);
580 // just need to set the text values for everything...
584 function formLabel(name, val) {
586 var lbl = Pman.Login.form.findField( name ).el.dom.parentNode.parentNode;
587 if (lbl.getElementsByTagName('label').length) {
588 lbl = lbl.getElementsByTagName('label')[0];
590 lbl = lbl.parentNode.getElementsByTagName('label')[0];
596 formLabel('password', "Password"+':');
597 formLabel('username', "Email Address"+':');
598 formLabel('lang', "Language"+':');
599 this.dialog.setTitle("Login");
600 this.dialog.buttons[0].setText("Forgot Password");
601 this.dialog.buttons[1].setText("Login");
607 inGroup : function(g)
609 return this.authUser && this.authUser.groups &&
610 this.authUser.groups.indexOf(g) > -1;
614 return this.authUser && this.authUser.company_id_comptype &&
615 this.authUser.company_id_comptype == 'OWNER';
619 * Depreciated = use Pman.I18n
622 i18nList: function (type, codes)
625 return Pman.I18n.listToNames(type, codes);
627 i18n: function(type, code)
629 return Pman.I18n.toName(type, code);