westdc-zf1/application/module/Helpers/View.php

175 lines
4.0 KiB
PHP
Raw Normal View History

<?php
namespace Helpers;
class View extends \Zend_Controller_Plugin_Abstract
{
private $db; //传入PDO对象.
private $product = 0; //产品环境
function __construct($db = NULL)
{
if(empty($db))
{
$this->db = \Zend_Registry::get('db');
}else{
$this->db = $db;
}
}
static function addPaginator($data,$ctl,$limit = 10)
{
$request = $ctl->getRequest();
$page = $request->getParam('page');
$paginator = \Zend_Paginator::factory($data);
$paginator->setCurrentPageNumber($page);
$paginator->setItemCountPerPage($limit);
$paginator->setView($ctl->view);
\Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml');
$ctl->view->paginator = $paginator;
}
static function Msg($type,$content,$url=''){
$html = '<div class="alert '.$type.'">'."\r\n";
$html.= '<a data-dismiss="alert" class="close">×</a>'."\r\n";
$html.= $content."\r\n";
$html.= '</div>'."\r\n";
if(!empty($url))
{
if($url == -1){
$html.= '<script language="javascript">setTimeout("window.history.back(-1);",3000);</script>'."\r\n";
}else{
$html.= '<script language="javascript">setTimeout("self.location=\''.$url.'\'",3000);</script>'."\r\n";
}
}
return $html;
}
static function Error($content,$type='',$url=''){
if(empty($type))
{
$AlertType = "alert-error";
}else{
$AlertType = $type;
}
$html = '<div class="alert alert-block fade in '.$AlertType.'" id="Alert-error-box">'."\r\n";
$html.= '<a class="close" data-dismiss="alert" href="#">×</a>'."\r\n";
if(!is_array($content)) {
$html.= '<h4 class="alert-heading">'.$content.'</h4>'."\r\n";
}else{
$html.= '<ul>'."\r\n";
foreach($content as $v) {
$html.='<li>'.$v.'</li>'."\r\n";
}
$html.= '</ul>'."\r\n";
}
$html.= '</div>'."\r\n";
return $html;
}
static function User($param = NULL){
$auth = \Zend_Auth::getInstance();
if($auth->hasIdentity())
{
if(!empty($param))
{
$user = $auth->getIdentity();
return $user->$param;
}else{
$user = $auth->getIdentity();
return $user;
}
}else{
return false;
}
}
static function setUserStorage($user)
{
$auth = \Zend_Auth::getInstance();
if($auth->hasIdentity())
{
if(get_class($user) == 'stdClass')
{
$auth->getStorage()->write($user);
return true;
}
}
return false;
}
static function Dump($data,$exit = true){
echo "<pre>"."\r\n";
var_dump($data);
echo "\r\n";
echo "</pre>";
if($exit)
{
exit();
}
}
static function Post(\Zend_Controller_Action $ctl,$msg,$url=""){
if(empty($msg))
{
return false;
}
if(!is_array($msg))
{
$msg = array('content'=>$msg,'url'=>$url);
}
$helper = new \Zend_Controller_Action_HelperBroker($ctl);
$helper->viewRenderer->setNoRender();
echo $ctl->view->partial('post-message.phtml', $msg);
return true;
}
static function HttpError($ctl,$code = 404){
$ctl->getResponse()->setHttpResponseCode($code);
$helper = new \Zend_Controller_Action_HelperBroker($ctl);
$helper->layout->setLayout('layout');
$helper->viewRenderer->setNoRender();
echo $ctl->view->partial('error/404.phtml');
return true;
}
static function getHostLink()
{
$protocol = "http";
if(strpos(strtolower($_SERVER['SERVER_PROTOCOL']),"https"))
{
$protocol = "https";
}
return $protocol."://".$_SERVER['SERVER_NAME'];
}
static function isXmlHttpRequest($ctl = NULL)
{
$request = new \Zend_Controller_Request_Http();
if($request->isXmlHttpRequest())
{
if(!empty($ctl))
{
$helper = new \Zend_Controller_Action_HelperBroker($ctl);
$helper->layout->disableLayout();
$helper->viewRenderer->setNoRender();
}
return true;
}else{
return false;
}
}
static function isUuid($uuid)
{
if(!preg_match("/^[0-9A-Za-z]{8}-[0-9A-Za-z]{4}-[0-9A-Za-z]{4}-[0-9A-Za-z]{4}-[0-9A-Za-z]{12}$/",$uuid))
{
return false;
}else{
return true;
}
}
}