<?php
/**
 * @version $Id: ImageEditAction.class.php 679 2012-08-20 12:41:35Z hodaka $
 * @package myuploader
 */

if (!defined('XOOPS_ROOT_PATH')) exit();

require_once dirname(dirname(__FILE__))."/class/AbstractEditAction.class.php";
require_once dirname(dirname(__FILE__)) . "/class/ImageResizer.class.php";

class Myuploader_ImageEditAction extends Myuploader_AbstractEditAction
{
	var $mCategory = null;
    var $mCategoryId = null;

    function isMemberOnly() {
    	return true;
    }

    /**
	 * @protected
	 */
	function _getId()
	{
		return xoops_getrequest('image_id');
	}

	/**
	 * @protected
	 */
	function &_getHandler()
	{
		$handler =& $this->mAsset->getObject('handler', 'image');
		return $handler;
	}

    function _setupObject() {
        parent::_setupObject();
        
        if($this->mObject->isNew()) {
            $catid = xoops_getrequest('category_id') != null ? xoops_getrequest('category_id') : 0;
            $this->mObject->set('category_id', $catid);
            $this->mObject->set('dohtml', in_array($this->mObject->get('category_id'), $this->mModuleGlobal->getHtmlAllowed()));
            // doimage <- allow_html flag
            $this->mObject->set('doimage', $this->mObject->get('dohtml'));
        }
    }

	/**
	 * @protected
	 */
	function _setupActionForm()
	{
		// $this->mActionForm = new Myuploader_ImageEditForm();
		$this->mActionForm =& $this->mAsset->getObject('form', 'image', false, 'edit');
		$this->mActionForm->prepare();
	}

    function hasPermission()
    {
        if(!(in_array($this->mObject->get('category_id'), $this->mModuleGlobal->getUploadable()))) {
        	return false;
        }
        
	$allCategories =& $this->mModuleGlobal->getAllCategories();
        $this->mActionForm->mCategory =& $allCategories[$this->mObject->get('category_id')];
        return true;
    }

    function _doExecute() {
    	if(false == $this->_store()) {
            return MYUPLOADER_FRAME_VIEW_INPUT;
    	}

        return parent::_doExecute();
    }

    function _store() {
    	if (!$this->mActionForm->mFormFile->mUploadFileFlag) {
            return true;
        }

        $resizer = new ImageResizer($this->mModuleGlobal->mTargetDir);
        if(!$resizer) {
            $this->mActionForm->mErrorMessages = array_merge($this->mActionForm->mErrorMessages, $resizer->mErrors);
            return false;
        }        

        if(!$this->mActionForm->mFormFile->saveAs($resizer->mTmpDir)) {
        	$this->mActionForm->addErrorMessage(_MD_MYUPLOADER_ERROR_MOVE_IMAGE);
            return false;
        }

        // initialize an image resource
        $source = $resizer->mTmpDir.'/'.$this->mActionForm->mFormFile->getFileName();
        if(!$resizer->load($source)) {
            $this->mActionForm->mErrorMessages = array_merge($this->mActionForm->mErrorMessages, $resizer->mErrors);
            @unlink($source);
            return false;
        }

        //
        // main image
        //
        $resizer->setTarget($this->mActionForm->mFormFile->getFileName());
        if(!$resizer->resize($this->mActionForm->get('resize_length'), $this->mActionForm->get('resize_length'))) {
            $this->mActionForm->mErrorMessages = array_merge($this->mActionForm->mErrorMessages, $resizer->mErrors);
            @unlink($source);
            return false;
        }

        // delete an old image if exists
        if($this->mActionForm->mOldFileName != null) {
            @unlink($this->mModuleGlobal->mTargetDir.'/'.$this->mActionForm->mOldFileName);
        }
        // save
        if(!$resizer->save()) {
            $this->mActionForm->mErrorMessages = array_merge($this->mActionForm->mErrorMessages, $resizer->mErrors);
            @unlink($source);
            return false;
        }
        
        // update obj
        $this->mObject->set('image_width', $resizer->mResizeInfo[0]);
        $this->mObject->set('image_height', $resizer->mResizeInfo[1]);

        //
        // thumbnail
        //
        $thumb = $this->mActionForm->mFormFile->getBodyName();
        $thumb .= '_thumb.'.$this->mActionForm->mFormFile->getExtension();
        $resizer->setTarget($thumb);

        // delete an old thumb if exists
        if(is_file($resizer->mTarget)) {
            @unlink($resizer->mTarget);
        }
        
        if(!$resizer->resize($this->mActionForm->get('thumb_length'), $this->mActionForm->get('thumb_length'))) {
            $this->mActionForm->mErrorMessages = array_merge($this->mActionForm->mErrorMessages, $resizer->mErrors);
            @unlink($source);
            return false;
        }

        // save
        if(!$resizer->save()) {
            $this->mActionForm->mErrorMessages = array_merge($this->mActionForm->mErrorMessages, $resizer->mErrors);
            @unlink($source);
            return false;
        }

        // update obj
        $this->mObject->set('image_thumbwidth', $resizer->mResizeInfo[0]);
        $this->mObject->set('image_thumbheight', $resizer->mResizeInfo[1]);
        @unlink($source);
        return true;
    }

	/**
	 * @public
	 */
	function executeViewInput(&$render)
	{
        $render->setTemplateName($this->mAsset->mDirname."_main_image_edit.html");
		$render->setAttribute('actionForm', $this->mActionForm);
		#cubson::lazy_load('image', $this->mObject);
        
        // set global info
        $this->mObjectHandler->loadGlobalInfo($this->mObject);
		$render->setAttribute('object', $this->mObject);
        $category_handler =& $this->mAsset->getObject('handler', 'category');
        $render->setAttribute('categories', $category_handler->getChildTreeArray(0, 'upload'));
        $render->setAttribute('actionName', $this->mModule->mActionName);
        $render->setAttribute('moduleConfig', $this->mModuleGlobal->mModuleConfig);

	}

	/**
	 * @public
	 */
	function executeViewSuccess(&$render)
	{
		$this->mRoot->mController->executeForward("./index.php?op=ImageView&image_id=".$this->mObject->get('image_id'));
	}

	/**
	 * @public
	 */
	function executeViewError(&$render)
	{
        $this->mRoot->mController->executeRedirect("./index.php?op=ImageList&category_id=".$this->mActionForm->get('category_id'), 1, _MD_MYUPLOADER_ERROR_DBUPDATE_FAILED);
	}

	/**
	 * @public
	 */
	function executeViewCancel(&$render)
	{
		$this->mRoot->mController->executeForward("./index.php?op=ImageView&image_id=".$this->mObject->get('image_id'));
	}
}

?>