来源:http://trac.seagullproject.org/wiki/Modules/Cms/Api

CMS API

下列是CMS API中可用的方法的列表。更多的API文档

可用的方法

// following notation borrowed from the Pragmatic Programmers
// static object calls indicated in standard way, eg SGL_Content::getType()
// object method calls indicated by SGL_Content#myMethod()
// API according CMS v. 1.4 (update 2008-07-16)
// SGL_Attribute
SGL_Attribute::SGL_Attribute(Object $oData = null) : SGL_Attribute;
SGL_Attribute::getById(Integer $id) : SGL_Attribute;
SGL_Attribute#getType() : Integer;
SGL_Attribute#changeType(Integer SGL_CONTENT_ATTR_TYPE_*) : Boolean;
SGL_Attribute#rename(String $newName) : Boolean;
SGL_Attribute#delete() : Boolean;
SGL_Attribute#save() : Boolean;
SGL_Attribute#getParams() : aData
// SGL_Content
SGL_Content::SGL_Content(Object $oData = null) : SGL_Content;
SGL_Content::createType(String $name) : SGL_Content;
SGL_Content::getById(Integer $id, Integer $version = null) : SGL_Content;
SGL_Content::getByName(Integer $name) : SGL_Content;
SGL_Content::getByType(String $type) : SGL_Content;
SGL_Content#getLinkedContents(String $typeId = null) : aSGL_Content;
SGL_Content#getType() : Integer;
SGL_Content#getStatus() : SGL_CMS_STATUS_*;
SGL_Content#setStatus(SGL_CMS_STATUS_* $status);
SGL_Content#validate() : Boolean
SGL_Content#save(Boolean $newVersion = false) : SGL_Content
SGL_Content#rename(String $newName) : Boolean;
SGL_Content#delete();
SGL_Content#getAttribId(String $attribName) : Integer
SGL_Content#addAttribute(SGL_Attribute $oAttrib) : SGL_Content;
// SGL_Context
SGL_Context::SGL_Context(Object $oData) : SGL_Context;
SGL_Context#process() : SGL_Context_*;
// SGL_Context_WebContentType
SGL_Context_WebContentType#process(Object $oData) : SGL_Context_WebContentType;
// SGL_Context_WebContent
SGL_Context_WebContent#process(Object $oData) : SGL_Context_WebContent;
// SGL_Finder
SGL_Finder::factory(String $handler) : SGL_Finder; // of type SGL_FINDER_*
SGL_Finder#addFilter(String $filterName, String $filterValue) : SGL_Finder;
SGL_Finder#retrieve() : Boolean;
SGL_Finder#getFilters() : aFilters;
SGL_Finder#getAttributeFilters() : aAttributes;
// SGL_Finder_File
// not implemented
// SGL_Finder_Contenttype
SGL_Finder_Contenttype#retrieve() : aSGL_Content;
// SGL_Finder_Content
SGL_Finder_Content#retrieve() : aSGL_Content;
// SGL_Finder_AssocContent
SGL_Finder_AssocContent#retrieve() : aSGL_Content;
SGL_Finder_AssocContent#addFilter(String $filterName, String $filterValue) : SGL_Finder_Assoccontent;

在模板中调用

{oMyCV.attributeName} //   using a __get() method

使用finder

SGL_Finder::factory('content')

应用实例

创建一个新的内容类型

$oRestoReview  = SGL_Content::createType('RestaurantReview')
  ->addAttribute(new SGL_Attribute(
      array('name' => 'dateEaten', 'typeId' => SGL_CONTENT_ATTR_TYPE_DATE)))
  ->addAttribute(new SGL_Attribute(
      array('name' => 'dishName', 'typeId' => SGL_CONTENT_ATTR_TYPE_TEXT)))
  ->addAttribute(new SGL_Attribute(
      array('name' => 'overallRating', 'typeId' => SGL_CONTENT_ATTR_TYPE_FLOAT)))
  ->save();

重命名或修改一个内容类型

$oRestoReview  = SGL_Content::getByType('RestaurantReview');
$oRestoReview->rename('Foo');
$oRestoReview->renameAttribute('bar', 'baz');
$oRestoReview->deleteAttribute('baz');

重命名或删除一个属性

$bar = SGL_Attribute::getById($id); // never by name, too ambiguous
$bar->rename('fluux');
$bar->delete();

获取一个属性类型

$bar = SGL_Attribute::getById($id);
$type = $bar->getType();

获取一个属性的参数

// eg for a choice attrib like RADIO
$attr = SGL_Attribute::getById($id);
if ($attr->getType() == SGL_CONTENT_ATTR_TYPE_RADIO) {
    $aParams = $attr->getParams();
}

改变一个属性类型

$bar = SGL_Attribute::getById($id);
$bar->changeType(SGL_ATTRTYPE_FLOAT);

改变一个属性值

$bar = SGL_Attribute::getById($id);
$bar->value = 'new value';
$oAttrib = $bar->save();

基于现有内容类型创建新的内容

$oRestoReview = SGL_Content::getByType('RestaurantReview');  
// or by $typeId: $oRestoReview = SGL_Content::getType($typeId)
$oRestoReview->dishName = 'Chicken Catchatorie';
$oRestoReview->overallRating = 7.6;
$oRestoReview->save();

基于现有内容类型从表单数据创建一个新的内容

//  pass data from post as arg
$oContext = new SGL_Context($input);
//context strategy determines if Content or ContentType
//and maps data correctly
//SGL_Content object can be built from any input (php object, post, xml, etc)
$oContent = new SGL_Content($oContext->process());

更新内容

$oRestoReview  = SGL_Content::getById($id);
$oRestoReview->dishName = "Duck a l'Orange";
$oRestoReview->save();

获取一个内容项

$oExampleCV = SGL_Content::getById($id);
print '<pre>';print_r($oExampleCV);
/*
SGL_Content Object
(
  [id] => 7
  [name] => simple CV example
  [createdByName] => admin
  [createdById] => 1
  [updatedById] => 1
  [dateCreated] => 2006-04-19 11:43:45
  [lastUpdated] => 2006-04-19 11:43:45
  [typeId] => 15
  [typeName] => CV Format Simple
  [aAttribs] => Array
      (
          [0] => SGL_Attribute Object
              (
                  [id] => 33
                  [typeId] => 6
                  [name] => website
                  [alias] => Website
                  [value] => http://foo.com
                  [contentTypeId] => 15
              )
          [1] => SGL_Attribute Object
              (
                  [id] => 34
                  [typeId] => 1
                  [name] => email
                  [alias] => Email
                  [value] => demian@muse23.com
                  [contentTypeId] => 15
              )
       )
)  */

删除一个内容项

$oRestoReview  = SGL_Content::getById($id);
$oRestoReview->delete();

检索内容集合

根据类型和所有都取得内容

$aReviews = SGL_Finder::factory('content')
->addFilter('typeName', 'RestaurantReview')
->addFilter('createdBy', SGL_Session::getUid())
->retrieve();

根据类型ID和所有者取得内容

$aReviews = SGL_Finder::factory('content')
->addFilter('typeId', 123)
->addFilter('createdBy', SGL_Session::getUid())
->addFilter('sortBy', 'field_name')
->addFilter('sortOrder', 'ASC')
->retrieve();

对取回的结果集应用limit,offset并排序

 $aReviews = SGL_Finder::factory('content')
      ->addFilter('sortBy', 'name')
      ->addFilter('sortOrder', 'ASC')
      ->addFilter('limit', array('offset' => 1, 'count' => 2))
      ->retrieve();

取回内容,对结果集应用limit并排序

$aReviews = SGL_Finder::factory('content')
      ->addFilter('sortBy', 'name')
      ->addFilter('sortOrder', 'ASC')
      ->addFilter('limit', array('count' => 2))
      ->retrieve();

取回内容,对结果集应用limit并排序

$aReviews = SGL_Finder::factory('content')
      ->addFilter('sortBy', 'name')
      ->addFilter('sortOrder', 'ASC')
      ->addFilter('limit', 2)
      ->retrieve();

取回内容,对结果集按属性值排序

$aReviews = SGL_Finder::factory('content')
      ->addFilter('sortBy', array('attribute' => 'title'))
      ->addFilter('sortOrder', 'DESC')
      ->retrieve();

根据分类ID取回内容,对结果集排序

$aReviews = SGL_Finder::factory('content')
      ->addFilter('sortBy', 'name')
      ->addFilter('sortOrder', 'ASC')
      ->addFilter('categoryId', 6)
      ->retrieve();

取回满足属性值条件的内容

(The operator below can be any allowable in SQL, for example:
'=','<>', '<', '>', '<=', '>=', 'LIKE' or 'IN', to match a list of possible values. 
Currently, when using IN, the list must be passed in the format "('value1','value2','value3')" to function properly.)
$attribFilter = array(
      'name'     => 'letter',
      'value'    => 'a',
      'operator' => '='
);
$aReviews = SGL_Finder::factory('content')
      ->addFilter('typeName', 'Glossary term')
      ->addFilter('status', SGL_CMS_STATUS_PUBLISHED)
      ->addFilter('attribute', $attribFilter)
      ->retrieve();

根据多个属性条件过滤

$aReviews = SGL_Finder::factory('content')
      ->addFilter('typeName', 'Article')
      ->addFilter('attribute', array(
          'name'     => 'introduction',
          'operator' => '=',
          'value'    => 'foo'))
      ->addFilter('attribute', array(
          'name'     => 'body',
          'operator' => '=',
          'value'    => 'bar'));

获取关联内容

$aContents = SGL_Finder::factory('assocContent')
      ->addFilter('typeId', SGL_CMS_CONTENTTYPE_REVIEW_PRODUCT_USER)
      ->addFilter('status', SGL_CMS_STATUS_PUBLISHED)
      ->addFilter('sortBy', 'date_created')
      ->addFilter('sortOrder', 'desc')
      ->addFilter('assocContents', array('parentId' => $contentId))
      ->retrieve();

获得关联的父内容

$aRet = SGL_Finder::factory('assocContent')
      ->addFilter('sortBy', 'content_id')
      ->addFilter('sortOrder', 'DESC')
      ->addFilter('assocContents', array('childId' => 8))
      // get articles only
      ->addFilter('typeId', 1)
      ->retrieve();

获取关联的媒体内容

首先你的内容类型必须包含一个file类型的属性。然后你必须在admin管理界面上传媒体文件到相应的内容。当你想访问该媒体文件时,在你的Finder搜索中指定文件名称,如下:

$aRet = SGL_Finder::factory('content')
      ->addFilter('categoryId', 6)
      ->addFilter('mediaInfo', array('name' => 'myProfilePic'));
      ->addFilter('mediaInfo', array('name' => 'myProfileThumb'));
      ->retrieve();

然后你可以遍历结果集

foreach ($aContents as $oContent) {
  // to access file name
  $oContent->media->myProfilePic->file_name
  // to access mime type
  $oContent->media->myProfileThumb->mime_type
}
 
modules/cms/api.txt · 最后更改: 2010/05/30 00:21 (外部编辑)
 
Except where otherwise noted, content on this wiki is licensed under the following license:GNU Free Documentation License 1.2