diff options
Diffstat (limited to 'app/Helper')
| -rw-r--r-- | app/Helper/Asset.php | 4 | ||||
| -rw-r--r-- | app/Helper/Board.php | 24 | ||||
| -rw-r--r-- | app/Helper/Dt.php (renamed from app/Helper/Datetime.php) | 22 | ||||
| -rw-r--r-- | app/Helper/Url.php | 98 | ||||
| -rw-r--r-- | app/Helper/User.php | 18 |
5 files changed, 137 insertions, 29 deletions
diff --git a/app/Helper/Asset.php b/app/Helper/Asset.php index 1b1e47c5..fd555e07 100644 --- a/app/Helper/Asset.php +++ b/app/Helper/Asset.php @@ -18,7 +18,7 @@ class Asset extends \Core\Base */ public function js($filename, $async = false) { - return '<script '.($async ? 'async' : '').' type="text/javascript" src="'.$filename.'?'.filemtime($filename).'"></script>'; + return '<script '.($async ? 'async' : '').' type="text/javascript" src="'.$this->helper->url->dir().$filename.'?'.filemtime($filename).'"></script>'; } /** @@ -31,7 +31,7 @@ class Asset extends \Core\Base */ public function css($filename, $is_file = true, $media = 'screen') { - return '<link rel="stylesheet" href="'.$filename.($is_file ? '?'.filemtime($filename) : '').'" media="'.$media.'">'; + return '<link rel="stylesheet" href="'.$this->helper->url->dir().$filename.($is_file ? '?'.filemtime($filename) : '').'" media="'.$media.'">'; } /** diff --git a/app/Helper/Board.php b/app/Helper/Board.php new file mode 100644 index 00000000..452a3b70 --- /dev/null +++ b/app/Helper/Board.php @@ -0,0 +1,24 @@ +<?php + +namespace Helper; + +/** + * Board Helper + * + * @package helper + * @author Frederic Guillot + */ +class Board extends \Core\Base +{ + /** + * Return true if tasks are collapsed + * + * @access public + * @param integer $project_id + * @return boolean + */ + public function isCollapsed($project_id) + { + return $this->userSession->isBoardCollapsed($project_id); + } +} diff --git a/app/Helper/Datetime.php b/app/Helper/Dt.php index 74ea9bdd..b338fdc8 100644 --- a/app/Helper/Datetime.php +++ b/app/Helper/Dt.php @@ -2,15 +2,35 @@ namespace Helper; +use DateTime; + /** * DateTime helpers * * @package helper * @author Frederic Guillot */ -class Datetime extends \Core\Base +class Dt extends \Core\Base { /** + * Get duration in seconds into human format + * + * @access public + * @param integer $seconds + * @return string + */ + public function duration($seconds) + { + if ($seconds == 0) { + return 0; + } + + $dtF = new DateTime("@0"); + $dtT = new DateTime("@$seconds"); + return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds'); + } + + /** * Get the age of an item in quasi human readable format. * It's in this format: <1h , NNh, NNd * diff --git a/app/Helper/Url.php b/app/Helper/Url.php index e133f195..964e0762 100644 --- a/app/Helper/Url.php +++ b/app/Helper/Url.php @@ -13,6 +13,9 @@ use Core\Security; */ class Url extends \Core\Base { + private $base = ''; + private $directory = ''; + /** * HTML Link tag * @@ -33,7 +36,7 @@ class Url extends \Core\Base } /** - * Hyperlink + * HTML Hyperlink * * @access public * @param string $controller Controller name @@ -41,22 +44,12 @@ class Url extends \Core\Base * @param array $params Url parameters * @param boolean $csrf Add a CSRF token * @param string $anchor Link Anchor + * @param boolean $absolute Absolute or relative link * @return string */ - public function href($controller, $action, array $params = array(), $csrf = false, $anchor = '') + public function href($controller, $action, array $params = array(), $csrf = false, $anchor = '', $absolute = false) { - $values = array( - 'controller' => $controller, - 'action' => $action, - ); - - if ($csrf) { - $params['csrf_token'] = Security::getCSRFToken(); - } - - $values += $params; - - return '?'.http_build_query($values, '', '&').(empty($anchor) ? '' : '#'.$anchor); + return $this->build('&', $controller, $action, $params, $csrf, $anchor, $absolute); } /** @@ -66,18 +59,13 @@ class Url extends \Core\Base * @param string $controller Controller name * @param string $action Action name * @param array $params Url parameters + * @param string $anchor Link Anchor + * @param boolean $absolute Absolute or relative link * @return string */ - public function to($controller, $action, array $params = array()) + public function to($controller, $action, array $params = array(), $anchor = '', $absolute = false) { - $values = array( - 'controller' => $controller, - 'action' => $action, - ); - - $values += $params; - - return '?'.http_build_query($values, '', '&'); + return $this->build('&', $controller, $action, $params, false, $anchor, $absolute); } /** @@ -88,7 +76,28 @@ class Url extends \Core\Base */ public function base() { - return $this->config->get('application_url') ?: $this->server(); + if (empty($this->base)) { + $this->base = $this->config->get('application_url') ?: $this->server(); + } + + return $this->base; + } + + /** + * Get application base directory + * + * @access public + * @return string + */ + public function dir() + { + if (empty($this->directory) && isset($_SERVER['REQUEST_METHOD'])) { + $this->directory = str_replace('\\', '/', dirname($_SERVER['PHP_SELF'])); + $this->directory = $this->directory !== '/' ? $this->directory.'/' : '/'; + $this->directory = str_replace('//', '/', $this->directory); + } + + return $this->directory; } /** @@ -99,13 +108,50 @@ class Url extends \Core\Base */ public function server() { - $self = str_replace('\\', '/', dirname($_SERVER['PHP_SELF'])); + if (empty($_SERVER['SERVER_NAME'])) { + return 'http://localhost/'; + } $url = Request::isHTTPS() ? 'https://' : 'http://'; $url .= $_SERVER['SERVER_NAME']; $url .= $_SERVER['SERVER_PORT'] == 80 || $_SERVER['SERVER_PORT'] == 443 ? '' : ':'.$_SERVER['SERVER_PORT']; - $url .= $self !== '/' ? $self.'/' : '/'; + $url .= $this->dir() ?: '/'; return $url; } + + /** + * Build relative url + * + * @access private + * @param string $separator Querystring argument separator + * @param string $controller Controller name + * @param string $action Action name + * @param array $params Url parameters + * @param boolean $csrf Add a CSRF token + * @param string $anchor Link Anchor + * @param boolean $absolute Absolute or relative link + * @return string + */ + private function build($separator, $controller, $action, array $params = array(), $csrf = false, $anchor = '', $absolute = false) + { + $path = $this->router->findUrl($controller, $action, $params); + $qs = array(); + + if (empty($path)) { + $qs['controller'] = $controller; + $qs['action'] = $action; + $qs += $params; + } + + if ($csrf) { + $qs['csrf_token'] = Security::getCSRFToken(); + } + + if (! empty($qs)) { + $path .= '?'.http_build_query($qs, '', $separator); + } + + return ($absolute ? $this->base() : $this->dir()).$path.(empty($anchor) ? '' : '#'.$anchor); + } } diff --git a/app/Helper/User.php b/app/Helper/User.php index 1cad6042..c1fff8c6 100644 --- a/app/Helper/User.php +++ b/app/Helper/User.php @@ -11,6 +11,24 @@ namespace Helper; class User extends \Core\Base { /** + * Get initials from a user + * + * @access public + * @param string $name + * @return string + */ + public function getInitials($name) + { + $initials = ''; + + foreach (explode(' ', $name) as $string) { + $initials .= mb_substr($string, 0, 1); + } + + return mb_strtoupper($initials); + } + + /** * Get user id * * @access public |
