<?php
if (!defined('IN_CONTEXT')) die('access violation error!');

class ModAttachment extends Module {

	protected $_filters = array(
        'check_admin' => ''
    );
	
    public function admin_list() {
        $this->_layout = 'content';
        
        $mode =& ParamHolder::get('mode');
        $default_watermark_image = 'images/watermark.png';
        $site_param =& ParamHolder::get('sparam', array());
        $png =& ParamHolder::get('WATERMARK_PNG', array(), PS_FILES);
        
        if( isset($mode) && !empty($mode) )
        {
        	$o_param = new Parameter();
        	
        	switch( $mode ) {
        		case 'submit':
			        if( isset($png['name']) && !empty($png['name']) ) 
			        {
			        	$png['name'] = iconv("UTF-8", "gb2312", $png['name']);
			        	if( !move_uploaded_file($png['tmp_name'], ROOT.'/admin/images/'.$png['name']) ) {
			        		Notice::set('mod_attachment/msg', __('Watemark image upload failed!'));
		            		Content::redirect(Html::uriquery('mod_attachment', 'admin_list'));
			        	} else {
			        		$site_param['WATERMARK_PNG'] = 'images/'.$png['name'];
			        	}
			        } else {
			        	$site_param['WATERMARK_PNG'] = $default_watermark_image;
			        }
	        		break;
	        	case 'restore':
	        		$site_param['WATERMARK_PNG'] = $default_watermark_image;
	        		break;
	        }
	        
	        // exec sql
	    	foreach ($site_param as $key => $val) {
	    	    $param =& $o_param->find('`key`=?', array($key));
	    	    if ($param) {
	    	        $param->val = $val;
	    	        $param->save();
	    	    }
	    	}
	    	
	    	Content::redirect(Html::uriquery('mod_attachment', 'admin_list'));
        }
        
        $this->assign('default_watermark_image', $default_watermark_image);
    }
    
    public function watermark_preview() {
    	$this->_layout = 'blank';
    }
}
?>