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
16 * Myapp.login = Roo.Login({
30 * Ext.apply(_T, _T[lang]);
34 Roo.Login = function(cfg)
41 Roo.each(this.items, function(e) {
44 Roo.Document.on('ready', function() {
48 Roo.Login.superclass.constructor.call(this);
52 Roo.extend(Roo.Login, Roo.LayoutDialog, {
55 * @cfg {String} method
56 * Method used to query for login details.
62 * URL to query login data. - eg. baseURL + '/Login.php'
66 onLoad : function() // called on page load...
70 if (Roo.get('loading')) { // clear any loading indicator..
71 Roo.get('loading').remove();
74 this.switchLang('en'); // set the language to english..
77 success: function(response, opts) { // check successfull...
79 var res = Pman.processResponse(response);
81 if (!res.success) { // error!
83 //console.log('call failure');
84 return this.failure(response,opts);
87 if (!res.data.id) { // id=0 == login failure.
88 return this.show(true);
92 //console.log(success);
93 this.fillAuth(res.data);
95 Roo.XComponent.build();
103 check: function(cfg) // called every so often to refresh cookie etc..
105 if (cfg.again) { // could be undefined..
112 if ( this.checkFails > 4) {
113 Roo.MessageBox.alert("Error",
114 "Error getting authentication status. - try reloading, or wait a while", function() {
115 _this.sending = false;
120 _this.check.defer(10000, _this, [ cfg ]); // check in 10 secs.
131 success: cfg.success || this.success,
132 failure : cfg.failure || this.failure,
140 success : function(response, opts) // check successfull...
142 this.sending = false;
143 var res = Pman.processResponse(response);
145 return this.failure(response, opts);
147 if (!res.data || !res.data.id) {
148 return this.failure(response,opts);
151 this.fillAuth(res.data);
158 failure : function (response, opts) // called if login 'check' fails.. (causes re-check)
161 this.sending = false;
162 var res = Pman.processResponse(response);
164 if ( Pman.Login.checkFails > 2) {
166 Roo.MessageBox.alert("Error", res.errorMsg ? res.errorMsg :
167 "Error getting authentication status. - try reloading");
170 opts.callCfg.again = true;
171 this.check.defer(1000, this, [ opts.callCfg ]);
177 fillAuth: function(au) {
178 this.startAuthCheck();
179 this.authUserId = au.id;
181 this.lastChecked = new Date();
182 this.fireEvent('refreshed', au);
183 //Pman.Tab.FaxQueue.newMaxId(au.faxMax);
184 //Pman.Tab.FaxTab.setTitle(au.faxNumPending);
186 //this.switchLang(Roo.state.Manager.get('Pman.Login.lang', 'en'));
187 Roo.state.Manager.set( this.realm + 'lang' , au.lang);
188 this.switchLang(au.lang);
191 // open system... - -on setyp..
192 if (this.authUserId < 0) {
193 Roo.MessageBox.alert("Warning",
194 "This is an open system - please set up a admin user with a password.");
197 //Pman.onload(); // which should do nothing if it's a re-auth result...
202 startAuthCheck : function() // starter for timeout checking..
204 if (Pman.Login.intervalID) { // timer already in place...
208 Pman.Login.intervalID = window.setInterval(function() {
209 Pman.Login.check(false);
210 }, 120000); // every 120 secs = 2mins..
227 center: { // needed??
230 // tabPosition: 'top',
233 alwaysShowTabs: false
237 rendered : function(dlg) {
238 this.form = this.items[0];
239 this.form.dialog = dlg;
240 this.buttons[0].form = this.form;
241 this.buttons[0].dialog = dlg
242 this.buttons[1].form = this.form;
243 this.buttons[1].dialog = dlg
249 xtype : 'ContentPanel',
260 style : 'margin : 10px;',
262 actionfailed : function(f, act) {
263 // form can return { errors: .... }
265 //act.result.errors // invalid form element list...
266 //act.result.errorMsg// invalid form element list...
268 this.dialog.el.unmask();
269 Roo.MessageBox.alert("Error", act.result.errorMsg ? act.result.errorMsg :
270 "Login failed - communication error - try again.");
273 actioncomplete: function(re, act) {
275 Roo.state.Manager.set(
276 this.dialog.realm + '.' + this.dialog.params.username,
277 this.form.findField(this.dialog.params.username).getValue()
279 Roo.state.Manager.set(
280 this.dialog.realm + '.lang',
281 Pman.Login.form.findField('lang').getValue()
284 this.dialog.fillAuth(act.result.data);
288 if (Roo.get('loading-mask')) {
289 Roo.get('loading-mask').show();
291 Roo.XComponent.build();
301 fieldLabel: "Email Address",
304 autoCreate : {tag: "input", type: "text", size: "20"}
309 fieldLabel: "Password",
310 inputType: 'password',
313 autoCreate : {tag: "input", type: "text", size: "20"},
315 specialkey : function(e,ev) {
316 if (ev.keyCode == 13) {
317 this.form.dialog.el.mask("Logging in");
318 this.form.doAction('submit', {
319 url: this.form.dialog.url,
320 method: this.form.dialog.method,
329 fieldLabel: "Language",
332 xtype : 'SimpleStore',
333 fields: ['lang', 'ldisp'],
336 [ 'zh_HK' , '\u7E41\u4E2D' ],
337 [ 'zh_CN', '\u7C21\u4E2D' ]
344 displayField:'ldisp',
348 triggerAction: 'all',
349 emptyText:'Select a Language...',
352 select : function(cb, rec, ix) {
353 this.form.switchLang(rec.data.lang);
367 label : "Forgot Password",
371 var n = this.form.findField('username').getValue();
373 Roo.MessageBox.alert("Error", "Fill in your email address");
381 method: this.dialog.method,
382 success: function(response, opts) { // check successfull...
384 var res = Pman.processResponse(response);
385 if (!res.success) { // error!
386 Roo.MessageBox.alert("Error" ,
387 res.errorMsg ? res.errorMsg : "Problem Requesting Password Reset");
390 Roo.MessageBox.alert("Notice" ,
391 "Please check you email for the Password Reset message");
393 failure : function() {
394 Roo.MessageBox.alert("Error" , "Problem Requesting Password Reset");
407 this.dialog.el.mask("Logging in");
408 this.form.doAction('submit', {
410 method: this.dialog.method
417 Pman.Login = new Roo.util.Observable({
430 authUser: { id : false },
440 intervalID : false, /// the login refresher...
448 // logoprefix comes from base config.
449 Pman.Login.form.el.createChild({
451 src: rootURL + '/Pman/'+appNameShort + '/templates/images/logo.gif',
452 style: 'margin-bottom: 10px;'
454 Pman.Login.form.el.dom.firstChild
457 var vp = this.dialog.getLayout().add('center', new Roo.ContentPanel(ef, {
459 //title: 'Org Details',
466 this.layout.endUpdate();
468 this.fireEvent('render', this);
475 resizeToLogo : function()
477 var sz = Roo.get(Pman.Login.form.el.query('img')[0]).getSize();
479 this.resizeToLogo.defer(1000,this);
482 var w = Ext.lib.Dom.getViewWidth() - 100;
483 var h = Ext.lib.Dom.getViewHeight() - 100;
484 Pman.Login.dialog.resizeTo(Math.max(350, Math.min(sz.width + 30, w)),Math.min(sz.height+200, h));
485 Pman.Login.dialog.center();
490 show: function (modal)
495 modal = modal || false;
496 if (Pman.Login.authUserId < 0) { // logout!?
500 if (Pman.Login.intervalID) {
502 window.clearInterval(Pman.Login.intervalID);
503 Pman.Login.intervalID = false;
510 if (Roo.get('loading')) {
511 Roo.get('loading').remove();
513 if (Roo.get('loading-mask')) {
514 Roo.get('loading-mask').hide();
517 //incomming._node = tnode;
519 this.dialog.modal = !modal;
521 this.dialog.el.unmask();
522 this.resizeToLogo.defer(1000,this);
525 this.form.setValues({
526 'username' : Roo.state.Manager.get('Pman.Login.username.'+appNameShort, ''),
527 'lang' : Roo.state.Manager.get('Pman.Login.lang.'+appNameShort, 'en')
529 Pman.Login.switchLang(Roo.state.Manager.get('Pman.Login.lang.'+appNameShort, ''));
530 if (this.form.findField('username').getValue().length > 0 ){
531 this.form.findField('password').focus();
533 this.form.findField('username').focus();
543 window.onbeforeunload = function() { }; // false does not work for IE..
544 Pman.Login.authUserId = -1;
546 url: baseURL + '/Login.html',
551 failure : function() {
552 Roo.MessageBox.alert("Error", "Error logging out. - continuing anyway.", function() {
553 document.location = baseURL + '?ts=' + Math.random();
557 success : function() {
558 Pman.Login.authUserId = -1;
559 Pman.Login.checkFails =0;
561 document.location = baseURL + '?ts=' + Math.random();
567 switchLang : function (lang) {
571 if (typeof(_T.en) == 'undefined') {
573 Roo.apply(_T.en, _T);
576 if (typeof(_T[lang]) == 'undefined') {
577 Roo.MessageBox.alert("Sorry", "Language not available yet (" + lang +')');
582 Roo.apply(_T, _T[lang]);
583 // just need to set the text values for everything...
587 function formLabel(name, val) {
589 var lbl = Pman.Login.form.findField( name ).el.dom.parentNode.parentNode;
590 if (lbl.getElementsByTagName('label').length) {
591 lbl = lbl.getElementsByTagName('label')[0];
593 lbl = lbl.parentNode.getElementsByTagName('label')[0];
599 formLabel('password', "Password"+':');
600 formLabel('username', "Email Address"+':');
601 formLabel('lang', "Language"+':');
602 this.dialog.setTitle("Login");
603 this.dialog.buttons[0].setText("Forgot Password");
604 this.dialog.buttons[1].setText("Login");
610 inGroup : function(g)
612 return this.authUser && this.authUser.groups &&
613 this.authUser.groups.indexOf(g) > -1;
617 return this.authUser && this.authUser.company_id_comptype &&
618 this.authUser.company_id_comptype == 'OWNER';
622 * Depreciated = use Pman.I18n
625 i18nList: function (type, codes)
628 return Pman.I18n.listToNames(type, codes);
630 i18n: function(type, code)
632 return Pman.I18n.toName(type, code);