<?php
/**
 * @version $Id: ImageEditForm.class.php 673 2011-12-16 00:26:41Z hodaka $
 * @package myuploader
 */

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

require_once XOOPS_ROOT_PATH . "/core/XCube_ActionForm.class.php";

class Myuploader_ImageEditForm extends XCube_ActionForm
{
    var $mOldFileName = null;
    var $_mIsNew = null;
    var $mFormFile = null;
    var $mCategory = null;
    var $mModuleGlobal = null;

    /**
     * @public
     */
    function getTokenName()
    {
        return "module.myuploader.ImageEditForm.TOKEN";
    }

    /**
     * @public
     */
    function prepare()
    {
        $this->mModuleGlobal =& Myuploader_Global::getInstance($this->mContext->mXoopsModule->get('dirname'));

        //
        // Set form properties
        //
        $this->mFormProperties['image_id'] = new XCube_IntProperty('image_id');
        $this->mFormProperties['image_title'] = new XCube_StringProperty('image_title');
        $this->mFormProperties['category_id'] = new XCube_IntProperty('category_id');
        $this->mFormProperties['image_description'] = new XCube_TextProperty('image_description');
        $this->mFormProperties['image_filename'] = new XCube_ImageFileProperty('image_filename');
        $this->mFormProperties['image_active'] = new XCube_BoolProperty('image_active');
        $this->mFormProperties['image_weight'] = new XCube_IntProperty('image_weight');
        $this->mFormProperties['dohtml'] = new XCube_BoolProperty('dohtml');
        $this->mFormProperties['doxcode'] = new XCube_BoolProperty('doxcode');
        $this->mFormProperties['doimage'] = new XCube_BoolProperty('doimage');
        $this->mFormProperties['dobr'] = new XCube_BoolProperty('dobr');
        $this->mFormProperties['resize_length'] = new XCube_IntProperty('resize_length');
        $this->mFormProperties['thumb_length'] = new XCube_IntProperty('thumb_length');

        //
        // Set field properties
        //
        if(!$this->_mIsNew){
            $this->mFieldProperties['image_id'] = new XCube_FieldProperty($this);
            $this->mFieldProperties['image_id']->setDependsByArray(array('required'));
            $this->mFieldProperties['image_id']->addMessage('required', _MD_MYUPLOADER_ERROR_REQUIRED, _MD_MYUPLOADER_LANG_IMAGE_ID);
        }
        $this->mFieldProperties['image_title'] = new XCube_FieldProperty($this);
        $this->mFieldProperties['image_title']->setDependsByArray(array('required','maxlength'));
        $this->mFieldProperties['image_title']->addMessage('required', _MD_MYUPLOADER_ERROR_REQUIRED, _MD_MYUPLOADER_LANG_IMAGE_TITLE, '100');
        $this->mFieldProperties['image_title']->addMessage('maxlength', _MD_MYUPLOADER_ERROR_MAXLENGTH, _MD_MYUPLOADER_LANG_IMAGE_TITLE, '100');
        $this->mFieldProperties['image_title']->addVar('maxlength', '100');

        $this->mFieldProperties['category_id'] = new XCube_FieldProperty($this);
        $this->mFieldProperties['category_id']->setDependsByArray(array('required'));
        $this->mFieldProperties['category_id']->addMessage('required', _MD_MYUPLOADER_ERROR_REQUIRED, _MD_MYUPLOADER_LANG_CATEGORY_ID);

        $this->mFieldProperties['image_description'] = new XCube_FieldProperty($this);
        $this->mFieldProperties['image_description']->setDependsByArray(array('required'));
        $this->mFieldProperties['image_description']->addMessage('required', _MD_MYUPLOADER_ERROR_REQUIRED, _MD_MYUPLOADER_LANG_IMAGE_DESCRIPTION);

        $this->mFieldProperties['image_filename'] = new XCube_FieldProperty($this);
        $this->mFieldProperties['image_filename']->setDependsByArray(array('extension'));
        $this->mFieldProperties['image_filename']->addVar('extension', 'jpg,png,gif');
    }

    /**
     * Check the permission of uploading.
     */
    function validateCategory_id()
    {
        $catid = $this->get('category_id');
        if(!empty($catid)) {
            if(!in_array($catid, $this->mModuleGlobal->getUploadable())) {
                $this->addErrorMessage(_MD_MYUPLOADER_ERROR_NO_PERM_CATEGORY);
            }
        }
    }

    /**
     * Check the permission of html.
     */
    function validateImage_dohtml()
    {
        if($this->get('dohtml')) {
            $catid = $this->get('category_id');
            if(!empty($catid)) {
                if(!in_array($catid, $this->mModuleGlobal->getHtmlAllowed())) {
                    $this->addErrorMessage(_MD_MYUPLOADER_ERROR_NO_PERM_HTML);
                }
            }
        }
    }

    function fetchImage_filename() {
        $this->mFormProperties['image_filename']->mValue = new XCube_FormImageFile('image_filename');
        $this->mFormProperties['image_filename']->mValue->fetch();
    }

    function validate()
    {
        parent::validate();

        $formFile = $this->get('image_filename');
        if (!$formFile->mUploadFileFlag && $this->_mIsNew ) {
            $this->addErrorMessage(XCube_Utils::formatString(_MD_MYUPLOADER_ERROR_REQUIRED, _MD_MYUPLOADER_LANG_IMAGE_FILENAME));
        } elseif($formFile->mUploadFileFlag) {
            // extension
            if(!in_array($formFile->getExtension(), array('jpg', 'png', 'gif'))) {
                $this->addErrorMessage(_MD_MYUPLOADER_ERROR_EXTENSION);
            }

            //
            // Imagefile width & height check.
            //
            if($formFile->getWidth() > $this->mCategory->get('cat_maxlength') || $formFile->getHeight() > $this->mCategory->get('cat_maxlength')) {
                $this->addErrorMessage(XCube_Utils::formatString(_MD_MYUPLOADER_ERROR_IMAGE_SIZE_OVER, $this->mCategory->get('cat_maxlength')));
            }

            //
            // Check file size
            //
            if($formFile->getFilesize() > $this->mCategory->get('cat_maxsize')*1024) {
                $this->addErrorMessage(XCube_Utils::formatString(_MD_MYUPLOADER_ERROR_FILE_SIZE_OVER, $this->mCategory->get('cat_maxsize')));
            }

            // resize length
            if(!$this->get('resize_length')) {
//            	$this->addErrorMessage(XCube_Utils::formatString(_MD_MYUPLOADER_ERROR_REQUIRED, _MD_MYUPLOADER_LANG_RESIZE_LENGTH));
                $this->set('resize_length', $this->mCategory->get('cat_resizelength'));
            } elseif($this->get('resize_length') > $this->mCategory->get('cat_resizelength')) {
                $this->addErrorMessage(XCube_Utils::formatString(_MD_MYUPLOADER_ERROR_MAIN_RESIZE_OVER, $this->mCategory->get('cat_resizelength')));
            }

            // thumb length
            if(!$this->get('thumb_length')) {
//                $this->addErrorMessage(XCube_Utils::formatString(_MD_MYUPLOADER_ERROR_REQUIRED, _MD_MYUPLOADER_LANG_THUMB_LENGTH));
                $this->set('thumb_length', $this->mCategory->get('cat_thumblength'));
            } elseif($this->get('thumb_length') > $this->mCategory->get('cat_thumblength')) {
                $this->addErrorMessage(XCube_Utils::formatString(_MD_MYUPLOADER_ERROR_THUMB_SIZE_OVER, $this->mCategory->get('cat_thumblength')));
            }
        }
    }

    /**
     * @public
     */
    function load(&$obj)
    {
        $this->set('image_id', $obj->get('image_id'));
        $this->set('image_title', $obj->get('image_title'));
        $this->set('category_id', $obj->get('category_id'));
        $this->set('image_description', $obj->get('image_description'));
        $this->set('image_filename', $obj->get('image_filename'));
        $this->set('image_bodyname', $obj->get('image_bodyname'));
        $this->set('image_extension', $obj->get('image_extension'));
        $this->set('image_mimetype', $obj->get('image_mimetype'));
        $this->set('image_width', $obj->get('image_width'));
        $this->set('image_height', $obj->get('image_height'));
        $this->set('image_thumbwidth', $obj->get('image_thumbwidth'));
        $this->set('image_thumbheight', $obj->get('image_thumbheight'));
        $this->set('image_active', $obj->get('image_active'));
        $this->set('image_weight', $obj->get('image_weight'));
        $this->set('dohtml', $obj->get('dohtml'));
        $this->set('doxcode', $obj->get('doxcode'));
        $this->set('doimage', $obj->get('doimage'));
        $this->set('dobr', $obj->get('dobr'));
        $this->set('image_uid', $obj->get('image_uid'));
        $this->set('image_created', $obj->get('image_created'));
        $this->set('image_modified', $obj->get('image_modified'));

        $this->_mIsNew = $obj->isNew();
        $this->mOldFileName = $obj->get('image_filename');

        $this->set('resize_length', $this->mCategory->get('cat_resizelength'));
        $this->set('thumb_length', $this->mCategory->get('cat_thumblength'));
    }

    /**
     * @public
     */
    function update(&$obj)
    {
        $obj->set('image_id', $this->get('image_id'));
        $obj->set('image_title', $this->get('image_title'));
        $obj->set('image_description', $this->get('image_description'));
        $obj->set('category_id', $this->get('category_id'));
        $obj->set('image_active', $this->get('image_active'));
        $obj->set('image_weight', $this->get('image_weight'));
        $obj->set('dohtml', $this->get('dohtml'));
        $obj->set('doxcode', $this->get('doxcode'));
        $obj->set('doimage', $this->get('doimage'));
        $obj->set('dobr', $this->get('dobr'));
        $obj->set('image_modified', time());

        $this->mFormFile = $this->get('image_filename');

        if ($this->mFormFile->mUploadFileFlag) {
            // rename into the old file name if exists
            if($this->mOldFileName != null) {
                // Get a body name of the old file.
                $match = array();
                if (preg_match("/(.+)\.\w+$/", $this->mOldFileName, $match)) {
                    $this->mFormFile->setBodyName($match[1]);
                }
            } else {
                $this->mFormFile->setRandomToBodyName('img');
                $filename = $this->mFormFile->getBodyName();
                $this->mFormFile->setBodyName(substr($filename, 0, 24));
            }

            $obj->set('image_filename', $this->mFormFile->getFileName());
            $obj->set('image_bodyname', $this->mFormFile->getBodyName());
            $obj->set('image_extension', $this->mFormFile->getExtension());
            $obj->set('image_mimetype', $this->mFormFile->getContentType());
        }
    }
}

?>