<?php
/**
 * @version $Id: myuploader_block.php 679 2012-08-20 12:41:35Z hodaka $
 * @package myuploader
 * @author hodaka <hodaka@kuri3.net>
 */

class Myuploader_BlockProcedure extends Legacy_BlockProcedure
{
    var $mRoot = null;
    var $mAssetManager = null;
    var $mModuleGlobal = null;
    var $mOptions = array();

    function Myuploader_BlockProcedure(&$block) {
        parent::Legacy_BlockProcedure($block);
    }

    function prepare() {
        $this->mRoot = XCube_Root::getSingleton();
        $this->mOptions = explode('|', $this->_mBlock->get('options'));
        XCube_DelegateUtils::call('Module.myuploader.Event.GetAssetManager', new XCube_Ref($this->mAssetManager), $this->_mBlock->get('dirname'));
        XCube_DelegateUtils::call('Module.myuploader.Event.GetModuleGlobal', new XCube_Ref($this->mModuleGlobal), $this->_mBlock->get('dirname'));

        return true;
    }

}

class Myuploader_LatestImages extends Myuploader_BlockProcedure
{
    function prepare() {
        parent::prepare();
        $this->mOptions['numperpage'] = isset($this->mOptions[1])? intval($this->mOptions[1]) : 5;
        $this->mOptions['template'] = empty($this->mOptions[2])? $this->_mBlock->get('dirname').'_block_latest_images.html' : trim($this->mOptions[2]);
        return true;
    }

    function execute() {
        $render = $this->getRenderTarget();
//        $render->setTemplateName($this->_mBlock->get('template'));
        $render->setTemplateName($this->mOptions['template']);  // for altsys-compatible.

        $render->setAttribute('images', $this->_getObjects());
        $render->setAttribute('moduleUrl', sprintf('%s/modules/%s', XOOPS_URL, htmlspecialchars($this->_mBlock->get('dirname'), ENT_QUOTES)));

        $renderSystem = $this->mRoot->getRenderSystem($this->getRenderSystemName());
        $renderSystem->renderBlock($render);
    }

    function getOptionForm() {
        $this->prepare();

        // read a language file
        $mytrustdirname = basename(dirname(dirname( __FILE__ )));
        $langmanpath = XOOPS_TRUST_PATH.'/libs/altsys/class/D3LanguageManager.class.php' ;
        if(!file_exists($langmanpath)) die('install the latest altsys');
        require_once($langmanpath) ;
        $langman = D3LanguageManager::getInstance() ;
        $langman->read('admin.php', $this->_mBlock->get('dirname'), $mytrustdirname, false);

        $form =<<<EOD
<input type="hidden" name="options[0]" value="%s" />
<label for="numperpage">%s</label>&nbsp;:
<input type="text" size="5" name="options[1]" id="numperpage" value="%d" /><br />
<label for="template">%s</label>&nbsp;:
<input type="text" size="50" name="options[2]" id="template" value="%s" />
EOD;
        return sprintf($form, $this->_mBlock->get('dirname'), _AD_MYUPLOADER_LANG_NUM_PER_PAGE, $this->mOptions['numperpage'], _AD_MYUPLOADER_LANG_TEMPLATE, $this->mOptions['template']);
    }

    function _getObjects() {
        $handler =& $this->mAssetManager->getObject('handler', 'image');
        $criteria = new criteriaCompo(new criteria('image_active', 1));
        $viewable_cat = $this->mModuleGlobal->getViewable();
        if(count($viewable_cat) > 0)
        $criteria->add(new criteria('category_id', '('.implode(',', $this->mModuleGlobal->getViewable()).')', 'IN'));
        $criteria->setSort('image_created');
        $criteria->setOrder('DESC');
        $criteria->setStart(0);
        $criteria->setLimit(intval($this->mOptions['numperpage']));
        $objs =& $handler->getObjects($criteria);
        return $objs;
    }

}

class Myuploader_CategoryList extends Myuploader_BlockProcedure
{
    function prepare() {
        parent::prepare();
        $this->mOptions['includechildren'] = isset($this->mOptions[1])? intval($this->mOptions[1]) : 0;
        $this->mOptions['template'] = empty($this->mOptions[2])? $this->_mBlock->get('dirname').'_block_category_list.html' : trim($this->mOptions[2]);
        return true;
    }

    function execute() {
        $root = XCube_Root::getSingleton();

        $render = $this->getRenderTarget();
//        $render->setTemplateName($this->_mBlock->get('template'));    // hodaka modified for altsys-compatible
        $render->setTemplateName($this->mOptions['template']);

        $cat_handler =& $this->mAssetManager->getObject('handler', 'category');
        $render->setAttribute('categoryTree', $cat_handler->renderCategoryTree($this->mOptions['includechildren']));
        $renderSystem = $root->getRenderSystem($this->getRenderSystemName());
        $renderSystem->renderBlock($render);
    }

    function getOptionForm() {
        $this->prepare();

        // read a language file
        $mytrustdirname = basename(dirname(dirname( __FILE__ )));
        $langmanpath = XOOPS_TRUST_PATH.'/libs/altsys/class/D3LanguageManager.class.php' ;
        if(!file_exists($langmanpath)) die('install the latest altsys');
        require_once($langmanpath) ;
        $langman = D3LanguageManager::getInstance() ;
        $langman->read('admin.php', $this->_mBlock->get('dirname'), $mytrustdirname, false);

        $form =<<<EOD
<input type="hidden" name="options[0]" value="%s" />
%s&nbsp;:<input type="radio" name="options[1]" value="1"%s id="radio_yes" /><label for="radio_yes">%s</label><input type="radio" name="options[1]" value="0"%s id="radio_no" /><label for="radio_no">%s</label><br />
<label for="template">%s</label>&nbsp;:
<input type="text" size="50" name="options[2]" id="template" value="%s" />
EOD;
        return sprintf($form, $this->_mBlock->get('dirname'), _AD_MYUPLOADER_LANG_INCLUDE_CHILDREN, !empty($this->mOptions['includechildren'])? ' checked="checked"' : '', _YES, empty($this->mOptions['includechildren'])? ' checked="checked"' : '', _NO, _AD_MYUPLOADER_LANG_TEMPLATE, $this->mOptions['template']);
    }
}

/**
 * @brief altsys compatible use
 */
/*function CategoryList_edit($options) {
    $form =<<<EOD
<input type="hidden" name="options[0]" value="%s" />
%s&nbsp;:<input type="radio" name="options[1]" value="1"%s id="radio_yes" /><label for="radio_yes">%s</label><input type="radio" name="options[1]" value="0"%s id="radio_no" /><label for="radio_no">%s</label><br />
<label for="template">%s</label>&nbsp;:
<input type="text" size="50" name="options[2]" id="template" value="%s" />
EOD;
    $dirname = $options[0];
    if(empty($dirname) || preg_match('/[^0-9A-Za-z_-]/', $dirname)) {
        exit('Invalid dirname in the block function.');
    }
    $children_yes = !empty($options[1])? ' checked="checked"' : '';
    $children_no = empty($options[1])? ' checked="checked"' : '';
    $template = empty($options[2])? $dirname.'_block_category_list.html' : trim($options[2]);
    return sprintf($form, $dirname, _MB_MYUPLOADER_LANG_INCLUDE_CHILDREN, $children_yes, _YES, $children_no, _NO, _MB_MYUPLOADER_LANG_TEMPLATE, $template);

}

function LatestImages_edit($options) {
    $form =<<<EOD
<input type="hidden" name="options[0]" value="%s" />
<label for="numperpage">%s</label>&nbsp;:
<input type="text" size="5" name="options[1]" id="numperpage" value="%d" /><br />
<label for="template">%s</label>&nbsp;:
<input type="text" size="50" name="options[2]" id="template" value="%s" />
EOD;
    $dirname = $options[0];
    if(empty($dirname) || preg_match('/[^0-9A-Za-z_-]/', $dirname)) {
        exit('Invalid dirname in the block function.');
    }
    $num = !empty($options[1])? intval($options[1]) : 5;
    $template = empty($options[2])? $dirname.'_block_latest_images.html' : trim($options[2]);
    return sprintf($form, $dirname, _MB_MYUPLOADER_LANG_NUM_PER_PAGE, $num, _MB_MYUPLOADER_LANG_TEMPLATE, $template);
}
*/
?>