* Put general method createLink for API into helper.

This commit is contained in:
dingguodong
2021-08-10 13:14:39 +08:00
parent b4f28ca7d7
commit 4c9a8772b6
4 changed files with 216 additions and 61 deletions
+4 -30
View File
@@ -39,7 +39,7 @@ class projectIssueEntry extends entry
$issue->lastEditedDate = $story->lastEditedDate < '1970-01-01 01:01:01' ? $story->openedDate : $story->lastEditedDate;
$issue->lastEditedBy = $story->lastEditedDate < '1970-01-01 01:01:01' ? $story->openedBy : $story->lastEditedBy;
$issue->status = $storyStatus[$story->status];
$issue->url = $this->createLink('story', 'view', "storyID=$id");
$issue->url = helper::createLink('story', 'view', "storyID=$id");
$storySpec = $this->dao->select('*')->from(TABLE_STORYSPEC)->where('story')->eq($id)->andWhere('version')->eq($story->version)->fetch();
$issue->desc = $storySpec->spec;
@@ -61,7 +61,7 @@ class projectIssueEntry extends entry
$issue->lastEditedDate = $bug->lastEditedDate < '1970-01-01 01:01:01' ? $bug->openedDate : $bug->lastEditedDate;
$issue->lastEditedBy = $bug->lastEditedDate < '1970-01-01 01:01:01' ? $bug->openedBy : $bug->lastEditedBy;
$issue->status = $bugStatus[$bug->status];
$issue->url = $this->createLink('bug', 'view', "bugID=$id");
$issue->url = helper::createLink('bug', 'view', "bugID=$id");
$issue->desc = $bug->steps;
break;
case 'task':
@@ -81,7 +81,7 @@ class projectIssueEntry extends entry
$issue->lastEditedDate = $task->lastEditedDate < '1970-01-01 01:01:01' ? $task->openedDate : $task->lastEditedDate;
$issue->lastEditedBy = $task->lastEditedDate < '1970-01-01 01:01:01' ? $task->openedBy : $task->lastEditedBy;
$issue->status = $taskStatus[$task->status];
$issue->url = $this->createLink('task', 'view', "taskID=$id");
$issue->url = helper::createLink('task', 'view', "taskID=$id");
$issue->desc = $task->desc;
break;
@@ -94,32 +94,6 @@ class projectIssueEntry extends entry
$this->send(200, array('issue' => $this->format($issue, 'openedDate:time,lastEditedDate:time')));
}
/**
* Create url of issue.
*
* @param string $module
* @param string $method
* @param string $vars
* @access private
* @return string
*/
private function createLink($module, $method, $vars)
{
$link = helper::createLink($module, $method, $vars, 'html');
$pos = strpos($link, '.php');
/* The requestTypes are: GET, PATH_INFO2, PATH_INFO */
if($this->config->requestType == 'GET')
{
$link = '/index' . substr($link, $pos);
}
elseif($this->config->requestType == 'PATH_INFO2')
{
$link = substr($link, $pos + 4);
}
return common::getSysURL() . $link;
}
/**
* Get the detail of the user.
*
@@ -136,7 +110,7 @@ class projectIssueEntry extends entry
$detail->id = $user->id;
$detail->account = $user->account;
$detail->realname = $user->realname;
$detail->url = $this->createLink('user', 'profile', "userID={$user->id}");
$detail->url = helper::createLink('user', 'profile', "userID={$user->id}");
if($user->avatar != "")
$detail->avatar = common::getSysURL() . $user->avatar;
+4 -30
View File
@@ -151,7 +151,7 @@ class projectIssuesEntry extends entry
$r->lastEditedDate = $task->lastEditedDate < '1970-01-01 01:01:01' ? $task->openedDate : $task->lastEditedDate;
$r->lastEditedBy = $task->lastEditedDate < '1970-01-01 01:01:01' ? $task->openedBy : $task->lastEditedBy;
$r->status = $issue['status'];
$r->url = $this->createLink('task', 'view', "taskID=$task->id");
$r->url = helper::createLink('task', 'view', "taskID=$task->id");
}
else if($issue['type'] == 'story')
{
@@ -167,7 +167,7 @@ class projectIssuesEntry extends entry
$r->lastEditedDate = $story->lastEditedDate < '1970-01-01 01:01:01' ? $story->openedDate : $story->lastEditedDate;
$r->lastEditedBy = $story->lastEditedDate < '1970-01-01 01:01:01' ? $story->openedBy : $story->lastEditedBy;
$r->status = $issue['status'];
$r->url = $this->createLink('story', 'view', "storyID=$story->id");
$r->url = helper::createLink('story', 'view', "storyID=$story->id");
}
else if($issue['type'] == 'bug')
{
@@ -183,7 +183,7 @@ class projectIssuesEntry extends entry
$r->lastEditedDate = $bug->lastEditedDate < '1970-01-01 01:01:01' ? $bug->openedDate : $bug->lastEditedDate;
$r->lastEditedBy = $bug->lastEditedDate < '1970-01-01 01:01:01' ? $bug->openedBy : $bug->lastEditedBy;
$r->status = $issue['status'];
$r->url = $this->createLink('bug', 'view', "bugID=$bug->id");
$r->url = helper::createLink('bug', 'view', "bugID=$bug->id");
}
$result[] = $this->format($r, 'openedDate:time,lastEditedDate:time');
@@ -210,32 +210,6 @@ class projectIssuesEntry extends entry
return '';
}
/**
* Create url of issue.
*
* @param string $module
* @param string $method
* @param string $vars
* @access private
* @return string
*/
private function createLink($module, $method, $vars)
{
$link = helper::createLink($module, $method, $vars, 'html');
$pos = strpos($link, '.php');
/* The requestTypes are: GET, PATH_INFO2, PATH_INFO */
if($this->config->requestType == 'GET')
{
$link = '/index' . substr($link, $pos);
}
elseif($this->config->requestType == 'PATH_INFO2')
{
$link = substr($link, $pos + 4);
}
return common::getSysURL() . $link;
}
/**
* Get the detail of the user.
*
@@ -252,7 +226,7 @@ class projectIssuesEntry extends entry
$detail->id = $user->id;
$detail->account = $user->account;
$detail->realname = $user->realname;
$detail->url = $this->createLink('user', 'profile', "userID={$user->id}");
$detail->url = helper::createLink('user', 'profile', "userID={$user->id}");
if($user->avatar != "")
$detail->avatar = common::getSysURL() . $user->avatar;
+207
View File
@@ -0,0 +1,207 @@
<?php
/**
* ZenTaoAPI的helper类。
* The helper class file of ZenTao API.
*
* The author disclaims copyright to this source code. In place of
* a legal notice, here is a blessing:
*
* May you do good and not evil.
* May you find forgiveness for yourself and forgive others.
* May you share freely, never taking more than you give.
*/
include dirname(dirname(__FILE__)) . '/base/helper.class.php';
class helper extends baseHelper
{
public static function getViewType($source = false)
{
global $config, $app;
if($config->requestType != 'GET')
{
$pathInfo = $app->getPathInfo();
if(!empty($pathInfo))
{
$dotPos = strrpos($pathInfo, '.');
if($dotPos)
{
$viewType = substr($pathInfo, $dotPos + 1);
}
else
{
$config->default->view = $config->default->view == 'mhtml' ? 'html' : $config->default->view;
}
}
}
elseif($config->requestType == 'GET')
{
if(isset($_GET[$config->viewVar]))
{
$viewType = $_GET[$config->viewVar];
}
else
{
/* Set default view when url has not module name. such as only domain. */
$config->default->view = ($config->default->view == 'mhtml' and isset($_GET[$config->moduleVar])) ? 'html' : $config->default->view;
}
}
if($source and isset($viewType)) return $viewType;
if(isset($viewType) and strpos($config->views, ',' . $viewType . ',') === false) $viewType = $config->default->view;
return isset($viewType) ? $viewType : $config->default->view;
}
/**
* Encode json for $.parseJSON
*
* @param array $data
* @param int $options
* @static
* @access public
* @return string
*/
public static function jsonEncode4Parse($data, $options = 0)
{
$json = json_encode($data);
if($options) $json = str_replace(array("'", '"'), array('\u0027', '\u0022'), $json);
$escapers = array("\\", "/", "\"", "'", "\n", "\r", "\t", "\x08", "\x0c", "\\\\u");
$replacements = array("\\\\", "\\/", "\\\"", "\'", "\\n", "\\r", "\\t", "\\f", "\\b", "\\u");
return str_replace($escapers, $replacements, $json);
}
/**
* Convert encoding.
*
* @param string $string
* @param string $fromEncoding
* @param string $toEncoding
* @static
* @access public
* @return string
*/
public static function convertEncoding($string, $fromEncoding, $toEncoding = 'utf-8')
{
$toEncoding = str_replace('utf8', 'utf-8', $toEncoding);
if(function_exists('mb_convert_encoding'))
{
/* Remove like utf-8//TRANSLIT. */
$position = strpos($toEncoding, '//');
if($position !== false) $toEncoding = substr($toEncoding, 0, $position);
/* Check string encoding. */
$encodings = array_merge(array('GB2312','GBK','BIG5'), mb_list_encodings());
$encoding = strtolower(mb_detect_encoding($string, $encodings));
if($encoding == $toEncoding) return $string;
return mb_convert_encoding($string, $toEncoding, $encoding);
}
elseif(function_exists('iconv'))
{
if($fromEncoding == $toEncoding) return $string;
$convertString = @iconv($fromEncoding, $toEncoding, $string);
/* iconv error then return original. */
if(!$convertString) return $string;
return $convertString;
}
return $string;
}
/**
* Calculate two working days.
*
* @param string $begin
* @param string $end
*
* @return bool|float
*/
public static function workDays($begin, $end)
{
$begin = strtotime($begin);
$end = strtotime($end);
if($end < $begin) return false;
$double = floor(($end - $begin) / (7 * 24 * 3600));
$begin = date('w', $begin);
$end = date('w', $end);
$end = $begin > $end ? $end + 5 : $end;
return $double * 5 + $end - $begin;
}
/**
* Unify string to standard chars.
*
* @param string $string
* @param string $to
* @static
* @access public
* @return string
*/
public static function unify($string, $to = ',')
{
$labels = array('_', '、', ' ', '-', '?', '@', '&', '%', '~', '`', '+', '*', '/', '\\', ',', '。');
$string = str_replace($labels, $to, $string);
return preg_replace("/[{$to}]+/", $to, trim($string, $to));
}
/**
* Create url of issue.
*
* @param string $module
* @param string $method
* @param string $vars
* @static
* @access public
* @return string
*/
public static function createLink($module, $method, $vars)
{
global $config;
$link = parent::createLink($module, $method, $vars, 'html');
$pos = strpos($link, '.php');
/* The requestTypes are: GET, PATH_INFO2, PATH_INFO */
if($config->requestType == 'GET')
{
$link = '/index' . substr($link, $pos);
}
elseif($config->requestType == 'PATH_INFO2')
{
$link = substr($link, $pos + 4);
}
return common::getSysURL() . $link;
}
}
/**
* Format time.
*
* @param int $time
* @param string $format
* @access public
* @return void
*/
function formatTime($time, $format = '')
{
$time = str_replace('0000-00-00', '', $time);
$time = str_replace('00:00:00', '', $time);
if(trim($time) == '') return ;
if($format) return date($format, strtotime($time));
return trim($time);
}
/**
* Fix for session error.
*
* @param int $class
* @access protected
* @return void
*/
function autoloader($class)
{
if(!class_exists($class))
{
if($class == 'post_max_size' or $class == 'max_input_vars') eval('class ' . $class . ' {};');
}
}
spl_autoload_register('autoloader');
+1 -1
View File
@@ -20,9 +20,9 @@ ob_start();
/* Load the framework. */
include '../framework/api/router.class.php';
include '../framework/api/entry.class.php';
include '../framework/api/helper.class.php';
include '../framework/control.class.php';
include '../framework/model.class.php';
include '../framework/helper.class.php';
/* Log the time and define the run mode. */
$startTime = getTime();