Example #1
0
 /**
  * Render this page into XHTML.
  *
  * This methods renders the whole page into a complete XHTML page. Usually
  * you want to use flush() to output the page to the browser.
  *
  * \return
  *   The rendered page as a string.
  *
  * \see AnewtPage::flush
  */
 public function render()
 {
     /* Content-type in meta tag. This must be the first element inside the
      * <head>...</head> element. */
     $this->_head->append_child(ax_meta(array('http-equiv' => 'Content-type', 'content' => $this->build_content_type_charset())));
     /* Base URI */
     $base_uri = $this->_get('base-uri');
     if (!is_null($base_uri)) {
         assert('is_string($base_uri); // base-uri must be a simple string');
         /* Employ a simple heuristic to make sure we use an absolute URI, as
          * is required by the HTML specification. */
         if (!str_contains($base_uri, '://')) {
             $base_uri = Request::canonical_base_url() . $base_uri;
         }
         $base = new AnewtXHTMLBase();
         $base->set_attribute('href', $base_uri);
         $this->_head->append_child($base);
     }
     /* Page title */
     $title = $this->_get('title');
     if (!is_null($title)) {
         $this->_head->append_child(ax_title($title));
     }
     /* Dublin Core metadata. See http://dublincore.org/documents/dcq-html/ * */
     if ($this->_get('show-dublin-core')) {
         $this->_head->append_child(ax_link(array('rel' => 'schema.DC', 'href' => 'http://purl.org/dc/elements/1.1/')));
         $this->_head->append_child(ax_meta_name_content('DC.language', $this->_get('language')));
         if (!is_null($title)) {
             $this->_head->append_child(ax_meta_name_content('DC.title', $title));
         }
         if ($this->_isset('creator')) {
             $this->_head->append_child(ax_meta_name_content('DC.creator', $this->_get('creator')));
         }
         if ($this->_isset('description')) {
             $this->_head->append_child(ax_meta_name_content('DC.description', $this->_get('description')));
         }
         if ($this->_isset('date')) {
             $date = $this->get('date');
         } else {
             $date = AnewtDateTime::now();
         }
         $this->_head->append_child(ax_meta_name_content('DC.date', AnewtDateTime::date($date)));
     }
     /* Powered by Anewt! */
     $generator = $this->_get('generator');
     if (!is_null($generator)) {
         assert('is_string($generator);');
         $this->_head->append_child(ax_meta_name_content('generator', $generator));
     }
     /* Robots */
     $robots = $this->_get('robots');
     if (!is_null($robots)) {
         assert('is_string($robots);');
         $this->_head->append_child(ax_meta(array('name' => 'robots', 'content' => $robots)));
     }
     /* Links (including stylesheets) and JavaScripts */
     $this->_head->append_children($this->_links);
     $this->_head->append_children($this->_javascripts);
     /* Body content */
     if ($this->_get('blocks')) {
         /* This is a page using div blocks */
         assert('!$this->_content || !$this->_content->has_child_nodes(); // pages using blocks should not have content outside blocks');
         /* The buffer holding all content is either a wrapper div or an
          * invisible fragment (both XML nodes, so the API is the same). */
         if ($this->_get('use-wrapper-div')) {
             $buffer = ax_div_id(null, $this->_get('wrapper-div-id'));
         } else {
             $buffer = ax_fragment();
         }
         /* Add the content */
         foreach ($this->_get('blocks') as $block_name) {
             $block = $this->get_block($block_name);
             $buffer->append_child(ax_div_id($block, $block_name));
             unset($block);
             unset($div);
         }
         $this->_body->append_child($buffer);
     } else {
         /* This page has no blocks, so we use the nodes in _content instead
          * (if any) */
         assert('!$this->_blocks; // pages not using blocks should not have content in blocks');
         if ($this->_content) {
             $this->_body->append_child($this->_content);
         }
     }
     /* Assemble the top level elements */
     $document = new AnewtXMLDomDocument();
     $document->set_document_type($this->_get('document-type'));
     $document->set_content_type($this->_get('content-type'));
     $document->set_encoding($this->_get('encoding'));
     $html = new AnewtXMLDomElement('html', array('xmlns' => 'http://www.w3.org/1999/xhtml', 'xml:lang' => $this->_get('language'), 'lang' => $this->_get('language')));
     $html->append_child($this->_head);
     $html->append_child($this->_body);
     $document->append_child($html);
     return to_string($document);
 }
Example #2
0
 /**
  * \private
  *
  * Helper function to create RSS XML elements.
  *
  * This is only for internal use.
  *
  * \param $obj
  *   The object from which to retrieve the value
  *
  * \param $property
  *   The property name
  *
  * \param $tagname
  *   The XML tag name
  *
  * \param $status
  *   Status of the element
  *
  * \param $type
  *   Data type of the element
  *
  * \return
  *   An AnewtXMLDomElement instance, or null.
  */
 public static function _build_rss_element($obj, $property, $tagname, $status, $type)
 {
     if (!$obj->_isset($property)) {
         /* If an optional element not provided it's not a problem... */
         if ($status == ANEWT_RSS_ELEMENT_STATUS_OPTIONAL) {
             return null;
         }
         /* ...but required means required! */
         throw new AnewtException('AnewtRssItem::render(): Required property "%s" not set.', $property);
     }
     $value = $obj->_get($property);
     switch ($type) {
         case ANEWT_RSS_ELEMENT_TYPE_STRING:
             assert('is_string($value);');
             break;
         case ANEWT_RSS_ELEMENT_TYPE_CANONICAL_URL:
             assert('is_string($value);');
             if (str_has_prefix($value, '/')) {
                 /* Fixup relative URL's without a http://hostname.ext/ part */
                 $value = Request::canonical_base_url() . $value;
             }
             break;
         case ANEWT_RSS_ELEMENT_TYPE_INTEGER:
             assert('is_int($value);');
             $value = (string) $value;
             break;
         case ANEWT_RSS_ELEMENT_TYPE_DATE:
             assert('$value instanceof AnewtDateTimeAtom;');
             $value = AnewtDateTime::rfc2822($value);
             break;
         default:
             assert('false; // not reached');
             break;
     }
     $element = new AnewtXMLDomElement($tagname);
     $element->append_child(new AnewtXMLDomText($value));
     return $element;
 }
Example #3
0
 /**
  * Returns the canonical URL for the current request. This includes the
  * http part, the hostname and (optionally) port number.
  *
  * \param $include_query_string
  *   Whether the query string (the part after the question mark in HTTP GET
  *   request should be included (optional, defaults to true)
  *
  * \return
  *   The canonical URL for the current request.
  *
  * \see Request::relative_url
  * \see Request::canonical_base_url
  */
 static function canonical_url($include_query_string = true)
 {
     $canonical_base_url = Request::canonical_base_url();
     $relative_url = Request::relative_url($include_query_string);
     if ($relative_url[0] != '/') {
         $relative_url = '/' . $relative_url;
     }
     return $canonical_base_url . $relative_url;
 }