/**
* CRUD主控類
* @author bluehire
*/
class SCrud {
const PATH_VIEW='crud'; //視圖路徑名稱
/**
* 目前清單操作配置
* @var SCrudOperation
*/
public $operation;
/**
* 資料存取物件
* @var STable
* /
public $model;
private $table; // 主表名
private $config; //表配置array
public $fields; //所有字段物件數組
public $title; // 目前整個整個CRUD的業務標題
public $pageSize=20; //清單分頁尺寸
public $pageSort='id'; //清單排序依據
public $pageDir='desc'; //清單排序方向
public $rowNo=true; / /清單是否顯示行號
/**
* 列表時的過濾
* @var Closure
*/
public $gridFilter;
//是否有以下操作
public $operationView=true;
public $operationInsert=true;
publicoperoperationUpdate=;
public $operationDelete=true;
public $operationMultiDelete=true;
// 目前請求的控制器與動作名稱,用於以後拼接超連結
private $controller;
private $action;
private $controller;
private $action; &*/
public function __construct($table, $controller, $action) {
$this->table = $table;
$this->controller = $controller;
$this->action = $action;
// 產生各種設定物件
$this->config = config ( 'crud/' . $table );
foreach ( $this->config as $c => $v ) {
if (strpos ( $c, '_' ) === 0) {
$n = substr ( $c, 1 );
$this->$n = $v;
} else {
$this->fields [$c] = new SCrudField ( $this, $v );
}
}
//本功能的標題
$this->title=$this->title?:$table.'管理';
//所有運算組合
$ this->operation = new SCrudOperationSet ( $this );
//資料存取模型
$this->model = table($table);
}
/**
*
* @param unknown $table 主表名
* @param unknown $controller 控制器名稱
* @param unknown $action 動作名稱
*/
public function field($ name) {
if (! isset ( $this->fields [$name] )) {
return null;
}
return $this->fields [$name];
}
/**
* 取一個欄位
*
* @param unknown $name
* @return SCrudField
* /
public function getPrimaryField() {
// 查看所有欄位
foreach ( $this->fields as $field ) {
// 主鍵欄位必須查詢
if ($field->primaryKey) {
return $field; }
}
return false;
}
/**
* 取本表主鍵欄位名稱
* @return SCrudField
*/
public function listSortable(){
return array_filter ( $this->fields, function ($f) {
return array_filter ( $this->fields, function ($f) {
return $f ();
} );
}
/**
* 取得所有可作為排序依據欄位
* @return array of SCrudField
*/
public function listSearchable(){
return array_filter ( $this->fields, function ($f) {
return $f->isSearchable (f->isSearchable ( );
} );
}
/**
* 取得所有可搜尋欄位
* @return multitype:
*/
public function listGridable(){
return array_filter ( $this->fields, function ($f) {
return $f->isable () ;
} );
}
/**
* 取得所有參與清單的欄位
* @return multitype:
*/
public function listViewable(){
return array_filter ( $this->fields, function ($f) {
return $f->isViewable ();
} );
}
/**
* 取得所有參與查看的欄位
* @return multitype:
*/
public function listInsertable(){
return array_filter ( $this->fields, function ($f) {
return $f->isInsertable (f->isInsertable); } );
}
/**
* 取得所有參與創建的欄位
* @return multitype:
*/
public function listUpdatable(){
return array_filter ( $this->fields, function ($f) {
return $f->isUpdatable (); );
}
/**
* 取得所有參與編輯的欄位
* @return multitype:
*/
public function listCreated(){
return array_filter ( $this->fields, function ($field) {
return $field->isCreated;
} );
return $field->isCreated;
} );
}
/**
* 取得所有 建立時間 欄位,通常只有一個
* @return multitype:
*/
public function listUpdated(){
return array_filter ( $this->fields, function ($field) {
return $field->isUpdated;
} );
return $field->isUpdated;
} );
/**
* 取得所有修改時間欄位,通常只有一個
* @return multitype:
*/
public function url($method, $params = array()) {
return STemplate::append ( LUrl::ice () . '/', array_merge ( $params, array (
'c' => $this->controller,
'a' => $this->action,
'm' => $method
) ) );
}
/**
* 增加一個普通多選操作
*
* @return SCrudOperationMulti
*/
public function operationMulti($method) {
return $this->operation->add ( new SCrudOperationMulti ( $this, $method ) );
}
/**
* 增加一個普通單行操作
*
* @return SCrudOperationRow
*/
public function operationRow($od) {
return $this->operation->add ( new SCrudOperationRow ( $this, $method ) );
}
/**
* 增加一個普通全表操作
*
* @return SCrudOperationTable
*/
public function operationTable($method) {
return $this-this> ->add ( new SCrudOperationTable ( $this, $method ) );
}
/**
* 處理請求,由操作類別來實作
*
* @param SRequest $req
*/
public function process(SRequest $req) {
//處理前,先為搜尋條件,列表,建立,修改,檢視,排序初始化欄位
foreach($this->fields as $field){
if(!$field->isAbandon){
$field->process();
}
}
///設定操作
if($this->operationInsert){
$this->operation->insert(new SCrudOperationTable($this,SCrudOperation::METHOD_INSERT));
$this->operation->insert(new SCrudOperation($this ,SCrudOperation::METHOD_DOINSERT));
}
if($this->operationMultiDelete){
$this->operation->insert(new SCrudOperationMulti($this,SCrudOperation::METHOD_DELETEMULTI)); ->operationDelete){
$this->operation->insert(new SCrudOperationRow($this,SCrudOperation::METHOD_DELETE));
}
if($this->operationUpdate){
$this->oper}
if($this->operationUpdate){
$this->operation->insert($this->operationUpdate){
$this->operation->insert($this->)($this) new SCrudOperationRow($this,SCrudOperation::METHOD_UPDATE));
$this->operation->insert(new SCrudOperation($this,SCrudOperation::METHOD_DOUPDATE);
}
if($this-View.($this-View. this->operation->insert(new SCrudOperationRow($this,SCrudOperation::METHOD_VIEW));
}
//轉交操作類別處理
$this->operation->process ( $req );
}
$this->operation->process ( $req );
}
**
* 顯示CRUD片段
* @param unknown $tpl 範本名稱
* @param unknown $params
*/
public function display($tpl, $params = array()) {
display ( self::PATH_VIEW . DIRECTORY_SEPARATOR . $tpl, array_merge ( $params, array (
'array (
=view'
= $ this->url ( SCrudOperation::METHOD_VIEW ),
'url_index' => $this->url ( SCrudOperation::METHOD_INDEX ),
'url_search' => $this->url ( SCrudOperation::HOD_SEHARCHat ) ' => $this->url ( SCrudOperation::METHOD_INSERT ),
'url_update' => $this->url( SCrudOperation::METHOD_UPDATE ),
'url_doupdate' => $this->url ( SCrudOperation::HOOperation::d ,
'url_doinsert' => $this->url ( SCrudOperation::METHOD_DOINSERT ),
'url_delete' =>$this->url ( SCrudOperation::METHOD_DELETE ),
'url_delete_multi'url>thisper> ::METHOD_DELETEMULTI)
) ) );
}
}
/**
* 所有CRUD子類別的基底類別,實作了一個記錄主CRUD物件的方法
*
* @author bluehire
*
*/
// 主CRUD對象
protected $crud;crud;的通用CRUD功能架構(五) SCrud 主控類別的內容,更多相關內容請關注PHP中文網(www.php.cn)!