2020-05-03 11:13:40 -04:00
|
|
|
<?php
|
|
|
|
/**
|
2021-03-29 02:40:20 -04:00
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
2020-05-03 11:13:40 -04:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Model\Post;
|
|
|
|
|
|
|
|
use Friendica\Database\DBA;
|
2021-01-15 23:13:22 -05:00
|
|
|
use Friendica\Model\Post;
|
2020-05-03 11:13:40 -04:00
|
|
|
use Friendica\Model\Tag;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Category
|
|
|
|
*
|
|
|
|
* This Model class handles category table interactions.
|
|
|
|
* This tables stores user-applied categories related to posts.
|
|
|
|
*/
|
|
|
|
class Category
|
|
|
|
{
|
|
|
|
const UNKNOWN = 0;
|
|
|
|
const CATEGORY = 3;
|
|
|
|
const FILE = 5;
|
|
|
|
|
2021-01-21 02:16:41 -05:00
|
|
|
/**
|
|
|
|
* Delete all categories and files from a given uri-id and user
|
|
|
|
*
|
|
|
|
* @param int $uri_id
|
|
|
|
* @param int $uid
|
|
|
|
* @return boolean success
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public static function deleteByURIId(int $uri_id, int $uid)
|
|
|
|
{
|
|
|
|
return DBA::delete('post-category', ['uri-id' => $uri_id, 'uid' => $uid]);
|
|
|
|
}
|
|
|
|
|
2021-01-21 15:25:06 -05:00
|
|
|
/**
|
|
|
|
* Delete all categories and files from a given uri-id and user
|
|
|
|
*
|
|
|
|
* @param int $uri_id
|
|
|
|
* @param int $uid
|
|
|
|
* @return boolean success
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public static function deleteFileByURIId(int $uri_id, int $uid, int $type, string $file)
|
|
|
|
{
|
|
|
|
$tagid = Tag::getID($file);
|
|
|
|
if (empty($tagid)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DBA::delete('post-category', ['uri-id' => $uri_id, 'uid' => $uid, 'type' => $type, 'tid' => $tagid]);
|
|
|
|
}
|
2020-05-03 11:13:40 -04:00
|
|
|
/**
|
|
|
|
* Generates the legacy item.file field string from an item ID.
|
|
|
|
* Includes only file and category terms.
|
|
|
|
*
|
2021-01-21 02:16:41 -05:00
|
|
|
* @param int $uri_id
|
|
|
|
* @param int $uid
|
2020-05-03 11:13:40 -04:00
|
|
|
* @return string
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public static function getTextByURIId(int $uri_id, int $uid)
|
|
|
|
{
|
|
|
|
$file_text = '';
|
|
|
|
|
2020-05-03 11:44:39 -04:00
|
|
|
$tags = DBA::selectToArray('category-view', ['type', 'name'], ['uri-id' => $uri_id, 'uid' => $uid]);
|
2020-05-03 11:13:40 -04:00
|
|
|
foreach ($tags as $tag) {
|
|
|
|
if ($tag['type'] == self::CATEGORY) {
|
2020-05-03 11:44:39 -04:00
|
|
|
$file_text .= '<' . $tag['name'] . '>';
|
2020-05-03 11:13:40 -04:00
|
|
|
} else {
|
2020-05-03 11:44:39 -04:00
|
|
|
$file_text .= '[' . $tag['name'] . ']';
|
2020-05-03 11:13:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $file_text;
|
|
|
|
}
|
|
|
|
|
2021-01-21 02:16:41 -05:00
|
|
|
/**
|
|
|
|
* Generates an array of files or categories of a given uri-id
|
|
|
|
*
|
|
|
|
* @param int $uid
|
|
|
|
* @param int $type
|
|
|
|
* @return array
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public static function getArray(int $uid, int $type)
|
|
|
|
{
|
|
|
|
$tags = DBA::selectToArray('category-view', ['name'], ['uid' => $uid, 'type' => $type],
|
|
|
|
['group_by' => ['name'], 'order' => ['name']]);
|
|
|
|
if (empty($tags)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_column($tags, 'name');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates an array of files or categories of a given uri-id
|
|
|
|
*
|
|
|
|
* @param int $uri_id
|
|
|
|
* @param int $uid
|
|
|
|
* @param int $type
|
|
|
|
* @return array
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public static function getArrayByURIId(int $uri_id, int $uid, int $type = self::CATEGORY)
|
|
|
|
{
|
|
|
|
$tags = DBA::selectToArray('category-view', ['type', 'name'], ['uri-id' => $uri_id, 'uid' => $uid, 'type' => $type]);
|
|
|
|
if (empty($tags)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_column($tags, 'name');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a comma separated list of files or categories
|
|
|
|
*
|
|
|
|
* @param int $uri_id
|
|
|
|
* @param int $uid
|
|
|
|
* @param int $type
|
|
|
|
* @return string
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public static function getCSVByURIId(int $uri_id, int $uid, int $type)
|
|
|
|
{
|
|
|
|
return implode(',', self::getArrayByURIId($uri_id, $uid, $type));
|
|
|
|
}
|
|
|
|
|
2020-05-03 11:13:40 -04:00
|
|
|
/**
|
|
|
|
* Inserts new terms for the provided item ID based on the legacy item.file field BBCode content.
|
|
|
|
* Deletes all previous file terms for the same item ID.
|
|
|
|
*
|
|
|
|
* @param integer $item_id item id
|
|
|
|
* @param $files
|
|
|
|
* @return void
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public static function storeTextByURIId(int $uri_id, int $uid, string $files)
|
|
|
|
{
|
2021-01-15 23:13:22 -05:00
|
|
|
$message = Post::selectFirst(['deleted'], ['uri-id' => $uri_id, 'uid' => $uid]);
|
2020-06-14 23:43:51 -04:00
|
|
|
if (DBA::isResult($message)) {
|
|
|
|
// Clean up all tags
|
|
|
|
DBA::delete('post-category', ['uri-id' => $uri_id, 'uid' => $uid]);
|
2020-05-03 11:13:40 -04:00
|
|
|
|
2020-06-14 23:43:51 -04:00
|
|
|
if ($message['deleted']) {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-03 11:13:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (preg_match_all("/\[(.*?)\]/ism", $files, $result)) {
|
|
|
|
foreach ($result[1] as $file) {
|
|
|
|
$tagid = Tag::getID($file);
|
|
|
|
if (empty($tagid)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-10-23 15:10:17 -04:00
|
|
|
DBA::replace('post-category', [
|
2020-05-03 11:13:40 -04:00
|
|
|
'uri-id' => $uri_id,
|
|
|
|
'uid' => $uid,
|
|
|
|
'type' => self::FILE,
|
|
|
|
'tid' => $tagid
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preg_match_all("/\<(.*?)\>/ism", $files, $result)) {
|
|
|
|
foreach ($result[1] as $file) {
|
2021-01-21 15:25:06 -05:00
|
|
|
self::storeFileByURIId($uri_id, $uid, self::FILE, $file);
|
2020-05-03 11:13:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-21 15:25:06 -05:00
|
|
|
|
|
|
|
public static function storeFileByURIId(int $uri_id, int $uid, int $type, string $file)
|
|
|
|
{
|
|
|
|
$tagid = Tag::getID($file);
|
|
|
|
if (empty($tagid)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DBA::replace('post-category', [
|
|
|
|
'uri-id' => $uri_id,
|
|
|
|
'uid' => $uid,
|
|
|
|
'type' => $type,
|
|
|
|
'tid' => $tagid
|
|
|
|
]);
|
|
|
|
}
|
2020-05-03 11:13:40 -04:00
|
|
|
}
|