[407] | 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 |
---|
[407] | 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=''; |
---|
| 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 | //echo $strReq; exit; |
---|
| 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 | */ |
---|
| 92 | public function addPostMedia($post_id,$media_id,$link_type='attachment') |
---|
| 93 | { |
---|
| 94 | $post_id = (integer) $post_id; |
---|
| 95 | $media_id = (integer) $media_id; |
---|
| 96 | |
---|
| 97 | $f = $this->getPostMedia(array('post_id'=>$post_id,'media_id'=>$media_id,'link_type'=>$link_type)); |
---|
| 98 | |
---|
| 99 | if (!$f->isEmpty()) { |
---|
| 100 | return; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | $cur = $this->con->openCursor($this->table); |
---|
| 104 | $cur->post_id = $post_id; |
---|
| 105 | $cur->media_id = $media_id; |
---|
| 106 | $cur->link_type = $link_type; |
---|
| 107 | |
---|
| 108 | $cur->insert(); |
---|
| 109 | $this->core->blog->triggerBlog(); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | /** |
---|
| 113 | Detaches a media from a post. |
---|
| 114 | |
---|
| 115 | @param post_id <b>integer</b> Post ID |
---|
| 116 | @param media_id <b>integer</b> Optionnal media ID |
---|
| 117 | */ |
---|
| 118 | public function removePostMedia($post_id,$media_id,$link_type=null) |
---|
| 119 | { |
---|
| 120 | $post_id = (integer) $post_id; |
---|
| 121 | $media_id = (integer) $media_id; |
---|
| 122 | |
---|
| 123 | $strReq = 'DELETE FROM '.$this->table.' '. |
---|
| 124 | 'WHERE post_id = '.$post_id.' '. |
---|
| 125 | 'AND media_id = '.$media_id.' '; |
---|
| 126 | if ($link_type != null) { |
---|
| 127 | $strReq .= "AND link_type = '".$this->con->escape($link_type)."'"; |
---|
| 128 | } |
---|
| 129 | $this->con->execute($strReq); |
---|
| 130 | $this->core->blog->triggerBlog(); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | /** |
---|
| 134 | Returns media items attached to a blog post. Result is an array containing |
---|
| 135 | fileItems objects. |
---|
| 136 | |
---|
| 137 | @param post_id <b>integer</b> Post ID |
---|
| 138 | @param media_id <b>integer</b> Optionnal media ID |
---|
| 139 | @return <b>array</b> Array of fileItems |
---|
| 140 | */ |
---|
| 141 | public function getLegacyPostMedia($post_id,$media_id=null) |
---|
| 142 | { |
---|
| 143 | $post_id = (integer) $post_id; |
---|
| 144 | |
---|
| 145 | $strReq = |
---|
| 146 | 'SELECT media_file, M.media_id, media_path, media_title, media_meta, media_dt, '. |
---|
| 147 | 'media_creadt, media_upddt, media_private, user_id '. |
---|
| 148 | 'FROM '.$this->table.' M '. |
---|
| 149 | 'INNER JOIN '.$this->table_ref.' PM ON (M.media_id = PM.media_id) '. |
---|
| 150 | "WHERE media_path = '".$this->path."' ". |
---|
| 151 | 'AND post_id = '.$post_id.' '; |
---|
| 152 | |
---|
| 153 | if ($media_id) { |
---|
| 154 | $strReq .= 'AND M.media_id = '.(integer) $media_id.' '; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | $rs = $this->con->select($strReq); |
---|
| 158 | |
---|
| 159 | $res = array(); |
---|
| 160 | |
---|
| 161 | while ($rs->fetch()) { |
---|
| 162 | $f = $this->fileRecord($rs); |
---|
| 163 | if ($f !== null) { |
---|
| 164 | $res[] = $f; |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | return $res; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | } |
---|
| 172 | ?> |
---|