<?php

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

class Xoonips_SystemAmazonAction extends Xoonips_ActionBase {
	
	protected function doInit(&$request, &$response) {

		//title
		$title = _AM_XOONIPS_SYSTEM_AMAZON_TITLE;
		$description = _AM_XOONIPS_SYSTEM_AMAZON_DESC;
		// breadcrumbs
		$breadcrumbs = array(
		  array(
		    'name' => _AM_XOONIPS_TITLE,
		    'url' => XOOPS_URL.'/modules/'.$this->dirname.'/admin/index.php',
		  ),
		  array(
		    'name' => _AM_XOONIPS_SYSTEM_TITLE,
		    'url' => XOOPS_URL.'/modules/'.$this->dirname.'/admin/system.php',
		  ),
		  array(
		    'name' => $title,
		  ),
		);
		// token ticket		
		$token_ticket = $this->createToken( $this->modulePrefix('admin_system_amazon') );
		
		// get configs
		$config_keys = array(
		  'access_key' => 's',
		  'secret_access_key' => 's',
		);
		$config_values = Xoonips_Utils::xoonipsAdminGetConfigs($config_keys, 'e', $this->dirname, $this->trustDirname);
		
		// get common viewdata
		$viewData = array();
		
		$viewData['token_ticket'] = $token_ticket;
		$viewData['config_values'] = $config_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_system_amazon') ) ) {
		 	$response->setSystemError('Ticket error');
			return false;
		}
		
		// get requests
		$post_keys = array(
		  'access_key' => array('s', false, true,),
		  'secret_access_key' => array('s', false, true,),
		);
		$post_vals = Xoonips_Utils::xoonipsAdminGetRequests( 'post', $post_keys );
		
		// 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/system_amazon.php';
		$viewData['redirect_msg'] = _AM_XOONIPS_MSG_DBUPDATED;
		
		$response->setViewData($viewData);
		$response->setForward('update_success');
		return true;
	}
}

