]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Post/Media.php
Merge branch '2021.03-rc' into copyright-2021
[friendica.git] / src / Model / Post / Media.php
index 57668fa99e37413eb0b5200027e04619a24d31c5..5dc5a0daa241ef8bc3df99e327cca313d98aa43b 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -23,6 +23,7 @@ namespace Friendica\Model\Post;
 
 use Friendica\Core\Logger;
 use Friendica\Core\System;
+use Friendica\Database\Database;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Util\Images;
@@ -72,20 +73,36 @@ class Media
 
                // We are storing as fast as possible to avoid duplicated network requests
                // when fetching additional information for pictures and other content.
-               $result = DBA::insert('post-media', $media, true);
+               $result = DBA::insert('post-media', $media, Database::INSERT_UPDATE);
                Logger::info('Stored media', ['result' => $result, 'media' => $media, 'callstack' => System::callstack()]);
                $stored = $media;
 
                $media = self::fetchAdditionalData($media);
 
                if (array_diff_assoc($media, $stored)) {
-                       $result = DBA::insert('post-media', $media, true);
+                       $result = DBA::insert('post-media', $media, Database::INSERT_UPDATE);
                        Logger::info('Updated media', ['result' => $result, 'media' => $media]);
                } else {
                        Logger::info('Nothing to update', ['media' => $media]);
                }
        }
 
+       /**
+        * Copy attachments from one uri-id to another
+        *
+        * @param integer $from_uri_id
+        * @param integer $to_uri_id
+        * @return void
+        */
+       public static function copy(int $from_uri_id, int $to_uri_id)
+       {
+               $attachments = self::getByURIId($from_uri_id);
+               foreach ($attachments as $attachment) {
+                       $attachment['uri-id'] = $to_uri_id;
+                       self::insert($attachment);
+               }
+       }
+
        /**
         * Creates the "[attach]" element from the given attributes
         *
@@ -263,11 +280,18 @@ class Media
         * Retrieves the media attachments associated with the provided item ID.
         *
         * @param int $uri_id
+        * @param array $types
         * @return array
         * @throws \Exception
         */
-       public static function getByURIId(int $uri_id)
+       public static function getByURIId(int $uri_id, array $types = [])
        {
-               return DBA::selectToArray('post-media', [], ['uri-id' => $uri_id]);
+               $condition = ['uri-id' => $uri_id];
+
+               if (!empty($types)) {
+                       $condition = DBA::mergeConditions($condition, ['type' => $types]);
+               }
+
+               return DBA::selectToArray('post-media', [], $condition);
        }
 }