aboutsummaryrefslogtreecommitdiffstats
path: root/admin/post/insert.php
blob: af70b1e6ace0b28ff178df1a02f9d5f1d5079296 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
#===============================================================================
# DEFINE: Administration
#===============================================================================
const ADMINISTRATION = true;
const AUTHENTICATION = true;

#===============================================================================
# INCLUDE: Initialization
#===============================================================================
require '../../core/application.php';

#===============================================================================
# Get repositories
#===============================================================================
$CategoryRepository = Application::getRepository('Category');
$PostRepository = Application::getRepository('Post');
$UserRepository = Application::getRepository('User');

#===============================================================================
# Instantiate new Post entity
#===============================================================================
$Post = new ORM\Entities\Post;

#===============================================================================
# Check for insert request
#===============================================================================
if(HTTP::issetPOST('insert')) {
	$Post->set('category', HTTP::POST('category') ?: null);
	$Post->set('user', HTTP::POST('user'));
	$Post->set('slug', HTTP::POST('slug') ?: generateSlug(HTTP::POST('name')));
	$Post->set('name', HTTP::POST('name') ?: null);
	$Post->set('body', HTTP::POST('body') ?: null);
	$Post->set('argv', HTTP::POST('argv') ?: null);
	$Post->set('time_insert', HTTP::POST('time_insert') ?: date('Y-m-d H:i:s'));
	$Post->set('time_update', HTTP::POST('time_update') ?: date('Y-m-d H:i:s'));

	if(HTTP::issetPOST(['token' => Application::getSecurityToken()])) {
		try {
			$PostRepository->insert($Post);
			HTTP::redirect(Application::getAdminURL('post/'));
		} catch(PDOException $Exception) {
			$messages[] = $Exception->getMessage();
		}
	}

	else {
		$messages[] = $Language->text('error_security_csrf');
	}
}

#===============================================================================
# Generate user list
#===============================================================================
foreach($UserRepository->getAll([], 'fullname ASC') as $User) {
	$userList[] = [
		'ID' => $User->getID(),
		'FULLNAME' => $User->get('fullname'),
		'USERNAME' => $User->get('username'),
	];
}

#===============================================================================
# Generate category list
#===============================================================================
foreach($CategoryRepository->getAll([], 'name ASC') as $Category) {
	$categoryList[] = [
		'ID' => $Category->getID(),
		'NAME' => $Category->get('name'),
		'PARENT' => $Category->get('parent'),
	];
}

#===============================================================================
# Build document
#===============================================================================
$FormTemplate = Template\Factory::build('post/form');
$FormTemplate->set('FORM', [
	'TYPE' => 'INSERT',
	'INFO' => $messages ?? [],
	'DATA' => array_change_key_case($Post->getAll(), CASE_UPPER),
	'USER_LIST' => $userList ??  [],
	'CATEGORY_LIST' => $categoryList ?? [],
	'CATEGORY_TREE' => generateCategoryDataTree($categoryList ?? []),
	'TOKEN' => Application::getSecurityToken()
]);

$InsertTemplate = Template\Factory::build('post/insert');
$InsertTemplate->set('HTML', $FormTemplate);

$MainTemplate = Template\Factory::build('main');
$MainTemplate->set('NAME', $Language->text('title_post_insert'));
$MainTemplate->set('HTML', $InsertTemplate);
echo $MainTemplate;