| 1 | <?php |
|---|
| 2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
|---|
| 3 | # |
|---|
| 4 | # This file is part of Dotclear 2. |
|---|
| 5 | # |
|---|
| 6 | # Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear |
|---|
| 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 | class dcPostMedia |
|---|
| 15 | { |
|---|
| 16 | protected $core; ///< <b>dcCore</b> dcCore instance |
|---|
| 17 | protected $con; ///< <b>connection</b> Database connection |
|---|
| 18 | protected $table; ///< <b>string</b> Post-Media table name |
|---|
| 19 | |
|---|
| 20 | /** |
|---|
| 21 | Object constructor. |
|---|
| 22 | |
|---|
| 23 | @param core <b>dcCore</b> dcCore instance |
|---|
| 24 | @param type <b>string</b> Media type filter |
|---|
| 25 | */ |
|---|
| 26 | public function __construct($core,$type='') |
|---|
| 27 | { |
|---|
| 28 | $this->core =& $core; |
|---|
| 29 | $this->con =& $core->con; |
|---|
| 30 | $this->table = $this->core->prefix.'post_media'; |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | /** |
|---|
| 34 | Returns media items attached to a blog post. Result is an array containing |
|---|
| 35 | fileItems objects. |
|---|
| 36 | |
|---|
| 37 | @param post_id <b>integer</b> Post ID |
|---|
| 38 | @param media_id <b>integer</b> Optionnal media ID |
|---|
| 39 | @return <b>array</b> Array of fileItems |
|---|
| 40 | */ |
|---|
| 41 | public function getPostMedia($params=array()) |
|---|
| 42 | { |
|---|
| 43 | $strReq = |
|---|
| 44 | 'SELECT M.media_file, M.media_id, M.media_path, M.media_title, M.media_meta, M.media_dt, '. |
|---|
| 45 | 'M.media_creadt, M.media_upddt, M.media_private, M.user_id, PM.post_id '; |
|---|
| 46 | |
|---|
| 47 | if (!empty($params['columns']) && is_array($params['columns'])) { |
|---|
| 48 | $strReq .= implode(', ',$params['columns']).', '; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | $strReq .= |
|---|
| 52 | 'FROM '.$this->core->prefix.'media M '. |
|---|
| 53 | 'INNER JOIN '.$this->table.' PM ON (M.media_id = PM.media_id) '; |
|---|
| 54 | |
|---|
| 55 | if (!empty($params['from'])) { |
|---|
| 56 | $strReq .= $params['from'].' '; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | $where = array(); |
|---|
| 60 | if (isset($params['post_id'])) { |
|---|
| 61 | $where[] = "PM.post_id ".$this->con->in($params['post_id']); |
|---|
| 62 | } |
|---|
| 63 | if (isset($params['media_id'])) { |
|---|
| 64 | $where[] = "M.media_id ".$this->con->in($params['media_id']); |
|---|
| 65 | } |
|---|
| 66 | if (isset($params['media_path'])) { |
|---|
| 67 | $where[] = "M.media_path ".$this->con->in($params['media_path']); |
|---|
| 68 | } |
|---|
| 69 | if (isset($params['link_type'])) { |
|---|
| 70 | $where[] = "PM.link_type ".$this->con->in($params['link_type']); |
|---|
| 71 | } else { |
|---|
| 72 | $where[] = "PM.link_type='attachment'"; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | $strReq .= 'WHERE '.join('AND ',$where).' '; |
|---|
| 76 | |
|---|
| 77 | if (isset($params['sql'])) { |
|---|
| 78 | $strReq .= $params['sql']; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | $rs = $this->con->select($strReq); |
|---|
| 82 | |
|---|
| 83 | return $rs; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | /** |
|---|
| 87 | Attaches a media to a post. |
|---|
| 88 | |
|---|
| 89 | @param post_id <b>integer</b> Post ID |
|---|
| 90 | @param media_id <b>integer</b> Optionnal media ID |
|---|
| 91 | @param link_type <b>string</b> Optionnal link type (default: attachment) |
|---|
| 92 | */ |
|---|
| 93 | public function addPostMedia($post_id,$media_id,$link_type='attachment') |
|---|
| 94 | { |
|---|
| 95 | $post_id = (integer) $post_id; |
|---|
| 96 | $media_id = (integer) $media_id; |
|---|
| 97 | |
|---|
| 98 | $f = $this->getPostMedia(array('post_id'=>$post_id,'media_id'=>$media_id,'link_type'=>$link_type)); |
|---|
| 99 | |
|---|
| 100 | if (!$f->isEmpty()) { |
|---|
| 101 | return; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | $cur = $this->con->openCursor($this->table); |
|---|
| 105 | $cur->post_id = $post_id; |
|---|
| 106 | $cur->media_id = $media_id; |
|---|
| 107 | $cur->link_type = $link_type; |
|---|
| 108 | |
|---|
| 109 | $cur->insert(); |
|---|
| 110 | $this->core->blog->triggerBlog(); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | /** |
|---|
| 114 | Detaches a media from a post. |
|---|
| 115 | |
|---|
| 116 | @param post_id <b>integer</b> Post ID |
|---|
| 117 | @param media_id <b>integer</b> Optionnal media ID |
|---|
| 118 | @param link_type <b>string</b> Optionnal link type |
|---|
| 119 | */ |
|---|
| 120 | public function removePostMedia($post_id,$media_id,$link_type=null) |
|---|
| 121 | { |
|---|
| 122 | $post_id = (integer) $post_id; |
|---|
| 123 | $media_id = (integer) $media_id; |
|---|
| 124 | |
|---|
| 125 | $strReq = 'DELETE FROM '.$this->table.' '. |
|---|
| 126 | 'WHERE post_id = '.$post_id.' '. |
|---|
| 127 | 'AND media_id = '.$media_id.' '; |
|---|
| 128 | if ($link_type != null) { |
|---|
| 129 | $strReq .= "AND link_type = '".$this->con->escape($link_type)."'"; |
|---|
| 130 | } |
|---|
| 131 | $this->con->execute($strReq); |
|---|
| 132 | $this->core->blog->triggerBlog(); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | /** |
|---|
| 136 | Returns media items attached to a blog post. Result is an array containing |
|---|
| 137 | fileItems objects. |
|---|
| 138 | |
|---|
| 139 | @param post_id <b>integer</b> Post ID |
|---|
| 140 | @param media_id <b>integer</b> Optionnal media ID |
|---|
| 141 | @return <b>array</b> Array of fileItems |
|---|
| 142 | */ |
|---|
| 143 | public function getLegacyPostMedia($post_id,$media_id=null) |
|---|
| 144 | { |
|---|
| 145 | $post_id = (integer) $post_id; |
|---|
| 146 | |
|---|
| 147 | $strReq = |
|---|
| 148 | 'SELECT media_file, M.media_id, media_path, media_title, media_meta, media_dt, '. |
|---|
| 149 | 'media_creadt, media_upddt, media_private, user_id '. |
|---|
| 150 | 'FROM '.$this->table.' M '. |
|---|
| 151 | 'INNER JOIN '.$this->table_ref.' PM ON (M.media_id = PM.media_id) '. |
|---|
| 152 | "WHERE media_path = '".$this->path."' ". |
|---|
| 153 | 'AND post_id = '.$post_id.' '; |
|---|
| 154 | |
|---|
| 155 | if ($media_id) { |
|---|
| 156 | $strReq .= 'AND M.media_id = '.(integer) $media_id.' '; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | $rs = $this->con->select($strReq); |
|---|
| 160 | |
|---|
| 161 | $res = array(); |
|---|
| 162 | |
|---|
| 163 | while ($rs->fetch()) { |
|---|
| 164 | $f = $this->fileRecord($rs); |
|---|
| 165 | if ($f !== null) { |
|---|
| 166 | $res[] = $f; |
|---|
| 167 | } |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | return $res; |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | } |
|---|