-
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();?>到此就完成了。
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/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的路由系統的高級功能,并討論一些看似急性銳邊的歷史。雖然所有在這篇文章中提供的技術可能不是最好的方式來實現自己的目標,作為...
閱讀 1342·2021-09-24 10:26
閱讀 3655·2021-09-06 15:02
閱讀 605·2019-08-30 14:18
閱讀 577·2019-08-30 12:44
閱讀 3119·2019-08-30 10:48
閱讀 1936·2019-08-29 13:09
閱讀 1994·2019-08-29 11:30
閱讀 2279·2019-08-26 13:36