Current File : /home/pacjaorg/public_html/km/administrator/components/com_djclassifieds/lib/djregion.php
<?php

/**
 * @package DJ-Classifieds
 * @copyright Copyright (C) DJ-Extensions.com, All rights reserved.
 * @license http://www.gnu.org/licenses GNU/GPL
 * @author url: http://dj-extensions.com
 * @author email contact@dj-extensions.com
 */

defined('_JEXEC') or die('Restricted access');

require_once(JPATH_ROOT . '/administrator/components/com_djclassifieds/lib/djseo.php');

class DJRegOptionList
{
	var $text;
	var $value;
	var $disable;
	var $level;

	function __construct()
	{
		$text = null;
		$value = null;
		$disable = null;
	}
}

class RegionItem
{
	var $id;
	var $name;
	var $alias;
	var $parent_id;
	var $parent_name;
	var $city;
	var $country;
	var $country_iso;
	var $published;
	var $level;
	var $ordering;
	var $items_count;

	function __construct()
	{
		$id = null;
		$name = null;
		$alias = null;
		$parent_id = null;
		$parent_name = null;
		$city = null;
		$country = null;
		$country_iso = null;
		$published = null;
		$level = 0;
		$ordering = null;
	}
}

class DJClassifiedsRegion
{
	var $parent_id;
	var $id;
	var $name;
	var $children = array();
	var $level;

	function __construct()
	{
		$parent_id = null;
		$id = null;
		$name = null;
		$children[] = null;
		$elem[] = null;
		$level = 0;
		$items_count = 0;
	}

	public static function getRegSelect()
	{
		$regions_main = DJClassifiedsRegion::getRegionsMain();
		$regions_sort = DJClassifiedsRegion::getRegionsSortParent();

		$sort_regions = DJClassifiedsRegion::getListSelect($regions_main, $regions_sort);

		return $sort_regions;
	}

	public static function getRegAll($pub = '0', $ord = 'name', $ord_dir = 'ASC')
	{
		$regions_main = DJClassifiedsRegion::getRegionsMain($pub, $ord, $ord_dir);
		$regions_sort = DJClassifiedsRegion::getRegionsSortParent($pub, $ord, $ord_dir);
		$sort_regs = DJClassifiedsRegion::getListAll($regions_main, $regions_sort);

		return $sort_regs;
	}

	public static function getSubReg($id, $show_count = '0', $pub = '0', $ord = 'name', $ord_dir = 'ASC')
	{
		$regions_main = DJClassifiedsRegion::getRegionsMain($pub);
		$regions_sort = DJClassifiedsRegion::getRegionsSortParent($pub, $ord, $ord_dir);

		$sort_regs = DJClassifiedsRegion::getListSubreg($regions_main, $regions_sort, $id);

		$reg_path = DJClassifiedsRegion::getParentPath($id);
		$level_over = count($reg_path);
		foreach ($sort_regs as &$reg) {
			$reg->level = $reg->level - $level_over;
			if ($reg->level < 0) {
				$reg->level = 0;
			}
		}

		if ($show_count) {
			$max_level = '0';
			foreach ($sort_regs as $r) {
				if ($r->level > $max_level) {
					$max_level = $r->level;
				}
			}

			for ($level = $max_level; $level > -1; $level--) {
				$parent_value = 0;
				for ($r = count($sort_regs); $r > 0; $r--) {
					if ($parent_value > 0 && $level > $sort_regs[$r - 1]->level) {
						$sort_regs[$r - 1]->items_count = $sort_regs[$r - 1]->items_count + $parent_value;
						$parent_value = 0;
					}
					if ($level == $sort_regs[$r - 1]->level) {
						$parent_value = $parent_value + $sort_regs[$r - 1]->items_count;
					}
				}
			}
		}
		return $sort_regs;
	}

	public static function getMenuRegions($rid = '0', $show_count = '0', $include_def = '0', $pub = '0', $ord = 'name', $ord_dir = 'ASC')
	{
		$regions_main = DJClassifiedsRegion::getRegionsMain($pub);
		$regions_sort = DJClassifiedsRegion::getRegionsSortParent($pub, $ord, $ord_dir);
		$sort_regs = DJClassifiedsRegion::getListAll($regions_main, $regions_sort);

		if ($show_count) {
			$max_level = '0';
			foreach ($sort_regs as $r) {
				if ($r->level > $max_level) {
					$max_level = $r->level;
				}
			}

			for ($level = $max_level; $level > -1; $level--) {
				$parent_value = 0;
				for ($r = count($sort_regs); $r > 0; $r--) {
					if ($parent_value > 0 && $level > $sort_regs[$r - 1]->level) {
						$sort_regs[$r - 1]->items_count = $sort_regs[$r - 1]->items_count + $parent_value;
						$parent_value = 0;
					}
					if ($level == $sort_regs[$r - 1]->level) {
						$parent_value = $parent_value + $sort_regs[$r - 1]->items_count;
					}
				}
			}
		}

		$reg_path = ',' . $rid . ',';
		if ($rid > 0) {
			$reg_id = $rid;
			while ($reg_id != 0) {
				$reg_found = 0;
				foreach ($sort_regs as $r) {
					if ($r->id == $reg_id) {
						$reg_id = $r->parent_id;
						$reg_path .= $reg_id . ',';
						$reg_found = 1;
						break;
					}
				}
				if (!$reg_found) {
					$reg_path = '';
					break;
				}
			}
		}

		$menu_regs = array();
		for ($i = 0; $i < count($sort_regs); $i++) {
			if (strstr($reg_path, ',' . $sort_regs[$i]->id . ',') || strstr($reg_path, ',' . $sort_regs[$i]->parent_id . ',')) {
				$menu_regs[] = $sort_regs[$i];
			}
		}
		$ret = array();
		$ret[] = $menu_regs;
		$ret[] = $reg_path;
		$ret[] = $sort_regs;

		return $ret;
	}

	public static function getRegAllItemsCount($pub = '0', $hide_empty = '0', $include_def = '0', $ord = 'name', $ord_dir = 'ASC')
	{
		$regs = DJClassifiedsRegion::getRegionsSortParent($pub, $ord, $ord_dir);

		if ($include_def) {
			$def_reg      = DJClassifiedsRegion::getDefaultRegion();
			if ($def_reg > 0) {
				return DJClassifiedsRegion::getSubReg($def_reg, 1);
			}
		}

		if (isset($regs[0])) {
			$sort_regs = DJClassifiedsRegion::getListAll($regs[0], $regs);
		} else {
			$sort_regs = array();
		}

		$max_level = '0';
		foreach ($sort_regs as $r) {
			if ($r->level > $max_level) {
				$max_level = $r->level;
			}
		}

		for ($level = $max_level; $level > -1; $level--) {
			$parent_value = 0;
			for ($r = count($sort_regs); $r > 0; $r--) {
				if ($parent_value > 0 && $level > $sort_regs[$r - 1]->level) {
					$sort_regs[$r - 1]->items_count = $sort_regs[$r - 1]->items_count + $parent_value;
					$parent_value = 0;
				}
				if ($level == $sort_regs[$r - 1]->level) {
					$parent_value = $parent_value + $sort_regs[$r - 1]->items_count;
				}
			}
		}

		if ($hide_empty) {
			$reg_items = array();
			for ($i = 0; $i < count($sort_regs); $i++) {
				if ($sort_regs[$i]->items_count) {
					$reg_items[] = $sort_regs[$i];
				}
			}
			return $reg_items;
		} else {
			return $sort_regs;
		}
	}

	public static function getParentPath($rid = '0', $pub = '0', $ord = 'name', $ord_dir = 'ASC')
	{
		$regs = DJClassifiedsRegion::getRegions($pub, $ord, $ord_dir);
		$reg_path = array();
		if (count($regs)) {
			while ($rid != 0) {
				if (isset($regs[$rid])) {
					$reg_path[] =  $regs[$rid];
					$rid = 	$regs[$rid]->parent_id;
				} else {
					break;
				}
			}
		}

		return $reg_path;
	}

	public static function getSEOParentPath($rid = '0', $pub = '0', $ord = 'name', $ord_dir = 'ASC')
	{
		$regs = DJClassifiedsRegion::getRegions($pub, $ord, $ord_dir);
		$reg_path = array();

		while ($rid != 0) {
			if (isset($regs[$rid])) {
				$reg_path[] = $regs[$rid]->id . ':' . (!empty($regs[$rid]->alias) ? $regs[$rid]->alias : $regs[$rid]->name);
				$rid = $regs[$rid]->parent_id;
			} else {
				break;
			}
		}

		return $reg_path;
	}

	public static function getDefaultRegion()
	{
		$inputCookie  = JFactory::getApplication()->input->cookie;
		$def_reg      = $inputCookie->get('djcf_regid', 0);
		return $def_reg;
	}

	public static function getDefaultRegionItem()
	{
		$inputCookie  = JFactory::getApplication()->input->cookie;
		$def_reg      = $inputCookie->get('djcf_regid', 0);

		if ($def_reg > 0) {
			$reg_all = self::getRegions();
			return !empty($reg_all[$def_reg]) ? $reg_all[$def_reg] : null;
		}

		return null;
	}

	public static function getDefaultRegionsIds()
	{
		$reg_ids = '';
		$def_reg = DJClassifiedsRegion::getDefaultRegion();
		if ($def_reg > 0) {
			$reg_ids = DJClassifiedsRegion::getDefaultSubRegions($def_reg, 1);
		}
		return $reg_ids;
	}

	public static function getDefaultSubRegions($id, $type = 0)
	{
		$regions_main = DJClassifiedsRegion::getRegionsMain();
		$regions_sort = DJClassifiedsRegion::getRegionsSortParent();

		$sort_regs = DJClassifiedsRegion::getListSubreg($regions_main, $regions_sort, $id);

		if ($type == 1) {
			$reg_ids = $id;
			foreach ($sort_regs as $reg) {
				$reg_ids .= ',' . $reg->id;
			}
			return $reg_ids;
		}

		return $sort_regs;
	}

	private static $_regions = null;
	private static $_regions_sort_parent = null;
	private static $_items_count = null;

	static function setItemsCount()
	{
		$db = JFactory::getDBO();
		$par = JComponentHelper::getParams('com_djclassifieds');
		$date_now = JFactory::getDate()->format('Y-m-d H:00:00');

		if (!isset(self::$_items_count)) {
			$query = "SELECT i.region_id, count(i.id) as items_count FROM #__djcf_items i WHERE i.published=1 AND i.blocked=0 AND i.date_exp > '" . $date_now . "' GROUP BY i.region_id";

			if ($par->get('cache_lib_regs', '0') == '1') {
				$cache = JFactory::getCache();
				$cache->setCaching(1);
				self::$_items_count = $cache->get(array('DJClassifiedsRegion', 'getItemsCount'), array($query));
			} else {
				$db->setQuery($query);
				self::$_items_count = $db->loadObjectList('region_id');
			}
		}
	}

	public static function getItemsCount($query)
	{
		$db = JFactory::getDBO();
		$db->setQuery($query);
		return $db->loadObjectList('region_id');
	}

	static function getAllRegions($p = '0', $ord = 'ordering', $ord_dir = 'ASC')
	{
		$db = JFactory::getDBO();
		$where = "";

		if ($p) {
			$where = "WHERE published=1 ";
		}

		$order = $ord == 'ord' ? 'ordering' : $ord;

		$query = "SELECT r.* FROM #__djcf_regions r "
			. $where
			. "ORDER BY r.parent_id, " . $order . " " . $ord_dir;
		$db->setQuery($query);
		$allregions = $db->loadObjectList('id');

		return $allregions;
	}

	static function getMainRegions($p = '0', $ord = 'ordering', $ord_dir = 'ASC')
	{
		$db = JFactory::getDBO();
		$where = "";

		if ($p) {
			$where = "AND published=1 ";
		}

		$order = $ord == 'ord' ? 'ordering' : $ord;

		$query = "SELECT r.* FROM #__djcf_regions r "
			. "WHERE r.parent_id=0 " . $where
			. "ORDER BY r.parent_id, " . $order . " " . $ord_dir;
		$db->setQuery($query);
		$allregions = $db->loadObjectList('id');

		return $allregions;
	}

	public static function getRegions($p = '0', $ord = 'ord', $ord_dir = 'ASC')
	{
		if (!self::$_regions) {
			self::$_regions = array();
		}
		if (isset(self::$_regions[$p . '_' . $ord])) {
			return self::$_regions[$p . '_' . $ord];
		}

		$allregions = self::getAllRegions($p, $ord, $ord_dir);
		self::setItemsCount();

		foreach ($allregions as $reg) {
			$reg->parent_name = isset($allregions[$reg->parent_id]) ? $allregions[$reg->parent_id]->name : '';
			$reg->items_count = !empty(self::$_items_count[$reg->id]) ? self::$_items_count[$reg->id]->items_count : 0;

			if (!$reg->alias) {
				$reg->alias = DJClassifiedsSEO::getAliasName($reg->name);
			}
		}

		self::$_regions[$p . '_' . $ord] = $allregions;
		return self::$_regions[$p . '_' . $ord];
	}

	public static function getRegionsMain($p = '0', $ord = 'ord', $ord_dir = 'ASC')
	{
		if (!self::$_regions) {
			self::$_regions = array();
		}
		if (isset(self::$_regions[$p . '_' . $ord])) {
			return self::$_regions[$p . '_' . $ord];
		}

		$allregions = self::getMainRegions($p, $ord, $ord_dir);
		self::setItemsCount();

		foreach ($allregions as $reg) {
			$reg->parent_name = isset($allregions[$reg->parent_id]) ? $allregions[$reg->parent_id]->name : '';
			$reg->items_count = !empty(self::$_items_count[$reg->id]) ? self::$_items_count[$reg->id]->items_count : 0;
		}

		self::$_regions[$p . '_' . $ord] = $allregions;
		return self::$_regions[$p . '_' . $ord];
	}

	public static function getRegionsSortParent($p = '0', $ord = 'ordering', $ord_dir = 'ASC')
	{
		if (!self::$_regions_sort_parent) {
			self::$_regions_sort_parent = array();
		}
		if (isset(self::$_regions_sort_parent[$p . '_' . $ord])) {
			return self::$_regions_sort_parent[$p . '_' . $ord];
		}

		$allregions = self::getAllRegions($p, $ord, $ord_dir);
		self::setItemsCount();

		$regions = array();
		foreach ($allregions as $reg) {
			$reg->parent_name = isset($allregions[$reg->parent_id]) ? $allregions[$reg->parent_id]->name : '';
			$reg->items_count = !empty(self::$_items_count[$reg->id]) ? self::$_items_count[$reg->id]->items_count : 0;

			if (!isset($regions[$reg->parent_id])) {
				$regions[$reg->parent_id] = array();
			}
			$regions[$reg->parent_id][] = $reg;
		}

		self::$_regions_sort_parent[$p . '_' . $ord] = $regions;
		return self::$_regions_sort_parent[$p . '_' . $ord];
	}

	public static function getListSelect(&$lists, &$lists_const, &$option = array())
	{
		foreach ($lists as $list) {
			$op = new DJRegOptionList;
			$op->text = $list->name;
			$op->value = $list->id;
			$op->level = !empty($list->level) ? $list->level : 0;
			$option[] = $op;
			$children = array();

			if (isset($lists_const[$list->id])) {
				for ($i = 0; $i < count($lists_const[$list->id]); $i++) {
					$child = new RegionItem();
					$child->id = $lists_const[$list->id][$i]->id;
					$child->parent_id = $lists_const[$list->id][$i]->parent_id;
					if (isset($list->level)) {
						$child->level = $list->level + 1;
					} else {
						$child->level = 1;
					}

					$new_name = $lists_const[$list->id][$i]->name;
					for ($lev = 0; $lev < $child->level; $lev++) {
						$new_name = "- " . $new_name;
					}
					$child->name = $new_name;
					$children[] = $child;
				}
				DJClassifiedsRegion::getListSelect($children, $lists_const, $option);
				unset($lists_const[$list->id]);
			}
		}

		return ($option);
	}

	public static function getListAll(&$lists, &$lists_const, &$option = array())
	{
		foreach ($lists as $list) {
			$reg_item = new RegionItem();
			$reg_item->id = $list->id;
			$reg_item->name = $list->name;
			$reg_item->alias = $list->alias;
			$reg_item->parent_id = $list->parent_id;
			$reg_item->parent_name = $list->parent_name;
			$reg_item->country = $list->country;
			$reg_item->country_iso = $list->country_iso;
			$reg_item->city = $list->city;
			$reg_item->published = $list->published;
			$reg_item->items_count = $list->items_count;
			$reg_item->ordering = $list->ordering;

			if (isset($list->level)) {
				$reg_item->level = $list->level;
			} else {
				$reg_item->level = 0;
			}

			$option[] = $reg_item;
			$children = array();

			if (isset($lists_const[$list->id])) {
				for ($i = 0; $i < count($lists_const[$list->id]); $i++) {
					$child = new RegionItem();
					$child->id = $lists_const[$list->id][$i]->id;
					$child->name = $lists_const[$list->id][$i]->name;
					$child->alias = $lists_const[$list->id][$i]->alias;
					$child->parent_id = $lists_const[$list->id][$i]->parent_id;
					$child->parent_name = $lists_const[$list->id][$i]->parent_name;
					$child->country = $lists_const[$list->id][$i]->country;
					$child->country_iso = $lists_const[$list->id][$i]->country_iso;
					$child->city = $lists_const[$list->id][$i]->city;
					$child->published = $lists_const[$list->id][$i]->published;
					$child->items_count = $lists_const[$list->id][$i]->items_count;
					$child->ordering = $lists_const[$list->id][$i]->ordering;

					if (isset($list->level)) {
						$child->level = $list->level + 1;
					} else {
						$child->level = 1;
					}
					$children[] = $child;
				}
				DJClassifiedsRegion::getListAll($children, $lists_const, $option);
				unset($lists_const[$list->id]);
			}
		}

		return ($option);
	}

	public static function getListSubreg(&$lists, &$lists_const, $main_id = 0, $main_level = 0, $main_f = 0, &$option = array())
	{
		foreach ($lists as $list) {
			if (isset($list->level)) {
				$current_level = $list->level;
			} else {
				$current_level = 0;
			}

			if ($main_f == 1 && ($main_level > $current_level || $current_level == $main_level)) {
				break;
			}

			if ($main_id == $list->id) {
				$main_f = 1;
				$main_level = $current_level;
			}

			if ($main_f == 1 && $main_level < $current_level) {
				$reg_item = new RegionItem;
				$reg_item->id = $list->id;
				$reg_item->name = $list->name;
				$reg_item->alias = $list->alias;
				$reg_item->parent_id = $list->parent_id;
				$reg_item->parent_name = $list->parent_name;
				$reg_item->country = $list->country;
				$reg_item->country_iso = $list->country_iso;
				$reg_item->city = $list->city;
				$reg_item->published = $list->published;
				$reg_item->level = $current_level;
				$reg_item->items_count = $list->items_count;
				$reg_item->ordering = $list->ordering;

				$option[] = $reg_item;
			}
			$children = array();

			if (isset($lists_const[$list->id])) {
				for ($i = 0; $i < count($lists_const[$list->id]); $i++) {
					$child = new RegionItem();
					$child->id = $lists_const[$list->id][$i]->id;
					$child->name = $lists_const[$list->id][$i]->name;
					$child->alias = $lists_const[$list->id][$i]->alias;
					$child->parent_id = $lists_const[$list->id][$i]->parent_id;
					$child->parent_name = $lists_const[$list->id][$i]->parent_name;
					$child->country = $lists_const[$list->id][$i]->country;
					$child->country_iso = $lists_const[$list->id][$i]->country_iso;
					$child->city = $lists_const[$list->id][$i]->city;
					$child->published = $lists_const[$list->id][$i]->published;
					$child->items_count = $lists_const[$list->id][$i]->items_count;
					$child->ordering = $lists_const[$list->id][$i]->ordering;

					if (isset($list->level)) {
						$child->level = $list->level + 1;
					} else {
						$child->level = 1;
					}
					$children[] = $child;
				}

				DJClassifiedsRegion::getListSubreg($children, $lists_const, $main_id, $main_level, $main_f, $option);
				unset($lists_const[$list->id]);
			}
		}
		return ($option);
	}

	public static function getRegFullPath($rid, $link = false, $delimiter = ' - ')
	{
		$regs = self::getParentPath($rid);
		$reg_items = array();

		foreach ($regs as $key => $reg) {
			$reg_item = '<span class="reg_path path' . $key . ' reg' . $reg->id . '">';
			if ($link) {
				$reg_item .= '<a href="' . DJClassifiedsSEO::getRegionRoute($reg->id . ':' . $reg->alias) . '">' . JText::_($reg->name) . '</a>';
			} else {
				$reg_item .= JText::_($reg->name);
			}
			$reg_item .= '</span>';

			$reg_items[] = $reg_item;
		}

		$reg_items = array_reverse($reg_items);
		return implode('<span class="delimiter">' . $delimiter . '</span>', $reg_items);
	}

	static function getRegionParts($region_id)
	{
		$regions = self::getParentPath($region_id, 1);
		$reg_parts = array();
		$reg_path = array();
		$reg_parts['country'] = '';
		$reg_parts['city'] = '';

		foreach ($regions as $li) {
			$li->name = JText::_($li->name);
			$reg_path[] = $li->name;
			if ($li->country) {
				$reg_parts['country'] = $li->name;
			}
			if ($li->city) {
				$reg_parts['city'] = $li->name;
			}
		}
		$reg_parts['reg_path'] = implode(', ', $reg_path);

		return $reg_parts;
	}
}
Site is undergoing maintenance

PACJA Events

Maintenance mode is on

Site will be available soon. Thank you for your patience!