/administrator/components/com_finder/src/Field/ContentmapField.php

https://github.com/Hackwar/joomla-cms · PHP · 127 lines · 78 code · 18 blank · 31 comment · 4 complexity · eafa1be676764f137d573da2caf7987c MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Administrator
  4. * @subpackage com_finder
  5. *
  6. * @copyright (C) 2016 Open Source Matters, Inc. <https://www.joomla.org>
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. namespace Joomla\Component\Finder\Administrator\Field;
  10. \defined('JPATH_PLATFORM') or die;
  11. use Joomla\CMS\Factory;
  12. use Joomla\CMS\Form\Field\GroupedlistField;
  13. use Joomla\CMS\HTML\HTMLHelper;
  14. use Joomla\CMS\Language\Text;
  15. use Joomla\Component\Finder\Administrator\Helper\LanguageHelper;
  16. /**
  17. * Supports a select grouped list of finder content map.
  18. *
  19. * @since 3.6.0
  20. */
  21. class ContentmapField extends GroupedlistField
  22. {
  23. /**
  24. * The form field type.
  25. *
  26. * @var string
  27. * @since 3.6.0
  28. */
  29. public $type = 'ContentMap';
  30. /**
  31. * Method to get the list of content map options grouped by first level.
  32. *
  33. * @return array The field option objects as a nested array in groups.
  34. *
  35. * @since 3.6.0
  36. */
  37. protected function getGroups()
  38. {
  39. $groups = array();
  40. // Get the database object and a new query object.
  41. $db = Factory::getDbo();
  42. // Levels subquery.
  43. $levelQuery = $db->getQuery(true);
  44. $levelQuery->select('title AS branch_title, 1 as level')
  45. ->select($db->quoteName('id'))
  46. ->from($db->quoteName('#__finder_taxonomy'))
  47. ->where($db->quoteName('parent_id') . ' = 1');
  48. $levelQuery2 = $db->getQuery(true);
  49. $levelQuery2->select('b.title AS branch_title, 2 as level')
  50. ->select($db->quoteName('a.id'))
  51. ->from($db->quoteName('#__finder_taxonomy', 'a'))
  52. ->join('LEFT', $db->quoteName('#__finder_taxonomy', 'b') . ' ON ' . $db->quoteName('a.parent_id') . ' = ' . $db->quoteName('b.id'))
  53. ->where($db->quoteName('a.parent_id') . ' NOT IN (0, 1)');
  54. $levelQuery->union($levelQuery2);
  55. // Main query.
  56. $query = $db->getQuery(true)
  57. ->select($db->quoteName('a.title', 'text'))
  58. ->select($db->quoteName('a.id', 'value'))
  59. ->select($db->quoteName('d.level'))
  60. ->from($db->quoteName('#__finder_taxonomy', 'a'))
  61. ->join('LEFT', '(' . $levelQuery . ') AS d ON ' . $db->quoteName('d.id') . ' = ' . $db->quoteName('a.id'))
  62. ->where($db->quoteName('a.parent_id') . ' <> 0')
  63. ->order('d.branch_title ASC, d.level ASC, a.title ASC');
  64. $db->setQuery($query);
  65. try
  66. {
  67. $contentMap = $db->loadObjectList();
  68. }
  69. catch (\RuntimeException $e)
  70. {
  71. return;
  72. }
  73. // Build the grouped list array.
  74. if ($contentMap)
  75. {
  76. $lang = Factory::getLanguage();
  77. $name = '';
  78. foreach ($contentMap as $branch)
  79. {
  80. if ((int) $branch->level === 1)
  81. {
  82. $name = $branch->text;
  83. }
  84. else
  85. {
  86. $levelPrefix = str_repeat('- ', max(0, $branch->level - 1));
  87. if (trim($name, '**') === 'Language')
  88. {
  89. $text = LanguageHelper::branchLanguageTitle($branch->text);
  90. }
  91. else
  92. {
  93. $key = LanguageHelper::branchSingular($branch->text);
  94. $text = $lang->hasKey($key) ? Text::_($key) : $branch->text;
  95. }
  96. // Initialize the group if necessary.
  97. if (!isset($groups[$name]))
  98. {
  99. $groups[$name] = array();
  100. }
  101. $groups[$name][] = HTMLHelper::_('select.option', $branch->value, $levelPrefix . $text);
  102. }
  103. }
  104. }
  105. // Merge any additional groups in the XML definition.
  106. $groups = array_merge(parent::getGroups(), $groups);
  107. return $groups;
  108. }
  109. }