Magento学习手记(第十三天)
第十三天
一、 Magento后台功能定制化实例测试:根据数据库表创建Grid
1. 为Grid创建后台控制器,该Grid以subscription实体为基础,控制器文件为SubscriptionController.php,路径/YEMA/Admindev/controllers/Adminhtml/Admindev;
1 SubscriptionController.php,路径/YEMA/Admindev/controllers/Adminhtml/Admindev; 2 class YEMA_Admindev_Adminhtml_Admindev_SubscriptionController extends Mage_Adminhtml_Controller_Action { 3 public function indexAction() { 4 $this->loadLayout (); 5 $this->_addContent ( $this->getLayout ()->createBlock ( 'admindev/adminhtml_subscription' ) ); 6 $this->renderLayout (); 7 } 8 }
2. 为控制器创建目录,编辑adminhtml.xml文件;
1 <menu> 2 <customer> 3 <children> 4 <subscription translate="title" module="admindev"> 5 <title>AdminDev Subscription</title> 6 <sort_order>10</sort_order> 7 <action>adminhtml/admindev_subscription</action> 8 </subscription> 9 </children> 10 </customer> 11 </menu>
3. 为控制器的动作创建ACL,编辑adminhtml.xml文件;
1 <acl> 2 <resources> 3 <all> 4 <title>Allow Everything</title> 5 </all> 6 <admin> 7 <children> 8 <customer> 9 <children> 10 <subscription> 11 <title>AdminDev Subscription</title> 12 <sort_order>10</sort_order> 13 </subscription> 14 </children> 15 </customer> 16 </children> 17 </admin> 18 </resources> 19 </acl>
4. 创建对应的Block,/Admindev/Block/Adminhtml/Subscription.php;
1 class YEMA_Admindev_Block_Adminhtml_Subscription extends Mage_Adminhtml_Block_Widget_Grid_Container { 2 public function __construct() { 3 $this->_headerText = Mage::helper ( 'admindev' )->__ ( 'Admindev subscriptions' ); 4 $this->_blockGroup = 'admindev'; 5 $this->_controller = 'adminhtml_subscription'; 6 $this->_headerText = 'Manage Subscriptions'; 7 parent::__construct (); 8 } 9 10 protected function _prepareLayout() { 11 $this->_removeButton('add'); 12 return parent::_prepareLayout(); 13 } 14 }
注:这里调用了帮助类的翻译函数(__)来获取数据;
4. 创建Grid,/Admindev/Block/Adminhtml/Subscription/Grid.php;
1 <?php 2 class YEMA_Admindev_Block_Adminhtml_Subscription_Grid extends Mage_Adminhtml_Block_Widget_Grid { 3 public function __construct() { 4 parent::__construct (); 5 6 $this->setId ( 'subscription_grid' ); 7 $this->setDefaultSort ( 'subscription_id' ); 8 $this->setDefaultDir ( 'DESC' ); 9 } 10 protected function _prepareCollection() { 11 $collection = Mage::getModel ( 'admindev/subscription' )->getCollection (); 12 $this->setCollection ( $collection ); 13 return parent::_prepareCollection (); 14 } 15 protected function _prepareColumns() { 16 $this->addColumn ( 'subscription_id', array ( 17 'index' => 'subscription_id', 18 'header' => Mage::helper ( 'admindev' )->__ ( 'Subscription id' ), 19 'type' => 'number', 20 'sortable' => true, 21 'width' => '100px' 22 ) ); 23 24 $this->addColumn ( 'firstname', array ( 25 'index' => 'firstname', 26 'header' => Mage::helper ( 'admindev' )->__ ( 'Firstname' ), 27 'sortable' => false 28 ) ); 29 30 $this->addColumn ( 'lastname', array ( 31 'index' => 'lastname', 32 'header' => Mage::helper ( 'admindev' )->__ ( 'Lastname' ), 33 'sortable' => false 34 ) ); 35 36 $this->addColumn ( 'email', array ( 37 'index' => 'email', 38 'header' => Mage::helper ( 'admindev' )->__ ( 'Email' ), 39 'sortable' => false 40 ) ); 41 42 return parent::_prepareColumns (); 43 } 44 public function getGridUrl() { 45 return $this->getUrl ( '*/*/grid', array ( 46 '_current' => true 47 ) ); 48 } 49 }
5. 创建index控制器,/Admindev/controllers/Adminhtml/Admindev/IndexController.php;
1 class YEMA_Admindev_Adminhtml_Admindev_IndexController extends Mage_Adminhtml_Controller_Action { 2 public function indexAction() { 3 $this->loadLayout (); 4 $this->renderLayout (); 5 } 6 }
6. 增加页面标题,编辑/Block/Adminhtml/Subscription.php文件的__construct();
1 $this->_headerText = 'Manage Subscriptions';
7. 在Grid中增加些内容,Grid.php中增加两个字段;
1 $this->addColumn ( 'create_at', array ( 2 'index' => 'create_at', 3 'header' => Mage::helper ( 'admindev' )->__ ( 'Create At' ), 4 'type' => 'datetime', 5 'sortable' => true, 6 'width' => '150px' 7 ) ); 8 9 $this->addColumn ( 'status', array ( 10 'index' => 'status', 11 'header' => Mage::helper ( 'admindev' )->__ ( 'Status' ), 12 'sortable' => true, 13 'frame_callback' => array ( 14 $this, 15 'prepareStatusLayout' 16 ), 17 'width' => '150px' 18 ) );
由于使用了frame_callback,所以添加相对应方法的定义:
1 public function prepareStatusLayout($value) { 2 $class = ''; 3 switch ($value) { 4 case 'pending': 5 $class = 'grid-severity-notice'; 6 break; 7 case 'approved': 8 $class='grid-severity-major'; 9 break; 10 case 'declined': 11 $class = 'grid-severity-critical'; 12 break; 13 } 14 return '<span class="'.$class.'"><span>'.$value.'</span></span>'; 15 }
后台常用widgets有:Grid、Form和Tab Left Menu。
注:addColumn()方法,建议每个column都配置如下三个属性:
- header(列头);
- index(该列在数据库表中的列名);
- sortable(是否可搜索);
其他可选属性还有:
- width(列显示的宽度);
- frame_callback(调用方法处理列值);
- type(列数据类型,如number、datetime和options);
- options(当类型是options时,定义数据源Model);
二、 Magento后台功能定制化实例测试:增加自定义属性
1. 创建install脚本,增加数据库表字段;
1 $installer = $this; 2 $installer->startSetup (); 3 4 // Create the attribute "loyaltynumber" for the customer entity 5 $installer->addAttribute ( 'customer', 'loyaltynumber', array ( 6 'type' => 'varchar', 7 'input' => 'text', 8 'required' => false, 9 'label' => 'Loyaltynumber', 10 'visible' => true, 11 'adminhtml_only' => true, 12 'user_defined' => true 13 ) ); 14 15 $installer->endSetup();
2. 在后台将增加新增字段增加到customer的Form中管理;
1 //Add the attribute to the backend forms 2 //@todo 3 $loyaltyAttribute = Mage::getSingleton('eav/config') 4 ->getAttribute('customer', 'loyaltynumber'); 5 $loyaltyAttribute->setData('used_in_forms', array('adminhtml_customer')); 6 $loyaltyAttribute->save();