summaryrefslogtreecommitdiffstats
path: root/core/include/user/main.php
blob: ee1fa76ad79eeda20ff88b268127235d33825a91 (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
95
96
97
98
99
100
101
102
<?php
#===============================================================================
# Get instances
#===============================================================================
$Database = Application::getDatabase();
$Language = Application::getLanguage();

#===============================================================================
# TRY: User\Exception
#===============================================================================
try {
	if(Application::get('USER.SLUG_URLS')) {
		$User = User\Factory::buildBySlug($param);
	}

	else {
		$User = User\Factory::build($param);
	}

	$user_data = generateItemTemplateData($User);

	#===============================================================================
	# Add user data for previous and next user
	#===============================================================================
	try {
		$PrevUser = User\Factory::build($User->getPrevID());
		$user_data['PREV'] = generateItemTemplateData($PrevUser);
	} catch(User\Exception $Exception){}

	try {
		$NextUser = User\Factory::build($User->getNextID());
		$user_data['NEXT'] = generateItemTemplateData($NextUser);
	} catch(User\Exception $Exception){}

	#===============================================================================
	# TRY: Template\Exception
	#===============================================================================
	try {
		#===============================================================================
		# TRY: PDOException
		#===============================================================================
		try {
			$PostCountStatement = $Database->query(sprintf('SELECT COUNT(*) FROM %s WHERE user = %d', Post\Attribute::TABLE, $User->getID()));
			$PageCountStatement = $Database->query(sprintf('SELECT COUNT(*) FROM %s WHERE user = %d', Page\Attribute::TABLE, $User->getID()));
		}

		#===============================================================================
		# CATCH: PDOException
		#===============================================================================
		catch(PDOException $Exception) {
			exit($Exception->getMessage());
		}

		$UserTemplate = Template\Factory::build('user/main');
		$UserTemplate->set('USER', $user_data);
		$UserTemplate->set('COUNT', [
			'POST' => $PostCountStatement->fetchColumn(),
			'PAGE' => $PageCountStatement->fetchColumn()
		]);

		$MainTemplate = Template\Factory::build('main');
		$MainTemplate->set('HTML', $UserTemplate);
		$MainTemplate->set('HEAD', [
			'NAME' => $user_data['ATTR']['FULLNAME'],
			'DESC' => description($user_data['BODY']['HTML'](), Application::get('USER.DESCRIPTION_SIZE')),
			'PERM' => $User->getURL(),
			'OG_IMAGES' => $User->getFiles()
		]);

		# Get access to the current item data from main template
		$MainTemplate->set('TYPE', 'USER');
		$MainTemplate->set('USER', $user_data);

		echo $MainTemplate;
	}

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

#===============================================================================
# CATCH: User\Exception
#===============================================================================
catch(User\Exception $Exception) {
	try {
		if(Application::get('USER.SLUG_URLS') === FALSE) {
			$User = User\Factory::buildBySlug($param);
		} else {
			$User = User\Factory::build($param);
		}

		HTTP::redirect($User->getURL());
	}

	catch(User\Exception $Exception) {
		Application::error404();
	}
}