来源:http://trac.seagullproject.org/wiki/Howto/WorkingWithWizards

使用wizards

FIXME: Malaney to update

Thanks to User/TobiasKuckuck for the following. Note: Wizard functionality needs to be updated to work with 0.5.x code.

概述

在docs/developer/exaples/modules/wizard中包含了从菜单跳到wizard的任意一步。另外,我在项目中添加了下列特性: wizard一直是作为一个弹出窗口使用的而没有一个默认的导航锚,在_toHtml中,下列代码证实了这点:

// cover $url with windowOpen if uri contains string 'Wiz'
if (strstr($section->resource_uri,'Wiz'] {
     $url = 'javascript:openWindow(\'' . $url . '\');';
}

我像其它模块一样使用模板和行为方法。每一步除了某些特殊方法还有”edit”, “update”, “view”方法,第一步添加了”add”和”insert”方法。默认行为是”edit”/“update”。而”add”/“insert”方法只在你以相应的行为变量启动wizard时才被调用。下面你会发现manager调用wizard的第一个步骤代码:

// set action and actionId for editing, etc.
SGL_Session::set('action',$req->get('action'];
SGL_Session::set('actionId',$req->get('actionId'];

第一行中的session变量用来检测是否要执行某个特殊的行为:

//set special action and usrId
if (SGL_Session::get('action'] {
     $defaultAction = SGL_Session::get('action');
     $input->usrId = SGL_Session::get('actionId');
     SGL_Session::remove('action');
     SGL_Session::remove('actionId');
}

每一个步骤在转到下一个步骤之前保存数据。如果用户退出,wizards优先处理所有步骤,它能在稍后完成。

如果session可用,数据是出session得到的,否则它将从数据库读取并保存到session中。因为每一个步骤你只需从数据库读一次数据(一直到退出wizard)。

在最后一个步骤,我关闭弹出窗口结束,并重新载入带有一个用来刷新列表的特殊链接的主窗口。

我为被编辑的数据库记录设置了排它锁。并在wizard正常结束后恢复它。否则在30分钟后自动过期以确保复位排它锁。

在方法”edit”中锁定一行的例子:

$user = & new DataObjects_Usr();
$user->autocommit();

// Lock Table
// check action
// if action = view => don't lock table
if ($output->action != "view") {
   require_once SGL_LIB_DIR . '/other/LockDB.php';
   // instatiate LockDB-class
   $lockDB = new LockDB($user->getDatabaseConnection(];
   $lockOn = $lockDB->setLock($conf['table']['user'],$input->usrId);
}

if ($lockOn) {
   // direct call, no data in session
   // get user data
   $user->get($input->usrId);
   $output->candidate = $user;
   $output->candidate->date_of_birth = SGL_Date::stringToArray($user->date_of_birth);
} else {
   $output->template = 'doc2Blank.html';
   SGL::raiseMsg('table is locked');
}
$user->commit();

去除锁定的例子:


// Unlock Table table_lock
require_once SGL_LIB_DIR . '/other/LockDB.php';
$dbh = &SGL_DB::singleton();
$dbh->autocommit();

// instatiate LockDB-class
$lockDB = new LockDB($dbh);
$lockOff = 
$lockDB->releaseLock($conf['table']['user'],$output->candidate->usr_id);
$dbh->commit();
 
howto/workingwithwizards.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