<?php
namespace Empire\Core;
use Empire\Legacy\db;
class PageCategory extends AbstractEntity implements Entity, Named {
const PRIMARY_FIELD = "PACA_ID";
const TABLE = "PAGE_CATEGORY";
public $name;
public $url;
public $order;
public $secure;
protected $PACA_ID;
protected $PACA_ORDER;
protected $PACA_NAME;
protected $PACA_DIRECT;
protected $PACA_SECURE;
private $subcategories;
private $pages;
function getPrimaryField() {
return self::PRIMARY_FIELD;
}
function getTable() {
return self::TABLE;
}
function initialize() {
$this->id = $this->PACA_ID;
$this->name = $this->PACA_NAME;
$this->url = $this->PACA_DIRECT;
$this->secure = $this->PACA_SECURE ? true: false;
$this->subcategories = [];
}
/**
* @param int $type
* @return PageCategory[]
* @throws \Exception
*/
static function loadAll($type = Page::PAGE_TYPE_ALL) {
if($type == Page::PAGE_TYPE_ALL) {
$stmt = db::prepare("SELECT * FROM !! ORDER BY PACA_ORDER ASC", self::TABLE);
} else {
$stmt = db::prepare("SELECT * FROM !! WHERE PACA_SECURE = !! ORDER BY PACA_ORDER ASC", self::TABLE, $type);
}
return db::fetchArrayOfObjects($stmt, self::class);
}
function getName() {
return $this->name;
}
static function getByUrl($url) {
$res = db::prepare("SELECT * FROM SECUREPAGE WHERE SEPA_PAGE = ?", $url);
return db::fetchObject($res, self::class);
}
/**
* @return PageSubCategory[]
*/
function subCategories() {
if(count($this->subcategories) < 1) {
$stmt = db::prepare("SELECT * FROM !! WHERE PACA_ID = ? ORDER BY SCAT_ORDER", PageSubCategory::TABLE, $this->id);
$this->subcategories = db::fetchArrayOfObjects($stmt, PageSubCategory::class);
}
return $this->subcategories;
}
/**
* @return Page[]
*/
function pages() {
if (count($this->pages) < 1) {
$stmt = db::prepare("SELECT * FROM !! WHERE PACA_ID = ? ORDER BY SEPA_PAGE ASC", Page::TABLE, $this->id);
$this->pages = db::fetchArrayOfObjects($stmt, Page::class);
}
return $this->pages;
}
/**
* @param string $name
* @return PageCategory $this
*/
function setName($name) {
$name = trim($name);
$this->name = $name;
$this->PACA_NAME = $name;
$this->updateFields(['PACA_NAME' => $name]);
return $this;
}
function delete() {
foreach ($this->subCategories() as $page) {
$page->delete();
}
return parent::delete();
}
/**
* @param $name
* @param bool $secure
* @param string $url
* @return int
* @throws \Exception
*/
static function addPageCategory($name, bool $secure, $url = "") {
$name = trim($name);
return db::insert(self::TABLE, [
'PACA_ORDER' => 0,
'PACA_NAME' => $name,
'PACA_DIRECT' => $url,
'PACA_SECURE' => $secure
]);
}
/**
* @param $array
* @return bool
* @throws \Exception
*/
public static function saveOrder($array) {
$i = $k = $paca = 0;
foreach ($array as $e) {
if(!isset($e['scat']) || !isset($e['paca'])) continue;
if($e['paca'] != $paca) {
$paca = $e['paca'];
(PageCategory::load([$paca]))->updateFields(['PACA_ORDER' => $k]);
$k++;
$i = 0;
}
/** @var PageSubCategory $pscat */
if(($pscat = PageSubCategory::load([$e['scat']])) !== null) {
$pscat->updateFields(['SCAT_ORDER' => $i]);
}
$i++;
if(isset($e['pages'])) {
$stmt = db::prepare("SELECT * FROM SECUREPAGE WHERE SEPA_ID IN ? ORDER BY FIELD(SEPA_ID, !! )", $e['pages'], implode(', ', $e['pages']));
/** @var Page[] $pages */
$pages = db::fetchArrayOfObjects($stmt, Page::class);
$j = 0;
foreach ($pages as $page) {
$page->updateFields(['PACA_ID' => $e['paca'], 'SCAT_ID' => $e['scat'], 'SEPA_ORDER' => $j]);
$j++;
}
}
}
return true;
}
}