public function test_list_id() { $this->assertEquals(1, Jam_Form::list_id($this->post)); $this->assertEquals(1, Jam_Form::list_id(1)); $this->assertEquals(array(1, 2), Jam_Form::list_id($this->post->test_tags)); $this->assertEquals(1, Jam_Form::list_id($this->post->test_tags, TRUE)); $this->assertEquals(1, Jam_Form::list_id(array(1, 5), TRUE)); }
public function render() { $html = Tart::html($this, function ($h, $self) { $h('table', $self->attributes(), function ($h, $self) { $h('thead', function ($h, $self) { if ($self->selected() !== NULL and $self->selected() !== FALSE) { $h('th', array('width' => 10), function ($h, $self) { $h('input', array('type' => 'checkbox', 'name' => 'all', 'value' => '1', 'checked' => array_diff($self->collection()->ids(), $self->selected()) ? NULL : 'checked')); }); } foreach ($self->columns() as $column) { $attributes = array('width' => $column->width()); if ($column->sort()) { $attributes['nowrap'] = 'nowrap'; } $h('th', $attributes, $column->sort() ? $column->sort_anchor() : $column->label()); } }); $h('tbody', function ($h, $self) { foreach ($self->collection() as $item) { $h('tr', array('class' => Tart_Table::item_class_name($item)), function ($h, $self) use($item) { if ($self->selected() !== NULL and $self->selected() !== FALSE) { $h('td', function ($h, $self) use($item) { $h('input', array('type' => 'checkbox', 'name' => 'id[]', 'value' => Jam_Form::list_id($item), 'checked' => in_array(Jam_Form::list_id($item), $self->selected()) ? TRUE : NULL)); }); } foreach ($self->columns() as $column) { $h('td', $column->item($item)->render()); } }); } }); if ($self->footer()) { $h('tfoot', $self->footer()); } }); }); return $html->render(); }
public function render() { $html = Tart::html($this, function ($h, $self) { $h('div.thumbnails', function ($h, $self) { foreach ($self->collection() as $item) { $h('div.media', function ($h, $self) use($item) { if ($self->selected() !== NULL) { $h('td', function ($h, $self) use($item) { $h('input', array('type' => 'checkbox', 'name' => 'id[]', 'value' => Jam_Form::list_id($item), 'checked' => in_array(Jam_Form::list_id($item), $self->selected()) ? TRUE : NULL)); }); } foreach ($self->columns() as $column) { $h('td', $column->render($item, $self)); } }); } if ($self->footer()) { $h('tfoot', $self->footer()); } }); }); return $html->render(); }
/** * 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 */ public function remoteselect($name, array $options = array(), array $attributes = array()) { $attributes = $this->default_attributes($name, $attributes); return Tart::html($this, function ($h, $self) use($name, $options, $attributes) { $current = $self->object()->{$name}; $disabled = Arr::get($attributes, 'disabled', null); $model = isset($options['model']) ? $options['model'] : $self->object()->meta()->association($name)->foreign_model; $template = Arr::get($options, 'template', 'tart/typeahead/remoteselect'); if (!is_object($current) and $current) { $current = Jam::find($model, $current); } $options = Arr::merge(array('model' => $model, 'container' => $attributes['id'] . '_container', 'source' => Tart::uri('typeahead') . '?model=' . $model, 'url' => Tart::uri('typeahead', 'remoteselect_template') . '?model={{model}}&name={{name}}&id={{id}}&template=' . $template, 'template' => $template), $options); $options['url'] = strtr($options['url'], array('{{name}}' => $attributes['name'])); $h('input', array('name' => $attributes['name'], 'type' => 'hidden', 'value' => '', 'disabled' => $disabled)); $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', $default_placeholder), 'class' => $current ? 'fade hide' : 'fade in', 'data-provide' => 'remoteselect', 'data-source' => $options['source'], 'data-container' => '#' . $options['container'], 'data-overwrite' => 1, 'data-url' => $options['url'], 'disabled' => $disabled)); $h('span', array('id' => $options['container'], 'class' => 'remoteselect-container'), function ($h, $self) use($current, $model, $options) { if ($current) { $url = strtr($options['url'], array('{{model}}' => $model, '{{id}}' => Jam_Form::list_id($current))); $split_uri = explode('?', $url); $uri = array_shift($split_uri); $query = array(); if ($split_uri) { parse_str($split_uri[0], $query); } $h->add(Request::factory($uri)->query($query)->execute()); } }); })->render(); }
public static function common_params($collection, array $params = array()) { $collection = ($collection instanceof Jam_Query_Builder_Collection or $collection instanceof Jam_Array_Model) ? $collection->as_array() : $collection; $common = array(); foreach ($params as $name => $param) { $attribute_name = is_numeric($name) ? $param : $name; $param_collection = array_map(function ($item) use($attribute_name) { return $item->{$attribute_name}; }, $collection); if (is_numeric($name)) { $common[$param] = array_reduce($param_collection, function ($result, $item) { return Jam_Form::list_id($result) !== Jam_Form::list_id($item) ? NULL : $item; }, reset($param_collection)); } else { $common[$name] = Jam_Form::common_params($param_collection, $param); } } return $common; }
public function render() { if (!$this->item()) { throw new Kohana_Exception('You must assign an item before you render'); } if ($callback = $this->callback()) { $content = call_user_func($callback, $this->item(), $this->name(), $this); } elseif ($field = $this->item()->meta()->field($this->name())) { $content = Tart_Column::render_field($this->item(), $field); } elseif ($association = $this->item()->meta()->association($this->name())) { $content = Tart_Column::render_association($this->item(), $association); } else { $content = (string) $this->item()->{$this->name()}; } if ($content) { if ($this->_filter and $this->_filter_name and $this->_is_link) { $content = $this->_filter->anchor($this->_filter_name, Jam_Form::list_id($this->item()->{$this->name()}), $content, array('href' => Tart::uri($this->item()->{$this->name()}))); } if ($this->_is_link) { $content = HTML::anchor(Tart::uri($this->item()->{$this->name()}), $content); } if ($this->_filter and $this->_filter_name) { $content = $this->_filter->anchor($this->_filter_name, Jam_Form::list_id($this->item()->{$this->name()}), $content); } } return $content; }
/** * HTML input select field * available options * - choices - this can be Jam_Query_Builder_Collection or a simple array * - include_blank - bool|string - include an empty option * * @param string $name the name of the Jam_Model attribute * @param array $options set the options of the select with 'choices' and 'include_blank' * @param array $attributes HTML attributes for the field * @return string */ public function select($name, array $options = array(), array $attributes = array()) { $attributes = $this->default_attributes($name, $attributes); if (!isset($options['choices'])) { throw new Kohana_Exception("Select tag widget requires a 'choices' option"); } $choices = Jam_Form::list_choices($options['choices']); if ($blank = Arr::get($options, 'include_blank')) { Arr::unshift($choices, '', $blank === TRUE ? " -- Select -- " : $blank); } $selected = Jam_Form::list_id($this->object()->{$name}); return Form::select($attributes['name'], $choices, $selected, $attributes); }