跳轉到內容

SilverStripe

0% developed
來自華夏公益教科書
SilverStripe logos
SilverStripe 徽標
SilverStripe

本書旨在提供一套解決方案,用於解決 SilverStripe CMS(“內容管理系統”)中常見的難題。

關於 SilverStripe

[編輯 | 編輯原始碼]

SilverStripe 是基於 PHP5 和 MySQL5 的 MVC(模型-檢視-控制器)框架和內容管理系統。

構建模組

[編輯 | 編輯原始碼]

載入頁面特定的 JavaScript 和 CSS

[編輯 | 編輯原始碼]
// controller
class Movies_Controller extends Page_Controller
{
	// init
	function init()
	{	
		Requirements::javascript('mysite/javascript/movies.js');

		parent::init();
	}
}

CMS 中的 CRUD(建立、讀取、更新和刪除)

[編輯 | 編輯原始碼]

假設我們需要允許管理員編輯“開發者”部分中的編碼人員列表。

首先,我們需要建立“開發者”資料物件。

class Developers_DataObject extends DataObject
{
        // fields
	static $db = array(
		'Name'		=>'Varchar(150)',
		'Lastname'	=>'Varchar(150)',);

	// edit form in the CMS
	function getCMSFields_forPopup()
	{
		$fields = new FieldSet();
		$fields->push( new TextField( 'Name', 'Name:' ) );
		$fields->push( new TextField( 'Lastname', 'Lastname:' ) );
		return $fields;
	}
}

在同一個資料物件中,我們需要為表單定義欄位。

接下來,我們需要為“開發者”部分建立頁面型別。

class Developers_Page extends Page {

    // make the relation between the section and the list of coders
    static $has_one = array(
        'MyDeveloper' => 'Developers_DataObject'
    );


    // manage the relation in the CMS
    function getCMSFields()
    {
        // don't overwrite the main fields
        $fields = parent::getCMSFields();

        $tablefield = new HasOneComplexTableField(
	    $this,
            'MyDeveloper',
            'Developers_DataObject', 
            array(
	        'Name' => 'Name',
		'Lastname' => 'Lastname',), 
            'getCMSFields_forPopup');

            // add a new tab in the CMS
            $fields->addFieldToTab( 'Root.Content.Coders', $tablefield );
					
            return $fields;
    }
}

我們的下一步是建立一個名為“開發者”的新頁面。

現在,我們需要轉到“行為”並更改頁面型別為“開發者”。

此新頁面將有一個名為“編碼人員”的標籤,我們可以在其中編輯開發者列表。

將資料物件與模組關聯

[編輯 | 編輯原始碼]

在這種情況下,我們將建立電影與城市的關聯。然後,我們將允許管理員在“電影”頁面型別中新增新城市。

<?php
/*
 * Local Cities Data Object
 * 
 */
class LocalCitiesDataObject extends DataObject
{
	static $db = array(
		"Name" => "Varchar(150)"
	);
	
	// relation
	static $has_one = array(
		'LocalCities' => 'Movies' // 1-n relation
	);
	
	// edit form for the CMS
	function getCMSFields_forPopup()
	{
		$fields = new FieldSet();
		$fields->push( new TextField( 'Name' ) );
		return $fields;
	}
}

/**
 * Defines the Movies page type
 */
class Movies extends Page
{

	// relation
	static $has_many = array(
		'LocalCities' => 'LocalCitiesDataObject' // 1-n relation
	);

	// records list in the CMS
	function getCMSFields()
	{
		// don't overwrite the defaults fields for the Page
		$fields = parent::getCMSFields();

		// for handling 1-n relation
		$tablefield = new HasManyComplexTableField(
			$this,
         	'LocalCities', // the name of the relationship
         	'LocalCitiesDataObject', // the related table 
			array(
				"Name" => "Name"
			),
	         'getCMSFields_forPopup' // the function to build the add/edit form
         );

         $fields->addFieldToTab( 'Root.Content.Cities', $tablefield );
         
         return $fields;
	}
}

此貢獻由以下編碼人員提供:

  • Guy Steuart
  • Leonardo Alberto Celis
華夏公益教科書