저자: Nick Sagona, III (nick@popphp.org)
예제 #1
0
 /**
  * Finalize the request and send the response.
  *
  * @param  int   $code
  * @param  array $headers
  * @throws \Pop\Mvc\Exception
  * @return void
  */
 public function send($code = 200, array $headers = null)
 {
     if (null === $this->view) {
         throw new Exception('The view object is not defined.');
     }
     if (!$this->view instanceof View) {
         throw new Exception('The view object is not an instance of Pop\\Mvc\\View.');
     }
     if (null !== $this->project->logger()) {
         $this->project->log("Response [" . $code . "]", time());
     }
     $this->response->setCode($code);
     if (null !== $headers) {
         foreach ($headers as $name => $value) {
             $this->response->setHeader($name, $value);
         }
     }
     // Trigger any dispatch events, then send the response
     if (null !== $this->project->getEventManager()->get('dispatch')) {
         $this->project->log('[Event] Dispatch', time(), \Pop\Log\Logger::NOTICE);
     }
     $this->project->getEventManager()->trigger('dispatch', array('controller' => $this));
     $this->response->setBody($this->view->render(true));
     if (null !== $this->project->getEventManager()->get('dispatch.send')) {
         $this->project->log('[Event] Dispatch Send', time(), \Pop\Log\Logger::NOTICE);
     }
     $this->project->getEventManager()->trigger('dispatch.send', array('controller' => $this));
     $this->response->send();
 }
 /**
  * Prepare view method
  *
  * @param  string $template
  * @param  array  $data
  * @return void
  */
 public function prepareView($template = null, array $data = array())
 {
     $site = \Phire\Table\Sites::getSite();
     if (null !== $template) {
         $template = $this->getCustomView($template, $site);
     }
     $sess = \Pop\Web\Session::getInstance();
     $config = \Phire\Table\Config::getSystemConfig();
     $i18n = \Phire\Table\Config::getI18n();
     $this->live = (bool) $config->live;
     $jsVars = null;
     $this->view = View::factory($template, $data);
     $this->view->set('base_path', $site->base_path)->set('content_path', CONTENT_PATH);
     // Check for an override Phire theme for the header/footer
     if (file_exists($site->document_root . $site->base_path . CONTENT_PATH . '/extensions/themes/phire/header.phtml') && file_exists($site->document_root . $site->base_path . CONTENT_PATH . '/extensions/themes/phire/footer.phtml')) {
         $this->view->set('phireHeader', $site->document_root . $site->base_path . CONTENT_PATH . '/extensions/themes/phire/header.phtml')->set('phireFooter', $site->document_root . $site->base_path . CONTENT_PATH . '/extensions/themes/phire/footer.phtml');
         // Else, just use the default header/footer
     } else {
         $this->view->set('phireHeader', $site->document_root . $site->base_path . APP_PATH . '/vendor/Phire/view/phire/header.phtml')->set('phireFooter', $site->document_root . $site->base_path . APP_PATH . '/vendor/Phire/view/phire/footer.phtml');
     }
     if (isset($this->view->assets)) {
         $jsVars = '?lang=' . $config->default_language;
     }
     if (isset($sess->user)) {
         if (isset($this->sess->user->last_action)) {
             $this->sess->user->last_action = date('Y-m-d H:i:s');
         }
         // Set the timeout warning, giving a 30 second buffer to act
         if (isset($this->view->assets)) {
             if (isset($this->view->acl) && $this->view->acl->getType()->session_expiration > 0 && $this->view->acl->getType()->timeout_warning) {
                 $exp = $this->view->acl->getType()->session_expiration * 60 - 30;
                 $uri = $site->base_path . (strtolower($this->view->acl->getType()->type) != 'user' ? '/' . strtolower($this->view->acl->getType()->type) : APP_URI);
                 $jsVars .= '&_exp=' . $exp . '&_base=' . urlencode($uri);
             }
         }
         $this->view->set('user', $sess->user)->set('role', \Phire\Table\UserRoles::getRole($sess->user->role_id))->set('globalAccess', $sess->user->global_access);
         if (isset($this->view->phireNav) && isset($this->view->acl) && $this->view->acl->hasRole($this->view->role->getName())) {
             $this->view->phireNav->setConfig(array('top' => array('node' => 'ul', 'id' => 'phire-nav')));
             $this->view->phireNav->setAcl($this->view->acl);
             $this->view->phireNav->setRole($this->view->role);
             $tree = $this->view->phireNav->getTree();
             // If the sub-children haven't been added yet
             if (isset($tree[0])) {
                 // And any user types to the main phire nav
                 $userTypes = \Phire\Table\UserTypes::findAll('id ASC');
                 if (isset($userTypes->rows)) {
                     foreach ($userTypes->rows as $type) {
                         $perm = 'index_' . $type->id;
                         if ($this->view->acl->isAuth('Phire\\Controller\\Phire\\User\\IndexController', 'index') && $this->view->acl->isAuth('Phire\\Controller\\Phire\\User\\IndexController', 'index_' . $type->id)) {
                             $perm = 'index';
                         }
                         $this->view->phireNav->addLeaf('Users', array('name' => ucwords(str_replace('-', ' ', $type->type)), 'href' => 'index/' . $type->id, 'acl' => array('resource' => 'Phire\\Controller\\Phire\\User\\IndexController', 'permission' => $perm)), 1);
                     }
                 }
                 // Set the language
                 $tree = $this->view->phireNav->getTree();
                 foreach ($tree as $key => $value) {
                     if (isset($value['name'])) {
                         $tree[$key]['name'] = $i18n->__($value['name']);
                         if (isset($value['children']) && count($value['children']) > 0) {
                             foreach ($value['children'] as $k => $v) {
                                 if ($v['name'] == 'Fields' && isset($tree[$key]['children'][$k]['children'][0]['name'])) {
                                     $tree[$key]['children'][$k]['children'][0]['name'] = $i18n->__($tree[$key]['children'][$k]['children'][0]['name']);
                                 }
                                 $tree[$key]['children'][$k]['name'] = $i18n->__($v['name']);
                             }
                         }
                     }
                 }
                 $this->view->phireNav->setTree($tree);
             }
             $this->view->phireNav->rebuild();
             $this->view->phireNav->nav()->setIndent('    ');
         }
     }
     if (isset($this->view->assets)) {
         $this->view->assets = str_replace('jax.3.2.0.min.js', 'jax.3.2.0.min.js' . $jsVars, $this->view->assets);
     }
     if (isset($sess->errors)) {
         $this->view->set('errors', $sess->errors);
     }
     // Set config object and system/site default data
     $this->view->set('i18n', $i18n)->set('system_title', $config->system_title)->set('system_email', $config->system_email)->set('site_title', $config->site_title)->set('base_path', $config->base_path)->set('separator', $config->separator)->set('default_language', $config->default_language)->set('datetime_format', $config->datetime_format);
 }
 /**
  * Error method
  *
  * @return void
  */
 public function error()
 {
     $install = new Model\Install(array('title' => $this->i18n->__('404 Error') . ' > ' . $this->i18n->__('Page Not Found')));
     $this->view = View::factory($this->viewPath . '/error.phtml', $install->getData());
     $this->view->set('i18n', $this->i18n);
     $this->send(404);
 }
예제 #4
0
    public function testRenderStringWithLoop()
    {
        $data = array('list' => array('Thing #1', 'Thing #2'));
        $template = <<<TMPL
    <ul>
[{list}]        <li>[{value}]</li>[{/list}]
    </ul>
TMPL;
        $v = View::factory($template, $data);
        $view = $v->render(true);
        $this->assertContains('<li>Thing #1</li>', $view);
        $this->assertContains('<li>Thing #2</li>', $view);
    }
예제 #5
0
 /**
  * Static method to output the results
  *
  * @param  string $url
  * @param  string $dir
  * @return void
  */
 public static function output($url, $dir)
 {
     // Create model object
     $data = array('title' => $url, 'urls' => self::$urls, 'errors' => self::$errors, 'depth' => self::$depth);
     // Create the HTML file
     $view = \Pop\Mvc\View::factory(__DIR__ . '/../../view/index.phtml', $data);
     copy(__DIR__ . '/../../data/styles.css', $dir . DIRECTORY_SEPARATOR . 'styles.css');
     copy(__DIR__ . '/../../data/scripts.js', $dir . DIRECTORY_SEPARATOR . 'scripts.js');
     file_put_contents($dir . DIRECTORY_SEPARATOR . 'index.html', $view->render(true));
     // Create the sitemap file
     $view = \Pop\Mvc\View::factory(__DIR__ . '/../../view/sitemap.phtml', $data);
     file_put_contents($dir . DIRECTORY_SEPARATOR . 'sitemap.xml', $view->render(true));
 }
예제 #6
0
파일: view2.php 프로젝트: nicksagona/PopPHP
require_once '../../bootstrap.php';
use Pop\Mvc\View;
try {
    $tmpl = <<<TMPL
<html>
<head>
    <title>[{title}]</title>
</head>
<body>
    <h1>[{body}]</h1>
    <p>
        This is a test template page.
    </p>
    <ul>
[{list}]        <li>[{value}]</li>[{/list}]
    </ul>
    <p>
        This is another list.
    </p>
    <ul>
[{pages}]        <li><a href="[{page_url}]">[{page_title}]</a></li>[{/pages}]
    </ul>
</body>
</html>
TMPL;
    $data = array('title' => 'Hello World', 'body' => 'This is the body (from template string)', 'list' => array('Thing #1', 'Something Else', 'Another Thing', 'Thing #2'), 'pages' => array(array('page_url' => 'http://www.google.com/', 'page_title' => 'Google'), array('page_url' => 'http://www.msn.com/', 'page_title' => 'MSN'), array('page_url' => 'http://www.yahoo.com/', 'page_title' => 'Yahoo!')));
    $view = View::factory($tmpl, $data);
    $view->render();
} catch (Exception $e) {
    echo $e->getMessage();
}
예제 #7
0
파일: view1.php 프로젝트: nicksagona/PopPHP
<?php

require_once '../../bootstrap.php';
use Pop\Mvc\View;
try {
    $data = array('title' => 'Hello World', 'body' => 'This is the body (from template file)', 'list' => array('Thing #1', 'Something Else', 'Another Thing', 'Thing #2'), 'pages' => array(array('page_url' => 'http://www.google.com/', 'page_title' => 'Google'), array('page_url' => 'http://www.msn.com/', 'page_title' => 'MSN'), array('page_url' => 'http://www.yahoo.com/', 'page_title' => 'Yahoo!')));
    $view = View::factory('template.phtml', $data);
    $view->render();
} catch (Exception $e) {
    echo $e->getMessage();
}
예제 #8
0
 /**
  * Event-based route error check
  *
  * @param  \Pop\Mvc\Router $router
  * @return void
  */
 public static function error($router)
 {
     $view = \Pop\Mvc\View::factory(__DIR__ . '/../../view/route.phtml', array('i18n' => \Phire\Table\Config::getI18n(), 'title' => 'Routing Error', 'msg' => '    <p>There was no controller assigned for this route.</p>'));
     $response = new \Pop\Http\Response(404);
     $response->setBody($view->render(true));
     $response->send();
 }
예제 #9
0
파일: index.php 프로젝트: nicksagona/PopPHP
 public function error()
 {
     $this->view = View::factory($this->viewPath . '/index.phtml', array('title' => 'Test Event', 'subtitle' => 'Error Page', 'content' => 'Page not found.'));
     $this->send(404);
 }