materio-d9/web/modules/custom/materio_flag/src/Controller/MaterioFlagController.php

93 lines
2.1 KiB
PHP
Raw Normal View History

<?php
namespace Drupal\materio_flag\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
2020-11-19 17:09:48 +01:00
use Drupal\Core\Session\AccountProxy;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\flag_lists\FlagListsService;
2020-11-19 17:09:48 +01:00
use Drupal\flag_lists\Entity\FlaggingCollection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Class AjaxHomeController.
*/
class MaterioFlagController extends ControllerBase {
/**
* @var \Drupal\flag_lists\FlagListsService
*/
protected $flaglists;
/**
2020-11-19 17:09:48 +01:00
* @var \Drupal\user\User
*/
2020-11-19 17:09:48 +01:00
protected $user;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('flaglists'),
2020-11-19 17:09:48 +01:00
$container->get('current_user')
);
}
2020-11-19 17:09:48 +01:00
/**
* Constructs a new MaterioFlagController object.
*/
public function __construct(FlagListsService $flag_lists_service, AccountProxyInterface $account) {
$this->flaglists = $flag_lists_service;
$this->user = $account;
}
/**
* Hello.
*
* @return string
* Return Hello string.
*/
public function getUsersFlaggingCollections() {
$colls = $this->flaglists->getUsersFlaggingCollections();
$data = [];
foreach ($colls as $id => $collection) {
$data[] = array(
"id" => $id,
"name" => $collection->getName()
);
}
return new JsonResponse($data);
}
2020-11-19 17:09:48 +01:00
public function createUserFlaggingCollection(Request $request) {
// dpm($request);
$post_data = json_decode( $request->getContent(),TRUE);
$name = $post_data['name'];
$newFlagColl = FlaggingCollection::Create([
'type' => 'flagging_collection_type',
'name' => $name,
'user_id' => $this->user->id(),
'templateflag' => 'dossier'
]);
$newFlagColl->save();
$data = [
'status' => $newFlagColl->get('status')->value,
'id' => $newFlagColl->id(),
'name' => $newFlagColl->getName(),
'newflagcoll_toarray' => $newFlagColl->toArray()
];
return new JsonResponse($data);
}
}