Customizing Zurmo
---- 自定義Zurmo
php
官方的動物園爲例:連接:http://zurmo.org/wiki/customi...html
新建模塊數據庫
全部擴展類將在本身的模塊中定義;在這個例子中,模塊將被稱爲animals
。文件目錄結構以下:緩存
在modules
的文件夾下新建一個名爲 animals
的新文件夾。app
PS:名字單複數不要緊,可是 Zurmo 自帶的模塊都是採用複數形式,因此最好統一。
模塊將會有如下文件:ui
PS:這是官方給出的模塊的基本文件結構,可是根據我本身的經驗,做爲一個最簡單的模塊的話,不須要這麼多文件,具體的文件目錄,請參看CSDN博客:http://blog.csdn.net/tyrannoaurus/article/details/73850397。
下面的表格是關於每一個文件的做用的:this
爲了在Zurmo應用程序中集成和安裝該模塊,須要建立其餘兩個文件url
perInstanceConfig.php:ZurmoZoo
項目的自定義配置。在 protected/config/
下新建spa
<?php /** * Custom configuration for the Zurmo Zoo project. */ $instanceConfig = array( 'modules' => array( 'animals', ), ); //引用文件,因此文件夾的名字必定要對應上 $instanceConfig['components']['custom']['class'] = 'application.extensions.zurmozoo.components.ZurmoZooCustomManagement'; $instanceConfig['import'][] = "application.extensions.zurmozoo.*"; $instanceConfig['import'][] = "application.extensions.zurmozoo.components.*"; $instanceConfig['import'][] = "application.extensions.zurmozoo.utils.*"; ?>
還有兩個文件,在 extensions/
下新建 zurmozoo.components.ZurmoZooCustomManagement
和 zurmozoo.utils.ZurmoZooInstallUtil
.net
ZurmoZoo
項目的具體定製管理 ZurmoZooCustomManagement.php
照寫就好了
/** * Specific custom management for the zurmo zoo project. */ class ZurmoZooCustomManagement extends CustomManagement { /** * (non-PHPdoc) * @see CustomManagement::runBeforeInstallationAutoBuildDatabase() */ public function runBeforeInstallationAutoBuildDatabase(MessageLogger $messageLogger) { ZurmoZooInstallUtil::resolveCustomMetadataAndLoad(); } /** * (non-PHPdoc) * @see CustomManagement::resolveIsCustomDataLoaded() */ public function resolveIsCustomDataLoaded() { ZurmoZooInstallUtil::resolveCustomMetadataAndLoad(); } }
ZurmoZooInstallUtil: Helper class for Zurmo Zoo customizations.
class ZurmoZooInstallUtil { public static function resolveCustomMetadataAndLoad() { $shouldSaveZurmoModuleMetadata = false; $metadata = ZurmoModule::getMetadata(); if(!in_array('animals', $metadata['global']['tabMenuItemsModuleOrdering'])) { //加入到側邊欄的配置中 $metadata['global']['tabMenuItemsModuleOrdering'][] = 'animals'; $shouldSaveZurmoModuleMetadata = true; } if($shouldSaveZurmoModuleMetadata) { ZurmoModule::setMetadata($metadata); GeneralCache::forgetAll(); } //------------------------------------------------------------------- //這段代碼爲官方的例子,做用是和 Activity 關聯起來,可是若是咱們的應用中不須要用到 Activity 的話,是不用引用這個代碼的,引用了這個代碼以後,在數據庫中會有記錄,可能之後的一些操做會報錯也不必定。 $metadata = Activity::getMetadata(); if(!in_array('Animal', $metadata['Activity']['activityItemsModelClassNames'])) { $metadata['Activity']['activityItemsModelClassNames'][] = 'Animal'; Activity::setMetadata($metadata); GeneralCache::forgetAll(); } //------------------------------------------------------------------- //新建時建立默認數據 Yii::import('application.extensions.zurmoinc.framework.data.*'); Yii::import('application.modules.animals.data.*'); $defaultDataMaker = new AnimalsDefaultDataMaker(); $defaultDataMaker->make(); } }
將基本字段添加到你的類
在本節中,介紹如何向最近建立的類添加類型爲integer,float或string的新的基本字段,在這個例子中,咱們將向Animal類添加一個新的字段 --description
首先要作的是定義類中的新字段。打開文件
protected-> modules-> animals-> models-> Animal.php
並添加字段描述的定義以下。
<?php …. public static function getDefaultMetadata() { $metadata = parent::getDefaultMetadata(); $metadata[__CLASS__] = array( 'members' => array( 'name', 'description', ), //類的普通字段 'relations' => array( 'type' => array(RedBeanModel::HAS_ONE, 'OwnedCustomField', RedBeanModel::OWNED), ), //和 OwnedCustomField 表的關係 type 'rules' => array( array('name', 'required'), array('name', 'type', 'type' => 'string'), array('name', 'length', 'max' => 100), array('description', 'type', 'type' => 'string'), ), //字段的規則,包括在頁面的規則 和存入數據庫的時候,會轉化爲字段的類型 'elements' => array( 'description' => 'TextArea', ), //字段的html類型, 'customFields' => array( 'type' => 'AnimalType', ), //和relations 相對應的屬性 , 會存入數據庫中, 'defaultSortAttribute' => 'name', //list頁面數據的默認排序方式 'noAudit' => array( 'description' ), //noAudit 表示這個字段的操做不會被記錄到操做記錄中 ); return $metadata; } ….. ?>
如今咱們須要將該字段放在頁面中。打開文件
protected-> modules-> animals-> views-> AnimalEditAndDetailsView.php
並定位新建立的字段,以下所示。只是部分代碼
class AnimalEditAndDetailsView extends SecuredEditAndDetailsView { public static function getDefaultMetadata() { $metadata = array( 'global' => array( 'toolbar' => array( 'elements' => array( array('type' => 'CancelLink', 'renderType' => 'Edit'), array('type' => 'SaveButton', 'renderType' => 'Edit'), array('type' => 'ListLink', 'renderType' => 'Details', 'label' => "eval:Yii::t('Default', 'Return to List')" ), array('type' => 'EditLink', 'renderType' => 'Details'), array('type' => 'AuditEventsModalListLink', 'renderType' => 'Details'), ), ), 'derivedAttributeTypes' => array( 'DateTimeCreatedUser', 'DateTimeModifiedUser', ), 'panelsDisplayType' => FormLayout::PANELS_DISPLAY_TYPE_ALL, 'panels' => array( array( 'rows' => array( array('cells' => array( array( 'elements' => array( array('attributeName' => 'name', 'type' => 'Text'), ), ), array( 'elements' => array( array('attributeName' => 'type', 'type' => 'DropDown', 'addBlank' => true), ), ), ) ), array('cells' => array( array( 'detailViewOnly' => false, 'elements' => array( array('attributeName' => 'integer', 'type' => 'Integer'), ), ), array( 'detailViewOnly' => false, 'elements' => array( array('attributeName' => 'text', 'type' => 'Text'), ), ), ) ), array('cells' => array( array( 'detailViewOnly' => false, 'elements' => array( array('attributeName' => 'radioPickList', 'type' => 'RadioDropDown', 'addBlank' => true), ), ), array( 'detailViewOnly' => false, 'elements' => array( array('attributeName' => 'pickList', 'type' => 'DropDown', 'addBlank' => true), ), ), ) ), array('cells' => array( array( 'detailViewOnly' => true, 'elements' => array( array('attributeName' => 'null', 'type' => 'DateTimeCreatedUser'), ), ), array( 'detailViewOnly' => true, 'elements' => array( array('attributeName' => 'null', 'type' => 'DateTimeModifiedUser'), ), ), ) ), ), ), ), ), ); return $metadata; } protected function getNewModelTitleLabel() { return Yii::t('Default', 'Create AnimalsModuleSingularLabel', LabelUtil::getTranslationParamsForAllModules()); } } ?>
如您所見,咱們添加了該字段的位置
array('cells' => array( array( 'elements' => array( array('attributeName' => 'description', 'type' => 'TextArea'), ), ), ) ),
並且還有如下連接:
array('type' => 'AnimalDeleteLink', 'renderType' => 'Details'),
這個不是必定要的,可是若是須要,您能夠在 details
頁面刪除動物。爲了實現這個功能,必須在
protected-> modules-> animals-> elements->
操做下添加 AnimalDeleteLinkActionElement.php
文件,並使用如下代碼完成該操做。
<?php class AnimalDeleteLinkActionElement extends DeleteLinkActionElement { protected function resolveConfirmAlertInHtmlOptions($htmlOptions) { $htmlOptions['confirm'] = Yii::t('Default', 'Are you sure you want to remove this AnimalsModuleSingularLowerCaseLabel?', LabelUtil::getTranslationParamsForAllModules()); return $htmlOptions; }
} ?>
截至今天,Zurmo支持如下類型的字段:
如下源代碼顯示瞭如何將它們整合到代碼中。
models->Animals.php
首先,您須要定義模型中的全部字段
在官網的示例代碼中都有,文檔中的那些代碼示例部分是之前的zurmo舊版本的代碼,在新版本的方法有些不同,以新版本代碼爲正。
好比: models->Animals.php
中的這段代碼,就是舊版本的代碼。如今已經換成新的代碼了。
'labels' => array( 'cust_checkbox' => array('en' => 'Check Box'), 'cust_currency' => array('en' => 'Currency'), 'cust_date' => array('en' => 'Date'), 'cust_datetime' => array('en' => 'Date Time'), 'cust_decimal' => array('en' => 'Decimal'), 'cust_integer' => array('en' => 'Integer'), 'cust_picklist' => array('en' => 'Pick List'), 'cust_phone' => array('en' => 'Phone'), 'cust_radiopicklist' => array('en' => 'Radio Pick List'), 'cust_text' => array('en' => 'Text'), 'cust_textarea' => array('en' => 'Text Area'), 'cust_url' => array('en' => 'URL'), ),
寫完以上的代碼就完成了部分了,具體的代碼在官網的示例模塊中都有,直接去下載就好了。連接:https://bitbucket.org/zurmo/z...
若是頁面效果沒有生效,記得 清除緩存 和 更新metadata.