Blame view

yaf/Hood/Action.php 4.72 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
<?php
namespace Hood;

use Yaf\Controller_Abstract;
use Hood\Helper\View as hoodView;
use Yaf;
use Hood\Validator as hoodValidator;

class Action extends Controller_Abstract
{
    private $_viewLink = array();

    private $_viewScript = array();

    private $_headTitle;

    private $_headmeta;

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


    /**
     * Script
     * @return \Hood\Helper\View\Script
     */
    public function _viewScript($scriptName = '_headScript')
    {
        if (!isset($this->_viewScript[$scriptName])) {
            $this->_viewScript[$scriptName] = new hoodView\Script();
            $this->getView()->assign($scriptName, $this->_viewScript[$scriptName]);
        }
        return $this->_viewScript[$scriptName];
    }

    /**
     *
     * @return \Hood\Helper\View\Link
     */
    public function _viewLink($linkName = '_headLink')
    {
        if (!isset($this->_viewLink[$linkName])) {
            $this->_viewLink[$linkName] = new hoodView\Link();
            $this->getView()->assign($linkName, $this->_viewLink[$linkName]);
        }
        return $this->_viewLink[$linkName];
    }

    /**
     * Title
     * @return \Hood\Helper\View\Title
     */
    public function _headTitle($title)
    {
        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 = '', $script = "history.back();")
    {
        $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 .= $script;
        $html .= "</script>";
        die($html);
    }

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

    /**
     * refresh跳转
     * @param $url
     * @param string $message
     */
    protected function helpRefresh($url, $message = '')
    {
        $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 null $message
     * @param null $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 array $data
     */
    protected function helpJson(array $data)
    {
        header('Content-type: application/json');
        echo json_encode($data);
        exit();
    }

    /**
     * JSONP Callback输出,用于远程调用
     * @param $callbackString
     * @param $code
     * @param null $message
     * @param null $data
     */
    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();
    }


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

    /**
     * 数据校验
     * @param array $data
     * @param array $rules
     * @param array $messagesAttribute
     * @return Helper\Validation\Validator
     */
    public function validator(array $data, array $rules, array $messagesAttribute = array())
    {
        return hoodValidator::make($data, $rules, $messagesAttribute);
    }
}