-
getCollection() as $val):?>
-
truncate($val["short_content"],160,"......")?>
- __("Training Dates");?>:getLocale()->date($val["endtime"])->toString("YYYY-MM-dd")?>
摘要:到此就完成了。
功能介绍:
安装好Magento插件后,需要把AW_Blog的前端显示方式要改变,变成类似选项卡的展示效果,先看下设计大体效果如下图:
那么,改如何修改插件呢?
还是和往常一样,新建一个模块,包含Block、controllers、etc、sql、Model文件夹
编写配置文件config.xml
0.1.0 Test_Lesson_Adminhtml Test_Lesson lesson test/lesson.xml Test_Lesson.csv Test_Lesson Test_Lesson_Model Test_Lesson_Block Test_Lesson_Block_Manage_Blog_Edit_Tab_Form Test_Lesson_Block_Manage_Blog_Grid Test_Lesson_Helper
配置好配置文件,来重写Grid.php:
//路径Test/Lesson/Block/Manage/Blog/Grid.php 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 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/identifier"); $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" => "" . $noticeMessage . "" . "", ) ); /** * 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" => "" . Mage::helper("blog")->__( "Hidden pages won"t be shown in blog but still can be accessed directly" ) . "", ) ); /*$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" => "" . Mage::helper("blog")->__( "Disabling will close the post to new comments" ) . "", ) );*/ /* 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
init($row, "thumbnail")->resize(97); //$out = ""; //return $out; $html = ""; return $html; } }
接着,就需要保存,就需要重新后台控制器中的saveAction(方法,同时新增image上传.
路径:Test/Lesson/controllers/Adminhtml/Awblog/Manage/BlogController.php
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
Model/Status.php
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
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.phploadLayout(); //$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路径:Block/Cource.php
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
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.xmlskin_js js/lesson.js skin_css css/lesson.css ]]> page/2columns-left.phtml pager.bottom pager.bottom template/lesson/container.phtml
getChildHtml("cource")?> getChildHtml("application")?> getChildHtml("video")?> getChildHtml("pager_bottom")?>template/lesson/cource.phtml
getCollection()->getData() != null):?>getCollection() as $val):?>
getChildHtml("pager_buttom");?>truncate($val["short_content"],160,"......")?>
- __("Training Dates");?>:getLocale()->date($val["endtime"])->toString("YYYY-MM-dd")?>
template/lesson/left.phtml
template/lesson/video.phtml
getCollection()->getData() != null):?>getCollection() as $val):?>
getChildHtml("pager_buttom");?>truncate($val["short_content"],160,"......")?>
- __("Training Dates");?>:getLocale()->date($val["endtime"])->toString("YYYY-MM-dd")?>
template/lesson/application.phtml
test();?>到此就完成了。
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/21462.html
摘要:重写控制器重写前端控制器对于以重写的为例。首先,对控制器的复写,需要分清是对还是的复写 Magento重写控制器 重写--前端控制器 对于以重写 Mage_Checkout_CartController的indexAction()为例。找到Mage/Checkout/CartController.php代码中的indexAction: public function indexActi...
摘要:版本中,安装插件,有两种方式可以安装插件,程序员最常用的方法就是将开发的插件模块文件直接从上传到对应目录即可。等到显示就是安装完成此时就可以去你的后台,来操作你刚刚装的插件模块了 Magento1.X版本中,安装插件,有两种方式可以安装插件,程序员最常用的方法就是将开发的插件模块文件直接从FTP上传到对应目录即可。而对于新手或不懂程序的人来说,此方法就有些晦涩难懂了(参见另一片文章)。...
摘要:添加可以先在外写好样式,再添加定义按钮的。也可以直接写,例如。类型,可以为是否为必选项定义按钮的添加图片代码。 功能介绍:今天要做一个后台表单元素显示出日期时间,开始以为跟Magento后台用到的表单元素标签(1)的功能一样,只需要设置type类型设置为date就可,但是设置了,开始怎么也弄不出来,又想着在后台加载一些js包,引用外部的js插件来实现这个功能,后来请教公司技术牛人,说可...
摘要:测试运行多次并取平均值。文章数量测试的基准测试基准测试结果基准测试结果基准测试结果基准测试结果基准测试结果基准测试结果不支持再次成为冠军请注意的运行环境需要或以上。同时,再次不能正常工作并抛出错误。 showImg(https://segmentfault.com/img/remote/1460000013690286); 我们每年都会尝试深入了解不同版本的 PHP 和 HHVM 在各...
摘要:以上是的的目录路由器配置。考虑重写模块以上文件文件使用的操作字符串。例如,如果你想回到第一个模块在这个系列和添加前端端点,所有你需要做的就是添加以下配置和以下控制器文件。默认操作字符串段我们从先进的路由移动之前,有一些最后要提。 今天,我们要介绍的几个Magento的路由系统的高级功能,并讨论一些看似急性锐边的历史。虽然所有在这篇文章中提供的技术可能不是最好的方式来实现自己的目标,作为...
阅读 1281·2021-09-24 10:26
阅读 3619·2021-09-06 15:02
阅读 537·2019-08-30 14:18
阅读 547·2019-08-30 12:44
阅读 3095·2019-08-30 10:48
阅读 1902·2019-08-29 13:09
阅读 1967·2019-08-29 11:30
阅读 2246·2019-08-26 13:36