Skip to content

Commit

Permalink
I18n support for Tart Columns, Tart Filters
Browse files Browse the repository at this point in the history
  • Loading branch information
Yasen Yanev committed Feb 26, 2014
1 parent f55de78 commit 03c0fac
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 81 deletions.
149 changes: 76 additions & 73 deletions classes/Kohana/Jam/Form/Tart/General.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php defined('SYSPATH') OR die('No direct script access.');

/**
* All the widgets needed for the admin.
* All the widgets needed for the admin.
* If you feel the need for extra widgets, you can extend this class in your module
*
* @package Jam tart
Expand Down Expand Up @@ -31,7 +31,7 @@ public static function list_vocabulary_choices($vocabulary, $visible = NULL)
$choices = $choices->visible($visible);
}
$choices_list = array();
foreach ($choices as $choice)
foreach ($choices as $choice)
{
$choices_list[$choice->id] = $choice->parent ? ($choice->parent->name(). ' / '.$choice->name()) : $choice->name();
}
Expand All @@ -42,10 +42,10 @@ public static function list_vocabulary_choices($vocabulary, $visible = NULL)
* Generate a row. Extends the default generator
* @param string $type the name of the widget
* @param string $name the name of the field
* @param array $options array can include 'label', 'help' and 'clear' all other options are passed to the widget
* @param array $attributes array
* @param array $options array can include 'label', 'help' and 'clear' all other options are passed to the widget
* @param array $attributes array
* @param string $template override the template
* @return string
* @return string
*/
public function row($type, $name, array $options = array(), array $attributes = array(), $template = NULL)
{
Expand All @@ -71,7 +71,7 @@ public function row($type, $name, array $options = array(), array $attributes =
}
$h->add(call_user_func(array($self, $type), $name, $options, $attributes));
}));

$h->add($errors);
if (isset($options['help']))
{
Expand All @@ -92,12 +92,12 @@ public function row($type, $name, array $options = array(), array $attributes =

/**
* The same as row, but places the input and label inline
* @param string $type
* @param string $name
* @param array $options
* @param array $attributes
* @param string $template
* @return string
* @param string $type
* @param string $name
* @param array $options
* @param array $attributes
* @param string $template
* @return string
*/
public function row_inline($type, $name, array $options = array(), array $attributes = array(), $template = NULL)
{
Expand Down Expand Up @@ -127,8 +127,8 @@ public function row_inline($type, $name, array $options = array(), array $attrib

/**
* Return the html for the errors for a given field
* @param string $name
* @return string
* @param string $name
* @return string
*/
public function errors($name)
{
Expand All @@ -138,14 +138,14 @@ public function errors($name)

/**
* Get a chozen select for vocabulary.
*
*
* Options
* - vocabulary: string|array, required
*
* @param string $name
* @param array $options
* @param array $attributes
* @return string
*
* @param string $name
* @param array $options
* @param array $attributes
* @return string
*/
public function taxonomy($name, array $options = array(), array $attributes = array())
{
Expand All @@ -155,10 +155,10 @@ public function taxonomy($name, array $options = array(), array $attributes = ar
if (is_array($options['vocabulary']))
{
$choices = array();
foreach ($options['vocabulary'] as $vocabulary)
foreach ($options['vocabulary'] as $vocabulary)
{
$choices[$vocabulary] = Jam_Form_Tart_General::list_vocabulary_choices($vocabulary, Arr::get($options, 'visible', TRUE));
}
}
}
else
{
Expand All @@ -174,15 +174,15 @@ public function taxonomy($name, array $options = array(), array $attributes = ar

/**
* A widget to upload an image
*
*
* Options:
* - remove: boolean, add a "remove" button, defaults true
* - thumbnail: string, the name of the thumbnail to display, if not set uses the full image
*
* @param string $name
* @param array $options
* @param array $attributes
* @return string
*
* @param string $name
* @param array $options
* @param array $attributes
* @return string
*/
public function upload($name, array $options = array(), array $attributes = array())
{
Expand Down Expand Up @@ -216,19 +216,19 @@ public function upload($name, array $options = array(), array $attributes = arra

if ($remove)
{
$h('a', array('href' => '#', 'class' => 'btn fileupload-exists', 'data-dismiss' => 'fileupload'), 'Remove');
$h('a', array('href' => '#', 'class' => 'btn fileupload-exists', 'data-dismiss' => 'fileupload'), __('Remove'));
}
});
})->render();
}

/**
* A widget used to input an array of string values, usually stored on a serialized field
*
* @param string $name
* @param array $options
* @param array $attributes
* @return string
*
* @param string $name
* @param array $options
* @param array $attributes
* @return string
*/
public function input_array($name, array $options = array(), array $attributes = array())
{
Expand All @@ -237,7 +237,7 @@ public function input_array($name, array $options = array(), array $attributes =
return Tart::html($this, function($h) use ($name, $attributes) {
$h('div', array('id' => $attributes['id'], 'data-index' => $attributes['name'].'[{{index}}]'), function($h, $self) use ($name, $attributes) {
$h('input', array('type' => 'hidden', 'name' => $attributes['name'].'[]', 'value' => ''));
foreach ( (array) $self->object()->$name as $index => $value)
foreach ( (array) $self->object()->$name as $index => $value)
{
$h('div.multiform', function($h, $self) use ($attributes, $index, $value) {
$h('input', array('type' => 'text', 'name' => $attributes['name']."[{$index}]", 'value' => $value, 'class' => Arr::get($attributes, 'class')));
Expand All @@ -252,18 +252,18 @@ public function input_array($name, array $options = array(), array $attributes =
});
});
}

/**
* A widget to enter a string value for a field, using typeahead.
* A widget to enter a string value for a field, using typeahead.
* If its a model, uses :name_key for that model to dispaly the value
*
* Options:
* - model: string, defaults to the foreign_model of the associaton, can be comma separated
* - source: string, the url used to retrieve the typeahead data. Defaults to the builtin typeahead action
* @param string $name
* @param array $options
* @param array $attributes
* @return string
* @param string $name
* @param array $options
* @param array $attributes
* @return string
*/
public function typeahead($name, array $options = array(), array $attributes = array())
{
Expand All @@ -280,23 +280,23 @@ public function typeahead($name, array $options = array(), array $attributes = a
}

/**
* A widget to enter a belnogsto / hasone like association.
* For displaying an exisiting item, you can use a template or a url.
* A widget to enter a belnogsto / hasone like association.
* For displaying an exisiting item, you can use a template or a url.
* If you use a template, it gets 'model', 'name', 'item' and 'id' as variables inside of it.
* If you use a 'url' option - it will use its response instead of a template, passing 'model', 'id' and 'name' as query parameter fillers. E.g. /admin/images/build?model={{model}}&id={{id}}
*
*
* Options:
* - model: string, defaults to the foreign_model of the association, can be comma separated
* - template: string, the path for the view that is used to render an existing item
* - url: string, a url to render an existing item
* - container: the html id of the container tag
* - source: string, the url used to retrieve the typeahead data. Defaults to the builtin typeahead action
* - placeholder: the placeholder for the typeahead search input
*
* @param string $name
* @param array $options
* @param array $attributes
* @return string
*
* @param string $name
* @param array $options
* @param array $attributes
* @return string
*/
public function remoteselect($name, array $options = array(), array $attributes = array())
{
Expand Down Expand Up @@ -325,9 +325,12 @@ public function remoteselect($name, array $options = array(), array $attributes

$h('input', array('name' => $attributes['name'], 'type' => 'hidden', 'value' => ''));

$label = Arr::get($options, 'label', __(Inflector::humanize($name)));
$default_placeholder = __('Search for :label', array(':label' => strtolower($label)));

$h('input', array(
'type' => 'text',
'placeholder' => Arr::get($options, 'placeholder', 'Search for '.Arr::get($options, 'label', strtolower(Inflector::humanize($name)))),
'placeholder' => Arr::get($options, 'placeholder', $default_placeholder),
'class' => $current ? 'fade hide' : 'fade in',
'data-provide' => 'remoteselect',
'data-source' => $options['source'],
Expand All @@ -343,7 +346,7 @@ public function remoteselect($name, array $options = array(), array $attributes
}
});

})->render();
})->render();
}

/**
Expand All @@ -363,11 +366,11 @@ public function remoteselect($name, array $options = array(), array $attributes
* - source: string, the url used to retrieve the typeahead data. Defaults to the builtin typeahead action
* - placeholder: the placeholder for the typeahead search input
* - sortable: boolean, set to TRUE to enable sortable javascript plugin
*
* @param string $name
* @param array $options
* @param array $attributes
* @return string
*
* @param string $name
* @param array $options
* @param array $attributes
* @return string
*/
public function multiselect($name, array $options = array(), array $attributes = array())
{
Expand Down Expand Up @@ -397,49 +400,49 @@ public function multiselect($name, array $options = array(), array $attributes =
$h('input', array('name' => $attributes['name'].'[]', 'type' => 'hidden', 'value' => ''));

$h('p', function($h) use ($name, $options, $attributes) {

$h('input', array(
'id' => $attributes['id'],
'type' => 'text',
'placeholder' => Arr::get($options, 'placeholder', 'Search for '.$options['label']),
'placeholder' => Arr::get($options, 'placeholder', __('Search for :label', array(':label' => $options['label']))),
'data-provide' => Arr::get($options, 'provide', 'remoteselect'),
'data-source' => $options['source'],
'data-container' => '#'.$options['container'],
'data-url' => $options['new'],
'data-templatestring' => $options['templatestring'],
'style' => $options['search'] ? '' : 'display:none'
));

if ($options['new_button'])
{
$h('button', array('type' => 'button', 'class' => 'btn', 'data-remoteselect-new' => $options['model']), $options['new_button']);
}

if ($options['list'])
{
$h('a', array('href' => $options['list'], 'class' => 'btn btn-link'), 'List '.$options['label']);
$h('a', array('href' => $options['list'], 'class' => 'btn btn-link'), __('List :item', array(':item' => $options['label'])));
}
});

$h('ul', array('class' => 'thumbnails', 'data-provide' => ($options['sortable'] ? 'sortable' : NULL), 'data-items' => ($options['sortable'] ? '> li.'.$options['sortable'] : NULL), 'data-tolerance' => 'pointer', 'data-placeholder' => 'sortable-placeholder thumbnail '.$options['sortable'], 'id' => $options['container']), function($h, $self) use ($name, $options, $attributes) {

if ($self->object()->$name AND count($self->object()->$name))
{
foreach ($self->object()->$name as $index => $item)
foreach ($self->object()->$name as $index => $item)
{
$h->add(View::factory($options['template'], array('name' => $attributes['name'].'[]', 'item' => $item, 'index' => $index, 'form' => $self->fields_for($name, $index))));
}
}
});
})->render();
})->render();
}

/**
* Radios select, bootstrap style
* @param string $name
* @param array $options
* @param array $attributes
* @return string
* @param string $name
* @param array $options
* @param array $attributes
* @return string
*/
public function radios($name, array $options = array(), array $attributes = array())
{
Expand All @@ -452,7 +455,7 @@ public function radios($name, array $options = array(), array $attributes = arra

if ($blank = Arr::get($options, 'include_blank'))
{
Arr::unshift($choices, '', ($blank === TRUE) ? " -- Select -- " : $blank);
Arr::unshift($choices, '', ($blank === TRUE) ? __(" -- Select -- ") : $blank);
}

$radios = array();
Expand All @@ -467,10 +470,10 @@ public function radios($name, array $options = array(), array $attributes = arra

/**
* An input to enter a url, when set display a link to that url alongside the input
* @param string $name
* @param array $options
* @param array $attributes
* @return string
* @param string $name
* @param array $options
* @param array $attributes
* @return string
*/
public function url($name, array $options = array(), array $attributes = array())
{
Expand All @@ -488,7 +491,7 @@ public function country($name, array $options = array(), array $attributes = arr
{
$options = Arr::merge(array(
'choices' => Jam::all('location')->where('type', '=', 'country')->order_by('name')->as_array('id', 'name'),
'include_blank' => 'Select Country',
'include_blank' => __('Select Country'),
), $options);

return $this->select($name, $options, $attributes, $template);
Expand Down
18 changes: 10 additions & 8 deletions classes/Kohana/Tart/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@
abstract class Kohana_Tart_Group extends Tart_Interface_Collection {

protected $_items = array();

public function items($key = NULL, $value = NULL)
{
if ($key === NULL)
return $this->_items;

if (is_array($key))
{
$this->_items = $key;
foreach ($this->_items as $key => $value)
foreach ($this->_items as $key => $value)
{
if ( ! ($value instanceof Tart_Group_Item))
throw new Kohana_Exception('Item :name must be instance of class Tart_Group_Item', array(':name' => $key));

$value->defaults($key, ucfirst(Inflector::humanize($key)), $this);
$label = __(ucfirst(Inflector::humanize($key)));
$value->defaults($key, $label, $this);
}
}
else
Expand All @@ -34,11 +35,12 @@ public function items($key = NULL, $value = NULL)

if ( ! ($value instanceof Tart_Group_Item))
throw new Kohana_Exception('Item :name must be instance of class Tart_Group_Item', array(':name' => $key));

$this->_items[$key] = $value->defaults($key, ucfirst(Inflector::humanize($key)), $this);

$label = __(ucfirst(Inflector::humanize($key)));
$this->_items[$key] = $value->defaults($key, $label, $this);
}

return $this;
}

}
Loading

0 comments on commit 03c0fac

Please sign in to comment.