diff options
Diffstat (limited to 'app/Helper')
-rw-r--r-- | app/Helper/App.php | 56 | ||||
-rw-r--r-- | app/Helper/Asset.php | 51 | ||||
-rw-r--r-- | app/Helper/Datetime.php | 61 | ||||
-rw-r--r-- | app/Helper/File.php | 56 | ||||
-rw-r--r-- | app/Helper/Form.php | 323 | ||||
-rw-r--r-- | app/Helper/Subtask.php | 42 | ||||
-rw-r--r-- | app/Helper/Task.php | 59 | ||||
-rw-r--r-- | app/Helper/Text.php | 91 | ||||
-rw-r--r-- | app/Helper/Url.php | 115 | ||||
-rw-r--r-- | app/Helper/User.php | 93 |
10 files changed, 947 insertions, 0 deletions
diff --git a/app/Helper/App.php b/app/Helper/App.php new file mode 100644 index 00000000..8f591143 --- /dev/null +++ b/app/Helper/App.php @@ -0,0 +1,56 @@ +<?php + +namespace Helper; + +/** + * Application helpers + * + * @package helper + * @author Frederic Guillot + */ +class App extends \Core\Base +{ + /** + * Get javascript language code + * + * @access public + * @return string + */ + public function jsLang() + { + return $this->config->getJsLanguageCode(); + } + + /** + * Get current timezone + * + * @access public + * @return string + */ + public function getTimezone() + { + return $this->config->getCurrentTimezone(); + } + + /** + * Get session flash message + * + * @access public + * @return string + */ + public function flashMessage() + { + $html = ''; + + if (isset($this->session['flash_message'])) { + $html = '<div class="alert alert-success alert-fade-out">'.$this->helper->e($this->session['flash_message']).'</div>'; + unset($this->session['flash_message']); + } + else if (isset($this->session['flash_error_message'])) { + $html = '<div class="alert alert-error">'.$this->helper->e($this->session['flash_error_message']).'</div>'; + unset($this->session['flash_error_message']); + } + + return $html; + } +} diff --git a/app/Helper/Asset.php b/app/Helper/Asset.php new file mode 100644 index 00000000..fe285081 --- /dev/null +++ b/app/Helper/Asset.php @@ -0,0 +1,51 @@ +<?php + +namespace Helper; + +/** + * Assets helpers + * + * @package helper + * @author Frederic Guillot + */ +class Asset extends \Core\Base +{ + /** + * Add a Javascript asset + * + * @param string $filename Filename + * @return string + */ + public function js($filename) + { + return '<script type="text/javascript" src="'.$filename.'?'.filemtime($filename).'"></script>'; + } + + /** + * Add a stylesheet asset + * + * @param string $filename Filename + * @param boolean $is_file Add file timestamp + * @param string $media Media + * @return string + */ + public function css($filename, $is_file = true, $media = 'screen') + { + return '<link rel="stylesheet" href="'.$filename.($is_file ? '?'.filemtime($filename) : '').'" media="'.$media.'">'; + } + + /** + * Get custom css + * + * @access public + * @return string + */ + public function customCss() + { + if ($this->config->get('application_stylesheet')) { + return '<style>'.$this->config->get('application_stylesheet').'</style>'; + } + + return ''; + } +} diff --git a/app/Helper/Datetime.php b/app/Helper/Datetime.php new file mode 100644 index 00000000..3a9c4c48 --- /dev/null +++ b/app/Helper/Datetime.php @@ -0,0 +1,61 @@ +<?php + +namespace Helper; + +/** + * DateTime helpers + * + * @package helper + * @author Frederic Guillot + */ +class Datetime extends \Core\Base +{ + /** + * Get all hours for day + * + * @access public + * @return array + */ + public function getDayHours() + { + $values = array(); + + foreach (range(0, 23) as $hour) { + foreach (array(0, 30) as $minute) { + $time = sprintf('%02d:%02d', $hour, $minute); + $values[$time] = $time; + } + } + + return $values; + } + + /** + * Get all days of a week + * + * @access public + * @return array + */ + public function getWeekDays() + { + $values = array(); + + foreach (range(1, 7) as $day) { + $values[$day] = $this->getWeekDay($day); + } + + return $values; + } + + /** + * Get the localized day name from the day number + * + * @access public + * @param integer $day Day number + * @return string + */ + public function getWeekDay($day) + { + return dt('%A', strtotime('next Monday +'.($day - 1).' days')); + } +} diff --git a/app/Helper/File.php b/app/Helper/File.php new file mode 100644 index 00000000..a35e4283 --- /dev/null +++ b/app/Helper/File.php @@ -0,0 +1,56 @@ +<?php + +namespace Helper; + +/** + * File helpers + * + * @package helper + * @author Frederic Guillot + */ +class File extends \Core\Base +{ + /** + * Get file icon + * + * @access public + * @param string $filename Filename + * @return string Font-Awesome-Icon-Name + */ + public function icon($filename){ + + $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); + + switch ($extension) { + case 'jpeg': + case 'jpg': + case 'png': + case 'gif': + return 'fa-file-image-o'; + case 'xls': + case 'xlsx': + return 'fa-file-excel-o'; + case 'doc': + case 'docx': + return 'fa-file-word-o'; + case 'ppt': + case 'pptx': + return 'fa-file-powerpoint-o'; + case 'zip': + case 'rar': + return 'fa-file-archive-o'; + case 'mp3': + return 'fa-audio-o'; + case 'avi': + return 'fa-video-o'; + case 'php': + case 'html': + case 'css': + return 'fa-code-o'; + case 'pdf': + return 'fa-file-pdf-o'; + } + + return 'fa-file-o'; + } +} diff --git a/app/Helper/Form.php b/app/Helper/Form.php new file mode 100644 index 00000000..83e3b113 --- /dev/null +++ b/app/Helper/Form.php @@ -0,0 +1,323 @@ +<?php + +namespace Helper; + +use Core\Security; + +/** + * Form helpers + * + * @package helper + * @author Frederic Guillot + */ +class Form extends \Core\Base +{ + /** + * Hidden CSRF token field + * + * @access public + * @return string + */ + public function csrf() + { + return '<input type="hidden" name="csrf_token" value="'.Security::getCSRFToken().'"/>'; + } + + /** + * Display a hidden form field + * + * @access public + * @param string $name Field name + * @param array $values Form values + * @return string + */ + public function hidden($name, array $values = array()) + { + return '<input type="hidden" name="'.$name.'" id="form-'.$name.'" '.$this->formValue($values, $name).'/>'; + } + + /** + * Display a select field + * + * @access public + * @param string $name Field name + * @param array $options Options + * @param array $values Form values + * @param array $errors Form errors + * @param string $class CSS class + * @return string + */ + public function select($name, array $options, array $values = array(), array $errors = array(), array $attributes = array(), $class = '') + { + $html = '<select name="'.$name.'" id="form-'.$name.'" class="'.$class.'" '.implode(' ', $attributes).'>'; + + foreach ($options as $id => $value) { + + $html .= '<option value="'.$this->helper->e($id).'"'; + + if (isset($values->$name) && $id == $values->$name) $html .= ' selected="selected"'; + if (isset($values[$name]) && $id == $values[$name]) $html .= ' selected="selected"'; + + $html .= '>'.$this->helper->e($value).'</option>'; + } + + $html .= '</select>'; + $html .= $this->errorList($errors, $name); + + return $html; + } + + /** + * Display a radio field group + * + * @access public + * @param string $name Field name + * @param array $options Options + * @param array $values Form values + * @return string + */ + public function radios($name, array $options, array $values = array()) + { + $html = ''; + + foreach ($options as $value => $label) { + $html .= $this->radio($name, $label, $value, isset($values[$name]) && $values[$name] == $value); + } + + return $html; + } + + /** + * Display a radio field + * + * @access public + * @param string $name Field name + * @param string $label Form label + * @param string $value Form value + * @param boolean $selected Field selected or not + * @param string $class CSS class + * @return string + */ + public function radio($name, $label, $value, $selected = false, $class = '') + { + return '<label><input type="radio" name="'.$name.'" class="'.$class.'" value="'.$this->helper->e($value).'" '.($selected ? 'checked="checked"' : '').'> '.$this->helper->e($label).'</label>'; + } + + /** + * Display a checkbox field + * + * @access public + * @param string $name Field name + * @param string $label Form label + * @param string $value Form value + * @param boolean $checked Field selected or not + * @param string $class CSS class + * @return string + */ + public function checkbox($name, $label, $value, $checked = false, $class = '') + { + return '<label><input type="checkbox" name="'.$name.'" class="'.$class.'" value="'.$this->helper->e($value).'" '.($checked ? 'checked="checked"' : '').'> '.$this->helper->e($label).'</label>'; + } + + /** + * Display a form label + * + * @access public + * @param string $name Field name + * @param string $label Form label + * @param array $attributes HTML attributes + * @return string + */ + public function label($label, $name, array $attributes = array()) + { + return '<label for="form-'.$name.'" '.implode(' ', $attributes).'>'.$this->helper->e($label).'</label>'; + } + + /** + * Display a textarea + * + * @access public + * @param string $name Field name + * @param array $values Form values + * @param array $errors Form errors + * @param array $attributes HTML attributes + * @param string $class CSS class + * @return string + */ + public function textarea($name, $values = array(), array $errors = array(), array $attributes = array(), $class = '') + { + $class .= $this->errorClass($errors, $name); + + $html = '<textarea name="'.$name.'" id="form-'.$name.'" class="'.$class.'" '; + $html .= implode(' ', $attributes).'>'; + $html .= isset($values->$name) ? $this->helper->e($values->$name) : isset($values[$name]) ? $values[$name] : ''; + $html .= '</textarea>'; + $html .= $this->errorList($errors, $name); + + return $html; + } + + /** + * Display a input field + * + * @access public + * @param string $type HMTL input tag type + * @param string $name Field name + * @param array $values Form values + * @param array $errors Form errors + * @param array $attributes HTML attributes + * @param string $class CSS class + * @return string + */ + public function input($type, $name, $values = array(), array $errors = array(), array $attributes = array(), $class = '') + { + $class .= $this->errorClass($errors, $name); + + $html = '<input type="'.$type.'" name="'.$name.'" id="form-'.$name.'" '.$this->formValue($values, $name).' class="'.$class.'" '; + $html .= implode(' ', $attributes).'>'; + + if (in_array('required', $attributes)) { + $html .= '<span class="form-required">*</span>'; + } + + $html .= $this->errorList($errors, $name); + + return $html; + } + + /** + * Display a text field + * + * @access public + * @param string $name Field name + * @param array $values Form values + * @param array $errors Form errors + * @param array $attributes HTML attributes + * @param string $class CSS class + * @return string + */ + public function text($name, $values = array(), array $errors = array(), array $attributes = array(), $class = '') + { + return $this->input('text', $name, $values, $errors, $attributes, $class); + } + + /** + * Display a password field + * + * @access public + * @param string $name Field name + * @param array $values Form values + * @param array $errors Form errors + * @param array $attributes HTML attributes + * @param string $class CSS class + * @return string + */ + public function password($name, $values = array(), array $errors = array(), array $attributes = array(), $class = '') + { + return $this->input('password', $name, $values, $errors, $attributes, $class); + } + + /** + * Display an email field + * + * @access public + * @param string $name Field name + * @param array $values Form values + * @param array $errors Form errors + * @param array $attributes HTML attributes + * @param string $class CSS class + * @return string + */ + public function email($name, $values = array(), array $errors = array(), array $attributes = array(), $class = '') + { + return $this->input('email', $name, $values, $errors, $attributes, $class); + } + + /** + * Display a number field + * + * @access public + * @param string $name Field name + * @param array $values Form values + * @param array $errors Form errors + * @param array $attributes HTML attributes + * @param string $class CSS class + * @return string + */ + public function number($name, $values = array(), array $errors = array(), array $attributes = array(), $class = '') + { + return $this->input('number', $name, $values, $errors, $attributes, $class); + } + + /** + * Display a numeric field (allow decimal number) + * + * @access public + * @param string $name Field name + * @param array $values Form values + * @param array $errors Form errors + * @param array $attributes HTML attributes + * @param string $class CSS class + * @return string + */ + public function numeric($name, $values = array(), array $errors = array(), array $attributes = array(), $class = '') + { + return $this->input('text', $name, $values, $errors, $attributes, $class.' form-numeric'); + } + + /** + * Display the form error class + * + * @access private + * @param array $errors Error list + * @param string $name Field name + * @return string + */ + private function errorClass(array $errors, $name) + { + return ! isset($errors[$name]) ? '' : ' form-error'; + } + + /** + * Display a list of form errors + * + * @access private + * @param array $errors List of errors + * @param string $name Field name + * @return string + */ + private function errorList(array $errors, $name) + { + $html = ''; + + if (isset($errors[$name])) { + + $html .= '<ul class="form-errors">'; + + foreach ($errors[$name] as $error) { + $html .= '<li>'.$this->helper->e($error).'</li>'; + } + + $html .= '</ul>'; + } + + return $html; + } + + /** + * Get an escaped form value + * + * @access private + * @param mixed $values Values + * @param string $name Field name + * @return string + */ + private function formValue($values, $name) + { + if (isset($values->$name)) { + return 'value="'.$this->helper->e($values->$name).'"'; + } + + return isset($values[$name]) ? 'value="'.$this->helper->e($values[$name]).'"' : ''; + } +} diff --git a/app/Helper/Subtask.php b/app/Helper/Subtask.php new file mode 100644 index 00000000..6348ebd1 --- /dev/null +++ b/app/Helper/Subtask.php @@ -0,0 +1,42 @@ +<?php + +namespace Helper; + +/** + * Subtask helpers + * + * @package helper + * @author Frederic Guillot + */ +class Subtask extends \Core\Base +{ + /** + * Get the link to toggle subtask status + * + * @access public + * @param array $subtask + * @param string $redirect + * @return string + */ + public function toggleStatus(array $subtask, $redirect) + { + if ($subtask['status'] == 0 && isset($this->session['has_subtask_inprogress']) && $this->session['has_subtask_inprogress'] === true) { + + return $this->helper->url->link( + trim($this->template->render('subtask/icons', array('subtask' => $subtask))) . $this->helper->e($subtask['title']), + 'subtask', + 'subtaskRestriction', + array('task_id' => $subtask['task_id'], 'subtask_id' => $subtask['id'], 'redirect' => $redirect), + false, + 'popover task-board-popover' + ); + } + + return $this->helper->url->link( + trim($this->template->render('subtask/icons', array('subtask' => $subtask))) . $this->helper->e($subtask['title']), + 'subtask', + 'toggleStatus', + array('task_id' => $subtask['task_id'], 'subtask_id' => $subtask['id'], 'redirect' => $redirect) + ); + } +} diff --git a/app/Helper/Task.php b/app/Helper/Task.php new file mode 100644 index 00000000..b3931cdb --- /dev/null +++ b/app/Helper/Task.php @@ -0,0 +1,59 @@ +<?php + +namespace Helper; + +/** + * Task helpers + * + * @package helper + * @author Frederic Guillot + */ +class Task extends \Core\Base +{ + /** + * Get the age of an item in quasi human readable format. + * It's in this format: <1h , NNh, NNd + * + * @access public + * @param integer $timestamp Unix timestamp of the artifact for which age will be calculated + * @param integer $now Compare with this timestamp (Default value is the current unix timestamp) + * @return string + */ + public function age($timestamp, $now = null) + { + if ($now === null) { + $now = time(); + } + + $diff = $now - $timestamp; + + if ($diff < 3600) { + return t('<1h'); + } + else if ($diff < 86400) { + return t('%dh', $diff / 3600); + } + + return t('%dd', ($now - $timestamp) / 86400); + } + + public function recurrenceTriggers() + { + return $this->task->getRecurrenceTriggerList(); + } + + public function recurrenceTimeframes() + { + return $this->task->getRecurrenceTimeframeList(); + } + + public function recurrenceBasedates() + { + return $this->task->getRecurrenceBasedateList(); + } + + public function canRemove(array $task) + { + return $this->taskPermission->canRemoveTask($task); + } +} diff --git a/app/Helper/Text.php b/app/Helper/Text.php new file mode 100644 index 00000000..cfb557b1 --- /dev/null +++ b/app/Helper/Text.php @@ -0,0 +1,91 @@ +<?php + +namespace Helper; + +use Core\Markdown; + +/** + * Text helpers + * + * @package helper + * @author Frederic Guillot + */ +class Text extends \Core\Base +{ + /** + * Markdown transformation + * + * @param string $text Markdown content + * @param array $link Link parameters for replacement + * @return string + */ + public function markdown($text, array $link = array()) + { + $parser = new Markdown($link, $this->helper->url); + $parser->setMarkupEscaped(MARKDOWN_ESCAPE_HTML); + return $parser->text($text); + } + + /** + * Format a file size + * + * @param integer $size Size in bytes + * @param integer $precision Precision + * @return string + */ + public function bytes($size, $precision = 2) + { + $base = log($size) / log(1024); + $suffixes = array('', 'k', 'M', 'G', 'T'); + + return round(pow(1024, $base - floor($base)), $precision).$suffixes[(int)floor($base)]; + } + + /** + * Truncate a long text + * + * @param string $value Text + * @param integer $max_length Max Length + * @param string $end Text end + * @return string + */ + public function truncate($value, $max_length = 85, $end = '[...]') + { + $length = strlen($value); + + if ($length > $max_length) { + return substr($value, 0, $max_length).' '.$end; + } + + return $value; + } + + /** + * Return true if needle is contained in the haystack + * + * @param string $haystack Haystack + * @param string $needle Needle + * @return boolean + */ + public function contains($haystack, $needle) + { + return strpos($haystack, $needle) !== false; + } + + /** + * Return a value from a dictionary + * + * @param mixed $id Key + * @param array $listing Dictionary + * @param string $default_value Value displayed when the key doesn't exists + * @return string + */ + public function in($id, array $listing, $default_value = '?') + { + if (isset($listing[$id])) { + return $this->helper->e($listing[$id]); + } + + return $default_value; + } +} diff --git a/app/Helper/Url.php b/app/Helper/Url.php new file mode 100644 index 00000000..9bb38e59 --- /dev/null +++ b/app/Helper/Url.php @@ -0,0 +1,115 @@ +<?php + +namespace Helper; + +use Core\Request; +use Core\Security; + +/** + * Url helpers + * + * @package helper + * @author Frederic Guillot + */ +class Url extends \Core\Base +{ + /** + * HTML Link tag + * + * @access public + * @param string $label Link label + * @param string $controller Controller name + * @param string $action Action name + * @param array $params Url parameters + * @param boolean $csrf Add a CSRF token + * @param string $class CSS class attribute + * @param boolean $new_tab Open the link in a new tab + * @return string + */ + public function link($label, $controller, $action, array $params = array(), $csrf = false, $class = '', $title = '', $new_tab = false) + { + return '<a href="'.$this->href($controller, $action, $params, $csrf).'" class="'.$class.'" title="'.$title.'" '.($new_tab ? 'target="_blank"' : '').'>'.$label.'</a>'; + } + + /** + * Hyperlink + * + * @access public + * @param string $controller Controller name + * @param string $action Action name + * @param array $params Url parameters + * @param boolean $csrf Add a CSRF token + * @return string + */ + public function href($controller, $action, array $params = array(), $csrf = false) + { + $values = array( + 'controller' => $controller, + 'action' => $action, + ); + + if ($csrf) { + $params['csrf_token'] = Security::getCSRFToken(); + } + + $values += $params; + + return '?'.http_build_query($values, '', '&'); + } + + /** + * Generate controller/action url + * + * @access public + * @param string $controller Controller name + * @param string $action Action name + * @param array $params Url parameters + * @return string + */ + public function to($controller, $action, array $params = array()) + { + $values = array( + 'controller' => $controller, + 'action' => $action, + ); + + $values += $params; + + return '?'.http_build_query($values, '', '&'); + } + + /** + * Get application base url + * + * @access public + * @return string + */ + public function base() + { + $application_url = $this->config->get('application_url'); + + if (! empty($application_url)) { + return $application_url; + } + + return $this->server(); + } + + /** + * Get current server base url + * + * @access public + * @return string + */ + public function server() + { + $self = str_replace('\\', '/', dirname($_SERVER['PHP_SELF'])); + + $url = Request::isHTTPS() ? 'https://' : 'http://'; + $url .= $_SERVER['SERVER_NAME']; + $url .= $_SERVER['SERVER_PORT'] == 80 || $_SERVER['SERVER_PORT'] == 443 ? '' : ':'.$_SERVER['SERVER_PORT']; + $url .= $self !== '/' ? $self.'/' : '/'; + + return $url; + } +} diff --git a/app/Helper/User.php b/app/Helper/User.php new file mode 100644 index 00000000..00018857 --- /dev/null +++ b/app/Helper/User.php @@ -0,0 +1,93 @@ +<?php + +namespace Helper; + +/** + * User helpers + * + * @package helper + * @author Frederic Guillot + */ +class User extends \Core\Base +{ + /** + * Get user profile + * + * @access public + * @return string + */ + public function getProfileLink() + { + return $this->helper->url->link( + $this->helper->e($this->getFullname()), + 'user', + 'show', + array('user_id' => $this->userSession->getId()) + ); + } + /** + * Check if the given user_id is the connected user + * + * @param integer $user_id User id + * @return boolean + */ + public function isCurrentUser($user_id) + { + return $this->userSession->getId() == $user_id; + } + + /** + * Return if the logged user is admin + * + * @access public + * @return boolean + */ + public function isAdmin() + { + return $this->userSession->isAdmin(); + } + + /** + * Proxy cache helper for acl::isManagerActionAllowed() + * + * @access public + * @param integer $project_id + * @return boolean + */ + public function isManager($project_id) + { + if ($this->userSession->isAdmin()) { + return true; + } + + return $this->memoryCache->proxy('acl', 'isManagerActionAllowed', $project_id); + } + + /** + * Return the user full name + * + * @param array $user User properties + * @return string + */ + public function getFullname(array $user = array()) + { + return $this->user->getFullname(empty($user) ? $_SESSION['user'] : $user); + } + + /** + * Display gravatar image + * + * @access public + * @param string $email + * @param string $alt + * @return string + */ + public function avatar($email, $alt = '') + { + if (! empty($email) && $this->config->get('integration_gravatar') == 1) { + return '<img class="avatar" src="https://www.gravatar.com/avatar/'.md5(strtolower($email)).'?s=25" alt="'.$this->helper->e($alt).'" title="'.$this->helper->e($alt).'">'; + } + + return ''; + } +} |