<?php

#
# XiVO Web-Interface
# Copyright (C) 2006-2014  Avencall
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

require_once(dwho_file::joinpath(XIVO_PATH_OBJECT,'application','abstract','datastorage','sql.inc'));

class xivo_application_xivo_object_stats_conf_sql extends xivo_application_abstract_datastorage_sql
{
	var $_table	= array(
				'stats_conf'		=> '',
				'stats_conf_queue'	=> '',
				'stats_conf_agent'	=> '',
				'stats_conf_xivouser'	=> '');

	function get($id)
	{
		$this->_dso->new_select($this->_table['stats_conf']);

		$this->_dso->where(array('id' => $id), $this->_table['stats_conf']);

		$r = $this->_dso->select_single();

		if(($this->_cnt = $this->_dso->select_count()) === 0)
			$r = false;

		return($r);
	}

	function get_all($disable=null,$order=null,$limit=null,$value=null,$xivouser=false)
	{
		$this->_dso->new_select($this->_table['stats_conf']);

		if($disable !== null)
			$this->_dso->andwhere(array('disable' => intval((bool) $disable)));

		if($xivouser !== false)
		{
			$this->_dso->innerjoin($this->_table['stats_conf_xivouser'],
					       $this->_table['stats_conf_xivouser'].'.stats_conf_id = '. $this->_table['stats_conf'].'.id',
					       null,
					       '');
			if (is_array($xivouser) === true)
				$this->_dso->joinwherein($this->_table['stats_conf_xivouser'],'stats_conf_id',$xivouser);
			elseif(is_numeric($xivouser) === true)
				$this->_dso->joinwhere($this->_table['stats_conf_xivouser'],array('user_id' => (int) $xivouser));
		}

		$this->_dso->new_order($order,null,$this->_table['stats_conf']);
		$this->_dso->limit($limit);

		$r = $this->_dso->select_all();

		$this->_dso->reset_group();
		$this->_dso->reset_order();
		$this->_dso->reset_limit();

		if(($this->_cnt = $this->_dso->select_distinct_count_singlecolumn(
							null,
							'id',
							$this->_table['stats_conf'])) === false
		|| isset($r[0]) === false)
			$r = false;

		return($r);
	}

	function search($value,$disable=null,$order=null,$limit=null)
	{
		$this->_cnt = 0;

		if(dwho_has_len($value) === false)
			return(false);

		$this->_dso->new_select($this->_table['stats_conf']);
		$this->_dso->wherecontain('name',$value);
		$this->_dso->orwherecontain('description',$value);

		if($this->_dso->aggregate_where() === false)
			return(false);

		if($disable !== null)
			$this->_dso->andwhere(array('disable' => intval((bool) $disable)));

		$this->_dso->new_order($order);
		$this->_dso->limit($limit);

		$r = $this->_dso->select_all();

		$this->_dso->reset_order();
		$this->_dso->reset_limit();

		if(($this->_cnt = $this->_dso->select_count()) === 0
		|| isset($r[0]) === false)
			$r = false;

		return($r);
	}

	function delete_all() {

		return($this->_dso->delete_all('stats_conf'));

	}

	function get_stats_conf_name_already_exist($name)
	{
		$this->_cnt = 0;

		if(dwho_has_len($name) === false)
			return(false);

		$this->_dso->new_select($this->_table['stats_conf']);
		$this->_dso->where(array('name' => $name));

		$r = true;
		if(($this->_cnt = $this->_dso->select_count()) > 0)
			$r = false;

		return($r);
	}
}

?>
