summaryrefslogtreecommitdiff
path: root/utils/modules/discography.php
blob: 2f4589d4c3efab70ef27f75416faee3313b635aa (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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
/* -*- Mode: php; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */

global $UTIL_DIR;

include_once($UTIL_DIR . "/convert.php");
include_once($UTIL_DIR . "/markdown.php");

function time2str($t)
{
	$min = sprintf("%d", floor($t / 60));
	$sec = $t % 60;
	return sprintf("%d:%02d", $min ,$sec);
}

function createNewlines($in)
{
	$str = "";
	$head = substr($in, 0, strpos($in, "\n"));
	$tail = substr($in, strpos($in, "\n")+1);
	$str .= "<div class=\"header\"><h2 class=\"lyrics_header\"><div class=\"header_text\">".$head."</div></h2></div>\n";
	$str .= str_replace("\n", "<br/>\n", $tail);
	return $str;
}

class Track {
	public $title;
	public $number;
	public $playtime;
	public $previewurl;
	public $lyrics;

	public function write($fp)
	{
		fwrite($fp, "      <track title=\"" .
					 htmlspecialchars($this->title, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "             number=\"" . $this->number . "\"\n");
		fwrite($fp, "             playtime=\"" . $this->playtime . "\"\n");
		fwrite($fp, "             previewurl=\"" . $this->previewurl . "\">\n");
		fwrite($fp, "        <lyrics>".htmlspecialchars($this->lyrics, ENT_QUOTES, "UTF-8")."</lyrics>\n");
		fwrite($fp, "      </track>\n");
	}

	public function showLyrics()
	{
		$str = "";
		if($this->lyrics) {
			$str .= createNewlines(htmlspecialchars_decode($this->lyrics));
		}
		return $str;
	}

	public function show($disc)
	{
		global $GLOBALS;

		$page = $GLOBALS["page"];
		$str = "";
		$str .= "      <div class=\"track\">\n";

		$str .= "        <span class=\"preview\">\n";
		if($this->previewurl) {
			$str .= "          <object type=\"application/x-shockwave-flash\" data=\"player_mp3_maxi.swf\" width=\"25\" height=\"12\">\n";
			$str .= "            <param name=\"movie\" value=\"player_mp3_maxi.swf\"/>\n"; 
			$str .= "            <param name=\"FlashVars\" value=\"mp3=".$this->previewurl."&amp;showslider=0&amp;width=25&amp;height=12&amp;skin=gfx/chicken.jpg\"/>\n"; 
			$str .= "          </object>\n";
		} else {
		}
		$str .= "        </span>\n";

		$str .= "        <span class=\"number\">".$this->number.". </span>\n";
		$str .= "        <span class=\"title\">".$this->title."</span>\n";
		$str .= "        <span class=\"playtime\">".time2str($this->playtime)."</span>\n";
		if($this->lyrics) {
			$str .= "      <a href=\"?page=".$page."&amp;lyrics=".$disc."&amp;track=".$this->number."\">Lyrics</a>\n"; 
		}
		$str .= "      </div>\n";

		return $str;
	}

	public function Track($title, $number, $playtime, $previewurl, $lyrics)
	{
		$this->title = $title;
		$this->number = $number;
		$this->playtime = $playtime;
		$this->previewurl = $previewurl;
		$this->lyrics = $lyrics;
	}
}

class Disc {
  public $title;
  public $releasetime;
  public $description;
  public $cover;
	public $releaser;
	public $tracks = array();

	public function write($fp)
	{
		fwrite($fp, "  <disc title=\"" .
					 htmlspecialchars($this->title, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "        releasetime=\"" . $this->releasetime . "\"\n");
		fwrite($fp, "        description=\"" .
					 htmlspecialchars($this->description, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "        cover=\"" . $this->cover . "\"\n");
		fwrite($fp, "        releaser=\"" . $this->releaser . "\">\n");
		fwrite($fp, "    <tracks>\n");
		if($this->tracks) {
			foreach($this->tracks as $track) {
				$track->write($fp);
			}
		}
		fwrite($fp, "    </tracks>\n");
		fwrite($fp, "  </disc>\n");
	}

	public function showLyrics($number)
	{
		$str = "";
 		if($this->tracks) {
			foreach($this->tracks as $track) {
				if($track->number == $number) {
					$str .= $track->showLyrics();
					break;
				}
			}
		}
		return $str;
	}

	public function show()
	{
		$str = "";

		$str .= "  <div class=\"disc\">\n"; 
		$str .= "    <span class=\"record_title\">".$this->title." (".date("Y", $this->releasetime).")</span>\n"; 
		$str .= "    <div class=\"cover\">\n"; 
		$str .= "      <a href=\"".$this->cover."\">\n";
		$str .= "        <img alt=\"".$this->title." cover\"\n";
		$str .= "             src=\"?mode=imagecache&amp;uri=" . $this->cover . "&amp;mod=discography&amp;cat=cover\"/>\n";
		$str .= "      </a>\n";
		$str .= "    </div>\n";
		$str .= "    <span class=\"label\">";		
		if($this->releasetime > time()) $str .= "To be";
		else $str .= "Was";
		$str .= " released by ".htmlspecialchars_decode($this->releaser)." on ".date("F jS Y", $this->releasetime)."</span>\n";
		$str .= "    <span class=\"tracklist_header\">";
		if($this->tracks && sizeof($this->tracks) > 1) {
			$str .= "Tracks:";
		} else {
			$str .= "Track:";
		}
		$str .= "</span>\n";
		$str .= "    <div class=\"tracklist\">\n";
		$total = 0;
 		if($this->tracks) {
			foreach($this->tracks as $track) {
				$str .= $track->show($this->releasetime);
				$total += $track->playtime;
			}
		}
		if($this->tracks && sizeof($this->tracks) > 1) {
			$str .= "      <span class=\"total_playtime\">Total playtime: ".time2str($total)."</span>\n";
		}
		$str .= "    </div>\n";
		$str .= "    <span class=\"description\">".Markdown(htmlspecialchars_decode($this->description))."</span>\n";
		$str .= "  </div>\n";

		return $str;
  }
	
	public function addTrack($track)
	{
		$key = $track->number;
		$this->tracks[$key] = $track;
	}

  public function Disc($title, $releasetime, $description, $cover, $releaser)
  {
    $this->title = $title;
    $this->releasetime = $releasetime;
    $this->description = $description;
    $this->cover = $cover;
    $this->releaser = $releaser;
  }
}

class Discography {

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

  // Admin config
  public $admin_title = "Discography";
  public $admin_submodules = array("Add Disc" => "add",
																	 "Edit Disc" => "edit",
																	 "Delete Disc" => "delete");
  public function admin($sub, $action, $vars)
  {
    switch($sub) {
    case "add":
      break;
    case "edit":
      break;
    case "delete":
      break;
    }
  }

  public function run($params)
  {
		global $GLOBALS;


		$str = "<div class=\"discography\">\n";

		$lyrics = $GLOBALS["lyrics"];
		$number = $GLOBALS["track"];

		//foreach($params as $param => $value) {}

		if($lyrics && $number) {
			if($this->discs) {
				foreach($this->discs as $disc) {
					if($disc->releasetime == $lyrics) {
						$str .= $disc->showLyrics($number);
						break;
					}
				}
			}
		} else {
			if($this->discs) {
				foreach($this->discs as $disc) {
					$str .= $disc->show();
				}
			}
		}

		$str .= "</div>\n";

		return $str;
  }

  public function add($disc) {
    $key = $disc->releasetime;
    $this->discs[$key] = $disc;
  }

	public function getImageSize($cat)
	{
		switch($cat) {
		case "cover":
			return new ImageSize($this->coverwidth, $this->coverheight);
		default:
			die(404);
		}
	}
	
  public function write()
  {
    $fp = fopen($this->file, "w");
    fwrite($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");

    fwrite($fp, "<discography>\n");
    foreach($this->discs as $disc) {
			$disc->write($fp);
    }
    fwrite($fp, "</discography>\n");

    fclose($fp);
  }
	
  private function read()
  {
    $dom = new DomDocument;
    $dom->preserveWhiteSpace = FALSE;
    $dom->load($this->file);

		$discography = $dom->documentElement;
		
		$this->coverwidth = $discography->getAttribute('coverwidth');
		$this->coverheight = $discography->getAttribute('coverheight');

    $discs = $dom->getElementsByTagName('disc');

    foreach($discs as $d) {
			$description = "";
			$dess = $d->getElementsByTagName('description');
			foreach($dess as $des) {
				$description = $des->textContent;
			}

      $disc = new Disc($d->getAttribute('title'),
											 $d->getAttribute('releasetime'),
											 $description,
											 $d->getAttribute('cover'),
											 $d->getAttribute('releaser'));

			$tracks = $d->getElementsByTagName('track');
			foreach($tracks as $t) {
				$lyrics = "";
				$ls = $t->getElementsByTagName('lyrics');
				foreach($ls as $l) {
					$lyrics = $l->textContent;
				}
				
				$track = new Track($t->getAttribute('title'),
													 $t->getAttribute('number'),
													 $t->getAttribute('playtime'),
													 $t->getAttribute('previewurl'),
													 $lyrics);

				$disc->addTrack($track);
			}

      $this->add($disc);
    }
  }

  public function Discography($file)
  {
    $this->file =  $file;
    if(file_exists($file)) $this->read();
  }

}

function discography_init()
{
  global $DATA_DIR;
  return new Discography($DATA_DIR . "/discography.xml");
}

?>