2021-12-30 00:51:12 +00: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/>.
|
|
|
|
// }}}
|
|
|
|
/**
|
|
|
|
* Collections for GNU social
|
|
|
|
*
|
|
|
|
* @package GNUsocial
|
|
|
|
* @category Plugin
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2022-01-02 20:04:52 +00:00
|
|
|
namespace Component\Collection\Util;
|
2021-12-30 00:51:12 +00:00
|
|
|
|
2022-03-27 15:19:09 +01:00
|
|
|
use App\Core\DB;
|
2021-12-30 00:51:12 +00:00
|
|
|
use App\Core\Event;
|
|
|
|
use App\Core\Form;
|
|
|
|
use function App\Core\I18n\_m;
|
|
|
|
use App\Entity\Actor;
|
|
|
|
use App\Util\Common;
|
|
|
|
use App\Util\Exception\RedirectException;
|
|
|
|
use App\Util\Formatting;
|
2022-04-03 21:40:32 +01:00
|
|
|
use EventResult;
|
2021-12-30 00:51:12 +00:00
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
2022-10-19 22:39:17 +01:00
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* */
|
2022-01-04 21:58:49 +00:00
|
|
|
trait MetaCollectionTrait
|
2021-12-30 00:51:12 +00:00
|
|
|
{
|
2022-02-26 14:09:41 +00:00
|
|
|
//protected const SLUG = 'collection';
|
|
|
|
//protected const PLURAL_SLUG = 'collections';
|
2021-12-30 00:51:12 +00:00
|
|
|
|
2022-01-07 12:23:37 +00:00
|
|
|
/**
|
|
|
|
* create a collection owned by Actor $owner.
|
|
|
|
*
|
2022-10-19 22:39:17 +01:00
|
|
|
* @param Actor $owner The collection's owner
|
|
|
|
* @param array<string, mixed> $vars Page vars sent by AppendRightPanelBlock event
|
|
|
|
* @param string $name Collection's name
|
2022-01-07 12:23:37 +00:00
|
|
|
*/
|
2022-10-19 22:39:17 +01:00
|
|
|
abstract protected function createCollection(Actor $owner, array $vars, string $name): void;
|
2022-01-07 12:23:37 +00:00
|
|
|
/**
|
|
|
|
* remove item from collections.
|
|
|
|
*
|
2022-10-19 22:39:17 +01:00
|
|
|
* @param Actor $owner Current user
|
|
|
|
* @param array<string, mixed> $vars Page vars sent by AppendRightPanelBlock event
|
|
|
|
* @param int[] $items Array of collections's ids to remove the current item from
|
|
|
|
* @param int[] $collections List of ids of collections owned by $owner
|
2022-01-07 12:23:37 +00:00
|
|
|
*/
|
2022-10-19 22:39:17 +01:00
|
|
|
abstract protected function removeItem(Actor $owner, array $vars, array $items, array $collections): bool;
|
2022-01-07 12:23:37 +00:00
|
|
|
/**
|
|
|
|
* add item to collections.
|
|
|
|
*
|
2022-10-19 22:39:17 +01:00
|
|
|
* @param Actor $owner Current user
|
|
|
|
* @param array<string, mixed> $vars Page vars sent by AppendRightPanelBlock event
|
|
|
|
* @param int[] $items Array of collections's ids to add the current item to
|
|
|
|
* @param int[] $collections List of ids of collections owned by $owner
|
2022-01-07 12:23:37 +00:00
|
|
|
*/
|
2022-10-19 22:39:17 +01:00
|
|
|
abstract protected function addItem(Actor $owner, array $vars, array $items, array $collections): void;
|
2021-12-30 00:51:12 +00:00
|
|
|
|
2022-01-02 22:02:25 +00:00
|
|
|
/**
|
|
|
|
* Check the route to determine whether the widget should be added
|
2022-10-19 22:39:17 +01:00
|
|
|
* @param array<string, mixed> $vars
|
2022-01-02 22:02:25 +00:00
|
|
|
*/
|
2022-10-19 22:39:17 +01:00
|
|
|
abstract protected function shouldAddToRightPanel(Actor $user, array $vars, Request $request): bool;
|
2022-01-07 12:23:37 +00:00
|
|
|
/**
|
|
|
|
* Get array of collections's owned by $actor
|
|
|
|
*
|
2022-10-19 22:39:17 +01:00
|
|
|
* @param Actor $owner Collection's owner
|
|
|
|
* @param null|array<string, mixed> $vars Page vars sent by AppendRightPanelBlock event
|
|
|
|
* @param bool $ids_only if true, the function must return only the primary key or each collections
|
|
|
|
* @return T[]|int[]
|
2022-01-07 12:23:37 +00:00
|
|
|
*/
|
2021-12-30 00:51:12 +00:00
|
|
|
abstract protected function getCollectionsBy(Actor $owner, ?array $vars = null, bool $ids_only = false): array;
|
2022-01-02 22:02:25 +00:00
|
|
|
|
2021-12-30 00:51:12 +00:00
|
|
|
/**
|
|
|
|
* Append Collections widget to the right panel.
|
|
|
|
* It's compose of two forms: one to select collections to add
|
|
|
|
* the current item to, and another to create a new collection.
|
2022-10-19 22:39:17 +01:00
|
|
|
*
|
|
|
|
* @param array<string, mixed> $vars
|
|
|
|
* @param string[] $res
|
2021-12-30 00:51:12 +00:00
|
|
|
*/
|
2022-10-19 22:39:17 +01:00
|
|
|
public function onAppendRightPanelBlock(Request $request, array $vars, array &$res): EventResult
|
2021-12-30 00:51:12 +00:00
|
|
|
{
|
|
|
|
$user = Common::actor();
|
|
|
|
if (\is_null($user)) {
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
if (!$this->shouldAddToRightPanel($user, $vars, $request)) {
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
$collections = $this->getCollectionsBy($user);
|
|
|
|
|
|
|
|
// form: add to collection
|
|
|
|
$choices = [];
|
|
|
|
foreach ($collections as $col) {
|
|
|
|
$choices[$col->getName()] = $col->getId();
|
|
|
|
}
|
2021-12-30 15:16:29 +00:00
|
|
|
|
|
|
|
$collections = array_map(fn ($x) => $x->getId(), $collections);
|
|
|
|
|
2021-12-30 00:51:12 +00:00
|
|
|
$already_selected = $this->getCollectionsBy($user, $vars, true);
|
|
|
|
$add_form = Form::create([
|
|
|
|
['collections', ChoiceType::class, [
|
|
|
|
'choices' => $choices,
|
|
|
|
'multiple' => true,
|
|
|
|
'required' => false,
|
|
|
|
'choice_attr' => function ($id) use ($already_selected) {
|
|
|
|
if (\in_array($id, $already_selected)) {
|
|
|
|
return ['selected' => 'selected'];
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
},
|
|
|
|
]],
|
|
|
|
['add', SubmitType::class, [
|
2022-02-26 14:09:41 +00:00
|
|
|
'label' => _m('Add to ' . static::PLURAL_SLUG),
|
2021-12-30 00:51:12 +00:00
|
|
|
'attr' => [
|
2022-02-26 14:09:41 +00:00
|
|
|
'title' => _m('Add to ' . static::PLURAL_SLUG),
|
2021-12-30 00:51:12 +00:00
|
|
|
],
|
|
|
|
]],
|
|
|
|
]);
|
|
|
|
$add_form->handleRequest($request);
|
|
|
|
if ($add_form->isSubmitted() && $add_form->isValid()) {
|
2021-12-30 15:16:29 +00:00
|
|
|
$selected = $add_form->getData()['collections'];
|
|
|
|
$removed = array_filter($already_selected, fn ($x) => !\in_array($x, $selected));
|
2022-03-27 15:19:09 +01:00
|
|
|
$added = array_filter($selected, fn ($x) => !\in_array($x, $already_selected));
|
2021-12-30 00:51:12 +00:00
|
|
|
if (\count($removed) > 0) {
|
2022-01-07 12:23:37 +00:00
|
|
|
$this->removeItem($user, $vars, $removed, $collections);
|
2021-12-30 00:51:12 +00:00
|
|
|
}
|
|
|
|
if (\count($added) > 0) {
|
2022-01-07 12:23:37 +00:00
|
|
|
$this->addItem($user, $vars, $added, $collections);
|
2021-12-30 00:51:12 +00:00
|
|
|
}
|
|
|
|
DB::flush();
|
|
|
|
throw new RedirectException();
|
|
|
|
}
|
|
|
|
|
|
|
|
// form: add to new collection
|
|
|
|
$create_form = Form::create([
|
|
|
|
['name', TextType::class, [
|
2022-02-26 14:09:41 +00:00
|
|
|
'label' => _m('Add to a new ' . static::SLUG),
|
2021-12-30 00:51:12 +00:00
|
|
|
'attr' => [
|
2022-02-26 14:09:41 +00:00
|
|
|
'placeholder' => _m('New ' . static::SLUG . ' name'),
|
2021-12-30 00:51:12 +00:00
|
|
|
'required' => 'required',
|
|
|
|
],
|
|
|
|
'data' => '',
|
|
|
|
]],
|
|
|
|
['create', SubmitType::class, [
|
2022-02-26 14:09:41 +00:00
|
|
|
'label' => _m('Create a new ' . static::SLUG),
|
2021-12-30 00:51:12 +00:00
|
|
|
'attr' => [
|
2022-02-26 14:09:41 +00:00
|
|
|
'title' => _m('Create a new ' . static::SLUG),
|
2021-12-30 00:51:12 +00:00
|
|
|
],
|
|
|
|
]],
|
|
|
|
]);
|
|
|
|
$create_form->handleRequest($request);
|
|
|
|
if ($create_form->isSubmitted() && $create_form->isValid()) {
|
|
|
|
$name = $create_form->getData()['name'];
|
|
|
|
$this->createCollection($user, $vars, $name);
|
|
|
|
DB::flush();
|
|
|
|
throw new RedirectException();
|
|
|
|
}
|
|
|
|
|
|
|
|
$res[] = Formatting::twigRenderFile(
|
2022-01-02 20:04:52 +00:00
|
|
|
'collection/widget_add_to.html.twig',
|
2021-12-30 00:51:12 +00:00
|
|
|
[
|
2022-02-26 14:09:41 +00:00
|
|
|
'ctitle' => _m('Add to ' . static::PLURAL_SLUG),
|
2021-12-30 00:51:12 +00:00
|
|
|
'user' => $user,
|
|
|
|
'has_collections' => \count($collections) > 0,
|
|
|
|
'add_form' => $add_form->createView(),
|
|
|
|
'create_form' => $create_form->createView(),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
return Event::next;
|
|
|
|
}
|
2022-01-02 22:02:25 +00:00
|
|
|
|
2022-10-19 22:39:17 +01:00
|
|
|
/**
|
|
|
|
* @param string[]
|
|
|
|
*/
|
2022-04-03 21:40:32 +01:00
|
|
|
public function onEndShowStyles(array &$styles, string $route): EventResult
|
2021-12-30 00:51:12 +00:00
|
|
|
{
|
|
|
|
$styles[] = 'components/Collection/assets/css/widget.css';
|
|
|
|
$styles[] = 'components/Collection/assets/css/pages.css';
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
}
|