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 = for periodic checking that sesion is valid.
16 * Myapp.login = Roo.Login({
30 * Ext.apply(_T, _T[lang]);
34 Roo.Login = function(cfg)
37 Roo.each(this.items, function(e) {
40 Roo.Document.on('ready', function() {
44 Roo.Login.superclass.constructor.call(this);
48 Roo.extend(Roo.Login, Roo.LayoutDialog, {
51 * @cfg {String} method
52 * Method used to query for login details.
58 * URL to query login data. - eg. baseURL + '/Login.php'
62 onLoad : function() // called on page load...
66 if (Roo.get('loading')) { // clear any loading indicator..
67 Roo.get('loading').remove();
70 this.switchLang('en'); // set the language to english..
73 success: function(response, opts) { // check successfull...
75 var res = Pman.processResponse(response);
77 if (!res.success) { // error!
79 //console.log('call failure');
80 return this.failure(response,opts);
83 if (!res.data.id) { // id=0 == login failure.
84 return this.show(true);
88 //console.log(success);
89 this.fillAuth(res.data);
91 Roo.XComponent.build();
99 check: function(cfg) // called every so often to refresh cookie etc..
101 if (cfg.again) { // could be undefined..
108 if ( this.checkFails > 4) {
109 Roo.MessageBox.alert("Error",
110 "Error getting authentication status. - try reloading, or wait a while", function() {
111 _this.sending = false;
116 _this.check.defer(10000, _this, [ cfg ]); // check in 10 secs.
127 success: cfg.success || this.success,
128 failure : cfg.failure || this.failure,
136 success : function(response, opts) // check successfull...
138 this.sending = false;
139 var res = Pman.processResponse(response);
141 return this.failure(response, opts);
143 if (!res.data || !res.data.id) {
144 return this.failure(response,opts);
147 this.fillAuth(res.data);
154 failure : function (response, opts) // called if login 'check' fails.. (causes re-check)
157 this.sending = false;
158 var res = Pman.processResponse(response);
160 if ( Pman.Login.checkFails > 2) {
162 Roo.MessageBox.alert("Error", res.errorMsg ? res.errorMsg :
163 "Error getting authentication status. - try reloading");
166 opts.callCfg.again = true;
167 this.check.defer(1000, this, [ opts.callCfg ]);
173 fillAuth: function(au) {
174 this.startAuthCheck();
175 this.authUserId = au.id;
177 this.lastChecked = new Date();
178 Pman.fireEvent('authrefreshed', au);
179 //Pman.Tab.FaxQueue.newMaxId(au.faxMax);
180 //Pman.Tab.FaxTab.setTitle(au.faxNumPending);
182 //this.switchLang(Roo.state.Manager.get('Pman.Login.lang', 'en'));
183 Roo.state.Manager.set('Pman.Login.lang.'+appNameShort, au.lang);
184 this.switchLang(au.lang);
187 // open system... - -on setyp..
188 if (this.authUserId < 0) {
189 Roo.MessageBox.alert("Warning",
190 "This is an open system - please set up a admin user with a password.");
193 //Pman.onload(); // which should do nothing if it's a re-auth result...
198 startAuthCheck : function() // starter for timeout checking..
200 if (Pman.Login.intervalID) { // timer already in place...
204 Pman.Login.intervalID = window.setInterval(function() {
205 Pman.Login.check(false);
206 }, 120000); // every 120 secs = 2mins..
226 // tabPosition: 'top',
229 alwaysShowTabs: false
232 rendered : function(dlg) {
233 this.items[0].dialog = dlg;
243 actionfailed : function(f, act) {
244 // form can return { errors: .... }
246 //act.result.errors // invalid form element list...
247 //act.result.errorMsg// invalid form element list...
249 this.dialog.el.unmask();
250 Roo.MessageBox.alert("Error", act.result.errorMsg ? act.result.errorMsg :
251 "Login failed - communication error - try again.");
254 actioncomplete: function(re, act) {
256 Roo.state.Manager.set(
257 this.dialog.realm + '.' + this.dialog.params.username,
258 this.form.findField(this.dialog.params.username).getValue()
260 Roo.state.Manager.set(
261 this.dialog.realm + '.lang',
262 Pman.Login.form.findField('lang').getValue()
265 this.dialog.fillAuth(act.result.data);
269 if (Roo.get('loading-mask')) {
270 Roo.get('loading-mask').show();
272 Roo.XComponent.build();
282 fieldLabel: "Email Address",
285 autoCreate : {tag: "input", type: "text", size: "20"}
290 fieldLabel: "Password",
291 inputType: 'password',
294 autoCreate : {tag: "input", type: "text", size: "20"},
296 specialkey : function(e,ev) {
297 if (ev.keyCode == 13) {
298 this.form.dialog.el.mask("Logging in");
299 this.form.doAction('submit', {
300 url: this.form.dialog.url,
301 method: this.form.dialog.method,
310 fieldLabel: "Language",
313 xtype : 'SimpleStore',
314 fields: ['lang', 'ldisp'],
317 [ 'zh_HK' , '\u7E41\u4E2D' ],
318 [ 'zh_CN', '\u7C21\u4E2D' ]
325 displayField:'ldisp',
329 triggerAction: 'all',
330 emptyText:'Select a Language...',
333 select : function(cb, rec, ix) {
334 this.form.switchLang(rec.data.lang);
346 Pman.Login = new Roo.util.Observable({
359 authUser: { id : false },
369 intervalID : false, /// the login refresher...
382 this.dialog = new Roo.LayoutDialog(Roo.get(document.body).createChild({tag:'div'}),
383 { // the real end set is here...
400 // tabPosition: 'top',
403 alwaysShowTabs: false
410 this.dialog.addButton("Forgot Password", function()
413 var n = _this.form.findField('username').getValue();
415 Roo.MessageBox.alert("Error", "Fill in your email address");
419 url: baseURL + '/Login.js',
424 success: function(response, opts) { // check successfull...
426 var res = Pman.processResponse(response);
427 if (!res.success) { // error!
428 Roo.MessageBox.alert("Error" , res.errorMsg ? res.errorMsg : "Problem Requesting Password Reset");
431 Roo.MessageBox.alert("Notice" , "Please check you email for the Password Reset message");
433 failure : function() {
434 Roo.MessageBox.alert("Error" , "Problem Requesting Password Reset");
440 this.dialog.addButton("Login", function()
442 Pman.Login.dialog.el.mask("Logging in");
443 Pman.Login.form.doAction('submit', {
444 url: baseURL + '/Login',
448 this.layout = this.dialog.getLayout();
449 this.layout.beginUpdate();
451 //layout.add('center', new Roo.ContentPanel('center', {title: 'The First Tab'}));
452 // generate some other tabs
453 this.form = new Roo.form.Form({
457 actionfailed : function(f, act) {
458 // form can return { errors: .... }
460 //act.result.errors // invalid form element list...
461 //act.result.errorMsg// invalid form element list...
463 Pman.Login.dialog.el.unmask();
464 Roo.MessageBox.alert("Error", act.result.errorMsg ? act.result.errorMsg :
465 "Login failed - communication error - try again.");
468 actioncomplete: function(re, act) {
470 Roo.state.Manager.set('Pman.Login.username.'+appNameShort, Pman.Login.form.findField('username').getValue() );
471 Roo.state.Manager.set('Pman.Login.lang.'+appNameShort, Pman.Login.form.findField('lang').getValue() );
472 Pman.Login.fillAuth(act.result.data);
474 Pman.Login.dialog.hide();
475 if (Roo.get('loading-mask')) {
476 //Roo.get('loading').show();
477 Roo.get('loading-mask').show();
501 var ef = this.dialog.getLayout().getEl().createChild({tag: 'div'});
502 ef.dom.style.margin = 10;
504 this.form.render(ef.dom);
505 // logoprefix comes from base config.
506 Pman.Login.form.el.createChild({
508 src: rootURL + '/Pman/'+appNameShort + '/templates/images/logo.gif',
509 style: 'margin-bottom: 10px;'
511 Pman.Login.form.el.dom.firstChild
514 var vp = this.dialog.getLayout().add('center', new Roo.ContentPanel(ef, {
516 //title: 'Org Details',
523 this.layout.endUpdate();
525 this.fireEvent('render', this);
532 resizeToLogo : function()
534 var sz = Roo.get(Pman.Login.form.el.query('img')[0]).getSize();
536 this.resizeToLogo.defer(1000,this);
539 var w = Ext.lib.Dom.getViewWidth() - 100;
540 var h = Ext.lib.Dom.getViewHeight() - 100;
541 Pman.Login.dialog.resizeTo(Math.max(350, Math.min(sz.width + 30, w)),Math.min(sz.height+200, h));
542 Pman.Login.dialog.center();
547 show: function (modal)
552 modal = modal || false;
553 if (Pman.Login.authUserId < 0) { // logout!?
557 if (Pman.Login.intervalID) {
559 window.clearInterval(Pman.Login.intervalID);
560 Pman.Login.intervalID = false;
567 if (Roo.get('loading')) {
568 Roo.get('loading').remove();
570 if (Roo.get('loading-mask')) {
571 Roo.get('loading-mask').hide();
574 //incomming._node = tnode;
576 this.dialog.modal = !modal;
578 this.dialog.el.unmask();
579 this.resizeToLogo.defer(1000,this);
582 this.form.setValues({
583 'username' : Roo.state.Manager.get('Pman.Login.username.'+appNameShort, ''),
584 'lang' : Roo.state.Manager.get('Pman.Login.lang.'+appNameShort, 'en')
586 Pman.Login.switchLang(Roo.state.Manager.get('Pman.Login.lang.'+appNameShort, ''));
587 if (this.form.findField('username').getValue().length > 0 ){
588 this.form.findField('password').focus();
590 this.form.findField('username').focus();
600 window.onbeforeunload = function() { }; // false does not work for IE..
601 Pman.Login.authUserId = -1;
603 url: baseURL + '/Login.html',
608 failure : function() {
609 Roo.MessageBox.alert("Error", "Error logging out. - continuing anyway.", function() {
610 document.location = baseURL + '?ts=' + Math.random();
614 success : function() {
615 Pman.Login.authUserId = -1;
616 Pman.Login.checkFails =0;
618 document.location = baseURL + '?ts=' + Math.random();
624 switchLang : function (lang) {
628 if (typeof(_T.en) == 'undefined') {
630 Roo.apply(_T.en, _T);
633 if (typeof(_T[lang]) == 'undefined') {
634 Roo.MessageBox.alert("Sorry", "Language not available yet (" + lang +')');
639 Roo.apply(_T, _T[lang]);
640 // just need to set the text values for everything...
644 function formLabel(name, val) {
646 var lbl = Pman.Login.form.findField( name ).el.dom.parentNode.parentNode;
647 if (lbl.getElementsByTagName('label').length) {
648 lbl = lbl.getElementsByTagName('label')[0];
650 lbl = lbl.parentNode.getElementsByTagName('label')[0];
656 formLabel('password', "Password"+':');
657 formLabel('username', "Email Address"+':');
658 formLabel('lang', "Language"+':');
659 this.dialog.setTitle("Login");
660 this.dialog.buttons[0].setText("Forgot Password");
661 this.dialog.buttons[1].setText("Login");
667 inGroup : function(g)
669 return this.authUser && this.authUser.groups &&
670 this.authUser.groups.indexOf(g) > -1;
674 return this.authUser && this.authUser.company_id_comptype &&
675 this.authUser.company_id_comptype == 'OWNER';
679 * Depreciated = use Pman.I18n
682 i18nList: function (type, codes)
685 return Pman.I18n.listToNames(type, codes);
687 i18n: function(type, code)
689 return Pman.I18n.toName(type, code);