[0] | 1 | <?php |
---|
| 2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
| 3 | # |
---|
| 4 | # This file is part of Dotclear 2. |
---|
| 5 | # |
---|
[1179] | 6 | # Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear |
---|
[0] | 7 | # Licensed under the GPL version 2.0 license. |
---|
| 8 | # See LICENSE file or |
---|
| 9 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
| 10 | # |
---|
| 11 | # -- END LICENSE BLOCK ----------------------------------------- |
---|
| 12 | if (!defined('DC_RC_PATH')) { return; } |
---|
| 13 | |
---|
| 14 | /** |
---|
| 15 | @ingroup DC_CORE |
---|
| 16 | @nosubgrouping |
---|
| 17 | @brief Dotclear blog class. |
---|
| 18 | |
---|
| 19 | Dotclear blog class instance is provided by dcCore $blog property. |
---|
| 20 | */ |
---|
| 21 | class dcBlog |
---|
| 22 | { |
---|
[337] | 23 | /** @var dcCore dcCore instance */ |
---|
| 24 | protected $core; |
---|
| 25 | /** @var connection Database connection object */ |
---|
| 26 | public $con; |
---|
| 27 | /** @var string Database table prefix */ |
---|
| 28 | public $prefix; |
---|
[2566] | 29 | |
---|
[337] | 30 | /** @var string Blog ID */ |
---|
| 31 | public $id; |
---|
| 32 | /** @var string Blog unique ID */ |
---|
| 33 | public $uid; |
---|
| 34 | /** @var string Blog name */ |
---|
| 35 | public $name; |
---|
| 36 | /** @var string Blog description */ |
---|
| 37 | public $desc; |
---|
| 38 | /** @var string Blog URL */ |
---|
| 39 | public $url; |
---|
| 40 | /** @var string Blog host */ |
---|
| 41 | public $host; |
---|
| 42 | /** @var string Blog creation date */ |
---|
| 43 | public $creadt; |
---|
| 44 | /** @var string Blog last update date */ |
---|
| 45 | public $upddt; |
---|
| 46 | /** @var string Blog status */ |
---|
| 47 | public $status; |
---|
[2566] | 48 | |
---|
[337] | 49 | /** @var dcSettings dcSettings object */ |
---|
| 50 | public $settings; |
---|
| 51 | /** @var string Blog theme path */ |
---|
| 52 | public $themes_path; |
---|
| 53 | /** @var string Blog public path */ |
---|
| 54 | public $public_path; |
---|
[2566] | 55 | |
---|
[0] | 56 | private $post_status = array(); |
---|
| 57 | private $comment_status = array(); |
---|
[2566] | 58 | |
---|
[0] | 59 | private $categories; |
---|
[2566] | 60 | |
---|
[337] | 61 | /** @var boolean Disallow entries password protection */ |
---|
| 62 | public $without_password = true; |
---|
[2566] | 63 | |
---|
[0] | 64 | /** |
---|
| 65 | Inits dcBlog object |
---|
[2566] | 66 | |
---|
[0] | 67 | @param core <b>dcCore</b> Dotclear core reference |
---|
| 68 | @param id <b>string</b> Blog ID |
---|
| 69 | */ |
---|
| 70 | public function __construct($core, $id) |
---|
| 71 | { |
---|
| 72 | $this->con =& $core->con; |
---|
| 73 | $this->prefix = $core->prefix; |
---|
| 74 | $this->core =& $core; |
---|
[2566] | 75 | |
---|
[0] | 76 | if (($b = $this->core->getBlog($id)) !== false) |
---|
| 77 | { |
---|
| 78 | $this->id = $id; |
---|
| 79 | $this->uid = $b->blog_uid; |
---|
| 80 | $this->name = $b->blog_name; |
---|
| 81 | $this->desc = $b->blog_desc; |
---|
| 82 | $this->url = $b->blog_url; |
---|
[1244] | 83 | $this->host = http::getHostFromURL($this->url); |
---|
[0] | 84 | $this->creadt = strtotime($b->blog_creadt); |
---|
| 85 | $this->upddt = strtotime($b->blog_upddt); |
---|
| 86 | $this->status = $b->blog_status; |
---|
[2566] | 87 | |
---|
[0] | 88 | $this->settings = new dcSettings($this->core,$this->id); |
---|
[2566] | 89 | |
---|
[0] | 90 | $this->themes_path = path::fullFromRoot($this->settings->system->themes_path,DC_ROOT); |
---|
| 91 | $this->public_path = path::fullFromRoot($this->settings->system->public_path,DC_ROOT); |
---|
[2566] | 92 | |
---|
[1353] | 93 | $this->post_status['-2'] = __('Pending'); |
---|
| 94 | $this->post_status['-1'] = __('Scheduled'); |
---|
| 95 | $this->post_status['0'] = __('Unpublished'); |
---|
| 96 | $this->post_status['1'] = __('Published'); |
---|
[2566] | 97 | |
---|
[1353] | 98 | $this->comment_status['-2'] = __('Junk'); |
---|
| 99 | $this->comment_status['-1'] = __('Pending'); |
---|
| 100 | $this->comment_status['0'] = __('Unpublished'); |
---|
| 101 | $this->comment_status['1'] = __('Published'); |
---|
[2566] | 102 | |
---|
[0] | 103 | # --BEHAVIOR-- coreBlogConstruct |
---|
| 104 | $this->core->callBehavior('coreBlogConstruct',$this); |
---|
| 105 | } |
---|
| 106 | } |
---|
[2566] | 107 | |
---|
[0] | 108 | /// @name Common public methods |
---|
| 109 | //@{ |
---|
| 110 | /** |
---|
| 111 | Returns blog URL ending with a question mark. |
---|
| 112 | */ |
---|
| 113 | public function getQmarkURL() |
---|
| 114 | { |
---|
| 115 | if (substr($this->url,-1) != '?') { |
---|
| 116 | return $this->url.'?'; |
---|
| 117 | } |
---|
[2566] | 118 | |
---|
[0] | 119 | return $this->url; |
---|
| 120 | } |
---|
[2566] | 121 | |
---|
[0] | 122 | /** |
---|
[3132] | 123 | Returns jQuery version selected for the blog. |
---|
[2800] | 124 | */ |
---|
| 125 | public function getJsJQuery() |
---|
| 126 | { |
---|
| 127 | $version = $this->settings->system->jquery_version; |
---|
| 128 | if ($version == '') { |
---|
| 129 | $version = DC_DEFAULT_JQUERY; // defined in inc/prepend.php |
---|
| 130 | } |
---|
| 131 | return 'jquery/'.$version; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | /** |
---|
[3132] | 135 | Returns public URL of specified plugin file. |
---|
| 136 | */ |
---|
| 137 | public function getPF($pf, $strip_host=true) |
---|
| 138 | { |
---|
| 139 | $ret = $this->getQmarkURL().'pf='.$pf; |
---|
| 140 | if ($strip_host) { |
---|
| 141 | $ret = html::stripHostURL($ret); |
---|
| 142 | } |
---|
| 143 | return $ret; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | /** |
---|
[0] | 147 | Returns an entry status name given to a code. Status are translated, never |
---|
| 148 | use it for tests. If status code does not exist, returns <i>unpublished</i>. |
---|
[2566] | 149 | |
---|
[0] | 150 | @param s <b>integer</b> Status code |
---|
| 151 | @return <b>string</b> Blog status name |
---|
| 152 | */ |
---|
| 153 | public function getPostStatus($s) |
---|
| 154 | { |
---|
| 155 | if (isset($this->post_status[$s])) { |
---|
| 156 | return $this->post_status[$s]; |
---|
| 157 | } |
---|
| 158 | return $this->post_status['0']; |
---|
| 159 | } |
---|
[2566] | 160 | |
---|
[0] | 161 | /** |
---|
| 162 | Returns an array of available entry status codes and names. |
---|
[2566] | 163 | |
---|
[0] | 164 | @return <b>array</b> Simple array with codes in keys and names in value |
---|
| 165 | */ |
---|
| 166 | public function getAllPostStatus() |
---|
| 167 | { |
---|
| 168 | return $this->post_status; |
---|
| 169 | } |
---|
[2566] | 170 | |
---|
[0] | 171 | /** |
---|
| 172 | Returns an array of available comment status codes and names. |
---|
[2566] | 173 | |
---|
[0] | 174 | @return <b>array</b> Simple array with codes in keys and names in value |
---|
| 175 | */ |
---|
| 176 | public function getAllCommentStatus() |
---|
| 177 | { |
---|
| 178 | return $this->comment_status; |
---|
| 179 | } |
---|
[2566] | 180 | |
---|
[0] | 181 | /** |
---|
| 182 | Disallows entries password protection. You need to set it to |
---|
| 183 | <var>false</var> while serving a public blog. |
---|
[2566] | 184 | |
---|
[0] | 185 | @param v <b>boolean</b> |
---|
| 186 | */ |
---|
| 187 | public function withoutPassword($v) |
---|
| 188 | { |
---|
| 189 | $this->without_password = (boolean) $v; |
---|
| 190 | } |
---|
| 191 | //@} |
---|
[2566] | 192 | |
---|
[0] | 193 | /// @name Triggers methods |
---|
| 194 | //@{ |
---|
| 195 | /** |
---|
| 196 | Updates blog last update date. Should be called every time you change |
---|
| 197 | an element related to the blog. |
---|
| 198 | */ |
---|
| 199 | public function triggerBlog() |
---|
| 200 | { |
---|
| 201 | $cur = $this->con->openCursor($this->prefix.'blog'); |
---|
[2566] | 202 | |
---|
[0] | 203 | $cur->blog_upddt = date('Y-m-d H:i:s'); |
---|
[2566] | 204 | |
---|
[0] | 205 | $cur->update("WHERE blog_id = '".$this->con->escape($this->id)."' "); |
---|
[2566] | 206 | |
---|
[0] | 207 | # --BEHAVIOR-- coreBlogAfterTriggerBlog |
---|
| 208 | $this->core->callBehavior('coreBlogAfterTriggerBlog',$cur); |
---|
| 209 | } |
---|
[2566] | 210 | |
---|
[0] | 211 | /** |
---|
| 212 | Updates comment and trackback counters in post table. Should be called |
---|
| 213 | every time a comment or trackback is added, removed or changed its status. |
---|
[2566] | 214 | |
---|
[0] | 215 | @param id <b>integer</b> Comment ID |
---|
| 216 | @param del <b>boolean</b> If comment is delete, set this to true |
---|
| 217 | */ |
---|
| 218 | public function triggerComment($id,$del=false) |
---|
| 219 | { |
---|
[1042] | 220 | $this->triggerComments($id,$del); |
---|
| 221 | } |
---|
[2566] | 222 | |
---|
[1042] | 223 | /** |
---|
| 224 | Updates comments and trackbacks counters in post table. Should be called |
---|
[1689] | 225 | every time comments or trackbacks are added, removed or changed their status. |
---|
[2566] | 226 | |
---|
[1042] | 227 | @param ids <b>mixed</b> Comment(s) ID(s) |
---|
| 228 | @param del <b>boolean</b> If comment is delete, set this to true |
---|
[1262] | 229 | @param affected_posts <b>mixed</b> Posts(s) ID(s) |
---|
[1042] | 230 | */ |
---|
[1262] | 231 | public function triggerComments($ids, $del=false, $affected_posts=null) |
---|
[1042] | 232 | { |
---|
[1561] | 233 | $comments_ids = dcUtils::cleanIds($ids); |
---|
[2566] | 234 | |
---|
[1570] | 235 | # Get posts affected by comments edition |
---|
| 236 | if (empty($affected_posts)) { |
---|
[2566] | 237 | $strReq = |
---|
[1570] | 238 | 'SELECT post_id '. |
---|
[1262] | 239 | 'FROM '.$this->prefix.'comment '. |
---|
[1561] | 240 | 'WHERE comment_id'.$this->con->in($comments_ids). |
---|
[1570] | 241 | 'GROUP BY post_id'; |
---|
[2566] | 242 | |
---|
[1262] | 243 | $rs = $this->con->select($strReq); |
---|
[2566] | 244 | |
---|
[1570] | 245 | $affected_posts = array(); |
---|
[1262] | 246 | while ($rs->fetch()) { |
---|
[1570] | 247 | $affected_posts[] = (integer) $rs->post_id; |
---|
[1262] | 248 | } |
---|
[1042] | 249 | } |
---|
[2566] | 250 | |
---|
[1570] | 251 | if (!is_array($affected_posts) || empty($affected_posts)) { |
---|
| 252 | return; |
---|
| 253 | } |
---|
[2566] | 254 | |
---|
[1570] | 255 | # Count number of comments if exists for affected posts |
---|
[2566] | 256 | $strReq = |
---|
[1570] | 257 | 'SELECT post_id, COUNT(post_id) AS nb_comment, comment_trackback '. |
---|
[1042] | 258 | 'FROM '.$this->prefix.'comment '. |
---|
[1110] | 259 | 'WHERE comment_status = 1 '. |
---|
[1570] | 260 | 'AND post_id'.$this->con->in($affected_posts). |
---|
[1042] | 261 | 'GROUP BY post_id,comment_trackback'; |
---|
[2566] | 262 | |
---|
[0] | 263 | $rs = $this->con->select($strReq); |
---|
[2566] | 264 | |
---|
[1570] | 265 | $posts = array(); |
---|
[1042] | 266 | while ($rs->fetch()) { |
---|
[1570] | 267 | if ($rs->comment_trackback) { |
---|
| 268 | $posts[$rs->post_id]['trackback'] = $rs->nb_comment; |
---|
| 269 | } else { |
---|
| 270 | $posts[$rs->post_id]['comment'] = $rs->nb_comment; |
---|
| 271 | } |
---|
[1042] | 272 | } |
---|
[2566] | 273 | |
---|
[1570] | 274 | # Update number of comments on affected posts |
---|
[0] | 275 | $cur = $this->con->openCursor($this->prefix.'post'); |
---|
[1570] | 276 | foreach($affected_posts as $post_id) |
---|
[1042] | 277 | { |
---|
[1570] | 278 | $cur->clean(); |
---|
[2566] | 279 | |
---|
[1570] | 280 | if (!array_key_exists($post_id,$posts)) { |
---|
| 281 | $cur->nb_trackback = 0; |
---|
| 282 | $cur->nb_comment = 0; |
---|
| 283 | } else { |
---|
| 284 | $cur->nb_trackback = empty($posts[$post_id]['trackback']) ? 0 : $posts[$post_id]['trackback']; |
---|
| 285 | $cur->nb_comment = empty($posts[$post_id]['comment']) ? 0 : $posts[$post_id]['comment']; |
---|
[1042] | 286 | } |
---|
[2566] | 287 | |
---|
[1570] | 288 | $cur->update('WHERE post_id = '.$post_id); |
---|
[0] | 289 | } |
---|
| 290 | } |
---|
| 291 | //@} |
---|
[2566] | 292 | |
---|
[0] | 293 | /// @name Categories management methods |
---|
| 294 | //@{ |
---|
| 295 | public function categories() |
---|
| 296 | { |
---|
| 297 | if (!($this->categories instanceof dcCategories)) { |
---|
| 298 | $this->categories = new dcCategories($this->core); |
---|
| 299 | } |
---|
[2566] | 300 | |
---|
[0] | 301 | return $this->categories; |
---|
| 302 | } |
---|
[2566] | 303 | |
---|
[0] | 304 | /** |
---|
| 305 | Retrieves categories. <var>$params</var> is an associative array which can |
---|
| 306 | take the following parameters: |
---|
[2566] | 307 | |
---|
[0] | 308 | - post_type: Get only entries with given type (default "post") |
---|
| 309 | - cat_url: filter on cat_url field |
---|
| 310 | - cat_id: filter on cat_id field |
---|
| 311 | - start: start with a given category |
---|
| 312 | - level: categories level to retrieve |
---|
[2566] | 313 | |
---|
[0] | 314 | @param params <b>array</b> Parameters |
---|
| 315 | @return <b>record</b> |
---|
| 316 | */ |
---|
| 317 | public function getCategories($params=array()) |
---|
| 318 | { |
---|
| 319 | $c_params = array(); |
---|
| 320 | if (isset($params['post_type'])) { |
---|
| 321 | $c_params['post_type'] = $params['post_type']; |
---|
| 322 | unset($params['post_type']); |
---|
| 323 | } |
---|
| 324 | $counter = $this->getCategoriesCounter($c_params); |
---|
[2566] | 325 | |
---|
[1610] | 326 | if (isset($params['without_empty']) && ($params['without_empty'] == false)) { |
---|
| 327 | $without_empty = false; |
---|
| 328 | } else { |
---|
| 329 | $without_empty = $this->core->auth->userID() == false; # Get all categories if in admin display |
---|
| 330 | } |
---|
[2566] | 331 | |
---|
[0] | 332 | $start = isset($params['start']) ? (integer) $params['start'] : 0; |
---|
| 333 | $l = isset($params['level']) ? (integer) $params['level'] : 0; |
---|
[2566] | 334 | |
---|
[0] | 335 | $rs = $this->categories()->getChildren($start,null,'desc'); |
---|
[2566] | 336 | |
---|
[0] | 337 | # Get each categories total posts count |
---|
| 338 | $data = array(); |
---|
| 339 | $stack = array(); |
---|
| 340 | $level = 0; |
---|
| 341 | $cols = $rs->columns(); |
---|
| 342 | while ($rs->fetch()) |
---|
| 343 | { |
---|
| 344 | $nb_post = isset($counter[$rs->cat_id]) ? (integer) $counter[$rs->cat_id] : 0; |
---|
[2566] | 345 | |
---|
[0] | 346 | if ($rs->level > $level) { |
---|
| 347 | $nb_total = $nb_post; |
---|
| 348 | $stack[$rs->level] = (integer) $nb_post; |
---|
| 349 | } elseif ($rs->level == $level) { |
---|
| 350 | $nb_total = $nb_post; |
---|
| 351 | $stack[$rs->level] += $nb_post; |
---|
| 352 | } else { |
---|
| 353 | $nb_total = $stack[$rs->level+1] + $nb_post; |
---|
| 354 | if (isset($stack[$rs->level])) { |
---|
| 355 | $stack[$rs->level] += $nb_total; |
---|
| 356 | } else { |
---|
| 357 | $stack[$rs->level] = $nb_total; |
---|
| 358 | } |
---|
| 359 | unset($stack[$rs->level+1]); |
---|
| 360 | } |
---|
[2566] | 361 | |
---|
[0] | 362 | if ($nb_total == 0 && $without_empty) { |
---|
| 363 | continue; |
---|
| 364 | } |
---|
[2566] | 365 | |
---|
[0] | 366 | $level = $rs->level; |
---|
[2566] | 367 | |
---|
[0] | 368 | $t = array(); |
---|
| 369 | foreach ($cols as $c) { |
---|
| 370 | $t[$c] = $rs->f($c); |
---|
| 371 | } |
---|
| 372 | $t['nb_post'] = $nb_post; |
---|
| 373 | $t['nb_total'] = $nb_total; |
---|
[2566] | 374 | |
---|
[0] | 375 | if ($l == 0 || ($l > 0 && $l == $rs->level)) { |
---|
| 376 | array_unshift($data,$t); |
---|
| 377 | } |
---|
| 378 | } |
---|
[2566] | 379 | |
---|
[0] | 380 | # We need to apply filter after counting |
---|
[836] | 381 | if (isset($params['cat_id']) && $params['cat_id'] !== '') |
---|
[0] | 382 | { |
---|
| 383 | $found = false; |
---|
| 384 | foreach ($data as $v) { |
---|
| 385 | if ($v['cat_id'] == $params['cat_id']) { |
---|
| 386 | $found = true; |
---|
| 387 | $data = array($v); |
---|
| 388 | break; |
---|
| 389 | } |
---|
| 390 | } |
---|
| 391 | if (!$found) { |
---|
| 392 | $data = array(); |
---|
| 393 | } |
---|
| 394 | } |
---|
[2566] | 395 | |
---|
| 396 | if (isset($params['cat_url']) && ($params['cat_url'] !== '') |
---|
[836] | 397 | && !isset($params['cat_id'])) |
---|
[0] | 398 | { |
---|
| 399 | $found = false; |
---|
| 400 | foreach ($data as $v) { |
---|
| 401 | if ($v['cat_url'] == $params['cat_url']) { |
---|
| 402 | $found = true; |
---|
| 403 | $data = array($v); |
---|
| 404 | break; |
---|
| 405 | } |
---|
| 406 | } |
---|
| 407 | if (!$found) { |
---|
| 408 | $data = array(); |
---|
| 409 | } |
---|
| 410 | } |
---|
[2566] | 411 | |
---|
[0] | 412 | return staticRecord::newFromArray($data); |
---|
| 413 | } |
---|
[2566] | 414 | |
---|
[0] | 415 | /** |
---|
| 416 | Retrieves a category by its ID. |
---|
[2566] | 417 | |
---|
[0] | 418 | @param id <b>integer</b> Category ID |
---|
| 419 | @return <b>record</b> |
---|
| 420 | */ |
---|
| 421 | public function getCategory($id) |
---|
| 422 | { |
---|
| 423 | return $this->getCategories(array('cat_id' => $id)); |
---|
| 424 | } |
---|
[2566] | 425 | |
---|
[0] | 426 | /** |
---|
| 427 | Retrieves parents of a given category. |
---|
[2566] | 428 | |
---|
[0] | 429 | @param id <b>integer</b> Category ID |
---|
| 430 | @return <b>record</b> |
---|
| 431 | */ |
---|
| 432 | public function getCategoryParents($id) |
---|
| 433 | { |
---|
| 434 | return $this->categories()->getParents($id); |
---|
| 435 | } |
---|
[2566] | 436 | |
---|
[0] | 437 | /** |
---|
| 438 | Retrieves first parent of a given category. |
---|
[2566] | 439 | |
---|
[0] | 440 | @param id <b>integer</b> Category ID |
---|
| 441 | @return <b>record</b> |
---|
| 442 | */ |
---|
| 443 | public function getCategoryParent($id) |
---|
| 444 | { |
---|
| 445 | return $this->categories()->getParent($id); |
---|
| 446 | } |
---|
[2566] | 447 | |
---|
[0] | 448 | /** |
---|
| 449 | Retrieves all category's first children |
---|
[2566] | 450 | |
---|
[0] | 451 | @param id <b>integer</b> Category ID |
---|
| 452 | @return <b>record</b> |
---|
| 453 | */ |
---|
| 454 | public function getCategoryFirstChildren($id) |
---|
| 455 | { |
---|
| 456 | return $this->getCategories(array('start' => $id,'level' => $id == 0 ? 1 : 2)); |
---|
| 457 | } |
---|
[2566] | 458 | |
---|
[0] | 459 | private function getCategoriesCounter($params=array()) |
---|
| 460 | { |
---|
| 461 | $strReq = |
---|
| 462 | 'SELECT C.cat_id, COUNT(P.post_id) AS nb_post '. |
---|
| 463 | 'FROM '.$this->prefix.'category AS C '. |
---|
| 464 | 'JOIN '.$this->prefix."post P ON (C.cat_id = P.cat_id AND P.blog_id = '".$this->con->escape($this->id)."' ) ". |
---|
| 465 | "WHERE C.blog_id = '".$this->con->escape($this->id)."' "; |
---|
[2566] | 466 | |
---|
[0] | 467 | if (!$this->core->auth->userID()) { |
---|
| 468 | $strReq .= 'AND P.post_status = 1 '; |
---|
| 469 | } |
---|
[2566] | 470 | |
---|
[0] | 471 | if (!empty($params['post_type'])) { |
---|
[302] | 472 | $strReq .= 'AND P.post_type '.$this->con->in($params['post_type']); |
---|
[0] | 473 | } |
---|
[2566] | 474 | |
---|
[0] | 475 | $strReq .= 'GROUP BY C.cat_id '; |
---|
[2566] | 476 | |
---|
[0] | 477 | $rs = $this->con->select($strReq); |
---|
| 478 | $counters = array(); |
---|
| 479 | while ($rs->fetch()) { |
---|
| 480 | $counters[$rs->cat_id] = $rs->nb_post; |
---|
| 481 | } |
---|
[2566] | 482 | |
---|
[0] | 483 | return $counters; |
---|
| 484 | } |
---|
[2566] | 485 | |
---|
[0] | 486 | /** |
---|
| 487 | Creates a new category. Takes a cursor as input and returns the new category |
---|
| 488 | ID. |
---|
[2566] | 489 | |
---|
[0] | 490 | @param cur <b>cursor</b> Category cursor |
---|
| 491 | @return <b>integer</b> New category ID |
---|
| 492 | */ |
---|
| 493 | public function addCategory($cur,$parent=0) |
---|
| 494 | { |
---|
| 495 | if (!$this->core->auth->check('categories',$this->id)) { |
---|
| 496 | throw new Exception(__('You are not allowed to add categories')); |
---|
| 497 | } |
---|
[2566] | 498 | |
---|
[0] | 499 | $url = array(); |
---|
| 500 | if ($parent != 0) |
---|
| 501 | { |
---|
| 502 | $rs = $this->getCategory($parent); |
---|
| 503 | if ($rs->isEmpty()) { |
---|
| 504 | $url = array(); |
---|
| 505 | } else { |
---|
| 506 | $url[] = $rs->cat_url; |
---|
| 507 | } |
---|
| 508 | } |
---|
[2566] | 509 | |
---|
[0] | 510 | if ($cur->cat_url == '') { |
---|
| 511 | $url[] = text::tidyURL($cur->cat_title,false); |
---|
| 512 | } else { |
---|
| 513 | $url[] = $cur->cat_url; |
---|
| 514 | } |
---|
[2566] | 515 | |
---|
[0] | 516 | $cur->cat_url = implode('/',$url); |
---|
[2566] | 517 | |
---|
[0] | 518 | $this->getCategoryCursor($cur); |
---|
| 519 | $cur->blog_id = (string) $this->id; |
---|
[2566] | 520 | |
---|
[0] | 521 | # --BEHAVIOR-- coreBeforeCategoryCreate |
---|
| 522 | $this->core->callBehavior('coreBeforeCategoryCreate',$this,$cur); |
---|
[2566] | 523 | |
---|
[1261] | 524 | $id = $this->categories()->addNode($cur,$parent); |
---|
| 525 | # Update category's cursor |
---|
| 526 | $rs = $this->getCategory($id); |
---|
| 527 | if (!$rs->isEmpty()) { |
---|
| 528 | $cur->cat_lft = $rs->cat_lft; |
---|
| 529 | $cur->cat_rgt = $rs->cat_rgt; |
---|
| 530 | } |
---|
[2566] | 531 | |
---|
[0] | 532 | # --BEHAVIOR-- coreAfterCategoryCreate |
---|
| 533 | $this->core->callBehavior('coreAfterCategoryCreate',$this,$cur); |
---|
| 534 | $this->triggerBlog(); |
---|
[2566] | 535 | |
---|
[0] | 536 | return $cur->cat_id; |
---|
| 537 | } |
---|
[2566] | 538 | |
---|
[0] | 539 | /** |
---|
| 540 | Updates an existing category. |
---|
[2566] | 541 | |
---|
[0] | 542 | @param id <b>integer</b> Category ID |
---|
| 543 | @param cur <b>cursor</b> Category cursor |
---|
| 544 | */ |
---|
| 545 | public function updCategory($id,$cur) |
---|
| 546 | { |
---|
| 547 | if (!$this->core->auth->check('categories',$this->id)) { |
---|
| 548 | throw new Exception(__('You are not allowed to update categories')); |
---|
| 549 | } |
---|
[2566] | 550 | |
---|
[0] | 551 | if ($cur->cat_url == '') |
---|
| 552 | { |
---|
| 553 | $url = array(); |
---|
| 554 | $rs = $this->categories()->getParents($id); |
---|
| 555 | while ($rs->fetch()) { |
---|
| 556 | if ($rs->index() == $rs->count()-1) { |
---|
| 557 | $url[] = $rs->cat_url; |
---|
| 558 | } |
---|
| 559 | } |
---|
[2566] | 560 | |
---|
| 561 | |
---|
[0] | 562 | $url[] = text::tidyURL($cur->cat_title,false); |
---|
| 563 | $cur->cat_url = implode('/',$url); |
---|
| 564 | } |
---|
[2566] | 565 | |
---|
[0] | 566 | $this->getCategoryCursor($cur,$id); |
---|
[2566] | 567 | |
---|
[0] | 568 | # --BEHAVIOR-- coreBeforeCategoryUpdate |
---|
| 569 | $this->core->callBehavior('coreBeforeCategoryUpdate',$this,$cur); |
---|
[2566] | 570 | |
---|
[0] | 571 | $cur->update( |
---|
| 572 | 'WHERE cat_id = '.(integer) $id.' '. |
---|
| 573 | "AND blog_id = '".$this->con->escape($this->id)."' "); |
---|
[2566] | 574 | |
---|
[0] | 575 | # --BEHAVIOR-- coreAfterCategoryUpdate |
---|
| 576 | $this->core->callBehavior('coreAfterCategoryUpdate',$this,$cur); |
---|
[2566] | 577 | |
---|
[0] | 578 | $this->triggerBlog(); |
---|
| 579 | } |
---|
[1532] | 580 | |
---|
| 581 | /** |
---|
| 582 | Set category position |
---|
| 583 | |
---|
| 584 | @param id <b>integer</b> Category ID |
---|
| 585 | @param left <b>integer</b> Category ID before |
---|
| 586 | @param right <b>integer</b> Category ID after |
---|
| 587 | */ |
---|
| 588 | public function updCategoryPosition($id,$left,$right) |
---|
| 589 | { |
---|
| 590 | $this->categories()->updatePosition($id,$left,$right); |
---|
| 591 | $this->triggerBlog(); |
---|
| 592 | } |
---|
[2566] | 593 | |
---|
[0] | 594 | /** |
---|
| 595 | DEPRECATED METHOD. Use dcBlog::setCategoryParent and dcBlog::moveCategory |
---|
| 596 | instead. |
---|
[2566] | 597 | |
---|
[0] | 598 | @param id <b>integer</b> Category ID |
---|
| 599 | @param order <b>integer</b> Category position |
---|
| 600 | */ |
---|
| 601 | public function updCategoryOrder($id,$order) |
---|
| 602 | { |
---|
| 603 | return; |
---|
| 604 | } |
---|
[2566] | 605 | |
---|
[0] | 606 | /** |
---|
| 607 | Set a category parent |
---|
[2566] | 608 | |
---|
[0] | 609 | @param id <b>integer</b> Category ID |
---|
| 610 | @param parent <b>integer</b> Parent Category ID |
---|
| 611 | */ |
---|
| 612 | public function setCategoryParent($id,$parent) |
---|
| 613 | { |
---|
| 614 | $this->categories()->setNodeParent($id,$parent); |
---|
| 615 | $this->triggerBlog(); |
---|
| 616 | } |
---|
[2566] | 617 | |
---|
[0] | 618 | /** |
---|
| 619 | Set category position |
---|
[2566] | 620 | |
---|
[0] | 621 | @param id <b>integer</b> Category ID |
---|
| 622 | @param sibling <b>integer</b> Sibling Category ID |
---|
| 623 | @param move <b>integer</b> Order (before|after) |
---|
| 624 | */ |
---|
| 625 | public function setCategoryPosition($id,$sibling,$move) |
---|
| 626 | { |
---|
| 627 | $this->categories()->setNodePosition($id,$sibling,$move); |
---|
| 628 | $this->triggerBlog(); |
---|
| 629 | } |
---|
[2566] | 630 | |
---|
[0] | 631 | /** |
---|
| 632 | Deletes a category. |
---|
[2566] | 633 | |
---|
[0] | 634 | @param id <b>integer</b> Category ID |
---|
| 635 | */ |
---|
| 636 | public function delCategory($id) |
---|
| 637 | { |
---|
| 638 | if (!$this->core->auth->check('categories',$this->id)) { |
---|
| 639 | throw new Exception(__('You are not allowed to delete categories')); |
---|
| 640 | } |
---|
[2566] | 641 | |
---|
[0] | 642 | $strReq = 'SELECT COUNT(post_id) AS nb_post '. |
---|
| 643 | 'FROM '.$this->prefix.'post '. |
---|
| 644 | 'WHERE cat_id = '.(integer) $id.' '. |
---|
| 645 | "AND blog_id = '".$this->con->escape($this->id)."' "; |
---|
[2566] | 646 | |
---|
[0] | 647 | $rs = $this->con->select($strReq); |
---|
[2566] | 648 | |
---|
[0] | 649 | if ($rs->nb_post > 0) { |
---|
| 650 | throw new Exception(__('This category is not empty.')); |
---|
| 651 | } |
---|
[2566] | 652 | |
---|
[0] | 653 | $this->categories()->deleteNode($id,true); |
---|
| 654 | $this->triggerBlog(); |
---|
| 655 | } |
---|
[2566] | 656 | |
---|
[0] | 657 | /** |
---|
| 658 | Reset categories order and relocate them to first level |
---|
| 659 | */ |
---|
| 660 | public function resetCategoriesOrder() |
---|
| 661 | { |
---|
| 662 | if (!$this->core->auth->check('categories',$this->id)) { |
---|
[822] | 663 | throw new Exception(__('You are not allowed to reset categories order')); |
---|
[0] | 664 | } |
---|
[2566] | 665 | |
---|
[0] | 666 | $this->categories()->resetOrder(); |
---|
[734] | 667 | $this->triggerBlog(); |
---|
[0] | 668 | } |
---|
[2566] | 669 | |
---|
[0] | 670 | private function checkCategory($title,$url,$id=null) |
---|
| 671 | { |
---|
[1429] | 672 | # Let's check if URL is taken... |
---|
[2566] | 673 | $strReq = |
---|
[1429] | 674 | 'SELECT cat_url FROM '.$this->prefix.'category '. |
---|
| 675 | "WHERE cat_url = '".$this->con->escape($url)."' ". |
---|
| 676 | ($id ? 'AND cat_id <> '.(integer) $id. ' ' : ''). |
---|
| 677 | "AND blog_id = '".$this->con->escape($this->id)."' ". |
---|
| 678 | 'ORDER BY cat_url DESC'; |
---|
[2566] | 679 | |
---|
[0] | 680 | $rs = $this->con->select($strReq); |
---|
[2566] | 681 | |
---|
[1429] | 682 | if (!$rs->isEmpty()) |
---|
| 683 | { |
---|
[1743] | 684 | if ($this->con->driver() == 'mysql' || $this->con->driver() == 'mysqli') { |
---|
[1429] | 685 | $clause = "REGEXP '^".$this->con->escape($url)."[0-9]+$'"; |
---|
| 686 | } elseif ($this->con->driver() == 'pgsql') { |
---|
| 687 | $clause = "~ '^".$this->con->escape($url)."[0-9]+$'"; |
---|
| 688 | } else { |
---|
| 689 | $clause = "LIKE '".$this->con->escape($url)."%'"; |
---|
| 690 | } |
---|
[2566] | 691 | $strReq = |
---|
[1429] | 692 | 'SELECT cat_url FROM '.$this->prefix.'category '. |
---|
| 693 | "WHERE cat_url ".$clause.' '. |
---|
| 694 | ($id ? 'AND cat_id <> '.(integer) $id. ' ' : ''). |
---|
| 695 | "AND blog_id = '".$this->con->escape($this->id)."' ". |
---|
| 696 | 'ORDER BY cat_url DESC '; |
---|
[2566] | 697 | |
---|
[1429] | 698 | $rs = $this->con->select($strReq); |
---|
| 699 | $a = array(); |
---|
| 700 | while ($rs->fetch()) { |
---|
| 701 | $a[] = $rs->cat_url; |
---|
| 702 | } |
---|
[2566] | 703 | |
---|
[1429] | 704 | natsort($a); |
---|
| 705 | $t_url = end($a); |
---|
[2566] | 706 | |
---|
[1429] | 707 | if (preg_match('/(.*?)([0-9]+)$/',$t_url,$m)) { |
---|
| 708 | $i = (integer) $m[2]; |
---|
| 709 | $url = $m[1]; |
---|
| 710 | } else { |
---|
| 711 | $i = 1; |
---|
| 712 | } |
---|
[2566] | 713 | |
---|
[1429] | 714 | return $url.($i+1); |
---|
[0] | 715 | } |
---|
[2566] | 716 | |
---|
[1429] | 717 | # URL is empty? |
---|
| 718 | if ($url == '') { |
---|
| 719 | throw new Exception(__('Empty category URL')); |
---|
| 720 | } |
---|
[2566] | 721 | |
---|
[1429] | 722 | return $url; |
---|
[0] | 723 | } |
---|
[2566] | 724 | |
---|
[0] | 725 | private function getCategoryCursor($cur,$id=null) |
---|
| 726 | { |
---|
| 727 | if ($cur->cat_title == '') { |
---|
| 728 | throw new Exception(__('You must provide a category title')); |
---|
| 729 | } |
---|
[2566] | 730 | |
---|
[0] | 731 | # If we don't have any cat_url, let's do one |
---|
| 732 | if ($cur->cat_url == '') { |
---|
| 733 | $cur->cat_url = text::tidyURL($cur->cat_title,false); |
---|
| 734 | } |
---|
[2566] | 735 | |
---|
[0] | 736 | # Still empty ? |
---|
| 737 | if ($cur->cat_url == '') { |
---|
| 738 | throw new Exception(__('You must provide a category URL')); |
---|
| 739 | } else { |
---|
| 740 | $cur->cat_url = text::tidyURL($cur->cat_url,true); |
---|
| 741 | } |
---|
[2566] | 742 | |
---|
[0] | 743 | # Check if title or url are unique |
---|
[1429] | 744 | $cur->cat_url = $this->checkCategory($cur->cat_title,$cur->cat_url,$id); |
---|
[2566] | 745 | |
---|
[0] | 746 | if ($cur->cat_desc !== null) { |
---|
| 747 | $cur->cat_desc = $this->core->HTMLfilter($cur->cat_desc); |
---|
| 748 | } |
---|
| 749 | } |
---|
| 750 | //@} |
---|
[2566] | 751 | |
---|
[0] | 752 | /// @name Entries management methods |
---|
| 753 | //@{ |
---|
| 754 | /** |
---|
| 755 | Retrieves entries. <b>$params</b> is an array taking the following |
---|
| 756 | optionnal parameters: |
---|
[2566] | 757 | |
---|
[0] | 758 | - no_content: Don't retrieve entry content (excerpt and content) |
---|
| 759 | - post_type: Get only entries with given type (default "post", array for many types and '' for no type) |
---|
[1541] | 760 | - post_id: (integer or array) Get entry with given post_id |
---|
[0] | 761 | - post_url: Get entry with given post_url field |
---|
| 762 | - user_id: (integer) Get entries belonging to given user ID |
---|
| 763 | - cat_id: (string or array) Get entries belonging to given category ID |
---|
| 764 | - cat_id_not: deprecated (use cat_id with "id ?not" instead) |
---|
| 765 | - cat_url: (string or array) Get entries belonging to given category URL |
---|
| 766 | - cat_url_not: deprecated (use cat_url with "url ?not" instead) |
---|
| 767 | - post_status: (integer) Get entries with given post_status |
---|
| 768 | - post_selected: (boolean) Get select flaged entries |
---|
| 769 | - post_year: (integer) Get entries with given year |
---|
| 770 | - post_month: (integer) Get entries with given month |
---|
| 771 | - post_day: (integer) Get entries with given day |
---|
| 772 | - post_lang: Get entries with given language code |
---|
| 773 | - search: Get entries corresponding of the following search string |
---|
| 774 | - columns: (array) More columns to retrieve |
---|
| 775 | - sql: Append SQL string at the end of the query |
---|
| 776 | - from: Append SQL string after "FROM" statement in query |
---|
| 777 | - order: Order of results (default "ORDER BY post_dt DES") |
---|
| 778 | - limit: Limit parameter |
---|
[774] | 779 | - sql_only : return the sql request instead of results. Only ids are selected |
---|
[1541] | 780 | - exclude_post_id : (integer or array) Exclude entries with given post_id |
---|
[2566] | 781 | |
---|
[0] | 782 | Please note that on every cat_id or cat_url, you can add ?not to exclude |
---|
| 783 | the category and ?sub to get subcategories. |
---|
[2566] | 784 | |
---|
[0] | 785 | @param params <b>array</b> Parameters |
---|
| 786 | @param count_only <b>boolean</b> Only counts results |
---|
[191] | 787 | @return <b>record</b> A record with some more capabilities or the SQL request |
---|
[0] | 788 | */ |
---|
[774] | 789 | public function getPosts($params=array(),$count_only=false) |
---|
[0] | 790 | { |
---|
[817] | 791 | # --BEHAVIOR-- coreBlogBeforeGetPosts |
---|
[819] | 792 | $params = new ArrayObject($params); |
---|
[817] | 793 | $this->core->callBehavior('coreBlogBeforeGetPosts',$params); |
---|
| 794 | |
---|
[0] | 795 | if ($count_only) |
---|
| 796 | { |
---|
[2315] | 797 | $strReq = 'SELECT count(DISTINCT P.post_id) '; |
---|
[0] | 798 | } |
---|
[2566] | 799 | elseif (!empty($params['sql_only'])) |
---|
[774] | 800 | { |
---|
[2317] | 801 | $strReq = 'SELECT P.post_id '; |
---|
[774] | 802 | } |
---|
[0] | 803 | else |
---|
| 804 | { |
---|
| 805 | if (!empty($params['no_content'])) { |
---|
| 806 | $content_req = ''; |
---|
| 807 | } else { |
---|
| 808 | $content_req = |
---|
| 809 | 'post_excerpt, post_excerpt_xhtml, '. |
---|
| 810 | 'post_content, post_content_xhtml, post_notes, '; |
---|
| 811 | } |
---|
[2566] | 812 | |
---|
[0] | 813 | if (!empty($params['columns']) && is_array($params['columns'])) { |
---|
| 814 | $content_req .= implode(', ',$params['columns']).', '; |
---|
| 815 | } |
---|
[2566] | 816 | |
---|
[2318] | 817 | |
---|
| 818 | $strReq = |
---|
| 819 | 'SELECT P.post_id, P.blog_id, P.user_id, P.cat_id, post_dt, '. |
---|
[0] | 820 | 'post_tz, post_creadt, post_upddt, post_format, post_password, '. |
---|
| 821 | 'post_url, post_lang, post_title, '.$content_req. |
---|
| 822 | 'post_type, post_meta, post_status, post_selected, post_position, '. |
---|
| 823 | 'post_open_comment, post_open_tb, nb_comment, nb_trackback, '. |
---|
| 824 | 'U.user_name, U.user_firstname, U.user_displayname, U.user_email, '. |
---|
| 825 | 'U.user_url, '. |
---|
| 826 | 'C.cat_title, C.cat_url, C.cat_desc '; |
---|
| 827 | } |
---|
[2566] | 828 | |
---|
[0] | 829 | $strReq .= |
---|
| 830 | 'FROM '.$this->prefix.'post P '. |
---|
| 831 | 'INNER JOIN '.$this->prefix.'user U ON U.user_id = P.user_id '. |
---|
| 832 | 'LEFT OUTER JOIN '.$this->prefix.'category C ON P.cat_id = C.cat_id '; |
---|
[2309] | 833 | |
---|
[0] | 834 | if (!empty($params['from'])) { |
---|
| 835 | $strReq .= $params['from'].' '; |
---|
| 836 | } |
---|
[2566] | 837 | |
---|
[0] | 838 | $strReq .= |
---|
| 839 | "WHERE P.blog_id = '".$this->con->escape($this->id)."' "; |
---|
[2566] | 840 | |
---|
[0] | 841 | if (!$this->core->auth->check('contentadmin',$this->id)) { |
---|
| 842 | $strReq .= 'AND ((post_status = 1 '; |
---|
[2566] | 843 | |
---|
[0] | 844 | if ($this->without_password) { |
---|
| 845 | $strReq .= 'AND post_password IS NULL '; |
---|
| 846 | } |
---|
| 847 | $strReq .= ') '; |
---|
[2566] | 848 | |
---|
[0] | 849 | if ($this->core->auth->userID()) { |
---|
| 850 | $strReq .= "OR P.user_id = '".$this->con->escape($this->core->auth->userID())."')"; |
---|
| 851 | } else { |
---|
| 852 | $strReq .= ') '; |
---|
| 853 | } |
---|
| 854 | } |
---|
[2566] | 855 | |
---|
[0] | 856 | #Adding parameters |
---|
| 857 | if (isset($params['post_type'])) |
---|
| 858 | { |
---|
[407] | 859 | if (is_array($params['post_type']) || $params['post_type'] != '') { |
---|
| 860 | $strReq .= 'AND post_type '.$this->con->in($params['post_type']); |
---|
| 861 | } |
---|
[0] | 862 | } |
---|
| 863 | else |
---|
| 864 | { |
---|
| 865 | $strReq .= "AND post_type = 'post' "; |
---|
| 866 | } |
---|
[2566] | 867 | |
---|
[836] | 868 | if (isset($params['post_id']) && $params['post_id'] !== '') { |
---|
[0] | 869 | if (is_array($params['post_id'])) { |
---|
| 870 | array_walk($params['post_id'],create_function('&$v,$k','if($v!==null){$v=(integer)$v;}')); |
---|
| 871 | } else { |
---|
| 872 | $params['post_id'] = array((integer) $params['post_id']); |
---|
| 873 | } |
---|
| 874 | $strReq .= 'AND P.post_id '.$this->con->in($params['post_id']); |
---|
| 875 | } |
---|
[2566] | 876 | |
---|
[1541] | 877 | if (isset($params['exclude_post_id']) && $params['exclude_post_id'] !== '') { |
---|
| 878 | if (is_array($params['exclude_post_id'])) { |
---|
| 879 | array_walk($params['exclude_post_id'],create_function('&$v,$k','if($v!==null){$v=(integer)$v;}')); |
---|
| 880 | } else { |
---|
| 881 | $params['exclude_post_id'] = array((integer) $params['exclude_post_id']); |
---|
| 882 | } |
---|
[1544] | 883 | $strReq .= 'AND P.post_id NOT '.$this->con->in($params['exclude_post_id']); |
---|
[1540] | 884 | } |
---|
[2566] | 885 | |
---|
[836] | 886 | if (isset($params['post_url']) && $params['post_url'] !== '') { |
---|
[0] | 887 | $strReq .= "AND post_url = '".$this->con->escape($params['post_url'])."' "; |
---|
| 888 | } |
---|
[2566] | 889 | |
---|
[0] | 890 | if (!empty($params['user_id'])) { |
---|
| 891 | $strReq .= "AND U.user_id = '".$this->con->escape($params['user_id'])."' "; |
---|
| 892 | } |
---|
[2566] | 893 | |
---|
[836] | 894 | if (isset($params['cat_id']) && $params['cat_id'] !== '') |
---|
[0] | 895 | { |
---|
| 896 | if (!is_array($params['cat_id'])) { |
---|
| 897 | $params['cat_id'] = array($params['cat_id']); |
---|
| 898 | } |
---|
| 899 | if (!empty($params['cat_id_not'])) { |
---|
| 900 | array_walk($params['cat_id'],create_function('&$v,$k','$v=$v." ?not";')); |
---|
| 901 | } |
---|
| 902 | $strReq .= 'AND '.$this->getPostsCategoryFilter($params['cat_id'],'cat_id').' '; |
---|
| 903 | } |
---|
[836] | 904 | elseif (isset($params['cat_url']) && $params['cat_url'] !== '') |
---|
[0] | 905 | { |
---|
| 906 | if (!is_array($params['cat_url'])) { |
---|
| 907 | $params['cat_url'] = array($params['cat_url']); |
---|
| 908 | } |
---|
| 909 | if (!empty($params['cat_url_not'])) { |
---|
| 910 | array_walk($params['cat_url'],create_function('&$v,$k','$v=$v." ?not";')); |
---|
| 911 | } |
---|
| 912 | $strReq .= 'AND '.$this->getPostsCategoryFilter($params['cat_url'],'cat_url').' '; |
---|
| 913 | } |
---|
[2566] | 914 | |
---|
[0] | 915 | /* Other filters */ |
---|
| 916 | if (isset($params['post_status'])) { |
---|
| 917 | $strReq .= 'AND post_status = '.(integer) $params['post_status'].' '; |
---|
| 918 | } |
---|
[2566] | 919 | |
---|
[0] | 920 | if (isset($params['post_selected'])) { |
---|
| 921 | $strReq .= 'AND post_selected = '.(integer) $params['post_selected'].' '; |
---|
| 922 | } |
---|
[2566] | 923 | |
---|
[0] | 924 | if (!empty($params['post_year'])) { |
---|
| 925 | $strReq .= 'AND '.$this->con->dateFormat('post_dt','%Y').' = '. |
---|
| 926 | "'".sprintf('%04d',$params['post_year'])."' "; |
---|
| 927 | } |
---|
[2566] | 928 | |
---|
[0] | 929 | if (!empty($params['post_month'])) { |
---|
| 930 | $strReq .= 'AND '.$this->con->dateFormat('post_dt','%m').' = '. |
---|
| 931 | "'".sprintf('%02d',$params['post_month'])."' "; |
---|
| 932 | } |
---|
[2566] | 933 | |
---|
[0] | 934 | if (!empty($params['post_day'])) { |
---|
| 935 | $strReq .= 'AND '.$this->con->dateFormat('post_dt','%d').' = '. |
---|
| 936 | "'".sprintf('%02d',$params['post_day'])."' "; |
---|
| 937 | } |
---|
[2566] | 938 | |
---|
[0] | 939 | if (!empty($params['post_lang'])) { |
---|
| 940 | $strReq .= "AND P.post_lang = '".$this->con->escape($params['post_lang'])."' "; |
---|
| 941 | } |
---|
[2566] | 942 | |
---|
[0] | 943 | if (!empty($params['search'])) |
---|
| 944 | { |
---|
| 945 | $words = text::splitWords($params['search']); |
---|
[2566] | 946 | |
---|
[0] | 947 | if (!empty($words)) |
---|
| 948 | { |
---|
| 949 | # --BEHAVIOR-- corePostSearch |
---|
| 950 | if ($this->core->hasBehavior('corePostSearch')) { |
---|
| 951 | $this->core->callBehavior('corePostSearch',$this->core,array(&$words,&$strReq,&$params)); |
---|
| 952 | } |
---|
[2566] | 953 | |
---|
[0] | 954 | if ($words) |
---|
| 955 | { |
---|
| 956 | foreach ($words as $i => $w) { |
---|
| 957 | $words[$i] = "post_words LIKE '%".$this->con->escape($w)."%'"; |
---|
| 958 | } |
---|
| 959 | $strReq .= 'AND '.implode(' AND ',$words).' '; |
---|
| 960 | } |
---|
| 961 | } |
---|
| 962 | } |
---|
[2566] | 963 | |
---|
[2309] | 964 | if (isset($params['media'])) { |
---|
| 965 | if ($params['media'] == '0') { |
---|
[2318] | 966 | $strReq .= 'AND NOT '; |
---|
| 967 | } else { |
---|
[2566] | 968 | $strReq .= 'AND '; |
---|
[2309] | 969 | } |
---|
[2318] | 970 | $strReq .= 'EXISTS (SELECT M.post_id FROM '.$this->prefix.'post_media M '. |
---|
| 971 | 'WHERE M.post_id = P.post_id '; |
---|
[2317] | 972 | if (isset($params['link_type'])) { |
---|
[2318] | 973 | $strReq .= " AND M.link_type ".$this->con->in($params['link_type'])." "; |
---|
[2317] | 974 | } |
---|
| 975 | $strReq .= ")"; |
---|
[2309] | 976 | } |
---|
| 977 | |
---|
[3033] | 978 | if (!empty($params['where'])) { |
---|
| 979 | $strReq .= $params['where'].' '; |
---|
| 980 | } |
---|
| 981 | |
---|
[0] | 982 | if (!empty($params['sql'])) { |
---|
| 983 | $strReq .= $params['sql'].' '; |
---|
| 984 | } |
---|
[2309] | 985 | |
---|
[0] | 986 | if (!$count_only) |
---|
| 987 | { |
---|
| 988 | if (!empty($params['order'])) { |
---|
| 989 | $strReq .= 'ORDER BY '.$this->con->escape($params['order']).' '; |
---|
| 990 | } else { |
---|
| 991 | $strReq .= 'ORDER BY post_dt DESC '; |
---|
| 992 | } |
---|
| 993 | } |
---|
[2566] | 994 | |
---|
[0] | 995 | if (!$count_only && !empty($params['limit'])) { |
---|
| 996 | $strReq .= $this->con->limit($params['limit']); |
---|
| 997 | } |
---|
[2566] | 998 | |
---|
[774] | 999 | if (!empty($params['sql_only'])) { |
---|
[191] | 1000 | return $strReq; |
---|
| 1001 | } |
---|
[2309] | 1002 | |
---|
[0] | 1003 | $rs = $this->con->select($strReq); |
---|
| 1004 | $rs->core = $this->core; |
---|
| 1005 | $rs->_nb_media = array(); |
---|
| 1006 | $rs->extend('rsExtPost'); |
---|
[2566] | 1007 | |
---|
[0] | 1008 | # --BEHAVIOR-- coreBlogGetPosts |
---|
| 1009 | $this->core->callBehavior('coreBlogGetPosts',$rs); |
---|
[2566] | 1010 | |
---|
[3134] | 1011 | # --BEHAVIOR-- coreBlogAfterGetPosts |
---|
| 1012 | $alt = new arrayObject(array('rs' => null,'params' => $params,'count_only' => $count_only)); |
---|
| 1013 | $this->core->callBehavior('coreBlogAfterGetPosts',$rs,$alt); |
---|
| 1014 | if ($alt['rs'] instanceof record) { |
---|
| 1015 | $rs = $alt['rs']; |
---|
| 1016 | } |
---|
| 1017 | |
---|
[0] | 1018 | return $rs; |
---|
| 1019 | } |
---|
[2566] | 1020 | |
---|
[0] | 1021 | /** |
---|
| 1022 | Returns a record with post id, title and date for next or previous post |
---|
| 1023 | according to the post ID. |
---|
| 1024 | $dir could be 1 (next post) or -1 (previous post). |
---|
[2566] | 1025 | |
---|
[0] | 1026 | @param post_id <b>integer</b> Post ID |
---|
| 1027 | @param dir <b>integer</b> Search direction |
---|
| 1028 | @param restrict_to_category <b>boolean</b> Restrict to post with same category |
---|
| 1029 | @param restrict_to_lang <b>boolean</b> Restrict to post with same lang |
---|
| 1030 | @return record |
---|
| 1031 | */ |
---|
| 1032 | public function getNextPost($post,$dir,$restrict_to_category=false, $restrict_to_lang=false) |
---|
| 1033 | { |
---|
| 1034 | $dt = $post->post_dt; |
---|
| 1035 | $post_id = (integer) $post->post_id; |
---|
[2566] | 1036 | |
---|
[0] | 1037 | if($dir > 0) { |
---|
[336] | 1038 | $sign = '>'; |
---|
| 1039 | $order = 'ASC'; |
---|
| 1040 | } |
---|
| 1041 | else { |
---|
| 1042 | $sign = '<'; |
---|
| 1043 | $order = 'DESC'; |
---|
| 1044 | } |
---|
[2566] | 1045 | |
---|
[0] | 1046 | $params['post_type'] = $post->post_type; |
---|
| 1047 | $params['limit'] = 1; |
---|
| 1048 | $params['order'] = 'post_dt '.$order.', P.post_id '.$order; |
---|
| 1049 | $params['sql'] = |
---|
| 1050 | 'AND ( '. |
---|
| 1051 | " (post_dt = '".$this->con->escape($dt)."' AND P.post_id ".$sign." ".$post_id.") ". |
---|
| 1052 | " OR post_dt ".$sign." '".$this->con->escape($dt)."' ". |
---|
| 1053 | ') '; |
---|
[2566] | 1054 | |
---|
[0] | 1055 | if ($restrict_to_category) { |
---|
| 1056 | $params['sql'] .= $post->cat_id ? 'AND P.cat_id = '.(integer) $post->cat_id.' ' : 'AND P.cat_id IS NULL '; |
---|
| 1057 | } |
---|
[2566] | 1058 | |
---|
[0] | 1059 | if ($restrict_to_lang) { |
---|
| 1060 | $params['sql'] .= $post->post_lang ? 'AND P.post_lang = \''. $this->con->escape($post->post_lang) .'\' ': 'AND P.post_lang IS NULL '; |
---|
| 1061 | } |
---|
[2566] | 1062 | |
---|
[0] | 1063 | $rs = $this->getPosts($params); |
---|
[2566] | 1064 | |
---|
[0] | 1065 | if ($rs->isEmpty()) { |
---|
| 1066 | return null; |
---|
| 1067 | } |
---|
[2566] | 1068 | |
---|
[0] | 1069 | return $rs; |
---|
| 1070 | } |
---|
[2566] | 1071 | |
---|
[0] | 1072 | /** |
---|
| 1073 | Retrieves different languages and post count on blog, based on post_lang |
---|
| 1074 | field. <var>$params</var> is an array taking the following optionnal |
---|
| 1075 | parameters: |
---|
[2566] | 1076 | |
---|
[0] | 1077 | - post_type: Get only entries with given type (default "post", '' for no type) |
---|
| 1078 | - lang: retrieve post count for selected lang |
---|
| 1079 | - order: order statement (default post_lang DESC) |
---|
[2566] | 1080 | |
---|
[0] | 1081 | @param params <b>array</b> Parameters |
---|
| 1082 | @return record |
---|
| 1083 | */ |
---|
| 1084 | public function getLangs($params=array()) |
---|
| 1085 | { |
---|
| 1086 | $strReq = 'SELECT COUNT(post_id) as nb_post, post_lang '. |
---|
| 1087 | 'FROM '.$this->prefix.'post '. |
---|
| 1088 | "WHERE blog_id = '".$this->con->escape($this->id)."' ". |
---|
| 1089 | "AND post_lang <> '' ". |
---|
| 1090 | "AND post_lang IS NOT NULL "; |
---|
[2566] | 1091 | |
---|
[0] | 1092 | if (!$this->core->auth->check('contentadmin',$this->id)) { |
---|
| 1093 | $strReq .= 'AND ((post_status = 1 '; |
---|
[2566] | 1094 | |
---|
[0] | 1095 | if ($this->without_password) { |
---|
| 1096 | $strReq .= 'AND post_password IS NULL '; |
---|
| 1097 | } |
---|
| 1098 | $strReq .= ') '; |
---|
[2566] | 1099 | |
---|
[0] | 1100 | if ($this->core->auth->userID()) { |
---|
| 1101 | $strReq .= "OR user_id = '".$this->con->escape($this->core->auth->userID())."')"; |
---|
| 1102 | } else { |
---|
| 1103 | $strReq .= ') '; |
---|
| 1104 | } |
---|
| 1105 | } |
---|
[2566] | 1106 | |
---|
[0] | 1107 | if (isset($params['post_type'])) { |
---|
| 1108 | if ($params['post_type'] != '') { |
---|
| 1109 | $strReq .= "AND post_type = '".$this->con->escape($params['post_type'])."' "; |
---|
| 1110 | } |
---|
| 1111 | } else { |
---|
| 1112 | $strReq .= "AND post_type = 'post' "; |
---|
| 1113 | } |
---|
[2566] | 1114 | |
---|
[0] | 1115 | if (isset($params['lang'])) { |
---|
| 1116 | $strReq .= "AND post_lang = '".$this->con->escape($params['lang'])."' "; |
---|
| 1117 | } |
---|
[2566] | 1118 | |
---|
[0] | 1119 | $strReq .= 'GROUP BY post_lang '; |
---|
[2566] | 1120 | |
---|
[0] | 1121 | $order = 'desc'; |
---|
| 1122 | if (!empty($params['order']) && preg_match('/^(desc|asc)$/i',$params['order'])) { |
---|
| 1123 | $order = $params['order']; |
---|
| 1124 | } |
---|
| 1125 | $strReq .= 'ORDER BY post_lang '.$order.' '; |
---|
[2566] | 1126 | |
---|
[0] | 1127 | return $this->con->select($strReq); |
---|
| 1128 | } |
---|
[2566] | 1129 | |
---|
[0] | 1130 | /** |
---|
| 1131 | Returns a record with all distinct blog dates and post count. |
---|
| 1132 | <var>$params</var> is an array taking the following optionnal parameters: |
---|
[2566] | 1133 | |
---|
[0] | 1134 | - type: (day|month|year) Get days, months or years |
---|
| 1135 | - year: (integer) Get dates for given year |
---|
| 1136 | - month: (integer) Get dates for given month |
---|
| 1137 | - day: (integer) Get dates for given day |
---|
| 1138 | - cat_id: (integer) Category ID filter |
---|
| 1139 | - cat_url: Category URL filter |
---|
| 1140 | - post_lang: lang of the posts |
---|
| 1141 | - next: Get date following match |
---|
| 1142 | - previous: Get date before match |
---|
| 1143 | - order: Sort by date "ASC" or "DESC" |
---|
[2566] | 1144 | |
---|
[0] | 1145 | @param params <b>array</b> Parameters array |
---|
| 1146 | @return record |
---|
| 1147 | */ |
---|
| 1148 | public function getDates($params=array()) |
---|
| 1149 | { |
---|
| 1150 | $dt_f = '%Y-%m-%d'; |
---|
| 1151 | $dt_fc = '%Y%m%d'; |
---|
| 1152 | if (isset($params['type'])) { |
---|
| 1153 | if ($params['type'] == 'year') { |
---|
| 1154 | $dt_f = '%Y-01-01'; |
---|
| 1155 | $dt_fc = '%Y0101'; |
---|
| 1156 | } elseif ($params['type'] == 'month') { |
---|
| 1157 | $dt_f = '%Y-%m-01'; |
---|
| 1158 | $dt_fc = '%Y%m01'; |
---|
| 1159 | } |
---|
| 1160 | } |
---|
| 1161 | $dt_f .= ' 00:00:00'; |
---|
| 1162 | $dt_fc .= '000000'; |
---|
[2566] | 1163 | |
---|
[0] | 1164 | $cat_field = $catReq = $limit = ''; |
---|
[2566] | 1165 | |
---|
[836] | 1166 | if (isset($params['cat_id']) && $params['cat_id'] !== '') { |
---|
[0] | 1167 | $catReq = 'AND P.cat_id = '.(integer) $params['cat_id'].' '; |
---|
| 1168 | $cat_field = ', C.cat_url '; |
---|
[836] | 1169 | } elseif (isset($params['cat_url']) && $params['cat_url'] !== '') { |
---|
[0] | 1170 | $catReq = "AND C.cat_url = '".$this->con->escape($params['cat_url'])."' "; |
---|
| 1171 | $cat_field = ', C.cat_url '; |
---|
| 1172 | } |
---|
| 1173 | if (!empty($params['post_lang'])) { |
---|
| 1174 | $catReq = 'AND P.post_lang = \''. $params['post_lang'].'\' '; |
---|
| 1175 | } |
---|
[2566] | 1176 | |
---|
[0] | 1177 | $strReq = 'SELECT DISTINCT('.$this->con->dateFormat('post_dt',$dt_f).') AS dt '. |
---|
| 1178 | $cat_field. |
---|
| 1179 | ',COUNT(P.post_id) AS nb_post '. |
---|
| 1180 | 'FROM '.$this->prefix.'post P LEFT JOIN '.$this->prefix.'category C '. |
---|
| 1181 | 'ON P.cat_id = C.cat_id '. |
---|
| 1182 | "WHERE P.blog_id = '".$this->con->escape($this->id)."' ". |
---|
| 1183 | $catReq; |
---|
[2566] | 1184 | |
---|
[0] | 1185 | if (!$this->core->auth->check('contentadmin',$this->id)) { |
---|
| 1186 | $strReq .= 'AND ((post_status = 1 '; |
---|
[2566] | 1187 | |
---|
[0] | 1188 | if ($this->without_password) { |
---|
| 1189 | $strReq .= 'AND post_password IS NULL '; |
---|
| 1190 | } |
---|
| 1191 | $strReq .= ') '; |
---|
[2566] | 1192 | |
---|
[0] | 1193 | if ($this->core->auth->userID()) { |
---|
| 1194 | $strReq .= "OR P.user_id = '".$this->con->escape($this->core->auth->userID())."')"; |
---|
| 1195 | } else { |
---|
| 1196 | $strReq .= ') '; |
---|
| 1197 | } |
---|
| 1198 | } |
---|
[2566] | 1199 | |
---|
[0] | 1200 | if (!empty($params['post_type'])) { |
---|
| 1201 | $strReq .= "AND post_type ".$this->con->in($params['post_type'])." "; |
---|
| 1202 | } else { |
---|
| 1203 | $strReq .= "AND post_type = 'post' "; |
---|
| 1204 | } |
---|
[2566] | 1205 | |
---|
[0] | 1206 | if (!empty($params['year'])) { |
---|
| 1207 | $strReq .= 'AND '.$this->con->dateFormat('post_dt','%Y')." = '".sprintf('%04d',$params['year'])."' "; |
---|
| 1208 | } |
---|
[2566] | 1209 | |
---|
[0] | 1210 | if (!empty($params['month'])) { |
---|
| 1211 | $strReq .= 'AND '.$this->con->dateFormat('post_dt','%m')." = '".sprintf('%02d',$params['month'])."' "; |
---|
| 1212 | } |
---|
[2566] | 1213 | |
---|
[0] | 1214 | if (!empty($params['day'])) { |
---|
| 1215 | $strReq .= 'AND '.$this->con->dateFormat('post_dt','%d')." = '".sprintf('%02d',$params['day'])."' "; |
---|
| 1216 | } |
---|
[2566] | 1217 | |
---|
[0] | 1218 | # Get next or previous date |
---|
| 1219 | if (!empty($params['next']) || !empty($params['previous'])) |
---|
| 1220 | { |
---|
| 1221 | if (!empty($params['next'])) { |
---|
| 1222 | $pdir = ' > '; |
---|
| 1223 | $params['order'] = 'asc'; |
---|
| 1224 | $dt = $params['next']; |
---|
| 1225 | } else { |
---|
| 1226 | $pdir = ' < '; |
---|
| 1227 | $params['order'] = 'desc'; |
---|
| 1228 | $dt = $params['previous']; |
---|
| 1229 | } |
---|
[2566] | 1230 | |
---|
[0] | 1231 | $dt = date('YmdHis',strtotime($dt)); |
---|
[2566] | 1232 | |
---|
[0] | 1233 | $strReq .= 'AND '.$this->con->dateFormat('post_dt',$dt_fc).$pdir."'".$dt."' "; |
---|
| 1234 | $limit = $this->con->limit(1); |
---|
| 1235 | } |
---|
[2566] | 1236 | |
---|
[0] | 1237 | $strReq .= 'GROUP BY dt '.$cat_field; |
---|
[2566] | 1238 | |
---|
[0] | 1239 | $order = 'desc'; |
---|
| 1240 | if (!empty($params['order']) && preg_match('/^(desc|asc)$/i',$params['order'])) { |
---|
| 1241 | $order = $params['order']; |
---|
| 1242 | } |
---|
[2566] | 1243 | |
---|
[0] | 1244 | $strReq .= |
---|
| 1245 | 'ORDER BY dt '.$order.' '. |
---|
| 1246 | $limit; |
---|
[2566] | 1247 | |
---|
[0] | 1248 | $rs = $this->con->select($strReq); |
---|
| 1249 | $rs->extend('rsExtDates'); |
---|
| 1250 | return $rs; |
---|
| 1251 | } |
---|
[2566] | 1252 | |
---|
[0] | 1253 | /** |
---|
| 1254 | Creates a new entry. Takes a cursor as input and returns the new entry |
---|
| 1255 | ID. |
---|
[2566] | 1256 | |
---|
[0] | 1257 | @param cur <b>cursor</b> Post cursor |
---|
| 1258 | @return <b>integer</b> New post ID |
---|
| 1259 | */ |
---|
| 1260 | public function addPost($cur) |
---|
| 1261 | { |
---|
| 1262 | if (!$this->core->auth->check('usage,contentadmin',$this->id)) { |
---|
| 1263 | throw new Exception(__('You are not allowed to create an entry')); |
---|
| 1264 | } |
---|
[2566] | 1265 | |
---|
[0] | 1266 | $this->con->writeLock($this->prefix.'post'); |
---|
| 1267 | try |
---|
| 1268 | { |
---|
| 1269 | # Get ID |
---|
| 1270 | $rs = $this->con->select( |
---|
| 1271 | 'SELECT MAX(post_id) '. |
---|
[2566] | 1272 | 'FROM '.$this->prefix.'post ' |
---|
[0] | 1273 | ); |
---|
[2566] | 1274 | |
---|
[0] | 1275 | $cur->post_id = (integer) $rs->f(0) + 1; |
---|
| 1276 | $cur->blog_id = (string) $this->id; |
---|
| 1277 | $cur->post_creadt = date('Y-m-d H:i:s'); |
---|
| 1278 | $cur->post_upddt = date('Y-m-d H:i:s'); |
---|
| 1279 | $cur->post_tz = $this->core->auth->getInfo('user_tz'); |
---|
[2566] | 1280 | |
---|
[0] | 1281 | # Post excerpt and content |
---|
| 1282 | $this->getPostContent($cur,$cur->post_id); |
---|
[2566] | 1283 | |
---|
[0] | 1284 | $this->getPostCursor($cur); |
---|
[2566] | 1285 | |
---|
[0] | 1286 | $cur->post_url = $this->getPostURL($cur->post_url,$cur->post_dt,$cur->post_title,$cur->post_id); |
---|
[2566] | 1287 | |
---|
[0] | 1288 | if (!$this->core->auth->check('publish,contentadmin',$this->id)) { |
---|
| 1289 | $cur->post_status = -2; |
---|
| 1290 | } |
---|
[2566] | 1291 | |
---|
[0] | 1292 | # --BEHAVIOR-- coreBeforePostCreate |
---|
| 1293 | $this->core->callBehavior('coreBeforePostCreate',$this,$cur); |
---|
[2566] | 1294 | |
---|
[0] | 1295 | $cur->insert(); |
---|
| 1296 | $this->con->unlock(); |
---|
| 1297 | } |
---|
| 1298 | catch (Exception $e) |
---|
| 1299 | { |
---|
| 1300 | $this->con->unlock(); |
---|
| 1301 | throw $e; |
---|
| 1302 | } |
---|
[2566] | 1303 | |
---|
[0] | 1304 | # --BEHAVIOR-- coreAfterPostCreate |
---|
| 1305 | $this->core->callBehavior('coreAfterPostCreate',$this,$cur); |
---|
[2566] | 1306 | |
---|
[0] | 1307 | $this->triggerBlog(); |
---|
[2566] | 1308 | |
---|
[0] | 1309 | return $cur->post_id; |
---|
| 1310 | } |
---|
[2566] | 1311 | |
---|
[0] | 1312 | /** |
---|
| 1313 | Updates an existing post. |
---|
[2566] | 1314 | |
---|
[0] | 1315 | @param id <b>integer</b> Post ID |
---|
| 1316 | @param cur <b>cursor</b> Post cursor |
---|
| 1317 | */ |
---|
| 1318 | public function updPost($id,$cur) |
---|
| 1319 | { |
---|
| 1320 | if (!$this->core->auth->check('usage,contentadmin',$this->id)) { |
---|
| 1321 | throw new Exception(__('You are not allowed to update entries')); |
---|
| 1322 | } |
---|
[2566] | 1323 | |
---|
[0] | 1324 | $id = (integer) $id; |
---|
[2566] | 1325 | |
---|
[0] | 1326 | if (empty($id)) { |
---|
| 1327 | throw new Exception(__('No such entry ID')); |
---|
| 1328 | } |
---|
[2566] | 1329 | |
---|
[0] | 1330 | # Post excerpt and content |
---|
| 1331 | $this->getPostContent($cur,$id); |
---|
[2566] | 1332 | |
---|
[0] | 1333 | $this->getPostCursor($cur); |
---|
[2566] | 1334 | |
---|
[0] | 1335 | if ($cur->post_url !== null) { |
---|
| 1336 | $cur->post_url = $this->getPostURL($cur->post_url,$cur->post_dt,$cur->post_title,$id); |
---|
| 1337 | } |
---|
[2566] | 1338 | |
---|
[0] | 1339 | if (!$this->core->auth->check('publish,contentadmin',$this->id)) { |
---|
| 1340 | $cur->unsetField('post_status'); |
---|
| 1341 | } |
---|
[2566] | 1342 | |
---|
[0] | 1343 | $cur->post_upddt = date('Y-m-d H:i:s'); |
---|
[2566] | 1344 | |
---|
[335] | 1345 | #If user is only "usage", we need to check the post's owner |
---|
[0] | 1346 | if (!$this->core->auth->check('contentadmin',$this->id)) |
---|
| 1347 | { |
---|
| 1348 | $strReq = 'SELECT post_id '. |
---|
| 1349 | 'FROM '.$this->prefix.'post '. |
---|
| 1350 | 'WHERE post_id = '.$id.' '. |
---|
| 1351 | "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; |
---|
[2566] | 1352 | |
---|
[0] | 1353 | $rs = $this->con->select($strReq); |
---|
[2566] | 1354 | |
---|
[0] | 1355 | if ($rs->isEmpty()) { |
---|
| 1356 | throw new Exception(__('You are not allowed to edit this entry')); |
---|
| 1357 | } |
---|
| 1358 | } |
---|
[2566] | 1359 | |
---|
[0] | 1360 | # --BEHAVIOR-- coreBeforePostUpdate |
---|
| 1361 | $this->core->callBehavior('coreBeforePostUpdate',$this,$cur); |
---|
[2566] | 1362 | |
---|
[0] | 1363 | $cur->update('WHERE post_id = '.$id.' '); |
---|
[2566] | 1364 | |
---|
[0] | 1365 | # --BEHAVIOR-- coreAfterPostUpdate |
---|
| 1366 | $this->core->callBehavior('coreAfterPostUpdate',$this,$cur); |
---|
[2566] | 1367 | |
---|
[0] | 1368 | $this->triggerBlog(); |
---|
| 1369 | } |
---|
[2566] | 1370 | |
---|
[0] | 1371 | /** |
---|
| 1372 | Updates post status. |
---|
[2566] | 1373 | |
---|
[0] | 1374 | @param id <b>integer</b> Post ID |
---|
| 1375 | @param status <b>integer</b> Post status |
---|
| 1376 | */ |
---|
| 1377 | public function updPostStatus($id,$status) |
---|
| 1378 | { |
---|
[1030] | 1379 | $this->updPostsStatus($id,$status); |
---|
| 1380 | } |
---|
[2566] | 1381 | |
---|
[1030] | 1382 | /** |
---|
| 1383 | Updates posts status. |
---|
[2566] | 1384 | |
---|
[1030] | 1385 | @param ids <b>mixed</b> Post(s) ID(s) |
---|
| 1386 | @param status <b>integer</b> Post status |
---|
| 1387 | */ |
---|
| 1388 | public function updPostsStatus($ids,$status) |
---|
| 1389 | { |
---|
[0] | 1390 | if (!$this->core->auth->check('publish,contentadmin',$this->id)) { |
---|
| 1391 | throw new Exception(__('You are not allowed to change this entry status')); |
---|
| 1392 | } |
---|
[2566] | 1393 | |
---|
[1030] | 1394 | $posts_ids = dcUtils::cleanIds($ids); |
---|
[0] | 1395 | $status = (integer) $status; |
---|
[2566] | 1396 | |
---|
[1030] | 1397 | $strReq = "WHERE blog_id = '".$this->con->escape($this->id)."' ". |
---|
| 1398 | "AND post_id ".$this->con->in($posts_ids); |
---|
[2566] | 1399 | |
---|
[0] | 1400 | #If user can only publish, we need to check the post's owner |
---|
| 1401 | if (!$this->core->auth->check('contentadmin',$this->id)) |
---|
| 1402 | { |
---|
[1030] | 1403 | $strReq .= "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; |
---|
[0] | 1404 | } |
---|
[2566] | 1405 | |
---|
[0] | 1406 | $cur = $this->con->openCursor($this->prefix.'post'); |
---|
[2566] | 1407 | |
---|
[0] | 1408 | $cur->post_status = $status; |
---|
| 1409 | $cur->post_upddt = date('Y-m-d H:i:s'); |
---|
[2566] | 1410 | |
---|
[1030] | 1411 | $cur->update($strReq); |
---|
[0] | 1412 | $this->triggerBlog(); |
---|
| 1413 | } |
---|
[2566] | 1414 | |
---|
[1030] | 1415 | /** |
---|
| 1416 | Updates post selection. |
---|
[2566] | 1417 | |
---|
[1030] | 1418 | @param id <b>integer</b> Post ID |
---|
| 1419 | @param selected <b>integer</b> Is selected post |
---|
| 1420 | */ |
---|
[0] | 1421 | public function updPostSelected($id,$selected) |
---|
| 1422 | { |
---|
[1030] | 1423 | $this->updPostsSelected($id,$selected); |
---|
| 1424 | } |
---|
[2566] | 1425 | |
---|
[1030] | 1426 | /** |
---|
| 1427 | Updates posts selection. |
---|
[2566] | 1428 | |
---|
[1030] | 1429 | @param ids <b>mixed</b> Post(s) ID(s) |
---|
| 1430 | @param selected <b>integer</b> Is selected post(s) |
---|
| 1431 | */ |
---|
| 1432 | public function updPostsSelected($ids,$selected) |
---|
| 1433 | { |
---|
[0] | 1434 | if (!$this->core->auth->check('usage,contentadmin',$this->id)) { |
---|
| 1435 | throw new Exception(__('You are not allowed to change this entry category')); |
---|
| 1436 | } |
---|
[2566] | 1437 | |
---|
[1030] | 1438 | $posts_ids = dcUtils::cleanIds($ids); |
---|
[0] | 1439 | $selected = (boolean) $selected; |
---|
[2566] | 1440 | |
---|
[1030] | 1441 | $strReq = "WHERE blog_id = '".$this->con->escape($this->id)."' ". |
---|
| 1442 | "AND post_id ".$this->con->in($posts_ids); |
---|
[2566] | 1443 | |
---|
[0] | 1444 | # If user is only usage, we need to check the post's owner |
---|
| 1445 | if (!$this->core->auth->check('contentadmin',$this->id)) |
---|
| 1446 | { |
---|
[1030] | 1447 | $strReq .= "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; |
---|
[0] | 1448 | } |
---|
[2566] | 1449 | |
---|
[0] | 1450 | $cur = $this->con->openCursor($this->prefix.'post'); |
---|
[2566] | 1451 | |
---|
[0] | 1452 | $cur->post_selected = (integer) $selected; |
---|
| 1453 | $cur->post_upddt = date('Y-m-d H:i:s'); |
---|
[2566] | 1454 | |
---|
[1030] | 1455 | $cur->update($strReq); |
---|
[0] | 1456 | $this->triggerBlog(); |
---|
| 1457 | } |
---|
[2566] | 1458 | |
---|
[0] | 1459 | /** |
---|
| 1460 | Updates post category. <var>$cat_id</var> can be null. |
---|
[2566] | 1461 | |
---|
[0] | 1462 | @param id <b>integer</b> Post ID |
---|
| 1463 | @param cat_id <b>integer</b> Category ID |
---|
| 1464 | */ |
---|
| 1465 | public function updPostCategory($id,$cat_id) |
---|
| 1466 | { |
---|
[1030] | 1467 | $this->updPostsCategory($id,$cat_id); |
---|
| 1468 | } |
---|
[2566] | 1469 | |
---|
[1030] | 1470 | /** |
---|
| 1471 | Updates posts category. <var>$cat_id</var> can be null. |
---|
[2566] | 1472 | |
---|
[1030] | 1473 | @param ids <b>mixed</b> Post(s) ID(s) |
---|
| 1474 | @param cat_id <b>integer</b> Category ID |
---|
| 1475 | */ |
---|
| 1476 | public function updPostsCategory($ids,$cat_id) |
---|
| 1477 | { |
---|
[0] | 1478 | if (!$this->core->auth->check('usage,contentadmin',$this->id)) { |
---|
| 1479 | throw new Exception(__('You are not allowed to change this entry category')); |
---|
| 1480 | } |
---|
[2566] | 1481 | |
---|
[1030] | 1482 | $posts_ids = dcUtils::cleanIds($ids); |
---|
[0] | 1483 | $cat_id = (integer) $cat_id; |
---|
[2566] | 1484 | |
---|
[1030] | 1485 | $strReq = "WHERE blog_id = '".$this->con->escape($this->id)."' ". |
---|
| 1486 | "AND post_id ".$this->con->in($posts_ids); |
---|
[2566] | 1487 | |
---|
[0] | 1488 | # If user is only usage, we need to check the post's owner |
---|
| 1489 | if (!$this->core->auth->check('contentadmin',$this->id)) |
---|
| 1490 | { |
---|
[1030] | 1491 | $strReq .= "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; |
---|
[0] | 1492 | } |
---|
[2566] | 1493 | |
---|
[0] | 1494 | $cur = $this->con->openCursor($this->prefix.'post'); |
---|
[2566] | 1495 | |
---|
[0] | 1496 | $cur->cat_id = ($cat_id ? $cat_id : null); |
---|
| 1497 | $cur->post_upddt = date('Y-m-d H:i:s'); |
---|
[2566] | 1498 | |
---|
[1030] | 1499 | $cur->update($strReq); |
---|
[0] | 1500 | $this->triggerBlog(); |
---|
| 1501 | } |
---|
[2566] | 1502 | |
---|
[0] | 1503 | /** |
---|
[1015] | 1504 | Updates posts category. <var>$new_cat_id</var> can be null. |
---|
[2566] | 1505 | |
---|
[1015] | 1506 | @param old_cat_id <b>integer</b> Old category ID |
---|
| 1507 | @param new_cat_id <b>integer</b> New category ID |
---|
| 1508 | */ |
---|
[1030] | 1509 | public function changePostsCategory($old_cat_id,$new_cat_id) |
---|
[1015] | 1510 | { |
---|
| 1511 | if (!$this->core->auth->check('contentadmin,categories',$this->id)) { |
---|
| 1512 | throw new Exception(__('You are not allowed to change entries category')); |
---|
| 1513 | } |
---|
[2566] | 1514 | |
---|
[1015] | 1515 | $old_cat_id = (integer) $old_cat_id; |
---|
| 1516 | $new_cat_id = (integer) $new_cat_id; |
---|
[2566] | 1517 | |
---|
[1015] | 1518 | $cur = $this->con->openCursor($this->prefix.'post'); |
---|
[2566] | 1519 | |
---|
[1015] | 1520 | $cur->cat_id = ($new_cat_id ? $new_cat_id : null); |
---|
| 1521 | $cur->post_upddt = date('Y-m-d H:i:s'); |
---|
[2566] | 1522 | |
---|
[1015] | 1523 | $cur->update( |
---|
| 1524 | 'WHERE cat_id = '.$old_cat_id.' '. |
---|
| 1525 | "AND blog_id = '".$this->con->escape($this->id)."' " |
---|
| 1526 | ); |
---|
| 1527 | $this->triggerBlog(); |
---|
| 1528 | } |
---|
[2566] | 1529 | |
---|
[1015] | 1530 | /** |
---|
[0] | 1531 | Deletes a post. |
---|
[2566] | 1532 | |
---|
[0] | 1533 | @param id <b>integer</b> Post ID |
---|
| 1534 | */ |
---|
| 1535 | public function delPost($id) |
---|
| 1536 | { |
---|
[1030] | 1537 | $this->delPosts($id); |
---|
| 1538 | } |
---|
[2566] | 1539 | |
---|
[1030] | 1540 | /** |
---|
| 1541 | Deletes multiple posts. |
---|
[2566] | 1542 | |
---|
[1030] | 1543 | @param ids <b>mixed</b> Post(s) ID(s) |
---|
| 1544 | */ |
---|
| 1545 | public function delPosts($ids) |
---|
| 1546 | { |
---|
[0] | 1547 | if (!$this->core->auth->check('delete,contentadmin',$this->id)) { |
---|
| 1548 | throw new Exception(__('You are not allowed to delete entries')); |
---|
| 1549 | } |
---|
[2566] | 1550 | |
---|
[1030] | 1551 | $posts_ids = dcUtils::cleanIds($ids); |
---|
[2566] | 1552 | |
---|
[1030] | 1553 | if (empty($posts_ids)) { |
---|
[0] | 1554 | throw new Exception(__('No such entry ID')); |
---|
| 1555 | } |
---|
[2566] | 1556 | |
---|
[1030] | 1557 | $strReq = 'DELETE FROM '.$this->prefix.'post '. |
---|
| 1558 | "WHERE blog_id = '".$this->con->escape($this->id)."' ". |
---|
| 1559 | "AND post_id ".$this->con->in($posts_ids); |
---|
[2566] | 1560 | |
---|
[0] | 1561 | #If user can only delete, we need to check the post's owner |
---|
| 1562 | if (!$this->core->auth->check('contentadmin',$this->id)) |
---|
| 1563 | { |
---|
[1030] | 1564 | $strReq .= "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; |
---|
[0] | 1565 | } |
---|
[2566] | 1566 | |
---|
[0] | 1567 | $this->con->execute($strReq); |
---|
| 1568 | $this->triggerBlog(); |
---|
| 1569 | } |
---|
[2566] | 1570 | |
---|
[0] | 1571 | /** |
---|
| 1572 | Publishes all entries flaged as "scheduled". |
---|
| 1573 | */ |
---|
| 1574 | public function publishScheduledEntries() |
---|
| 1575 | { |
---|
| 1576 | $strReq = 'SELECT post_id, post_dt, post_tz '. |
---|
| 1577 | 'FROM '.$this->prefix.'post '. |
---|
| 1578 | 'WHERE post_status = -1 '. |
---|
| 1579 | "AND blog_id = '".$this->con->escape($this->id)."' "; |
---|
[2566] | 1580 | |
---|
[0] | 1581 | $rs = $this->con->select($strReq); |
---|
[2566] | 1582 | |
---|
[0] | 1583 | $now = dt::toUTC(time()); |
---|
[833] | 1584 | $to_change = new ArrayObject(); |
---|
| 1585 | |
---|
[0] | 1586 | if ($rs->isEmpty()) { |
---|
| 1587 | return; |
---|
| 1588 | } |
---|
[2566] | 1589 | |
---|
[0] | 1590 | while ($rs->fetch()) |
---|
| 1591 | { |
---|
| 1592 | # Now timestamp with post timezone |
---|
| 1593 | $now_tz = $now + dt::getTimeOffset($rs->post_tz,$now); |
---|
[2566] | 1594 | |
---|
[0] | 1595 | # Post timestamp |
---|
| 1596 | $post_ts = strtotime($rs->post_dt); |
---|
[2566] | 1597 | |
---|
[0] | 1598 | # If now_tz >= post_ts, we publish the entry |
---|
| 1599 | if ($now_tz >= $post_ts) { |
---|
| 1600 | $to_change[] = (integer) $rs->post_id; |
---|
| 1601 | } |
---|
| 1602 | } |
---|
[833] | 1603 | if (count($to_change)) |
---|
[0] | 1604 | { |
---|
[811] | 1605 | # --BEHAVIOR-- coreBeforeScheduledEntriesPublish |
---|
| 1606 | $this->core->callBehavior('coreBeforeScheduledEntriesPublish',$this,$to_change); |
---|
| 1607 | |
---|
[0] | 1608 | $strReq = |
---|
| 1609 | 'UPDATE '.$this->prefix.'post SET '. |
---|
| 1610 | 'post_status = 1 '. |
---|
| 1611 | "WHERE blog_id = '".$this->con->escape($this->id)."' ". |
---|
[833] | 1612 | 'AND post_id '.$this->con->in((array)$to_change).' '; |
---|
[0] | 1613 | $this->con->execute($strReq); |
---|
| 1614 | $this->triggerBlog(); |
---|
[811] | 1615 | |
---|
| 1616 | # --BEHAVIOR-- coreAfterScheduledEntriesPublish |
---|
| 1617 | $this->core->callBehavior('coreAfterScheduledEntriesPublish',$this,$to_change); |
---|
[0] | 1618 | } |
---|
[2566] | 1619 | |
---|
[0] | 1620 | } |
---|
[2566] | 1621 | |
---|
[0] | 1622 | /** |
---|
| 1623 | Retrieves all users having posts on current blog. |
---|
[2566] | 1624 | |
---|
[0] | 1625 | @param post_type <b>string</b> post_type filter (post) |
---|
| 1626 | @return record |
---|
| 1627 | */ |
---|
| 1628 | public function getPostsUsers($post_type='post') |
---|
| 1629 | { |
---|
| 1630 | $strReq = 'SELECT P.user_id, user_name, user_firstname, '. |
---|
| 1631 | 'user_displayname, user_email '. |
---|
| 1632 | 'FROM '.$this->prefix.'post P, '.$this->prefix.'user U '. |
---|
| 1633 | 'WHERE P.user_id = U.user_id '. |
---|
| 1634 | "AND blog_id = '".$this->con->escape($this->id)."' "; |
---|
[2566] | 1635 | |
---|
[0] | 1636 | if ($post_type) { |
---|
| 1637 | $strReq .= "AND post_type = '".$this->con->escape($post_type)."' "; |
---|
| 1638 | } |
---|
[2566] | 1639 | |
---|
[0] | 1640 | $strReq .= 'GROUP BY P.user_id, user_name, user_firstname, user_displayname, user_email '; |
---|
[2566] | 1641 | |
---|
[0] | 1642 | return $this->con->select($strReq); |
---|
| 1643 | } |
---|
[2566] | 1644 | |
---|
[0] | 1645 | private function getPostsCategoryFilter($arr,$field='cat_id') |
---|
| 1646 | { |
---|
| 1647 | $field = $field == 'cat_id' ? 'cat_id' : 'cat_url'; |
---|
[2566] | 1648 | |
---|
[0] | 1649 | $sub = array(); |
---|
| 1650 | $not = array(); |
---|
| 1651 | $queries = array(); |
---|
[2566] | 1652 | |
---|
[0] | 1653 | foreach ($arr as $v) |
---|
| 1654 | { |
---|
| 1655 | $v = trim($v); |
---|
| 1656 | $args = preg_split('/\s*[?]\s*/',$v,-1,PREG_SPLIT_NO_EMPTY); |
---|
| 1657 | $id = array_shift($args); |
---|
| 1658 | $args = array_flip($args); |
---|
[2566] | 1659 | |
---|
[0] | 1660 | if (isset($args['not'])) { $not[$id] = 1; } |
---|
| 1661 | if (isset($args['sub'])) { $sub[$id] = 1; } |
---|
| 1662 | if ($field == 'cat_id') { |
---|
| 1663 | if (preg_match('/^null$/i',$id)) { |
---|
| 1664 | $queries[$id] = 'P.cat_id IS NULL'; |
---|
| 1665 | } |
---|
| 1666 | else { |
---|
| 1667 | $queries[$id] = 'P.cat_id = '.(integer) $id; |
---|
| 1668 | } |
---|
| 1669 | } else { |
---|
| 1670 | $queries[$id] = "C.cat_url = '".$this->con->escape($id)."' "; |
---|
| 1671 | } |
---|
| 1672 | } |
---|
[2566] | 1673 | |
---|
[0] | 1674 | if (!empty($sub)) { |
---|
| 1675 | $rs = $this->con->select( |
---|
| 1676 | 'SELECT cat_id, cat_url, cat_lft, cat_rgt FROM '.$this->prefix.'category '. |
---|
| 1677 | "WHERE blog_id = '".$this->con->escape($this->id)."' ". |
---|
| 1678 | 'AND '.$field.' '.$this->con->in(array_keys($sub)) |
---|
| 1679 | ); |
---|
[2566] | 1680 | |
---|
[0] | 1681 | while ($rs->fetch()) { |
---|
| 1682 | $queries[$rs->f($field)] = '(C.cat_lft BETWEEN '.$rs->cat_lft.' AND '.$rs->cat_rgt.')'; |
---|
| 1683 | } |
---|
| 1684 | } |
---|
[2566] | 1685 | |
---|
[0] | 1686 | # Create queries |
---|
| 1687 | $sql = array( |
---|
| 1688 | 0 => array(), # wanted categories |
---|
| 1689 | 1 => array() # excluded categories |
---|
| 1690 | ); |
---|
[2566] | 1691 | |
---|
[0] | 1692 | foreach ($queries as $id => $q) { |
---|
| 1693 | $sql[(integer) isset($not[$id])][] = $q; |
---|
| 1694 | } |
---|
[2566] | 1695 | |
---|
[0] | 1696 | $sql[0] = implode(' OR ',$sql[0]); |
---|
| 1697 | $sql[1] = implode(' OR ',$sql[1]); |
---|
[2566] | 1698 | |
---|
[0] | 1699 | if ($sql[0]) { |
---|
| 1700 | $sql[0] = '('.$sql[0].')'; |
---|
| 1701 | } else { |
---|
| 1702 | unset($sql[0]); |
---|
| 1703 | } |
---|
[2566] | 1704 | |
---|
[0] | 1705 | if ($sql[1]) { |
---|
| 1706 | $sql[1] = '(P.cat_id IS NULL OR NOT('.$sql[1].'))'; |
---|
| 1707 | } else { |
---|
| 1708 | unset($sql[1]); |
---|
| 1709 | } |
---|
[2566] | 1710 | |
---|
[0] | 1711 | return implode(' AND ',$sql); |
---|
| 1712 | } |
---|
[2566] | 1713 | |
---|
[0] | 1714 | private function getPostCursor($cur,$post_id=null) |
---|
| 1715 | { |
---|
| 1716 | if ($cur->post_title == '') { |
---|
| 1717 | throw new Exception(__('No entry title')); |
---|
| 1718 | } |
---|
[2566] | 1719 | |
---|
[0] | 1720 | if ($cur->post_content == '') { |
---|
| 1721 | throw new Exception(__('No entry content')); |
---|
| 1722 | } |
---|
[2566] | 1723 | |
---|
[0] | 1724 | if ($cur->post_password === '') { |
---|
| 1725 | $cur->post_password = null; |
---|
| 1726 | } |
---|
[2566] | 1727 | |
---|
[0] | 1728 | if ($cur->post_dt == '') { |
---|
| 1729 | $offset = dt::getTimeOffset($this->core->auth->getInfo('user_tz')); |
---|
| 1730 | $now = time() + $offset; |
---|
| 1731 | $cur->post_dt = date('Y-m-d H:i:00',$now); |
---|
| 1732 | } |
---|
[2566] | 1733 | |
---|
[0] | 1734 | $post_id = is_int($post_id) ? $post_id : $cur->post_id; |
---|
[2566] | 1735 | |
---|
[0] | 1736 | if ($cur->post_content_xhtml == '') { |
---|
| 1737 | throw new Exception(__('No entry content')); |
---|
| 1738 | } |
---|
[2566] | 1739 | |
---|
[0] | 1740 | # Words list |
---|
| 1741 | if ($cur->post_title !== null && $cur->post_excerpt_xhtml !== null |
---|
| 1742 | && $cur->post_content_xhtml !== null) |
---|
| 1743 | { |
---|
| 1744 | $words = |
---|
| 1745 | $cur->post_title.' '. |
---|
| 1746 | $cur->post_excerpt_xhtml.' '. |
---|
| 1747 | $cur->post_content_xhtml; |
---|
[2566] | 1748 | |
---|
[0] | 1749 | $cur->post_words = implode(' ',text::splitWords($words)); |
---|
| 1750 | } |
---|
| 1751 | } |
---|
[2566] | 1752 | |
---|
[0] | 1753 | private function getPostContent($cur,$post_id) |
---|
| 1754 | { |
---|
| 1755 | $post_excerpt = $cur->post_excerpt; |
---|
| 1756 | $post_excerpt_xhtml = $cur->post_excerpt_xhtml; |
---|
| 1757 | $post_content = $cur->post_content; |
---|
| 1758 | $post_content_xhtml = $cur->post_content_xhtml; |
---|
[2566] | 1759 | |
---|
[0] | 1760 | $this->setPostContent( |
---|
| 1761 | $post_id,$cur->post_format,$cur->post_lang, |
---|
| 1762 | $post_excerpt,$post_excerpt_xhtml, |
---|
| 1763 | $post_content,$post_content_xhtml |
---|
| 1764 | ); |
---|
[2566] | 1765 | |
---|
[0] | 1766 | $cur->post_excerpt = $post_excerpt; |
---|
| 1767 | $cur->post_excerpt_xhtml = $post_excerpt_xhtml; |
---|
| 1768 | $cur->post_content = $post_content; |
---|
| 1769 | $cur->post_content_xhtml = $post_content_xhtml; |
---|
| 1770 | } |
---|
[2566] | 1771 | |
---|
[0] | 1772 | /** |
---|
| 1773 | Creates post HTML content, taking format and lang into account. |
---|
[2566] | 1774 | |
---|
[0] | 1775 | @param post_id <b>integer</b> Post ID |
---|
| 1776 | @param format <b>string</b> Post format |
---|
| 1777 | @param lang <b>string</b> Post lang |
---|
| 1778 | @param excerpt <b>string</b> Post excerpt |
---|
| 1779 | @param[out] excerpt_xhtml <b>string</b> Post excerpt HTML |
---|
| 1780 | @param content <b>string</b> Post content |
---|
| 1781 | @param[out] content_xhtml <b>string</b> Post content HTML |
---|
| 1782 | */ |
---|
| 1783 | public function setPostContent($post_id,$format,$lang,&$excerpt,&$excerpt_xhtml,&$content,&$content_xhtml) |
---|
| 1784 | { |
---|
| 1785 | if ($format == 'wiki') |
---|
| 1786 | { |
---|
| 1787 | $this->core->initWikiPost(); |
---|
| 1788 | $this->core->wiki2xhtml->setOpt('note_prefix','pnote-'.$post_id); |
---|
[970] | 1789 | switch ($this->settings->system->note_title_tag) { |
---|
| 1790 | case 1: |
---|
| 1791 | $tag = 'h3'; |
---|
| 1792 | break; |
---|
| 1793 | case 2: |
---|
| 1794 | $tag = 'p'; |
---|
| 1795 | break; |
---|
| 1796 | default: |
---|
| 1797 | $tag = 'h4'; |
---|
| 1798 | break; |
---|
| 1799 | } |
---|
| 1800 | $this->core->wiki2xhtml->setOpt('note_str','<div class="footnotes"><'.$tag.' class="footnotes-title">'. |
---|
| 1801 | __('Notes').'</'.$tag.'>%s</div>'); |
---|
| 1802 | $this->core->wiki2xhtml->setOpt('note_str_single','<div class="footnotes"><'.$tag.' class="footnotes-title">'. |
---|
| 1803 | __('Note').'</'.$tag.'>%s</div>'); |
---|
[0] | 1804 | if (strpos($lang,'fr') === 0) { |
---|
| 1805 | $this->core->wiki2xhtml->setOpt('active_fr_syntax',1); |
---|
| 1806 | } |
---|
| 1807 | } |
---|
[2566] | 1808 | |
---|
[0] | 1809 | if ($excerpt) { |
---|
| 1810 | $excerpt_xhtml = $this->core->callFormater($format,$excerpt); |
---|
| 1811 | $excerpt_xhtml = $this->core->HTMLfilter($excerpt_xhtml); |
---|
| 1812 | } else { |
---|
| 1813 | $excerpt_xhtml = ''; |
---|
| 1814 | } |
---|
[2566] | 1815 | |
---|
[0] | 1816 | if ($content) { |
---|
| 1817 | $content_xhtml = $this->core->callFormater($format,$content); |
---|
| 1818 | $content_xhtml = $this->core->HTMLfilter($content_xhtml); |
---|
| 1819 | } else { |
---|
| 1820 | $content_xhtml = ''; |
---|
| 1821 | } |
---|
[2566] | 1822 | |
---|
[0] | 1823 | # --BEHAVIOR-- coreAfterPostContentFormat |
---|
| 1824 | $this->core->callBehavior('coreAfterPostContentFormat',array( |
---|
| 1825 | 'excerpt' => &$excerpt, |
---|
| 1826 | 'content' => &$content, |
---|
| 1827 | 'excerpt_xhtml' => &$excerpt_xhtml, |
---|
| 1828 | 'content_xhtml' => &$content_xhtml |
---|
| 1829 | )); |
---|
| 1830 | } |
---|
[2566] | 1831 | |
---|
[0] | 1832 | /** |
---|
| 1833 | Returns URL for a post according to blog setting <var>post_url_format</var>. |
---|
| 1834 | It will try to guess URL and append some figures if needed. |
---|
[2566] | 1835 | |
---|
[0] | 1836 | @param url <b>string</b> Origin URL, could be empty |
---|
| 1837 | @param post_dt <b>string</b> Post date (in YYYY-MM-DD HH:mm:ss) |
---|
| 1838 | @param post_title <b>string</b> Post title |
---|
| 1839 | @param post_id <b>integer</b> Post ID |
---|
| 1840 | @return <b>string</b> result URL |
---|
| 1841 | */ |
---|
| 1842 | public function getPostURL($url,$post_dt,$post_title,$post_id) |
---|
| 1843 | { |
---|
| 1844 | $url = trim($url); |
---|
[2566] | 1845 | |
---|
[0] | 1846 | $url_patterns = array( |
---|
| 1847 | '{y}' => date('Y',strtotime($post_dt)), |
---|
| 1848 | '{m}' => date('m',strtotime($post_dt)), |
---|
| 1849 | '{d}' => date('d',strtotime($post_dt)), |
---|
| 1850 | '{t}' => text::tidyURL($post_title), |
---|
| 1851 | '{id}' => (integer) $post_id |
---|
| 1852 | ); |
---|
[2566] | 1853 | |
---|
[0] | 1854 | # If URL is empty, we create a new one |
---|
| 1855 | if ($url == '') |
---|
| 1856 | { |
---|
| 1857 | # Transform with format |
---|
| 1858 | $url = str_replace( |
---|
| 1859 | array_keys($url_patterns), |
---|
| 1860 | array_values($url_patterns), |
---|
| 1861 | $this->settings->system->post_url_format |
---|
| 1862 | ); |
---|
| 1863 | } |
---|
| 1864 | else |
---|
| 1865 | { |
---|
| 1866 | $url = text::tidyURL($url); |
---|
| 1867 | } |
---|
[2566] | 1868 | |
---|
[0] | 1869 | # Let's check if URL is taken... |
---|
| 1870 | $strReq = 'SELECT post_url FROM '.$this->prefix.'post '. |
---|
| 1871 | "WHERE post_url = '".$this->con->escape($url)."' ". |
---|
| 1872 | 'AND post_id <> '.(integer) $post_id. ' '. |
---|
| 1873 | "AND blog_id = '".$this->con->escape($this->id)."' ". |
---|
| 1874 | 'ORDER BY post_url DESC'; |
---|
[2566] | 1875 | |
---|
[0] | 1876 | $rs = $this->con->select($strReq); |
---|
[2566] | 1877 | |
---|
[0] | 1878 | if (!$rs->isEmpty()) |
---|
| 1879 | { |
---|
[1743] | 1880 | if ($this->con->driver() == 'mysql' || $this->con->driver() == 'mysqli') { |
---|
[0] | 1881 | $clause = "REGEXP '^".$this->con->escape($url)."[0-9]+$'"; |
---|
| 1882 | } elseif ($this->con->driver() == 'pgsql') { |
---|
| 1883 | $clause = "~ '^".$this->con->escape($url)."[0-9]+$'"; |
---|
| 1884 | } else { |
---|
| 1885 | $clause = "LIKE '".$this->con->escape($url)."%'"; |
---|
| 1886 | } |
---|
| 1887 | $strReq = 'SELECT post_url FROM '.$this->prefix.'post '. |
---|
| 1888 | "WHERE post_url ".$clause.' '. |
---|
| 1889 | 'AND post_id <> '.(integer) $post_id.' '. |
---|
| 1890 | "AND blog_id = '".$this->con->escape($this->id)."' ". |
---|
| 1891 | 'ORDER BY post_url DESC '; |
---|
[2566] | 1892 | |
---|
[0] | 1893 | $rs = $this->con->select($strReq); |
---|
| 1894 | $a = array(); |
---|
| 1895 | while ($rs->fetch()) { |
---|
| 1896 | $a[] = $rs->post_url; |
---|
| 1897 | } |
---|
[2566] | 1898 | |
---|
[0] | 1899 | natsort($a); |
---|
| 1900 | $t_url = end($a); |
---|
[2566] | 1901 | |
---|
[0] | 1902 | if (preg_match('/(.*?)([0-9]+)$/',$t_url,$m)) { |
---|
| 1903 | $i = (integer) $m[2]; |
---|
| 1904 | $url = $m[1]; |
---|
| 1905 | } else { |
---|
| 1906 | $i = 1; |
---|
| 1907 | } |
---|
[2566] | 1908 | |
---|
[0] | 1909 | return $url.($i+1); |
---|
| 1910 | } |
---|
[2566] | 1911 | |
---|
[0] | 1912 | # URL is empty? |
---|
| 1913 | if ($url == '') { |
---|
| 1914 | throw new Exception(__('Empty entry URL')); |
---|
| 1915 | } |
---|
[2566] | 1916 | |
---|
[0] | 1917 | return $url; |
---|
| 1918 | } |
---|
| 1919 | //@} |
---|
[2566] | 1920 | |
---|
[0] | 1921 | /// @name Comments management methods |
---|
| 1922 | //@{ |
---|
| 1923 | /** |
---|
| 1924 | Retrieves comments. <b>$params</b> is an array taking the following |
---|
| 1925 | optionnal parameters: |
---|
[2566] | 1926 | |
---|
[0] | 1927 | - no_content: Don't retrieve comment content |
---|
[2566] | 1928 | - post_type: Get only entries with given type (default no type, array for many types) |
---|
[0] | 1929 | - post_id: (integer) Get comments belonging to given post_id |
---|
| 1930 | - cat_id: (integer or array) Get comments belonging to entries of given category ID |
---|
[2521] | 1931 | - comment_id: (integer or array) Get comment with given ID (or IDs) |
---|
[1689] | 1932 | - comment_site: (string) Get comments with given comment_site |
---|
[0] | 1933 | - comment_status: (integer) Get comments with given comment_status |
---|
| 1934 | - comment_trackback: (integer) Get only comments (0) or trackbacks (1) |
---|
| 1935 | - comment_ip: (string) Get comments with given IP address |
---|
| 1936 | - post_url: Get entry with given post_url field |
---|
| 1937 | - user_id: (integer) Get entries belonging to given user ID |
---|
| 1938 | - q_author: Search comments by author |
---|
| 1939 | - sql: Append SQL string at the end of the query |
---|
| 1940 | - from: Append SQL string after "FROM" statement in query |
---|
| 1941 | - order: Order of results (default "ORDER BY comment_dt DES") |
---|
| 1942 | - limit: Limit parameter |
---|
[774] | 1943 | - sql_only : return the sql request instead of results. Only ids are selected |
---|
[2566] | 1944 | |
---|
[0] | 1945 | @param params <b>array</b> Parameters |
---|
| 1946 | @param count_only <b>boolean</b> Only counts results |
---|
| 1947 | @return <b>record</b> A record with some more capabilities |
---|
| 1948 | */ |
---|
| 1949 | public function getComments($params=array(),$count_only=false) |
---|
| 1950 | { |
---|
| 1951 | if ($count_only) |
---|
| 1952 | { |
---|
| 1953 | $strReq = 'SELECT count(comment_id) '; |
---|
| 1954 | } |
---|
[2566] | 1955 | elseif (!empty($params['sql_only'])) |
---|
[774] | 1956 | { |
---|
| 1957 | $strReq = 'SELECT P.post_id '; |
---|
| 1958 | } |
---|
[0] | 1959 | else |
---|
| 1960 | { |
---|
| 1961 | if (!empty($params['no_content'])) { |
---|
| 1962 | $content_req = ''; |
---|
| 1963 | } else { |
---|
| 1964 | $content_req = 'comment_content, '; |
---|
| 1965 | } |
---|
[2566] | 1966 | |
---|
[0] | 1967 | if (!empty($params['columns']) && is_array($params['columns'])) { |
---|
| 1968 | $content_req .= implode(', ',$params['columns']).', '; |
---|
| 1969 | } |
---|
[2566] | 1970 | |
---|
[0] | 1971 | $strReq = |
---|
| 1972 | 'SELECT C.comment_id, comment_dt, comment_tz, comment_upddt, '. |
---|
| 1973 | 'comment_author, comment_email, comment_site, '. |
---|
| 1974 | $content_req.' comment_trackback, comment_status, '. |
---|
| 1975 | 'comment_spam_status, comment_spam_filter, comment_ip, '. |
---|
| 1976 | 'P.post_title, P.post_url, P.post_id, P.post_password, P.post_type, '. |
---|
| 1977 | 'P.post_dt, P.user_id, U.user_email, U.user_url '; |
---|
| 1978 | } |
---|
[2566] | 1979 | |
---|
[0] | 1980 | $strReq .= |
---|
| 1981 | 'FROM '.$this->prefix.'comment C '. |
---|
| 1982 | 'INNER JOIN '.$this->prefix.'post P ON C.post_id = P.post_id '. |
---|
| 1983 | 'INNER JOIN '.$this->prefix.'user U ON P.user_id = U.user_id '; |
---|
[2566] | 1984 | |
---|
[0] | 1985 | if (!empty($params['from'])) { |
---|
| 1986 | $strReq .= $params['from'].' '; |
---|
| 1987 | } |
---|
[2566] | 1988 | |
---|
[0] | 1989 | $strReq .= |
---|
| 1990 | "WHERE P.blog_id = '".$this->con->escape($this->id)."' "; |
---|
[2566] | 1991 | |
---|
[0] | 1992 | if (!$this->core->auth->check('contentadmin',$this->id)) { |
---|
| 1993 | $strReq .= 'AND ((comment_status = 1 AND P.post_status = 1 '; |
---|
[2566] | 1994 | |
---|
[0] | 1995 | if ($this->without_password) { |
---|
| 1996 | $strReq .= 'AND post_password IS NULL '; |
---|
| 1997 | } |
---|
| 1998 | $strReq .= ') '; |
---|
[2566] | 1999 | |
---|
[0] | 2000 | if ($this->core->auth->userID()) { |
---|
| 2001 | $strReq .= "OR P.user_id = '".$this->con->escape($this->core->auth->userID())."')"; |
---|
| 2002 | } else { |
---|
| 2003 | $strReq .= ') '; |
---|
| 2004 | } |
---|
| 2005 | } |
---|
[2566] | 2006 | |
---|
[0] | 2007 | if (!empty($params['post_type'])) |
---|
| 2008 | { |
---|
[340] | 2009 | $strReq .= 'AND post_type '.$this->con->in($params['post_type']); |
---|
[0] | 2010 | } |
---|
[2566] | 2011 | |
---|
[836] | 2012 | if (isset($params['post_id']) && $params['post_id'] !== '') { |
---|
[0] | 2013 | $strReq .= 'AND P.post_id = '.(integer) $params['post_id'].' '; |
---|
| 2014 | } |
---|
[2566] | 2015 | |
---|
[836] | 2016 | if (isset($params['cat_id']) && $params['cat_id'] !== '') { |
---|
[0] | 2017 | $strReq .= 'AND P.cat_id = '.(integer) $params['cat_id'].' '; |
---|
| 2018 | } |
---|
[2566] | 2019 | |
---|
[836] | 2020 | if (isset($params['comment_id']) && $params['comment_id'] !== '') { |
---|
[2521] | 2021 | if (is_array($params['comment_id'])) { |
---|
| 2022 | array_walk($params['comment_id'],create_function('&$v,$k','if($v!==null){$v=(integer)$v;}')); |
---|
| 2023 | } else { |
---|
| 2024 | $params['comment_id'] = array((integer) $params['comment_id']); |
---|
| 2025 | } |
---|
| 2026 | $strReq .= 'AND comment_id '.$this->con->in($params['comment_id']); |
---|
[0] | 2027 | } |
---|
[2566] | 2028 | |
---|
[1689] | 2029 | if (isset($params['comment_site'])) { |
---|
| 2030 | $comment_site = $this->con->escape(str_replace('*','%',$params['comment_site'])); |
---|
| 2031 | $strReq .= "AND comment_site LIKE '".$comment_site."' "; |
---|
| 2032 | } |
---|
[2566] | 2033 | |
---|
[0] | 2034 | if (isset($params['comment_status'])) { |
---|
| 2035 | $strReq .= 'AND comment_status = '.(integer) $params['comment_status'].' '; |
---|
| 2036 | } |
---|
[2566] | 2037 | |
---|
[0] | 2038 | if (!empty($params['comment_status_not'])) |
---|
| 2039 | { |
---|
| 2040 | $strReq .= 'AND comment_status <> '.(integer) $params['comment_status_not'].' '; |
---|
| 2041 | } |
---|
[2566] | 2042 | |
---|
[0] | 2043 | if (isset($params['comment_trackback'])) { |
---|
| 2044 | $strReq .= 'AND comment_trackback = '.(integer) (boolean) $params['comment_trackback'].' '; |
---|
| 2045 | } |
---|
[2566] | 2046 | |
---|
[0] | 2047 | if (isset($params['comment_ip'])) { |
---|
[1018] | 2048 | $comment_ip = $this->con->escape(str_replace('*','%',$params['comment_ip'])); |
---|
| 2049 | $strReq .= "AND comment_ip LIKE '".$comment_ip."' "; |
---|
[0] | 2050 | } |
---|
[2566] | 2051 | |
---|
[0] | 2052 | if (isset($params['q_author'])) { |
---|
| 2053 | $q_author = $this->con->escape(str_replace('*','%',strtolower($params['q_author']))); |
---|
| 2054 | $strReq .= "AND LOWER(comment_author) LIKE '".$q_author."' "; |
---|
| 2055 | } |
---|
[2566] | 2056 | |
---|
[0] | 2057 | if (!empty($params['search'])) |
---|
| 2058 | { |
---|
| 2059 | $words = text::splitWords($params['search']); |
---|
[2566] | 2060 | |
---|
[0] | 2061 | if (!empty($words)) |
---|
| 2062 | { |
---|
| 2063 | # --BEHAVIOR coreCommentSearch |
---|
| 2064 | if ($this->core->hasBehavior('coreCommentSearch')) { |
---|
| 2065 | $this->core->callBehavior('coreCommentSearch',$this->core,array(&$words,&$strReq,&$params)); |
---|
| 2066 | } |
---|
[2566] | 2067 | |
---|
[0] | 2068 | if ($words) |
---|
| 2069 | { |
---|
| 2070 | foreach ($words as $i => $w) { |
---|
| 2071 | $words[$i] = "comment_words LIKE '%".$this->con->escape($w)."%'"; |
---|
| 2072 | } |
---|
| 2073 | $strReq .= 'AND '.implode(' AND ',$words).' '; |
---|
| 2074 | } |
---|
| 2075 | } |
---|
| 2076 | } |
---|
[2566] | 2077 | |
---|
[0] | 2078 | if (!empty($params['sql'])) { |
---|
| 2079 | $strReq .= $params['sql'].' '; |
---|
| 2080 | } |
---|
[2566] | 2081 | |
---|
[0] | 2082 | if (!$count_only) |
---|
| 2083 | { |
---|
| 2084 | if (!empty($params['order'])) { |
---|
| 2085 | $strReq .= 'ORDER BY '.$this->con->escape($params['order']).' '; |
---|
| 2086 | } else { |
---|
| 2087 | $strReq .= 'ORDER BY comment_dt DESC '; |
---|
| 2088 | } |
---|
| 2089 | } |
---|
[2566] | 2090 | |
---|
[0] | 2091 | if (!$count_only && !empty($params['limit'])) { |
---|
| 2092 | $strReq .= $this->con->limit($params['limit']); |
---|
| 2093 | } |
---|
[774] | 2094 | |
---|
| 2095 | if (!empty($params['sql_only'])) { |
---|
| 2096 | return $strReq; |
---|
| 2097 | } |
---|
[2566] | 2098 | |
---|
[0] | 2099 | $rs = $this->con->select($strReq); |
---|
| 2100 | $rs->core = $this->core; |
---|
| 2101 | $rs->extend('rsExtComment'); |
---|
[2566] | 2102 | |
---|
[0] | 2103 | # --BEHAVIOR-- coreBlogGetComments |
---|
| 2104 | $this->core->callBehavior('coreBlogGetComments',$rs); |
---|
[2566] | 2105 | |
---|
[0] | 2106 | return $rs; |
---|
| 2107 | } |
---|
[2566] | 2108 | |
---|
[0] | 2109 | /** |
---|
| 2110 | Creates a new comment. Takes a cursor as input and returns the new comment |
---|
| 2111 | ID. |
---|
[2566] | 2112 | |
---|
[0] | 2113 | @param cur <b>cursor</b> Comment cursor |
---|
| 2114 | @return <b>integer</b> New comment ID |
---|
| 2115 | */ |
---|
| 2116 | public function addComment($cur) |
---|
| 2117 | { |
---|
| 2118 | $this->con->writeLock($this->prefix.'comment'); |
---|
| 2119 | try |
---|
| 2120 | { |
---|
| 2121 | # Get ID |
---|
| 2122 | $rs = $this->con->select( |
---|
| 2123 | 'SELECT MAX(comment_id) '. |
---|
[2566] | 2124 | 'FROM '.$this->prefix.'comment ' |
---|
[0] | 2125 | ); |
---|
[2566] | 2126 | |
---|
[0] | 2127 | $cur->comment_id = (integer) $rs->f(0) + 1; |
---|
| 2128 | $cur->comment_upddt = date('Y-m-d H:i:s'); |
---|
[2566] | 2129 | |
---|
[0] | 2130 | $offset = dt::getTimeOffset($this->settings->system->blog_timezone); |
---|
| 2131 | $cur->comment_dt = date('Y-m-d H:i:s',time() + $offset); |
---|
| 2132 | $cur->comment_tz = $this->settings->system->blog_timezone; |
---|
[2566] | 2133 | |
---|
[0] | 2134 | $this->getCommentCursor($cur); |
---|
[2566] | 2135 | |
---|
[0] | 2136 | if ($cur->comment_ip === null) { |
---|
| 2137 | $cur->comment_ip = http::realIP(); |
---|
| 2138 | } |
---|
[2566] | 2139 | |
---|
[0] | 2140 | # --BEHAVIOR-- coreBeforeCommentCreate |
---|
| 2141 | $this->core->callBehavior('coreBeforeCommentCreate',$this,$cur); |
---|
[2566] | 2142 | |
---|
[0] | 2143 | $cur->insert(); |
---|
| 2144 | $this->con->unlock(); |
---|
| 2145 | } |
---|
| 2146 | catch (Exception $e) |
---|
| 2147 | { |
---|
| 2148 | $this->con->unlock(); |
---|
| 2149 | throw $e; |
---|
| 2150 | } |
---|
[2566] | 2151 | |
---|
[0] | 2152 | # --BEHAVIOR-- coreAfterCommentCreate |
---|
| 2153 | $this->core->callBehavior('coreAfterCommentCreate',$this,$cur); |
---|
[2566] | 2154 | |
---|
[0] | 2155 | $this->triggerComment($cur->comment_id); |
---|
| 2156 | if ($cur->comment_status != -2) { |
---|
| 2157 | $this->triggerBlog(); |
---|
[2566] | 2158 | } |
---|
[0] | 2159 | return $cur->comment_id; |
---|
| 2160 | } |
---|
[2566] | 2161 | |
---|
[0] | 2162 | /** |
---|
| 2163 | Updates an existing comment. |
---|
[2566] | 2164 | |
---|
[0] | 2165 | @param id <b>integer</b> Comment ID |
---|
| 2166 | @param cur <b>cursor</b> Comment cursor |
---|
| 2167 | */ |
---|
| 2168 | public function updComment($id,$cur) |
---|
| 2169 | { |
---|
| 2170 | if (!$this->core->auth->check('usage,contentadmin',$this->id)) { |
---|
| 2171 | throw new Exception(__('You are not allowed to update comments')); |
---|
| 2172 | } |
---|
[2566] | 2173 | |
---|
[0] | 2174 | $id = (integer) $id; |
---|
[2566] | 2175 | |
---|
[0] | 2176 | if (empty($id)) { |
---|
| 2177 | throw new Exception(__('No such comment ID')); |
---|
| 2178 | } |
---|
[2566] | 2179 | |
---|
[0] | 2180 | $rs = $this->getComments(array('comment_id' => $id)); |
---|
[2566] | 2181 | |
---|
[0] | 2182 | if ($rs->isEmpty()) { |
---|
| 2183 | throw new Exception(__('No such comment ID')); |
---|
| 2184 | } |
---|
[2566] | 2185 | |
---|
[0] | 2186 | #If user is only usage, we need to check the post's owner |
---|
| 2187 | if (!$this->core->auth->check('contentadmin',$this->id)) |
---|
| 2188 | { |
---|
| 2189 | if ($rs->user_id != $this->core->auth->userID()) { |
---|
| 2190 | throw new Exception(__('You are not allowed to update this comment')); |
---|
| 2191 | } |
---|
| 2192 | } |
---|
[2566] | 2193 | |
---|
[0] | 2194 | $this->getCommentCursor($cur); |
---|
[2566] | 2195 | |
---|
[0] | 2196 | $cur->comment_upddt = date('Y-m-d H:i:s'); |
---|
[2566] | 2197 | |
---|
[0] | 2198 | if (!$this->core->auth->check('publish,contentadmin',$this->id)) { |
---|
| 2199 | $cur->unsetField('comment_status'); |
---|
| 2200 | } |
---|
[2566] | 2201 | |
---|
[0] | 2202 | # --BEHAVIOR-- coreBeforeCommentUpdate |
---|
| 2203 | $this->core->callBehavior('coreBeforeCommentUpdate',$this,$cur,$rs); |
---|
[2566] | 2204 | |
---|
[0] | 2205 | $cur->update('WHERE comment_id = '.$id.' '); |
---|
[2566] | 2206 | |
---|
[0] | 2207 | # --BEHAVIOR-- coreAfterCommentUpdate |
---|
| 2208 | $this->core->callBehavior('coreAfterCommentUpdate',$this,$cur,$rs); |
---|
[2566] | 2209 | |
---|
[0] | 2210 | $this->triggerComment($id); |
---|
| 2211 | $this->triggerBlog(); |
---|
| 2212 | } |
---|
[2566] | 2213 | |
---|
[0] | 2214 | /** |
---|
| 2215 | Updates comment status. |
---|
[2566] | 2216 | |
---|
[0] | 2217 | @param id <b>integer</b> Comment ID |
---|
| 2218 | @param status <b>integer</b> Comment status |
---|
| 2219 | */ |
---|
| 2220 | public function updCommentStatus($id,$status) |
---|
| 2221 | { |
---|
[1041] | 2222 | $this->updCommentsStatus($id,$status); |
---|
| 2223 | } |
---|
[2566] | 2224 | |
---|
[1041] | 2225 | /** |
---|
| 2226 | Updates comments status. |
---|
[2566] | 2227 | |
---|
[1041] | 2228 | @param ids <b>mixed</b> Comment(s) ID(s) |
---|
| 2229 | @param status <b>integer</b> Comment status |
---|
| 2230 | */ |
---|
| 2231 | public function updCommentsStatus($ids,$status) |
---|
| 2232 | { |
---|
[0] | 2233 | if (!$this->core->auth->check('publish,contentadmin',$this->id)) { |
---|
| 2234 | throw new Exception(__("You are not allowed to change this comment's status")); |
---|
| 2235 | } |
---|
[2566] | 2236 | |
---|
[1041] | 2237 | $co_ids = dcUtils::cleanIds($ids); |
---|
| 2238 | $status = (integer) $status; |
---|
[2566] | 2239 | |
---|
| 2240 | $strReq = |
---|
[2626] | 2241 | 'UPDATE '.$this->prefix.'comment '. |
---|
| 2242 | 'SET comment_status = '.$status.' '; |
---|
[2566] | 2243 | $strReq .= |
---|
[2626] | 2244 | 'WHERE comment_id'.$this->con->in($co_ids). |
---|
| 2245 | 'AND post_id in (SELECT tp.post_id '. |
---|
| 2246 | 'FROM '.$this->prefix.'post tp '. |
---|
| 2247 | "WHERE tp.blog_id = '".$this->con->escape($this->id)."' "; |
---|
[1041] | 2248 | if (!$this->core->auth->check('contentadmin',$this->id)) |
---|
| 2249 | { |
---|
[2566] | 2250 | $strReq .= |
---|
[1041] | 2251 | "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; |
---|
| 2252 | } |
---|
[2626] | 2253 | $strReq .= ')'; |
---|
[1041] | 2254 | $this->con->execute($strReq); |
---|
[1042] | 2255 | $this->triggerComments($co_ids); |
---|
[1041] | 2256 | $this->triggerBlog(); |
---|
[0] | 2257 | } |
---|
[2566] | 2258 | |
---|
[0] | 2259 | /** |
---|
| 2260 | Delete a comment |
---|
[2566] | 2261 | |
---|
[0] | 2262 | @param id <b>integer</b> Comment ID |
---|
| 2263 | */ |
---|
| 2264 | public function delComment($id) |
---|
| 2265 | { |
---|
[1041] | 2266 | $this->delComments($id); |
---|
| 2267 | } |
---|
[2566] | 2268 | |
---|
[1041] | 2269 | /** |
---|
| 2270 | Delete comments |
---|
[2566] | 2271 | |
---|
[1041] | 2272 | @param ids <b>mixed</b> Comment(s) ID(s) |
---|
| 2273 | */ |
---|
| 2274 | public function delComments($ids) |
---|
| 2275 | { |
---|
[0] | 2276 | if (!$this->core->auth->check('delete,contentadmin',$this->id)) { |
---|
| 2277 | throw new Exception(__('You are not allowed to delete comments')); |
---|
| 2278 | } |
---|
[2566] | 2279 | |
---|
[1041] | 2280 | $co_ids = dcUtils::cleanIds($ids); |
---|
[2566] | 2281 | |
---|
[1262] | 2282 | if (empty($co_ids)) { |
---|
[0] | 2283 | throw new Exception(__('No such comment ID')); |
---|
| 2284 | } |
---|
[2566] | 2285 | |
---|
[1262] | 2286 | # Retrieve posts affected by comments edition |
---|
| 2287 | $affected_posts = array(); |
---|
| 2288 | $strReq = |
---|
[1570] | 2289 | 'SELECT post_id '. |
---|
[1262] | 2290 | 'FROM '.$this->prefix.'comment '. |
---|
[1570] | 2291 | 'WHERE comment_id'.$this->con->in($co_ids). |
---|
| 2292 | 'GROUP BY post_id'; |
---|
[2566] | 2293 | |
---|
[1262] | 2294 | $rs = $this->con->select($strReq); |
---|
[2566] | 2295 | |
---|
[1262] | 2296 | while ($rs->fetch()) { |
---|
| 2297 | $affected_posts[] = (integer) $rs->post_id; |
---|
| 2298 | } |
---|
[2566] | 2299 | |
---|
[2626] | 2300 | $strReq = |
---|
| 2301 | 'DELETE FROM '.$this->prefix.'comment '. |
---|
| 2302 | 'WHERE comment_id'.$this->con->in($co_ids).' '. |
---|
| 2303 | 'AND post_id in (SELECT tp.post_id '. |
---|
| 2304 | 'FROM '.$this->prefix.'post tp '. |
---|
| 2305 | "WHERE tp.blog_id = '".$this->con->escape($this->id)."' "; |
---|
[0] | 2306 | #If user can only delete, we need to check the post's owner |
---|
| 2307 | if (!$this->core->auth->check('contentadmin',$this->id)) |
---|
| 2308 | { |
---|
[2566] | 2309 | $strReq .= |
---|
[2626] | 2310 | "AND tp.user_id = '".$this->con->escape($this->core->auth->userID())."' "; |
---|
[0] | 2311 | } |
---|
[2626] | 2312 | $strReq .= ")"; |
---|
[1041] | 2313 | $this->con->execute($strReq); |
---|
[1262] | 2314 | $this->triggerComments($co_ids, true, $affected_posts); |
---|
[0] | 2315 | $this->triggerBlog(); |
---|
| 2316 | } |
---|
[1052] | 2317 | |
---|
| 2318 | public function delJunkComments() |
---|
| 2319 | { |
---|
| 2320 | if (!$this->core->auth->check('delete,contentadmin',$this->id)) { |
---|
| 2321 | throw new Exception(__('You are not allowed to delete comments')); |
---|
| 2322 | } |
---|
[2566] | 2323 | |
---|
[2626] | 2324 | $strReq = |
---|
| 2325 | 'DELETE FROM '.$this->prefix.'comment '. |
---|
| 2326 | 'WHERE comment_status = -2 '. |
---|
| 2327 | 'AND post_id in (SELECT tp.post_id '. |
---|
| 2328 | 'FROM '.$this->prefix.'post tp '. |
---|
| 2329 | "WHERE tp.blog_id = '".$this->con->escape($this->id)."' "; |
---|
[1052] | 2330 | #If user can only delete, we need to check the post's owner |
---|
| 2331 | if (!$this->core->auth->check('contentadmin',$this->id)) |
---|
| 2332 | { |
---|
[2566] | 2333 | $strReq .= |
---|
[2626] | 2334 | "AND tp.user_id = '".$this->con->escape($this->core->auth->userID())."' "; |
---|
[1052] | 2335 | } |
---|
[2626] | 2336 | $strReq .= ")"; |
---|
[1052] | 2337 | $this->con->execute($strReq); |
---|
| 2338 | $this->triggerBlog(); |
---|
| 2339 | } |
---|
[2566] | 2340 | |
---|
[0] | 2341 | private function getCommentCursor($cur) |
---|
| 2342 | { |
---|
| 2343 | if ($cur->comment_content !== null && $cur->comment_content == '') { |
---|
| 2344 | throw new Exception(__('You must provide a comment')); |
---|
| 2345 | } |
---|
[2566] | 2346 | |
---|
[0] | 2347 | if ($cur->comment_author !== null && $cur->comment_author == '') { |
---|
| 2348 | throw new Exception(__('You must provide an author name')); |
---|
| 2349 | } |
---|
[2566] | 2350 | |
---|
[0] | 2351 | if ($cur->comment_email != '' && !text::isEmail($cur->comment_email)) { |
---|
| 2352 | throw new Exception(__('Email address is not valid.')); |
---|
| 2353 | } |
---|
[2566] | 2354 | |
---|
[0] | 2355 | if ($cur->comment_site !== null && $cur->comment_site != '') { |
---|
[1227] | 2356 | if (!preg_match('|^http(s?)://|i',$cur->comment_site, $matches)) { |
---|
[0] | 2357 | $cur->comment_site = 'http://'.$cur->comment_site; |
---|
[1227] | 2358 | }else{ |
---|
| 2359 | $cur->comment_site = strtolower($matches[0]).substr($cur->comment_site, strlen($matches[0])); |
---|
[0] | 2360 | } |
---|
| 2361 | } |
---|
[2566] | 2362 | |
---|
[0] | 2363 | if ($cur->comment_status === null) { |
---|
| 2364 | $cur->comment_status = (integer) $this->settings->system->comments_pub; |
---|
| 2365 | } |
---|
[2566] | 2366 | |
---|
[0] | 2367 | # Words list |
---|
| 2368 | if ($cur->comment_content !== null) |
---|
| 2369 | { |
---|
| 2370 | $cur->comment_words = implode(' ',text::splitWords($cur->comment_content)); |
---|
| 2371 | } |
---|
| 2372 | } |
---|
| 2373 | //@} |
---|
| 2374 | } |
---|