. * * @copyright Copyright 2009-2010 Chunsheng Wang * @author Chunsheng Wang * @package action * @version $Id$ * @link http://www.zentao.cn */ ?> company = $this->app->company->id; $action->objectType = $objectType; $action->objectID = $objectID; $action->actor = $this->app->user->account; $action->action = $actionType; $action->date = date('Y-m-d H:i:s'); $action->comment = htmlspecialchars($comment); $action->extra = $extra; $this->dao->insert(TABLE_ACTION)->data($action)->autoCheck()->exec(); return $this->dbh->lastInsertID(); } /* 返回某一个对象的所有action列表。*/ public function getList($objectType, $objectID) { $actions = $this->dao->select('*')->from(TABLE_ACTION)->where('objectType')->eq($objectType)->andWhere('objectID')->eq($objectID)->orderBy('id')->fetchAll('id'); $histories = $this->getHistory(array_keys($actions)); foreach($actions as $actionID => $action) { $action->history = isset($histories[$actionID]) ? $histories[$actionID] : array(); $actions[$actionID] = $action; } return $actions; } /* 获得action信息。*/ public function getById($actionID) { return $this->dao->findById((int)$actionID)->from(TABLE_ACTION)->fetch(); } /* 返回某一个action所对应的字段修改记录。*/ public function getHistory($actionID) { return $this->dao->select()->from(TABLE_HISTORY)->where('action')->in($actionID)->orderBy('id')->fetchGroup('action'); } /* 记录历史。*/ public function logHistory($actionID, $changes) { foreach($changes as $change) { $change['action'] = $actionID; $this->dao->insert(TABLE_HISTORY)->data($change)->exec(); } } /* 打印action标题。*/ public function printAction($action) { $objectType = $action->objectType; $actionType = strtolower($action->action); if(isset($this->lang->$objectType->action->$actionType)) { $desc = $this->lang->$objectType->action->$actionType; } elseif(isset($this->lang->action->desc->$actionType)) { $desc = $this->lang->action->desc->$actionType; } else { $desc = $action->extra ? $this->lang->action->desc->extra : $this->lang->action->desc->common; } foreach($action as $key => $value) { if($key == 'history') continue; if(is_array($desc)) { if($key == 'extra') continue; $desc['main'] = str_replace('$' . $key, $value, $desc['main']); } else { $desc = str_replace('$' . $key, $value, $desc); } } if(is_array($desc)) { $extra = strtolower($action->extra); if(isset($desc['extra'][$extra])) { echo str_replace('$extra', $desc['extra'][$extra], $desc['main']); } else { echo str_replace('$extra', $action->extra, $desc['main']); } } else { echo $desc; } } /* 打印动态信息。*/ public function getDynamic($objectType = 'all', $count = 30) { $actions = $this->dao->select('*')->from(TABLE_ACTION)->onCaseOf($objectType != 'all')->where('objectType')->eq($objectType)->endCase()->orderBy('id desc')->limit($count)->fetchAll(); if(!$actions) return array(); foreach($actions as $action) { $actionType = strtolower($action->action); $objectType = strtolower($action->objectType); $action->date = date('H:i', strtotime($action->date)); $action->actionLabel = isset($this->lang->action->label->$actionType) ? $this->lang->action->label->$actionType : $action->action; $action->objectLabel = isset($this->lang->action->label->$objectType) ? $this->lang->action->label->$objectType : $objectType; if(strpos($action->objectLabel, '|') !== false) { list($objectLabel, $moduleName, $methodName, $vars) = explode('|', $action->objectLabel); $action->objectLink = html::a(helper::createLink($moduleName, $methodName, sprintf($vars, $action->objectID)), '#' . $action->objectID); $action->objectLabel = $objectLabel; } else { $action->objectLink = '#' . $action->objectID; } } return $actions; } /* 打印修改记录。*/ public function printChanges($objectType, $histories) { if(empty($histories)) return; /* 计算字段的最大长度,并将历史记录根据是否有diff分开,以保证含有diff的字段显示在最后面。*/ $maxLength = 0; $historiesWithDiff = array(); $historiesWithoutDiff = array(); foreach($histories as $history) { $fieldName = $history->field; $history->fieldLabel = isset($this->lang->$objectType->$fieldName) ? $this->lang->$objectType->$fieldName : $fieldName; if(($length = strlen($history->fieldLabel)) > $maxLength) $maxLength = $length; $history->diff ? $historiesWithDiff[] = $history : $historiesWithoutDiff[] = $history; } $histories = array_merge($historiesWithoutDiff, $historiesWithDiff); foreach($histories as $history) { $history->fieldLabel = str_pad($history->fieldLabel, $maxLength, $this->lang->action->label->space); if($history->diff != '') { printf($this->lang->action->desc->diff2, $history->fieldLabel, nl2br($history->diff)); } else { printf($this->lang->action->desc->diff1, $history->fieldLabel, $history->old, $history->new); } } } }