<?php
//
//	file: includes/acp/acp_attach_cats.php
//	author: ptirhiik
//	begin: 28/02/2006
//	version: 1.6.0 - 10/06/2006
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//
//	based on : attachment_mod copyright (c) 2002 Meik Sievertsen (acydburn)
//

if ( !defined('IN_PHPBB') )
{
	die('Hack attempt');
}

$cp_title = 'Manage_categories';
$cp_title_explain = 'Manage_categories_explain';

class attach_cats_auto_form extends auto_form
{
	function init()
	{
		parent::init();
		$this->search_imagick();
		$this->check_thumbnails();
	}

	function validate()
	{
		global $warning, $warning_msg;
		if ( $warning )
		{
			return;
		}
		parent::validate();
	}

	function display()
	{
		global $template, $config, $user;
		global $warning, $warning_msg;

		if ( $warning )
		{
			$template->assign_block_vars('warning', array(
				'WARNING_TITLE' => $user->lang('Information'),
				'WARNING_MSG' => $user->lang($warning_msg),
			));
		}
		parent::display();
	}

	function update_table(&$form_fields)
	{
		global $db, $config;
		global $attach_config;

		if ( empty($form_fields) )
		{
			return;
		}
		$keys = array();
		$db->sql_stack_reset();
		foreach ( $form_fields as $name => $value )
		{
			$keys[] = $db->sql_escape_string($name);
			$fields = array(
				'config_name' => $name,
				'config_value' => $value,
			);
			$db->sql_stack_statement($fields);
		}
		$sql = 'DELETE FROM ' . ATT_CONFIG_TABLE . '
					WHERE config_name IN(\'' . implode('\', \'', $keys) . '\')';
		$db->sql_query($sql, false, __LINE__, __FILE__);
		$db->sql_stack_insert(ATT_CONFIG_TABLE, false, __LINE__, __FILE__);

		// recache config
		$attach_config->read(true);

		// send achievement message
		message_return('Attach_config_updated', $this->return_msg, $config->url($this->requester, $this->return_parms, true));
	}

	function search_imagick()
	{
		if ( !_button('search_imagick') || !isset($this->fields['img_imagick']) )
		{
			return;
		}

		// raz
		$imagick = '';

		// search windows
		if ( eregi('WIN', PHP_OS) )
		{
			$path = 'c:/imagemagick/convert.exe';
			if ( @file_exists(phpbb_realpath($path)) )
			{
				$imagick = $path;
			}
		}

		// search linux
		else
		{
			$retval = @exec('whereis convert');
			$paths = explode(' ', $retval);
			if ( is_array($paths) )
			{
				$count_paths = count($paths);
				for ( $i = 0; $i < $count_paths; $i++ )
				{
					$path = basename($paths[$i]);
					if ( $path == 'convert' )
					{
						$imagick = $paths[$i];
						break;
					}
				}
			}
		}
		$this->fields['img_imagick']->value = trim($imagick);
		if ( empty($this->fields['img_imagick']->value) )
		{
			_warning('Imagick_not_found');
		}
	}

	function check_thumbnails()
	{
		global $warning, $warning_msg;
		global $attach_config;

		if ( !_button('check_settings') )
		{
			return;
		}
		$file_handler = new file_handler($attach_config->data);

		// thumbnail creation not activated
		if ( !$this->fields['img_create_thumbnail']->value )
		{
			_warning('No_thumbnail_choosen');
			_warning('Test_settings_successful');
			_warning('Click_submit_to_save');
		}
		// check if thumbnails are supported
		else if ( (trim($attach_config->data['img_imagick']) != '') && !@extension_loaded('gd') )
		{
			$this->fields['img_create_thumbnail']->value = 0;
			_warning('Thumbnails_not_available');
		}
		// check if the directory is writable
		else if ( !$file_handler->check_settings(THUMB_DIR) )
		{
			_warning($file_handler->error());
		}
		// all is fine
		else
		{
			_warning('Test_settings_successful');
			_warning('Click_submit_to_save');
		}
	}
}

// start process
$cat_menus = $cp_panels->get_menu($ctx_menus[$ctx_id], $view_user);
$catm_id = $cp_panels->get_menu_id(POST_CAT_URL, $cat_menus);

$categories = array(
	'images' => array('cat_id' => IMAGE_CAT, 'panel_title' => 'Settings_cat_images'),
	'stream' => array('cat_id' => STREAM_CAT, 'panel_title' => 'Settings_cat_streams'),
	'swf' => array('cat_id' => SWF_CAT, 'panel_title' => 'Settings_cat_flash'),
	'denied' => array('cat_id' => DENIED_CAT, 'panel_title' => 'Settings_cat_denied'),
	'none' => array('cat_id' => NONE_CAT, 'panel_title' => 'Settings_cat_none'),
);
$dft_cat = _first_key($categories);

// buid a sub-menu
$template->assign_block_vars('ctx', array(
	'L_MENU' => $user->lang($cp_panels->data[ $sub_menus[$subm_id] ]['panel_name']),
	'L_OPTION' => $user->lang($cp_panels->data[ $ctx_menus[$ctx_id] ]['panel_name']),
));
foreach ( $cat_menus as $cat_shortcut => $cat_panel )
{
	if ( !$cp_panels->data[$cat_panel]['panel_hidden'] )
	{
		$template->assign_block_vars('ctx.option', array(
			'U_OPTION' => $config->url($requester, $cp_parms + array('mode' => $menu_id, 'sub' => $subm_id, 'ctx' => $ctx_id, POST_CAT_URL => $cat_shortcut), true),
			'L_OPTION' => $user->lang($cp_panels->data[$cat_panel]['panel_name']),
		));
		$template->set_switch('ctx.option.select', $cat_shortcut == $catm_id);
	}
}
$template->assign_block_vars('cp_menus', array('BOX' => $template->include_file('cp_menus_ctx_box.tpl', 'ctx')));

// prepare process
$parms = array(
	'mode' => $menu_id,
	'sub' => $subm_id == $menu_id ? '' : $subm_id,
	'ctx' => $ctx_id == $subm_id ? '' : $ctx_id,
	POST_CAT_URL => $catm_id == $dft_cat ? '' : $catm_id,
);
_hide(POST_CAT_URL, $catm_id);
$cp_panel_name = $cp_panels->data[ $cat_menus[$catm_id] ]['panel_name'];
$navigation->add($cp_panels->data[ $cat_menus[$catm_id] ]['panel_name'], '', $cp_requester, $cp_parms + $parms);

// get the selected category id
$cat_id = $categories[$catm_id]['cat_id'];

// get a list of group affected
$sql = 'SELECT g.ext_group_id, g.ext_group_name, e.ext_id, e.extension
			FROM ' . ATT_EXT_GROUPS_TABLE . ' g
				LEFT JOIN ' . ATT_EXTENSIONS_TABLE . ' e
					ON e.ext_group_id = g.ext_group_id
			WHERE g.cat_id = ' . intval($cat_id) . '
			ORDER BY g.ext_group_name, e.extension';
$result = $db->sql_query($sql, false, __LINE__, __FILE__);
$groups = array();
$extensions = array();
while ( $row = $db->sql_fetchrow($result) )
{
	if ( !isset($groups[ intval($row['ext_group_id']) ]) )
	{
		$groups[ intval($row['ext_group_id']) ] = $row['ext_group_name'];
	}
	$extensions[ intval($row['ext_id']) ] = $row['extension'];
}
$db->sql_freeresult($result);
if ( empty($groups) )
{
	$groups[0] = $user->lang('None');
}

// get the fields
$fields = array(
	'affectation_title' => array('type' => 'sub_title', 'legend' => 'Assigned_group'),
	'groups_affected' => array('type' => 'list', 'over' => true, 'over.center' => true, 'options' => $groups, 'options.no_translate' => true, 'html' => ' size="5" style="width: 250px;"'),
	'extensions' => array('type' => 'list', 'combined' => true, 'options' => $extensions, 'options.no_translate' => true, 'html' => ' size="5" style="width: 250px;"'),
) + (empty($fields) ? $cp_panels->get_fields($cat_menus[$catm_id]) : $fields);

// instantiate the form
$form = new attach_cats_auto_form($attach_config->data, $fields, $cp_requester, 'Click_return_attach_config', $cp_parms + $parms);
$form->process();
$template->set_switch('form');
$template->set_switch('free_size');
$template->set_filenames(array('body' => 'cp_generic.tpl'));

// add the buttons
if ( $catm_id == 'images' )
{
	$buttons = array(
		'check_settings' => array('txt' => 'Test_settings', 'img' => 'cmd_check', 'key' => 'cmd_check'),
	);
	display_buttons($buttons);
}

?>