Skip to content

Commit cb1b242

Browse files
committed
Add our own form fields
1 parent 900d228 commit cb1b242

File tree

8 files changed

+310
-87
lines changed

8 files changed

+310
-87
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Patch testing component for the Joomla! CMS
4+
*
5+
* @copyright Copyright (C) 2011 - 2012 Ian MacLennan, Copyright (C) 2013 - 2018 Open Source Matters, Inc. All rights reserved.
6+
* @license GNU General Public License version 2 or later
7+
*/
8+
9+
namespace PatchTester\Field;
10+
11+
use Joomla\CMS\Factory;
12+
use Joomla\CMS\Form\Field\ListField;
13+
14+
defined('_JEXEC') or die;
15+
16+
/**
17+
* List of available branches.
18+
*
19+
* @package PatchTester
20+
* @since 4.1.0
21+
*/
22+
class BranchField extends ListField
23+
{
24+
/**
25+
* Type of field
26+
*
27+
* @var string
28+
* @since 4.1.0
29+
*/
30+
protected $type = 'Branch';
31+
32+
/**
33+
* Build a list of available branches.
34+
*
35+
* @return array List of options
36+
*
37+
* @since 4.1.0
38+
*/
39+
public function getOptions(): array
40+
{
41+
$db = Factory::getContainer()->get('DatabaseDriver');
42+
$query = $db->getQuery(true);
43+
44+
$query->select('DISTINCT(' . $db->quoteName('branch') . ') AS ' . $db->quoteName('text'))
45+
->select($db->quoteName('branch', 'value'))
46+
->from('#__patchtester_pulls')
47+
->where($db->quoteName('branch') . ' != ' . $db->quote(''))
48+
->order($db->quoteName('branch') . ' ASC');
49+
50+
$options = $db->setQuery($query)->loadAssocList();
51+
52+
return array_merge(parent::getOptions(), $options);
53+
}
54+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Patch testing component for the Joomla! CMS
4+
*
5+
* @copyright Copyright (C) 2011 - 2012 Ian MacLennan, Copyright (C) 2013 - 2018 Open Source Matters, Inc. All rights reserved.
6+
* @license GNU General Public License version 2 or later
7+
*/
8+
9+
namespace PatchTester\Field;
10+
11+
use Joomla\CMS\Factory;
12+
use Joomla\CMS\Form\Field\ListField;
13+
14+
defined('_JEXEC') or die;
15+
16+
/**
17+
* List of available banks.
18+
*
19+
* @package PatchTester
20+
* @since 4.1.0
21+
*/
22+
class LabelField extends ListField
23+
{
24+
/**
25+
* Type of field
26+
*
27+
* @var string
28+
* @since 4.1.0
29+
*/
30+
protected $type = 'Label';
31+
32+
/**
33+
* Build a list of available fields.
34+
*
35+
* @return array List of options
36+
*
37+
* @since 4.1.0
38+
*/
39+
public function getOptions(): array
40+
{
41+
$db = Factory::getContainer()->get('DatabaseDriver');
42+
$query = $db->getQuery(true);
43+
44+
$query->select('DISTINCT(' . $db->quoteName('name') . ') AS ' . $db->quoteName('text'))
45+
->select($db->quoteName('name', 'value'))
46+
->from($db->quoteName('#__patchtester_pulls_labels'))
47+
->order($db->quoteName('name') . ' ASC');
48+
49+
$options = $db->setQuery($query)->loadAssocList();
50+
51+
return array_merge(parent::getOptions(), $options);
52+
}
53+
}

administrator/components/com_patchtester/PatchTester/Model/PullsModel.php

Lines changed: 73 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88

99
namespace PatchTester\Model;
1010

11+
use Joomla\CMS\Form\Form;
12+
use Joomla\CMS\Form\FormFactoryAwareTrait;
1113
use Joomla\CMS\HTML\HTMLHelper;
1214
use Joomla\CMS\Language\Text;
15+
use Joomla\CMS\MVC\Model\FormBehaviorTrait;
1316
use Joomla\CMS\Pagination\Pagination;
1417
use Joomla\Registry\Registry;
1518
use PatchTester\GitHub\Exception\UnexpectedResponse;
@@ -22,6 +25,9 @@
2225
*/
2326
class PullsModel extends AbstractModel
2427
{
28+
use FormBehaviorTrait;
29+
use FormFactoryAwareTrait;
30+
2531
/**
2632
* The object context
2733
*
@@ -55,51 +61,6 @@ public function __construct($context, Registry $state = null,
5561
$this->context = $context;
5662
}
5763

58-
/**
59-
* Method to get an array of branches.
60-
*
61-
* @return array
62-
*
63-
* @since 3.0.0
64-
*/
65-
public function getBranches(): array
66-
{
67-
$db = $this->getDb();
68-
$query = $db->getQuery(true);
69-
70-
// Select distinct branches excluding empty values
71-
$query->select('DISTINCT(branch) AS text')
72-
->from('#__patchtester_pulls')
73-
->where($db->quoteName('branch') . ' != ' . $db->quote(''))
74-
->order('branch ASC');
75-
76-
return $db->setQuery($query)->loadAssocList();
77-
}
78-
79-
/**
80-
* Method to get an array of labels.
81-
*
82-
* @return array The list of labels
83-
*
84-
* @since 4.0.0
85-
*/
86-
public function getLabels(): array
87-
{
88-
$db = $this->getDb();
89-
$query = $db->getQuery(true);
90-
91-
// Select distinct branches excluding empty values
92-
$query->select(
93-
'DISTINCT(' . $db->quoteName('name') . ') AS ' . $db->quoteName(
94-
'text'
95-
)
96-
)
97-
->from($db->quoteName('#__patchtester_pulls_labels'))
98-
->order($db->quoteName('name') . ' ASC');
99-
100-
return $db->setQuery($query)->loadAssocList();
101-
}
102-
10364
/**
10465
* Method to get an array of data items.
10566
*
@@ -703,4 +664,71 @@ public function truncateTable()
703664
{
704665
$this->getDb()->truncateTable('#__patchtester_pulls');
705666
}
667+
668+
/**
669+
* Get the filter form
670+
*
671+
* @param array $data data
672+
* @param boolean $loadData load current data
673+
*
674+
* @return Form|null The \JForm object or null if the form can't be found
675+
*
676+
* @since 3.2
677+
*/
678+
public function getFilterForm($data = array(), $loadData = true)
679+
{
680+
// Try to locate the filter form automatically. Example: ContentModelArticles => "filter_articles"
681+
if (empty($this->filterFormName))
682+
{
683+
$classNameParts = explode('Model', \get_called_class());
684+
685+
if (\count($classNameParts) >= 2)
686+
{
687+
$this->filterFormName = 'filter_' . str_replace('\\', '', strtolower($classNameParts[1]));
688+
}
689+
}
690+
691+
if (empty($this->filterFormName))
692+
{
693+
return null;
694+
}
695+
696+
try
697+
{
698+
// Get the form.
699+
return $this->loadForm($this->context . '.filter', $this->filterFormName, array('control' => '', 'load_data' => $loadData));
700+
}
701+
catch (\RuntimeException $e)
702+
{
703+
}
704+
705+
return null;
706+
}
707+
708+
/**
709+
* Function to get the active filters
710+
*
711+
* @return array Associative array in the format: array('filter_published' => 0)
712+
*
713+
* @since 3.2
714+
*/
715+
public function getActiveFilters()
716+
{
717+
$activeFilters = array();
718+
719+
if (!empty($this->filter_fields))
720+
{
721+
foreach ($this->filter_fields as $filter)
722+
{
723+
$filterName = 'filter.' . $filter;
724+
725+
if (property_exists($this->state, $filterName) && (!empty($this->state->{$filterName}) || is_numeric($this->state->{$filterName})))
726+
{
727+
$activeFilters[$filter] = $this->state->get($filterName);
728+
}
729+
}
730+
}
731+
732+
return $activeFilters;
733+
}
706734
}

administrator/components/com_patchtester/PatchTester/View/Pulls/PullsHtmlView.php

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace PatchTester\View\Pulls;
1010

1111
use Joomla\CMS\Factory;
12+
use Joomla\CMS\Form\Form;
1213
use Joomla\CMS\Language\Text;
1314
use Joomla\CMS\Pagination\Pagination;
1415
use Joomla\CMS\Toolbar\Toolbar;
@@ -22,26 +23,10 @@
2223
*
2324
* @since 2.0
2425
*
25-
* @property-read \PatchTester\Model\PullsModel $model The model object.
26+
* @property-read \PatchTester\Model\PullsModel $model The model object.
2627
*/
2728
class PullsHtmlView extends DefaultHtmlView
2829
{
29-
/**
30-
* Array containing the list of branches
31-
*
32-
* @var array
33-
* @since 3.0.0
34-
*/
35-
protected $branches = [];
36-
37-
/**
38-
* Array containing the list of labels
39-
*
40-
* @var array
41-
* @since 4.0.0
42-
*/
43-
protected $labels = [];
44-
4530
/**
4631
* Array containing environment errors
4732
*
@@ -66,6 +51,20 @@ class PullsHtmlView extends DefaultHtmlView
6651
*/
6752
protected $pagination;
6853

54+
/**
55+
* Form object for search filters
56+
*
57+
* @var Form
58+
*/
59+
public $filterForm;
60+
61+
/**
62+
* The active search filters
63+
*
64+
* @var array
65+
*/
66+
public $activeFilters;
67+
6968
/**
7069
* The model state
7170
*
@@ -104,12 +103,15 @@ public function render()
104103
// Only process the data if there are no environment errors
105104
if (!count($this->envErrors))
106105
{
107-
$this->state = $this->model->getState();
108-
$this->items = $this->model->getItems();
109-
$this->pagination = $this->model->getPagination();
110-
$this->trackerAlias = TrackerHelper::getTrackerAlias($this->state->get('github_user'), $this->state->get('github_repo'));
111-
$this->branches = $this->model->getBranches();
112-
$this->labels = $this->model->getLabels();
106+
$this->state = $this->model->getState();
107+
$this->items = $this->model->getItems();
108+
$this->pagination = $this->model->getPagination();
109+
$this->filterForm = $this->model->getFilterForm();
110+
$this->activeFilters = $this->model->getActiveFilters();
111+
$this->trackerAlias = TrackerHelper::getTrackerAlias(
112+
$this->state->get('github_user'),
113+
$this->state->get('github_repo')
114+
);
113115
}
114116

115117
// Change the layout if there are environment errors

administrator/components/com_patchtester/PatchTester/View/Pulls/tmpl/default.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Joomla\CMS\Factory;
1010
use Joomla\CMS\HTML\HTMLHelper;
1111
use Joomla\CMS\Language\Text;
12+
use Joomla\CMS\Layout\LayoutHelper;
1213
use Joomla\CMS\Router\Route;
1314

1415
/** @var \PatchTester\View\Pulls\PullsHtmlView $this */
@@ -28,6 +29,7 @@
2829

2930
HTMLHelper::_('behavior.core');
3031
HTMLHelper::_('searchtools.form', '#adminForm', $searchToolsOptions);
32+
HTMLHelper::_('behavior.multiselect');
3133
HTMLHelper::_('stylesheet', 'com_patchtester/octicons.css', ['version' => '3.5.0', 'relative' => true]);
3234
HTMLHelper::_('script', 'com_patchtester/patchtester.js', ['version' => 'auto', 'relative' => true]);
3335

@@ -49,6 +51,7 @@
4951
<div class="row">
5052
<div class="col-md-12">
5153
<div id="j-main-container" class="j-main-container">
54+
<?php echo LayoutHelper::render('joomla.searchtools.default', ['view' => $this]); ?>
5255
<div class="js-stools" role="search">
5356
<div class="js-stools-container-bar">
5457
<div class="btn-toolbar">

0 commit comments

Comments
 (0)