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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
|
<?php
/*
Copyright (c) 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 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.
*/
/**
* Tag Class
* This class hnadles all of the tag relation operations
*/
class Tag extends database_object {
public $id;
public $name;
// constructed
public $weight=0;
public $count=0;
public $owner=0;
/**
* constructor
* This takes a tag id and returns all of the relevent information
*/
public function __construct($id) {
if (!$id) { return false; }
$info = $this->get_info($id);
foreach ($info as $key=>$value) {
$this->$key = $value;
} // end foreach
} // constructor
/**
* construct_from_name
* This attempts to construct the tag from a name, rather then the ID
*/
public static function construct_from_name($name) {
$tag_id = self::tag_exists($name);
$tag = new Tag($tag_id);
return $tag;
} // construct_from_name
/**
* format
* This makes the tag presentable to the great humans that use this program, other life forms
* will just have to fend for themselves
*/
public function format($type=0,$object_id=0) {
if (!self::validate_type($type)) { return false; }
if ($type) {
$this->set_object($type,$object_id);
}
$size = 3 + ($this->weight-1) - ($this->count-1);
if ($size > 4) { $size = 4; }
if ($this->owner == $GLOBALS['user']->id) {
$action = '?page=tag&action=remove_tag&type=' . scrub_out($type) . '&tag_id=' . intval($this->id) . '&object_id=' . intval($object_id);
$class = "hover-remove ";
}
else {
$action = '?page=tag&action=add_tag&type=' . scrub_out($type) . '&tag_id=' . intval($this->id) . '&object_id=' . intval($object_id);
$class = "hover-add ";
}
$class .= 'tag_size' . $size;
$this->f_name = Ajax::text($action,$this->name,'modify_tag_' . $this->id . '_' . $object_id,'',$class);
} // format
/**
* set_object
* This assoicates the tag with a specified object, we try to get the data
* from the map cache, otherwise I guess we'll just have to look it up
*/
public function set_object($type,$object_id) {
if (parent::is_cached('tag_top_' . $type,$object_id)) {
$data = parent::get_from_cache('tag_top_' . $type,$object_id);
}
else {
$data = self::get_top_tags($type,$object_id);
}
$this->weight = $data[$this->id]['count'];
if (in_array($GLOBALS['user']->id,$data[$this->id]['users'])) {
$this->owner = $GLOBALS['user']->id;
}
$this->count = count($data);
} // set_object
/**
* build_cache
* This takes an array of object ids and caches all of their information
* in a single query, cuts down on the connections
*/
public static function build_cache($ids) {
if (!is_array($ids) OR !count($ids)) { return false; }
$idlist = '(' . implode(',',$ids) . ')';
$sql = "SELECT * FROM `tag` WHERE `id` IN $idlist";
$db_results = Dba::read($sql);
while ($row = Dba::fetch_assoc($db_results)) {
parent::add_to_cache('tag',$row['id'],$row);
}
return true;
} // build_cache
/**
* build_map_cache
* This builds a cache of the mappings for the specified object, no limit is given
*/
public static function build_map_cache($type,$ids) {
if (!is_array($ids) OR !count($ids)) { return false; }
$type = self::validate_type($type);
$idlist = '(' . implode(',',$ids) . ')';
$sql = "SELECT `tag_map`.`tag_id`,`tag_map`.`object_id`,`tag_map`.`user` FROM `tag_map` " .
"WHERE `tag_map`.`object_type`='$type' AND `tag_map`.`object_id` IN $idlist ";
$db_results = Dba::query($sql);
$tags = array();
while ($row = Dba::fetch_assoc($db_results)) {
$tags[$row['object_id']][$row['tag_id']]['users'][] = $row['user'];
$tags[$row['object_id']][$row['tag_id']]['count']++;
}
// Run through our origional ids as we want to cache NULL results
foreach ($ids as $id) {
parent::add_to_cache('tag_top_' . $type,$id,$tags[$id]);
}
return true;
} // build_map_cache
/**
* add
* This is a wrapper function, it figures out what we need to add, be it a tag
* and map, or just the mapping
*/
public static function add($type,$id,$value,$user='') {
// Validate the tag type
if (!self::validate_type($type)) { return false; }
if (!is_numeric($id)) { return false; }
$cleaned_value = self::clean_tag($value);
if (!strlen($cleaned_value)) { return false; }
$uid = ($user == '') ? intval($user) : intval($GLOBALS['user']->id);
// Check and see if the tag exists, if not create it, we need the tag id from this
if (!$tag_id = self::tag_exists($cleaned_value)) {
$tag_id = self::add_tag($cleaned_value);
}
if (!$tag_id) {
debug_event('Error','Error unable to create tag value:' . $cleaned_value . ' unknown error','1');
return false;
}
// We've got the tag id, let's see if it's already got a map, if not then create the map and return the value
if (!$map_id = self::tag_map_exists($type,$id,$tag_id,$user)) {
$map_id = self::add_tag_map($type,$id,$tag_id,$user);
}
return $map_id;
} // add
/**
* add_tag
* This function adds a new tag, for now we're going to limit the tagging a bit
*/
public static function add_tag($value) {
// Clean it up and make it tagish
$value = self::clean_tag($value);
if (!strlen($value)) { return false; }
$value = Dba::escape($value);
$sql = "REPLACE INTO `tag` SET `name`='$value'";
$db_results = Dba::write($sql);
$insert_id = Dba::insert_id();
parent::add_to_cache('tag_name',$value,$insert_id);
return $insert_id;
} // add_tag
/**
* add_tag_map
* This adds a specific tag to the map for specified object
*/
public static function add_tag_map($type,$object_id,$tag_id,$user='') {
$uid = ($user == '') ? intval($GLOBALS['user']->id) : intval($user);
$tag_id = intval($tag_id);
if (!self::validate_type($type)) { return false; }
$id = intval($object_id);
if (!$tag_id || !$id) { return false; }
$sql = "INSERT INTO `tag_map` (`tag_id`,`user`,`object_type`,`object_id`) " .
"VALUES ('$tag_id','$uid','$type','$id')";
$db_results = Dba::write($sql);
$insert_id = Dba::insert_id();
parent::add_to_cache('tag_map_' . $type,$insert_id,array('tag_id'=>$tag_id,'user'=>$uid,'object_type'=>$type,'object_id'=>$id));
return $insert_id;
} // add_tag_map
/**
* tag_exists
* This checks to see if a tag exists, this has nothing to do with objects or maps
*/
public static function tag_exists($value) {
if (parent::is_cached('tag_name',$value)) {
return parent::get_from_cache('tag_name',$value);
}
$value = Dba::escape($value);
$sql = "SELECT * FROM `tag` WHERE `name`='$value'";
$db_results = Dba::read($sql);
$results = Dba::fetch_assoc($db_results);
parent::add_to_cache('tag_name',$results['name'],$results['id']);
return $results['id'];
} // tag_exists
/**
* tag_map_exists
* This looks to see if the current mapping of the current object of the current tag of the current
* user exists, lots of currents... taste good in scones.
*/
public static function tag_map_exists($type,$object_id,$tag_id,$user) {
if (!self::validate_type($type)) { return false; }
if (parent::is_cached('tag_map_' . $type,$object_id)) {
$data = parent::get_from_cache('tag_map_' . $type,$object_id);
return $data['id'];
}
$object_id = Dba::escape($object_id);
$tag_id = Dba::escape($tag_id);
$user = Dba::escape($user);
$type = Dba::escape($type);
$sql = "SELECT * FROM `tag_map` WHERE `tag_id`='$tag_id' AND `user`='$user' AND `object_id`='$object_id' AND `object_type`='$type'";
$db_results = Dba::read($sql);
$results = Dba::fetch_assoc($db_results);
parent::add_to_cache('tag_map_' . $type,$results['id'],$results);
return $results['id'];
} // tag_map_exists
/**
* get_top_tags
* This gets the top tags for the specified object using limit
*/
public static function get_top_tags($type,$object_id,$limit='10') {
if (!self::validate_type($type)) { return false; }
if (parent::is_cached('tag_top_' . $type,$object_id)) {
return parent::get_from_cache('tag_top_' . $type,$object_id);
}
$object_id = intval($object_id);
$limit = intval($limit);
$sql = "SELECT `tag_map`.`tag_id`,`tag_map`.`user` FROM `tag_map` " .
"WHERE `tag_map`.`object_type`='$type' AND `tag_map`.`object_id`='$object_id' " .
"LIMIT $limit";
$db_results = Dba::query($sql);
$results = array();
while ($row = Dba::fetch_assoc($db_results)) {
$results[$row['tag_id']]['users'][] = $row['user'];
$results[$row['tag_id']]['count']++;
}
parent::add_to_cache('tag_top_' . $type,$object_id,$results);
return $results;
} // get_top_tags
/**
* get_object_tags
* Display all tags that apply to maching target type of the specified id
*/
public static function get_object_tags($type, $id) {
if (!self::validate_type($type)) { return array(); }
$sql = "SELECT DISTINCT `tag_map`.`id`, `tag`.`name`, `tag_map`.`user` FROM `tag` " .
"LEFT JOIN `tag_map` ON `tag_map`.`id`=`tag`.`map_id` " .
"LEFT JOIN `$type` ON `$type`.`id`=`tag_map`.`object_id` " .
"WHERE `tag_map`.`object_type`='$type'";
$results = array();
$db_results = Dba::query($sql);
while ($row = Dba::fetch_assoc($db_results)) {
$results[] = $row;
}
return $results;
} // get_object_tags
/**
* filter_with_prefs
* This filters the tags based on the users preference
*/
public static function filter_with_prefs($l) {
$colors = array('#0000FF',
'#00FF00', '#FFFF00', '#00FFFF','#FF00FF','#FF0000');
$prefs = 'tag company';
// $prefs = Config::get('tags_userlist');
$ulist = explode(' ', $prefs);
$req = '';
foreach($ulist as $i) {
$req .= "'" . Dba::escape($i) . "',";
}
$req = rtrim($req, ',');
$sql = 'SELECT `id`,`username` FROM `user` WHERE ';
if ($prefs=='all') {
$sql .= '1';
}
else {
$sql .= 'username in ('.$req.')';
}
$db_results = Dba::query($sql);
$uids=array();
$usernames = array();
$p = 0;
while ($r = Dba::fetch_assoc($db_results)) {
$usernames[$r['id']] = $r['username'];
$uids[$r['id']] = $colors[$p];
$p++;
if ($p == sizeof($colors)) {
$p = 0;
}
}
$res = array();
foreach ($l as $i) {
if ($GLOBALS['user']->id == $i['user']) {
$res[] = $i;
}
elseif (isset($uids[$i['user']])) {
$i['color'] = $uids[$i['user']];
$i['username'] = $usernames[$i['user']];
$res[] = $i;
}
}
return $res;
} // filter_with_prefs
/**
* remove_map
* This will only remove tag maps for the current user
*/
public function remove_map($type,$object_id) {
if (!self::validate_type($type)) { return false; }
$type = Dba::escape($type);
$tag_id = Dba::escape($this->id);
$object_id = Dba::escape($object_id);
$user_id = Dba::escape($GLOBALS['user']->id);
$sql = "DELETE FROM `tag_map` WHERE `tag_id`='$tag_id' AND `object_type`='$type' AND `object_id`='$object_id' AND `user`='$user_id'";
$db_results = Dba::write($sql);
return true;
} // remove_map
/**
* validate_type
* This validates the type of the object the user wants to tag, we limit this to types
* we currently support
*/
public static function validate_type($type) {
$valid_array = array('song','artist','album');
if (in_array($type,$valid_array)) { return $type; }
return false;
} // validate_type
/**
* clean_tag
* This takes a string and makes it Tagish
*/
public static function clean_tag($value) {
$tag = preg_replace("/[^\w\_\-\s\&]/","",$value);
return $tag;
} // clean_tag
} // end of Tag class
?>
|