<?php
/**
 * @version     1.4.7
 * @package     sellacious
 *
 * @copyright   Copyright (C) 2012-2017 Bhartiy Web Technologies. All rights reserved.
 * @license     SPL Sellacious Private License; see http://www.sellacious.com/spl.html
 * @author      Izhar Aazmi <info@bhartiy.com> - http://www.bhartiy.com
 */
// no direct access
defined('_JEXEC') or die;

use Joomla\Registry\Registry;

/**
 * Methods supporting a list of Sellacious user records.
 */
class SellaciousModelUsers extends SellaciousModelList
{
	/**
	 * Constructor.
	 *
	 * @param  array  $config  An optional associative array of configuration settings.
	 *
	 * @see    JController
	 * @since  1.6
	 */
	public function __construct($config = array())
	{
		if (empty($config['filter_fields']))
		{
			$config['filter_fields'] = array(
				'id', 'u.id',
				'name', 'u.name',
				'username', 'u.username',
				'state', 'a.state',
			);
		}

		parent::__construct($config);
	}

	/**
	 * Gets the list of groups and adds expensive joins to the result set.
	 *
	 * @return  mixed  An array of data items on success, false on failure.
	 */
	public function getItems()
	{
		$store = $this->getStoreId();

		if (empty($this->cache[$store]))
		{
			if ($items = parent::getItems())
			{
				foreach ($items as &$item)
				{
					$bankinfo = new Registry;
					$bankinfo->loadString($item->bankinfo, 'JSON');
					$item->bankinfo = $bankinfo->toArray();

					$taxinfo = new Registry;
					$taxinfo->loadString($item->taxinfo, 'JSON');
					$item->taxinfo = $taxinfo->toArray();
				}
			}

			$this->cache[$store] = $items;
		}

		return $this->cache[$store];
	}

	/**
	 * Build an SQL query to load the list data.
	 *
	 * @return  JDatabaseQuery
	 */
	protected function getListQuery()
	{
		// Create a new query object.
		$db    = $this->getDbo();
		$query = $db->getQuery(true);

		$query->select('u.id, u.name, u.username, u.email, u.block as state, u.activation')
			  ->from('#__users u')
			  ->group('u.id')

			  ->select('a.id as profile_id, a.mobile, a.website, a.currency')
			  ->select('a.bankinfo, a.taxinfo, a.state as profile_state, a.ordering')
			  ->join('LEFT', '#__sellacious_profiles a ON a.user_id = u.id')

			  ->select('client.category_id AS client_category_id')
			  ->select('cc.title client_category_name')
			  ->join('LEFT', '#__sellacious_clients client ON client.user_id = u.id')
			  ->join('LEFT', '#__sellacious_categories cc ON cc.id = client.category_id')

			  ->select('mfr.category_id AS mfr_category_id, mfr.title AS mfr_company')
			  ->select('mc.title mfr_category_name')
			  ->join('LEFT', '#__sellacious_manufacturers mfr ON mfr.user_id = u.id')
			  ->join('LEFT', '#__sellacious_categories mc ON mc.id = mfr.category_id')

			  ->select('staff.category_id AS staff_category_id')
			  ->select('sc.title staff_category_name')
			  ->join('LEFT', '#__sellacious_staffs staff ON staff.user_id = u.id')
			  ->join('LEFT', '#__sellacious_categories sc ON sc.id = staff.category_id')

			  ->select('seller.category_id AS seller_category_id, seller.title AS seller_company')
			  ->select('vc.title seller_category_name')
			  ->join('LEFT', '#__sellacious_sellers seller ON seller.user_id = u.id')
			  ->join('LEFT', '#__sellacious_categories vc ON vc.id = seller.category_id');

		// Filter over the search string if set.
		$search = $this->getState('filter.search');

		if (!empty($search))
		{
			if (stripos($search, 'id:') === 0)
			{
				$query->where('a.user_id = ' . (int) substr($search, 3));
			}
			else
			{
				$search = $db->q('%' . $db->escape($search, true) . '%');
				$query->where('(u.name LIKE ' . $search . ' OR u.username LIKE ' . $search . ')');
			}
		}

		// Filter by published state
		$state = $this->getState('filter.state');

		if (is_numeric($state))
		{
			$query->where('a.state = ' . (int) $state);
		}
		elseif ($state == '')
		{
			$query->where('(a.state IN (0, 1) OR a.state IS NULL)');
		}

		// Filter by profile_type
		$profile_type = $this->getState('filter.profile_type');

		if (!empty($profile_type) && in_array($profile_type, explode(',', 'client,seller,staff,mfr')))
		{
			$query->where($db->qn($profile_type . '.category_id') . ' != ' . $db->q(''))->where($profile_type . '.state = 1');
		}

		// Filter by category(ies)
		$category = $this->getState('filter.category');

		if (!empty($category))
		{
			$query->where($db->q($category) . ' IN (client.category_id, mfr.category_id, staff.category_id, seller.category_id)');
		}

		// Add the list ordering clause.
		$ordering = $this->state->get('list.fullordering', 'a.ordering ASC');

		if ($ordering)
		{
			$query->order($db->escape($ordering));
		}

		return $query;
	}
}
