pepiscms/application/libraries/Document.php
<?php
/**
* PepisCMS
*
* Simple content management system
*
* @package PepisCMS
* @author Piotr Polak
* @copyright Copyright (c) 2007-2018, Piotr Polak
* @license See license.txt
* @link http://www.polak.ro/
*/
defined('BASEPATH') or exit('No direct script access allowed');
/**
* HTML Document representation.
* Document instance is generated by module (frontend) or builtin component and passed to the theme template.
*
* @since 0.2.0
*/
class Document extends ContainerAware
{
/** @var int */
private $id;
/** @var string */
private $contents;
/** @var string */
private $title;
/** @var string */
private $description;
/** @var string */
private $keywords;
/** @var string */
private $relative_url = null;
/** @var string */
private $canonical_url = null;
/** @var bool */
private $is_default = false;
/** @var string */
private $page_style;
/** @var array */
private $page_stylesheets = array();
/** @var array */
private $page_javascript = array();
/** @var int */
private $menu_item_id = null; // To be checked if needed
/** @var Menu */
private $menu = null; // Kind of cache
/** @var string */
private $image_relative_path = null;
/**
* Document constructor.
*/
public function __construct()
{
$this->load->model('Menu_model');
}
/**
* Returns ID of the current menu item
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Sets ID of the current menu item
*
* @param int $id
* @return Document
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* Returns ID of menu item if page attached to menu, otherwise false
*
* @return int
*/
public function getMenuItemId()
{
if ($this->menu_item_id == null) {
$this->menu_item_id = $this->Menu_model->getItemIdByPageId($this->getId());
}
return $this->menu_item_id;
}
/**
* Returns full menu tree for a given language
* All URLs are prefixed and "ready to use"
*
* @return Menu
*/
public function getMenu()
{
if ($this->menu == null) {
$this->menu = new Menu();
}
return $this->menu;
}
/**
* Sets page title
*
* @param $title
* @return Document
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Returns page title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Sets page contents
*
* @param string $contents
* @return Document
*/
public function setContents($contents)
{
$this->contents = $contents;
return $this;
}
/**
* Returns page contents
*
* @return string
*/
public function getContents()
{
return $this->contents;
}
/**
* Sets page description
*
* @param $description
* @return Document
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Returns page description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Sets page keywords
*
* @param $keywords
* @return Document
*/
public function setKeywords($keywords)
{
$this->keywords = $keywords;
return $this;
}
/**
* Returns page keywords
*
* @return string
*/
public function getKeywords()
{
return $this->keywords;
}
/**
* Sets canonical absolute URL of the page
*
* @param string $canonical_url
* @return Document
*/
public function setCanonicalAbsoluteUrl($canonical_url)
{
$this->canonical_url = $canonical_url;
return $this;
}
/**
* Returns absolute canonical URL, if it does not exist, then it is created from relative URL
*
* @return string
*/
public function getCanonicalAbsoluteUrl()
{
if ($this->canonical_url === null) {
$this->setCanonicalAbsoluteUrl(base_url() . $this->getRelativeUrl());
}
return $this->canonical_url;
}
/**
* Sets relative URL
*
* @param string $relative_url
* @return Document
*/
public function setRelativeUrl($relative_url)
{
$this->relative_url = $relative_url;
return $this;
}
/**
* Returns relative URL
*
* @return string
*/
public function getRelativeUrl()
{
return $this->relative_url;
}
/**
* Makes the page default
*
* @param bool $is_default
* @return Document
*/
public function setDefault($is_default = true)
{
$this->is_default = $is_default;
return $this;
}
/**
* Tells whether the page is default
*
* @return bool
*/
public function isDefault()
{
return $this->is_default;
}
/**
* Returns URL prefix
* For the default language the prefix is empty
*
* @return string
*/
public function getLanguagePrefix()
{
return Dispatcher::getUriPrefix();
}
/**
* Returns URL code
* Similar to getLanguagePrefix() but it always returns a value
*
* @return string
*/
public function getLanguageCode()
{
return Dispatcher::getSiteLanguage()->code;
}
/**
* Returns the breadcrumb items
*
* @return array
*/
public function getBreadcrumbMenuItems()
{
$breadcrumb_items = array();
$item = $this->getMenu()->getMenuItemByCanonicalAbsoluteUrl($this->getCanonicalAbsoluteUrl());
if ($item) {
while (true) {
$breadcrumb_items[] = $item;
$parent = $item->getParent();
if (!($parent instanceof MenuItem)) { // When we reach top most element
break;
}
$item = $parent;
}
}
$breadcrumb_items = array_reverse($breadcrumb_items);
return $breadcrumb_items;
}
/* Compatibility */
/**
* Returns formatted style, ready to inject
*
* @return string
*/
public function getPageStyles()
{
$styles = '';
if (isset($this->page_style)) {
$styles = '<style type="text/css">' . "\n<!--\n" . $this->page_style . "\n-->\n</style>\n";
}
if (count($this->page_stylesheets)) {
foreach ($this->page_stylesheets as $stylesheet) {
$styles .= '<link rel="stylesheet" type="text/css" href="' . site_theme_url() . $stylesheet . '" media="screen" />' . "\n";
}
}
return $styles;
}
/**
* Returns formatted JS, ready to inject
*
* @return string
*/
public function getPageJavaScript()
{
$code = '';
if (count($this->page_javascript)) {
foreach ($this->page_javascript as $item) {
$code .= '<script src="' . site_theme_url() . $item . '"></script>' . "\n";
}
}
return $code;
}
/**
* Inserts code to style section
*
* @param string $styles
* @return Document
*/
public function addInlinePageStyles($styles)
{
if (isset($this->page_style)) {
$this->page_style .= $styles;
} else {
$this->page_style = $styles;
}
return $this;
}
/**
* Attaches stylesheet
*
* @param string $stylesheet_name
* @return Document
*/
public function attachStyleSheet($stylesheet_name)
{
$this->page_stylesheets[] = $stylesheet_name;
return $this;
}
/**
* @param string $stylesheet_name
*
* @deprecated as PepisCMS 1.0.0
*/
public function attachSyleSheet($stylesheet_name)
{
trigger_error('Document::attachSyleSheet() is deprecated and scheduled for deletion. Please use Document::attachStyleSheet()', E_USER_DEPRECATED);
$this->attachStyleSheet($stylesheet_name);
}
/**
* Attaches JavaScript
*
* @param string $javascript_file_name
* @return Document
*/
public function attachJavaScript($javascript_file_name)
{
$this->page_javascript[] = $javascript_file_name;
return $this;
}
/**
* Overwrites the entire array of stylesheets
*
* @param array $stylesheets
* @return Document
*/
public function setStyleSheets($stylesheets)
{
$this->page_stylesheets = $stylesheets;
return $this;
}
/**
* Sets JavaScript
*
* @param array $javascript
* @return Document
*/
public function setJavaScript($javascript)
{
$this->page_javascript = $javascript;
return $this;
}
/**
* Sets image relative path
*
* @param $image_relative_path
* @return $this
*/
public function setImageRelativePath($image_relative_path)
{
$this->image_relative_path = $image_relative_path;
return $this;
}
/**
* Returns image relative path
*
* @return string
*/
public function getImageRelativePath()
{
return $this->image_relative_path;
}
}