summaryrefslogtreecommitdiff
path: root/app/Query/QueryMedia.php
blob: cbda1c1709c2420a0775bc82d4e663be110b7fc0 (plain)
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
<?php
/**
 * webtrees: online genealogy
 * Copyright (C) 2015 webtrees development team
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
namespace Fisharebest\Webtrees\Query;

use Fisharebest\Webtrees\Database;
use Fisharebest\Webtrees\Filter;
use Fisharebest\Webtrees\Media;

/**
 * Generate lists of files for admin_media.php
 */
class QueryMedia {
	/**
	 * Generate a list of all the folders in the current tree - for the media list.
	 *
	 * @return string[]
	 */
	public static function folderList() {
		global $WT_TREE;

		$folders = Database::prepare(
			"SELECT SQL_CACHE LEFT(m_filename, CHAR_LENGTH(m_filename) - CHAR_LENGTH(SUBSTRING_INDEX(m_filename, '/', -1))) AS media_path" .
			" FROM  `##media`" .
			" WHERE m_file = ?" .
			" AND   m_filename NOT LIKE 'http://%'" .
			" AND   m_filename NOT LIKE 'https://%'" .
			" GROUP BY 1" .
			" ORDER BY 1"
		)->execute(array($WT_TREE->getTreeId()))->fetchOneColumn();

		if (!$folders || reset($folders) != '') {
			array_unshift($folders, '');
		}

		return array_combine($folders, $folders);
	}

	/**
	 * Generate a list of all folders from all the trees - for the media admin.
	 *
	 * @return array
	 */
	public static function folderListAll() {
		$folders = Database::prepare(
			"SELECT SQL_CACHE LEFT(m_filename, CHAR_LENGTH(m_filename) - CHAR_LENGTH(SUBSTRING_INDEX(m_filename, '/', -1))) AS media_path" .
			" FROM  `##media`" .
			" WHERE m_filename NOT LIKE 'http://%'" .
			" AND   m_filename NOT LIKE 'https://%'" .
			" GROUP BY 1" .
			" ORDER BY 1"
		)->execute()->fetchOneColumn();

		if ($folders) {
			return array_combine($folders, $folders);
		} else {
			return array();
		}
	}

	/**
	 * Generate a filtered, sourced, privacy-checked list of media objects - for the media list.
	 *
	 * @param string $folder     folder to search
	 * @param string $subfolders either "include" or "exclude"
	 * @param string $sort       either "file" or "title"
	 * @param string $filter     optional search string
	 * @param string $form_type  option OBJE/FILE/FORM/TYPE
	 *
	 * @throws \Exception
	 *
	 * @return Media[]
	 */
	public static function mediaList($folder, $subfolders, $sort, $filter, $form_type) {
		global $WT_TREE;

		// All files in the folder, plus external files
		$sql =
			"SELECT m_id AS xref, m_gedcom AS gedcom" .
			" FROM `##media`" .
			" WHERE m_file=?";
		$args = array(
			$WT_TREE->getTreeId(),
		);

		// Only show external files when we are looking at the root folder
		if ($folder == '') {
			$sql_external = " OR m_filename LIKE 'http://%' OR m_filename LIKE 'https://%'";
		} else {
			$sql_external = "";
		}

		// Include / exclude subfolders (but always include external)
		switch ($subfolders) {
		case 'include':
			$sql .= " AND (m_filename LIKE CONCAT(?, '%') $sql_external)";
			$args[] = Filter::escapeLike($folder);
			break;
		case 'exclude':
			$sql .= " AND (m_filename LIKE CONCAT(?, '%')  AND m_filename NOT LIKE CONCAT(?, '%/%') $sql_external)";
			$args[] = Filter::escapeLike($folder);
			$args[] = Filter::escapeLike($folder);
			break;
		default:
			throw new \Exception('Bad argument (subfolders=' . $subfolders . ') in QueryMedia::mediaList()');
		}

		// Apply search terms
		if ($filter) {
			$sql .= " AND (SUBSTRING_INDEX(m_filename, '/', -1) LIKE CONCAT('%', ?, '%') OR m_titl LIKE CONCAT('%', ?, '%'))";
			$args[] = Filter::escapeLike($filter);
			$args[] = Filter::escapeLike($filter);
		}

		if ($form_type) {
			$sql .= " AND (m_gedcom LIKE CONCAT('%\n3 TYPE ', ?, '%'))";
			$args[] = $form_type;
		}

		switch ($sort) {
		case 'file':
			$sql .= " ORDER BY m_filename";
			break;
		case 'title':
			$sql .= " ORDER BY m_titl";
			break;
		default:
			throw new \Exception('Bad argument (sort=' . $sort . ') in QueryMedia::mediaList()');
		}

		$rows = Database::prepare($sql)->execute($args)->fetchAll();
		$list = array();
		foreach ($rows as $row) {
			$media = Media::getInstance($row->xref, $WT_TREE, $row->gedcom);
			if ($media->canShow()) {
				$list[] = $media;
			}
		}

		return $list;
	}
}