summaryrefslogtreecommitdiffstats
path: root/modules/admin.inc.php
blob: b74b5ef5edc0b51b436b62c3e4bf1aafcd39ab86 (plain) (blame)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php

class Page_Admin extends Page
{

	private $template = false;
	private $files = false;
	private $table = false;
	private $tags = false;
	
	/**
	 * Implementation of the abstract doPreprocess function
	 *
	 * Checks if the user is logged in and processes any
	 * action if one was specified in the request.
	 *
	 */
	protected function doPreprocess()
	{
		// load user, we will need it later
		User::load();
		
		// only admins should be able to access the administration page
		if (!User::hasPermission('superadmin')) {
			Message::addError('no-permission');
			Util::redirect('?do=Main');
		}
		
		if(Request::any('template')){
			$this->template = Request::any('template');
		}
		
	}

	/**
	 * Implementation of the abstract doRender function
	 *
	 * Fetch the list of news from the database and paginate it.
	 *
	 */
	protected function doRender()
	{
		if(!$this->template || !$this->templateAnalysis($this->template)){
			$this->initTable();
			Render::addTemplate('administration/_page', array(
				'token' => Session::get('token'),
				'adminMessage' => $this->message,
				'table' => $this->table
			));
		}else{
			Render::addTemplate('administration/template', array(
				'template' => $this->template,
				'tags' => $this->tags
			));
		}
		

	}
	
	private function initTable(){
		$this->listTemplates();
		$de = $this->listJson('de/');
		$en = $this->listJson('en/');
		$pt = $this->listJson('pt/');
		
		foreach($this->files as $key => $value){
			
			$this->table[] = array(
			'template' => $value,
			'link' => $key,
			'de' => $this->checkJson($de[$key],'de'),
			'en' => $this->checkJson($en[$key],'en'),
			'pt' => $this->checkJson($pt[$key],'pt')
			);
		}
		
	}
	
	private function listTemplates(){
		$this->files = array();
		$dir = 'templates/';
		$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
		foreach($objects as $name => $object){
			if(array_pop(explode('.',$name)) === 'html'){
				$key = str_replace($dir, '', $name);
				$key = str_replace('.html', '', $key);
		    		$this->files[$key] = $name;
		    	}
		}
	}
	
	private function listJson($lang){
		$json = array();
		$dir = 'lang/' . $lang;
		$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
		foreach($objects as $name => $object){
			if(array_pop(explode('.',$name)) === 'json'){
				$key = str_replace($dir, '', $name);
				$key = str_replace('.json', '', $key);
		    		$json[$key] = $key;
		    	}
		}
		return $json;
	}
	
	private function checkJson($path,$lang){
		if($path){
			$htmlTemplate = file_get_contents('templates/' . $path . '.html');
			$json = Dictionary::getArrayTemplate($path,$lang);
			$htmlCount = substr_count($htmlTemplate, 'lang_');
			$matchCount = 0;
			
			foreach($json as $key => $value){
				if($key != 'lang'){
					$key = $key . '}}';
					$matchCount += substr_count($htmlTemplate, $key);
				}
			}
			
			$diff = $htmlCount - $matchCount;
			
			//allright
			if($diff == 0) return "OK";
			if($diff > 0) return $diff . " JSON tag(s) are missing";
			if($diff < 0) return ($diff * -1) . " extra JSON tag(s)";
		}else{
			return "JSON file is missing";
		}
		
	}
	
	private function templateAnalysis($path){
		if(!file_exists('templates/' . $path . '.html')){
			Message::addError('invalid-template');
			return false;
		}
		$htmlTemplate = file_get_contents('templates/' . $path . '.html');
		preg_match_all('/{{lang_(.*?)}}/s', $htmlTemplate, $matches);
		
		$tags = $matches[1];
		
		foreach($tags as $tag){
			$this->tags[] = array(
				'tag' => 'lang_' . $tag,
				'de' => $this->checkJsonTag($path,$tag,'de/'),
				'en' => $this->checkJsonTag($path,$tag,'en/'),
				'pt' => $this->checkJsonTag($path,$tag,'pt/')
			);
		}
		
		return true;
	}
	
	private function checkJsonTag($path,$tag,$lang){
		if($json = Dictionary::getArrayTemplate($path,$lang)){
			return $json['lang_' . $tag];
		}
		return '';	
	}
}