How to add "Calender" widget to the blog demo

Since original blog demo has been changed, I would like to clarify the difference (patch) of my calendar component to the original blog demo. My previous article was http://www.yiiframew…61.html#msg3561.

Screen shot:

Posted Image

download several files

  • Download a component of calender portlet under blog/protected/components/.

  • Download a css for calender portlet under blog/protected/components/css/.

  • Download a view for calender portlet under blog/protected/components/views/.

  • Download a generator for calender portlet under blog/protected/components/views/.

> Note: If you would like to use Japanese version, download this. Also modify blog/protected/components/views/calendar.php as below.



[php]


// Set locale


$locale='ja_JP.utf8';


$setlocale = setlocale(LC_ALL,  $locale);





// load the algorithm of the calendar


include_once('generate_calendar_Japan.php');


  • Download a view of the page for the posts on a certain date under blog/protected/views/post/.

  • Download a view of the page for the posts in a certain month under blog/protected/views/post/.

Modify several files

  • Modify your config file of blog/protected/config/main.php as below.


[php]


   'posts'=>'post/list',


   'post/<id:\d+>'=>'post/show',


   'post/update/<id:\d+>'=>'post/update',


+    'date/<time:\d+>'=>'post/PostedOnDate',


+    'month/<time:\d+>/<pn:p|c|n>'=>'post/PostedInMonth',


  • Add access rule and two methods (actionPostedOnDate() and actionPostedInMonth()

) in blog/protected/controllers/PostController.php.



[php]


- 'actions'=>array('list','show','captcha'),


+ 'actions'=>array('list','show','captcha', 'PostedInMonth','PostedOnDate'),




[php]


        /**


         * Collect posts issued on specific date


         */


        public function actionPostedOnDate()


        {


          $criteria=new CDbCriteria;


          $criteria->condition='status='.Post::STATUS_PUBLISHED;


          $criteria->order='createTime DESC';





          $criteria->condition.=' AND createTime > :time1 AND createTime < :time2';


          $month = date('n', $_GET['time']);


          $date = date('j', $_GET['time']);


          $year = date('Y', $_GET['time']);


          $criteria->params[':time1']= $theDay = mktime(0,0,0,$month,$date,$year);


          $criteria->params[':time2']= mktime(0,0,0,$month,$date+1,$year);





          $pages=new CPagination(Post::model()->count($criteria));


          $pages->pageSize=Yii::app()->params['postsPerPage'];


          $pages->applyLimit($criteria);





          $posts=Post::model()->with('author')->findAll($criteria);





          $this->render('date',array(


                                     'posts'=>$posts,


                                     'pages'=>$pages,


                                     'theDay'=>$theDay,


                                     ));


        }





        /**


         * Collect posts issued in specific month


         */


        public function actionPostedInMonth()


        {


          $criteria=new CDbCriteria;


          $criteria->condition='status='.Post::STATUS_PUBLISHED;


          $criteria->order='createTime DESC';





          $criteria->condition.=' AND createTime > :time1 AND createTime < :time2';


          $month = date('n', $_GET['time']);


          $year = date('Y', $_GET['time']);


          if ($_GET['pn'] == 'n') $month++;


          if ($_GET['pn'] == 'p') $month--;


          $criteria->params[':time1']= $firstDay = mktime(0,0,0,$month,1,$year);


          $criteria->params[':time2']= mktime(0,0,0,$month+1,1,$year);





          $pages=new CPagination(Post::model()->count($criteria));


          $pages->pageSize=Yii::app()->params['postsPerPage'];


          $pages->applyLimit($criteria);





          $posts=Post::model()->with('author')->findAll($criteria);





          $this->render('month',array(


                                      'posts'=>$posts,


                                      'pages'=>$pages,


                                      'firstDay'=> $firstDay,


                                      ));


        }


  • Add following method to blog/protected/models/Post.php.


[php]


        /**


         * Find articles posted in this month


         * @return array the artcles posted in this month


         */


        public function findArtclePostedThisMonth()


        {


          if (!empty($_GET['time'])) {


            $month = date('n', $_GET['time']);


            $year = date('Y', $_GET['time']);


            if (!empty($_GET['pn']) && $_GET['pn'] == 'n') $month++;


            if (!empty($_GET['pn']) && $_GET['pn'] == 'p') $month--;


          } else {


            $month = date('n');


            $year = date('Y');


          }





          $criteria=array(


                          'condition'=>'createTime > :time1 AND createTime < :time2


                                        AND Post.status='.self::STATUS_PUBLISHED,


                          'params'=>array(':time1' => mktime(0,0,0,$month,1,$year),


                                          ':time2' => mktime(0,0,0,$month+1,1,$year),


                                          ),


                          'order'=>'Post.createTime DESC',


                          );


          return $this->findAll($criteria);


        }


  • Place calendar component on the page. Add following line to blog/protected/views/layouts/main.php.


[php]


    <?php $this->widget('RecentComments'); ?>





+    <?php $this->widget('Calendar'); ?>





Have fun. ;)


[Updated: Feb 20, 2009]

  • Modified the patch to blog/protected/models/Post.php as below.


-                          'condition'=>'createTime > :time1 AND createTime < :time2',


+                          'condition'=>'createTime > :time1 AND createTime < :time2


+                                        AND Post.status='.self::STATUS_PUBLISHED,



[Updated: Feb 22, 2009]

Collection of the enhancements:

http://code.google.c…gdemo-enhanced/


[Updated: Feb 25, 2009]

Source updated in order to support non path URL format.

http://code.google.c…gdemo-enhanced/

Since this is the first widget that I had created ago, I have re-organised this Calendar widget so as to move the working code from the view to the widget.

The latest version as of today (June 21, 2009) is r41. Please get the code there.

why are links not working right? The link mask appears as http://www.yiiframew…n#dateFormatter. Well doesn’t matter how it looks but underlined link is same as well.

Please see this issue. http://code.google.com/p/yii/issues/detail?id=540#c1