<?php
/**
 * @version $Id: Image.class.php 680 2012-08-22 03:12:37Z hodaka $
 * @package myuploader
 */

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

class Myuploader_ImageObject extends XoopsSimpleObject
{
    var $mCategory = null;
    var $mUploadable = null;
    var $mHtmlAllowed = null;
    var $mSubscriber = null;

    /**
     * @public
     */
    function Myuploader_ImageObject()
    {
        $root = XCube_Root::getSingleton();
        if(is_object($root->mContext->mXoopsUser)) {
            $uid = $root->mContext->mXoopsUser->get('uid');
        } else {
            $uid = 0;
        }
        $this->initVar('image_id', XOBJ_DTYPE_INT, '', false);
        $this->initVar('image_title', XOBJ_DTYPE_STRING, '', false, 100);
        $this->initVar('category_id', XOBJ_DTYPE_INT, '0', false);
        $this->initVar('image_description', XOBJ_DTYPE_TEXT, '', false);
        $this->initVar('image_filename', XOBJ_DTYPE_STRING, '', false, 50);
        $this->initVar('image_bodyname', XOBJ_DTYPE_STRING, '', false, 50);
        $this->initVar('image_extension', XOBJ_DTYPE_STRING, '', false, 10);
        $this->initVar('image_mimetype', XOBJ_DTYPE_STRING, '', false, 30);
        $this->initVar('image_width', XOBJ_DTYPE_INT, '0', false);
        $this->initVar('image_height', XOBJ_DTYPE_INT, '0', false);
        $this->initVar('image_thumbwidth', XOBJ_DTYPE_INT, '0', false);
        $this->initVar('image_thumbheight', XOBJ_DTYPE_INT, '0', false);
        $this->initVar('image_active', XOBJ_DTYPE_BOOL, '1', false);
        $this->initVar('image_weight', XOBJ_DTYPE_INT, '0', false);
        $this->initVar('dohtml', XOBJ_DTYPE_INT, 0, false);
        $this->initVar('doxcode', XOBJ_DTYPE_INT, 1, false);
        $this->initVar('doimage', XOBJ_DTYPE_INT, 0, false);
        $this->initVar('dobr', XOBJ_DTYPE_INT, 1, false);
        $this->initVar('image_uid', XOBJ_DTYPE_INT, $uid, false);
        $this->initVar('image_created', XOBJ_DTYPE_INT, time(), false);
        $this->initVar('image_modified', XOBJ_DTYPE_INT, time(), false);
    }

}

class Myuploader_ImageHandler extends XoopsObjectGenericHandler
{
    var $mTable = '{dirname}_image';
    var $mPrimary = 'image_id';
    var $mClass = 'Myuploader_ImageObject';
    var $mDirname = null;
    var $mAssetManager = null;
    var $mModuleGlobal = null;

    /**
     * Myuploader_ImageHandler
     *
     * @param   XoopsDatabase  &$db
     * @param   string  $dirname
     *
     * @return  void
     *
     * @public
     */
    function Myuploader_ImageHandler(&$db, $dirname)
    {
        $this->mTable = str_replace('{dirname}',$dirname,$this->mTable);
        parent::XoopsObjectGenericHandler($db);

        $this->mDirname = $dirname;
        $this->mAssetManager =& Myuploader_AssetManager::getInstance($dirname);
        XCube_DelegateUtils::call('Module.myuploader.Event.GetModuleGlobal', new XCube_Ref($this->mModuleGlobal), $dirname);
    }

    function loadGlobalInfo(&$obj) {
        $allcategories =& $this->mModuleGlobal->getAllCategories();
        $obj->mCategory = $allcategories[$obj->get('category_id')];
        $obj->mUploadable = in_array($obj->get('category_id'), $this->mModuleGlobal->getUploadable());
        $obj->mHtmlAllowed = in_array($obj->get('category_id'), $this->mModuleGlobal->getHtmlAllowed());
        $member_handler =& xoops_gethandler('member');
        $obj->mSubscriber =& $member_handler->getUser($obj->get('image_uid'));
    }

    function deleteByCategory($catid, $force=false) {
        if(!empty($catid)) {
            $criteria = new criteriaCompo(new criteria('category_id', intval($catid)));
            $images =& $this->getObjects($criteria);
            foreach($images as $image) {
                if(!$this->delete($image, $force)) {
                    return false;
                }
            }
            return true;
        }

        return false;
    }

    function delete(&$obj, $force=false) {
        $this->_unlink($obj);
        return parent::delete($obj, $force);
    }

    function _unlink(&$obj) {
        @unlink($this->mModuleGlobal->mTargetDir.'/'.$obj->get('image_filename'));
        // thumbnail
//        $thumb = preg_replace("/(.+)\.([^\.]+)$/", '$1'.'_thumb.'.'$2', $this->get('image_filename'));
        @unlink($this->mModuleGlobal->mTargetDir.'/'.$obj->get('image_bodyname').'_thumb.'.$obj->get('image_extension'));
    }

/*    function &getObjects($criteria=null, $id_as_key=false)
    {
        $ret = array();
        $sql = "SELECT * FROM `" . $this->mTable . '`';

        if($criteria !== null && is_a($criteria, 'CriteriaElement')) {
//            $where = $this->_makeCriteria4sql($criteria);
            $where = $criteria->render();
            if (trim($where)) {
                $sql .= " WHERE " . $where;
            }
            $sorts = array();
            foreach ($criteria->getSorts() as $sort) {
                $sorts[] = '`' . $sort['sort'] . '` ' . $sort['order'];
            }
            if ($criteria->getSort() != '') {
                $sql .= " ORDER BY " . implode(',', $sorts);
            }
            $limit = $criteria->getLimit();
            $start = $criteria->getStart();
        }
        else {
            $limit = 0;
            $start = 0;
        }

        $result = $this->db->query($sql, $limit, $start);
        if (!$result) {
            return $ret;
        }
        while($row = $this->db->fetchArray($result)) {
            $obj = new $this->mClass();
            $obj->assignVars($row);
            $obj->unsetNew();
            if ($id_as_key) {
                $ret[$obj->get($this->mPrimary)] =& $obj;
            }
            else {
                $ret[]=&$obj;
            }
            unset($obj);
        }

        return $ret;
    }*/

}

?>