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

178 lines
5.0 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;
2020-11-24 14:07:10 +01:00
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\flag_lists\FlagListsService;
2020-11-19 17:09:48 +01:00
use Drupal\flag_lists\Entity\FlaggingCollection;
2020-11-24 14:07:10 +01:00
use Drupal\flag_lists\Controller\ActionLinkController;
2020-11-19 17:09:48 +01:00
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Class AjaxHomeController.
*/
class MaterioFlagController extends ControllerBase {
2020-11-24 14:07:10 +01:00
/*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* @var \Drupal\flag_lists\FlagListsService
*/
2020-11-24 14:07:10 +01:00
protected $flagListsService;
/**
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(
2020-11-24 14:07:10 +01:00
$container->get('entity_type.manager'),
$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.
*/
2020-11-24 14:07:10 +01:00
public function __construct(EntityTypeManagerInterface $entity_type_manager, FlagListsService $flag_lists_service, AccountProxyInterface $account) {
$this->entityTypeManager = $entity_type_manager;
$this->flagListsService = $flag_lists_service;
$this->user = $account;
2020-11-19 17:09:48 +01:00
}
/**
* Hello.
*
* @return string
* Return Hello string.
*/
public function getUsersFlaggingCollections() {
2020-11-24 14:07:10 +01:00
$colls = $this->flagListsService->getUsersFlaggingCollections();
$data = [];
foreach ($colls as $id => $collection) {
2020-11-24 14:07:10 +01:00
$flag_id = $collection->getRelatedFlag()->id();
// get the items
$itemsids = $this->flagListsService->getFlagListItemIds($flag_id,$id);
$items = [];
foreach ($this->flagListsService->getFlagListItems($itemsids) as $id => $item) {
// $items[] = array(
// 'id' => $id,
// 'name' => $item->getName(),
// 'connected_entity_id' => $item->getConnectedEntityId(),
// );
$items[] = (int)$item->getConnectedEntityId();
2020-11-24 14:07:10 +01:00
}
// $items_uuids = [];
// foreach ($items as $nid) {
// $node = $this->entityTypeManager->getStorage('node')->load($nid);
// $items_uuids[] = $node->uuid();
// }
2020-11-24 14:07:10 +01:00
$data[$collection->id()] = array(
"id" => $collection->id(),
"name" => $collection->getName(),
"flag_id" => $flag_id,
"items" => $items
// "items_uuids" => $items_uuids
);
}
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);
}
2020-11-19 19:02:04 +01:00
public function deleteUserFlaggingCollection(Request $request) {
// dpm($request);
$post_data = json_decode( $request->getContent(),TRUE);
2020-11-24 14:07:10 +01:00
$flagcollid = $post_data['flagcollid'];
2020-11-19 19:02:04 +01:00
2020-11-24 14:07:10 +01:00
$flagcoll = $this->flagListsService->getFlaggingCollectionById($flagcollid);
$relatedflag = $flagcoll->getRelatedFlag();
// flaglistitems are not deleted with the flagcoll
$itemsids = $this->flagListsService->getFlagListItemIds($relatedflag->id(),$flagcoll->id());
foreach ($this->flagListsService->getFlagListItems($itemsids) as $item) {
$item->delete();
}
2020-11-19 21:43:56 +01:00
// TODO: warning, sometimes relatedFlag deos not exists
// $flag = $flagcoll->getRelatedFlag();
2020-11-19 19:02:04 +01:00
// finaly delete the flag collection
$flagcoll->delete();
2020-11-19 19:02:04 +01:00
$data = [
2020-11-19 21:43:56 +01:00
// 'result' => $flag,
2020-11-24 14:07:10 +01:00
'id' => $flagcollid
2020-11-19 19:02:04 +01:00
];
return new JsonResponse($data);
}
2020-11-19 17:09:48 +01:00
2020-11-24 14:07:10 +01:00
// public function flag(Request $request) {
// // dpm($request);
// $post_data = json_decode( $request->getContent(),TRUE);
// $flagid = $post_data['flagid'];
// $nid = $post_data['nid'];
// $flagcollid = $post_data['flagcollid'];
//
// $actionLinkController = new ActionLinkController();
// // FlagInterface $flag, $entity_id, $flag_list
// $status = $actionLinkController->flag($flagid, $nid, $flagcollid);
//
// // $flagcoll = $this->flagListsService->getFlaggingCollectionById($flagid);
// // // dump($flagcoll);
// // $flagcoll->delete();
// // // TODO: warning, sometimes relatedFlag deos not exists
// // // $flag = $flagcoll->getRelatedFlag();
//
// $data = [
// 'status' => $flag,
// 'flagid' => $flagid,
// 'nid' => $nid,
// 'flagcollid' => $flagcollid
// ];
// return new JsonResponse($data);
// }
}