Sindbad~EG File Manager

Current Path : /var/www/ispania.gr/modules/mod_rsdirectory_categories/
Upload File :
Current File : /var/www/ispania.gr/modules/mod_rsdirectory_categories/helper.php

<?php
/**
 * @package RSDirectory!
 * @copyright (C) 2013-2021 www.rsjoomla.com
 * @license GPL, http://www.gnu.org/copyleft/gpl.html
 */

// No direct access.
defined('_JEXEC') or die('Restricted access');

/**
 * RSDirectory! Categories Module Helper.
 */
class RSDirectoryCategoriesHelper
{
	protected $params;
	public $selected_categories;
	protected $show_subcategories = 1;
	
	public function __construct($params) {
		$this->params = $params;

		$this->selected_categories = RSDirectoryHelper::arrayInt($this->params->get('selected_categories', array()));
		if (empty($this->selected_categories) || in_array(0, $this->selected_categories)) {
			$this->selected_categories = array();
		}

		$this->show_subcategories = (int) $this->params->get('show_subcategories', '1');
	}
	
	public function escape($string) {
		return htmlentities($string, ENT_COMPAT, 'utf-8');
	}
	
	// Get categories
	public function getCategories() {

		$options = array(
			'extension' 	=> 'com_rsdirectory',
			'table' 		=> '#__rsdirectory_entries',
			'field' 		=> 'category_id',
			'statefield' 	=> 'published'
		);
			
		// Create a new JCategories object.
		$categories = new JCategories($options);

		if ($root = $categories->get('root')) {
			if ($children = $root->getChildren()) {
				if ($this->selected_categories) {
					$this->getIds($children);
					$children = $this->remakeChildren($children);
				} else {
					if (!$this->show_subcategories) {
						// when all categories are selected but the show_subcategories is set to 'no' we must remove the child elements
						$new_children = array();
						foreach ($children as &$child) {
							if ($inner_children = $child->getChildren()){
								foreach ($inner_children as $inner_child) {
									if (RSDirectoryHelper::isJ4())
									{
										$child->removeChild($inner_child);
									}
									else {
										$inner_child->removeChild($inner_child->id);
									}
								}
							}
							$new_children[] = $child;
						}
						$children = $new_children;
					}
				}

				return $children;
			}
		}
		
		return false;
	}

	protected function remakeChildren($children) {
		$new_children = array();
		foreach ($children as $child) {
			if(!in_array($child->id, $this->selected_categories)) {
				if ($tmp = $child->getChildren()) {
					$new_children = array_merge($new_children, $this->remakeChildren($tmp));
				}
			} else {
				$new_children[] = $child;
			}
		}

		return $new_children;
	}

	protected function getIds($categories) {
		foreach ($categories as $category) {
			$has_children = $category->hasChildren();
			$children = null;

			// if selected categories are defined search inside
			if ($this->selected_categories && $this->show_subcategories) {
				$children_ids = array();

				if ($has_children) {
					$children = $category->getChildren();

					foreach ($children as $child) {
						$children_ids[] = $child->id;
					}

					$intersect = array_intersect($this->selected_categories, $children_ids);

					// if the child categories are not in the selected categories but the parent is add all the children to the ids
					if (count($intersect) == 0 && in_array($category->id, $this->selected_categories)) {
						$tmp = array_merge($this->selected_categories, $children_ids);
						$this->selected_categories = array_unique($tmp);
					}
				}
			}
			// if the category has any children parse them too
			if ($has_children) {
				$this->getIds((!is_null($children) ? $children : $category->getChildren()));
			}
		}
	}
	
	public function numEntriesCategories($category_id = null) {
		static $num_items = array();

		// skip for null
		if (is_null($category_id)) {
			return 0;
		}

		if (!isset($num_items[$category_id])) {
			$num_items[$category_id] = RSDirectoryHelper::countEntriesFromCategory($category_id);
		}

		return (int) $num_items[$category_id];
	}
	
	public function sortCategories(&$categories) {
		switch ($this->params->get('order_categories', 'order'))
		{
			case 'alpha':
				usort($categories, array($this, 'compareAlpha'));
			break;
			
			case 'ralpha':
				usort($categories, array($this, 'compareReverseAlpha'));
			break;
		}
	}
	
	protected function compareAlpha($a, $b) {
		return strcmp($a->title, $b->title);
	}
	
	protected function compareReverseAlpha($a, $b) {
		return strcmp($b->title, $a->title);
	}
	
	public function getThumbnail($thumbnail) {
		// Get DBO.
        $db = JFactory::getDbo();
		
		$query = $db->getQuery(true)
               ->select( $db->qn('hash') )
               ->from( $db->qn('#__rsdirectory_uploaded_files'))
               ->where( $db->qn('id') . ' = ' . $db->q($thumbnail) );
		
		if ($hash = $db->setQuery($query)->loadResult()) {
			return RSDirectoryHelper::getImageURL($hash, 'small');
		}
		
		return false;
	}
	
}

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists