[1505] | 1 | <?php |
---|
[3731] | 2 | /** |
---|
| 3 | * @package Dotclear |
---|
| 4 | * @subpackage Core |
---|
| 5 | * |
---|
| 6 | * @copyright Olivier Meunier & Association Dotclear |
---|
| 7 | * @copyright GPL-2.0-only |
---|
| 8 | */ |
---|
| 9 | |
---|
[3730] | 10 | if (!defined('DC_RC_PATH')) {return;} |
---|
[1505] | 11 | |
---|
| 12 | class dcPostMedia |
---|
| 13 | { |
---|
[3730] | 14 | protected $core; ///< <b>dcCore</b> dcCore instance |
---|
| 15 | protected $con; ///< <b>connection</b> Database connection |
---|
| 16 | protected $table; ///< <b>string</b> Post-Media table name |
---|
[2566] | 17 | |
---|
[3730] | 18 | /** |
---|
| 19 | Object constructor. |
---|
[2566] | 20 | |
---|
[3730] | 21 | @param core <b>dcCore</b> dcCore instance |
---|
| 22 | @param type <b>string</b> Media type filter |
---|
| 23 | */ |
---|
| 24 | public function __construct($core, $type = '') |
---|
| 25 | { |
---|
| 26 | $this->core = &$core; |
---|
| 27 | $this->con = &$core->con; |
---|
| 28 | $this->table = $this->core->prefix . 'post_media'; |
---|
| 29 | } |
---|
[2566] | 30 | |
---|
[3730] | 31 | /** |
---|
| 32 | Returns media items attached to a blog post. Result is an array containing |
---|
| 33 | fileItems objects. |
---|
[2566] | 34 | |
---|
[3730] | 35 | @param post_id <b>integer</b> Post ID |
---|
| 36 | @param media_id <b>integer</b> Optionnal media ID |
---|
| 37 | @return <b>array</b> Array of fileItems |
---|
| 38 | */ |
---|
| 39 | public function getPostMedia($params = array()) |
---|
| 40 | { |
---|
| 41 | $strReq = |
---|
| 42 | 'SELECT M.media_file, M.media_id, M.media_path, M.media_title, M.media_meta, M.media_dt, ' . |
---|
| 43 | 'M.media_creadt, M.media_upddt, M.media_private, M.user_id, PM.post_id '; |
---|
[2566] | 44 | |
---|
[3730] | 45 | if (!empty($params['columns']) && is_array($params['columns'])) { |
---|
| 46 | $strReq .= implode(', ', $params['columns']) . ', '; |
---|
| 47 | } |
---|
[2566] | 48 | |
---|
[3730] | 49 | $strReq .= |
---|
| 50 | 'FROM ' . $this->core->prefix . 'media M ' . |
---|
| 51 | 'INNER JOIN ' . $this->table . ' PM ON (M.media_id = PM.media_id) '; |
---|
[2566] | 52 | |
---|
[3730] | 53 | if (!empty($params['from'])) { |
---|
| 54 | $strReq .= $params['from'] . ' '; |
---|
| 55 | } |
---|
[2566] | 56 | |
---|
[3730] | 57 | $where = array(); |
---|
| 58 | if (isset($params['post_id'])) { |
---|
| 59 | $where[] = "PM.post_id " . $this->con->in($params['post_id']); |
---|
| 60 | } |
---|
| 61 | if (isset($params['media_id'])) { |
---|
| 62 | $where[] = "M.media_id " . $this->con->in($params['media_id']); |
---|
| 63 | } |
---|
| 64 | if (isset($params['media_path'])) { |
---|
| 65 | $where[] = "M.media_path " . $this->con->in($params['media_path']); |
---|
| 66 | } |
---|
| 67 | if (isset($params['link_type'])) { |
---|
| 68 | $where[] = "PM.link_type " . $this->con->in($params['link_type']); |
---|
| 69 | } else { |
---|
| 70 | $where[] = "PM.link_type='attachment'"; |
---|
| 71 | } |
---|
[1505] | 72 | |
---|
[3730] | 73 | $strReq .= 'WHERE ' . join('AND ', $where) . ' '; |
---|
[1505] | 74 | |
---|
[3730] | 75 | if (isset($params['sql'])) { |
---|
| 76 | $strReq .= $params['sql']; |
---|
| 77 | } |
---|
[3167] | 78 | |
---|
[3730] | 79 | $rs = $this->con->select($strReq); |
---|
[2566] | 80 | |
---|
[3730] | 81 | return $rs; |
---|
| 82 | } |
---|
[1505] | 83 | |
---|
[3730] | 84 | /** |
---|
| 85 | Attaches a media to a post. |
---|
[2566] | 86 | |
---|
[3730] | 87 | @param post_id <b>integer</b> Post ID |
---|
| 88 | @param media_id <b>integer</b> Optionnal media ID |
---|
| 89 | @param link_type <b>string</b> Optionnal link type (default: attachment) |
---|
| 90 | */ |
---|
| 91 | public function addPostMedia($post_id, $media_id, $link_type = 'attachment') |
---|
| 92 | { |
---|
| 93 | $post_id = (integer) $post_id; |
---|
| 94 | $media_id = (integer) $media_id; |
---|
[2566] | 95 | |
---|
[3730] | 96 | $f = $this->getPostMedia(array('post_id' => $post_id, 'media_id' => $media_id, 'link_type' => $link_type)); |
---|
[2566] | 97 | |
---|
[3730] | 98 | if (!$f->isEmpty()) { |
---|
| 99 | return; |
---|
| 100 | } |
---|
[2566] | 101 | |
---|
[3730] | 102 | $cur = $this->con->openCursor($this->table); |
---|
| 103 | $cur->post_id = $post_id; |
---|
| 104 | $cur->media_id = $media_id; |
---|
| 105 | $cur->link_type = $link_type; |
---|
[2566] | 106 | |
---|
[3730] | 107 | $cur->insert(); |
---|
| 108 | $this->core->blog->triggerBlog(); |
---|
| 109 | } |
---|
[2566] | 110 | |
---|
[3730] | 111 | /** |
---|
| 112 | Detaches a media from a post. |
---|
[2566] | 113 | |
---|
[3730] | 114 | @param post_id <b>integer</b> Post ID |
---|
| 115 | @param media_id <b>integer</b> Optionnal media ID |
---|
| 116 | @param link_type <b>string</b> Optionnal link type |
---|
| 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; |
---|
[2566] | 122 | |
---|
[3730] | 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 | } |
---|
[2566] | 132 | |
---|
[3730] | 133 | /** |
---|
| 134 | Returns media items attached to a blog post. Result is an array containing |
---|
| 135 | fileItems objects. |
---|
[2566] | 136 | |
---|
[3730] | 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; |
---|
[2566] | 144 | |
---|
[3730] | 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 . ' '; |
---|
[2566] | 152 | |
---|
[3730] | 153 | if ($media_id) { |
---|
| 154 | $strReq .= 'AND M.media_id = ' . (integer) $media_id . ' '; |
---|
| 155 | } |
---|
[2566] | 156 | |
---|
[3730] | 157 | $rs = $this->con->select($strReq); |
---|
[2566] | 158 | |
---|
[3730] | 159 | $res = array(); |
---|
[2566] | 160 | |
---|
[3730] | 161 | while ($rs->fetch()) { |
---|
| 162 | $f = $this->fileRecord($rs); |
---|
| 163 | if ($f !== null) { |
---|
| 164 | $res[] = $f; |
---|
| 165 | } |
---|
| 166 | } |
---|
[2566] | 167 | |
---|
[3730] | 168 | return $res; |
---|
| 169 | } |
---|
[2566] | 170 | |
---|
[1505] | 171 | } |
---|