Skip to content

Instantly share code, notes, and snippets.

@hans2103
Created March 11, 2019 11:03
Show Gist options
  • Save hans2103/94a4dc741205e50e9980a91fa20b8d9a to your computer and use it in GitHub Desktop.
Save hans2103/94a4dc741205e50e9980a91fa20b8d9a to your computer and use it in GitHub Desktop.
RSForm Helper to get a list of titles from published categories and subdirectories
//<code>
require_once JPATH_SITE . '/templates/yourtemplate/html/com_rsform/custom/contact.php';
return (new ContactHelper)->getCourses(1402);
//</code>
<?php
/**
* @package Template
* @copyright 2019 Perfect Web Team / perfectwebteam.nl
* @license GNU General Public License version 3 or later
*/
// stored in templates/yourtemplate/html/rsform/custom/contact.php
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\Registry\Registry;
defined('_JEXEC') or die();
/**
* Helper for contact form.
*
* @package Template
* @since 1.0.0
*/
class ContactHelper
{
/**
* Get a list of courses to show.
*
* @param integer $categoryId The parent category ID to get the articles for
*
* @return string The courses to show.
*
* @since 1.0.0
*/
public function getCourses(int $categoryId)
{
BaseDatabaseModel::addIncludePath(JPATH_ROOT . '/components/com_content/models/');
/** @var ContentModelArticles $oModel */
$oModel = BaseDatabaseModel::getInstance('Articles', 'ContentModel', array('ignore_request' => true));
// Get the subcategories
$catid = $this->getSubcategories($categoryId);
$oModel->setState('filter.category_id', $catid);
$oModel->setState('params', new Registry);
$oModel->setState('filter.language', true);
// We assume that any training that is no longer published, is finished so can be ignored
$oModel->setState('filter.published', 1);
$oModel->setState('filter.get_children', 5);
$oModel->setState('list.ordering', 'c.rgt');
$items = $oModel->getItems();
$values = array();
$values[] = '|' . Text::_('JSELECT');
$group = '';
foreach ($items as $item)
{
if ($group !== $item->category_title)
{
$group = $item->category_title;
$values[] = $group . '[g]';
}
$values[] = $item->title;
}
return implode("\n", $values);
}
/**
* Get a list of category IDs.
*
* @param int $categoryId The category to get the sub IDs for
*
* @return array List of subcategory IDs.
*
* @since 1.0.0
*/
private function getSubcategories($categoryId)
{
// Get a list of content categories
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('node.id'))
->from($db->quoteName('#__categories', 'node'))
->from($db->quoteName('#__categories', 'parent'))
->where($db->quoteName('node.lft') . ' BETWEEN ' . $db->quoteName('parent.lft') . ' AND ' . $db->quoteName('parent.rgt'))
->where($db->quoteName('parent.id') . ' = ' . (int) $categoryId);
$db->setQuery($query);
return $db->loadColumn();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment