Monthly Archives widget

And I have added the Monthly Archives widget to the blog demo. The code I have added is very concise since I made use of existing code such as Calendar widget. Followings are all the code that is added.

As usual, you can download it from the google code repositry and can try the demo out. Have fun :)

protected/components/MonthlyArchives.php:

<?php


class MonthlyArchives extends Portlet


{


  public $title='Monthly Archives';





  public function findAllPostDate()


  {


    $yearmonth = array();


    $posts = Post::model()->findRecentPosts(PHP_INT_MAX);


    foreach ($posts as $post) {


      $ym = date('M, Y', $post->createTime);


      if (!isset($yearmonth[$ym])) {


        $yearmonth[$ym] = 1;


      } else {


        $yearmonth[$ym]++;


      }


    }


    return $yearmonth;


  }





  protected function renderContent()


  {


    $this->render('monthlyArchives');


  }


}

protected/components/views/monthlyArchives.php:

<ul>


<?php foreach ($this->findAllPostDate() as $month=>$val): ?>


<li>


<?php echo CHtml::link("$month ($val)", CHtml::normalizeUrl(array('post/PostedInMonth',


                                                                  'time'=>strtotime($month),


                                                                  'pnc'=>'c')));  ?>


</li>


<?php endforeach; ?>


</ul>

I've found a bug.



$ym = date('M, Y', $post->createTime);


should be



$ym = date('F Y', $post->createTime);


in order for strtotime() function to work correctly. The SVN has already been fixed.