summaryrefslogtreecommitdiffstats
path: root/lib/class/shoutbox.class.php
blob: ce85813dfe3d9fee68d6e7595109d31809e302e8 (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
<?php
/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */
/**
 *
 * LICENSE: GNU General Public License, version 2 (GPLv2)
 * Copyright 2001 - 2013 Ampache.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License v2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

class shoutBox {

	public $id;

	/**
	 * Constructor
	 * This pulls the shoutbox information from the database and returns
	 * a constructed object, uses user_shout table
	 */
	public function __construct($shout_id) {

		// Load the data from the database
		$this->_get_info($shout_id);

		return true;

	} // Constructor

	/**
	 * _get_info
	 * does the db call, reads from the user_shout table
	 */
	private function _get_info($shout_id) {

		$sticky_id = Dba::escape($shout_id);

		$sql = "SELECT * FROM `user_shout` WHERE `id`='$shout_id'";
		$db_results = Dba::read($sql);

		$data = Dba::fetch_assoc($db_results);

		foreach ($data as $key=>$value) {
			$this->$key = $value;
		}

		return true;

	} // _get_info

	/**
	 * get_top
	 * This returns the top user_shouts, shoutbox objects are always shown regardless and count against the total
	 * number of objects shown
	 */
	public static function get_top($limit) {

		$shouts = self::get_sticky();

		// If we've already got too many stop here
		if (count($shouts) > $limit) {
			$shouts = array_slice($shouts,0,$limit);
			return $shouts;
		}

		// Only get as many as we need
		$limit = intval($limit) - count($shouts);
		$sql = "SELECT * FROM `user_shout` WHERE `sticky`='0' ORDER BY `date` DESC LIMIT $limit";
		$db_results = Dba::read($sql);

		while ($row = Dba::fetch_assoc($db_results)) {
			$shouts[] = $row['id'];
		}

		return $shouts;

	} // get_top

	/**
	 * get_sticky
	 * This returns all current sticky shoutbox items
	 */
	public static function get_sticky() {

		$sql = "SELECT * FROM `user_shout` WHERE `sticky`='1' ORDER BY `date` DESC";
		$db_results = Dba::read($sql);

		$results = array();

		while ($row = Dba::fetch_assoc($db_results)) {
			$results[] = $row['id'];
		}

		return $results;

	} // get_sticky

	/**
	 * get_object
	 * This takes a type and an ID and returns a created object
	 */
	public static function get_object($type,$object_id) {

		$allowed_objects = array('song','genre','album','artist','radio');

		if (!in_array($type,$allowed_objects)) {
			return false;
		}

		$object = new $type($object_id);

		return $object;

	} // get_object

	/**
	 * get_image
	 * This returns an image tag if the type of object we're currently rolling with
	 * has an image associated with it
	 */
	public function get_image() {

		switch ($this->object_type) {
			case 'album':
				$image_string = "<img class=\"shoutboximage\" height=\"75\" width=\"75\" src=\"" . Config::get('web_path') . "/image.php?id=" . $this->object_id . "&amp;thumb=1\" />";
			break;
			case 'artist':

			break;
			case 'song':
				$song = new Song($this->object_id);
				$image_string = "<img class=\"shoutboximage\" height=\"75\" width=\"75\" src=\"" . Config::get('web_path') . "/image.php?id=" . $song->album . "&amp;thumb=1\" />";
			break;
			default:
				// Rien a faire
			break;
		} // end switch

		return $image_string;

	} // get_image

	/**
	 * create
	 * This takes a key'd array of data as input and inserts a new shoutbox entry, it returns the auto_inc id
	 */
	public static function create($data) {

		$user 		= Dba::escape($GLOBALS['user']->id);
		$text 		= Dba::escape(strip_tags($data['comment']));
		$date 		= time();
		$sticky 	= isset($data['sticky']) ? 1 : 0;
		$object_id 	= Dba::escape($data['object_id']);
		$object_type	= Dba::escape($data['object_type']);

		$sql = "INSERT INTO `user_shout` (`user`,`date`,`text`,`sticky`,`object_id`,`object_type`) " .
			"VALUES ('$user','$date','$text','$sticky','$object_id','$object_type')";
		$db_results = Dba::write($sql);

		$insert_id = Dba::insert_id();

		return $insert_id;

	} // create

	/**
	 * update
	 * This takes a key'd array of data as input and updates a shoutbox entry
	 */
	public static function update($data) {

		$id		= Dba::escape($data['shout_id']);
		$text 		= Dba::escape(strip_tags($data['comment']));
		$sticky 	= make_bool($data['sticky']);

		$sql = "UPDATE `user_shout` SET `text`='$text', `sticky`='$sticky' WHERE `id`='$id'";
		$db_results = Dba::write($sql);

		return true;

	} // create

	/**
	 * format
	 * this function takes the object and reformats some values
	 */

	public function format() {
		$this->sticky = ($this->sticky == "0") ? 'No' : 'Yes';
		$this->date = date("m\/d\/Y - H:i", $this->date);
		return true;

	} //format

	/**
	 * delete
	 * this function deletes a specific shoutbox entry
	 */

	public function delete($shout_id) {

		// Delete the shoutbox post
		$shout_id = Dba::escape($shout_id);
		$sql = "DELETE FROM `user_shout` WHERE `id`='$shout_id'";
		$db_results = Dba::write($sql);

	} // delete

} // shoutBox class
?>