3 * @file src/Model/FileTag.php
6 namespace Friendica\Model;
8 use Friendica\Core\L10n;
9 use Friendica\Core\PConfig;
10 use Friendica\Database\DBA;
11 use Friendica\Model\Item;
14 * @brief This class handles FileTag related functions
18 // post categories and "save to file" use the same item.file table for storage.
19 // We will differentiate the different uses by wrapping categories in angle brackets
20 // and save to file categories in square brackets.
21 // To do this we need to escape these characters if they appear in our tag.
24 * @brief URL encode <, >, left and right brackets
26 public static function encode($s)
28 return str_replace(['<', '>', '[', ']'], ['%3c', '%3e', '%5b', '%5d'], $s);
32 * @brief URL decode <, >, left and right brackets
34 public static function decode($s)
36 return str_replace(['%3c', '%3e', '%5b', '%5d'], ['<', '>', '[', ']'], $s);
40 * @brief Query files for tag
42 public static function fileQuery($table, $s, $type = 'file')
44 if ($type == 'file') {
45 $str = preg_quote('[' . str_replace('%', '%%', self::encode($s)) . ']');
47 $str = preg_quote('<' . str_replace('%', '%%', self::encode($s)) . '>');
50 return " AND " . (($table) ? DBA::escape($table) . '.' : '') . "file regexp '" . DBA::escape($str) . "' ";
54 * @brief Get file tags from list
56 * ex. given music,video return <music><video> or [music][video]
58 public static function listToFile($list, $type = 'file')
62 $list_array = explode(",", $list);
63 if ($type == 'file') {
71 foreach ($list_array as $item)
75 $tag_list .= $lbracket . self::encode(trim($item)) . $rbracket;
84 * @brief Get list from file tags
86 * ex. given <music><video>[friends], return music,video or friends
88 public static function fileToList($file, $type = 'file')
92 if ($type == 'file') {
93 $cnt = preg_match_all('/\[(.*?)\]/', $file, $matches, PREG_SET_ORDER);
95 $cnt = preg_match_all('/<(.*?)>/', $file, $matches, PREG_SET_ORDER);
98 foreach ($matches as $mtch) {
102 $list .= self::decode($mtch[1]);
110 * @brief Update file tags in PConfig
112 public static function updatePconfig($uid, $file_old, $file_new, $type = 'file')
114 // $file_old - categories previously associated with an item
115 // $file_new - new list of categories for an item
119 } elseif ($file_old == $file_new) {
123 $saved = PConfig::get($uid, 'system', 'filetags');
127 if ($type == 'file') {
130 $termtype = TERM_FILE;
134 $termtype = TERM_CATEGORY;
137 $filetags_updated = $saved;
139 // check for new tags to be added as filetags in pconfig
141 $check_new_tags = explode(",", self::fileToList($file_new, $type));
143 foreach ($check_new_tags as $tag)
145 if (!stristr($saved,$lbracket . self::encode($tag) . $rbracket)) {
150 $filetags_updated .= self::listToFile(implode(",", $new_tags), $type);
152 // check for deleted tags to be removed from filetags in pconfig
154 $check_deleted_tags = explode(",", self::fileToList($file_old, $type));
156 foreach ($check_deleted_tags as $tag)
158 if (!stristr($file_new,$lbracket . self::encode($tag) . $rbracket)) {
159 $deleted_tags[] = $tag;
163 foreach ($deleted_tags as $key => $tag)
165 $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d",
167 intval(TERM_OBJ_POST),
171 if (DBA::isResult($r)) {
172 unset($deleted_tags[$key]);
174 $filetags_updated = str_replace($lbracket . self::encode($tag) . $rbracket, '', $filetags_updated);
178 if ($saved != $filetags_updated)
180 PConfig::set($uid, 'system', 'filetags', $filetags_updated);
184 } elseif (strlen($file_new)) {
185 PConfig::set($uid, 'system', 'filetags', $file_new);
192 * @brief Add tag to file
194 public static function saveFile($uid, $item_id, $file)
201 $item = Item::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
202 if (DBA::isResult($item))
204 if (!stristr($item['file'], '[' . self::encode($file) . ']'))
206 $fields = ['file' => $item['file'] . '[' . self::encode($file) . ']'];
207 Item::update($fields, ['id' => $item_id]);
210 $saved = PConfig::get($uid, 'system', 'filetags');
212 if (!strlen($saved) || !stristr($saved, '[' . self::encode($file) . ']'))
214 PConfig::set($uid, 'system', 'filetags', $saved . '[' . self::encode($file) . ']');
217 info(L10n::t('Item filed'));
224 * @brief Remove tag from file
226 public static function unsaveFile($uid, $item_id, $file, $cat = false)
234 $pattern = '<' . self::encode($file) . '>' ;
235 $termtype = TERM_CATEGORY;
237 $pattern = '[' . self::encode($file) . ']' ;
238 $termtype = TERM_FILE;
241 $item = Item::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
243 if (!DBA::isResult($item))
248 $fields = ['file' => str_replace($pattern, '', $item['file'])];
249 Item::update($fields, ['id' => $item_id]);
251 $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d",
253 intval(TERM_OBJ_POST),
258 if (!DBA::isResult($r))
260 $saved = PConfig::get($uid, 'system', 'filetags');
261 PConfig::set($uid, 'system', 'filetags', str_replace($pattern, '', $saved));