/**
	 * Display Function
	 *
	 * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
	 *
	 * @return  mixed  A string if successful, otherwise a Error object.
	 *
	 * @since       1.7.2
	 */
	public function display($tpl = null)
	{
		/** @var JApplicationSite $app */
		$app = JFactory::getApplication();

		// Check for errors.
		if (count($errors = $this->get('Errors')))
		{
			$app->enqueueMessage(implode("\n", $errors), 'error');

			return false;
		}

		$doc       = JFactory::getDocument();
		$params    = $app->getParams();
		$feedEmail = $app->get('feed_email', 'author');
		$siteEmail = $app->get('mailfrom');
		$fromName  = $app->get('fromname');

		$app->input->set('limit', $app->get('feed_limit'));

		// Get some data from the models
		$category = $this->get('Category');
		$rows     = $this->get('Items');

		$doc->link = JRoute::_(ChurchDirectoryHelperRoute::getCategoryRoute($category->id));

		foreach ($rows as $row)
		{
			// Strip html from feed item title
			$title = $this->escape($row->name);
			$title = html_entity_decode($title, ENT_COMPAT, 'UTF-8');

			// Compute the churchdirectory slug
			$row->slug = $row->alias ? ($row->id . ':' . $row->alias) : $row->id;

			// Url link to article
			$link = JRoute::_(ChurchDirectoryHelperRoute::getMemberRoute($row->slug, $row->catid));

			$description = $row->introtext;
			$author      = $row->created_by_alias ? $row->created_by_alias : $row->author;
			@$date = ($row->created ? date('r', strtotime($row->created)) : '');

			// Load individual item creator class
			$item              = new JFeedItem;
			$item->title       = $title;
			$item->link        = $link;
			$item->description = $description;
			$item->date        = $date;
			$item->category    = $category->title;
			$item->author      = $author;

			// We don't have the author email so we have to use site in both cases.
			if ($feedEmail == 'site')
			{
				$item->authorEmail = $siteEmail;
			}
			elseif ($feedEmail == 'author')
			{
				$item->authorEmail = $row->author_email;
			}

			// Loads item info into rss array
			$doc->addItem($item);
		}

		return true;
	}
$class = ' class="first"';
if (count($this->children[$this->category->id]) > 0 && $this->maxLevel != 0) :
	?>
<ul class="list-striped list-condensed">
	<?php foreach ($this->children[$this->category->id] as $id => $child) : ?>
	<?php
	if ($this->params->get('show_empty_categories') || $child->numitems || count($child->getChildren())) :
		if (!isset($this->children[$this->category->id][$id + 1]))
		{
			$class = ' class="last"';
		}
		?>
        <li<?php echo $class; ?>>
			<?php $class = ''; ?>
            <h4 class="item-title">
                <a href="<?php echo JRoute::_(ChurchDirectoryHelperRoute::getCategoryRoute($child->id)); ?>">
					<?php echo $this->escape($child->title); ?>
                </a>

				<?php if ($this->params->get('show_cat_items') == 1) : ?>
                <span class="badge badge-info pull-right"
                      title="<?php echo JText::_('COM_CONTACT_CAT_NUM'); ?>"><?php echo $child->numitems; ?></span>
				<?php endif; ?>
            </h4>

			<?php if ($this->params->get('show_subcat_desc') == 1) : ?>
			<?php if ($child->description) : ?>
                <small class="category-desc">
					<?php echo JHtml::_('content.prepare', $child->description, '', 'com_churchdirectoy.category'); ?>
                </small>
				<?php endif; ?>
			{
				echo $children . '<br />';
			} ?>
		</p>
	<?php } ?>
	<?php if ($this->params->get('show_contact_category') == 'show_no_link')
	{
		?>
		<h3>
			<span class="contact-category"><?php echo $this->member->category_title; ?></span>
		</h3>
	<?php } ?>
	<?php if ($this->params->get('show_contact_category') == 'show_with_link')
	{
		?>
		<?php $contactLink = ChurchDirectoryHelperRoute::getCategoryRoute($this->member->catid); ?>
		<h3>
			<span class="contact-category"><a href="<?php echo $contactLink; ?>">
					<?php echo $this->escape($this->member->category_title); ?></a>
			</span>
		</h3>
	<?php } ?>
	<?php if ($this->params->get('show_contact_list') && count($this->contacts) > 1)
	{
		?>
		<form action="#" method="get" name="selectForm" id="selectForm">
			<?php echo JText::_('COM_CHURCHDIRECTORY_SELECT_CHURCHDIRECTORY'); ?>
			<?php echo JHtml::_('select.genericlist', $this->contacts, 'id', 'class="inputbox" onchange="document.location.href = this.value"', 'link', 'name', $this->member->link); ?>
		</form>
	<?php } ?>
	/**
	 * Prepares the document
	 *
	 * @return void
	 *
	 * @since       1.7.2
	 */
	protected function _prepareDocument()
	{
		$app     = JFactory::getApplication();
		$menus   = $app->getMenu();
		$pathway = $app->getPathway();
		$title   = null;

		// Because the application sets a default page title,
		// we need to get it from the menu item itself
		$menu = $menus->getActive();

		if ($menu)
		{
			$this->params->def('page_heading', $this->params->get('page_title', $menu->title));
		}
		else
		{
			$this->params->def('page_heading', JText::_('COM_CHURCHDIRECTORY_DEFAULT_PAGE_TITLE'));
		}

		$title = $this->params->get('page_title', '');

		$id = (int) @$menu->query['id'];

		// If the menu item does not concern this contact
		if ($menu && ($menu->query['option'] != 'com_churchdirectory' || $menu->query['view'] != 'member' || $id != $this->item->id))
		{
			// If this is not a single churchdirectory menu item, set the page title to the contact title
			if ($this->item->name)
			{
				$title = $this->item->name;
			}

			$path     = [['title' => $this->member->name, 'link' => '']];
			$category = JCategories::getInstance('ChurchDirectory')->get($this->member->catid);

			while ($category && ($menu->query['option'] != 'com_churchdirectory'
					|| $menu->query['view'] == 'member'
					|| $id != $category->id) && $category->id > 1)
			{
				$path[]   = ['title' => $category->title, 'link' => ChurchDirectoryHelperRoute::getCategoryRoute($this->member->catid)];
				$category = $category->getParent();
			}

			$path = array_reverse($path);

			foreach ($path as $item)
			{
				$pathway->addItem($item['title'], $item['link']);
			}
		}

		if (empty($title))
		{
			$title = $app->get('sitename');
		}
		elseif ($app->get('sitename_pagetitles', 0) == 1)
		{
			$title = JText::sprintf('JPAGETITLE', $app->get('sitename'), $title);
		}
		elseif ($app->get('sitename_pagetitles', 0) == 2)
		{
			$title = JText::sprintf('JPAGETITLE', $title, $app->get('sitename'));
		}

		if (empty($title))
		{
			$title = $this->item->name;
		}

		$this->document->setTitle($title);

		if ($this->item->metadesc)
		{
			$this->document->setDescription($this->item->metadesc);
		}
		elseif (!$this->item->metadesc && $this->params->get('menu-meta_description'))
		{
			$this->document->setDescription($this->params->get('menu-meta_description'));
		}

		if ($this->item->metakey)
		{
			$this->document->setMetadata('keywords', $this->item->metakey);
		}
		elseif (!$this->item->metakey && $this->params->get('menu-meta_keywords'))
		{
			$this->document->setMetadata('keywords', $this->params->get('menu-meta_keywords'));
		}

		if ($this->params->get('robots'))
		{
			$this->document->setMetadata('robots', $this->params->get('robots'));
		}

		$mdata = $this->item->metadata->toArray();

		foreach ($mdata as $k => $v)
		{
			if ($v)
			{
				$this->document->setMetadata($k, $v);
			}
		}
	}