Aclarando que ya tenemos que tener configurado el foro de MyBB, pueden ver en el enlace que se dejo
arriba como hacerlo, luego en un proyecto creado con YiiFramework generamos el modelo Threads con la herramienta Gii de YiiFramework, también necesitaremos generar el modelo Posts ambos extienden del Active Record de Yii, comenzamos a trabajar en nuestro código
Lo primero es hacer la relación entre ambos models/Threads.php
/** * @return array relational rules. */ public function relations() { // NOTE: you may need to adjust the relation name and the related // class name for the relations automatically generated below. return array( 'posts' => array(self::HAS_MANY, 'posts', 'tid'), ); }models/Posts.php
/** * @return array relational rules. */ public function relations() { // NOTE: you may need to adjust the relation name and the related // class name for the relations automatically generated below. return array( 'threads' => array(self::BELONGS_TO, 'Threads', 'tid'), ); }
De esta manera tenemos la relación, una vez establecemos esto, realizamos el método hotpost, que es el que nos da los datos que necesitamos
/** * hotThread return the hot threads of the foro * @param string $type [description] * @param integer $length [description] * @return [type] [description] */ public function hotThread($type = 'views', $length = 5) { if($type == 'views') { //shows the hotpost for views $criteria = new CDbCriteria(); $criteria->select = 'tid, subject, views, dateline, username'; $criteria->with = array( 'posts'=>array( 'joinType'=>'LEFT JOIN', 'order' => 'pid', ), ); $criteria->order = 'views DESC'; $criteria->together = true; $criteria->limit = $length; $dataProvider = new CActiveDataProvider($this, array( 'criteria' => $criteria, 'pagination'=>array( 'pageSize'=>20, ), )); } elseif($type == 'answerd') { //show the hotpost by answerd $criteria = new CDbCriteria(); $criteria->select = 'tid, COUNT(posts.tid) as length, subject, views, dateline, username'; $criteria->order = 'length DESC'; $criteria->with = array( 'posts'=>array( 'joinType'=>'RIGHT JOIN', 'order' => 'pid', ), ); $criteria->together = true; $criteria->group = 'posts.tid'; $criteria->limit = $length; $dataProvider = new CActiveDataProvider($this, array( 'criteria' => $criteria, )); } return $dataProvider; }
Como vemos estamos obteniendo un nuevo atributo para la categoría por respuesta, entonces tenemos que agregar a nuestra clase Threads el atributo length
class Threads extends CActiveRecord { public $length; /*...*/Ya que obtuvimos los hotpost por su respectiva categoria (visitas y respuestas), ahora realizaremos el controller, controllers/SiteController.php
public function actionHotpost() { $threads = new Threads(); $dataProviderVisitPost = $threads->hotThread('views'); $dataProviderMostAnswerdPost = $threads->hotThread('answerd'); $this->render('index-test',array( 'dataProviderVisitPost'=>$dataProviderVisitPost, 'dataProviderMostAnswerdPost' => $dataProviderMostAnswerdPost) ); }
Ahora, hacemos la vista, esta consta de un CListView,
views/site/index-test.php
pageTitle=Yii::app()->name; ?> h1=""> titulo de la pagina /h1> h2=""> Temas más Visitados widget('zii.widgets.CListView', array( 'dataProvider'=>$dataProviderVisitPost, 'itemView'=>'_hotpostitem', ));?> h2=""> Con mayor numero de respuesta /h2> widget('zii.widgets.CListView', array( 'dataProvider'=>$dataProviderMostAnswerdPost, 'itemView'=>'_hotpostitemvisit', ));?>
Disculpen los horrores en las etiquetas html, no le tengo instalado plugin para codigo al blog, por eso no esta el principio <.
Ahora solo nos queda realizar el código de
views/site/_hotpostitem.php
div class="view"> h2>subject); ?>/h2> span>Creado por: username);?> en la fecha dateline));?>/span> p>Visto: views); ?>/p> p>posts[0]->message, 0, 350)); ?>/p> a href="http://localhost/mybb/Upload/showthread.php?tid=tid)?>" alt="leer mas">Leer mas/a> /div>Y de views/site/_hotpostitemvisit.php
div class="view"> h2>subject); ?>/h2> span>Creado por: username);?> en la fecha dateline));?>/span> p>Cantidad de Respuestas: length - 1); ?>/p> p>posts[0]->message, 0, 350)); ?>/p> a href="http://localhost/mybb/Upload/showthread.php?tid=tid)?>" alt="leer mas">Leer mas/a> /div>Espero que este sirva de agrado a los que lo lean, el código pronto estará en github, en la cuenta de yienespanol y en la de mi persona, saludos