<?php
system::includeLib('treesort');
class documents
{
/**
* @var mysqliConnection
*/
private $_db;
private $_tableName;
public function __construct(&$db)
{
$this->_db = &$db;
$this->_tableName = config::get('documents', 'tableName');
$db->checkTable($this->_tableName, "
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`body` text COLLATE utf8_unicode_ci NOT NULL,
`link` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`parent_id` int(10) unsigned NOT NULL,
`is_locked` tinyint(1) unsigned NOT NULL,
`is_visible` tinyint(1) unsigned NOT NULL,
`created` datetime NOT NULL,
`updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
");
}
public function create($title)
{
$this->_db->insert($this->_tableName,
array('title' => $title,
'body' => '',
'parent_id' => 0,
'is_locked' => 0,
'is_visible' => 0,
'created' => 'FROM_UNIXTIME(' . time() . ')'));
return $this->_db->lastInsertedId();
}
public function edit($id, $title, $body, $visible, $parent_id)
{
if (empty($title)) {
$title = trim(substr(strip_tags($body), 0, 10)) . '...';
}
$array = array(
'title' => $title,
'body' => $body,
'is_visible' => $visible,
'parent_id' => $parent_id
);
$document = $this->get($id);
if ($document['is_locked'] == 0) {
$array['link'] = $this->_makeLink($title);
}
$this->_db->update($this->_tableName, $array, 'id = '.$this->_db->quote($id));
}
public function generateMenu($documents)
{
return treesort::sort($documents);
}
/*public function generateMenu($documents)
{
$menu = array();
$tempOrphans = array();
$links = array();
foreach ($documents as $document) {
if ($document['parent_id'] == 0) {
$menu[$document['id']] = $document;
$links[$document['id']] = &$menu[$document['id']];
if (isset($tempOrphans[$document['id']])) {
$menu[$document['id']]['children'] = $tempOrphans[$document['id']];
} else {
$menu[$document['id']]['children'] = array();
}
} else {
if (!isset($links[$document['parent_id']])) {
$tempOrphans[$document['id']][] = $document;
} else {
$links[$document['parent_id']]['children'][] = $document;
}
}
}
var_dump($menu);die;
return $menu;
}
public function generateMenuFlat($documents)
{*/
/*$menu = array();
$links = array();
foreach ($documents as $document) {
if (!isset($links[$document['location']])) {
$menu[] = $document;
$links[$document['id']] = count($menu) - 1;
} else {
for ($i = $links[$document['location']] + 1; $menu[$i]['location'] == $document['location']; $i++) {}
array_splice($menu, $i, 0, $document);
}
}*/
/*
return $this->_flattenMenu($this->generateMenu($documents));;
}*/
public function get($id)
{
$document = $this->_db->getRow($this->_tableName, '*', 'id', $id);
if (is_null($document)) {
die('Document does not exist');
}
return $document;
}
public function getAll($cols = '*', $filterParentId = 0, $onlyVisible = false)
{
$tableName = $this->_tableName;
$whereArray = array();
if (!empty($filterParentId)) {
$whereArray[] = "parent_id = " . $this->_db->quote($filterParentId);
}
if ($onlyVisible) {
$whereArray[] = 'is_visible = 1';
}
$where = !empty($whereArray) ? 'WHERE' : '';
$sql_query = "SELECT $cols FROM $tableName $where " . implode(' AND ', $whereArray) . " ORDER BY id ASC";
return $this->_db->getRows($sql_query);
}
public function getByLink($link)
{
return $this->_db->getRow($this->_tableName, '*', 'link', $link);
}
public function getIncludeKeys($body, $keys)
{
$includeKeys = array();
foreach ($keys as $key) {
$includeKeys[$key] = array();
preg_match_all("/%($key)(\(([a-z0-9]+)='([^']*)'(,\s*([a-z0-9]+)='([^']*)')*\))?%/", $body, $matches);
foreach ($matches[1] as $index => $keyName) {
$parameters = array();
for ($i = 3; !empty($matches[$i][$index]); $i += 3) {
$parameters[$matches[$i][$index]] = html_entity_decode($matches[$i + 1][$index], ENT_COMPAT, 'utf-8');
}
$includeKeys[$key][] = array(
'includeString' => $matches[0][$index],
'name' => $keyName,
'parameters' => $parameters
);
}
}
return $includeKeys;
}
public function remove($id)
{
$this->_db->delete($this->_tableName, 'id = '.$this->_db->quote($id));
}
/*private function _flattenMenu($menu, $level = 0)
{
$flatMenu = array();
foreach ($menu as $item) {
$flatChildren = array();
// Get children
if (!empty($item['children'])) {
$flatChildren = $this->_flattenMenu($item['children'], $level + 1);
}
// Set level
$item['level'] = $level;
// Remove children, because we are flattening
unset($item['children']);
// Add item
$flatMenu[] = $item;
// Add children
array_splice($flatMenu, count($flatMenu), 0, $flatChildren);
}
return $flatMenu;
}*/
private function _makeLink($text)
{
$linkUnique = $link = $this->_linkify($text);
$i = 1;
while (!is_null($this->getByLink($linkUnique))) {
$linkUnique = $link . '-' . $i++;
}
return $linkUnique;
}
private function _linkify($filename, $additionalCharacters = '')
{
if (mb_detect_encoding($filename, 'UTF-8, ISO-8859-1') == 'UTF-8') {
$filename = utf8_decode($filename);
}
if (mb_detect_encoding($additionalCharacters, 'UTF-8, ISO-8859-1') == 'UTF-8') {
$additionalCharacters = utf8_decode($additionalCharacters);
}
return utf8_encode(preg_replace("/[^a-z0-9{$additionalCharacters}_.!~*')(-]/", '',
preg_replace('/\s+/', '-',
strtolower($filename)
)
));
}
}