summaryrefslogtreecommitdiffstats
path: root/lib/class/flag.class.php
blob: 0e86f6438fe3c0dd725dd3ce1e067b096ec3be96 (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
<?php
/*

 Copyright 2001 - 2006 Ampache.org
 All Rights Reserved

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 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. 

*/

/**
 * Flag Class
 * This handles flagging of songs, albums and artists	
 */
class Flag {

	/* DB based variables */
	var $id; 
	var $user;
	var $object_id;
	var $object_type;
	var $comment;
	var $flag;
	var $date;
	var $approved=0;

	/* Generated Values */
	var $name; // Blank
	var $title; // Blank

	/**
	 * Constructor
	 * This takes a flagged.id and then pulls in the information for said flag entry
	 */
	function Flag($flag_id=0) { 

		$this->id = intval($flag_id);

		if (!$this->id) { return false; }

		$info = $this->_get_info();

		$this->user		= $info['user'];
		$this->object_id	= $info['object_id'];
		$this->object_type	= $info['object_type'];
		$this->comment		= $info['comment'];
		$this->flag		= $info['flag'];
		$this->date		= $info['date'];
		$this->approved		= $info['approved'];

		return true;

	} // flag

	/**
	 * _get_info
	 * Private function for getting the information for this object from the database 
	 */
	function _get_info() { 

		$id = sql_escape($this->id);

		$sql = "SELECT * FROM flagged WHERE id='$id'";
		$db_results = mysql_query($sql, dbh());

		$results = mysql_fetch_assoc($db_results);

		return $results;

	} // _get_info

	/**
	 * get_recent
	 * This returns the id's of the most recently flagged songs, it takes an int
	 * as an argument which is the count of the object you want to return
	 */
	function get_recent($count=0) { 

		if ($count) { $limit = " LIMIT " . intval($count);  } 

		$results = array();

		$sql = "SELECT id FROM flagged ORDER BY date " . $limit;
		$db_results = mysql_query($sql, dbh());

		while ($r = mysql_fetch_assoc($db_results)) { 
			$results[] = $r['id'];
		}
		
		return $results;

	} // get_recent

	/**
	 * get_total
	 * Returns the total number of flagged objects
	 */
	function get_total() { 

		$sql = "SELECT COUNT(id) FROM flagged";
		$db_results = mysql_query($sql, dbh());

		$results = mysql_fetch_row($db_results);

		return $results['0'];

	} // get_total

	/**
	 * get_flagged
	 * This returns an array of ids of flagged songs if no limit is passed
	 * it gets everything
	 */
	function get_flagged($count=0) { 

		if ($count) { $limit_clause = "LIMIT " . intval($count); } 
		
		$sql = "SELECT id FROM flagged ORDER BY id $limit_clause";
		$db_results = mysql_query($sql, dbh());

		/* Default it to an array */
		$results = array();

		/* While the query */
		while ($r = mysql_fetch_assoc($db_results)) { 
			$results[] = $r['id'];
		}

		return $results;

	} // get_flagged

	/**
	 * get_approved
	 * This returns an array of approved flagged songs
	 */
	function get_approved() { 

		$sql = "SELECT id FROM flagged WHERE approved='1'";
		$db_results = mysql_query($sql,dbh()); 


		/* Default the results array */
		$results = array(); 

		/* While it */
		while ($r = mysql_fetch_assoc($db_results)) { 
			$results[] = $r['id'];
		}

		return $results;

	} // get_approved

	/**
	 * add
	 * This adds a flag entry for an item, it takes an id, a type, the flag type
	 * and a comment and then inserts the mofo
	 */
	function add($id,$type,$flag,$comment) { 
	
		$id 		= sql_escape($id);
		$type		= sql_escape($type);
		$flag		= sql_escape($flag);
		$comment	= sql_escape($comment);
		$time		= time();
		$approved	= '0';

		/* If they are an admin, it's auto approved */
		if ($GLOBALS['user']->has_access('100')) { $approved = '1'; } 

		$sql = "INSERT INTO flagged (`object_id`,`object_type`,`flag`,`comment`,`date`,`approved`) VALUES " . 
			" ('$id','$type','$flag','$comment','$time','$approved')";
		$db_results = mysql_query($sql, dbh());

		return true;

	} // add

	/**
	 * delete_flag
	 * This deletes the flagged entry and rescans the file to revert to the origional
	 * state, in a perfect world, I could just roll the changes back... not until 3.4
	 */
	function delete_flag() { 

		$sql = "DELETE FROM flagged WHERE id='$this->id'";
		$db_results = mysql_query($sql, dbh());

		return true;

	} // delete_flag

	/**
	 * approve
	 * This approves the current flag object ($this->id) by setting approved to
	 * 1
	 */
	 function approve() { 

		$sql = "UPDATE flagged SET approved='1' WHERE id='$this->id'";
		$db_results = mysql_query($sql, dbh());

		return true;
	
	 } // approve

	/**
	 * format_name
	 * This function formats and sets the $this->name variable and $this->title 
	 */
	function format_name() { 

		switch ($this->object_type) { 
			case 'song':
				$song = new Song($this->object_id);
				$song->format();
				$name 	= $song->f_title . " - " . $song->f_artist;
				$title	= $song->title . " - " . $song->get_artist_name();
			break;
			default: 
			
			break;
		} // end switch on object type

		$this->title = $title; 
		$this->name = $name;

	} // format_name()
	
	/**
	 * print_name
	 * This function formats and prints out a userfriendly name of the flagged
	 * object
	 */
	function print_name() { 

		$this->format_name();
		echo "<span title=\"" . $this->title . "\">" . $this->name . "</span>";

	} // print_name


	/**
	 * print_status
	 * This prints out a userfriendly version of the current status for this flagged
	 * object
	 */
	function print_status() { 

		if ($this->approved) { echo _('Approved'); }
		else { echo _('Pending'); }

	} // print_status

	/**
	 * print_flag
	 * This prints out a userfriendly version of the current flag type
	 */
	function print_flag() { 

		switch ($this->flag) { 
			case 'delete':
				$name = _('Delete');
			break;
			case 'retag':
				$name = _('Re-Tag'); 
			break;
			case 'reencode':
				$name = _('Re-encode');
			break;
			case 'other':
				$name = _('Other'); 
			break;
			default:
				$name = _('Unknown');
			break;
		} // end switch

		echo $name;
		
	} // print_flag

} //end of flag class

?>