<?php
/**
 * The control file of block module of ZDOO.
 *
 * @copyright   Copyright 2009-2018 青岛易软天创网络科技有限公司(QingDao Nature Easy Soft Network Technology Co,LTD, www.cnezsoft.com)
 * @license     ZPL (http://zpl.pub/page/zplv12.html)
 * @author      Tingting Dai <daitingting@xirangit.com>
 * @package     block
 * @version     $Id$
 * @link        http://www.zdoo.com
 */
class block extends control
{
    /**
     * Block Index Page.
     *
     * @access public
     * @return void
     */
    public function index()
    {
        $lang = $this->get->lang;
        $this->app->setClientLang($lang);
        $this->app->loadLang('common', 'team');
        $this->app->loadLang('block');

        $mode = strtolower($this->get->mode);
        if($mode == 'getblocklist')
        {
            echo $this->block->getAvailableBlocks();
        }
        elseif($mode == 'getblockform')
        {
            $code = strtolower($this->get->blockid);
            $func = 'get' . ucfirst($code) . 'Params';
            echo $this->block->$func();
        }
        elseif($mode == 'getblockdata')
        {
            $code = strtolower($this->get->blockid);
            $func = 'print' . ucfirst($code) . 'Block';
            $this->$func();
        }
    }

    /**
     * Block Admin Page.
     *
     * @param  int    $id
     * @param  string $type == block field in mysql
     * @access public
     * @return void
     */
    public function admin($id = 0, $type = '')
    {
        $this->app->loadLang('block', 'sys');

        if($_POST)
        {
            $this->block->save($id, 'system', 'team');
            if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::geterror()));
            return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => $this->server->http_referer));
        }

        $block = $this->block->getByID($id, 'team');
        $type  = $type ? $type : ($block ? $block->block : '');

        $blocks = json_decode($this->block->getAvailableBlocks(), true);
        $this->view->blocks  = array_merge(array(''), $blocks);

        $title = $id == 0 ? $this->lang->block->createBlock : $this->lang->block->editBlock;

        $this->view->title      = $title;
        $this->view->params     = $type ? json_decode($this->block->{'get' . ucfirst($type) . 'Params'}(), true) : array();
        $this->view->type       = $type;
        $this->view->block      = $block;
        $this->view->id         = $id;
        $this->view->modalWidth = 384;
        $this->display();
    }

    /**
     * Sort block.
     *
     * @param  string    $orders
     * @access public
     * @return void
     */
    public function sort($orders)
    {
        $this->locate($this->createLink('sys.block', 'sort', "orders=$orders&app=team"));
    }

    /**
     * Resize block
     *
     * @param  int    $id
     * @param  string $type
     * @param  string $data
     * @access public
     * @return void
     */
    public function resize($id, $type, $data)
    {
        $this->locate($this->createLink('sys.block', 'resize', "id=$id&type=$type&data=$data"));
    }

    /**
     * Delete block.
     *
     * @param  int    $id
     * @access public
     * @return void
     */
    public function delete($id)
    {
        $this->locate($this->createLink('sys.block', 'delete', "id=$id"));
    }

    /**
     * Print blog block.
     *
     * @access public
     * @return void
     */
    public function printBlogBlock()
    {
        $this->lang->blog = new stdclass();
        $this->app->loadLang('blog', 'team');

        $this->processParams();

        $blogs = $this->dao->select('*')->from(TABLE_ARTICLE)
            ->where('type')->eq('blog')
            ->orderBy('createdDate desc')
            ->limit($this->params->num)
            ->fetchAll('id');
        $blogs = $this->loadModel('article')->process($blogs);

        $this->view->blogs = $blogs;
        $this->view->users = $this->loadModel('user')->getPairs();

        $this->display();
    }

    /**
     * Print thread block.
     *
     * @access public
     * @return void
     */
    public function printThreadBlock()
    {
        $this->lang->thread = new stdclass();
        $this->app->loadLang('thread', 'team');

        $this->processParams();

        $threads = $this->dao->select('*')->from(TABLE_THREAD)
            ->beginIF(isset($this->params->type) and $this->params->type == 'stick')
            ->where('stick')->in('1,2')
            ->fi()
            ->orderBy('createdDate desc')
            ->limit($this->params->num)
            ->fetchAll('id');

        $this->loadModel('thread', 'team')->setRealNames($threads);
        $threads = $this->thread->process($threads);

        $this->view->threads = $threads;

        $this->display();
    }

    /**
     * Process params.
     *
     * @access public
     * @return void
     */
    public function processParams()
    {
        $params = $this->get->param;
        $this->params = json_decode(base64_decode($params));

        $this->view->sso  = base64_decode($this->get->sso);
        $this->view->code = $this->get->blockid;
    }
}
