forked from realslacker/Downloads-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DownloadTagConnection.php
105 lines (88 loc) · 3.47 KB
/
DownloadTagConnection.php
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
<?php
/*
* Downloads Plugin for WolfCMS <http://www.wolfcms.org>
* Copyright (C) 2011 Shannon Brooks <[email protected]>
*
* This file is part of Downloads Plugin. Downloads Plugin is licensed under the GNU GPLv3 license.
* Please see license.txt for the full license text.
*/
// Security Measure
if (!defined('IN_CMS')) { exit(); }
// DownloadTagConnection class represents a DownloadTagConnection record
class DownloadTagConnection extends Record {
const TABLE_NAME = 'downloads_tags';
// search function to perform query
public static function find($args = array()) {
// Collect attributes...
$where = isset($args['where']) ? trim($args['where']) : '1';
$order_by = isset($args['order']) ? trim($args['order']) : 'downloads_tags.download_id ASC';
$offset = isset($args['offset']) ? (int)$args['offset'] : 0;
$limit = isset($args['limit']) ? (int)$args['limit'] : 0;
$group_by = isset($args['group']) ? trim($args['group']) : '';
$having = isset($args['having']) ? trim($args['having']) : '';
// Prepare query parts
$order_by_string = empty($order_by) ? '' : "ORDER BY $order_by";
$group_by_string = empty($group_by) ? '' : "GROUP BY $group_by";
$having_string = empty($having) ? '' : "HAVING $having";
$limit_string = $limit > 0 ? "LIMIT $limit" : '';
$offset_string = $offset > 0 ? "OFFSET $offset" : '';
$tablename = self::tableNameFromClassName('DownloadTagConnection');
$tablename_downloads = self::tableNameFromClassName('Download');
$tablename_tags = self::tableNameFromClassName('DownloadTag');
// Prepare SQL
$sql = "SELECT downloads_tags.*, downloads.*, downloadtags.name AS tag FROM $tablename AS downloads_tags "
. "LEFT JOIN $tablename_downloads AS downloads ON downloads.id=downloads_tags.download_id "
. "LEFT JOIN $tablename_tags AS downloadtags ON downloadtags.id=downloads_tags.tag_id "
. "WHERE $where $group_by_string $having_string $order_by_string $limit_string $offset_string";
$stmt = self::$__CONN__->prepare($sql);
$stmt->execute();
// Run!
if ($limit == 1) {
return $stmt->fetchObject('DownloadTagConnection');
} else {
$objects = array();
while ($object = $stmt->fetchObject('DownloadTagConnection'))
$objects[] = $object;
return $objects;
}
} //*/
// find all records
public static function findAll($args = array()) {
return self::find($args);
} //*/
// find a specific record by it's download_id
public static function findAllByDownloadId($id) {
return self::find(array(
'where' => 'downloads_tags.download_id=' . Record::escape((int)$id)
));
} //*/
// find a specific record by it's tag_id
public static function findAllByTagId($id) {
return self::find(array(
'where' => 'downloads_tags.tag_id=' . Record::escape((int)$id)
));
} //*/
/* find all records by matching the tag name
public static function findAllByTagName($name) {
return self::find(array(
'where' => 'downloadtags.name=' . Record::escape($name),
'order' => 'downloads.name ASC'
));
} //*/
// find all records with matching tags
public static function findAllByTagName($tags=array()) {
$tags = is_array($tags) ? $tags : explode(',',$tags);
$where = '';
$count = 0;
foreach ($tags as $tag) if (! empty($tag)) {
$where .= (!empty($where) ? ',' : '') . Record::escape($tag);
$count++;
}
return self::find(array(
'where' => "downloadtags.name IN ($where)",
'order' => 'downloads.name ASC',
'group' => 'downloads.id',
'having' => "COUNT(*)>=$count"
));
} //*/
} // end Banner class