Exemplo n.º 1
0
 /**
  * Is this menu item the active one?
  *
  * @return  boolean
  */
 public function isActive()
 {
     // Get the current URI
     $uri = Uri::getInstance();
     // If we have an exact match to the custom URL, return true
     if ($uri->toString() == $this->url) {
         return true;
     }
     // If there are no parameters to check and the URLs don't match, it's not an active menu item
     if (empty($this->params)) {
         return false;
     }
     // Otherwise check if the parameters match
     foreach ($this->params as $k => $v) {
         $uv = $uri->getVar($k, null);
         if ($uv != $v) {
             return false;
         }
     }
     return true;
 }
Exemplo n.º 2
0
 /**
  * Creates a FOFHalDocument using the provided data
  *
  * @param   array      $data   The data to put in the document
  * @param   DataModel  $model  The model of this view
  *
  * @return  \Awf\Hal\Document  A HAL-enabled document
  */
 protected function _createDocumentWithHypermedia($data, $model = null)
 {
     // Create a new HAL document
     if (is_array($data)) {
         $count = count($data);
     } else {
         $count = null;
     }
     if ($count == 1) {
         reset($data);
         $document = new \Awf\Hal\Document(end($data));
     } else {
         $document = new \Awf\Hal\Document($data);
     }
     // Create a self link
     $router = $this->container->router;
     $uri = (string) Uri::getInstance();
     $uri = $this->_removeURIBase($uri);
     $uri = $router->route($uri);
     $document->addLink('self', new Link($uri));
     // Create relative links in a record list context
     if (is_array($data) && $model instanceof DataModel) {
         if (!isset($this->total)) {
             $this->total = $model->count();
         }
         if (!isset($this->limitStart)) {
             $this->limitStart = $model->getState('limitstart', 0);
         }
         if (!isset($this->limit)) {
             $this->limit = $model->getState('limit', 0);
         }
         $pagination = new Pagination($this->total, $this->limitStart, $this->limit, 10, $this->container->application);
         if ($pagination->pagesTotal > 1) {
             // Try to guess URL parameters and create a prototype URL
             // NOTE: You are better off specialising this method
             $protoUri = $this->_getPrototypeURIForPagination();
             // The "first" link
             $uri = clone $protoUri;
             $uri->setVar('limitstart', 0);
             $uri = $router->route($uri);
             $document->addLink('first', new Link($uri));
             // Do we need a "prev" link?
             if ($pagination->pagesCurrent > 1) {
                 $prevPage = $pagination->pagesCurrent - 1;
                 $limitstart = ($prevPage - 1) * $pagination->limit;
                 $uri = clone $protoUri;
                 $uri->setVar('limitstart', $limitstart);
                 $uri = $router->route($uri);
                 $document->addLink('prev', new Link($uri));
             }
             // Do we need a "next" link?
             if ($pagination->pagesCurrent < $pagination->pagesTotal) {
                 $nextPage = $pagination->pagesCurrent + 1;
                 $limitstart = ($nextPage - 1) * $pagination->limit;
                 $uri = clone $protoUri;
                 $uri->setVar('limitstart', $limitstart);
                 $uri = $router->route($uri);
                 $document->addLink('next', new Link($uri));
             }
             // The "last" link?
             $lastPage = $pagination->pagesTotal;
             $limitstart = ($lastPage - 1) * $pagination->limit;
             $uri = clone $protoUri;
             $uri->setVar('limitstart', $limitstart);
             $uri = $router->route($uri);
             $document->addLink('last', new Link($uri));
         }
     }
     return $document;
 }
Exemplo n.º 3
0
 /**
  * Redirect to another URL.
  *
  * Optionally enqueues a message in the system message queue (which will be displayed
  * the next time a page is loaded) using the enqueueMessage method. If the headers have
  * not been sent the redirect will be accomplished using a "301 Moved Permanently"
  * code in the header pointing to the new location. If the headers have already been
  * sent this will be accomplished using a JavaScript statement.
  *
  * @param   string  $url     The URL to redirect to. Can only be http/https URL
  * @param   string  $msg     An optional message to display on redirect.
  * @param   string  $msgType An optional message type. Defaults to message.
  * @param   boolean $moved   True if the page is 301 Permanently Moved, otherwise 303 See Other is assumed.
  *
  * @return  void  Calls exit().
  *
  * @see     Application::enqueueMessage()
  */
 public function redirect($url, $msg = '', $msgType = 'info', $moved = false)
 {
     // Check for relative internal links.
     if (preg_match('#^index\\.php#', $url)) {
         $url = Uri::base(false, $this->container) . $url;
     }
     // Strip out any line breaks.
     $url = preg_split("/[\r\n]/", $url);
     $url = $url[0];
     /*
      * If we don't start with a http we need to fix this before we proceed.
      * We could validly start with something else (e.g. ftp), though this would
      * be unlikely and isn't supported by this API.
      */
     if (!preg_match('#^http#i', $url)) {
         $uri = Uri::getInstance();
         $prefix = $uri->toString(array('scheme', 'user', 'pass', 'host', 'port'));
         if ($url[0] == '/') {
             // We just need the prefix since we have a path relative to the root.
             $url = $prefix . $url;
         } else {
             // It's relative to where we are now, so lets add that.
             $parts = explode('/', $uri->toString(array('path')));
             array_pop($parts);
             $path = implode('/', $parts) . '/';
             $url = $prefix . $path . $url;
         }
     }
     // If the message exists, enqueue it.
     if (is_string($msg) && trim($msg)) {
         $this->enqueueMessage($msg, $msgType);
     }
     // Persist messages if they exist.
     if (count($this->messageQueue)) {
         $this->container->segment->setFlash('application_queue', $this->messageQueue);
     }
     $this->container->session->commit();
     // If the headers have been sent, then we cannot send an additional location header
     // so we will output a javascript redirect statement.
     if (headers_sent()) {
         $url = htmlspecialchars($url);
         $url = str_replace('&amp;', '&', $url);
         echo "<script>document.location.href='" . $url . "';</script>\n";
     } else {
         header($moved ? 'HTTP/1.1 301 Moved Permanently' : 'HTTP/1.1 303 See other');
         header('Location: ' . $url);
     }
     exit(0);
 }