1 | <?php |
---|
2 | /** |
---|
3 | * @package Dotclear |
---|
4 | * @subpackage Core |
---|
5 | * |
---|
6 | * @copyright Olivier Meunier & Association Dotclear |
---|
7 | * @copyright GPL-2.0-only |
---|
8 | */ |
---|
9 | |
---|
10 | if (!defined('DC_RC_PATH')) {return;} |
---|
11 | |
---|
12 | class dcPostMedia |
---|
13 | { |
---|
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 |
---|
17 | |
---|
18 | /** |
---|
19 | Object constructor. |
---|
20 | |
---|
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 | } |
---|
30 | |
---|
31 | /** |
---|
32 | Returns media items attached to a blog post. Result is an array containing |
---|
33 | fileItems objects. |
---|
34 | |
---|
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 '; |
---|
44 | |
---|
45 | if (!empty($params['columns']) && is_array($params['columns'])) { |
---|
46 | $strReq .= implode(', ', $params['columns']) . ', '; |
---|
47 | } |
---|
48 | |
---|
49 | $strReq .= |
---|
50 | 'FROM ' . $this->core->prefix . 'media M ' . |
---|
51 | 'INNER JOIN ' . $this->table . ' PM ON (M.media_id = PM.media_id) '; |
---|
52 | |
---|
53 | if (!empty($params['from'])) { |
---|
54 | $strReq .= $params['from'] . ' '; |
---|
55 | } |
---|
56 | |
---|
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 | } |
---|
72 | |
---|
73 | $strReq .= 'WHERE ' . join('AND ', $where) . ' '; |
---|
74 | |
---|
75 | if (isset($params['sql'])) { |
---|
76 | $strReq .= $params['sql']; |
---|
77 | } |
---|
78 | |
---|
79 | $rs = $this->con->select($strReq); |
---|
80 | |
---|
81 | return $rs; |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | Attaches a media to a post. |
---|
86 | |
---|
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; |
---|
95 | |
---|
96 | $f = $this->getPostMedia(array('post_id' => $post_id, 'media_id' => $media_id, 'link_type' => $link_type)); |
---|
97 | |
---|
98 | if (!$f->isEmpty()) { |
---|
99 | return; |
---|
100 | } |
---|
101 | |
---|
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; |
---|
106 | |
---|
107 | $cur->insert(); |
---|
108 | $this->core->blog->triggerBlog(); |
---|
109 | } |
---|
110 | |
---|
111 | /** |
---|
112 | Detaches a media from a post. |
---|
113 | |
---|
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; |
---|
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 | } |
---|