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

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

$noteid = isset($_POST["noteid"]) ? $_POST["noteid"] : 0;
$noteid = is_numeric($noteid)?$noteid:0;
$userid = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : 0;
$shared = isset($_POST['shared']) ? $_POST['shared'] : 0;
$sharedmd5key = isset($_POST['sharedmd5key'])?$_POST['sharedmd5key']:'';
$shared = is_numeric($shared)?$shared:0;
$widgetid = isset($_POST['widgetid'])?$_POST['widgetid']:0;
$shareditemkey = isset($_POST['shareditemkey'])?$_POST['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="color" display_name="lblBgColor" datatype="enum" default_value="F7F384">
  <EnumValue value="ffffff" display_value="Blanc"/>
  <EnumValue value="F7F384" display_value="Jaune"/>
  <EnumValue value="C3D9FF" display_value="Bleu"/>
  <EnumValue value="FFDFDE" display_value="Rouge"/>
  <EnumValue value="B8E0BA" display_value="Vert"/>
  <EnumValue value="FADCB3" display_value="Orange"/>
  <EnumValue value="F3E7B3" display_value="Marron"/>
  <EnumValue value="E1C7E1" display_value="Violet"/>
</UserPref>
<UserPref name="noteid" display_name="." datatype="hidden" default_value="0" />
<UserPref name="sharedkey" display_name="keysharing" 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 odata__MODULE_ID__;
var id__MODULE_ID__ = <?php echo $noteid;?>;
var ownerallowwriting__MODULE_ID__ = '<?php echo $ownerallowwriting;?>';
var acceptwriteshare__MODULE_ID__ = '<?php echo $acceptwriteshare;?>';
var shareditemkey__MODULE_ID__ = '<?php echo $shareditemkey;?>';
var shared__MODULE_ID__ = <?php echo $shared;?>;
var note__MODULE_ID__ = <?php
       
	if ( $noteid!=0 && isset($_SESSION["user_id"]) ){
        //sharedmd5key to share widget only
       if( empty($sharedmd5key) 
            || (!isset($sharedmd5key)) 
            || $sharedmd5key=='undefined') {
                $DB->getResults($xml_getUserNotes,$DB->escape($noteid),$DB->escape($_SESSION["user_id"]));
				$row = $DB->fetch(0);
				if( empty($row["notes"]) ) {
					$DB->freeResults();
					$DB->getResults($xml_getUserNotesBySharedKey,
                                $DB->escape($noteid),
								$DB->quote($shareditemkey)
                                );
					$row = $DB->fetch(0);
				}
		} else {
                $DB->getResults($xml_getUserNotesShared,
                                $DB->quote("%noteid=".$DB->escape($noteid)."%"),
                                $DB->quote($sharedmd5key),
                                $DB->escape($_SESSION['user_id']),
                                $DB->escape($widgetid),
                                $DB->escape($noteid)
                                );
				        $row = $DB->fetch(0);
        }

		if( !empty($row["notes"]) ) {
			echo '"'.$row["notes"].'"';
		} else {
			echo "__txtnote";
		}
		$DB->freeResults();
		$DB->close();
    //shareditemkey to whare widget content
	} elseif( $noteid!=0 && !empty($shareditemkey) ) {
        $DB->getResults($xml_getUserNotesBySharedKey,
                                $DB->escape($noteid),
								$DB->quote($shareditemkey)
                                );
		$row = $DB->fetch(0);
		if( !empty($row["notes"]) ) {
			echo '"'.$row["notes"].'"';
		} else {
			echo "__txtnote";
		}
		$DB->freeResults();
		$DB->close();
	}
	else {echo "__txtnote";}
?>;
write__MODULE_ID__ = function()
{
	if (shared__MODULE_ID__ == 1) return false; // if widget shared, writing is desabled

	var mainobj = $p.get("bn__MODULE_ID__"),
        obj = $p.get("bnc__MODULE_ID__"),
        data=obj.innerHTML;

	//firefox bug correction
	data = data.replace(/\r\n/g," ");
	odata__MODULE_ID__ = data;
	data = $p.string.htmlToText(data);
	var oWidth = obj.offsetWidth;
	var oHeight = (obj.offsetHeight>100)?obj.offsetHeight:100;

	mainobj.innerHTML = "<textarea id='bntxt__MODULE_ID__' style='width:"+oWidth+"px;height:"+oHeight+"px;border:1px dashed #ff0000;font-size:10px;FONT-FAMILY:Verdana,Arial;' onblur='save__MODULE_ID__();return false;' onmouseover='this.focus()'>"
        + data
        + "</textarea>";
}
save__MODULE_ID__=function()
{
	var mainobj = $p.get("bn__MODULE_ID__"),
        obj = $p.get("bntxt__MODULE_ID__"),
        data = $p.string.textToHtml($p.string.doubleToSimpleCot(obj.value));

	if (data != odata__MODULE_ID__)
    {
        $p.ajax.call("../modules/wid_notes.php",
                {
                    'type':'load',
                    'method':'post',
                    'variables':
                        'noteid='+id__MODULE_ID__
                        + '&notes='+$p.string.esc(data)
                        + '&userid=<?php echo $userid;?>'
                        + '&acceptwriteshare='+acceptwriteshare__MODULE_ID__
                        + '&shareditemkey='+shareditemkey__MODULE_ID__
                    ,
                    'callback':
                    {
                        'function':getId__MODULE_ID__,
                        'variables':
                        {
                            'noteid':id__MODULE_ID__
                        }
                    }
                }
            );    
    
    
        //executescr("../modules/wid_notes.php","noteid="+id__MODULE_ID__+"&notes="+$p.string.esc(data)+"&userid=<?php echo $userid;?>",false,true,getId__MODULE_ID__);
    }
	else
        init__MODULE_ID__();
}
getId__MODULE_ID__=function(response,vars)
{
    var v_id = $p.ajax.getVal(response,"ret","str",false,0);
    id__MODULE_ID__ = v_id;
    tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].changeVar("noteid",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 (ownerallowwriting == 'yes') {
        tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].changeVar("sharedkey",sharedkey);
        tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].changeVar("ownerallowwriting","yes");
       
    }
	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");
	}
	//added 1.4 to refresh module options
	tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].refresh();
}
function acceptShareState__MODULE_ID__ () {

        var shareState = {'yes':lg('widgetWorldUpdating'),'no':''};
        var l_s = shareState[acceptwriteshare__MODULE_ID__];
        
		if (l_s != '')
		{
			$p.print("acceptwriteshare__MODULE_ID__", l_s);
			$p.show("acceptwriteshare__MODULE_ID__", "block");
        }
}
// 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.show("lkd__MODULE_ID__",'block');
	$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_notes.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,"noteid","str",false,0);
	id__MODULE_ID__ = l_id;
	var l_sharedkey = $p.ajax.getVal(response,"sharedkey","str",false,0);
	
	tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].changeVar("noteid",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
	{
		$p.show("lkd__MODULE_ID__",'none');
		$p.print("lkd__MODULE_ID__","");
		var obj = $p.get("bn__MODULE_ID__");
		obj.innerHTML = "<table cellpadding='0' cellspacing='0' border='0' width='100%'>"
	        + "<tr>"
	        + "<?php if ($userid == 0){echo "<td height='20' onclick='alert__MODULE_ID__();'>";} else {echo "<td height='20' onclick='javascript:write__MODULE_ID__();' id='bnc__MODULE_ID__'>";};?>"
	        + note__MODULE_ID__
	        + "</td>"
	        + "</tr>"
	        + "</table>";
	    acceptShareState__MODULE_ID__();
	}
}
alert__MODULE_ID__=function()
{
	alert(lg('msgNeedToBeConnected')+' !');
}
init__MODULE_ID__();
</script>
</head>
<body>
<div id="lkd__MODULE_ID__" style='display: none;'></div>
<table cellpadding="3" cellspacing="0" width="100%" border="0">
 <tr>
  <td id="bn__MODULE_ID__" bgcolor="<?php echo (isset($_POST["color"])?"#".$_POST["color"]:"#F7F384");?>">
  </td>
 </tr>
</table>
<div class="fadetext" id="acceptwriteshare__MODULE_ID__" style='display: none;'></div>
</body>
]]></Content>
</Module>