Search code examples
magento-1.7blogsarchive

archive functionality for magento aw_blog extension


I have been working with a client who wanted a blog in the magento website. After installing the AW blog extension i realized that it doesn't have archive functionality, and i didn't find any solution in the web.

After few days of work, I have finally found a solution for this task. I didn't know where to post it, and I thought that this is the best place to share it.

I know that people usually don't post answers here, but I have to share, because out there, somebody is having difficult time, or might have.


Solution

  • I will presume that you already installed the AW Blog extension to your magento project. blog extension page

    1) first of all we need a placeholder for future datepicker. I choose the sidebar of AW_blog extension: app\design\frontend\YOUR_THEME\default\template\aw_blog\menu.phtml

    I added following html markup almost to the end:

    <div class="block-content">
        <div class="menu-recent">
            <h5 class="uppercase"><?php echo Mage::helper('blog')->__('Archives'); ?></h5>
            <div id="datepicker"></div>
        </div>  
    </div>
    

    2) After this i added jquery ui datepicker into project. we will need it for date selection. jquery ui page. Here I need make some note. I only copied the jquery-ui.js, jquery-ui.css, images folder with theme icons. I did this because i didn't need anything else. you can download everything if you want, but the key files are jquery-ui.js, jquery-ui.css, images folder.

    a) copy jquery-ui.js into skin\js directory

    b) copy jquery-ui.css and images folder into skin\frontend\YOUR_THEME\default\css directory

    3) Add following code to app\design\frontend\default\default\layout\page.xml

    <default translate="label" module="page">
        <label>All Pages</label>
        <block type="page/html" name="root" output="toHtml" template="page/3columns.phtml">
            <block type="page/html_head" name="head" as="head">
                ....
                <action method="addJs"><script>jquery-ui.js</script></action>
                ...
                <action method="addCss"><stylesheet>css/jquery-ui.css</stylesheet></action>
    

    4) add following code to app\design\frontend\YOUR_THEME\default\template\page\head.phtml this is just simple work with datepicker plugin

    jQuery(function(){
        jQuery( "#datepicker" ).datepicker({
            dateFormat:"yy-mm-dd",
            onSelect: function(dateText) {
                window.location = '<?php echo Mage::getBaseUrl();?>news/?date='+dateText;//refreshes screen
            }
        });
    });
    

    all these four 4 steps are for frontend stuff. now we will go to the backend programming

    1) open app\code\community\AW\Blog\Block\blog.php and replace getPosts() function with following code

    public function getPosts()
    {
        $collection = parent::_prepareCollection();
    
        $tag = $this->getRequest()->getParam('tag');
    $date = $this->getRequest()->getParam('date');//gets $_GET[date] parameter
    
        if ($tag) {
            $collection->addTagFilter(urldecode($tag));
        }
    else if ($date) { // checks if $date parameter exists
            $collection->addDateFilter(urldecode($date));
        }
        parent::_processCollection($collection);
    
        return $collection;
    }
    

    2) open app\code\community\AW\Blog\Model\Mysql4\Blog\Collection.php and add following function

    public function addDateFilter($date) {
        if ($date = trim($date)) {
            $whereString = sprintf("main_table.created_time >= %s", $this->getConnection()->quote($date));
            $this->getSelect()->where($whereString);
        }
        return $this;
    }
    

    This is it guys. If you need any help, just let me know. Thanks for your time.