<?php
// Created by Ardora www.webardora.net
//COMENTARIOS-ardoraAjaxJson
//bajo licencia Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)
//para otros usos contacte con el autor
$_SERVER['HTTP_REFERER'] = "introDatos.php";session_start();$slogin_noauthpage=0;$slogin_pagetitle="";include("para.php");include_once (profUname.prof2."../".uName."/".uName."_lib.php");include("cfg.php");session_cache_limiter("nocache,private");ini_set("memory_limit", "80M");
if($_POST["action"]=="all"){$xml=simplexml_load_file("../ardoraWorkFiles/ci/ci.xml");$data=array();foreach($xml -> children() as $note) {$data[] = array("id" => (string) $note -> id, "father" => (string) $note -> father, "usr" => (string) $note -> usr, "comment" => (string) $note -> comment, "dt" => (string) $note -> dt);};echo json_encode($data);}
if ($_POST["action"]=="getDataUser") {$autor=trim($_POST["author"]);$type=dameValor($autor, "TIP");$userName=dameValor($autor, "NOM");$course=dameValor($autor, "CUR");$group=dameValor($autor, "GRU");echo json_encode(array("type"=>$type, "userName"=>$userName, "course"=>$course, "group"=>$group));}
if ($_POST["action"]=="saveComment") {if(empty($_POST["comment"])) die("0");$comment=$_POST["comment"];$parent=$_POST["parent"];$user=$_POST["user"];$file = new SimpleXMLElement("../".xmlFile, null, true);$numItems=0;
foreach ($file->entrada as $entrada) {if ( (int)$entrada->id > $numItems){$numItems=(int)$entrada->id;}};$numItems++;$entrada = $file->addChild("entrada");$entrada->addChild("id",$numItems);$entrada->addChild("father",$parent);$entrada->addChild("usr",$user);$entrada->addChild("comment",trim($comment));$entrada->addChild("dt",date("d-m-Y H:i:s"));$file->asXML("../".xmlFile);echo $numItems;}
if ($_POST["action"] == "deleComment") {$id=$_POST["id"];$user=$_POST["user"];$numItems=0;$file=new SimpleXMLElement("../".xmlFile, null, true);$cadena='//father[.="'.$id.'"]/ancestor::entrada';$result = $file->xpath($cadena);$errlevel = error_reporting(E_ALL & ~E_WARNING);
foreach ($result as $r) unset ($r[0]);$cadena='//id[.="'.$id.'"]/ancestor::entrada';$result = $file->xpath($cadena);foreach ($result as $r) unset ($r[0]);error_reporting($errlevel);$file->asXML("../".xmlFile);echo $numItems;}
function showComment($arr,$user){if ($user==""){if (empty(trim($arr["usr"]))){echo '<div class="waveComment com-'.$arr['id'].'"><div class="commentP">';} else {echo '<div class="waveComment com-'.$arr['id'].'"><div class="comment">';};if (!empty(trim($arr["usr"]))){echo '<div class="waveTime">'.waveTime($arr['dt']).'</div>';
echo '<div class="commentAvatar">';};if (file_exists("ardoraWorkFiles/avatars/".$arr['usr'].".jpg")){$arquivo="ardoraWorkFiles/avatars/".$arr['usr'].".jpg";}else{$arquivo="ardoraWorkFiles/avatars/avatar.png";}
if (empty(trim($arr["usr"]))){echo '<div class="commentTextP"><span class="name"></span> '.$arr['comment'].'</div><div class="clear"></div></div>';}else{echo '<img src="'.$arquivo.'" width="30" height="30" alt="'.$arr['usr'].'" />';
echo '</div><div class="commentText"><span class="name">'.$arr['usr'].':</span> '.$arr['comment'].'</div><div class="clear"></div></div>';}}else{
if (empty(trim($arr["usr"]))){echo '<div class="waveComment com-'.$arr['id'].'"><div class="commentP">';}
else{echo '<div class="waveComment com-'.$arr['id'].'"><div class="comment">';}
if (!empty(trim($arr["usr"]))){echo '<div class="waveTime">'.waveTime($arr['dt']).'</div>';echo '<div class="commentAvatar">';}
if (file_exists("../ardoraWorkFiles/avatars/".$arr['usr'].".jpg")){$arquivo="../ardoraWorkFiles/avatars/".$arr['usr'].".jpg";}else{$arquivo="../ardoraWorkFiles/avatars/avatar.png";}
if (empty(trim($arr["usr"]))){echo '<div class="commentTextP"><span class="name"></span> '.$arr['comment'].'</div><div class="replyLink"><button class="ui-button ui-widget ui-state-default ui-corner-all"  onclick="addComment(this,'.$arr['id'].',\''.$user.'\')" style="margin-bottom:5px; margin-left:5px;"><span class="ui-icon ui-icon-plusthick"></span></button></div>';}
else{echo '<img src="'.$arquivo.'" width="30" height="30" alt="'.$arr['usr'].'" />';echo   '</div><div class="commentText"><span class="name">'.$arr['usr'].':</span> '.$arr['comment'].'</div><div class="replyLink">
<button class="ui-button ui-widget ui-state-default ui-corner-all"  onclick="addComment(this,'.$arr['id'].',\''.$user.'\')" style="margin-bottom:5px; margin-left:5px;"><span class="ui-icon ui-icon-plusthick"></span></button></div>';}
if ($user==$arr['usr'] || dameValor($user, "TIP")=="profe" || dameValor($user, "TIP")=="admin"){ echo '<div style="float:right"><button class="ui-button ui-widget ui-state-default ui-corner-all"  onclick="delComment(this,'.$arr['id'].',\''.$user.'\')" style="margin-bottom:5px; margin-left:5px;"><span class="ui-icon ui-icon-minusthick"></span></button></div>';} echo '<div class="clear"></div></div>';}
if($arr["replies"]){foreach($arr["replies"] as $r) showComment($r,$user);}echo "</div>";}
function waveTime($t){$t = strtotime($t); if(date('d')==date('d',$t)) return date('h:i A',$t); return date('F jS Y h:i A',$t);}
?>
