Action.php 4.04 KB
<?php
namespace Hood;

use Yaf\Controller_Abstract;
use Hood\Helper\View as hoodView;
use Hood\Session;
use Yaf;

class Action extends Controller_Abstract
{
    private $_headlink;

    private $_headscript;

    private $_headTitle;

    private $_headmeta;

    private $_qdebug;

    private $_qdebugMicrotime;

    /**
     * Meta
     * @return Q_View_Header_Meta
     */
    public function _headMeta()
    {
        if (empty($this->_headmeta)) {
            $this->_headmeta = new hoodView\Meta();
            $this->getView()->assign("_headmeta", $this->_headmeta);
        }
        return $this->_headmeta;
    }

    /**
     * Script
     * @return Q_View_Header_Script
     */
    public function _headScript()
    {
        if (empty($this->_headscript)) {
            $this->_headscript = new hoodView\Script();
            $this->getView()->assign("_headScript", $this->_headscript);
        }
        return $this->_headscript;
    }

    /**
     *
     * @return Q_View_Header_Link
     */
    public function _headLink()
    {
        if (empty($this->_headlink)) {
            $this->_headlink = new hoodView\Link();
            $this->getView()->assign("_headLink", $this->_headlink);
        }
        return $this->_headlink;
    }

    /**
     * Title
     * @return Q_View_Header_Title
     */
    public function _headTitle($title = null)
    {
        if (empty($this->_headTitle)) {
            $this->_headTitle = new hoodView\Title();
            $this->getView()->assign("_headTitle", $this->_headTitle);
        }
        return $this->_headTitle->headTitle($title);
    }

    /**
     * js 跳转 并 提示
     *
     * @param String $url
     * @param String $expression
     */
    protected function helpJsRedirect($message = '', $expression = "history.back()")
    {
        $this->helpLocation($message, $expression);
    }

    /**
     * 跳转
     *
     * @param String $url
     */
    protected function helpLocation($url)
    {
        header('Location: ' . $url);
    }

    /**
     * refresh跳转
     * @param $url
     * @param string $message
     * @param int $content
     */
    protected function helpRefresh($url, $message = '', $content = 0)
    {
        $html = '';
        if (!empty($message)) {
            header("content-type: text/html; charset=utf-8");
            $message = str_replace("\n", "\\n", $message);
            $html .= "<script language=\"javascript\">";
            $html .= "alert(\"{$message}\");";
            $html .= "</script>";
        }
        $html .= "<script language=\"javascript\">";
        $html .= "window.location.href='{$url}';";
        $html .= "</script>";
        echo $html;
    }

    /**
     * JSON输出
     * @param $code
     * @param string $message
     * @param int $data
     */
    protected function helpJsonResult($code, $message = null, $data = null)
    {
        header('Content-type: application/json');
        echo json_encode(array('code'=>$code, 'message'=>$message, 'data'=>$data));
        exit();
    }

    /**
     * JSON输出
     *
     * @param String $caption
     * @param Integer $code
     * @param mixed $content
     */
    protected function helpJson($data = null)
    {
        header('Content-type: application/json');
        echo json_encode($data);
        exit();
    }

    /**
     * JSONP Callback输出,用于远程调用
     *
     * @param String $caption
     * @param Integer $code
     * @param mixed $content
     */
    protected function helpJsonCallbackResult($callbackString, $code, $message = null, $data = null)
    {
        header('Content-type: application/json');
        echo $callbackString . "(";
        echo json_encode(array('code'=>$code, 'message'=>$message, 'data'=>$data));
        echo ")";
        exit();
    }

    private function _g($key, $default = '')
    {
        return $this->getRequest()->getQuery($key, $default);
    }

    /**
     * @param string $namespace
     * @return Core\Session\SessionNamespace
     */
    public function _session($namespace = 'session_default')
    {
        return Session::start($namespace);
    }
}