| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types = 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // {{{ License
 | 
					
						
							|  |  |  | // This file is part of GNU social - https://www.gnu.org/software/social
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social 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.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social 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 GNU social.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							|  |  |  | // }}}
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Attachments Albums for GNU social | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package   GNUsocial | 
					
						
							|  |  |  |  * @category  Plugin | 
					
						
							| 
									
										
										
										
											2021-12-25 17:31:16 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  |  * @author    Phablulo <phablulo@gmail.com> | 
					
						
							|  |  |  |  * @copyright 2018-2019, 2021 Free Software Foundation, Inc http://www.fsf.org | 
					
						
							|  |  |  |  * @license   https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Plugin\AttachmentCollections; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use App\Core\DB\DB; | 
					
						
							| 
									
										
										
										
											2021-12-25 17:31:16 +00:00
										 |  |  | use App\Core\Event; | 
					
						
							|  |  |  | use App\Core\Form; | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  | use function App\Core\I18n\_m; | 
					
						
							| 
									
										
										
										
											2021-12-25 17:31:16 +00:00
										 |  |  | use App\Core\Modules\Plugin; | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  | use App\Core\Router\RouteLoader; | 
					
						
							| 
									
										
										
										
											2021-12-25 17:31:16 +00:00
										 |  |  | use App\Core\Router\Router; | 
					
						
							|  |  |  | use App\Entity\Feed; | 
					
						
							|  |  |  | use App\Entity\LocalUser; | 
					
						
							|  |  |  | use App\Util\Common; | 
					
						
							| 
									
										
										
										
											2021-12-28 04:39:09 +00:00
										 |  |  | use App\Util\Exception\RedirectException; | 
					
						
							| 
									
										
										
										
											2021-12-25 17:31:16 +00:00
										 |  |  | use App\Util\Formatting; | 
					
						
							|  |  |  | use App\Util\Nickname; | 
					
						
							| 
									
										
										
										
											2021-12-28 04:39:09 +00:00
										 |  |  | use Component\Collection\Entity\Collection; | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  | use Plugin\AttachmentCollections\Controller as C; | 
					
						
							| 
									
										
										
										
											2021-12-28 04:39:09 +00:00
										 |  |  | use Plugin\AttachmentCollections\Entity\AttachmentCollectionEntry; | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  | use Symfony\Component\Form\Extension\Core\Type\ChoiceType; | 
					
						
							| 
									
										
										
										
											2021-12-25 17:31:16 +00:00
										 |  |  | use Symfony\Component\Form\Extension\Core\Type\SubmitType; | 
					
						
							|  |  |  | use Symfony\Component\Form\Extension\Core\Type\TextType; | 
					
						
							|  |  |  | use Symfony\Component\HttpFoundation\Request; | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | class AttachmentCollections extends Plugin | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public function onAddRoute(RouteLoader $r): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // View all collections by actor id and nickname
 | 
					
						
							|  |  |  |         $r->connect( | 
					
						
							| 
									
										
										
										
											2021-12-25 17:31:16 +00:00
										 |  |  |             id: 'collections_view_by_actor_id', | 
					
						
							|  |  |  |             uri_path: '/actor/{id<\d+>}/collections', | 
					
						
							|  |  |  |             target: [C\Controller::class, 'collectionsViewByActorId'], | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  |         ); | 
					
						
							|  |  |  |         $r->connect( | 
					
						
							| 
									
										
										
										
											2021-12-25 17:31:16 +00:00
										 |  |  |             id: 'collections_view_by_nickname', | 
					
						
							|  |  |  |             uri_path: '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/collections', | 
					
						
							|  |  |  |             target: [C\Controller::class, 'collectionsByActorNickname'], | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  |         ); | 
					
						
							|  |  |  |         // View notes from a collection by actor id and nickname
 | 
					
						
							|  |  |  |         $r->connect( | 
					
						
							| 
									
										
										
										
											2021-12-25 17:31:16 +00:00
										 |  |  |             id: 'collection_notes_view_by_actor_id', | 
					
						
							|  |  |  |             uri_path: '/actor/{id<\d+>}/collections/{cid<\d+>}', | 
					
						
							|  |  |  |             target: [C\Controller::class, 'collectionNotesViewByActorId'], | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  |         ); | 
					
						
							|  |  |  |         $r->connect( | 
					
						
							| 
									
										
										
										
											2021-12-25 17:31:16 +00:00
										 |  |  |             id: 'collection_notes_view_by_nickname', | 
					
						
							|  |  |  |             uri_path: '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/collections/{cid<\d+>}', | 
					
						
							|  |  |  |             target: [C\Controller::class, 'collectionNotesByNickname'], | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  |         ); | 
					
						
							|  |  |  |         return Event::next; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-28 04:39:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  |     public function onCreateDefaultFeeds(int $actor_id, LocalUser $user, int &$ordering) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         DB::persist(Feed::create([ | 
					
						
							|  |  |  |             'actor_id' => $actor_id, | 
					
						
							| 
									
										
										
										
											2021-12-25 17:31:16 +00:00
										 |  |  |             'url'      => Router::url($route = 'collections_view_by_nickname', ['nickname' => $user->getNickname()]), | 
					
						
							|  |  |  |             'route'    => $route, | 
					
						
							|  |  |  |             'title'    => _m('Attachment Collections'), | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  |             'ordering' => $ordering++, | 
					
						
							|  |  |  |         ])); | 
					
						
							|  |  |  |         return Event::next; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-28 04:39:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-25 11:24:40 -03:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Append Attachment Collections widget to the right panel. | 
					
						
							|  |  |  |      * It's compose of two forms: one to select collections to add | 
					
						
							|  |  |  |      * the current attachment to, and another to create a new collection. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  |     public function onAppendRightPanelBlock($vars, Request $request, &$res): bool | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-27 15:30:32 -03:00
										 |  |  |         if ($vars['path'] !== 'note_attachment_show') { | 
					
						
							| 
									
										
										
										
											2021-12-25 17:31:16 +00:00
										 |  |  |             return Event::next; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  |         $user = Common::user(); | 
					
						
							| 
									
										
										
										
											2021-12-25 17:31:16 +00:00
										 |  |  |         if (\is_null($user)) { | 
					
						
							|  |  |  |             return Event::next; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-28 04:39:09 +00:00
										 |  |  |         $collections = DB::findBy(Collection::class, ['actor_id' => $user->getId()]); | 
					
						
							| 
									
										
										
										
											2021-12-25 17:31:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  |         // add to collection form
 | 
					
						
							|  |  |  |         $attachment_id = $vars['vars']['attachment_id']; | 
					
						
							| 
									
										
										
										
											2021-12-28 04:39:09 +00:00
										 |  |  |         $note_id       = $vars['vars']['note_id']; | 
					
						
							| 
									
										
										
										
											2021-12-25 17:31:16 +00:00
										 |  |  |         $choices       = []; | 
					
						
							| 
									
										
										
										
											2021-12-28 04:39:09 +00:00
										 |  |  |         foreach ($collections as $col) { | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  |             $choices[$col->getName()] = $col->getId(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $already_selected = DB::dql( | 
					
						
							| 
									
										
										
										
											2021-12-28 04:39:09 +00:00
										 |  |  |             <<<'EOF' | 
					
						
							|  |  |  |                 SELECT entry.collection_id FROM \Plugin\AttachmentCollections\Entity\AttachmentCollectionEntry AS entry | 
					
						
							|  |  |  |                 INNER JOIN \Component\Collection\Entity\Collection AS attachment_collection | 
					
						
							|  |  |  |                 WITH attachment_collection.id = entry.collection_id | 
					
						
							|  |  |  |                 WHERE entry.attachment_id = :attach_id AND entry.note_id = :note_id AND attachment_collection.actor_id = :id | 
					
						
							|  |  |  |                 EOF, | 
					
						
							|  |  |  |             ['attach_id' => $attachment_id, 'note_id' => $note_id, 'id' => $user->getId()], | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  |         ); | 
					
						
							| 
									
										
										
										
											2021-12-25 17:31:16 +00:00
										 |  |  |         $already_selected = array_map(fn ($x) => $x['collection_id'], $already_selected); | 
					
						
							|  |  |  |         $add_form         = Form::create([ | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  |             ['collections', ChoiceType::class, [ | 
					
						
							| 
									
										
										
										
											2021-12-25 17:31:16 +00:00
										 |  |  |                 'choices'     => $choices, | 
					
						
							|  |  |  |                 'multiple'    => true, | 
					
						
							|  |  |  |                 'required'    => false, | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  |                 'choice_attr' => function ($id) use ($already_selected) { | 
					
						
							|  |  |  |                     if (\in_array($id, $already_selected)) { | 
					
						
							|  |  |  |                         return ['selected' => 'selected']; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     return []; | 
					
						
							| 
									
										
										
										
											2021-12-25 17:31:16 +00:00
										 |  |  |                 }, | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  |             ]], | 
					
						
							|  |  |  |             ['add', SubmitType::class, [ | 
					
						
							|  |  |  |                 'label' => _m('Add to collections'), | 
					
						
							| 
									
										
										
										
											2021-12-25 17:31:16 +00:00
										 |  |  |                 'attr'  => [ | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  |                     'title' => _m('Add to collection'), | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |             ]], | 
					
						
							|  |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2021-12-25 11:24:40 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  |         $add_form->handleRequest($request); | 
					
						
							|  |  |  |         if ($add_form->isSubmitted() && $add_form->isValid()) { | 
					
						
							|  |  |  |             $collections = $add_form->getData()['collections']; | 
					
						
							| 
									
										
										
										
											2021-12-25 17:31:16 +00:00
										 |  |  |             $removed     = array_filter($already_selected, fn ($x) => !\in_array($x, $collections)); | 
					
						
							|  |  |  |             $added       = array_filter($collections, fn ($x) => !\in_array($x, $already_selected)); | 
					
						
							| 
									
										
										
										
											2021-12-28 04:39:09 +00:00
										 |  |  |             if (\count($removed) > 0) { | 
					
						
							|  |  |  |                 DB::dql(<<<'EOF' | 
					
						
							|  |  |  |                         DELETE FROM \Plugin\AttachmentCollections\Entity\AttachmentCollectionEntry AS entry | 
					
						
							|  |  |  |                         WHERE entry.attachment_id = :attach_id AND entry.note_id = :note_id | 
					
						
							|  |  |  |                         AND entry.collection_id IN ( | 
					
						
							|  |  |  |                             SELECT album.id FROM \Component\Collection\Entity\Collection AS album | 
					
						
							|  |  |  |                             WHERE album.actor_id = :user_id | 
					
						
							|  |  |  |                               AND album.id IN (:ids) | 
					
						
							|  |  |  |                         ) | 
					
						
							|  |  |  |                     EOF, ['attach_id' => $attachment_id, 'note_id' => $note_id, 'user_id' => $user->getId(), 'ids' => $removed]); | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  |             } | 
					
						
							|  |  |  |             foreach ($added as $cid) { | 
					
						
							| 
									
										
										
										
											2021-12-25 11:27:00 -03:00
										 |  |  |                 // prevent user from putting something in a collection (s)he doesn't own:
 | 
					
						
							| 
									
										
										
										
											2021-12-28 04:39:09 +00:00
										 |  |  |                 if (\in_array($cid, $collections)) { | 
					
						
							|  |  |  |                     DB::persist(AttachmentCollectionEntry::create([ | 
					
						
							| 
									
										
										
										
											2021-12-25 11:27:00 -03:00
										 |  |  |                         'attachment_id' => $attachment_id, | 
					
						
							| 
									
										
										
										
											2021-12-28 04:39:09 +00:00
										 |  |  |                         'note_id'       => $note_id, | 
					
						
							| 
									
										
										
										
											2021-12-25 11:27:00 -03:00
										 |  |  |                         'collection_id' => $cid, | 
					
						
							|  |  |  |                     ])); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  |             } | 
					
						
							|  |  |  |             DB::flush(); | 
					
						
							| 
									
										
										
										
											2021-12-27 15:25:20 -03:00
										 |  |  |             throw new RedirectException(); | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  |         } | 
					
						
							|  |  |  |         // add to new collection form
 | 
					
						
							|  |  |  |         $create_form = Form::create([ | 
					
						
							|  |  |  |             ['name', TextType::class, [ | 
					
						
							| 
									
										
										
										
											2021-12-25 17:31:16 +00:00
										 |  |  |                 'label' => _m('Add to a new collection'), | 
					
						
							|  |  |  |                 'attr'  => [ | 
					
						
							|  |  |  |                     'placeholder' => _m('New collection name'), | 
					
						
							|  |  |  |                     'required'    => 'required', | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  |                 ], | 
					
						
							|  |  |  |                 'data' => '', | 
					
						
							|  |  |  |             ]], | 
					
						
							|  |  |  |             ['create', SubmitType::class, [ | 
					
						
							|  |  |  |                 'label' => _m('Create a new collection'), | 
					
						
							| 
									
										
										
										
											2021-12-25 17:31:16 +00:00
										 |  |  |                 'attr'  => [ | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  |                     'title' => _m('Create a new collection'), | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |             ]], | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |         $create_form->handleRequest($request); | 
					
						
							|  |  |  |         if ($create_form->isSubmitted() && $create_form->isValid()) { | 
					
						
							|  |  |  |             $name = $create_form->getData()['name']; | 
					
						
							|  |  |  |             $coll = Collection::create([ | 
					
						
							| 
									
										
										
										
											2021-12-25 17:31:16 +00:00
										 |  |  |                 'name'     => $name, | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  |                 'actor_id' => $user->getId(), | 
					
						
							|  |  |  |             ]); | 
					
						
							|  |  |  |             DB::persist($coll); | 
					
						
							| 
									
										
										
										
											2021-12-28 04:39:09 +00:00
										 |  |  |             DB::persist(AttachmentCollectionEntry::create([ | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  |                 'attachment_id' => $attachment_id, | 
					
						
							| 
									
										
										
										
											2021-12-28 04:39:09 +00:00
										 |  |  |                 'note_id'       => $note_id, | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  |                 'collection_id' => $coll->getId(), | 
					
						
							|  |  |  |             ])); | 
					
						
							|  |  |  |             DB::flush(); | 
					
						
							| 
									
										
										
										
											2021-12-27 15:25:20 -03:00
										 |  |  |             throw new RedirectException(); | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $res[] = Formatting::twigRenderFile( | 
					
						
							|  |  |  |             'AttachmentCollections/widget.html.twig', | 
					
						
							|  |  |  |             [ | 
					
						
							| 
									
										
										
										
											2021-12-28 04:39:09 +00:00
										 |  |  |                 'has_collections' => $collections, | 
					
						
							| 
									
										
										
										
											2021-12-27 15:38:47 -03:00
										 |  |  |                 'add_form'        => $add_form->createView(), | 
					
						
							|  |  |  |                 'create_form'     => $create_form->createView(), | 
					
						
							| 
									
										
										
										
											2021-12-25 17:31:16 +00:00
										 |  |  |             ], | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  |         ); | 
					
						
							|  |  |  |         return Event::next; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-28 04:39:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-23 21:38:06 -03:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Output our dedicated stylesheet | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param array $styles stylesheets path | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool hook value; true means continue processing, false means stop | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function onEndShowStyles(array &$styles, string $route): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $styles[] = 'plugins/AttachmentCollections/assets/css/widget.css'; | 
					
						
							|  |  |  |         $styles[] = 'plugins/AttachmentCollections/assets/css/pages.css'; | 
					
						
							|  |  |  |         return Event::next; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |