-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
ContentmapField.php
136 lines (115 loc) · 3.03 KB
/
ContentmapField.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
* @package Joomla.Administrator
* @subpackage com_finder
*
* @copyright (C) 2016 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Finder\Administrator\Field;
\defined('JPATH_PLATFORM') or die;
use Joomla\CMS\Factory;
use Joomla\CMS\Form\Field\GroupedlistField;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\Component\Finder\Administrator\Helper\LanguageHelper;
/**
* Supports a select grouped list of finder content map.
*
* @since 3.6.0
*/
class ContentmapField extends GroupedlistField
{
/**
* The form field type.
*
* @var string
* @since 3.6.0
*/
public $type = 'ContentMap';
/**
* Method to get the list of content map options grouped by first level.
*
* @return array The field option objects as a nested array in groups.
*
* @since 3.6.0
*/
protected function getGroups()
{
$groups = array();
// Get the database object and a new query object.
$db = $this->getDatabase();
// Main query.
$query = $db->getQuery(true)
->select($db->quoteName('a.title', 'text'))
->select($db->quoteName('a.id', 'value'))
->select($db->quoteName('a.parent_id'))
->select($db->quoteName('a.level'))
->from($db->quoteName('#__finder_taxonomy', 'a'))
->where($db->quoteName('a.parent_id') . ' <> 0')
->order('a.title ASC');
$db->setQuery($query);
try
{
$contentMap = $db->loadObjectList();
}
catch (\RuntimeException $e)
{
return [];
}
// Build the grouped list array.
if ($contentMap)
{
$parents = [];
foreach ($contentMap as $item)
{
if (!isset($parents[$item->parent_id]))
{
$parents[$item->parent_id] = [];
}
$parents[$item->parent_id][] = $item;
}
foreach ($parents[1] as $branch)
{
$groups[$branch->text] = $this->prepareLevel($branch->value, $parents);
}
}
// Merge any additional groups in the XML definition.
$groups = array_merge(parent::getGroups(), $groups);
return $groups;
}
/**
* Indenting and translating options for the list
*
* @param int $parent Parent ID to process
* @param array $parents Array of arrays of items with parent IDs as keys
*
* @return array The indented list of entries for this branch
*
* @since __DEPLOY_VERSION__
*/
private function prepareLevel($parent, $parents)
{
$lang = Factory::getLanguage();
$entries = [];
foreach ($parents[$parent] as $item)
{
$levelPrefix = str_repeat('- ', $item->level - 1);
if (trim($item->text, '*') === 'Language')
{
$text = LanguageHelper::branchLanguageTitle($item->text);
}
else
{
$key = LanguageHelper::branchSingular($item->text);
$text = $lang->hasKey($key) ? Text::_($key) : $item->text;
}
$entries[] = HTMLHelper::_('select.option', $item->value, $levelPrefix . $text);
if (isset($parents[$item->value]))
{
$entries = array_merge($entries, $this->prepareLevel($item->value, $parents));
}
}
return $entries;
}
}