} else {
    $site->fdat['selected_group'] = 'group_id:' . $site->fdat['group_id'];
}
#echo $site->fdat['selected_group']. " gr:".$site->fdat['group_id'];
########### find ALL GROUPS as TREE
# push all groups to level array
$all_levels = array();
foreach (get_groupleafs(array("group_id" => $top_group)) as $key => $tmpgroup) {
    if (!$tmpgroup['level']) {
        $tmpgroup['level'] = 1;
    }
    $all_levels[$tmpgroup['level']][] = $tmpgroup;
}
#printr($all_levels);
################## get SELECTED item group PARENTS
$grouptree = get_grouptree(array("group_id" => $site->fdat['group_id']));
#printr($grouptree);
foreach ($grouptree as $tmgroup) {
    $selected_parents[] = $tmgroup['id'];
}
#printr($selected_parents);
#printr($site->fdat['user_id']);
#################
# SAVE
if ($site->fdat['op'] == 'save') {
    verify_form_token();
    save_all_permissions();
    $site->fdat['op'] == '';
}
# / SAVE
#################
function print_permission_row($args){
	global $site;
	global $objekt;

	$perm = $args['perm'];
	$remove_href = $args['remove_href'];
	$copy_href = $args['copy_href'];
	$crud = $args['crud'];
#printr($perm);
	$id = $perm['is_role'] ? $perm['role_id'] : ($perm['is_group'] ? $perm['group_id'] : $perm['user_id']);

	# check if we have public folder objekt - it has some exceptional behaviour
	$is_public_folder = false;

	if($objekt->objekt_id && $objekt->all['tyyp_id'] == 22){
		$objekt->load_sisu(); # load content table to get fullpath value
		if(strpos($objekt->all['relative_path'], '/public') === 0)
		{
			$is_public_folder = true;
		}
	}

		# mouseover message for group/user name, displays full path of group membership
		if($perm['is_role']) {
		}
		elseif($perm['is_group']) {
			$grouptree = get_grouptree(array("group_id" => $perm['group_id']));
		}
		else {
			$tmpuser = new User(array(
				user_id => $perm['user_id'],
			));
			$grouptree = get_grouptree(array("group_id" => $tmpuser->group_id));		
		}
		$group_msg = array();
		if(sizeof($grouptree)>0){
			foreach($grouptree as $key=>$group){
				$group_msg[] = $group['name'];
			}
		}
		if($perm['is_role']) { 	$href_title = $site->sys_sona(array(sona => "role", tyyp=>"kasutaja")); }
		else { $href_title = join(" > ",$group_msg); }

		# acl - shows if we have user or group or role
		$acl = $perm['is_role'] ? 'role': ($perm['is_group']?'group': 'user');
	?>

		<?######### name ########?>
			<tr> 
              <td nowrap  width="16"><img alt="" src="<?=$site->CONF['wwwroot'].$site->CONF['styles_path']?>/gfx/icons/16x16/users/<?=$perm['is_role'] ? 'contacts': ($perm['is_group']?'group':'user')?>.png" width="16" height="16"></td>
			  <td nowrap><a href="#" title="<?=$href_title?>"><?=$perm['name']?></a>

		<?######### hidden ID (eg permission_user_11, permission_group_1) ########?>
			 <input type=hidden name=permission_<?=$acl?>_<?=$id?> value="<?=$id?>">
			 </td>
		  <? foreach($crud as $char) {?>
			<?if($char == 'S') { ########## subtree copy button 

			$copy_href .= '&perm_user_id='.$perm['user_id'];
			$copy_href .= '&perm_group_id='.$perm['group_id'];
			$copy_href .= '&perm_role_id='.$perm['role_id'];
			?>
			 <td align="center" width="24"> 
				<ul class="scms_button_row"><li><a href="javascript:void(openpopup('<?=$copy_href?>&copypermissions='+get_copypermissions_url('<?=$acl?>','<?=$id?>'),'copypermissions','300','108'))"  class="button_subtree" title="<?=$site->sys_sona(array(sona => "Copy permissions to subtree", tyyp=>"editor"))?>"></a></li></ul>

<!--old checkbox
				<input id="<?=$acl?>_<?=$char?>_<?=$id?>" name="<?=$acl?>_<?=$char?>_<?=$id?>" type="checkbox" value="1">
old checkbox-->
              </td>
			<?} else { ############ C/R/U/P/D ?>
			 <td align="center" width="24">
				
				<?
				### exception for public folder: Read is already ON and disabled (Bug #2216)
				if($char == 'R' && $is_public_folder) {  ?>
					<input type="hidden"	name="<?=$acl?>_<?=$char?>_<?=$id?>" value="1">
					<input name="tmp" type="checkbox" value="1" checked disabled> 
				<?}
				### usual case
				else{?>
	                <input id="<?=$acl?>_<?=$char?>_<?=$id?>" name="<?=$acl?>_<?=$char?>_<?=$id?>" type="checkbox" value="1" <?=($perm[$char]?' checked':'')?> onclick="sanity_check(this,'<?=$acl?>','<?=$char?>','<?=$id?>');">
				<?}?>
              </td>
			<?}?>
		  <? } ?>
			<?	######## delete button: OBJ ONLY:dont allow to delete everybody row ?>
              <td align="center" width="24"><ul class="scms_button_row"><li><?if(!($args['type']=='OBJ' && $perm['predefined_group'])){?><a href="<?=$remove_href?>" class="button_delete" title="<?=$site->sys_sona(array(sona => "Kustuta", tyyp=>"editor"))?>"></a><?} else { ?><img src="<?=$site->CONF['wwwroot'].$site->CONF['img_path']?>/px.gif"  width="11" height="12" border="0"><?}?></li></ul></td>

<!--old              <td align="center" width="24"><?if(!($args['type']=='OBJ' && $perm['predefined_group'])){?><a href="<?=$remove_href?>"><img src="<?=$site->CONF['wwwroot'].$site->CONF['styles_path']?>/gfx/icons/16x16/actions/delete.png" alt="Remove" width="16" height="16" border="0"></a><?} else { ?><img src="<?=$site->CONF['wwwroot'].$site->CONF['img_path']?>/px.gif"  width="11" height="12" border="0"><?}?></td>
-->
            </tr>
<?
}
 /**
 * load_aclpermissions 
 * 
 * returns all acl (group) permissions for guest (that means for group Everybody);
 * returns 0 if failed;
 * function is not called by default in site class but
 * in edit-group-window and edit-user-window, 
 * also in all templates where any group or user info is used/printed/etc
 * 
 * 
 * @package CMS
 * 
 * @param -
 */
 function load_aclpermissions()
 {
     $args = $this->args;
     $perm = array();
     $timer = new Timer();
     # alustame m??????tmine
     ######### GET EVERYBODY GROUP
     $grouptree = get_grouptree(array("group_id" => ''));
     foreach ($grouptree as $key => $group) {
         $group_names[] = $group['name'];
         $group_arr[] = $group['id'];
         $group_msg[] = $group['name'] . " (ID " . $group['id'] . ")";
     }
     $this->debug->msg("Guest group info: " . join(" => ", $group_msg));
     ######### LOAD ALL EVERYBODY GROUP PERMISSIONS: type ACL
     $sql = $this->site->db->prepare("SELECT * FROM permissions WHERE type=? ", 'ACL');
     $sql .= $this->site->db->prepare(" AND (group_id IN('" . join("','", $group_arr) . "') AND user_id=?)", 0);
     #print $sql;
     $sth = new SQL($sql);
     $cemented_perm = array();
     while ($permtmp = $sth->fetch('ASSOC')) {
         $obj_id = $permtmp['source_id'];
         if ($permtmp['group_id']) {
             # group permission
             $cemented_perm[$obj_id] = $permtmp;
         }
     }
     $this->debug->msg("ACL permissions loaded:  " . sizeof(array_keys($cemented_perm)) . " permissions found. Load time: " . $timer->get_aeg());
     ########### RETURN PERMISSIONS
     return $cemented_perm;
 }