This repository has been archived by the owner on Nov 28, 2018. It is now read-only.
/
tagger.php
117 lines (92 loc) · 3.06 KB
/
tagger.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
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* Tagger Plugin for Wolf CMS <http://thehub.silentworks.co.uk/plugins/frog-cms/tagger.html>
* Alternate Mirror site <http://www.tbeckett.net/articles/plugins/tagger.xhtml>
* Copyright (C) 2008 - 2011 Andrew Smith <a.smith@silentworks.co.uk>
* Copyright (C) 2008 - 2011 Tyler Beckett <tyler@tbeckett.net>
*
* Dual licensed under the MIT (license/mit-license.txt)
* and GPL (license/gpl-license.txt) licenses.
*/
class Tagger
{
public function __construct(&$page, $params)
{
$this->page =& $page;
$this->params = $params;
switch(count($params))
{
case 0: break;
case 1:
$this->pagesByTag($params);
break;
default:
page_not_found();
}
}
public function tag($params = false)
{
if(!$params) $params = $this->params;
return $params[0];
}
/**
* Retrieve the original tag name
*/
public function tagName($params = false)
{
if (!$params) $params = $this->params;
// Prepare the query parameters
$tag = $params[0];
$tag_unslugified = unslugify($params[0]);
$sql = "SELECT name FROM " . TABLE_PREFIX . "tag AS tag WHERE tag.name = '$tag' OR tag.name = '$tag_unslugified'";
// Retrieve the original tag name
$stmt = Record::getConnection()->prepare($sql);
$stmt->execute();
if ($obj = $stmt->fetchObject())
$tag = $obj->name;
return $tag;
}
public function pagesByTag($params = false)
{
$pdoConn = Record::getConnection();
if(!$params) $params = $this->params;
$pages = array();
$tag_unslugified = unslugify(isset($params[0]) ? $params[0] : NULL);
$tag = isset($params[0]) ? $params[0] : NULL;
$where = " WHERE page.id = page_tag.page_id AND page_tag.tag_id = tag.id AND ((tag.name = '$tag') OR (tag.name = '$tag_unslugified'))"
." AND page.status_id != ".Page::STATUS_HIDDEN." AND page.status_id != ".Page::STATUS_DRAFT." ORDER BY page.created_on DESC";
// Count rows in table
$sql_count = "SELECT count(*) FROM ".TABLE_PREFIX."page AS page, ".TABLE_PREFIX."page_tag AS page_tag, ".TABLE_PREFIX."tag AS tag" . $where;
$query = $pdoConn->query($sql_count);
if($query->fetchColumn() > 0) {
$sql = "SELECT page.* FROM ".TABLE_PREFIX."page AS page, ".TABLE_PREFIX."page_tag AS page_tag, ".TABLE_PREFIX."tag AS tag" . $where;
$stmt = $pdoConn->prepare($sql);
$stmt->execute();
while ($object = $stmt->fetchObject()) {
$page = new PageTagger($object);
// assignParts
$page->part = Page::get_parts($page->id);
$pages[] = $page;
}
} else return false;
return $pages;
}
}
class PageTagger extends Page
{
protected function setUrl()
{
$page = Page::findById($this->id);
$this->url = trim($page->getUri(), '/');
}
}
/**
* Internal Function
* Unslugify the tag to make it human readable.
*
* @since 1.0.1
* @param string $string
*/
function unslugify($string) {
return str_replace('-', ' ', $string);
}