-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblems.php
85 lines (62 loc) · 2.78 KB
/
problems.php
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
<?php
require_once 'autoload.php';
use ProjectEuler\Main\Site;
use ProjectEuler\Main\Config;
use ProjectEuler\Content\ProblemsContent;
use ProjectEuler\Content\ProblemsHelper;
use ProjectEuler\Renderers\ProblemsRenderer;
use ProjectEuler\Renderers\Paginator;
use ProjectEuler\Renderers\PaginationRenderer;
$site = new Site();
$problemsContent = new ProblemsContent();
$helper = new ProblemsHelper($problemsContent, $site);
$renderer = new ProblemsRenderer();
$paginationRenderer = new PaginationRenderer();
$page = $_GET['page'];
if (! $helper->isValidGET()) {
$rebuiltGET = $helper->rebuildGET($page);
header("Location: problems.php?$rebuiltGET");
exit();
}
$tagsFromDB = $problemsContent->getTags();
$tagCount = count($tagsFromDB);
$maxProblemsPerPage = Config::getValue('problems.max_per_page');
$paginationWidth = Config::getValue('problems.pagination_width');
for ($i = 0; $i < $tagCount; $i++) {
$checked = isset($_GET['tag' . $tagsFromDB[$i]['id']]);
$tagsFromDB[$i]['checked'] = $checked;
}
$cleanTags = $helper->getCleanTags();
if (count($cleanTags) == 0) {
$problems = $problemsContent->getProblems($page);
$problemsCount = $problemsContent->getProblemsCount();
} else {
$problems = $problemsContent->getFilteredProblems($cleanTags, $page);
$problemsCount = $problemsContent->getFilteredProblemsCount($cleanTags);
}
if (count($problems) == 0) {
$rebuiltGET = $helper->rebuildGET(1);
header("Location: problems.php?$rebuiltGET");
exit();
}
$paginator = new Paginator($problemsCount, $maxProblemsPerPage, $page, $paginationWidth);
$pages = $paginator->getPagination();
$problemsTpl = file_get_contents('templates/problems/problems.tpl');
$tagTpl = file_get_contents('templates/problems/tags_element.tpl');
$translatedTpl = file_get_contents('templates/problems/problem_translated.tpl');
$notTranslatedTpl = file_get_contents('templates/problems/problem_not_translated.tpl');
$paginationTpl = file_get_contents('templates/problems/pagination.tpl');
$currentLinkTpl = file_get_contents('templates/problems/pagination_element_current.tpl');
$pageLinkTpl = file_get_contents('templates/problems/pagination_element_foreign.tpl');
$renderedTags = $renderer->renderTagsTable($tagTpl, $tagsFromDB);
$renderedProblems = $renderer->renderProblems($translatedTpl, $notTranslatedTpl, $problems);
$renderedPagination = $paginationRenderer->render($paginationTpl, $currentLinkTpl, $pageLinkTpl, $pages, $cleanTags);
$content = array(
'rendered_tags' => $renderedTags,
'rendered_problems' => $renderedProblems,
'rendered_pagination' => $renderedPagination
);
$finalContent = $renderer->renderProblemsPage($problemsTpl, $content);
$htmlout = $site->getFullPageTemplate('problems.php');
$htmlout = str_replace('{page_content}', $finalContent, $htmlout);
echo $htmlout;