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

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

$taskid = isset($_POST["taskid"])?$_POST["taskid"]:0;
$taskid = is_numeric($taskid)?$taskid:0;
$userid = isset($_SESSION['user_id'])?$_SESSION['user_id']:0;
$shared = isset($_POST['shared'])?$_POST['shared']:0;
$shared = is_numeric($shared)?$shared:0;
$sharedmd5key = isset($_POST['sharedmd5key'])?$_POST['sharedmd5key']:'';
$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';

// function getOldTaskList ($DB) {
    // global $widtask_getOldLists;
    // $DB->getResults($widtask_getOldLists,
                                // $DB->escape($_SESSION['user_id'])
                           // );
    // if ($DB->nbResults()>0) {
        // while ($row = $DB->fetch(0)) {
            // $id = $row['id'];
            // $status = $row['status'];
            // $shared = $row['shared'];
            // $tasklistname = $row['tasklistname'];
            // echo "tasklists__MODULE_ID__[$id]={'name':'$tasklistname'};\n";
        // }
    // }
// }

?>
<Module>
<UserPref name="taskid" 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[
<script>
var id__MODULE_ID__=<?php echo $taskid;?>;
var inc__MODULE_ID__=0;
var tasks__MODULE_ID__=[];
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;?>';
var createdby__MODULE_ID__='';
var existstasklistid__MODULE_ID__=true;
var tasklists__MODULE_ID__ = {};
taskObj__MODULE_ID__=function(id,name,comment,status){
	this.id=id;
	this.name=name;
	this.comment=comment;
	this.status=status;
};
add__MODULE_ID__=function(v_form){
	var oName=$p.string.esc($p.string.doubleToSimpleCot(v_form.n__MODULE_ID__.value));
	var oCat=$p.string.esc($p.string.doubleToSimpleCot(v_form.r__MODULE_ID__.value));
	var oId=v_form.id.value;
	$p.ajax.call("../modules/wid_tasks.php",
		{
			'type':'load',
			'method':'post',
			'variables':
				"modid="+id__MODULE_ID__
				+ "&taskid="+oId
				+ "&act=add&name="+oName
				+ '&acceptwriteshare='+acceptwriteshare__MODULE_ID__
				+ '&shareditemkey='+shareditemkey__MODULE_ID__
				+"&cat="+oCat
			,
			'callback':
			{
				'function':refr__MODULE_ID__,
				'variables':
				{
					'taskid':id__MODULE_ID__
				}
			}
		}
	); 
}
sup__MODULE_ID__=function(id,newL){
	var response=confirm(lg("msgAreYouSureSupElement"));
	if (response==1) {
         $p.ajax.call("../modules/wid_tasks.php",
                {
                    'type':'load',
                    'method':'post',
                    'variables':
                        "modid="+id__MODULE_ID__
                        + "&taskid="+id
                        + "&act=sup"
                        + '&acceptwriteshare='+acceptwriteshare__MODULE_ID__
                        + '&shareditemkey='+shareditemkey__MODULE_ID__
                    ,
                    'callback':
                    {
                        'function':refr__MODULE_ID__,
                        'variables':
                        {
                            'taskid':id__MODULE_ID__
                        }
                    }
                }
            );   
	/*{executescr("../modules/wid_tasks.php","modid="+id__MODULE_ID__+"&act=sup&taskid="+id,false,true,refr__MODULE_ID__);}*/
    
    }
}
refr__MODULE_ID__=function(response,vars) {
    var v_id = $p.ajax.getVal(response,"tasklistid","str",false,0);
    id__MODULE_ID__ = v_id;
    tab[$p.app.tabs.sel].module[MODULE_ID_to_id("__MODULE_ID__")].changeVar("taskid",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);
    }
	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();
}
opt__MODULE_ID__=function(v_id){
	if (<?php echo $userid;?>==0){alert(lg("msgNeedToBeConnected"));}
	else {
		var obj=$p.get("addb__MODULE_ID__");
		obj.innerHTML='<form action="" onsubmit="add__MODULE_ID__(this);return false;">'
                    +'<table cellpadding="0">'
                    +"<tr><td>"+lg("lblTitle")+"</td>"
                    +"<td><input name='n__MODULE_ID__' type='text' maxlength='50' size='20' value='"+(v_id==indef?"":tasks__MODULE_ID__[v_id].name)+"' /></td>"
                    +"</tr>"
                    +"<tr><td>"+lg("Comment")+"</td>"
                    +"<td>"
                    +"<input name='r__MODULE_ID__' type='text' maxlength='255' size='35' value='"+(v_id==indef?"":tasks__MODULE_ID__[v_id].comment)+"' />"
                    +"</td></tr>"
                    +"<tr><td></td>"
                    +"<td>"
                    +"<input name='id' type='hidden' value='"+(v_id==indef?"":tasks__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>";
         $p.get("linkadd__MODULE_ID__").innerHTML='';
	}
}
noopt__MODULE_ID__=function(){
	if (shared__MODULE_ID__==1) return;
	var obj=$p.get("linkadd__MODULE_ID__");
	obj.innerHTML = "<a href='#' onclick='opt__MODULE_ID__();return false;'><img src='../images/ico_add.gif' /> "+lg("lblAddTask")+"</a>";
    $p.get("addb__MODULE_ID__").innerHTML='';
}
done__MODULE_ID__=function(id,obj){
         $p.ajax.call("../modules/wid_tasks.php",
                {
                    'type':'load',
                    'method':'post',
                    'variables':
                        "modid="+id__MODULE_ID__
                        + "&taskid="+id
                        + "&val="+(obj.checked?"Y":"N")
                        + "&act=done"
                        + '&acceptwriteshare='+acceptwriteshare__MODULE_ID__
                        + '&shareditemkey='+shareditemkey__MODULE_ID__
                    ,
                    'callback':
                    {
                        'function':refr__MODULE_ID__,
                        'variables':
                        {
                            'taskid':id__MODULE_ID__
                        }
                    }
                }
            );   


	/*executescr("../modules/wid_tasks.php","act=done&val="+(obj.checked?"Y":"N")+"&taskid="+id,false,true,refr__MODULE_ID__);*/
}


// getOldTaskLists__MODULE_ID__ = function () {
    // if ( !existstasklistid__MODULE_ID__ ) {
        // executescr("../modules/wid_tasks.php",
                        // "act=get",
                        // false,
                        // true,
                        // displayOlds__MODULE_ID__);
    // }
// }

// displayOlds__MODULE_ID__ = function () {
    // var obj=$p.get("tasklists__MODULE_ID__");
    // if ( tasklists__MODULE_ID__ ) {
        // var titleOTL =lg('widoldtasklist') +"<br />\n";
		// var oldTaskListe = "";
        // for (var tasklistid in tasklists__MODULE_ID__ ) {
            // var tasklistname = tasklistid;
            // if ( tasklists__MODULE_ID__['name']  ) {
                // tasklistname = tasklists__MODULE_ID__['name'];
            // }
            // var addtask = '<a href="#" onclick="refr__MODULE_ID__(\''+tasklistid+'_1\');">'+lg('open')+'</a>';
            // oldTaskListe += ""+tasklistname+ " " + addtask  +" <br />\n";
        // }
		// if( oldTaskListe != "" )
			// obj.innerHTML += titleOTL + oldTaskListe;
    // }
// }

// renameForm__MODULE_ID__ = function (tasklistid) {
    // var obj = $p.get("task__MODULE_ID__" + tasklistid);

// }

show__MODULE_ID__=function(){
	var l_s = '';

	for (var i=0;i<tasks__MODULE_ID__.length;i++)
    {
		l_s += '<div class="articleborder">';
        
   		if (shared__MODULE_ID__==0)
		{
			l_s += '<div style="float: right;line-height: 16px;">'
                +'<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="#" onclick="sup__MODULE_ID__('+tasks__MODULE_ID__[i].id+',false);return false;">'
                +'<?php echo $userid==0?'':'<img src="../images/ico_close.gif" align="absmiddle" />';?>'
                +'</a>'
                +'</div>';
		}

        l_s += (shared__MODULE_ID__ == 0 ? '<input class="nostyle" style="vertical-align: middle;" type="checkbox" onclick="done__MODULE_ID__('+tasks__MODULE_ID__[i].id+',this)"'+(tasks__MODULE_ID__[i].status=='Y'?'checked="checked" ':'')+'/>' : '')
			+ (tasks__MODULE_ID__[i].status=='Y'?'<strike>':'')
			+ '<a style="vertical-align: middle;" onmouseover=\'mouseBox("'+tasks__MODULE_ID__[i].comment+'",event)\' onmouseout=\'mouseBox("")\'">'
			+ tasks__MODULE_ID__[i].name
			+ '</a>'
			+ (tasks__MODULE_ID__[i].status=='Y'?'</strike>':'')
            + '</div>';
	}

	$p.print("c__MODULE_ID__",l_s);
}

taskShareState__MODULE_ID__ = function () {
  //if (createdby__MODULE_ID__ == 'another') {
        var shareState = {'yes':lg('widgetWorldUpdating'),'no':''};
        var l_s = shareState[acceptwriteshare__MODULE_ID__];
        $p.print("taskinfo__MODULE_ID__",l_s);
    //}
}
// 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("c__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_tasks.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,"tasklistid","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("taskid",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 ($taskid!=0 && isset($_SESSION['user_id'])) {
    echo "createdby__MODULE_ID__='another';\n";
    //sharedmd5key to share widget only
    if( (empty($sharedmd5key))
        || (!isset($sharedmd5key)) 
        || ($sharedmd5key=='undefined') ) {
            $DB->getResults($wid_getTasks,  
                                $DB->escape($taskid),
                                $DB->escape($_SESSION['user_id'])
                            ); 
            if ($DB->nbResults() == 0) {
                    $DB->getResults($wid_getTasksSharedKey,
								$DB->quote($shareditemkey),
								$DB->escape($taskid)
                                );            
            }
    } 
    else {
        $DB->getResults($wid_getTasksShared,
                            $DB->quote("%taskid=".$DB->escape($taskid)."%"),
                            $DB->quote($sharedmd5key),
                            $DB->escape($_SESSION['user_id']),
                            $DB->escape($widgetid),
                            $DB->escape($taskid)
                        );
    }	
    $inc=0;
    while ($row = $DB->fetch(0)){
        echo "tasks__MODULE_ID__[".$inc."]=new taskObj__MODULE_ID__(".$row["task_id"].",\"".$row["name"]."\",\"".str_replace("'"," ",$row["comments"])."\",\"".$row["done"]."\");";
        $inc++;
    }
    if ($DB->nbResults()==0) {
        echo "\nexiststasklistid__MODULE_ID__=false;\n";
    }    
	$DB->freeResults();
} elseif( $taskid!=0 && !empty($shareditemkey) ) {
    echo "createdby__MODULE_ID__='another';\n";
        $DB->getResults($wid_getTasksSharedKey,
								$DB->quote($shareditemkey),
								$DB->escape($taskid)
                                );
	    $inc=0;
	    while ($row = $DB->fetch(0)){
	        echo "tasks__MODULE_ID__[".$inc."]=new taskObj__MODULE_ID__(".$row["task_id"].",\"".$row["name"]."\",\"".str_replace("'"," ",$row["comments"])."\",\"".$row["done"]."\");";
	        $inc++;
	    }
        if ($DB->nbResults()==0) {
            echo "\nexiststasklistid__MODULE_ID__=false;\n";
        }
		$DB->freeResults();
} else {
    echo "createdby__MODULE_ID__='myself';\n";
    echo "\nexiststasklistid__MODULE_ID__=false;\n";
    // getOldTaskList($DB);
}
?>
		show__MODULE_ID__();
		taskShareState__MODULE_ID__();
   }
}
init__MODULE_ID__();
</script>
<div id="c__MODULE_ID__" style="margin:6px;padding:6px;width:95%;" ></div>
<!--<div id="tasklists__MODULE_ID__" style='padding:2px 0 4px 4px;' ></div>-->
<div id="taskfooter__MODULE_ID__">
<span id="taskinfo__MODULE_ID__" style="color:#C0C0C0;float: right;"></span>
<span id="linkadd__MODULE_ID__" style="padding:2px 0 4px 4px;"></span>
</div>
<div "clear:both;"></div>
<div id="addb__MODULE_ID__" style="padding:2px 0 4px 4px;"></div>

]]></Content>
</Module>