您的位置:首页 > 编程语言 > PHP开发

IcePHP框架中的快速后台中的通用CRUD功能框架(八) SCrudOperation 指定操作类

2014-01-17 11:09 405 查看
<?php

/**

* 每一个CRUD操作

* @author bluehire

*

*/

class SCrudOperation {

/**

* 主CRUD对象

*

* @var SCrud

*/

protected $crud;

/**

* 过滤条件,只有满足此条件的数据才有此操作

* @var Closure

*/

public $filter;

// 配置信息

protected $config;

// 操作对应的METHOD名

public $method;

//操作显示名

public $title;

// 设置配置信息

public function config(array $config) {

$this->config = $config;

}

/**

* 执行修改的具体 操作

* @param SRequest $req

*/

private function processDoUpdate(SRequest $req){

$primaryField=$this->crud->getPrimaryField();

$fields = $this->crud->listUpdatable ();

$data = array ();

foreach ( $fields as $f ) {

$k = $f->name;

$inputName = 'crud_update_' . $k;

if (! $req->exist ( $inputName ) or ! $req->$inputName) {

$v = $f->defaultValue;

} else {

$v = $req->$inputName;

}

if ($f->isPassword and ! $v) {

continue;

}

if ($f->encode) {

$v = $f->encode ( $v );

}

$data [$k] = $v;

}

$inputName = 'crud_update_' . $primaryField->name;

if (! $req->exist ( $inputName ) or ! $req->$inputName) {

return $this->error ( '错误 的编辑 操作.' );

}

$primaryValue = $req->$inputName;

foreach ( $this->crud->listUpdated () as $f ) {

$data [$f->name] = time ();

}

//@todo:执行真正的修改操作

return $this->ajaxOk(array('msg'=>'修改了一条数据','go'=>'list',dump($data,'data',true)));

}

/**

* 执行新增操作的处理

* @param SRequest $req

*/

private function processDoInsert(SRequest $req){

$fields = $this->crud->listInsertable ();

$data = array ();

foreach ( $fields as $f ) {

$k = $f->name;

$inputName = 'crud_insert_' . $k;

if (! $req->exist ( $inputName ) or ! $req->$inputName) {

$v = $f->defaultValue;

} else {

$v = $req->$inputName;

}

if ($f->encode) {

$v = $f->encode ( $v );

}

$data [$k] = $v;

}

foreach ( array_merge ( $this->crud->listCreated (), $this->crud->listUpdated () ) as $f ) {

$data [$f->name] = time ();

}

//@todo:真正插入一条数据

return $this->ajaxOk(array('msg'=>'插入了一条数据','go'=>'list',dump($data,'data',true)));

}

// 当前操作的具体处理

public function process(SRequest $req){

if($this->method==self::METHOD_DOUPDATE){

return $this->processDoUpdate($req);

}

if($this->method==self::METHOD_DOINSERT){

return $this->processDoInsert($req);

}

}

/**

* 调用错误模板,显示一条错误信息

* @param unknown $msg

*/

protected function error($msg){

$this->crud->display('error',array('msg'=>$msg));

}

/**

* 返回Json成功数据

*

* @param mixed $data 要返回的具体数据

*/

protected function ajaxOk($data) {

echo json_encode ( SDebug::end ( array ('status' => 'success', 'success' => true, 'data' => $data ) ) );

}

/**

* 返回Json失败信息

*

* @param string $msg 错误信息

*/

protected function ajaxError($msg) {

echo json_encode ( SDebug::end ( array ('status' => 'error', 'success' => false, 'msg' => $msg ) ) );

}

//以下是各种系统操作的Method名称

const METHOD_INDEX='Index';

const METHOD_SEARCH='Search';

const METHOD_VIEW='View';

const METHOD_DELETE='Delete';

const METHOD_UPDATE='Update';

const METHOD_DELETEMULTI='DeleteMulti';

const METHOD_INSERT='Insert';

const METHOD_DOUPDATE='DoUpdate';

const METHOD_DOINSERT='DoInsert';

//以下是系统操作的标题

protected $defaultTitle=array(

self::METHOD_DELETE=>'删除',

self::METHOD_DELETEMULTI=>'删除所选',

self::METHOD_DOINSERT=>'确认新增',

self::METHOD_DOUPDATE=>'确认修改',

self::METHOD_INDEX=>'返回首页',

self::METHOD_INSERT=>'新增',

self::METHOD_SEARCH=>'搜索',

self::METHOD_UPDATE=>'修改',

self::METHOD_VIEW=>'查看',

);

/**

* 默认的构造 方法

* @param SCrud $father 主CRUD对象

* @param unknown $method 操作方法名称

*/

public function __construct(SCrud $father, $method) {

$this->crud = $father;

$this->method = $method;

//设置默认操作标题

if(isset($this->defaultTitle[$method])){

$this->title=$this->defaultTitle[$method];

}

}

}

/**

* 行级操作类

*

* @author bluehire

*

*/

class SCrudOperationRow extends SCrudOperation {

/**

* 点击行操作后的具体功能

* @var Closure

*/

public $do;

//接收通用行级操作的请求

public function process(SRequest $req) {

//行级操作,必须传递主键值 ,参数名为id(无论主键是否为id)

if (! $req->exist ( 'id' ) or ! $req->id) {

return $this->error ( '缺少必要的参数' );

}

$id = $req->id;

//获取本表主键

$primaryField = $this->crud->getPrimaryField ();

if ($primaryField->simpleType == 'I' or $primaryField->simpleType == 'R') {

$id = intval ( $id );

} elseif ($primaryField->simpleType == 'C') {

$id = trim ( $id );

} else {

return $this->error ( '主键字段只允许数值及文本类型' );

}

//下面用到的查询条件

$where = array ( $primaryField->name => $id );

switch ($this->method){

case self::METHOD_VIEW:

$fields = array_merge ( array (

$primaryField->name => $primaryField

), $this->crud->listViewable () );

return $this->crud->display ( 'view', array (

'fields' => $fields,

'row' => $this->crud->model->fields ( array_keys ( $fields ) )->where ( $where )->row ()

) );

case self::METHOD_UPDATE:

$fields = array_merge ( array (

$primaryField->name => $primaryField

), $this->crud->listUpdatable () );

return $this->crud->display ( 'update', array (

'fields' => $fields,

'row' => $this->crud->model->fields ( array_keys ( $fields ) )->where ( $where )->row (),

'title' => $this->title

) );

case self::METHOD_DELETE:

// @todo: 真实删除

$this->ajaxOk ( '删除完成' );

return;

}

// 执行普通行级操作

list ( $controller, $action ) = $this->do;

$ret = $controller->$action ( $req );

$ret ['html'] = ob_get_clean ();

return $this->ajaxOk ( $ret );

}

/**

* 显示一个行级操作按钮

*

* @see SCrudOperation::show()

*/

public function show($row = null) {

// 判断是否需要过滤

if ($this->filter) {

$f = $this->filter;

if (! $f ( $row )) {

return;

}

}

switch ($this->method){

case self::METHOD_VIEW:

return $this->crud->display ( 'operation_view', array (

'title' => $this->title,

'row' => $row

) );

case self::METHOD_UPDATE:

return $this->crud->display ( 'operation_update', array (

'title' => $this->title,

'row' => $row

) );

case self::METHOD_DELETE:

return $this->crud->display ( 'operation_delete', array (

'title' => $this->title,

'row' => $row

) );

}

//显示普通行操作按钮

return $this->crud->display ( 'operation_row', array (

'title' => $this->title,

'row' => $row,

'action' => $this->crud->url ( $this->method )

) );

}

}

/**

* 多选操作类

*

* @author bluehire

*

*/

class SCrudOperationMulti extends SCrudOperation {

public $confirm=true;

/**

* 点击行操作后的具体功能

* @var Closure

*/

public $do;

/**

* 具体处理通用多选操作

* @see SCrudOperation::process()

*/

public function process(SRequest $req) {

if(!$req->exist('ids')){

return $this->error('缺少必要的参数');

}

$ids=$req->ids;

if($this->method==self::METHOD_DELETEMULTI){

//@todo:真实删除数据

return $this->ajaxOk('多选删除完成:'.dump($ids,'ids',true));

}

// 执行具体操作

list ( $controller, $action ) = $this->do;

$ret = $controller->$action ( $req );

$ret ['html'] = ob_get_clean ();

return $this->ajaxOk ( $ret );

}

/**

* 显示通用行级操作按钮

* @see SCrudOperation::show()

*/

public function show($row = null) {

if($this->method==self::METHOD_DELETEMULTI){

return $this->crud->display('operation_delete_multi',array('title'=>$this->title));

}

$this->crud->display ( 'operation_multi', array (

'title' => $this->title,

'action' => $this->crud->url ( $this->method ),

'confirm' => $this->confirm

) );

}

}

/**

* 表级操作类

*

* @author bluehire

*

*/

class SCrudOperationTable extends SCrudOperation {

/**

* 具体 处理一个通用表级操作

* @see SCrudOperation::process()

*/

public function process(SRequest $req) {

if($this->method==self::METHOD_INSERT){

$fields = $this->crud->listInsertable ();

return $this->crud->display ( 'insert', array (

'fields' => $fields,

'title' => $this->title

) );

}

// 执行具体操作

list ( $controller, $action ) = $this->do;

$ret = $controller->$action ( $req );

$ret ['html'] = ob_get_clean ();

return $this->ajaxOk ( $ret );

}

/**

* 显示一个表级操作按钮

* @see SCrudOperation::show()

*/

public function show($row = null) {

if($this->method==self::METHOD_INSERT){

return $this->crud->display ( 'operation_insert', array (

'title' => $this->title

) );

}

$this->crud->display ( 'operation_table', array (

'title' => $this->title,

'action' => $this->crud->url ( $this->method )

) );

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐