1 | <?php |
---|
2 | /** |
---|
3 | * @brief blogroll, a plugin for Dotclear 2 |
---|
4 | * |
---|
5 | * @package Dotclear |
---|
6 | * @subpackage Plugins |
---|
7 | * |
---|
8 | * @copyright Olivier Meunier & Association Dotclear |
---|
9 | * @copyright GPL-2.0-only |
---|
10 | */ |
---|
11 | |
---|
12 | if (!defined('DC_RC_PATH')) {return;} |
---|
13 | |
---|
14 | class dcBlogroll |
---|
15 | { |
---|
16 | private $blog; |
---|
17 | private $con; |
---|
18 | private $table; |
---|
19 | |
---|
20 | public function __construct($blog) |
---|
21 | { |
---|
22 | $this->blog = &$blog; |
---|
23 | $this->con = &$blog->con; |
---|
24 | $this->table = $this->blog->prefix . 'link'; |
---|
25 | } |
---|
26 | |
---|
27 | public function getLinks($params = array()) |
---|
28 | { |
---|
29 | $strReq = 'SELECT link_id, link_title, link_desc, link_href, ' . |
---|
30 | 'link_lang, link_xfn, link_position ' . |
---|
31 | 'FROM ' . $this->table . ' ' . |
---|
32 | "WHERE blog_id = '" . $this->con->escape($this->blog->id) . "' "; |
---|
33 | |
---|
34 | if (isset($params['link_id'])) { |
---|
35 | $strReq .= 'AND link_id = ' . (integer) $params['link_id'] . ' '; |
---|
36 | } |
---|
37 | |
---|
38 | $strReq .= 'ORDER BY link_position '; |
---|
39 | |
---|
40 | $rs = $this->con->select($strReq); |
---|
41 | $rs = $rs->toStatic(); |
---|
42 | |
---|
43 | $this->setLinksData($rs); |
---|
44 | |
---|
45 | return $rs; |
---|
46 | } |
---|
47 | |
---|
48 | public function getLink($id) |
---|
49 | { |
---|
50 | $params['link_id'] = $id; |
---|
51 | |
---|
52 | $rs = $this->getLinks($params); |
---|
53 | |
---|
54 | return $rs; |
---|
55 | } |
---|
56 | |
---|
57 | public function addLink($title, $href, $desc = '', $lang = '', $xfn = '') |
---|
58 | { |
---|
59 | $cur = $this->con->openCursor($this->table); |
---|
60 | |
---|
61 | $cur->blog_id = (string) $this->blog->id; |
---|
62 | $cur->link_title = (string) $title; |
---|
63 | $cur->link_href = (string) $href; |
---|
64 | $cur->link_desc = (string) $desc; |
---|
65 | $cur->link_lang = (string) $lang; |
---|
66 | $cur->link_xfn = (string) $xfn; |
---|
67 | |
---|
68 | if ($cur->link_title == '') { |
---|
69 | throw new Exception(__('You must provide a link title')); |
---|
70 | } |
---|
71 | |
---|
72 | if ($cur->link_href == '') { |
---|
73 | throw new Exception(__('You must provide a link URL')); |
---|
74 | } |
---|
75 | |
---|
76 | $strReq = 'SELECT MAX(link_id) FROM ' . $this->table; |
---|
77 | $rs = $this->con->select($strReq); |
---|
78 | $cur->link_id = (integer) $rs->f(0) + 1; |
---|
79 | |
---|
80 | $cur->insert(); |
---|
81 | $this->blog->triggerBlog(); |
---|
82 | } |
---|
83 | |
---|
84 | public function updateLink($id, $title, $href, $desc = '', $lang = '', $xfn = '') |
---|
85 | { |
---|
86 | $cur = $this->con->openCursor($this->table); |
---|
87 | |
---|
88 | $cur->link_title = (string) $title; |
---|
89 | $cur->link_href = (string) $href; |
---|
90 | $cur->link_desc = (string) $desc; |
---|
91 | $cur->link_lang = (string) $lang; |
---|
92 | $cur->link_xfn = (string) $xfn; |
---|
93 | |
---|
94 | if ($cur->link_title == '') { |
---|
95 | throw new Exception(__('You must provide a link title')); |
---|
96 | } |
---|
97 | |
---|
98 | if ($cur->link_href == '') { |
---|
99 | throw new Exception(__('You must provide a link URL')); |
---|
100 | } |
---|
101 | |
---|
102 | $cur->update('WHERE link_id = ' . (integer) $id . |
---|
103 | " AND blog_id = '" . $this->con->escape($this->blog->id) . "'"); |
---|
104 | $this->blog->triggerBlog(); |
---|
105 | } |
---|
106 | |
---|
107 | public function updateCategory($id, $desc) |
---|
108 | { |
---|
109 | $cur = $this->con->openCursor($this->table); |
---|
110 | |
---|
111 | $cur->link_desc = (string) $desc; |
---|
112 | |
---|
113 | if ($cur->link_desc == '') { |
---|
114 | throw new Exception(__('You must provide a category title')); |
---|
115 | } |
---|
116 | |
---|
117 | $cur->update('WHERE link_id = ' . (integer) $id . |
---|
118 | " AND blog_id = '" . $this->con->escape($this->blog->id) . "'"); |
---|
119 | $this->blog->triggerBlog(); |
---|
120 | } |
---|
121 | |
---|
122 | public function addCategory($title) |
---|
123 | { |
---|
124 | $cur = $this->con->openCursor($this->table); |
---|
125 | |
---|
126 | $cur->blog_id = (string) $this->blog->id; |
---|
127 | $cur->link_desc = (string) $title; |
---|
128 | $cur->link_href = ''; |
---|
129 | $cur->link_title = ''; |
---|
130 | |
---|
131 | if ($cur->link_desc == '') { |
---|
132 | throw new Exception(__('You must provide a category title')); |
---|
133 | } |
---|
134 | |
---|
135 | $strReq = 'SELECT MAX(link_id) FROM ' . $this->table; |
---|
136 | $rs = $this->con->select($strReq); |
---|
137 | $cur->link_id = (integer) $rs->f(0) + 1; |
---|
138 | |
---|
139 | $cur->insert(); |
---|
140 | $this->blog->triggerBlog(); |
---|
141 | |
---|
142 | return $cur->link_id; |
---|
143 | } |
---|
144 | |
---|
145 | public function delItem($id) |
---|
146 | { |
---|
147 | $id = (integer) $id; |
---|
148 | |
---|
149 | $strReq = 'DELETE FROM ' . $this->table . ' ' . |
---|
150 | "WHERE blog_id = '" . $this->con->escape($this->blog->id) . "' " . |
---|
151 | 'AND link_id = ' . $id . ' '; |
---|
152 | |
---|
153 | $this->con->execute($strReq); |
---|
154 | $this->blog->triggerBlog(); |
---|
155 | } |
---|
156 | |
---|
157 | public function updateOrder($id, $position) |
---|
158 | { |
---|
159 | $cur = $this->con->openCursor($this->table); |
---|
160 | $cur->link_position = (integer) $position; |
---|
161 | |
---|
162 | $cur->update('WHERE link_id = ' . (integer) $id . |
---|
163 | " AND blog_id = '" . $this->con->escape($this->blog->id) . "'"); |
---|
164 | $this->blog->triggerBlog(); |
---|
165 | } |
---|
166 | |
---|
167 | private function setLinksData($rs) |
---|
168 | { |
---|
169 | $cat_title = null; |
---|
170 | while ($rs->fetch()) { |
---|
171 | $rs->set('is_cat', !$rs->link_title && !$rs->link_href); |
---|
172 | |
---|
173 | if ($rs->is_cat) { |
---|
174 | $cat_title = $rs->link_desc; |
---|
175 | $rs->set('cat_title', null); |
---|
176 | } else { |
---|
177 | $rs->set('cat_title', $cat_title); |
---|
178 | } |
---|
179 | } |
---|
180 | $rs->moveStart(); |
---|
181 | } |
---|
182 | |
---|
183 | public function getLinksHierarchy($rs) |
---|
184 | { |
---|
185 | $res = array(); |
---|
186 | |
---|
187 | foreach ($rs->rows() as $k => $v) { |
---|
188 | if (!$v['is_cat']) { |
---|
189 | $res[$v['cat_title']][] = $v; |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | return $res; |
---|
194 | } |
---|
195 | } |
---|