Skip to content

Commit

Permalink
SITE: improved get_comment action and made it callable through ajax.php
Browse files Browse the repository at this point in the history
  • Loading branch information
Asimov4 committed Apr 7, 2012
1 parent bd61831 commit 1f56ca7
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 11 deletions.
10 changes: 6 additions & 4 deletions SITE/ajax.php
Expand Up @@ -25,7 +25,9 @@

include_once("config.php");
include_once("script_php/pages_secondlevel/actions.php");
include_once("script_php/pages_secondlevel/comments.php");

$privileges = user_privilege_level();
$login = $_SESSION['login_c'];


// Demande
Expand All @@ -39,11 +41,11 @@
{
// appel de la fonction post par Ajax
case "post":
post($_POST['title'],$_POST['message'],$_POST['anonymization'],$_POST['category'],$_SESSION['login_c'],$valid=0,$output='JSON');
post($_POST['title'],$_POST['message'],$_POST['anonymization'],$_POST['category'],$login,$valid=0,$output='JSON');
break;

case "unrollcomment":
affichage_comments(27);
case "comments":
get_comments(27,$privileges,$login,$output='JSON');
break;

// Message d'erreur
Expand Down
12 changes: 8 additions & 4 deletions SITE/examples/example_unrollcomments.php
Expand Up @@ -27,7 +27,7 @@

<body>

<a class='speccom' href="?action=unrollcomment&amp;order=0&amp;thread_id=27#a27">
<a class='speccom' href="?action=comments&amp;order=0&amp;thread_id=27#a27">
<span class="newslinkcomment_roll">2 comments</span>
</a>

Expand Down Expand Up @@ -56,11 +56,15 @@
url: url,
success: function(rep)
{
$('.newsformcomment').html(rep);
var display = '<ul>';
var callback = jQuery.parseJSON(rep);
$.each(callback.DATA, function(key,comment) {
display = display + '<li>' + comment.text + ' - ' + comment.possibly_name + ' - ' + comment.date + '</li>';
});
display = display + '</ul>';
$('.newsformcomment').html(display);
}
});



return false;

Expand Down
69 changes: 68 additions & 1 deletion SITE/script_php/pages_secondlevel/actions.php
Expand Up @@ -25,6 +25,7 @@
include_once("tool.php");
include_once("errors.php");
include_once("votes.php");
include_once("comments.php");

/**
* class for action functions
Expand All @@ -34,6 +35,7 @@ class action {
var $result = False; // Result of the action
var $warnings = array(); // list of generated warnings
var $successes = array(); // list of generated successes
var $data = array();

// set result
function set_result($result) {
Expand Down Expand Up @@ -66,7 +68,7 @@ function echo_successes() {

// display all results in JSON format
function output_result($output) {
$array = array( 'RESULT' => $this->result, 'WARNINGS' => $this->warnings, 'SUCCESSES' => $this->successes );
$array = array( 'RESULT' => $this->result, 'WARNINGS' => $this->warnings, 'SUCCESSES' => $this->successes, 'DATA' => $this->data );

if ($output == 'JSON') {
echo json_encode($array);
Expand Down Expand Up @@ -164,5 +166,70 @@ function post($title,$message,$anonymization,$category,$login,$valid=0,$output='
return $action;
}


/**
* returns a list of comments for a given thread id
*
* @param string $title title of the idea
* @param string $message message of the idea
* @param string $anonymization tells if idea is to be anonymized
* @param integer $category id of the category for the idea
* @param string $login login of the poster
* @param integer $valid says if the idea needs to be moderated (default 0 = needs moderation)
* @return array
*/
function get_comments($thread_id,$privileges,$login,$output='') {

$action = new action;
$action->set_result(False);

$escaped_threadid=mysql_real_escape_string($thread_id);
$escaped_name=mysql_real_escape_string($login);
$result=@mysql_query(sprintf("(SELECT C.comment_id,C.rand_prop,C.hash_prop,C.text,C.date,C.is_valid,C.already_mod,C.possibly_name,
SUM(V.vote) AS pro_vote, COUNT(V.vote) AS total_vote,
MAX(CAST(SHA1(CONCAT('%s',CAST(V.rand_prop AS CHAR))) AS CHAR)=V.hash_prop) AS my_vote,
MAX(CAST(SHA1(CONCAT('%s',CAST(V.rand_prop AS CHAR))) AS CHAR)=V.hash_prop AND V.vote) AS my_provote
FROM comment C, vote_comment V
WHERE C.thread_id='%s' AND V.comment_id=C.comment_id
GROUP BY C.comment_id,C.rand_prop,C.hash_prop,C.text,C.date,C.is_valid,C.already_mod,C.possibly_name)
UNION
(SELECT C.comment_id,C.rand_prop,C.hash_prop,C.text,C.date,C.is_valid,C.already_mod,C.possibly_name,
0 AS pro_vote, 0 AS total_vote,0 AS my_vote, 0 AS my_provote
FROM comment C
WHERE C.thread_id='%s' AND C.comment_id<>ALL(SELECT comment_id FROM vote_comment))
ORDER BY date ASC",$escaped_name,$escaped_name,$escaped_threadid,$escaped_threadid));



if($result)
{
while($row=mysql_fetch_assoc($result))
{
$is_proprio=check_property($row["rand_prop"],$row["hash_prop"]);
$is_valid=$row["is_valid"];

if ($is_valid || $is_proprio || $privileges>3)
{
$comment = array();
$comment['is_proprio'] = check_property($row["rand_prop"],$row["hash_prop"]);
$comment['is_valid'] = $row["is_valid"];
$comment['already_mod'] = $row["already_mod"];
$comment['date'] = $row['date'];
$comment['possibly_name'] = $row['possibly_name'];
$comment['text'] = text_display_prepare(trim($row["text"]));
$comment['my_vote'] = $row['my_vote'];
$comment['my_provote'] = $row['my_provote'];
$comment['total_vote'] = $row['total_vote'];

$action->data[$row["comment_id"]] = $comment;

}
}
}

$action->output_result($output);
return $action;
}

?>

4 changes: 2 additions & 2 deletions SITE/script_php/pages_secondlevel/comments.php
Expand Up @@ -772,7 +772,7 @@ function display_speccom($unique_mode,$ancre,$thread_id,$nb_comment,$roll) {
}

// display a comment with all its wrapper
function display_comment($row,$is_logged,$privileges,$is_admin,$unique_mode) {
function display_comment($row,$is_logged,$privileges,$unique_mode) {

$is_proprio=check_property($row["rand_prop"],$row["hash_prop"]);
$is_valid=$row["is_valid"];
Expand Down Expand Up @@ -1047,7 +1047,7 @@ function affichage_comments($thread_id,$moderation_mode=false,$unique_mode=false
{

// afficher les commentaires
display_comment($row,$is_logged,$privileges,$is_admin,$unique_mode);
display_comment($row,$is_logged,$privileges,$unique_mode);

}
}
Expand Down

0 comments on commit 1f56ca7

Please sign in to comment.