<?php
/**
 * @version $Header$
 * @package articles
 *
 * @copyright Copyright (c) 2004-2006, bitweaver.org
 * All Rights Reserved. See below for details and a complete list of authors.
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details.
 */

/**
 * Required setup
 */
namespace Bitweaver\Contact;

use Bitweaver\BitBase;

/**
 * @package contact
 */
class ContactType extends BitBase {
	public $mContactType;

	public function __construct() {
		parent::__construct();
	}

	/**
	 * setup()
	 * Setup the contact types for use in the content filter.
	 */
	public function setup() {
		global $gBitUser, $gBitSmarty;

			$roles = array_keys($gBitUser->mRoles ?? []) ?: [-1];
			$bindVars = [];
			$bindVars = array_merge( $bindVars, $roles, [ $gBitUser->mUserId ] );

			$sql = "SELECT r.`item`, r.`cross_ref_title`
					FROM `".BIT_DB_PREFIX."liberty_xref_item` r
					JOIN `".BIT_DB_PREFIX."liberty_xref_group` t ON t.`x_group` = r.`x_group` AND t.`content_type_guid` = r.`content_type_guid`
					LEFT OUTER JOIN `".BIT_DB_PREFIX."users_roles_map` purm ON ( purm.`user_id`=".$gBitUser->mUserId." ) AND ( purm.`role_id`=r.`role_id` )
					WHERE r.`content_type_guid` = 'contact' AND t.`sort_order` = 0 AND (r.`role_id` IN(". implode(',', array_fill(0, count($roles), '?')) ." ) OR purm.`user_id`=?)
					ORDER BY r.`item`";

		$result = $this->mDb->query( $sql, $bindVars );

		while( $res = $result->fetchRow() ) {
			$this->mContactType[ $res['item']] = $res['cross_ref_title'];
		}

//		asort($this->mContactType);
		$gBitSmarty->assign( 'contContactTypes', $this->mContactType );
	}

	/**
	 * processRequestHash(&$pRequest, &$pStore)
	 * Build contact_type settins hash for the session
	 */
	public function processRequestHash(&$pRequest, &$pStore) {
		global $gBitUser;
		if( !empty( $pRequest["contact_type_guid"] ) ) {
			if( $gBitUser->isRegistered() ) {
				$gBitUser->storePreference( 'contact_default_guids', serialize( $pRequest['contact_type_guid'] ) );
			}
			$pStore['contact_type_guid'] = $pRequest["contact_type_guid"];
		} elseif( !isset( $pStore['content_type_guid'] ) && $gBitUser->getPreference( 'contact_default_guids' ) && $gBitUser->isRegistered() ) {
			$pStore['contact_type_guid'] = unserialize( $gBitUser->getPreference( 'contact_default_guids' ) );
		} elseif( !isset( $pStore['content_type_guid'] ) ) {
			$pStore['contact_type_guid'] = [];
		} elseif( isset( $pRequest["refresh"] ) && !isset( $pRequest["contact_type_guid"] ) ) {
			$pStore['contact_type_guid'] = [];
		}
	}

	public static function getContactTypeList( $pOptionHash=NULL ) {
		global $gBitSystem;

		$where = '';
		$bindVars = [];
		if( !empty( $pOptionHash['active_role'] ) ) {
			$where = " WHERE cxt.`role_id` = ? ";
			$bindVars[] = $pOptionHash['active_role'];
		}
		if ( !empty(  $pOptionHash['title'] ) ) {
			$where = " WHERE cxt.`title` = ? ";
			$bindVars[] = $pOptionHash['title'];
		}

		$guidWhere = " cxt.`content_type_guid` = 'contact' ";
		$where     = $where ? $where . " AND $guidWhere" : " WHERE $guidWhere";

		$query = "SELECT cxt.*
				 FROM `".BIT_DB_PREFIX."liberty_xref_group` cxt
				 $where ORDER BY cxt.`sort_order`";

		$result = $gBitSystem->mDb->query( $query, $bindVars );

		$ret = [];

		while( $res = $result->fetchRow() ) {
			$res["num_types"] = $gBitSystem->mDb->getOne(
				"SELECT COUNT(*) FROM `".BIT_DB_PREFIX."liberty_xref_item` WHERE `x_group` = ? AND `content_type_guid` = 'contact'",
				[ $res["x_group"] ]
			);
			$ret[] = $res;
		}

		return $ret;
	}
}
