2013-08-16 09:59:11 +00:00
|
|
|
|
<?php
|
2013-08-22 07:59:09 +00:00
|
|
|
|
namespace Helpers;
|
2013-08-16 09:59:11 +00:00
|
|
|
|
|
|
|
|
|
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)) {
|
2013-12-27 05:54:27 +00:00
|
|
|
|
$html.= ''.$content.''."\r\n";
|
2013-08-16 09:59:11 +00:00
|
|
|
|
}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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-13 08:42:34 +00:00
|
|
|
|
static function setUserStorage($user)
|
|
|
|
|
{
|
|
|
|
|
$auth = \Zend_Auth::getInstance();
|
|
|
|
|
if($auth->hasIdentity())
|
|
|
|
|
{
|
|
|
|
|
if(get_class($user) == 'stdClass')
|
|
|
|
|
{
|
|
|
|
|
$auth->getStorage()->write($user);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-16 09:59:11 +00:00
|
|
|
|
static function Dump($data,$exit = true){
|
2013-08-22 07:59:09 +00:00
|
|
|
|
echo "<pre>"."\r\n";
|
2013-08-16 09:59:11 +00:00
|
|
|
|
var_dump($data);
|
2013-08-22 07:59:09 +00:00
|
|
|
|
echo "\r\n";
|
2013-08-16 09:59:11 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-13 08:42:34 +00:00
|
|
|
|
static function JsonOutput(\Zend_Controller_Action $ctl,$data,$json_numeric_check = true)
|
|
|
|
|
{
|
|
|
|
|
if($json_numeric_check === true)
|
|
|
|
|
{
|
|
|
|
|
$body = json_encode($data,JSON_NUMERIC_CHECK);
|
|
|
|
|
}else{
|
|
|
|
|
$body = json_encode($data);
|
|
|
|
|
}
|
|
|
|
|
$ctl ->getResponse()
|
|
|
|
|
->setHeader('Content-Type', 'application/json')
|
|
|
|
|
->appendBody($body);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-16 09:59:11 +00:00
|
|
|
|
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'];
|
|
|
|
|
}
|
2013-09-05 01:52:27 +00:00
|
|
|
|
|
2013-10-09 03:22:27 +00:00
|
|
|
|
static function isXmlHttpRequest($ctl = NULL)
|
2013-09-05 01:52:27 +00:00
|
|
|
|
{
|
2013-10-09 03:22:27 +00:00
|
|
|
|
$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;
|
|
|
|
|
}
|
2013-09-05 01:52:27 +00:00
|
|
|
|
}
|
2013-12-13 08:42:34 +00:00
|
|
|
|
|
|
|
|
|
|
2013-08-16 09:59:11 +00:00
|
|
|
|
}
|