File: class.dc.postmedia.php

package info (click to toggle)
dotclear 2.6.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 8,420 kB
  • sloc: php: 54,270; sql: 1,290; sh: 213; xml: 173; makefile: 158
file content (171 lines) | stat: -rw-r--r-- 4,471 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
# -- BEGIN LICENSE BLOCK ---------------------------------------
#
# This file is part of Dotclear 2.
#
# Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear
# Licensed under the GPL version 2.0 license.
# See LICENSE file or
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#
# -- END LICENSE BLOCK -----------------------------------------
if (!defined('DC_RC_PATH')) { return; }

class dcPostMedia
{
	protected $core;		///< <b>dcCore</b> dcCore instance
	protected $con;		///< <b>connection</b> Database connection
	protected $table;		///< <b>string</b> Post-Media table name

	/**
	Object constructor.

	@param	core		<b>dcCore</b>		dcCore instance
	@param	type		<b>string</b>		Media type filter
	*/
	public function __construct($core,$type='')
	{
		$this->core =& $core;
		$this->con =& $core->con;
		$this->table = $this->core->prefix.'post_media';
	}

	/**
	Returns media items attached to a blog post. Result is an array containing
	fileItems objects.

	@param	post_id	<b>integer</b>		Post ID
	@param	media_id	<b>integer</b>		Optionnal media ID
	@return	<b>array</b> Array of fileItems
	*/
	public function getPostMedia($params=array())
	{
		$strReq =
		'SELECT M.media_file, M.media_id, M.media_path, M.media_title, M.media_meta, M.media_dt, '.
		'M.media_creadt, M.media_upddt, M.media_private, M.user_id, PM.post_id ';

		if (!empty($params['columns']) && is_array($params['columns'])) {
			$strReq .= implode(', ',$params['columns']).', ';
		}

		$strReq .=
		'FROM '.$this->core->prefix.'media M '.
		'INNER JOIN '.$this->table.' PM ON (M.media_id = PM.media_id) ';

		if (!empty($params['from'])) {
			$strReq .= $params['from'].' ';
		}

		$where='';
		if (isset($params['post_id'])) {
			$where[]="PM.post_id ".$this->con->in($params['post_id']);
		}
		if (isset($params['media_id'])) {
			$where[]="M.media_id ".$this->con->in($params['media_id']);
		}
		if (isset($params['media_path'])) {
			$where[]="M.media_path ".$this->con->in($params['media_path']);
		}
		if (isset($params['link_type'])) {
			$where[]="PM.link_type ".$this->con->in($params['link_type']);
		} else {
			$where[]="PM.link_type='attachment'";
		}

		$strReq .= 'WHERE '.join('AND ',$where).' ';

		if (isset($params['sql'])) {
			$strReq .= $params['sql'];
		}
	//echo $strReq; exit;
		$rs = $this->con->select($strReq);

		return $rs;
	}

	/**
	Attaches a media to a post.

	@param	post_id	<b>integer</b>		Post ID
	@param	media_id	<b>integer</b>		Optionnal media ID
	*/
	public function addPostMedia($post_id,$media_id,$link_type='attachment')
	{
		$post_id = (integer) $post_id;
		$media_id = (integer) $media_id;

		$f = $this->getPostMedia(array('post_id'=>$post_id,'media_id'=>$media_id,'link_type'=>$link_type));

		if (!$f->isEmpty()) {
			return;
		}

		$cur = $this->con->openCursor($this->table);
		$cur->post_id = $post_id;
		$cur->media_id = $media_id;
		$cur->link_type = $link_type;

		$cur->insert();
		$this->core->blog->triggerBlog();
	}

	/**
	Detaches a media from a post.

	@param	post_id	<b>integer</b>		Post ID
	@param	media_id	<b>integer</b>		Optionnal media ID
	*/
	public function removePostMedia($post_id,$media_id,$link_type=null)
	{
		$post_id = (integer) $post_id;
		$media_id = (integer) $media_id;

		$strReq = 'DELETE FROM '.$this->table.' '.
				'WHERE post_id = '.$post_id.' '.
				'AND media_id = '.$media_id.' ';
		if ($link_type != null) {
			$strReq .= "AND link_type = '".$this->con->escape($link_type)."'";
		}
		$this->con->execute($strReq);
		$this->core->blog->triggerBlog();
	}

	/**
	Returns media items attached to a blog post. Result is an array containing
	fileItems objects.

	@param	post_id	<b>integer</b>		Post ID
	@param	media_id	<b>integer</b>		Optionnal media ID
	@return	<b>array</b> Array of fileItems
	*/
	public function getLegacyPostMedia($post_id,$media_id=null)
	{
		$post_id = (integer) $post_id;

		$strReq =
		'SELECT media_file, M.media_id, media_path, media_title, media_meta, media_dt, '.
		'media_creadt, media_upddt, media_private, user_id '.
		'FROM '.$this->table.' M '.
		'INNER JOIN '.$this->table_ref.' PM ON (M.media_id = PM.media_id) '.
		"WHERE media_path = '".$this->path."' ".
		'AND post_id = '.$post_id.' ';

		if ($media_id) {
			$strReq .= 'AND M.media_id = '.(integer) $media_id.' ';
		}

		$rs = $this->con->select($strReq);

		$res = array();

		while ($rs->fetch()) {
			$f = $this->fileRecord($rs);
			if ($f !== null) {
				$res[] = $f;
			}
		}

		return $res;
	}

}