<?php
//
//	file: includes/class_attach_admin_groups.php
//	author: ptirhiik
//	begin: 05/03/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('Hacking attempt');
	exit;
}

class attach_quota_settings
{
	var $group_id;
	var $data;
	var $done;

	function attach_quota_settings($group_id)
	{
		$this->group_id = intval($group_id);
		$this->data = array();
		$this->done = false;
	}

	function process($mode)
	{
		global $template, $db, $config, $user;
		global $attach_config;

		if ( !($group_id = intval($this->group_id)) )
		{
			return;
		}

		if ( !_button('group_update') && _button('edit') )
		{
			// Show the contents
			$sql = 'SELECT quota_limit_id, quota_type
						FROM ' . ATT_QUOTA_TABLE . '
						WHERE group_id = ' . intval($group_id);
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			$pm_quota = $upload_quota = 0;
			$found = false;
			while ( $row = $db->sql_fetchrow($result) )
			{
				$found = true;
				switch ( intval($row['quota_type']) )
				{
					case QUOTA_UPLOAD_LIMIT:
						$upload_quota = intval($row['quota_limit_id']);
						break;

					case QUOTA_PM_LIMIT:
						$pm_quota = intval($row['quota_limit_id']);
						break;
				}
			}
			$db->sql_freeresult($result);

			// Set Default Quota Limit
			if ( !$found )
			{
				$upload_quota = intval($attach_config->data['default_upload_quota']);
				$pm_quota = intval($attach_config->data['default_pm_quota']);
			}

			$template->assign_vars(array(
				'S_SELECT_UPLOAD_QUOTA' => $this->quota_list('group_upload_quota', $upload_quota),
				'S_SELECT_PM_QUOTA' => $this->quota_list('group_pm_quota', $pm_quota),
				'L_UPLOAD_QUOTA' => $user->lang('Upload_quota'),
				'L_PM_QUOTA' => $user->lang('Pm_quota'),
			));
		}

		if ( _button('group_update') && _button('group_delete') )
		{
			$this->validate(QUOTA_UPLOAD_LIMIT, 0);
			$this->validate(QUOTA_PM_LIMIT, 0);
		}
		else if ( _button('group_update') )
		{
			// Get the contents
			$upload_quota = get_var('group_upload_quota', 0);
			$pm_quota = get_var('group_pm_quota', 0);

			$this->validate(QUOTA_UPLOAD_LIMIT, $upload_quota);
			$this->validate(QUOTA_PM_LIMIT, $pm_quota);
		}
	}

	// Set/Change Quotas
	function validate($quota_type, $quota_limit_id = 0)
	{
		global $db;

		// delete
		if ( !intval($quota_limit_id) )
		{
			$sql = 'DELETE FROM ' . ATT_QUOTA_TABLE . '
						WHERE group_id = ' . intval($this->group_id) . '
							AND quota_type = ' . intval($quota_type);
			$db->sql_query($sql, false, __LINE__, __FILE__);
			return;
		}

		// Check if the group is already entered
		$sql = 'SELECT group_id
					FROM ' . ATT_QUOTA_TABLE . '
					WHERE group_id = ' . intval($this->group_id) . '
						AND quota_type = ' . intval($quota_type);
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		$found = $db->sql_numrows($result);
		$db->sql_freeresult($result);
		if ( $found )
		{
			$sql = 'UPDATE ' . ATT_QUOTA_TABLE . '
						SET quota_limit_id = ' . intval($quota_limit_id) . '
						WHERE group_id = ' . intval($this->group_id) . '
							AND quota_type = ' . intval($quota_type);
			$db->sql_query($sql, false, __LINE__, __FILE__);
		}
		else
		{
			$fields = array(
				'group_id' => intval($this->group_id),
				'quota_type' => intval($quota_type),
				'quota_limit_id' => intval($quota_limit_id),
			);
			$db->sql_statement($fields);
			$sql = 'INSERT INTO ' . ATT_QUOTA_TABLE . '
						(' . $db->sql_fields . ') VALUES(' . $db->sql_values . ')';
			$db->sql_query($sql, false, __LINE__, __FILE__);
		}
	}

	// select quota limit
	function quota_list($select_name, $default_quota=0)
	{
		global $db, $user;

		if ( !$this->done )
		{
			$this->done = true;
			$this->data = array(0 => '');
			$sql = 'SELECT quota_limit_id, quota_desc
						FROM ' . ATT_QUOTA_LIMITS_TABLE . '
						ORDER BY quota_limit ASC';
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			while ( $row = $db->sql_fetchrow($result) )
			{
				$this->data[ intval($row['quota_limit_id']) ] = $row['quota_desc'];
			}
			$db->sql_freeresult($result);
		}

		$this->data[0] = $user->lang('Not_assigned');
		$res = '';
		$line = '<option value="%s"%s>%s</option>';
		foreach ( $this->data as $key => $desc )
		{
			$res .= sprintf($line, $key, $key == $default_quota ? ' selected="selected"' : '', $desc);
		}
		return '<select name="' . $select_name . '">' . $res . '</select>';
	}
}

?>