<?php
/**
 * The control file of notify of ZDOO.
 *
 * @copyright   Copyright 2009-2020 青岛易软天创网络科技有限公司(QingDao Nature Easy Soft Network Technology Co,LTD, www.cnezsoft.com)
 * @license     ZPL (http://zpl.pub/page/zplv12.html)
 * @author      Gang Liu <liugang@easycorp.ltd>
 * @package     notify
 * @version     $Id$
 * @link        http://www.zdoo.com
 */
class notify extends control
{
    /**
     * Index
     *
     * @access public
     * @return void
     */
    public function index()
    {
        foreach($this->lang->notify->typeList as $type => $typeName)
        {
            if(isset($this->config->notify->typeLink[$type]))
            {
                list($moduleName, $methodName) = explode('|', $this->config->notify->typeLink[$type]);
                if(common::hasPriv($moduleName, $methodName)) $this->locate($this->createLink($moduleName, $methodName));
            }
        }

        if(common::hasPriv('notify', 'setting')) $this->locate($this->createLink('notify', 'setting'));
    }

    /**
     * Setting
     *
     * @access public
     * @return void
     */
    public function setting()
    {
        if($_POST)
        {
            $data = fixer::input('post')->get();
            $data->notifySetting = json_encode($data->notifySetting);
            $this->loadModel('setting')->setItem('system.notify.setting', $data->notifySetting);
            die(js::reload('parent'));
        }

        $this->loadModel('webhook');
        $this->loadModel('action');

        $this->view->title      = $this->lang->notify->setting;
        $this->view->position[] = $this->lang->notify->common;
        $this->view->position[] = $this->lang->notify->setting;

        $this->view->objectTypes   = $this->notify->getObjectTypes();
        $this->view->objectActions = $this->notify->getObjectActions();
        $this->display();
    }

    /**
     * Ajax get message.
     *
     * @access public
     * @return void
     */
    public function ajaxGetMessage($windowBlur = false)
    {
        $waitMessages = $this->message->getMessages('wait');
        $todos = $this->message->getNoticeTodos();
        if(empty($waitMessages) and empty($todos)) die();

        $messages = '';
        $newline  = $windowBlur ? "\n" : '<br />';
        $idList   = array();
        foreach($waitMessages as $message)
        {
            $messages .= $message->data . $newline;
            $idList[]  = $message->id;
        }
        $this->dao->update(TABLE_NOTIFY)->set('status')->eq('sent')->set('sendTime')->eq(helper::now())->where('id')->in($idList)->exec();

        foreach($todos as $todo) $messages .= $todo->data . $newline;
        if($windowBlur) $messages = strip_tags($messages);

        if($windowBlur)
        {
            echo json_encode(array('message' => $messages));
        }
        else
        {
            echo <<<EOT
<div class='alert alert-info with-icon alert-dismissable' style='width:380px; position:fixed; bottom:25px; right:15px; z-index: 9999;'>
   <i class='icon icon-mail'>  </i>
   <div class='content'>{$messages}</div>
   <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
 </div>
EOT;
        }

        $this->dao->delete()->from(TABLE_NOTIFY)->where('objectType')->eq('message')->andWhere('status')->ne('wait')->exec();
    }

    /**
     * Check for additional.
     * @account public
     * @return void
     */
    public function additional()
    {
        $this->loadModel('todo')->notCreateTodo();
        echo "ok\n";
    }
}
