<?php
namespace Empire\Content;
use Empire\Content\PartType\AbstractType;
use Empire\Core\AbstractEntity;
use Empire\Core\Entity;
use Empire\Core\Event;
use Empire\Core\Named;
use Empire\Core\User;
use Empire\Legacy\db;
use Empire\Legacy\cgt;
class Content extends AbstractEntity implements Entity, Named {
const PRIMARY_FIELD = "CONT_ID";
const TABLE = "CONTENT";
public $name;
public $title;
/**
* @var $content string The content
* @deprecated
*/
public $content;
/** @var Category $category */
public $category;
public $page;
public $created;
public $updated;
/** @var Part[] $parts */
public $parts;
protected $CONT_ID;
protected $CCAT_ID;
protected $CONT_PAGE;
protected $CONT_TITLE;
protected $CONT_TEXT;
protected $CONT_CREATED;
protected $CONT_UPDATED;
function __construct() {
$this->type = cint_TYPE_CONTENT;
parent::__construct();
}
/**
* @param string $title
* @param string $page
* @param int|Category $category
* @return int
* @throws \Exception
*/
public static function addPage($title, $page, $category) {
if($category instanceof Category) {
$category = $category->getID();
}
if(!self::isValidPageSlug($page)) {
throw new \Exception('Invalid characters for the url slug. Allowed characters are <code>a-z A-Z 0-9 . _ -</code>.');
}
$stmt = db::insert(self::TABLE, [
'CCAT_ID' => $category,
'CONT_TITLE' => $title,
'CONT_PAGE' => $page,
'CONT_TEXT' => '',
'CONT_UPDATED' => "NOW()",
]);
$id = db::insertID();
$stmt = db::insert(Part::TABLE, [
"CONT_ID" => $id,
'CPAR_TYPE' => 'basic',
'CPAR_TEXT' => "<p>Newly created page.</p>",
'CPAR_REGION' => 'main',
'CPAR_ORDER' => 1,
'CPAR_SETTINGS' => ''
]);
return $id;
}
public static function sanitiseUrl($url) {
return strtolower(filter_var($url, FILTER_SANITIZE_URL));
}
function initialize() {
$this->id = $this->CONT_ID;
$this->title = $this->name = $this->CONT_TITLE;
$this->content = $this->CONT_TEXT;
$this->page = $this->CONT_PAGE;
$this->category = Category::load([$this->CCAT_ID]);
$this->created = cgt::evaluateString($this->CONT_CREATED);
$this->updated = cgt::evaluateString($this->CONT_UPDATED);
$this->parts = Part::loadPage($this->id);
}
function getTable() {
return self::TABLE;
}
function getPrimaryField() {
return self::PRIMARY_FIELD;
}
function getName() {
return $this->title;
}
/**
* @param string $category
* @param string $page
* @return Content
* @throws \Exception
*/
public static function getByPage($category, $page) {
$stmt = db::prepare("SELECT * FROM CONTENT LEFT JOIN CONTENT_CATEGORIES ON CONTENT.CCAT_ID = CONTENT_CATEGORIES.CCAT_ID WHERE CCAT_NAME = ? AND CONT_PAGE = ?", $category, $page);
return db::fetchObject($stmt, self::class);
}
function canEdit(User $user) {
return $this->category->canEdit($user);
}
function savePage($title, $content, User $user) {
$stmt = db::prepare("UPDATE CONTENT SET CONT_TITLE = ?, CONT_TEXT = ?, CONT_UPDATED = NOW() WHERE CONT_ID = ?", $title, $content, $this->id);
Event::send($user->getID(), cint_RECEIVER_ADMIN, sprintf("%s has updated page %s/%s", $user->getName(), $this->category->name, $this->page), cint_EVENTTYPE_ADMIN);
$this->refresh();
}
public function orderParts($region, array $parts) {
$list = array_shift($parts);
for($i = 0; $i < count($parts); $i++) {
$list .= "," . $parts[$i];
}
$query = sprintf("SET @pos := 0; UPDATE `%s` SET CPAR_ORDER = (@pos := @pos +1) WHERE `%s` IN (%s) AND `CPAR_REGION` = '%s' ORDER BY FIELD(%s, %s);",
Part::TABLE,
Part::PRIMARY_FIELD,
$list,
$region,
Part::PRIMARY_FIELD,
$list);
\Empire\Core\Core::db()->executeQuery($query) or die($query . "<br>" . \Empire\Core\Core::db()->error);
}
static function isValidPageSlug($slug) {
$matches = preg_match('/[^a-zA-Z0-9_\-.]+/', $slug);
if($matches === false) {
throw new \Exception('Something when wrong while attempting to test the url slug for validity');
}
if($matches > 0 ) return false;
else return true;
}
/**
* @param $type
* @param $region
* @return Part
* @throws \Exception
*/
public function addPart($type, $region) {
$order = $this->parts[count($this->parts) -1]->order;
$className = Part::TYPES[$type];
/** @var AbstractType $class */
$class = new $className();
db::insert(Part::TABLE, [
'CONT_ID' => $this->id,
'CPAR_ORDER' => $order + 1,
'CPAR_TYPE' => $type,
'CPAR_REGION' => $region,
'CPAR_TEXT' => json_encode($class->defaultText()),
'CPAR_SETTINGS' => json_encode($class->defaultConfig())
]);
$this->refresh();
return $this->parts[count($this->parts) -1];
}
}