| [0] | 1 | <?php |
|---|
| [3731] | 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 | |
|---|
| [3730] | 12 | if (!defined('DC_RC_PATH')) {return;} |
|---|
| [0] | 13 | |
|---|
| 14 | class dcBlogroll |
|---|
| 15 | { |
|---|
| [3730] | 16 | private $blog; |
|---|
| 17 | private $con; |
|---|
| 18 | private $table; |
|---|
| [2566] | 19 | |
|---|
| [3730] | 20 | public function __construct($blog) |
|---|
| 21 | { |
|---|
| 22 | $this->blog = &$blog; |
|---|
| 23 | $this->con = &$blog->con; |
|---|
| 24 | $this->table = $this->blog->prefix . 'link'; |
|---|
| 25 | } |
|---|
| [2566] | 26 | |
|---|
| [3874] | 27 | public function getLinks($params = []) |
|---|
| [3730] | 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) . "' "; |
|---|
| [2566] | 33 | |
|---|
| [3730] | 34 | if (isset($params['link_id'])) { |
|---|
| 35 | $strReq .= 'AND link_id = ' . (integer) $params['link_id'] . ' '; |
|---|
| 36 | } |
|---|
| [2566] | 37 | |
|---|
| [3730] | 38 | $strReq .= 'ORDER BY link_position '; |
|---|
| [2566] | 39 | |
|---|
| [3730] | 40 | $rs = $this->con->select($strReq); |
|---|
| 41 | $rs = $rs->toStatic(); |
|---|
| [2566] | 42 | |
|---|
| [3730] | 43 | $this->setLinksData($rs); |
|---|
| [2566] | 44 | |
|---|
| [3730] | 45 | return $rs; |
|---|
| 46 | } |
|---|
| [2566] | 47 | |
|---|
| [3874] | 48 | public function getLangs($params = []) |
|---|
| [3817] | 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 | |
|---|
| [3730] | 72 | public function getLink($id) |
|---|
| 73 | { |
|---|
| 74 | $params['link_id'] = $id; |
|---|
| [2566] | 75 | |
|---|
| [3730] | 76 | $rs = $this->getLinks($params); |
|---|
| [2566] | 77 | |
|---|
| [3730] | 78 | return $rs; |
|---|
| 79 | } |
|---|
| [2566] | 80 | |
|---|
| [3730] | 81 | public function addLink($title, $href, $desc = '', $lang = '', $xfn = '') |
|---|
| 82 | { |
|---|
| 83 | $cur = $this->con->openCursor($this->table); |
|---|
| [2566] | 84 | |
|---|
| [3730] | 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; |
|---|
| [2566] | 91 | |
|---|
| [3730] | 92 | if ($cur->link_title == '') { |
|---|
| 93 | throw new Exception(__('You must provide a link title')); |
|---|
| 94 | } |
|---|
| [2566] | 95 | |
|---|
| [3730] | 96 | if ($cur->link_href == '') { |
|---|
| 97 | throw new Exception(__('You must provide a link URL')); |
|---|
| 98 | } |
|---|
| [2566] | 99 | |
|---|
| [3730] | 100 | $strReq = 'SELECT MAX(link_id) FROM ' . $this->table; |
|---|
| 101 | $rs = $this->con->select($strReq); |
|---|
| 102 | $cur->link_id = (integer) $rs->f(0) + 1; |
|---|
| [2566] | 103 | |
|---|
| [3730] | 104 | $cur->insert(); |
|---|
| 105 | $this->blog->triggerBlog(); |
|---|
| 106 | } |
|---|
| [2566] | 107 | |
|---|
| [3730] | 108 | public function updateLink($id, $title, $href, $desc = '', $lang = '', $xfn = '') |
|---|
| 109 | { |
|---|
| 110 | $cur = $this->con->openCursor($this->table); |
|---|
| [2566] | 111 | |
|---|
| [3730] | 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; |
|---|
| [2566] | 117 | |
|---|
| [3730] | 118 | if ($cur->link_title == '') { |
|---|
| 119 | throw new Exception(__('You must provide a link title')); |
|---|
| 120 | } |
|---|
| [2566] | 121 | |
|---|
| [3730] | 122 | if ($cur->link_href == '') { |
|---|
| 123 | throw new Exception(__('You must provide a link URL')); |
|---|
| 124 | } |
|---|
| [2566] | 125 | |
|---|
| [3730] | 126 | $cur->update('WHERE link_id = ' . (integer) $id . |
|---|
| 127 | " AND blog_id = '" . $this->con->escape($this->blog->id) . "'"); |
|---|
| 128 | $this->blog->triggerBlog(); |
|---|
| 129 | } |
|---|
| [2566] | 130 | |
|---|
| [3730] | 131 | public function updateCategory($id, $desc) |
|---|
| 132 | { |
|---|
| 133 | $cur = $this->con->openCursor($this->table); |
|---|
| [2566] | 134 | |
|---|
| [3730] | 135 | $cur->link_desc = (string) $desc; |
|---|
| [2566] | 136 | |
|---|
| [3730] | 137 | if ($cur->link_desc == '') { |
|---|
| 138 | throw new Exception(__('You must provide a category title')); |
|---|
| 139 | } |
|---|
| [2566] | 140 | |
|---|
| [3730] | 141 | $cur->update('WHERE link_id = ' . (integer) $id . |
|---|
| 142 | " AND blog_id = '" . $this->con->escape($this->blog->id) . "'"); |
|---|
| 143 | $this->blog->triggerBlog(); |
|---|
| 144 | } |
|---|
| [2566] | 145 | |
|---|
| [3730] | 146 | public function addCategory($title) |
|---|
| 147 | { |
|---|
| 148 | $cur = $this->con->openCursor($this->table); |
|---|
| [2566] | 149 | |
|---|
| [3730] | 150 | $cur->blog_id = (string) $this->blog->id; |
|---|
| 151 | $cur->link_desc = (string) $title; |
|---|
| 152 | $cur->link_href = ''; |
|---|
| 153 | $cur->link_title = ''; |
|---|
| [2566] | 154 | |
|---|
| [3730] | 155 | if ($cur->link_desc == '') { |
|---|
| 156 | throw new Exception(__('You must provide a category title')); |
|---|
| 157 | } |
|---|
| [2566] | 158 | |
|---|
| [3730] | 159 | $strReq = 'SELECT MAX(link_id) FROM ' . $this->table; |
|---|
| 160 | $rs = $this->con->select($strReq); |
|---|
| 161 | $cur->link_id = (integer) $rs->f(0) + 1; |
|---|
| [2566] | 162 | |
|---|
| [3730] | 163 | $cur->insert(); |
|---|
| 164 | $this->blog->triggerBlog(); |
|---|
| [2566] | 165 | |
|---|
| [3730] | 166 | return $cur->link_id; |
|---|
| 167 | } |
|---|
| [2566] | 168 | |
|---|
| [3730] | 169 | public function delItem($id) |
|---|
| 170 | { |
|---|
| 171 | $id = (integer) $id; |
|---|
| [2566] | 172 | |
|---|
| [3730] | 173 | $strReq = 'DELETE FROM ' . $this->table . ' ' . |
|---|
| 174 | "WHERE blog_id = '" . $this->con->escape($this->blog->id) . "' " . |
|---|
| 175 | 'AND link_id = ' . $id . ' '; |
|---|
| [2566] | 176 | |
|---|
| [3730] | 177 | $this->con->execute($strReq); |
|---|
| 178 | $this->blog->triggerBlog(); |
|---|
| 179 | } |
|---|
| [2566] | 180 | |
|---|
| [3730] | 181 | public function updateOrder($id, $position) |
|---|
| 182 | { |
|---|
| 183 | $cur = $this->con->openCursor($this->table); |
|---|
| 184 | $cur->link_position = (integer) $position; |
|---|
| [2566] | 185 | |
|---|
| [3730] | 186 | $cur->update('WHERE link_id = ' . (integer) $id . |
|---|
| 187 | " AND blog_id = '" . $this->con->escape($this->blog->id) . "'"); |
|---|
| 188 | $this->blog->triggerBlog(); |
|---|
| 189 | } |
|---|
| [2566] | 190 | |
|---|
| [3730] | 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); |
|---|
| [2566] | 196 | |
|---|
| [3730] | 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 | } |
|---|
| [2566] | 206 | |
|---|
| [3730] | 207 | public function getLinksHierarchy($rs) |
|---|
| 208 | { |
|---|
| [3874] | 209 | $res = []; |
|---|
| [2566] | 210 | |
|---|
| [3730] | 211 | foreach ($rs->rows() as $k => $v) { |
|---|
| 212 | if (!$v['is_cat']) { |
|---|
| 213 | $res[$v['cat_title']][] = $v; |
|---|
| 214 | } |
|---|
| 215 | } |
|---|
| [2566] | 216 | |
|---|
| [3730] | 217 | return $res; |
|---|
| 218 | } |
|---|
| [0] | 219 | } |
|---|