Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
Merge pull request #78 from timble/feature/77-helper
Browse files Browse the repository at this point in the history
De-couple helpers from template
  • Loading branch information
johanjanssens committed May 4, 2016
2 parents f86b037 + 6a21c50 commit 5fec4cc
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 56 deletions.
2 changes: 1 addition & 1 deletion code/template/behavior/helperable.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ public function invokeHelper($identifier, $params = array())
$params = array_merge($this->getParameters()->toArray(), $params);
}

return $helper->$function($params, $this->getMixer());
return $helper->$function($params);
}
}
14 changes: 7 additions & 7 deletions code/template/helper/actionbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class TemplateHelperActionbar extends TemplateHelperAbstract
* @param array $config An optional array with configuration options
* @return string Html
*/
public function render($config = array(), TemplateInterface $template)
public function render($config = array())
{
$config = new ObjectConfigJson($config);
$config->append(array(
Expand All @@ -44,9 +44,9 @@ public function render($config = array(), TemplateInterface $template)
$name = $command->getName();

if(method_exists($this, $name)) {
$html .= $this->$name(ObjectConfig::unbox($command), $template);
$html .= $this->$name(ObjectConfig::unbox($command));
} else {
$html .= $this->command(ObjectConfig::unbox($command), $template);
$html .= $this->command(ObjectConfig::unbox($command));
}
}
$html .= '</div>';
Expand All @@ -62,7 +62,7 @@ public function render($config = array(), TemplateInterface $template)
* @param array $config An optional array with configuration options
* @return string Html
*/
public function command($config = array(), TemplateInterface $template)
public function command($config = array())
{
$config = new ObjectConfigJson($config);
$config->append(array(
Expand Down Expand Up @@ -100,7 +100,7 @@ public function command($config = array(), TemplateInterface $template)

//Create the href
if(!empty($config->href)) {
$config->attribs['href'] = $template->route($config->href);
$config->attribs['href'] = $config->href;
}

$html = '<a '.$this->buildAttributes($config->attribs).'>';
Expand Down Expand Up @@ -134,10 +134,10 @@ public function separator($config = array())
* @param array $config An optional array with configuration options
* @return string Html
*/
public function dialog($config = array(), TemplateInterface $template)
public function dialog($config = array())
{
$html = $this->createHelper('behavior')->modal();
$html .= $this->command($config, $template);
$html .= $this->command($config);

return $html;
}
Expand Down
44 changes: 28 additions & 16 deletions code/template/helper/behavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,46 +274,59 @@ public function validator($config = array())
/**
* Keep session alive
*
* This will send an ascynchronous request to the server via AJAX on an interval in miliseconds
* This will send an ascynchronous request to the server via AJAX on an interval in secs
*
* @param array $config An optional array with configuration options
* @return string The html output
*/
public function keepalive($config = array(), TemplateInterface $template)
public function keepalive($config = array())
{
$config = new ObjectConfigJson($config);
$config->append(array(
'refresh' => 15 * 60000, //default refresh is 15min
'url' => $template->route('', false, false),
'refresh' => 15 * 60, //default refresh is 15min
'url' => '', //default to window.location.url
));

$html = '';

// Only load once
if (!isset(self::$_loaded['keepalive']))
{
$session = $this->getObject('user')->getSession();
if($session->isActive())
{
//Get the config session lifetime
$lifetime = $session->getLifetime() * 1000;
$lifetime = $session->getLifetime();

//Refresh time is 1 minute less than the lifetime
$refresh = ($lifetime <= 60000) ? 30000 : $lifetime - 60000;
$refresh = ($lifetime <= 60) ? 30 : $lifetime - 60;
}
else $refresh = (int) $config->refresh;

// Longest refresh period is one hour to prevent integer overflow.
if ($refresh > 3600000 || $refresh <= 0) {
$refresh = 3600000;
if ($refresh > 3600 || $refresh <= 0) {
$refresh = 3600;
}

if(empty($config->url)) {
$url = 'window.location.url';
} else {
$url = "'.$config->url.'";
}

// Build the keep alive script.
$html =
"<script>
Kodekit.keepalive = function() {
var request = new Request({method: 'get', url: '" . $config->url . "'}).send();
}
window.addEvent('domready', function() { Kodekit.keepalive.periodical('" . $refresh . "'); });
</script>";
(function($){
var refresh = '" . $refresh . "';
setInterval(function() {
$.ajax({
url: $url,
method: 'HEAD',
cache: false
})
}, refresh * 1000);
})(kQuery);</script>";

self::$_loaded['keepalive'] = true;
}
Expand Down Expand Up @@ -383,7 +396,7 @@ public function autocomplete($config = array())
'validate' => false, //Toggle if the forms validation helper is loaded
'queryVarName' => 'search',
'width' => 'resolve',
'model' => $config->model,
'model' => $config->model,
'placeholder' => $config->prompt,
'allowClear' => $config->deselect,
'value' => $config->value,
Expand All @@ -405,8 +418,7 @@ public function autocomplete($config = array())
}

$config->options->url->setQuery(array('fields' => $config->value.','.$config->text), true);

$config->options->url = (string)$config->options->url;
$config->options->url = (string) $config->options->url;

$options = $config->options;
$signature = md5('autocomplete-'.$config->element.$options);
Expand Down
22 changes: 12 additions & 10 deletions code/template/helper/grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,15 @@ public function checkall($config = array())
* @param array $config An optional array with configuration options
* @return string Html
*/
public function sort($config = array(), TemplateInterface $template)
public function sort($config = array())
{
$config = new ObjectConfigJson($config);
$config->append(array(
'title' => '',
'column' => '',
'direction' => 'asc',
'sort' => ''
'sort' => '',
'url' => null
));

$translator = $this->getObject('translator');
Expand All @@ -191,8 +192,8 @@ public function sort($config = array(), TemplateInterface $template)
}

//Set the direction
$direction = strtolower($config->direction);
$direction = in_array($direction, array('asc', 'desc')) ? $direction : 'asc';
$direction = strtolower($config->direction);
$direction = in_array($direction, array('asc', 'desc')) ? $direction : 'asc';

//Set the class
$class = '';
Expand All @@ -202,14 +203,15 @@ public function sort($config = array(), TemplateInterface $template)
$class = 'class="-koowa-'.$direction.'"';
}

$url = $template->route();
//Set the query in the route
if(!$config->url instanceof HttpUrlInterface) {
$config->url = HttpUrl::fromString($config->url);
}

$query = $url->getQuery(1);
$query['sort'] = $config->column;
$query['direction'] = $direction;
$url->setQuery($query);
$config->url->query['sort'] = $config->column;
$config->url->query['direction'] = $direction;

$html = '<a href="'.$url.'" title="'.$translator->translate('Click to sort by this column').'" '.$class.'>';
$html = '<a href="'.$config->url.'" title="'.$translator->translate('Click to sort by this column').'" '.$class.'>';
$html .= $translator->translate($config->title);

// Mark the current column
Expand Down
13 changes: 6 additions & 7 deletions code/template/helper/listbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class TemplateHelperListbox extends TemplateHelperSelect
* @return string Html
* @see __call()
*/
public function render($config = array(), TemplateInterface $template)
public function render($config = array())
{
$config = new ObjectConfig($config);
$config->append(array(
Expand Down Expand Up @@ -62,9 +62,9 @@ public function render($config = array(), TemplateInterface $template)
}

if($config->autocomplete) {
$result = $this->__autocomplete($config, $template);
$result = $this->__autocomplete($config);
} else {
$result = $this->__listbox($config, $template);
$result = $this->__listbox($config);
}

return $result;
Expand Down Expand Up @@ -251,7 +251,7 @@ public function timezones($config = array())
* @return string Html
* @see __call()
*/
private function __listbox($config = array(), TemplateInterface $template)
private function __listbox($config = array())
{
$config = new ObjectConfigJson($config);
$config->append(array(
Expand Down Expand Up @@ -316,7 +316,7 @@ private function __listbox($config = array(), TemplateInterface $template)
* @param array|ObjectConfig $config
* @return string The html output
*/
private function __autocomplete($config = array(), TemplateInterface $template)
private function __autocomplete($config = array())
{
$config = new ObjectConfigJson($config);
$config->append(array(
Expand Down Expand Up @@ -352,11 +352,10 @@ private function __autocomplete($config = array(), TemplateInterface $template)
$parts = array_merge($parts, ObjectConfig::unbox($config->filter));
}

$config->url = $template->route($parts, false, false);
$config->url = $this->getObject('lib:dispatcher.router.route')->setQuery($parts);
}

$html = '';

$html .= $this->createHelper('behavior')->autocomplete($config);

$config->attribs->name = $config->name;
Expand Down
35 changes: 20 additions & 15 deletions code/template/helper/paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ class TemplateHelperPaginator extends TemplateHelperSelect
* @param array $config An optional array with configuration options
* @return string Html
*/
public function pagination($config = array(), TemplateInterface $template)
public function pagination($config = array())
{
$config = new ModelPaginator($config);
$config->append(array(
'url' => null,
'total' => 0,
'display' => 4,
'offset' => 0,
Expand All @@ -52,7 +53,7 @@ public function pagination($config = array(), TemplateInterface $template)
}

if($config->show_pages) {
$html .= $this->pages($config, $template);
$html .= $this->pages($config);
}

if($config->show_count) {
Expand All @@ -73,7 +74,7 @@ public function limit($config = array())
{
$config = new ObjectConfigJson($config);
$config->append(array(
'limit' => 0,
'limit' => 0,
'attribs' => array(),
'page_rows' => array(10, 20, 50, 100)
));
Expand Down Expand Up @@ -112,10 +113,11 @@ public function limit($config = array())
* @param array $config An optional array with configuration options
* @return string Html
*/
public function pages($config = array(), TemplateInterface $template)
public function pages($config = array())
{
$config = new ModelPaginator($config);
$config->append(array(
'url' => null,
'total' => 0,
'display' => 4,
'offset' => 0,
Expand All @@ -125,15 +127,15 @@ public function pages($config = array(), TemplateInterface $template)

$html = '<ul class="pagination">';
if($config->offset) {
$html .= $this->link($config->pages->prev, $template);
$html .= $this->page($config->pages->prev, $config->url);
}

foreach($config->pages->offsets as $offset) {
$html .= $this->link($offset, $template);
$html .= $this->page($offset, $config->url);
}

if($config->total > ($config->offset + $config->limit)) {
$html .= $this->link($config->pages->next, $template);
$html .= $this->page($config->pages->next, $config->url);
}
$html .= '</ul>';

Expand All @@ -143,13 +145,13 @@ public function pages($config = array(), TemplateInterface $template)
/**
* Render a page link
*
* @param array $config An optional array with configuration options
* @param ObjectConfigInterface $page The page data
* @param HttpUrlInterface $url The base url to create the link
* @return string Html
*/
public function link($config, TemplateInterface $template)
public function page(ObjectConfigInterface $page, HttpUrlInterface $url)
{
$config = new ObjectConfig($config);
$config->append(array(
$page->append(array(
'title' => '',
'current' => false,
'active' => false,
Expand All @@ -159,11 +161,14 @@ public function link($config, TemplateInterface $template)
'attribs' => array(),
));

$route = $template->route('limit='.$config->limit.'&offset='.$config->offset);
$rel = !empty($config->rel) ? 'rel="'.$config->rel.'"' : '';
//Set the offset and limit
$url->query['limit'] = $page->limit;
$url->query['offset'] = $page->offset;

$rel = !empty($page->rel) ? 'rel="'.$page->rel.'"' : '';

$html = '<li '.$this->buildAttributes($config->attribs).'>';
$html .= '<a href="'.$route.'" '.$rel.'>'.$this->getObject('translator')->translate($config->title).'</a>';
$html = '<li '.$this->buildAttributes($page->attribs).'>';
$html .= '<a href="'.$url.'" '.$rel.'>'.$this->getObject('translator')->translate($page->title).'</a>';
$html .= '</li>';

return $html;
Expand Down

0 comments on commit 5fec4cc

Please sign in to comment.