Dotclear

source: plugins/blogroll/class.dc.blogroll.php @ 3874:ab8368569446

Revision 3874:ab8368569446, 6.1 KB checked in by franck <carnet.franck.paul@…>, 7 years ago (diff)

short notation for array (array() → [])

Line 
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
12if (!defined('DC_RC_PATH')) {return;}
13
14class 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 = [])
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 getLangs($params = [])
49    {
50        // Use post_lang as an alias of link_lang to be able to use the dcAdminCombos::getLangsCombo() function
51        $strReq = 'SELECT COUNT(link_id) as nb_link, link_lang as post_lang ' .
52        'FROM ' . $this->table . ' ' .
53        "WHERE blog_id = '" . $this->con->escape($this->blog->id) . "' " .
54            "AND link_lang <> '' " .
55            "AND link_lang IS NOT NULL ";
56
57        if (isset($params['lang'])) {
58            $strReq .= "AND link_lang = '" . $this->con->escape($params['lang']) . "' ";
59        }
60
61        $strReq .= 'GROUP BY link_lang ';
62
63        $order = 'desc';
64        if (!empty($params['order']) && preg_match('/^(desc|asc)$/i', $params['order'])) {
65            $order = $params['order'];
66        }
67        $strReq .= 'ORDER BY link_lang ' . $order . ' ';
68
69        return $this->con->select($strReq);
70    }
71
72    public function getLink($id)
73    {
74        $params['link_id'] = $id;
75
76        $rs = $this->getLinks($params);
77
78        return $rs;
79    }
80
81    public function addLink($title, $href, $desc = '', $lang = '', $xfn = '')
82    {
83        $cur = $this->con->openCursor($this->table);
84
85        $cur->blog_id    = (string) $this->blog->id;
86        $cur->link_title = (string) $title;
87        $cur->link_href  = (string) $href;
88        $cur->link_desc  = (string) $desc;
89        $cur->link_lang  = (string) $lang;
90        $cur->link_xfn   = (string) $xfn;
91
92        if ($cur->link_title == '') {
93            throw new Exception(__('You must provide a link title'));
94        }
95
96        if ($cur->link_href == '') {
97            throw new Exception(__('You must provide a link URL'));
98        }
99
100        $strReq       = 'SELECT MAX(link_id) FROM ' . $this->table;
101        $rs           = $this->con->select($strReq);
102        $cur->link_id = (integer) $rs->f(0) + 1;
103
104        $cur->insert();
105        $this->blog->triggerBlog();
106    }
107
108    public function updateLink($id, $title, $href, $desc = '', $lang = '', $xfn = '')
109    {
110        $cur = $this->con->openCursor($this->table);
111
112        $cur->link_title = (string) $title;
113        $cur->link_href  = (string) $href;
114        $cur->link_desc  = (string) $desc;
115        $cur->link_lang  = (string) $lang;
116        $cur->link_xfn   = (string) $xfn;
117
118        if ($cur->link_title == '') {
119            throw new Exception(__('You must provide a link title'));
120        }
121
122        if ($cur->link_href == '') {
123            throw new Exception(__('You must provide a link URL'));
124        }
125
126        $cur->update('WHERE link_id = ' . (integer) $id .
127            " AND blog_id = '" . $this->con->escape($this->blog->id) . "'");
128        $this->blog->triggerBlog();
129    }
130
131    public function updateCategory($id, $desc)
132    {
133        $cur = $this->con->openCursor($this->table);
134
135        $cur->link_desc = (string) $desc;
136
137        if ($cur->link_desc == '') {
138            throw new Exception(__('You must provide a category title'));
139        }
140
141        $cur->update('WHERE link_id = ' . (integer) $id .
142            " AND blog_id = '" . $this->con->escape($this->blog->id) . "'");
143        $this->blog->triggerBlog();
144    }
145
146    public function addCategory($title)
147    {
148        $cur = $this->con->openCursor($this->table);
149
150        $cur->blog_id    = (string) $this->blog->id;
151        $cur->link_desc  = (string) $title;
152        $cur->link_href  = '';
153        $cur->link_title = '';
154
155        if ($cur->link_desc == '') {
156            throw new Exception(__('You must provide a category title'));
157        }
158
159        $strReq       = 'SELECT MAX(link_id) FROM ' . $this->table;
160        $rs           = $this->con->select($strReq);
161        $cur->link_id = (integer) $rs->f(0) + 1;
162
163        $cur->insert();
164        $this->blog->triggerBlog();
165
166        return $cur->link_id;
167    }
168
169    public function delItem($id)
170    {
171        $id = (integer) $id;
172
173        $strReq = 'DELETE FROM ' . $this->table . ' ' .
174        "WHERE blog_id = '" . $this->con->escape($this->blog->id) . "' " .
175            'AND link_id = ' . $id . ' ';
176
177        $this->con->execute($strReq);
178        $this->blog->triggerBlog();
179    }
180
181    public function updateOrder($id, $position)
182    {
183        $cur                = $this->con->openCursor($this->table);
184        $cur->link_position = (integer) $position;
185
186        $cur->update('WHERE link_id = ' . (integer) $id .
187            " AND blog_id = '" . $this->con->escape($this->blog->id) . "'");
188        $this->blog->triggerBlog();
189    }
190
191    private function setLinksData($rs)
192    {
193        $cat_title = null;
194        while ($rs->fetch()) {
195            $rs->set('is_cat', !$rs->link_title && !$rs->link_href);
196
197            if ($rs->is_cat) {
198                $cat_title = $rs->link_desc;
199                $rs->set('cat_title', null);
200            } else {
201                $rs->set('cat_title', $cat_title);
202            }
203        }
204        $rs->moveStart();
205    }
206
207    public function getLinksHierarchy($rs)
208    {
209        $res = [];
210
211        foreach ($rs->rows() as $k => $v) {
212            if (!$v['is_cat']) {
213                $res[$v['cat_title']][] = $v;
214            }
215        }
216
217        return $res;
218    }
219}
Note: See TracBrowser for help on using the repository browser.

Sites map