<?php

require_once dirname(dirname(dirname(__FILE__))) . '/class/core/ActionBase.class.php';
require_once XOOPS_ROOT_PATH . '/class/xoopsblock.php';

class Xoonips_PolicyGroupAction extends Xoonips_ActionBase {

	protected function doInit(&$request, &$response) {

		//title
		$title = _AM_XOONIPS_POLICY_GROUP_TITLE;
		$description = _AM_XOONIPS_POLICY_GROUP_DESC;
		// breadcrumbs
		$breadcrumbs = array(
  		array(
    		'name' => _AM_XOONIPS_TITLE,
    		'url' => XOOPS_URL.'/modules/'.$this->dirname.'/admin/index.php',
  		),
  		array(
    		'name' => _AM_XOONIPS_POLICY_TITLE,
    		'url' => XOOPS_URL.'/modules/'.$this->dirname.'/admin/policy.php',
  		),
  		array(
    		'name' => $title,
  		),
		);

		// token ticket
		$token_ticket = $this->createToken( $this->modulePrefix('admin_policy_group') );

		// get configs
		$config_keys = array();

		$initvals_config_keys = array(
  			'group_item_number_limit' => array(
    			'title' => _AM_XOONIPS_LABEL_ITEM_NUMBER_LIMIT,
    			'desc' => _AM_XOONIPS_POLICY_GROUP_INITIAL_MAX_ITEM_DESC,
    			'type' => 'i',
  			),
  			'group_index_number_limit' => array(
   	 			'title' => _AM_XOONIPS_LABEL_INDEX_NUMBER_LIMIT,
    			'desc' => _AM_XOONIPS_POLICY_GROUP_INITIAL_MAX_INDEX_DESC,
    			'type' => 'i',
  			),
  			'group_item_storage_limit' => array(
    			'title' => _AM_XOONIPS_LABEL_ITEM_STORAGE_LIMIT,
    			'desc' => _AM_XOONIPS_POLICY_GROUP_INITIAL_MAX_DISK_DESC,
    			'type' => 'i',
  			),
		);

		foreach ($initvals_config_keys as $key => $value) {
  			$config_keys[$key] = $value['type'];
		}
		$config_values = Xoonips_Utils::xoonipsAdminGetConfigs($config_keys, 'e', $this->dirname, $this->trustDirname);

		// initial values
		$initil_values = array();
		foreach ($initvals_config_keys as $name => $value) {
  			$iv = array();
  			$iv['label'] = $value['title'];
  			$iv['desc'] = $value['desc'];
  			$iv['name'] = $name;
  			if ($name == 'group_item_storage_limit') {
    			$iv['value'] = $config_values[$name] / 1024 / 1024;
  			} else {
    			$iv['value'] = $config_values[$name];
  			}
  			$initial_values[] = $iv;
		}

		//get common viewdata
		$viewData = array();
		$viewData['token_ticket'] = $token_ticket;
		$viewData['initial_values'] = $initial_values;
		$viewData['breadcrumbs'] = $breadcrumbs;
		$viewData['title'] = $title;
		$viewData['description'] = $description;
		$viewData['dirname'] = $this->dirname;

		$response->setViewData($viewData);
		$response->setForward('init_success');
		
		return true;
	}

	protected function doUpdate(&$request, &$response) {

		// check token ticket
		if ( !$this->validateToken( $this->modulePrefix('admin_policy_group') ) ) {
			$response->setSystemError('Ticket error');
			return false;
		}
		
		// get parameters
		$post_keys = array(
		  	'group_item_number_limit' => array('i', false, true,),
		  	'group_index_number_limit' => array('i', false, true,),
		  	'group_item_storage_limit' => array('f', false, true,),
		);
		$post_vals = Xoonips_Utils::xoonipsAdminGetRequests('post', $post_keys);
		$post_vals['group_item_storage_limit'] = (int) ($post_vals['group_item_storage_limit'] * 1024 * 1024);
		

		// set config keys
		$config_keys = array();
		foreach ( $post_keys as $key => $attributes ) {
  			list( $data_type, $is_array, $required ) = $attributes;
  			$config_keys[$key] = $data_type; 				
		}

		// update db values
		foreach ( $config_keys as $key => $type ) {
  			Xoonips_Utils::xoonipsAdminSetConfig($key, $post_vals[$key], $type, $this->dirname, $this->trustDirname);
		}
		
		$viewData['url'] = XOOPS_URL.'/modules/'.$this->dirname.'/admin/policy_group.php';
		$viewData['redirect_msg'] = _AM_XOONIPS_MSG_DBUPDATED;
		
		$response->setViewData($viewData);
		$response->setForward('update_success');	
		return true;
	}
}
?>
