功能介绍:
安装好Magento插件后,需要把AW_Blog的前端显示方式要改变,变成类似选项卡的展示效果,先看下设计大体效果如下图:
图片描述
那么,改如何修改插件呢?
还是和往常一样,新建一个模块,包含Block、controllers、etc、sql、Model文件夹
图片描述
编写配置文件config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Test_Lesson>
            <version>0.1.0</version>
        </Test_Lesson>      
    </modules>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                    <!--rewrite backendcontroller-->
                        <Test_Lesson before="Aw_Adminhtml">Test_Lesson_Adminhtml</Test_Lesson>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
    <frontend>
        <routers>
            <lesson>
                <use>standard</use>
                <args>
                    <module>Test_Lesson</module>
                    <frontName>lesson</frontName>
                </args>
            </lesson>
        </routers>
        <layout>
            <updates>
                <lesson>
                    <file>test/lesson.xml</file>
                </lesson>
            </updates>
        </layout>
        <translate>
            <modules>
                <Test_Lesson>
                    <files>
                        <default>Test_Lesson.csv</default>
                    </files>
                </Test_Lesson>
            </modules>
        </translate>
    </frontend>  
    <global>
        <resources>
        <!--资源模型,与sql/test_lesson_setup对应-->
            <test_lesson_setup>
                <setup>
                   <module>Test_Lesson</module>                 
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </test_lesson_setup>
            <test_lesson_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </test_lesson_write>
            <test_lesson_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </test_lesson_read>
        </resources>
        <models>
            <lesson>
                <class>Test_Lesson_Model</class>
            </lesson>
        </models>
        <blocks>
            <lesson>
                <class>Test_Lesson_Block</class>
            </lesson>         
               <blog>
                 <rewrite>
                     <!-- rewrite block -->
                     <manage_blog_edit_tab_form>Test_Lesson_Block_Manage_Blog_Edit_Tab_Form</manage_blog_edit_tab_form>
                     <manage_blog_grid>Test_Lesson_Block_Manage_Blog_Grid</manage_blog_grid>    
                 </rewrite>  
            </blog>
        </blocks>
        <helpers>
            <lesson>
                <class>Test_Lesson_Helper</class>
            </lesson>
        </helpers>
    </global>
</config>

配置好配置文件,来重写Grid.php:

//路径Test/Lesson/Block/Manage/Blog/Grid.php
<?php
class Test_Manage_Blog_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct()
    {
        parent::__construct();
        $this->setId('blogGrid');
        $this->setDefaultSort('created_time');
        $this->setDefaultDir('desc');
        $this->setSaveParametersInSession(true);
    }

    protected function _getStore()
    {
        $storeId = (int)$this->getRequest()->getParam('store', 0);
        return Mage::app()->getStore($storeId);
    }

    protected function _prepareCollection()
    {
        $collection = Mage::getModel('blog/blog')->getCollection();
        $store = $this->_getStore();
        if ($store->getId()) {
            $collection->addStoreFilter($store);
        }
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

    protected function _prepareColumns()
    {
        $this->addColumn(
            'post_id',
            array(
                 'header' => Mage::helper('blog')->__('ID'),
                 'align'  => 'right',
                 'width'  => '50px',
                 'index'  => 'post_id',
            )
        );

        $this->addColumn(
            'title',
            array(
                 'header' => Mage::helper('blog')->__('Title'),
                 'align'  => 'left',
                 'index'  => 'title',
            )
        );
        /* add image and content */
        $this->addColumn(
                'imagepath',
                array(
                        'header' => Mage::helper('blog')->__('Image'),
                        'align'  => 'left',
                        'index'  => 'imagepath',    
                        'width'     => '10',
                        'renderer' =>'Test_Lesson_Block_Adminhtml_Template_Grid_Renderer_Image',
                )
        );
        
        $this->addColumn(
                'short_content',
                array(
                        'header' => Mage::helper('blog')->__('Profile'),
                        'align'  => 'left',
                        'index'  => 'short_content',
                )
        );
        
        $this->addColumn(
                'endtime',
                array(
                        'header' => Mage::helper('blog')->__('Endtime'),
                        'align'  => 'left',
                        'width'  => '100px',
                        'index'  => 'endtime',
                        'type'   => 'date',
                        //'format' => Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT),
                )
        );
        /* end */
/*
        $this->addColumn(
            'identifier',
            array(
                 'header' => Mage::helper('blog')->__('Identifier'),
                 'align'  => 'left',
                 'index'  => 'identifier',
            )
        );*/

/*        $this->addColumn(
            'user',
            array(
                 'header' => Mage::helper('blog')->__('Poster'),
                 'width'  => '150px',
                 'index'  => 'user',
            )
        );*/

/*        $this->addColumn(
            'created_time',
            array(
                 'header'    => Mage::helper('blog')->__('Created at'),
                 'index'     => 'created_time',
                 'type'      => 'datetime',
                 'width'     => '120px',
                 'gmtoffset' => true,
                 'default'   => ' -- '
            )
        );

        $this->addColumn(
            'update_time',
            array(
                 'header'    => Mage::helper('blog')->__('Updated at'),
                 'index'     => 'update_time',
                 'width'     => '120px',
                 'type'      => 'datetime',
                 'gmtoffset' => true,
                 'default'   => ' -- '
            )
        );*/

        $this->addColumn(
            'status',
            array(
                 'header'  => Mage::helper('blog')->__('Status'),
                 'align'   => 'left',
                 'width'   => '80px',
                 'index'   => 'status',
                 'type'    => 'options',
                 'options' => array(
                     1 => Mage::helper('blog')->__('Enabled'),
                     2 => Mage::helper('blog')->__('Disabled'),
                     3 => Mage::helper('blog')->__('Hidden'),
                 ),
            )
        );

        $this->addColumn(
            'action',
            array(
                 'header'    => Mage::helper('blog')->__('Action'),
                 'width'     => '100px',
                 'type'      => 'action',
                 'getter'    => 'getId',
                 'actions'   => array(
                     array(
                         'caption' => Mage::helper('blog')->__('Edit'),
                         'url'     => array('base' => '*/*/edit'),
                         'field'   => 'id',
                     )
                 ),
                 'filter'    => false,
                 'sortable'  => false,
                 'index'     => 'stores',
                 'is_system' => true,
            )
        );

        return parent::_prepareColumns();
    }

    protected function _prepareMassaction()
    {
        $this->setMassactionIdField('post_id');
        $this->getMassactionBlock()->setFormFieldName('blog');

        $this->getMassactionBlock()->addItem(
            'delete',
            array(
                 'label'   => Mage::helper('blog')->__('Delete'),
                 'url'     => $this->getUrl('*/*/massDelete'),
                 'confirm' => Mage::helper('blog')->__('Are you sure?'),
            )
        );

        $statuses = Mage::getSingleton('blog/status')->getOptionArray();

        array_unshift($statuses, array('label' => '', 'value' => ''));
        $this->getMassactionBlock()->addItem(
            'status',
            array(
                 'label'      => Mage::helper('blog')->__('Change status'),
                 'url'        => $this->getUrl('*/*/massStatus', array('_current' => true)),
                 'additional' => array(
                     'visibility' => array(
                         'name'   => 'status',
                         'type'   => 'select',
                         'class'  => 'required-entry',
                         'label'  => Mage::helper('blog')->__('Status'),
                         'values' => $statuses,
                     )
                 )
            )
        );
        return $this;
    }

    public function getRowUrl($row)
    {
        return $this->getUrl('*/*/edit', array('id' => $row->getId()));
    }
}

打开后台如图所示:
图片描述
接下来来写Form.php

//路径Test/Lesson/Block/Manage/Blog/Edit/Tab/Form.php
<?php

class Test_Lesson_Block_Manage_Blog_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
    protected function _prepareForm()
    {
      
        $form = new Varien_Data_Form();
        $this->setForm($form);
        $fieldset = $form->addFieldset('blog_form', array('legend' => Mage::helper('blog')->__('Post information')));

        $fieldset->addField(
            'title',
            'text',
            array(
                 'label'    => Mage::helper('blog')->__('Title'),
                 'class'    => 'required-entry',
                 'required' => true,
                 'name'     => 'title',
            )
        );

        $noticeMessage = Mage::helper('blog')->__('e.g. domain.com/blog/<b>identifier</b>');

        $validationErrorMessage = addslashes(
            Mage::helper('blog')->__(
                "Please use only letters (a-z or A-Z), numbers (0-9) or symbols '-' and '_' in this field"
            )
        );

        $fieldset->addField(
            'identifier',
            'text',
            array(
                 'label'              => Mage::helper('blog')->__('Identifier'),
                 'class'              => 'required-entry aw-blog-validate-identifier',
                 'required'           => true,
                 'name'               => 'identifier',
                 'after_element_html' => '<span class="hint">' . $noticeMessage . '</span>'
                     . "<script>
                        Validation.add(
                            'aw-blog-validate-identifier',
                            '" . $validationErrorMessage . "',
                            function(v, elm) {
                                var regex = new RegExp(/^[a-zA-Z0-9_-]+$/);
                                return v.match(regex);
                            }
                        );
                        </script>",
            )
        );

        /**
         * Check is single store mode
         */
        if (!Mage::app()->isSingleStoreMode()) {
            $fieldset->addField(
                'store_id',
                'multiselect',
                array(
                     'name'     => 'stores[]',
                     'label'    => Mage::helper('cms')->__('Store View'),
                     'title'    => Mage::helper('cms')->__('Store View'),
                     'required' => true,
                     'values'   => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(false, true),
                )
            );
        }

        $categories = array();
        $collection = Mage::getModel('blog/cat')->getCollection()->setOrder('sort_order', 'asc');
        foreach ($collection as $cat) {
            $categories[] = (array(
                'label' => (string)$cat->getTitle(),
                'value' => $cat->getCatId()
            ));
        }

        $fieldset->addField(
            'cat_id',
            'multiselect',
            array(
                 'name'     => 'cats[]',
                 'label'    => Mage::helper('blog')->__('Category'),
                 'title'    => Mage::helper('blog')->__('Category'),
                 'required' => true,
                 'style'    => 'height:100px',
                 'values'   => $categories,
            )
        );

        $fieldset->addField(
            'status',
            'select',
            array(
                 'label'              => Mage::helper('blog')->__('Status'),
                 'name'               => 'status',
                 'values'             => array(
                     array(
                         'value' => 1,
                         'label' => Mage::helper('blog')->__('Enabled'),
                     ),
                     array(
                         'value' => 2,
                         'label' => Mage::helper('blog')->__('Disabled'),
                     ),
                     array(
                         'value' => 3,
                         'label' => Mage::helper('blog')->__('Hidden'),
                     ),
                 ),
                 'after_element_html' => '<span class="hint">'
                     . Mage::helper('blog')->__(
                         "Hidden pages won't be shown in blog but still can be accessed directly"
                     )
                     . '</span>',
            )
        );

        /*$fieldset->addField(
            'comments',
            'select',
            array(
                 'label'              => Mage::helper('blog')->__('Enable Comments'),
                 'name'               => 'comments',
                 'values'             => array(
                     array(
                         'value' => 0,
                         'label' => Mage::helper('blog')->__('Enabled'),
                     ),
                     array(
                         'value' => 1,
                         'label' => Mage::helper('blog')->__('Disabled'),
                     ),
                 ),
                 'after_element_html' => '<span class="hint">'
                     . Mage::helper('blog')->__(
                         'Disabling will close the post to new comments'
                     )
                     . '</span>',
            )
        );*/

        /* add image upload */
        
        $fieldset->addField(
                'imagepath',
                'file',
                array(
                        'name'               => 'imagepath',
                        //'required'           => true,
                        'label'              => Mage::helper('blog')->__('Image'),
                        'title'              => Mage::helper('blog')->__('Image')
                )
        );
         
        $fieldset->addField(
                'endtime',
                'date',
                 array(
                        'label'     => Mage::helper('blog')->__('Endtime'),
                        //'required'  => true,
                        'name'      => 'endtime',
                        'image'  => $this->getSkinUrl('images/grid-cal.gif'),
                        'format' => Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT),
                ));

        /*end*/

        
        $fieldset->addField(
            'tags',
            'text',
            array(
                 'name'               => 'tags',
                 'label'              => Mage::helper('blog')->__('Tags'),
                 'title'              => Mage::helper('blog')->__('tags'),
                 'style'              => 'width:700px;',
                 'after_element_html' => Mage::helper('blog')->__('Use comma as separator'),
            )
        );

        try {
            $config = Mage::getSingleton('cms/wysiwyg_config')->getConfig();
            $config->setData(
                Mage::helper('blog')->recursiveReplace(
                    '/blog_admin/',
                    '/' . (string)Mage::app()->getConfig()->getNode('admin/routers/adminhtml/args/frontName') . '/',
                    $config->getData()
                )
            );
        } catch (Exception $ex) {
            $config = null;
        }

        if (Mage::getStoreConfig('blog/blog/useshortcontent')) {
            $fieldset->addField(
                'short_content',
                'editor',
                array(
                     'name'   => 'short_content',
                     'label'  => Mage::helper('blog')->__('Short Content'),
                     'title'  => Mage::helper('blog')->__('Short Content'),
                     'style'  => 'width:700px; height:100px;',
                     'config' => $config,
                )
            );
        }
        

       /*  
        $fieldset->addField(
            'post_content',
            'editor',
            array(
                 'name'   => 'post_content',
                 'label'  => Mage::helper('blog')->__('Content'),
                 'title'  => Mage::helper('blog')->__('Content'),
                 'style'  => 'width:700px; height:500px;',
                 'config' => $config
            )
        );  */

         if (Mage::getSingleton('adminhtml/session')->getBlogData()) {
            $form->setValues(Mage::getSingleton('adminhtml/session')->getBlogData());
            Mage::getSingleton('adminhtml/session')->setBlogData(null);
        } elseif (Mage::registry('blog_data')) {
            Mage::registry('blog_data')->setTags(
                Mage::helper('blog')->convertSlashes(Mage::registry('blog_data')->getTags())
            );
            $form->setValues(Mage::registry('blog_data')->getData());
        } 
        return parent::_prepareForm();
    }
}

打开后台可以看到:
图片描述
这里需要注意的是,在Grid.php中有这么一句代码'renderer' =>'Test_Lesson_Block_Adminhtml_Template_Grid_Renderer_Image',涉及到图片的上传,所以,所以还需要新增一个Image.php

<?php
class Test_Lesson_Block_Adminhtml_Template_Grid_Renderer_Image extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
    public function render(Varien_Object $row)
    {
        //$val = Mage::helper('catalog/image')->init($row, 'thumbnail')->resize(97);
        //$out = "<img src=". $val ." width='97px'/>";
        //return $out;
        $html  = '<img ';
        $html .= 'id="' . $this->getColumn()->getId() . '" ';
        $html .= 'src="' . $row->getData($this->getColumn()->getIndex()) . '" ';
        $html .=  'width="80" height="60"';
        $html .= 'class="grid-image ' . $this->getColumn()->getInlineCss() . '"/>';
        return $html;
    }
}

接着,就需要保存,就需要重新后台控制器中的saveAction(方法,同时新增image上传.
路径:Test/Lesson/controllers/Adminhtml/Awblog/Manage/BlogController.php

<?php
require_once "AW/Blog/controllers/Adminhtml/Awblog/Manage/BlogController.php";//重写控制器,这句不能少
class Test_Lesson_Adminhtml_Awblog_Manage_BlogController extends AW_Blog_Adminhtml_Awblog_Manage_BlogController
{   
    /* rewrite AW_Blog saveAction*/
    public function saveAction()
    {
//        $data = $this->getRequest()->getPost();
//        print_r($data);
//        exit();
        if ($data = $this->getRequest()->getPost()) {
            $model = Mage::getModel('blog/post');
            /* add image upload */
            if(isset($_FILES['imagepath']['name']) and (file_exists($_FILES['imagepath']['tmp_name']))) {
                try {
                    $uploader = new Varien_File_Uploader('imagepath');
                    $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
            
                        $uploader->setAllowRenameFiles(true);
                                    
                        // setAllowRenameFiles(true) -> move your file in a folder the magento way
                        // setAllowRenameFiles(true) -> move your file directly in the $path folder
                        $uploader->setFilesDispersion(false);
                                    
                        $path = Mage::getBaseDir('media') . DS . 'awblog' . DS . date('Y-m-d');
                                    
                        $uploader->save($path, $_FILES['imagepath']['name']);
                        $url = Mage::getModel('core/config_data')->getCollection()
                        ->addfieldtofilter('path','web/secure/base_url');
                        $baseUrl = $url->getData();
                   
                        //$data['imagepath'] =$baseUrl[0]['value'] .'media/awblog/'. date('Y-m-d').'/'.$_FILES['imagepath']['name'];
                        $data['imagepath'] =$baseUrl[0]['value'] .'media/awblog/'. date('Y-m-d').'/'.$_FILES['imagepath']['name'];
                        }catch(Exception $e) {
                                    
                        }
             }
            /*  end*/
            if (isset($data['tags'])) {
                if ($this->getRequest()->getParam('id')) {
                    $model->load($this->getRequest()->getParam('id'));
                    $originalTags = explode(",", $model->getTags());
                } else {
                    $originalTags = array();
                }

                $tags = explode(',', $data['tags']);
                array_walk($tags, 'trim');

                foreach ($tags as $key => $tag) {
                    $tags[$key] = Mage::helper('blog')->convertSlashes($tag, 'forward');
                }
                $tags = array_unique($tags);

                $commonTags = array_intersect($tags, $originalTags);
                $removedTags = array_diff($originalTags, $commonTags);
                $addedTags = array_diff($tags, $commonTags);

                if (count($tags)) {
                    $data['tags'] = trim(implode(',', $tags));
                } else {
                    $data['tags'] = '';
                }
            }
            if (isset($data['stores'])) {
                if ($data['stores'][0] == 0) {
                    unset($data['stores']);
                    $data['stores'] = array();
                    $stores = Mage::getSingleton('adminhtml/system_store')->getStoreCollection();
                    foreach ($stores as $store) {
                        $data['stores'][] = $store->getId();
                    }
                }
            }

            $model->setData($data)->setId($this->getRequest()->getParam('id'));

            try {
                $format = Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM);
                if (isset($data['created_time']) && $data['created_time']) {
                    $dateFrom = Mage::app()->getLocale()->date($data['created_time'], $format);
                    $model->setCreatedTime(Mage::getModel('core/date')->gmtDate(null, $dateFrom->getTimestamp()));
                    $model->setUpdateTime(Mage::getModel('core/date')->gmtDate());
                } else {
                    $model->setCreatedTime(Mage::getModel('core/date')->gmtDate());
                }

                if ($this->getRequest()->getParam('user') == null) {
                    $model
                        ->setUser(
                            Mage::getSingleton('admin/session')->getUser()->getFirstname() . " " . Mage::getSingleton(
                                'admin/session'
                            )->getUser()->getLastname()
                        )
                        ->setUpdateUser(
                            Mage::getSingleton('admin/session')->getUser()->getFirstname() . " " . Mage::getSingleton(
                                'admin/session'
                            )->getUser()->getLastname()
                        )
                    ;
                } else {
                    $model
                        ->setUpdateUser(
                            Mage::getSingleton('admin/session')->getUser()->getFirstname() . " " . Mage::getSingleton(
                                'admin/session'
                            )->getUser()->getLastname()
                        )
                    ;
                }

                $model->save();

                /* recount affected tags */
                if (isset($data['stores'])) {
                    $stores = $data['stores'];
                } else {
                    $stores = array(null);
                }

                $affectedTags = array_merge($addedTags, $removedTags);

                foreach ($affectedTags as $tag) {
                    foreach ($stores as $store) {
                        if (trim($tag)) {
                            Mage::getModel('blog/tag')->loadByName($tag, $store)->refreshCount();
                        }
                    }
                }

                Mage::getSingleton('adminhtml/session')->addSuccess(
                    Mage::helper('blog')->__('Post was successfully saved')
                );
                Mage::getSingleton('adminhtml/session')->setFormData(false);

                if ($this->getRequest()->getParam('back')) {
                    $this->_redirect('*/*/edit', array('id' => $model->getId()));
                    return;
                }
                $this->_redirect('*/*/');
                return;
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                Mage::getSingleton('adminhtml/session')->setFormData($data);
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                return;
            }
        }
        Mage::getSingleton('adminhtml/session')->addError(Mage::helper('blog')->__('Unable to find post to save'));
        $this->_redirect('*/*/');
    }

}

所需要的功能基本完成了,剩下来,就是需要将数据在前台展示出来,需要新建Helper类,Model和新增的数据表字段,依次将它们新建起来:
Helper/Data.php

<?php
class Test_Lesson_Helper_Data extends Mage_Core_Helper_Abstract
{
}

Model/Status.php

<?php
class Test_Lesson_Model_Status extends Mage_Core_Model_Abstract
{
    /*
    *changestatus截止时间过期后改变状态
    */
    public function changeStatus()
    {
        $model = Mage::getModel('blog/blog')->getCollection();
        $data = $model->getData();
        foreach ($data as $val) {
            $currentDate = strtotime(date('Y-m-d'));
            $dateline = strtotime(Mage::app()->getLocale()->date($val['endtime'])->toString('YYYY-MM-dd'));
            $poorDate = floor(($dateline - $currentDate) / (3600 * 24));
            $connection = Mage::getSingleton('core/resource')->getConnection('core_write');
            $table = $connection->getTableName('aw_blog');
            if ($poorDate >= 0) {
                $sql = "UPDATE {$table} SET isexpiry=1 WHERE post_id=" . $val['post_id'];
            } else {
                $sql = "UPDATE {$table} SET isexpiry=0 WHERE post_id=" . $val['post_id'];
            }
            $connection->query($sql);
        }
        //return $sql;
    }
}

sql/test_lesson_setup/mysql4-install-0.1.0.php

<?php 
$installer = $this;
$installer->StartSetup();
$sql="ALTER TABLE 'aw_blog' ADD imagepath VARCHAR(255) NOT NULL,ADD endtime datetime DEFAULT NULL,ADD isexpiry tinyint DEFAULT  NULL";
$installer->run($sql);
$installer->EndSetup();

接着,来写前台的内容,前台的页面展示,需要设计到Block和IndexController控制器,所以,也一并写好:
IndexController.php

<?php
class Test_Lesson_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction()
    {
        $this->loadLayout();
        //$this->_title('lesson')->_title('index');
        $this->renderLayout();
 
    }
    public function applicationAction()
    {
        $this->loadLayout();
        $this->renderLayout();

    }
    public function videoAction()
    {
        $this->loadLayout();
        $this->renderLayout();
    }
    
}

Block下的文件Left.php,Video.php,Cource.php,Application.php
路径:Block/Left.php

<?php
class Test_Lesson_Block_Left extends Mage_Core_Block_Template
{}

路径:Block/Cource.php

<?php
class Test_Lesson_Block_Cource extends Mage_Core_Block_Template
{
    public function _prepareLayout()
    {
        $status = Mage::getModel('lesson/status')->changeStatus();
        //$status = Mage::getModel('lesson/status');
        $model =  Mage::getModel('blog/blog')->getCollection();
        $model->addFieldtoFilter('status',AW_Blog_Model_Status::STATUS_ENABLED)
            ->addFieldtoFilter('isexpiry',1);
        $model->setOrder('created_time','DESC');
        $this->setCollection($model);
        return $this;
    }
     
    /*
     *page 
     */
    public function bindPager($pagerName)
    {
        $pager = $this->getLayout()->getBlock($pagerName);
        if ($pager) {
            $pager->setLimit(2); // 设置每页显示新闻的数量
            $pager->setCollection($this->getCollection());
            $pager->setShowPerPage(false);
        }
    } 

}

Block/Video.php

<?php
class Test_Lesson_Block_Video extends Mage_Core_Block_Template
{
    public function _prepareLayout()
    {
        $status = Mage::getModel('lesson/status')->changeStatus();
        $model =  Mage::getModel('blog/blog')->getCollection();
        $model->addFieldtoFilter('status',AW_Blog_Model_Status::STATUS_ENABLED)->addFieldtoFilter('isexpiry','0');
        $model->setOrder('created_time','ASC');
        $this->setCollection($model);
        return $this;
    }

    /*
     *page
     */
    public function bindPager($pagerName)
    {
        $pager = $this->getLayout()->getBlock($pagerName);
        if ($pager) {
            $pager->setLimit(2); // 设置每页显示新闻的数量
            $pager->setCollection($this->getCollection());
            $pager->setShowPerPage(false);
        }
    }

}

Block/Application.php

class Test_Lesson_Block_Application extends Mage_Core_Block_Template
{
    public function test()
    {
        return "this is application content";
    }
}

现在就只剩下design前台展示部分了。
图片描述
lesson.xml

<?xml version="1.0"?>
<layout>
    <default>
        <reference name="head">
            <action method="addItem"><type>skin_js</type><name>js/lesson.js</name></action>
            <action method="addItem"><type>skin_css</type><name>css/lesson.css</name><params/><if><![CDATA[<!--[if (gte IE 9) | (IEMobile)]><!-->]]></if></action>
        </reference>   
        <reference name="root">  
            <action method="setTemplate"><template>page/2columns-left.phtml</template></action>
        </reference>
        <reference name='left'>
            <block type="lesson/left" name='lesson.left' template="lesson/left.phtml"/>
        </reference>
    </default>
    <lesson_index_index>
        <reference name="content">
               <block type="page/template_container" name="lesson.container" template="lesson/container.phtml">
                   <block type="page/html_pager" name="pager.bottom" as="pager_bottom" template="page/html/pager.phtml"/>
                   <block type="lesson/cource" name="cource" template="lesson/cource.phtml">
                       <action method="bindPager"><pager>pager.bottom</pager></action>
                   </block>
               </block>  
        </reference>
    </lesson_index_index>
    <lesson_index_application>
        <reference name="content">
            <block type="lesson/application" name="lesson.application" template="lesson/application.phtml"/>
        </reference>
    </lesson_index_application>
    <lesson_index_video>
        <reference name="content">
            <block type="page/template_container" name="lesson.container" template="lesson/container.phtml">
                <block type="page/html_pager" name="pager.bottom" as="pager_bottom" template="page/html/pager.phtml"/>
                <block type="lesson/video" name="video" template="lesson/video.phtml">
                    <action method="bindPager"><pager>pager.bottom</pager></action>
                </block>
            </block>
        </reference>
    </lesson_index_video>
</layout> 

template/lesson/container.phtml

<div class="page-lesson-cource">
    <?php echo $this->getChildHtml('cource')?>
    <?php echo $this->getChildHtml('application')?>
    <?php echo $this->getChildHtml('video')?>
    <?php echo $this->getChildHtml('pager_bottom')?>
</div>

template/lesson/cource.phtml

<div id="wrap">
<div id="content">
        <?php if ($this->getCollection()->getData() != null):?>
             <div id="c1" class='cource'>
                <ul>
                   <?php foreach( $this->getCollection() as $val):?>
                       <?php// print_r($val['post_id']);?>
                       <div class='cource-container' >
                           <div class ='img'><img src='<?php echo $val['imagepath']?>' width='250' height='200'/></div>
                           <div class = 'details'>
                               <li>
                                    <h3><?php echo $val['title']?></h3>
                               </li>
                               <li>
                                   <p><?php echo Mage::helper('core/string')->truncate($val['short_content'],160,'......')?></p>
                               </li>
                               <li>
                                   <span><?php echo $this->__('Training Dates');?>:<?php echo Mage::app()->getLocale()->date($val['endtime'])->toString('YYYY-MM-dd')?></span>
                               </li>
                           </div>
                       </div>
                   <?php endforeach;?>             
                </ul>
                <span><?php $this->getChildHtml('pager_buttom');?></span>              
            </div>
        <?php endif ?>    
    </div>
</div>

template/lesson/left.phtml

<div id="menu">
    <ul>
        <li><a href="<?php echo $this->getUrl('lesson')?>"><?php echo $this->__('Cource')?></a></li>
        <li><a href="<?php echo $this->getUrl('lesson/index/application')?>"><?php echo $this->__('Application')?></a></li>
        <li><a href="<?php echo $this->getUrl('lesson/index/video')?>"><?php echo $this->__('Video Download')?></a></li>
    </ul>
</div>

template/lesson/video.phtml

<div id="wrap">
    <div id="content">
        <?php if ($this->getCollection()->getData() != null):?>
            <div id="c1" class='cource'>
                <ul>
                    <?php foreach( $this->getCollection() as $val):?>
                        <?php// echo $val['post_id'];?>
                        <div class='cource-container' >
                            <div class ='img'><img src='<?php echo $val['imagepath']?>' width='250' height='200'/></div>
                            <div class = 'details'>
                                <li>
                                    <h3><?php echo $val['title']?></h3>
                                </li>
                                <li>
                                    <p><?php echo Mage::helper('core/string')->truncate($val['short_content'],160,'......')?></p>
                                </li>
                                <li>
                                    <span><?php echo $this->__('Training Dates');?>:<?php echo Mage::app()->getLocale()->date($val['endtime'])->toString('YYYY-MM-dd')?></span>
                                </li>
                            </div>
                        </div>
                    <?php endforeach;?>
                </ul>
                <span><?php $this->getChildHtml('pager_buttom');?></span>
            </div>
        <?php endif ?>
    </div>
</div>

template/lesson/application.phtml

<?php echo $this->test();?>

到此就完成了。


denson
366 声望57 粉丝