Mostrar últimos comentarios de CuteNews

Iniciado por Xt3mP, Abril 28, 2012, 05:02:21 PM

Tema anterior - Siguiente tema

0 Miembros y 1 Visitante están viendo este tema.

Abril 28, 2012, 05:02:21 PM Ultima modificación: Octubre 25, 2013, 11:37:01 PM por Expermicid
CuteNews es un sistema de gestión de noticias muy fácil de usar y de implementar en la mayoría de los proyectos a nivel web. Sin embargo, a la hora de tratar de mostrar los últimos comentarios se dificulta un poco por lo que he creado un pequeño "hack" para facilitar ésta tarea; claro, como todo script tiene algunos contras:

    Pro
        Muestra los comentarios a base de un template sumamente editable.
    Contra
        Se deberan mudar los comentarios manualmente.

Lo primero que debemos hacer es dirigirnos a la línea 95 del archivo show.inc.php de la carpeta inc y sustituír:

Código: php
$name = trim($name);
$mail = trim($mail);
$id = (int) $id;  // Yes it's stupid how I didn't thought about this :/


Por:

Código: php
$name = trim($name);
$namec = trim($name); #Don't edit this (at: Xt3mP)
$mail = trim($mail);
$id = (int) $id;  // Yes it's stupid how I didn't thought about this :/
$commentc = $comments; #Don't edit this (at: Xt3mP)


Después en la línea 342 (contando que ya modificamos lo anterior) agregamos:

Código: php
//============================
//   Last comments by Xt3mP
//============================
$newComments = @fopen('data/newComments.txt', 'a+');
@fwrite($newComments, $mail.'|'.$namec.'|'.$commentc.'|'.$id.'|'.$time."\r\n");
@fclose($newComments);


Con ésto sólo tendríamos el sistema que cada vez que se comente correctamente, abrirá el archivo newComments.txt en la carpeta data y agregará el comentario para posteriormente procesarlo y mostrarlo.

Ahora sólo queda crear el archivo show_comments.php en la raíz del sistema con el siguiente código PHP:

Código: php
<?php
/*
* Show comments from CuteNews 1.0
* Author: Xt3mP
* Author website: http://xt3mp.mx
* Contact: [email protected]
* Tested on: CuteNews 1.4.6
*/
//============================
//       Configuration
//============================
#Where do you've installed cutenews?
$basePath = 'http://localhost/projects/works/Habbostorm/noticias/';
#Where do you've the news show file?
$baseFile = 'show_news2.php';
#The show template
    #{url} = News' link
    #{comment} = The comment (oh really?)
    #{name} = The author
    #{date} = Time of the post
$template = '<a href="{url}">{name}</a>:
{comment} - {date}
';
#How many comments wanna show?
$maxComments = 6;
#How length need to be any comment?
$maxLenght = 10;
#The "hack" comment file {don't edit this}
#============================
#  Process {don't edit this}
#============================
$dataContent = file_get_contents('data/newComments.txt');
$comments = explode("\r\n", $dataContent);
$from = count($comments) - ($maxComments + 1);
$to = count($comments);
$showComments = array();
for($i = $from; $i < $to;  $i++) {   $newTemplate = $template; //Stupid fix?     $comment = explode('|', $comments[$i]);     if(!empty($comment[0]))     {       $newTemplate = str_replace('{url}', 'show_news2.php?subaction=showcomments&template=&id='.$comment[3].'&archive=&start_from=&ucat=', $newTemplate);         if(strlen($comment[2]) > $maxLenght)
            $newTemplate = str_replace('{comment}', htmlentities(substr($comment[2], 0, $maxLenght)).'...', $newTemplate);
        else
            $newTemplate = str_replace('{comment}', htmlentities($comment[2]), $newTemplate);
        $newTemplate = str_replace('{name}',  htmlentities($comment[1]), $newTemplate);
        $newTemplate = str_replace('{date}', date('d M Y h:i a', $comment[4]), $newTemplate);
        $showComments[] = $newTemplate;
    }
}
#Reverse array way
krsort($showComments);
#Print array
foreach($showComments as $comments)
    echo $comments;
?>


Con ésto sólo bastaría tener un iframe de la siguiente forma:

Código: html
<iframe src="show_comments.php" width="200" height"100"></iframe>


La forma de editar el template se basa en la línea 21 del archivo show_comments.php el cual puede ser de varias formas:

Código: php
$template = '<a href="{url}">{name}</a>:<br /> {comment} - {date}<br />';
$template = '<a href="{url}">{comment}</a>:<br /> {date}<br />';
$template = '<a href="{url}">{name}</a>:<br /> {comment}<br />';
$template = '<div id="comment"><div class="name"><a href="{url}">{name}</a>:</div><div class="comment">{comment}</div></div>';


Inclusive ustedes pueden agregar sus divs como en el último ejemplo. Para mudar los comentarios sólo deben seguir el formato:

Código: html
correo|autor|comentario|id_noticia|timestamp(tiempo)


y guardarlos en el archivo newComments.txt.

Es todo, cualquier duda, comentario o sugerencia comenten.
Cada vez que me das Karma me motivas