<?php
// this file needs to be utf8 without BOM. Check accentuated chars : é à è ù
header("content-type: application/xml");
$folder = "";
$not_access = 0;
$pagename = "modules/p_links.php";
//includes
require_once('includes.php');

echo '<'.'?xml version="1.0" encoding="UTF-8"?'.'>';

$linkid=isset($_POST["linkid"])?$_POST["linkid"]:0;
$linkid = is_numeric($linkid)?$linkid:0;
$userid=isset($_SESSION['user_id'])?$_SESSION['user_id']:0;
$selkey=isset($_POST["selkey"])?htmlentities($_POST["selkey"]):"";
$shared=isset($_POST['shared'])?$_POST['shared']:0;
$shared = is_numeric($shared)?$shared:0;
$sharedmd5key=isset($_REQUEST['sharedmd5key'])?$_REQUEST['sharedmd5key']:'';
$widgetid = isset($_REQUEST['widgetid'])?$_REQUEST['widgetid']:0;
$shareditemkey = isset($_REQUEST['shareditemkey'])?$_REQUEST['shareditemkey']:'';
$shareditemkey = (empty($shareditemkey) && isset($_POST['sharedkey']))?$_POST['sharedkey']:$shareditemkey;
$acceptwriteshare = (empty($acceptwriteshare) && isset($_POST['acceptwriteshare']))?$_POST['acceptwriteshare']:'no';
$ownerallowwriting = (empty($ownerallowwriting) && isset($_POST['ownerallowwriting']))?$_POST['ownerallowwriting']:'no';
?>
<Module>
<UserPref name="linkid" display_name="." datatype="hidden" default_value="0" />
<UserPref name="sharedkey" display_name="keysharing" datatype="hidden" default_value="" />
<UserPref name="sharedmd5key" display_name="." datatype="hidden" default_value="" />
<UserPref name="shareditemkey" display_name="." datatype="hidden" default_value="" />
<UserPref name="shared" display_name="." datatype="hidden" default_value="" />
<?php 
	if (isset($_SESSION['user_id']) && $_SESSION['type'] == 'A')
	{
?>
<UserPref name="ownerallowwriting" datatype="enum" default_value="no" display_name="ownerallowwriting" >
    <EnumValue value="yes" display_value="yes" />
    <EnumValue value="no" display_value="no" />
</UserPref>  
<UserPref name="acceptwriteshare" datatype="enum" default_value="no" display_name="acceptwriteshare" >
    <EnumValue value="yes" display_value="yes" />
    <EnumValue value="no" display_value="no" />
</UserPref> 
<?php
	} else {
?>
<UserPref name="ownerallowwriting" datatype="hidden" default_value="no" display_name="ownerallowwriting" />
<UserPref name="acceptwriteshare" datatype="hidden" default_value="no" display_name="acceptwriteshare" />
<?php
	}
?>


<Content><![CDATA[
<head>
<script>
var id__MODULE_ID__=<?php echo $linkid;?>;
var inc__MODULE_ID__=0;
var name__MODULE_ID__="";
var links__MODULE_ID__=[];
var keys__MODULE_ID__=[];
var selKey__MODULE_ID__="<?php echo $selkey;?>";
var shared__MODULE_ID__=<?php echo $shared;?>;
var shareditemkey__MODULE_ID__ ='<?php echo $shareditemkey;?>'; 
var ownerallowwriting__MODULE_ID__ ='<?php echo $ownerallowwriting;?>';
var acceptwriteshare__MODULE_ID__ = '<?php echo $acceptwriteshare;?>';

linkObj__MODULE_ID__=function(id,name,url,tags){
	this.id=id;
	this.name=name;
	this.url=url;
	this.tags=tags;
}
add__MODULE_ID__=function(v_form){
	var oName=v_form.n__MODULE_ID__.value;
	var oLink=v_form.l__MODULE_ID__.value;
	var oTags=v_form.t__MODULE_ID__.value;
	var oId=v_form.id.value;
	//remove unused chars
	oTags=oTags.replace(";",",");
	oTags=$p.string.trim(oTags);
	oTags=oTags.replace(" ,",",");
	oTags=oTags.replace(", ",",");
    
	if (oLink.indexOf("http://") == -1 
            && oLink.indexOf("https://") == -1 
            && oLink.indexOf("file://") == -1
        ) 
    {
        oLink="http://"+oLink;
    }
	name__MODULE_ID__=oName;
    
        $p.ajax.call("../modules/wid_links.php",
                {
                    'type':'load',
                    'method':'post',
                    'variables':
                        "modid="+id__MODULE_ID__
                        + "&linkid="+oId
                        + "&act=add&name="+$p.string.esc(oName)
                        + "&link="+$p.string.esc(oLink)
                        + "&tags="+$p.string.esc(oTags)
                        + '&acceptwriteshare='+acceptwriteshare__MODULE_ID__
                        + '&shareditemkey='+shareditemkey__MODULE_ID__
                    ,
                    'callback':
                    {
                        'function':treat__MODULE_ID__,
                        'variables':
                        {
                            'linklistid':id__MODULE_ID__
                        }
                    }
                    
                }
                
            );

    /*
	executescr("../modules/wid_links.php",
                        "modid="+id__MODULE_ID__
                        +"&linkid="+oId
                        +"&act=add&name="
                        +$p.string.esc(oName)
                        +"&link="+$p.string.esc(oLink)
                        +"&tags="+$p.string.esc(oTags),
                        false,true,treat__MODULE_ID__);
                        */
}
treat__MODULE_ID__=function(response,vars) {

    var v_id = $p.ajax.getVal(response,"linklistid","str",false,vars['linklistid']);
    id__MODULE_ID__ = v_id;
    tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].changeVar("linkid",v_id);   
    var sharedkey = $p.ajax.getVal(response,"sharedkey","str",false,'');  
    var ownerallowwriting = $p.string.getVar(tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].vars,"ownerallowwriting");
	var acceptwriteshare = $p.string.getVar(tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].vars,"acceptwriteshare");

	if(acceptwriteshare==indef || acceptwriteshare=="") {
		acceptwriteshare = 'no';
	}
    if (ownerallowwriting == 'yes') {
        tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].changeVar("sharedkey",sharedkey);
    }
	if (acceptwriteshare == 'yes') {
		tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].changeVar("acceptwriteshare","yes");
	} else {
		tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].changeVar("acceptwriteshare","no");
	}
	tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].refresh();
	//refr__MODULE_ID__();
    
}
sup__MODULE_ID__=function(id,newL)
{
	var response = confirm(lg("msgAreYouSureSupElement"));
	if (response == 1)
	{
         $p.ajax.call("../modules/wid_links.php",
                {
                    'type':'load',
                    'method':'post',
                    'variables':
                        "modid="+id__MODULE_ID__
                        + "&linkid="+id
                        + "&act=sup"
                        + '&acceptwriteshare='+acceptwriteshare__MODULE_ID__
                        + '&shareditemkey='+shareditemkey__MODULE_ID__
                    ,
                    'callback':
                    {
                        'function':treat__MODULE_ID__,
                        'variables':
                        {
                            'linklistid':id__MODULE_ID__
                        }
                    }
                }
            );  
    }
    /*
	if (response==1)
    {executescr("../modules/wid_links.php",
                "modid="+id__MODULE_ID__+"&act=sup&linkid="+id,false,true,treat__MODULE_ID__);}
                */
}
opt__MODULE_ID__=function(v_id){
	if (<?php echo $userid;?>==0) 
    {
        alert(lg("msgNeedToBeConnected"));
    }
	else 
    {
        var linkname = (v_id == indef) ? "" : links__MODULE_ID__[v_id].name;
        var url = (v_id==indef) ? "http://" : links__MODULE_ID__[v_id].url;
        var tags = (v_id==indef) ? "" :links__MODULE_ID__[v_id].tags;
		var obj=$p.get("addb__MODULE_ID__");
		obj.innerHTML="<form action='' onsubmit='add__MODULE_ID__(this);return false;'>"
                +"<table>"
                +"<tr>"
                +"<td>"+lg("lblTitle")+"</td>"
                +'<td><input name="n__MODULE_ID__" type="text" maxlength="30" size="30" value="'+linkname+'" /></td>'
                +"</tr>"
                +"<tr>"
                +"<td>URL</td>"
                +"<td><input name='l__MODULE_ID__' type='text' maxlength='199' size='30' value='"+url+"' /></td>"
                +"</tr>"
                +"<tr>"
                +"<td>"+lg("tags")+"</td>"
                +"<td><input name='t__MODULE_ID__' type='text' maxlength='100' size='28' value='"+tags+"' /> "+tooltip('linkKeysHlp')+"</td></tr>"
                +"<tr><td></td>"
                +"<td>"
                +"<input name='id' type='hidden' value='"+(v_id==indef?"":links__MODULE_ID__[v_id].id)+"' />"
                +"<input class='btn' type='submit' value='"+(v_id==indef?lg("lblAdd"):lg("lblModify"))+"' /> "
                +"<a href='#' onclick='noopt__MODULE_ID__();return false;'><img src='../images/ico_close.gif' /></a>"
                +"</td></tr>"
                +"</table>"
                +"</form>";
	}
}
noopt__MODULE_ID__=function(){
	if (shared__MODULE_ID__==1) return false;
	var obj=$p.get("addb__MODULE_ID__");
	obj.innerHTML="<span id='acceptwriteshare__MODULE_ID__' style='color: #C0C0C0;float: right;'></span> <a href='#' onclick='opt__MODULE_ID__();return false;'><img src='../images/ico_add.gif' /> "+lg("lblAddFav")+"</a>";
	// write if widget is shared or not
	acceptShareState__MODULE_ID__();
}
refr__MODULE_ID__=function(){
	tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].refresh();
}
// display sharing question
initiateSharingMessage__MODULE_ID__ = function() {
	var l_s = lg('doYouWantToShareWidget')
		+ ' ?<br />'
		+ '<center>'
		+ '<input type="button" onclick="setWidgetAsShared__MODULE_ID__(true);" value="' + lg('yes') + '" /> '
		+ '<input type="button" onclick="setWidgetAsShared__MODULE_ID__(false);" value="' + lg('no') + '" /> '
		+ '</center>';

	$p.print("lkd__MODULE_ID__",l_s);
}
// Define the widget as shared
setWidgetAsShared__MODULE_ID__ = function(v_shared) {
	var l_shared = (v_shared ? 'yes' : 'no');
	tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].changeVar("acceptwriteshare",l_shared);
	acceptwriteshare__MODULE_ID__ = l_shared;
	createWidgetItemInDb__MODULE_ID__();
}
// initialize the widget in the DB - create the task item
createWidgetItemInDb__MODULE_ID__ = function(){
	$p.ajax.call("../modules/wid_links.php",
		{
			'type':'load',
			'method':'post',
			'variables':'act=init',
			'callback':
			{
				'function' : createWidgetItemInDbCallback__MODULE_ID__
			}
		}
	); 
}
createWidgetItemInDbCallback__MODULE_ID__ = function(response,vars) {	
	var l_id = $p.ajax.getVal(response,"linklistid","str",false,0);
	var l_sharedkey = $p.ajax.getVal(response,"sharedkey","str",false,0);
	
	tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].changeVar("linkid",l_id);
	id__MODULE_ID__ = l_id;
	
	tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].changeVar("shareditemkey",l_sharedkey);
	shareditemkey__MODULE_ID__ = l_sharedkey;
	
	// refresh the widget content
	init__MODULE_ID__();
}


init__MODULE_ID__=function(){
	if (id__MODULE_ID__ == 0 && $p.app.user.id == -1)
	{
		initiateSharingMessage__MODULE_ID__();
	}
    else
	{
		noopt__MODULE_ID__();  
<?php
if ($linkid!=0 && isset($_SESSION['user_id'])) {
    if( (empty($sharedmd5key))
        || (!isset($sharedmd5key)) 
        || ($sharedmd5key=='undefined') ) {
        $DB->getResults($wid_getUserLinks,
                            $DB->escape($linkid),
                            $DB->escape($_SESSION['user_id'])
                       );
            if ($DB->nbResults() == 0) {
                    $DB->getResults($wid_getUserLinksSharedKey,
								$DB->quote($shareditemkey),
                                $DB->escape($linkid)
                                );       
            }                       
    } else {
    
        $DB->getResults($wid_getUserLinksShared,
                            $DB->quote("%linkid=".$DB->escape($linkid)."%"),
                            $DB->quote($sharedmd5key),
                            $DB->escape($_SESSION['user_id']),
                            $DB->escape($widgetid),
                            $DB->escape($linkid)
                        );
    }	
    $inc=0;
	while ($row = $DB->fetch(0)){
		echo "links__MODULE_ID__[".$inc."]=new linkObj__MODULE_ID__(".$row["link_id"].",\"".$row["name"]."\",\"".$row["url"]."\",\"".$row["tags"]."\");";
		$inc++;
	}
	$DB->freeResults();
} elseif( $linkid!=0 && !empty($shareditemkey) ) {
        $DB->getResults($wid_getUserLinksSharedKey,
								$DB->quote($shareditemkey),
                                $DB->escape($linkid)
                                );
	    $inc=0;
		while ($row = $DB->fetch(0)){
			echo "links__MODULE_ID__[".$inc."]=new linkObj__MODULE_ID__(".$row["link_id"].",\"".$row["name"]."\",\"".$row["url"]."\",\"".$row["tags"]."\");";
			$inc++;
		}
		$DB->freeResults();
}
?>
		getKeys__MODULE_ID__();
		show__MODULE_ID__();
	}
}
getKeys__MODULE_ID__=function(){
	for (var i=0;i<links__MODULE_ID__.length;i++){
		var arr=links__MODULE_ID__[i].tags.split(",");
		for (var j=0;j<arr.length;j++){
			if (arr[j]!="" && !$p.array.find(keys__MODULE_ID__,arr[j])){
				keys__MODULE_ID__.push(arr[j]);
			}
		}
	}
	keys__MODULE_ID__.sort();
}
show__MODULE_ID__=function(){
	var l_s = "",
        l_slink = "";

	for (var i=0;i<links__MODULE_ID__.length;i++)
    {
		if (selKey__MODULE_ID__=="" || $p.array.find((links__MODULE_ID__[i].tags.split(",")),selKey__MODULE_ID__))
        {
            l_slink += '<div class="articleborder" style="padding-bottom: 3px;background: url(../images/ico_menu_star.gif) no-repeat top left;padding-left: 20px;">';
			if (shared__MODULE_ID__==0)
			{
				l_slink += '<div style="float: right">'
				+ '<a href="#" title="'+lg("lblModify")+'" onclick="opt__MODULE_ID__('+i+');return false;">'
				+ '<?php echo $userid==0?'':'<img src="../images/ico_edit.gif" align="absmiddle" />';?>'
				+ '</a> '
				+ '<a href="#" title="'+lg("lblSuppress")+'" onclick="sup__MODULE_ID__('+links__MODULE_ID__[i].id+',false);return false;">'
				+ '<?php echo $userid==0?'':'<img src="../images/ico_close.gif" align="absmiddle" />';?>'
				+ '</a>'
				+ '</div>'
			}
            l_slink += '<a id="lk__MODULE_ID___'+i+'" href="'+links__MODULE_ID__[i].url+'" target="_blank" style="color:#000000">'
                +links__MODULE_ID__[i].name
                +'</a>'
                +'</div>';
		}
	}
	if (keys__MODULE_ID__.length>0){
		l_s+="<div style='background-color:#efefef;padding:1px;'><form>"+lg("tags")+" : <select name='keywords' onchange='changeKey__MODULE_ID__(this.value);'><option value=''>* "+lg("all")+" *</option>";
		keys__MODULE_ID__.sort();
		for (var i=0;i<keys__MODULE_ID__.length;i++){
			l_s+='<option value="'+keys__MODULE_ID__[i]+'"'+(keys__MODULE_ID__[i]==selKey__MODULE_ID__?' selected="selected" ':'')+'>'+keys__MODULE_ID__[i]+'</option>';
		}
		l_s+="</select></form></div>";
	}

	l_s += l_slink;

	$p.print("lkd__MODULE_ID__",l_s);
}
changeKey__MODULE_ID__=function(v_key){
	selKey__MODULE_ID__=v_key;
	tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].changeVar("selkey",v_key);
	show__MODULE_ID__();
}
function acceptShareState__MODULE_ID__ () {

        var shareState = {'yes':lg('widgetWorldUpdating'),'no':''};
        var l_s = shareState[acceptwriteshare__MODULE_ID__.toLowerCase()];
        var obj = $p.get("acceptwriteshare__MODULE_ID__");
        obj.innerHTML = l_s;
}
init__MODULE_ID__();
</script>
</head>
<body>
<div id="lkd__MODULE_ID__" style='padding:6px'></div>
<div id="addb__MODULE_ID__" style='padding:2px 0 4px 4px;'></div>
</body>
]]></Content>
</Module>