Skip to content

Instantly share code, notes, and snippets.

@igorsgm
Last active November 24, 2016 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igorsgm/1c5bb2212d4849c1010ee26fdafda315 to your computer and use it in GitHub Desktop.
Save igorsgm/1c5bb2212d4849c1010ee26fdafda315 to your computer and use it in GitHub Desktop.
Filtros personalizados no componente
<field name="filter_evento" type="sql"
query="SELECT '' AS `id`, '- Selecionar Evento -' AS `val` UNION SELECT `id`, `nome` AS val FROM `#__eventos`"
key_field="id" value_field="val" class="inputbox" onchange="this.form.submit()" translate="true"/>
<?php
/* Primeiro de tudo: Verificar se no método construtor o campo `evento`, `a`.`evento` está no $config['filter_fields']. */
public function __construct($config = array())
{
if (empty($config['filter_fields'])) {
$config['filter_fields'] = array(
'evento',
'a.`evento`',
);
}
parent::__construct($config);
}
/* Verificar se tem o setState 'filter.evento' */
protected function populateState($ordering = null, $direction = null)
{
// Filtering evento
$this->setState('filter.evento',
$app->getUserStateFromRequest($this->context . '.filter.evento', 'filter_evento', '', 'string'));
}
protected function getListQuery()
{
/**
* No local que o método faz os selects e joins, adicionar ao final da query.
* Atenção: não precisa ter as foreign keys criadas, como nos outros prováveis filtros acima deste
*/
// Join over the 'eventos'
$query->select('`#__eventos`.`nome`');
$query->join('LEFT', '#__eventos ON #__eventos.`id` = a.`evento`');
/* Após os selects e joins terem sido criados, adicioanar os critérios WHERE do evento */
//Filtering evento
$filter_evento = $this->state->get("filter.evento");
if ($filter_evento) {
$query->where("a.`evento` = '" . $db->escape($filter_evento) . "'");
}
}
<?php
/* Adicionar no método do Joomla addToolbar, o filtro do Evento */
protected function addToolbar()
{
//Filter for the field evento;
jimport('joomla.form.form');
$options = array();
JForm::addFormPath(JPATH_COMPONENT . '/models/forms');
$form = JForm::getInstance('com_eventos_plus.inscricao', 'inscricao');
$field = $form->getField('evento');
$query = $form->getFieldAttribute('filter_evento', 'query');
$translate = $form->getFieldAttribute('filter_evento', 'translate');
$key = $form->getFieldAttribute('filter_evento', 'key_field');
$value = $form->getFieldAttribute('filter_evento', 'value_field');
// Get the database object.
$db = JFactory::getDbo();
// Set the query and get the result list.
$db->setQuery($query);
$items = $db->loadObjectlist();
// Build the field options.
if (!empty($items)) {
foreach ($items as $item) {
if ($translate == true) {
$options[] = JHtml::_('select.option', $item->$key, JText::_($item->$value));
} else {
$options[] = JHtml::_('select.option', $item->$key, $item->$value);
}
}
}
JHtmlSidebar::addFilter(
'$Evento',
'filter_evento',
JHtml::_('select.options', $options, "value", "text", $this->state->get('filter.evento')),
true
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment