2012-02-23 07:05:32 +00:00
// ERPNext - web based ERP (http://erpnext.com)
// Copyright (C) 2012 Web Notes Technologies Pvt Ltd
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2011-06-08 09:07:15 +00:00
pscript [ 'onload_My Company' ] = function ( ) {
var wrapper = page _body . pages [ 'My Company' ] ;
// body
2011-07-14 08:54:16 +00:00
wrapper . className = 'layout_wrapper' ;
2011-06-08 09:07:15 +00:00
wrapper . head = new PageHeader ( wrapper , 'People' ) ;
wrapper . body = $a ( wrapper , 'div' , '' , { marginRight : '11px' , marginTop : '11px' } ) ;
wrapper . message = $a ( wrapper . body , 'div' ) ;
wrapper . tab = make _table ( wrapper . body , 1 , 2 , '100%' , [ '25%' , '75%' ] ) ;
$y ( wrapper . tab , { tableLayout : 'fixed' } )
pscript . myc _make _toolbar ( wrapper ) ;
pscript . myc _make _list ( wrapper ) ;
if ( pscript . is _erpnext _saas ) {
pscript . myc _show _erpnext _message ( ) ;
}
}
pscript . myc _make _toolbar = function ( wrapper ) {
if ( has _common ( user _roles , [ 'System Manager' , 'Administrator' ] ) ) {
wrapper . head . add _button ( 'Add User' , pscript . myc _add _user )
}
}
//
// Only for erpnext product - show max users allowed
//
pscript . myc _show _erpnext _message = function ( ) {
var callback = function ( r , rt ) {
if ( r . exc ) { msgprint ( r . exc ) ; return ; }
$a ( wrapper . message , 'div' , 'help_box' , '' , 'You have ' + r . message . enabled
+ ' users enabled out of ' + r . message . max _user
+ '. Go to <a href="javascript:pscript.go_to_account_settings()">Account Settings</a> to increase the number of users' ) ;
}
2011-06-14 08:05:31 +00:00
$c _page ( 'home' , 'my_company' , 'get_max_users' , '' , callback )
2011-06-08 09:07:15 +00:00
}
//
// Add user dialog and server call
//
pscript . myc _add _user = function ( ) {
2011-12-14 11:42:12 +00:00
var fields = [ {
2012-01-17 12:47:06 +00:00
fieldtype : 'Data' ,
fieldname : 'user' ,
reqd : 1 ,
label : 'Email Id of the user to add'
} ,
2011-12-14 11:42:12 +00:00
{
fieldtype : 'Data' ,
fieldname : 'first_name' ,
reqd : 1 ,
label : 'First Name'
} ,
{
fieldtype : 'Data' ,
fieldname : 'last_name' ,
label : 'Last Name'
} ,
{
fieldtype : 'Data' ,
fieldname : 'password' ,
reqd : 1 ,
label : 'Password'
2012-01-17 12:47:06 +00:00
} ,
{
fieldtype : 'Button' ,
label : 'Add' ,
fieldname : 'add'
2012-01-19 09:39:49 +00:00
} ] ;
2011-12-14 11:42:12 +00:00
2012-01-17 12:47:06 +00:00
fields . push ( ) ;
2011-12-14 11:42:12 +00:00
2011-06-08 09:07:15 +00:00
var d = new wn . widgets . Dialog ( {
title : 'Add User' ,
width : 400 ,
2011-12-14 11:42:12 +00:00
fields : fields
2011-06-08 09:07:15 +00:00
} ) ;
d . make ( ) ;
d . fields _dict . add . input . onclick = function ( ) {
v = d . get _values ( ) ;
if ( v ) {
d . fields _dict . add . input . set _working ( ) ;
2011-06-14 08:05:31 +00:00
$c _page ( 'home' , 'my_company' , 'add_user' , v , function ( r , rt ) {
2011-06-08 09:07:15 +00:00
if ( r . exc ) { msgprint ( r . exc ) ; return ; }
else {
d . hide ( ) ;
pscript . myc _refresh ( ) ;
}
} )
}
}
d . show ( ) ;
}
pscript . myc _refresh = function ( ) {
page _body . pages [ 'My Company' ] . member _list . lst . run ( ) ;
}
pscript . myc _make _list = function ( wrapper ) {
wrapper . member _list = new MemberList ( wrapper )
}
pscript . get _fullname = function ( uid ) {
if ( uid == 'Administrator' ) return uid ;
return page _body . pages [ 'My Company' ] . member _list . member _items [ uid ] . fullname ;
}
//=============================================
MemberList = function ( parent ) {
var me = this ;
this . profiles = { } ;
this . member _items = { } ;
this . role _objects = { } ;
this . cur _profile = null ;
this . list _wrapper = $a ( $td ( parent . tab , 0 , 0 ) , 'div' , '' , { marginLeft : '11px' } ) ;
2011-07-14 08:54:16 +00:00
var cell = $td ( parent . tab , 0 , 1 ) ;
$y ( cell , { border : '1px solid #aaa' } ) ;
cell . className = 'layout_wrapper' ;
this . profile _wrapper = $a ( cell , 'div' ) ;
2011-06-08 09:07:15 +00:00
this . no _user _selected = $a ( this . profile _wrapper , 'div' , 'help_box' , null , 'Please select a user to view profile' ) ;
this . make _search ( ) ;
if ( pscript . online _users ) {
this . make _list ( ) ;
} else {
2011-06-14 08:05:31 +00:00
$c _page ( 'home' , 'event_updates' , 'get_online_users' , '' , function ( r , rt ) {
2011-06-08 09:07:15 +00:00
pscript . online _users = r . message ;
me . make _list ( ) ;
} )
}
}
// ----------------------
MemberList . prototype . make _search = function ( ) {
var me = this ;
this . search _area = $a ( this . list _wrapper , 'div' , '' , { textAlign : 'center' , padding : '8px' } ) ;
this . search _inp = $a ( this . search _area , 'input' , '' , { fontSize : '14px' , width : '80%' } ) ;
this . search _inp . set _empty = function ( ) {
this . value = 'Search' ; $fg ( this , '#888' ) ;
}
this . search _inp . onfocus = function ( ) {
$fg ( this , '#000' ) ;
if ( this . value == 'Search' ) this . value = '' ;
}
this . search _inp . onchange = function ( ) {
if ( ! this . value ) this . set _empty ( ) ;
}
this . search _inp . set _empty ( ) ;
}
// ----------------------
MemberList . prototype . make _list = function ( ) {
var me = this ;
this . lst _area = $a ( this . list _wrapper , 'div' ) ;
2011-08-25 09:45:25 +00:00
this . lst = new wn . widgets . Listing ( {
parent : this . lst _area ,
as _dict : 1 ,
get _query : function ( ) {
var c1 = '' ;
if ( me . search _inp . value && me . search _inp . value != 'Search' ) {
var c1 = repl ( ' AND (first_name LIKE "%(txt)s" OR last_name LIKE "%(txt)s" OR name LIKE "%(txt)s")' , { txt : '%' + me . search _inp . value + '%' } ) ;
}
return repl ( " SELECT name , \
ifnull ( concat _ws ( ' ' , first _name , last _name ) , '' ) as full _name , \
gender , file _list , enabled \
FROM tabProfile \
WHERE docstatus != 2 \
AND name not in ( 'Guest' , 'Administrator' ) % ( cond ) s \
ORDER BY name asc " , { cond : c1 } ) ;
} ,
render _row : function ( parent , data ) {
me . member _items [ data . name ] = new MemberItem ( parent , data , me ) ;
2011-06-08 09:07:15 +00:00
}
2011-08-25 09:45:25 +00:00
} ) ;
2011-06-08 09:07:15 +00:00
this . lst . run ( ) ;
}
/ *
Create / show profile
* /
MemberList . prototype . show _profile = function ( uid , member _item ) {
$dh ( this . no _user _selected ) ;
// if not exists, create
if ( ! this . profiles [ uid ] ) {
if ( ! member _item ) member _item = this . member _items [ uid ] ;
this . profiles [ uid ] = new MemberProfile ( this . profile _wrapper , uid , member _item ) ;
}
// hide current
if ( this . cur _profile )
this . cur _profile . hide ( ) ;
// show this
this . profiles [ uid ] . show ( ) ;
this . cur _profile = this . profiles [ uid ] ;
}
// Member Item
// List item of all profiles
// on the left hand sidebar of the page
MemberItem = function ( parent , det , mlist ) {
var me = this ;
this . det = det ;
this . wrapper = $a ( parent , 'div' ) ;
2011-08-25 09:45:25 +00:00
this . enabled = det . enabled ;
2011-06-08 09:07:15 +00:00
this . tab = make _table ( this . wrapper , 1 , 2 , '100%' , [ '20%' , '70%' ] , { padding : '4px' , overflow : 'hidden' } ) ;
$y ( this . tab , { tableLayout : 'fixed' , borderCollapse : 'collapse' } )
this . is _online = function ( ) {
for ( var i = 0 ; i < pscript . online _users . length ; i ++ ) {
2011-08-25 09:45:25 +00:00
if ( det . name == pscript . online _users [ i ] [ 0 ] ) return true ;
2011-06-08 09:07:15 +00:00
}
}
this . refresh _name _link = function ( ) {
// online / offline
$fg ( this . name _link , '#00B' ) ;
if ( ! this . is _online ( ) )
$fg ( this . name _link , '#444' ) ;
if ( ! this . enabled )
$fg ( this . name _link , '#777' ) ;
}
this . set _image = function ( ) {
// image
this . img = $a ( $td ( this . tab , 0 , 0 ) , 'img' , '' , { width : '41px' } ) ;
2011-08-25 09:45:25 +00:00
set _user _img ( this . img , det . name , null ,
2012-01-20 10:02:18 +00:00
( det . file _list ? det . file _list . split ( NEWLINE ) [ 0 ] . split ( ',' ) [ 1 ] :
( 'no_img_' + ( det . gender == 'Female' ? 'f' : 'm' ) ) ) ) ;
2011-06-08 09:07:15 +00:00
}
// set other details like email id, name etc
this . set _details = function ( ) {
// name
2011-08-25 09:45:25 +00:00
this . fullname = det . full _name || det . name ;
2011-06-08 09:07:15 +00:00
var div = $a ( $td ( this . tab , 0 , 1 ) , 'div' , '' , { fontWeight : 'bold' , padding : '2px 0px' } ) ;
this . name _link = $a ( div , 'span' , 'link_type' ) ;
2011-07-14 08:54:16 +00:00
this . name _link . innerHTML = crop ( this . fullname , 15 ) ;
2011-06-08 09:07:15 +00:00
this . name _link . onclick = function ( ) {
2011-08-25 09:45:25 +00:00
mlist . show _profile ( me . det . name , me ) ;
2011-06-08 09:07:15 +00:00
}
// "you" tag
2011-08-25 09:45:25 +00:00
if ( user == det . name ) {
2011-06-08 09:07:15 +00:00
var span = $a ( div , 'span' , '' , { padding : '2px' , marginLeft : '3px' } ) ;
span . innerHTML = '(You)'
}
// email id
var div = $a ( $td ( this . tab , 0 , 1 ) , 'div' , '' , { color : '#777' , fontSize : '11px' } ) ;
2011-08-25 09:45:25 +00:00
div . innerHTML = det . name ;
2011-06-08 09:07:15 +00:00
// working img
var div = $a ( $td ( this . tab , 0 , 1 ) , 'div' ) ;
this . working _img = $a ( div , 'img' , '' , { display : 'none' } ) ;
2011-10-24 10:19:11 +00:00
this . working _img . src = 'lib/images/ui/button-load.gif' ;
2011-06-08 09:07:15 +00:00
this . refresh _name _link ( ) ;
}
this . select = function ( ) {
$ ( this . wrapper ) . addClass ( 'my-company-member-item-selected' ) ;
}
this . deselect = function ( ) {
$ ( this . wrapper ) . removeClass ( 'my-company-member-item-selected' ) ;
}
this . set _image ( ) ;
this . set _details ( ) ;
// show initial
2011-08-25 09:45:25 +00:00
if ( user == det . name ) me . name _link . onclick ( ) ;
2011-06-08 09:07:15 +00:00
}
//
// Member Profile
// shows profile with Photo and conversation
//
MemberProfile = function ( parent , uid , member _item ) {
this . parent = parent ;
this . uid = uid ;
this . member _item = member _item ;
var me = this ;
// make the UI
this . make = function ( ) {
this . wrapper = $a ( this . parent , 'div' , '' , { display : 'none' } ) ;
this . tab = make _table ( this . wrapper , 3 , 2 , '100%' , [ '120px' , null ] , { padding : '3px' } ) ;
$y ( this . tab , { tableLayout : 'fixed' } ) ;
this . make _image _and _bio ( ) ;
this . make _toolbar ( ) ;
this . make _message _list ( ) ;
}
// create elements
this . make _image _and _bio = function ( ) {
var rh = $td ( this . tab , 0 , 1 ) ;
// image
this . img = $a ( $td ( this . tab , 0 , 0 ) , 'img' , '' , { width : '80px' , marginLeft : '10px' } ) ;
set _user _img ( this . img , this . uid ) ;
// details
this . name _area = $a ( rh , 'div' , 'my-company-name-head' ) ;
var div = $a ( rh , 'div' , 'my-company-email' ) ;
this . email _area = $a ( div , 'span' ) ;
this . online _status _area = $a ( div , 'span' , 'my-company-online-status' ) ;
this . bio _area = $a ( rh , 'div' , 'my-company-bio' ) ;
this . toolbar _area = $a ( rh , 'div' , 'my-company-toolbar' ) ;
this . status _span = $a ( this . toolbar _area , 'span' , '' , { marginRight : '7px' } ) ;
}
// the toolbar
this . make _toolbar = function ( ) {
if ( has _common ( [ 'Administrator' , 'System Manager' ] , user _roles ) ) {
var roles _btn = $btn ( this . toolbar _area , 'Set Roles' , function ( ) { me . show _roles ( ) } , { marginRight : '3px' } ) ;
var delete _btn = $btn ( this . toolbar _area , 'Delete User' , function ( ) { me . delete _user ( ) ; } , { marginRight : '3px' } ) ;
2011-08-25 09:45:25 +00:00
var ip _btn = $btn ( this . toolbar _area , 'Securty Settings' , function ( ) { me . set _security ( ) ; } , { marginRight : '3px' } ) ;
2011-06-08 09:07:15 +00:00
}
}
// create the role object
this . show _roles = function ( ) {
if ( ! this . role _object )
this . role _object = new RoleObj ( this . uid ) ;
this . role _object . dialog . show ( ) ;
}
2011-08-25 09:45:25 +00:00
// show securty settings
this . set _security = function ( ) {
2011-09-09 08:11:36 +00:00
var sd = new wn . widgets . Dialog ( {
2011-08-25 09:45:25 +00:00
title : 'Set User Security' ,
width : 500 ,
fields : [
{
label : 'IP Address' ,
description : ' Restrict user login by IP address , partial ips ( 111.111 . 111 ) , \
multiple addresses ( separated by commas ) allowed ' ,
fieldname : 'restrict_ip' ,
fieldtype : 'Data'
} ,
{
label : 'Login After' ,
description : 'User can only login after this hour (0-24)' ,
fieldtype : 'Int' ,
fieldname : 'login_after'
} ,
{
label : 'Login Before' ,
description : 'User can only login before this hour (0-24)' ,
fieldtype : 'Int' ,
fieldname : 'login_before'
} ,
{
label : 'New Password' ,
description : 'Update the current user password' ,
fieldtype : 'Data' ,
fieldname : 'new_password'
} ,
{
label : 'Update' ,
fieldtype : 'Button' ,
fieldname : 'update'
}
]
} ) ;
2011-09-09 08:11:36 +00:00
me . sec _dialog = sd
sd . onshow = function ( ) {
me . sec _dialog . set _values ( {
2011-08-25 09:45:25 +00:00
restrict _ip : me . profile . restrict _ip || '' ,
login _before : me . profile . login _before || '' ,
login _after : me . profile . login _after || '' ,
new _password : ''
2011-09-09 08:11:36 +00:00
} ) ;
} ;
sd . fields _dict . update . input . onclick = function ( ) {
2011-08-25 09:45:25 +00:00
var btn = this ;
this . set _working ( ) ;
2011-09-09 08:11:36 +00:00
var args = me . sec _dialog . get _values ( ) ;
2011-08-25 09:45:25 +00:00
args . user = me . profile . name ;
2011-09-09 08:11:36 +00:00
if ( args . new _password ) {
var pass _d = new wn . widgets . Dialog ( {
title : 'Your Password' ,
width : 300 ,
fields : [
{
2012-01-12 09:20:51 +00:00
label : 'Please Enter <b style="color: black">Your Password</b>' ,
2011-09-09 08:11:36 +00:00
description : "Your password is required to update the concerned user's password" ,
fieldtype : 'Password' ,
fieldname : 'sys_admin_pwd' ,
reqd : 1
} ,
{
label : 'Continue' ,
fieldtype : 'Button' ,
fieldname : 'continue'
}
]
} ) ;
pass _d . fields _dict . continue . input . onclick = function ( ) {
btn . pwd _dialog . hide ( ) ;
args . sys _admin _pwd = btn . pwd _dialog . get _values ( ) . sys _admin _pwd ;
btn . set _working ( ) ;
me . update _security ( args ) ;
2011-08-25 09:45:25 +00:00
btn . done _working ( ) ;
}
2011-09-09 08:11:36 +00:00
pass _d . show ( ) ;
btn . pwd _dialog = pass _d ;
btn . done _working ( ) ;
} else {
btn . done _working ( ) ;
me . update _security ( args ) ;
}
} ;
sd . show ( ) ;
}
this . update _security = function ( args ) {
$c _page ( 'home' , 'my_company' , 'update_security' , JSON . stringify ( args ) , function ( r , rt ) {
if ( r . exc ) {
msgprint ( r . exc ) ;
return ;
}
me . sec _dialog . hide ( ) ;
$ . extend ( me . profile , me . sec _dialog . get _values ( ) ) ;
} ) ;
2011-08-25 09:45:25 +00:00
}
2011-06-08 09:07:15 +00:00
// delete user
// create a confirm dialog and call server method
this . delete _user = function ( ) {
2012-01-24 09:03:21 +00:00
var cp = wn . control _panel ;
2011-06-08 09:07:15 +00:00
var d = new Dialog ( 400 , 200 , 'Delete User' ) ;
d . make _body ( [
[ 'HTML' , '' , 'Do you really want to remove ' + this . uid + ' from system?' ] , [ 'Button' , 'Delete' ]
] ) ;
d . onshow = function ( ) {
this . clear _inputs ( ) ;
}
d . widgets [ 'Delete' ] . onclick = function ( ) {
this . set _working ( ) ;
var callback = function ( r , rt ) {
d . hide ( ) ;
if ( r . exc ) {
msgprint ( r . exc ) ;
return ;
}
pscript . myc _refresh ( )
msgprint ( "User Deleted Successfully" ) ;
}
2011-06-14 08:05:31 +00:00
$c _page ( 'home' , 'my_company' , 'delete_user' , { 'user' : me . uid } , callback ) ;
2011-06-08 09:07:15 +00:00
}
d . show ( ) ;
}
// set enabled
this . set _enable _button = function ( ) {
var me = this ;
var act = this . profile . enabled ? 'Disable' : 'Enable' ;
if ( this . status _button ) {
this . status _button . innerHTML = act ;
} else {
// make the button
this . status _button = $btn ( this . toolbar _area , act , function ( ) {
var callback = function ( r , rt ) {
locals [ 'Profile' ] [ me . profile . name ] . enabled = cint ( r . message ) ;
me . status _button . done _working ( ) ;
me . refresh _enable _disable ( ) ;
}
this . set _working ( ) ;
2011-06-14 08:05:31 +00:00
$c _page ( 'home' , 'my_company' , this . innerHTML . toLowerCase ( ) + '_profile' , me . profile . name , callback ) ;
2011-06-08 09:07:15 +00:00
} , null , null , 1 ) ;
}
if ( this . uid == user ) $dh ( this . status _button ) ; else $di ( this . status _button ) ;
}
// render the details of the user from Profile
this . render = function ( ) {
this . profile = locals [ 'Profile' ] [ uid ] ;
scroll ( 0 , 0 ) ;
// name
if ( cstr ( this . profile . first _name ) || cstr ( this . profile . last _name ) ) {
this . fullname = cstr ( this . profile . first _name ) + ' ' + cstr ( this . profile . last _name ) ;
} else {
this . fullname = this . profile . name ;
}
this . name _area . innerHTML = this . fullname ;
// email
this . email _area . innerHTML = this . profile . name ;
// online / offline
this . online _status _area . innerHTML = ( this . member _item . is _online ( ) ? '(Online)' : '(Offline)' )
if ( this . member _item . is _online ( ) ) {
$y ( this . online _status _area , { color : 'green' } ) ;
}
// refresh enable / disabled
this . refresh _enable _disable ( ) ;
// designation
this . bio _area . innerHTML = this . profile . designation ? ( 'Designation: ' + cstr ( this . profile . designation ) + '<br>' ) : '' ;
this . bio _area . innerHTML += this . profile . bio ? this . profile . bio : 'No bio' ;
new MemberConversation ( this . wrapper , this . profile . name , this . fullname ) ;
}
// refresh enable / disable
this . refresh _enable _disable = function ( ) {
this . profile = locals [ 'Profile' ] [ this . uid ]
if ( ! this . profile . enabled ) {
$fg ( this . name _area , '#999' ) ;
} else {
$fg ( this . name _area , '#000' ) ;
}
this . member _item . enabled = this . profile . enabled ;
this . member _item . refresh _name _link ( ) ;
this . status _span . innerHTML = this . profile . enabled ? 'Enabled' : 'Disabled' ;
// set styles and buttons
if ( has _common ( [ 'Administrator' , 'System Manager' ] , user _roles ) ) {
this . set _enable _button ( ) ;
}
}
// Load user profile (if not loaded)
this . load = function ( ) {
if ( locals [ 'Profile' ] && locals [ 'Profile' ] [ uid ] ) {
this . render ( ) ;
return ;
}
var callback = function ( r , rt ) {
$dh ( me . member _item . working _img ) ;
$ds ( me . wrapper ) ;
me . loading = 0 ;
me . render ( ) ;
}
$ds ( this . member _item . working _img ) ;
$dh ( this . wrapper ) ;
this . loading = 1 ;
2012-01-23 10:48:34 +00:00
$c ( 'webnotes.widgets.form.load.getdoc' , { 'name' : this . uid , 'doctype' : 'Profile' , 'user' : user } , callback ) ; // onload
2011-06-08 09:07:15 +00:00
}
// show / hide
this . show = function ( ) {
if ( ! this . loading ) $ds ( this . wrapper ) ;
// select profile
this . member _item . select ( ) ;
}
this . hide = function ( ) {
$dh ( this . wrapper ) ;
// select profile
this . member _item . deselect ( ) ;
}
this . make _message _list = function ( ) {
}
this . make ( ) ;
this . load ( ) ;
}
// Member conversation
// Between the current user and the displayed profile
// or if same, then the conversation with all other
// profiles
MemberConversation = function ( parent , uid , fullname ) {
var me = this ;
this . wrapper = $a ( parent , 'div' , 'my-company-conversation' ) ;
this . fullname = fullname ;
this . make = function ( ) {
if ( user != uid ) {
this . make _input ( ) ;
}
this . make _list ( ) ;
// set all messages
// as "read" (docstatus = 0)
if ( user == uid ) {
2011-06-14 08:05:31 +00:00
$c _page ( 'home' , 'my_company' , 'set_read_all_messages' , '' , function ( r , rt ) { } ) ;
2011-06-08 09:07:15 +00:00
}
}
this . make _input = function ( ) {
this . input _wrapper = $a ( this . wrapper , 'div' , 'my-company-input-wrapper' ) ;
var tab = make _table ( this . input _wrapper , 1 , 2 , '100%' , [ '64%' , '36%' ] , { padding : '3px' } )
this . input = $a ( $td ( tab , 0 , 0 ) , 'textarea' ) ;
// button
var div = $a ( this . input _wrapper , 'div' ) ;
this . post = $btn ( div , 'Post' . bold ( ) , function ( ) { me . post _message ( ) ; } , { margin : '0px 13px 0px 3px' } )
2012-01-02 10:30:15 +00:00
this . post . disabled = true ;
2011-06-08 09:07:15 +00:00
this . input . onkeyup = this . input . onchange = function ( ) {
if ( this . value ) {
2012-01-02 10:30:15 +00:00
me . post . disabled = false ;
2011-06-08 09:07:15 +00:00
} else {
2012-01-02 10:30:15 +00:00
me . post . disabled = true ;
2011-06-08 09:07:15 +00:00
}
}
// notification check
this . notify _check = $a _input ( div , 'checkbox' , null ) ;
$a ( div , 'span' , '' , { marginLeft : '3px' } , 'Notify ' + fullname + ' by email' )
}
this . post _message = function ( ) {
if ( me . input . value == $ ( me . input ) . attr ( 'default_text' ) ) {
msgprint ( 'Please write a message first!' ) ; return ;
}
this . post . set _working ( ) ;
2011-06-14 08:05:31 +00:00
$c _page ( 'home' , 'my_company' , 'post_comment' , {
2011-06-08 09:07:15 +00:00
uid : uid ,
comment : $ ( me . input ) . val ( ) ,
notify : me . notify _check . checked ? 1 : 0
} , function ( r , rt ) {
$ ( me . input ) . val ( "" ) . blur ( ) ;
me . post . done _working ( ) ;
if ( r . exc ) { msgprint ( r . exc ) ; return ; }
me . notify _check . checked = false ;
me . refresh ( ) ;
} )
}
this . make _list = function ( ) {
this . lst _area = $a ( this . wrapper , 'div' , 'my-company-conversation' , { padding : '7px 13px' } ) ;
if ( user == uid ) {
this . my _messages _box = $a ( this . lst _area , 'div' , 'my-company-conversation-head' , { marginBottom : '7px' } , 'Messages by everyone to me<br>To send a message, click on the user on the left' )
}
this . lst = new wn . widgets . Listing ( {
parent : this . lst _area ,
2011-08-25 09:45:25 +00:00
as _dict : 1 ,
2011-06-08 09:07:15 +00:00
no _result _message : ( user == uid
? 'No messages by anyone yet'
: 'No messages yet. To start a conversation post a new message' ) ,
get _query : function ( ) {
if ( uid == user ) {
return repl ( "SELECT comment, owner, comment_docname, creation, docstatus " +
"FROM `tabComment Widget Record` " +
"WHERE comment_doctype='My Company' " +
"AND comment_docname='%(user)s' " +
"ORDER BY creation DESC " , { user : user } ) ;
} else {
return repl ( "SELECT comment, owner, comment_docname, creation, docstatus " +
"FROM `tabComment Widget Record` " +
"WHERE comment_doctype='My Company' " +
"AND ((owner='%(user)s' AND comment_docname='%(uid)s') " +
"OR (owner='%(uid)s' AND comment_docname='%(user)s')) " +
"ORDER BY creation DESC " , { uid : uid , user : user } ) ;
}
} ,
render _row : function ( parent , data ) {
new MemberCoversationComment ( parent , data , me ) ;
} ,
} )
this . refresh ( ) ;
}
this . refresh = function ( ) {
me . lst . run ( )
}
this . make ( ) ;
}
MemberCoversationComment = function ( cell , det , conv ) {
var me = this ;
this . det = det ;
this . wrapper = $a ( cell , 'div' , 'my-company-comment-wrapper' ) ;
this . comment = $a ( this . wrapper , 'div' , 'my-company-comment' ) ;
2011-08-25 09:45:25 +00:00
this . user = $a ( this . comment , 'span' , 'link_type' , { fontWeight : 'bold' } , pscript . get _fullname ( det . owner ) ) ;
2011-06-08 09:07:15 +00:00
this . user . onclick = function ( ) {
2011-08-25 09:45:25 +00:00
page _body . pages [ 'My Company' ] . member _list . show _profile ( me . det . owner ) ;
2011-06-08 09:07:15 +00:00
}
2011-08-25 09:45:25 +00:00
var st = ( ! det . docstatus ? { fontWeight : 'bold' } : null ) ;
2011-08-30 08:47:33 +00:00
this . msg = $a ( this . comment , 'span' , 'social' , st , ': ' + det . comment ) ;
2011-06-08 09:07:15 +00:00
2011-08-25 09:45:25 +00:00
if ( det . full _name == user ) {
2011-06-08 09:07:15 +00:00
$y ( this . wrapper , { backgroundColor : '#D9D9F3' } ) ;
}
2011-08-25 09:45:25 +00:00
this . timestamp = $a ( this . wrapper , 'div' , 'my-company-timestamp' , '' , comment _when ( det . creation ) ) ;
2011-06-08 09:07:15 +00:00
}
// ========================== Role object =====================================
pscript . all _roles = null ;
RoleObj = function ( profile _id ) {
this . roles _dict = { } ;
this . profile _id = profile _id ;
this . setup _done = 0 ;
var d = new Dialog ( 500 , 500 , 'Assign Roles' ) ;
d . make _body ( [
[ 'HTML' , 'roles' ]
] ) ;
this . dialog = d ;
this . make _role _body ( profile _id ) ;
this . make _help _body ( ) ;
2011-10-24 10:19:11 +00:00
this . body . innerHTML = '<span style="color:#888">Loading...</span> <img src="lib/images/ui/button-load.gif">'
2011-06-08 09:07:15 +00:00
var me = this ;
d . onshow = function ( ) {
if ( ! me . setup _done )
me . get _all _roles ( me . profile _id ) ;
}
}
// make role body
RoleObj . prototype . make _role _body = function ( id ) {
var me = this ;
var d = this . dialog ;
this . role _div = $a ( d . widgets [ 'roles' ] , 'div' ) ;
this . head = $a ( this . role _div , 'div' , '' , { marginLeft : '4px' , marginBottom : '4px' , fontWeight : 'bold' } ) ;
this . body = $a ( this . role _div , 'div' ) ;
this . footer = $a ( this . role _div , 'div' ) ;
this . update _btn = $btn ( this . footer , 'Update' , function ( ) { me . update _roles ( me . profile _id ) ; } , { marginRight : '4px' } , '' , 1 ) ;
}
// make help body
RoleObj . prototype . make _help _body = function ( ) {
var me = this ;
var d = this . dialog ;
this . help _div = $a ( d . widgets [ 'roles' ] , 'div' ) ;
var head = $a ( this . help _div , 'div' ) ; this . help _div . head = head ;
var body = $a ( this . help _div , 'div' ) ; this . help _div . body = body ;
var tail = $a ( this . help _div , 'div' ) ; this . help _div . tail = tail ;
var back _btn = $btn ( tail , 'Back' , function ( ) {
// back to assign roles
$ ( me . role _div ) . slideToggle ( 'medium' ) ;
$ ( me . help _div ) . slideToggle ( 'medium' ) ;
} ) ;
this . help _div . back _btn = back _btn ;
$dh ( this . help _div ) ;
}
// get all roles
RoleObj . prototype . get _all _roles = function ( id ) {
if ( pscript . all _roles ) {
this . make _roles ( id ) ;
return ;
}
var me = this ;
var callback = function ( r , rt ) {
pscript . all _roles = r . message ;
me . make _roles ( id ) ;
}
$c _obj ( 'Company Control' , 'get_all_roles' , '' , callback ) ;
}
// make roles
RoleObj . prototype . make _roles = function ( id ) {
var me = this ;
var list = pscript . all _roles ;
me . setup _done = 1 ;
me . body . innerHTML = '' ;
var tbl = make _table ( me . body , cint ( list . length / 2 ) + 1 , 4 , '100%' , [ '5%' , '45%' , '5%' , '45%' ] , { padding : '4px' } ) ;
var in _right = 0 ; var ridx = 0 ;
for ( i = 0 ; i < list . length ; i ++ ) {
var cidx = in _right * 2 ;
me . make _checkbox ( tbl , ridx , cidx , list [ i ] ) ;
me . make _label ( tbl , ridx , cidx + 1 , list [ i ] ) ;
// change column
if ( in _right ) { in _right = 0 ; ridx ++ } else in _right = 1 ;
}
me . get _user _roles ( id ) ;
}
// make checkbox
RoleObj . prototype . make _checkbox = function ( tbl , ridx , cidx , role ) {
var me = this ;
var a = $a _input ( $a ( $td ( tbl , ridx , cidx ) , 'div' ) , 'checkbox' ) ;
a . role = role ;
me . roles _dict [ role ] = a ;
$y ( a , { width : '20px' } ) ;
$y ( $td ( tbl , ridx , cidx ) , { textAlign : 'right' } ) ;
}
// make label
RoleObj . prototype . make _label = function ( tbl , ridx , cidx , role ) {
var me = this ;
var t = make _table ( $td ( tbl , ridx , cidx ) , 1 , 2 , null , [ '16px' , null ] , { marginRight : '5px' } ) ;
var ic = $a ( $td ( t , 0 , 0 ) , 'img' , '' , { cursor : 'pointer' , marginRight : '5px' } ) ;
2012-02-06 10:58:06 +00:00
ic . src = 'lib/images/icons/help.png' ;
2011-06-08 09:07:15 +00:00
ic . role = role ;
ic . onclick = function ( ) {
me . get _permissions ( this . role ) ;
}
$td ( t , 0 , 1 ) . innerHTML = role ;
}
// get user roles
RoleObj . prototype . get _user _roles = function ( id ) {
var me = this ;
me . head . innerHTML = 'Roles for ' + id ;
$ds ( me . role _div ) ;
$dh ( me . help _div ) ;
var callback = function ( r , rt ) {
me . set _user _roles ( r . message ) ;
}
$c _obj ( 'Company Control' , 'get_user_roles' , id , callback ) ;
}
// set user roles
RoleObj . prototype . set _user _roles = function ( list ) {
var me = this ;
for ( d in me . roles _dict ) {
me . roles _dict [ d ] . checked = 0 ;
}
for ( l = 0 ; l < list . length ; l ++ ) {
me . roles _dict [ list [ l ] ] . checked = 1 ;
}
}
// update roles
RoleObj . prototype . update _roles = function ( id ) {
var me = this ;
if ( id == user && has _common ( [ 'System Manager' ] , user _roles ) && ! me . roles _dict [ 'System Manager' ] . checked ) {
var callback = function ( r , rt ) {
if ( r . message ) {
if ( r . message > 1 ) {
var c = confirm ( "You have unchecked the System Manager role.\nYou will lose administrative rights and will not be able to set roles.\n\nDo you want to continue anyway?" ) ;
if ( ! c ) return ;
}
else {
var c = "There should be atleast one user with System Manager role." ;
me . roles _dict [ 'System Manager' ] . checked = 1 ;
}
}
me . set _roles ( id ) ;
}
$c _obj ( 'Company Control' , 'get_sm_count' , '' , callback ) ;
}
else {
me . set _roles ( id ) ;
}
}
// set roles
RoleObj . prototype . set _roles = function ( id ) {
var me = this ;
var role _list = [ ] ;
for ( d in me . roles _dict ) {
if ( me . roles _dict [ d ] . checked ) {
role _list . push ( d ) ;
}
}
var callback = function ( r , rt ) {
me . update _btn . done _working ( ) ;
me . dialog . hide ( ) ;
}
var arg = { 'usr' : id , 'role_list' : role _list } ;
me . update _btn . set _working ( ) ;
$c _obj ( 'Company Control' , 'update_roles' , docstring ( arg ) , callback ) ;
}
// get permission
RoleObj . prototype . get _permissions = function ( role ) {
var me = this ;
var callback = function ( r , rt ) {
$ ( me . help _div ) . slideToggle ( 'medium' ) ;
$ ( me . role _div ) . slideToggle ( 'medium' ) ;
me . set _permissions ( r . message , role ) ;
}
$c _obj ( 'Company Control' , 'get_permission' , role , callback ) ;
}
// set permission
RoleObj . prototype . set _permissions = function ( perm , role ) {
var me = this ;
me . help _div . body . innerHTML = '' ;
if ( perm ) {
me . help _div . head . innerHTML = 'Permissions for ' + role + ':<br><br>' ;
perm _tbl = make _table ( me . help _div . body , cint ( perm . length ) + 2 , 7 , '100%' , [ '30%' , '10%' , '10%' , '10%' , '10%' , '10%' , '10%' ] , { padding : '4px' } ) ;
var head _lst = [ 'Document' , 'Read' , 'Write' , 'Create' , 'Submit' , 'Cancel' , 'Amend' ] ;
for ( var i = 0 ; i < ( head _lst . length - 1 ) ; i ++ ) {
$td ( perm _tbl , 0 , i ) . innerHTML = "<b>" + head _lst [ i ] + "</b>" ;
}
2011-10-24 10:19:11 +00:00
var accept _img1 = 'lib/images/icons/accept.gif' ;
var cancel _img1 = 'lib/images/icons/cancel.gif' ;
2011-06-08 09:07:15 +00:00
for ( i = 1 ; i < perm . length + 1 ; i ++ ) {
$td ( perm _tbl , i , 0 ) . innerHTML = get _doctype _label ( perm [ i - 1 ] [ 0 ] ) ;
for ( var j = 1 ; j < ( head _lst . length - 1 ) ; j ++ ) {
if ( perm [ i - 1 ] [ j ] ) {
var accept _img = $a ( $td ( perm _tbl , i , j ) , 'img' ) ; accept _img . src = accept _img1 ;
}
else {
var cancel _img = $a ( $td ( perm _tbl , i , j ) , 'img' ) ; cancel _img . src = cancel _img1 ;
}
$y ( $td ( perm _tbl , i , j ) , { textAlign : 'center' } ) ;
}
}
}
else
me . help _div . head . innerHTML = 'No Permission set for ' + role + '.<br><br>' ;
}