Создать простой сайт, где будет страница с формой для авторизации и страница для авторизованного пользователя.
<?php
require_once("Models/ContentItem.php");
require_once("DatabaseService.php");
class ContentRegistry
{
private $table_name = "content_items";
private $dbService;
public function __construct()
{
$this->dbService = new DatabaseService();
}
public function createNode($parent_id, $title, $description)
{
if (empty($title))
return;
$title = Helper::sanitizeInput($title);
$description = Helper::sanitizeInput($description);
//$db_service = new DatabaseService();
$this->dbService->execute("INSERT INTO {$this->table_name} (parent_id, title, description) VALUES ({$parent_id}, '{$title}', '{$description}')");
}
public function updateNode($node_id, $title, $description)
{
if (empty($title))
return;
$title = Helper::sanitizeInput($title);
$description = Helper::sanitizeInput($description);
//$db_service = new DatabaseService();
$this->dbService->execute("UPDATE {$this->table_name} SET title = '{$title}', description = '{$description}' WHERE item_id = {$node_id}");
}
public function getRootNode()
{
$item = null;
//$db_service = new DatabaseService();
//$db_connect = $db_service->getConn();
$sql = "SELECT * FROM {$this->table_name} WHERE parent_id = 0";
$data = $this->dbService->query($sql);
if ($data->num_rows > 0)
{
$row = $data->fetch_assoc();
$item = new ContentItem($row["item_id"], $row["parent_id"], $row["title"], $row["description"]);
$data->free_result();
}
//$db_connect->close();
return $item;
}
public function getNodeById($node_id)
{
$item = null;
if ($node_id > 0)
{
//$db_service = new DatabaseService();
//$db_connect = $db_service->getConn();
$sql = "SELECT * FROM {$this->table_name} WHERE item_id = ".$node_id;
$data = $this->dbService->query($sql);
if ($data->num_rows > 0)
{
$row = $data->fetch_assoc();
$item = new ContentItem($row["item_id"], $row["parent_id"], $row["title"], $row["description"]);
$data->free_result();
}
//$db_connect->close();
}
return $item;
}
public function getItemsTree($node_id)
{
$items = array();
//$db_service = new DatabaseService();
//$db_connect = $db_service->getConn();
$sql = "WITH RECURSIVE items_tree (item_id, parent_id, title, description, path, level) AS
(
SELECT item_id, parent_id, title, description, title as path, 0 level
FROM {$this->table_name}
WHERE parent_id = {$node_id}
UNION ALL
SELECT c.item_id, c.parent_id, c.title, c.description, CONCAT(it.path, '/', c.title), it.level + 1
FROM items_tree AS it
JOIN {$this->table_name} AS c ON it.item_id = c.parent_id
)
SELECT * FROM items_tree ORDER BY path, level;";
$data = $this->dbService->query($sql);
if ($data->num_rows > 0)
{
while ($row = $data->fetch_assoc())
{
$items[] = new ContentItem($row["item_id"], $row["parent_id"], $row["title"], $row["description"]);
}
$data->free_result();
}
//$db_connect->close();
return $items;
}
public function nodeHasChildren($items, $id)
{
foreach ($items as $item)
{
if ($item->getParentId() == $id)
return true;
}
return false;
}
public function buildTree($items, $parent, $current_node_id)
{
$result = "<ul class=\"tree\">\r\n";
foreach ($items as $item)
{
if ($item->getParentId() == $parent)
{
if ($current_node_id == $item->getId())
$result .= "<li class=\"tree-node tree-node-active\">{$item->getTitle()}\r\n";
else
$result .= "<li class=\"tree-node\"><a href=\"content.php?id={$item->getId()}\">{$item->getTitle()}</a>\r\n";
if ($this->nodeHasChildren($items, $item->getId()))
$result .= $this->buildTree($items, $item->getId(), $current_node_id);
$result .= "</li>\r\n";
}
}
$result .= "</ul>\r\n";
return $result;
}
public function displayItemsTree($node_id)
{
echo $this->buildTree($this->getItemsTree(0), 0, $node_id);
}
public function deleteNode($node_id)
{
$items = $this->getItemsTree($node_id);
// delete child nodes
//$db_service = new DatabaseService();
foreach ($items as $item)
{
$this->dbService->execute("DELETE FROM {$this->table_name} WHERE item_id = {$item->getId()}");
}
$node_info = $this->getNodeById($node_id);
if (!$this->isRootNode($node_info))
$this->dbService->execute("DELETE FROM {$this->table_name} WHERE item_id = {$node_id}");
}
public function isNodeExists($node_id)
{
$item = $this->getNodeById($node_id);
return $item != null;
}
public function isRootNode(ContentItem $node_info)
{
if ($node_info != null)
{
return $node_info->getParentId() == 0;
}
return false;
}
public function resetContent()
{
//$db_service = new DatabaseService();
$this->dbService->execute("TRUNCATE TABLE {$this->table_name}");
$this->dbService->execute("INSERT INTO {$this->table_name} (parent_id, title, description) VALUES (0, 'PHP Manual', 'PHP, which stands for \"PHP: Hypertext Preprocessor\" is a widely-used Open Source general-purpose scripting language that is especially suited for web development and can be embedded into HTML. Its syntax draws upon C, Java, and Perl, and is easy to learn. The main goal of the language is to allow web developers to write dynamically generated web pages quickly, but you can do much more with PHP.')");
$this->dbService->execute("INSERT INTO {$this->table_name} (parent_id, title, description) VALUES (1, 'Introduction', '')");
$this->dbService->execute("INSERT INTO {$this->table_name} (parent_id, title, description) VALUES (2, 'PHP description', 'PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.')");
$this->dbService->execute("INSERT INTO {$this->table_name} (parent_id, title, description) VALUES (2, 'What can PHP do?', 'Anything. PHP is mainly focused on server-side scripting, so you can do anything any other CGI program can do, such as collect form data, generate dynamic page content, or send and receive cookies. But PHP can do much more. There are three main areas where PHP scripts are used.')");
$this->dbService->execute("INSERT INTO {$this->table_name} (parent_id, title, description) VALUES (1, 'Language Reference', '')");
$this->dbService->execute("INSERT INTO {$this->table_name} (parent_id, title, description) VALUES (5, 'Basic syntax', '')");
$this->dbService->execute("INSERT INTO {$this->table_name} (parent_id, title, description) VALUES (6, 'PHP tags', 'When PHP parses a file, it looks for opening and closing tags, which are <?php and ?> which tell PHP to start and stop interpreting the code between them. Parsing in this manner allows PHP to be embedded in all sorts of different documents, as everything outside of a pair of opening and closing tags is ignored by the PHP parser. PHP includes a short echo tag <?= which is a short-hand to the more verbose <?php echo. ')");
$this->dbService->execute("INSERT INTO {$this->table_name} (parent_id, title, description) VALUES (6, 'Instruction separation', 'As in C or Perl, PHP requires instructions to be terminated with a semicolon at the end of each statement. The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block. The closing tag for the block will include the immediately trailing newline if one is present.')");
}
}