contentmap.php
3.12 KB
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
<?php
/**
* @package Joomla.Administrator
* @subpackage com_finder
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('JPATH_PLATFORM') or die;
JFormHelper::loadFieldClass('groupedlist');
JLoader::register('FinderHelperLanguage', JPATH_ADMINISTRATOR . '/components/com_finder/helpers/language.php');
/**
* Supports a select grouped list of finder content map.
*
* @since 3.6.0
*/
class JFormFieldContentMap extends JFormFieldGroupedList
{
/**
* 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 = JFactory::getDbo();
// Levels subquery.
$levelQuery = $db->getQuery(true);
$levelQuery->select('title AS branch_title, 1 as level')
->select($db->quoteName('id'))
->from($db->quoteName('#__finder_taxonomy'))
->where($db->quoteName('parent_id') . ' = 1');
$levelQuery2 = $db->getQuery(true);
$levelQuery2->select('b.title AS branch_title, 2 as level')
->select($db->quoteName('a.id'))
->from($db->quoteName('#__finder_taxonomy', 'a'))
->join('LEFT', $db->quoteName('#__finder_taxonomy', 'b') . ' ON ' . $db->qn('a.parent_id') . ' = ' . $db->qn('b.id'))
->where($db->quoteName('a.parent_id') . ' NOT IN (0, 1)');
$levelQuery->union($levelQuery2);
// Main query.
$query = $db->getQuery(true)
->select($db->quoteName('a.title', 'text'))
->select($db->quoteName('a.id', 'value'))
->select($db->quoteName('d.level'))
->from($db->quoteName('#__finder_taxonomy', 'a'))
->join('LEFT', '(' . $levelQuery . ') AS d ON ' . $db->qn('d.id') . ' = ' . $db->qn('a.id'))
->where($db->quoteName('a.parent_id') . ' <> 0')
->order('d.branch_title ASC, d.level ASC, a.title ASC');
$db->setQuery($query);
try
{
$contentMap = $db->loadObjectList();
}
catch (RuntimeException $e)
{
return;
}
// Build the grouped list array.
if ($contentMap)
{
$lang = JFactory::getLanguage();
foreach ($contentMap as $branch)
{
if ((int) $branch->level === 1)
{
$name = $branch->text;
}
else
{
$levelPrefix = str_repeat('- ', max(0, $branch->level - 1));
if (trim($name, '**') === 'Language')
{
$text = FinderHelperLanguage::branchLanguageTitle($branch->text);
}
else
{
$key = FinderHelperLanguage::branchSingular($branch->text);
$text = $lang->hasKey($key) ? JText::_($key) : $branch->text;
}
// Initialize the group if necessary.
if (!isset($groups[$name]))
{
$groups[$name] = array();
}
$groups[$name][] = JHtml::_('select.option', $branch->value, $levelPrefix . $text);
}
}
}
// Merge any additional groups in the XML definition.
$groups = array_merge(parent::getGroups(), $groups);
return $groups;
}
}