summaryrefslogtreecommitdiff
path: root/utils/news.php
blob: 809e8dca52c6fbe366311445c2eb35a1349674dd (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
103
<?php

include_once("convert.php");

class NewsEntry {
	public $title;
	public $time;
	public $description;
	public $category;

	public function show()
	{
		echo "<div class=\"news_entry\">\n";
		echo "  <div class=\"news_title\">" .
			htmlspecialchars_decode($this->title, ENT_QUOTES) . "</div>\n";
		echo "  <div class=\"news_time\">" . date("D M jS Y G:i", $this->time) . "</div>\n";
		echo "  <div class=\"news_description\">" .
			htmlspecialchars_decode($this->description, ENT_QUOTES) . "</div>\n";
		echo "</div>\n";
	}

	public function NewsEntry($title, $time, $category, $description)
	{
		$this->title = $title;
		$this->time = $time;
		$this->category = $category;
		$this->description = $description;
	}
}

class News {

	private $file;
	private $news = array();

	public function show($number, $category)
	{
		// If number is -1 show all shows.
		if($number == -1) $number = 100000;
		
		foreach($this->news as $newsentry) {
			if($newsentry->category == $category || $category == "all") {
				$newsentry->show();
				$number--;
			}
			if(!$number) return;
		}
	}

	public function add($newsentry) {
		$key = $newsentry->time;
		$this->news[$key] = $newsentry;
	}
	
	public function write()
	{
		$fp = fopen($this->file, "w");
		fwrite($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");

		fwrite($fp, "<news>\n");
		foreach($this->news as $newsentry) {
			fwrite($fp, "  <newsentry title=\"" .
						 htmlspecialchars($newsentry->title, ENT_QUOTES, "UTF-8") . "\"\n");
			fwrite($fp, "             time=\"" . $newsentry->time . "\"\n");
			fwrite($fp, "             category=\"" . $newsentry->category . "\"\n");
			fwrite($fp, "             description=\"" .
						 htmlspecialchars($newsentry->description, ENT_QUOTES, "UTF-8") . "\">\n");
			fwrite($fp, "  </newsentry>\n");
		}
		fwrite($fp, "</news>\n");

		fclose($fp);
	}
	
	private function read()
	{

		$dom = new DomDocument;
		$dom->preserveWhiteSpace = FALSE;
		$dom->load($this->file);
		$params = $dom->getElementsByTagName('newsentry');

		foreach ($params as $param) {
			$newsentry = new NewsEntry($param->getAttribute('title'),
																 $param->getAttribute('time'),
																 $param->getAttribute('category'),
																 $param->getAttribute('description'));
			$this->add($newsentry);
		}
		
		// Key sort
		krsort($this->news);
	}

	public function News($file)
	{
		$this->file =  $file;
		$this->read();
	}

}

?>