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 * @param string $s String to be URL encoded.
28 * @return string The URL encoded string.
30 public static function encode($s)
32 return str_replace(['<', '>', '[', ']'], ['%3c', '%3e', '%5b', '%5d'], $s);
36 * @brief URL decode <, >, left and right brackets
38 * @param string $s The URL encoded string to be decoded
40 * @return string The decoded string.
42 public static function decode($s)
44 return str_replace(['%3c', '%3e', '%5b', '%5d'], ['<', '>', '[', ']'], $s);
48 * @brief Query files for tag
50 * @param string $table The table to be queired.
51 * @param string $s The search term
52 * @param string $type Optional file type.
54 * @return string Query string.
56 public static function fileQuery($table, $s, $type = 'file')
58 if ($type == 'file') {
59 $str = preg_quote('[' . str_replace('%', '%%', self::encode($s)) . ']');
61 $str = preg_quote('<' . str_replace('%', '%%', self::encode($s)) . '>');
64 return " AND " . (($table) ? DBA::escape($table) . '.' : '') . "file regexp '" . DBA::escape($str) . "' ";
68 * @brief Get file tags from list
70 * ex. given music,video return <music><video> or [music][video]
71 * @param string $list A comma delimited list of tags.
72 * @param string $type Optional file type.
74 * @return string A list of file tags.
76 public static function listToFile($list, $type = 'file')
80 $list_array = explode(",", $list);
81 if ($type == 'file') {
89 foreach ($list_array as $item)
93 $tag_list .= $lbracket . self::encode(trim($item)) . $rbracket;
102 * @brief Get list from file tags
104 * ex. given <music><video>[friends], return music,video or friends
105 * @param string $file File tags
106 * @param string $type Optional file type.
108 * @return string Comma delimited list of tag names.
110 public static function fileToList($file, $type = 'file')
115 if ($type == 'file') {
116 $cnt = preg_match_all('/\[(.*?)\]/', $file, $matches, PREG_SET_ORDER);
118 $cnt = preg_match_all('/<(.*?)>/', $file, $matches, PREG_SET_ORDER);
123 foreach ($matches as $mtch)
130 $list .= self::decode($mtch[1]);
138 * @brief Update file tags in PConfig
140 * @param int $uid Unique Identity.
141 * @param string $file_old Categories previously associated with an item
142 * @param string $file_new New list of categories for an item
143 * @param string $type Optional file type.
145 * @return boolean A value indicating success or failure.
147 public static function updatePconfig($uid, $file_old, $file_new, $type = 'file')
151 } elseif ($file_old == $file_new) {
155 $saved = PConfig::get($uid, 'system', 'filetags');
159 if ($type == 'file') {
162 $termtype = TERM_FILE;
166 $termtype = TERM_CATEGORY;
169 $filetags_updated = $saved;
171 // check for new tags to be added as filetags in pconfig
173 $check_new_tags = explode(",", self::fileToList($file_new, $type));
175 foreach ($check_new_tags as $tag)
177 if (!stristr($saved,$lbracket . self::encode($tag) . $rbracket)) {
182 $filetags_updated .= self::listToFile(implode(",", $new_tags), $type);
184 // check for deleted tags to be removed from filetags in pconfig
186 $check_deleted_tags = explode(",", self::fileToList($file_old, $type));
188 foreach ($check_deleted_tags as $tag)
190 if (!stristr($file_new,$lbracket . self::encode($tag) . $rbracket)) {
191 $deleted_tags[] = $tag;
195 foreach ($deleted_tags as $key => $tag)
197 $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d",
199 intval(TERM_OBJ_POST),
203 if (DBA::isResult($r)) {
204 unset($deleted_tags[$key]);
206 $filetags_updated = str_replace($lbracket . self::encode($tag) . $rbracket, '', $filetags_updated);
210 if ($saved != $filetags_updated)
212 PConfig::set($uid, 'system', 'filetags', $filetags_updated);
216 } elseif (strlen($file_new)) {
217 PConfig::set($uid, 'system', 'filetags', $file_new);
224 * @brief Add tag to file
226 * @param int $uid Unique identity.
227 * @param int $item_id Item identity.
228 * @param string $file File tag.
230 * @return boolean A value indicating success or failure.
232 public static function saveFile($uid, $item_id, $file)
239 $item = Item::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
240 if (DBA::isResult($item))
242 if (!stristr($item['file'], '[' . self::encode($file) . ']'))
244 $fields = ['file' => $item['file'] . '[' . self::encode($file) . ']'];
245 Item::update($fields, ['id' => $item_id]);
248 $saved = PConfig::get($uid, 'system', 'filetags');
250 if (!strlen($saved) || !stristr($saved, '[' . self::encode($file) . ']'))
252 PConfig::set($uid, 'system', 'filetags', $saved . '[' . self::encode($file) . ']');
255 info(L10n::t('Item filed'));
262 * @brief Remove tag from file
264 * @param int $uid Unique identity.
265 * @param int $item_id Item identity.
266 * @param string $file File tag.
267 * @param boolean $cat Optional value indicating the term type (i.e. Category or File)
269 * @return boolean A value indicating success or failure.
271 public static function unsaveFile($uid, $item_id, $file, $cat = false)
279 $pattern = '<' . self::encode($file) . '>';
280 $termtype = TERM_CATEGORY;
282 $pattern = '[' . self::encode($file) . ']';
283 $termtype = TERM_FILE;
286 $item = Item::selectFirst(['file'], ['id' => $item_id, 'uid' => $uid]);
288 if (!DBA::isResult($item))
293 $fields = ['file' => str_replace($pattern, '', $item['file'])];
295 Item::update($fields, ['id' => $item_id]);
297 $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d",
299 intval(TERM_OBJ_POST),
304 if (!DBA::isResult($r))
306 $saved = PConfig::get($uid, 'system', 'filetags');
307 PConfig::set($uid, 'system', 'filetags', str_replace($pattern, '', $saved));