<?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/>.
#

dwho_file::required(array(XIVO_PATH_OBJECT,'application','xivo.inc'),true);

class xivo_application_xivo_object_provdplugin extends xivo_application_xivo_object_abstract
{
	protected $_commonconf 		= false;
	protected $_provdplugin		= false;
	protected $_conf			= array();

	public function __construct(&$xobj)
	{
		parent::__construct($xobj);

		if(($this->_commonconf = &$this->_xobj->get_module('commonconf')) === false
		|| ($this->_provdplugin = &$this->_xobj->get_module('provdplugin')) === false)
			return(false);

		return(true);
	}

	public function get($id)
	{
		return($this->_provdplugin->get($id));
	}

	public function get_plugin_list($search='',$limit=null)
	{
		$this->_cnt = 0;

		if (($list_installed = $this->_provdplugin->get_plugin_installed($search)) === false)
			$list_installed = array();

		if (($list_installable = $this->_provdplugin->get_plugin_installable($search)) === false)
			$list_installable = array();

		$list_old_pni = array_diff_key($list_installed, $list_installable);
		$list_installable = array_merge($list_installable,$list_old_pni);

		ksort($list_installable);

		$list = array();
		foreach ($list_installable as $k => &$v)
		{
			if (isset($list_installed[$k]) === true)
			{
				$v['version'] = $list_installed[$k]['version'];
				$v['type'] = 'installed';
			}
			array_push($list, $v);
		}

		$this->_cnt = $total = count($list);

		$rs = $list;
		if ($limit !== null)
		{
			if (($nb = ($limit[0]+$limit[1])) < $total)
				$total = $nb;

			$rs = array();
			for($i=$limit[0];$i<$total;$i++)
				array_push($rs, $list[$i]);
		}
		$list = array_values($rs);

		return($list);
	}
}

?>
