aboutsummaryrefslogtreecommitdiffstats
path: root/core/include/feed/main.php
blob: b8e03dc762459e3931f5632373aaa1954ef5702d (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
<?php
#===============================================================================
# Get instances
#===============================================================================
$Database = Application::getDatabase();
$Language = Application::getLanguage();

#===============================================================================
# HEADER: Content-Type for XML document
#===============================================================================
HTTP::responseHeader(HTTP::HEADER_CONTENT_TYPE, HTTP::CONTENT_TYPE_XML);

#===============================================================================
# TRY: Template\Exception
#===============================================================================
try {
	if(!isset($param) OR $param !== 'page') {
		$POST['FEED_SORT'] = Application::get('POST.FEED_SORT');
		$POST['FEED_SIZE'] = Application::get('POST.FEED_SIZE');

		$execSQL = "SELECT id FROM %s ORDER BY {$POST['FEED_SORT']} LIMIT {$POST['FEED_SIZE']}";
		$postIDs = $Database->query(sprintf($execSQL, Post\Attribute::TABLE))->fetchAll($Database::FETCH_COLUMN);

		foreach($postIDs as $postID) {
			try {
				$Post = Post\Factory::build($postID);
				$User = User\Factory::build($Post->attr('user'));

				$ItemTemplate = Template\Factory::build('feed/item_post');
				$ItemTemplate->set('POST', generateItemTemplateData($Post));
				$ItemTemplate->set('USER', generateItemTemplateData($User));

				$posts[] = $ItemTemplate;
			}

			catch(Post\Exception $Exception){}
			catch(User\Exception $Exception){}
		}
	}

	if(!isset($param) OR $param !== 'post') {
		$PAGE['FEED_SORT'] = Application::get('PAGE.FEED_SORT');
		$PAGE['FEED_SIZE'] = Application::get('PAGE.FEED_SIZE');

		$execSQL = "SELECT id FROM %s ORDER BY {$PAGE['FEED_SORT']} LIMIT {$PAGE['FEED_SIZE']}";
		$pageIDs = $Database->query(sprintf($execSQL, Page\Attribute::TABLE))->fetchAll($Database::FETCH_COLUMN);

		foreach($pageIDs as $pageID) {
			try {
				$Page = Page\Factory::build($pageID);
				$User = User\Factory::build($Page->attr('user'));

				$ItemTemplate = Template\Factory::build('feed/item_page');
				$ItemTemplate->set('PAGE', generateItemTemplateData($Page));
				$ItemTemplate->set('USER', generateItemTemplateData($User));

				$pages[] = $ItemTemplate;
			}

			catch(Page\Exception $Exception){}
			catch(User\Exception $Exception){}
		}
	}

	$FeedTemplate = Template\Factory::build('feed/main');
	$FeedTemplate->set('FEED', [
		'TYPE' => $param ?? NULL,
		'LIST' => [
			'POSTS' => $posts ?? [],
			'PAGES' => $pages ?? [],
		]
	]);

	echo $FeedTemplate;
}

#===============================================================================
# CATCH: Template\Exception
#===============================================================================
catch(Template\Exception $Exception) {
	Application::exit($Exception->getMessage());
}
?>