void Element::setUsers( UserList ul ) { if (!isRecord()) return; UserList toAdd; UserList toRemove; UserList currentUsers = users(); foreach( User emp, ul ) if( !currentUsers.contains( emp ) ) toAdd += emp; foreach( User eu, currentUsers ) if( !ul.contains( eu ) ) toRemove += eu; ElementUserList tul = ElementUser::recordsByElement( *this ); foreach( ElementUser eu, tul ) if( toRemove.contains( eu.user() ) ) eu.remove(); foreach( User emp, toAdd ) { ElementUser tu; tu.setElement( *this ); tu.setUser( emp ); tu.setActive( 1 ); tu.commit(); }
ElementUser Element::addUser( const User & user ) { if( !isRecord() || !user.isRecord() ) return ElementUser(); ElementUserList tul( ElementUser::recordsByElement( *this ) ); foreach( ElementUser eu, tul ) if( eu.user() == user ) return eu; ElementUser tu; tu.setUser( user ); tu.setElement( *this ); tu.setActive( 1 ); tu.commit(); if( !elementStatus().isRecord() || elementStatus().name() == "New" ) { setElementStatus( ElementStatus::recordByName( "Assigned" ) ); commit(); } return tu; }