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

$appextenfeatures = &$ipbx->get_application('extenfeatures');

$appfeatures = &$ipbx->get_apprealstatic('features');

$info = array();
$info['extenfeatures'] = $appextenfeatures->get_all_by_context();

$appgeneralfeatures = &$appfeatures->get_module('general');
$info['generalfeatures'] = $appgeneralfeatures->get_all_by_category();

$appfeaturemap = &$appfeatures->get_module('featuremap');
$info['featuremap'] = $appfeaturemap->get_all_by_category();

$element = array();
$element['extenfeatures'] = $appextenfeatures->get_elements();
$element['generalfeatures'] = $appgeneralfeatures->get_elements();
$element['featuremap'] = $appfeaturemap->get_elements();

$error = array();
$error['extenfeatures'] = array();
$error['generalfeatures'] = array();
$error['featuremap'] = array();

$musiconhold = &$ipbx->get_module('musiconhold');

$fm_save = null;

if(isset($_QR['fm_send']) === true)
{
	if(dwho_issa('extenfeatures',$_QR) === true)
	{
		if($info['extenfeatures'] === false)
			$info['extenfeatures'] = array();

		$rs = array();
		$element_extenfeatures = array_keys($element['extenfeatures']);

		foreach ($element_extenfeatures as $key)
		{
			if (dwho_issa($key, $_QR['extenfeatures']) === false)
				continue;

			$data_exten = $_QR['extenfeatures'][$key];

			if(isset($data_exten['exten']) === true)
				$exten = $data_exten['exten'];
			else
				$exten = '';

			$rs['commented'] = isset($data_exten['enable']) === false;
			$rs['typeval'] = $key;
			$rs['exten'] = $exten;

			if(isset($info['extenfeatures'][$key]) === false)
				$info['extenfeatures'][$key] = $rs;
			else
			{
				$info['extenfeatures'][$key]['exten'] = $rs['exten'];
				$info['extenfeatures'][$key]['commented'] = $rs['commented'];
			}

			if($appextenfeatures->set($rs) === false
			|| $appextenfeatures->save() === false)
			{
				$info['extenfeatures'][$key]['exten'] = '';
				$error['extenfeatures'][] = $key;
			}

			if(empty($error['extenfeatures']) === false)
				$fm_save = false;
			else if($fm_save !== false)
				$fm_save = true;
		}
	}

	if(dwho_issa('generalfeatures',$_QR) === true
	&& ($rs = $appgeneralfeatures->set_save_all($_QR['generalfeatures'],false)) !== false)
	{
		$info['generalfeatures'] = $rs['result'];
		$error['generalfeatures'] = $rs['error'];

		if(empty($rs['error']) === false)
			$fm_save = false;
		else if($fm_save !== false)
			$fm_save = true;
	}

	if(dwho_issa('featuremap',$_QR) === true
	&& ($rs = $appfeaturemap->set_save_all($_QR['featuremap'],false)) !== false)
	{
		$info['featuremap'] = $rs['result'];
		$error['featuremap'] = $rs['error'];

		if(empty($rs['error']) === false)
			$fm_save = false;
		else if($fm_save !== false)
			$fm_save = true;
	}
}

$_TPL->set_var('fm_save',$fm_save);
$_TPL->set_var('error',$error);
$_TPL->set_var('extenfeatures',$info['extenfeatures']);
$_TPL->set_var('generalfeatures',$info['generalfeatures']);
$_TPL->set_var('featuremap',$info['featuremap']);
$_TPL->set_var('sound_list',$appgeneralfeatures->get_sound());
$_TPL->set_var('musicclass', $musiconhold->get_all_by_category());
$_TPL->set_var('element',$element);

$dhtml = &$_TPL->get_module('dhtml');
$dhtml->set_js('js/service/ipbx/'.$ipbx->get_name().'/extenfeatures.js');

$menu = &$_TPL->get_module('menu');
$menu->set_top('top/user/'.$_USR->get_info('meta'));
$menu->set_left('left/service/ipbx/'.$ipbx->get_name());

$_TPL->set_bloc('main','service/ipbx/'.$ipbx->get_name().'/pbx_services/extenfeatures');
$_TPL->set_struct('service/ipbx/'.$ipbx->get_name());
$_TPL->display('index');

?>
