flagging unflagging cards

This commit is contained in:
2020-11-24 14:07:10 +01:00
parent ffc4a88094
commit a38653f7ce
14 changed files with 476 additions and 81 deletions

View File

@@ -6,8 +6,10 @@ use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Session\AccountProxy;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\flag_lists\FlagListsService;
use Drupal\flag_lists\Entity\FlaggingCollection;
use Drupal\flag_lists\Controller\ActionLinkController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
@@ -16,10 +18,15 @@ use Symfony\Component\HttpFoundation\JsonResponse;
class MaterioFlagController extends ControllerBase {
/*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* @var \Drupal\flag_lists\FlagListsService
*/
protected $flaglists;
protected $flagListsService;
/**
* @var \Drupal\user\User
@@ -31,6 +38,7 @@ class MaterioFlagController extends ControllerBase {
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager'),
$container->get('flaglists'),
$container->get('current_user')
);
@@ -39,9 +47,10 @@ class MaterioFlagController extends ControllerBase {
/**
* Constructs a new MaterioFlagController object.
*/
public function __construct(FlagListsService $flag_lists_service, AccountProxyInterface $account) {
$this->flaglists = $flag_lists_service;
$this->user = $account;
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;
}
/**
@@ -52,13 +61,35 @@ class MaterioFlagController extends ControllerBase {
*/
public function getUsersFlaggingCollections() {
$colls = $this->flaglists->getUsersFlaggingCollections();
$colls = $this->flagListsService->getUsersFlaggingCollections();
$data = [];
foreach ($colls as $id => $collection) {
$data[] = array(
"id" => $id,
"name" => $collection->getName()
$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[] = $item->getConnectedEntityId();
}
$items_uuids = [];
foreach ($items as $nid) {
$node = $this->entityTypeManager->getStorage('node')->load($nid);
$items_uuids[] = $node->uuid();
}
$data[$collection->id()] = array(
"id" => $collection->id(),
"name" => $collection->getName(),
"flag_id" => $flag_id,
"items" => $items,
"items_uuids" => $items_uuids,
);
}
@@ -91,9 +122,9 @@ class MaterioFlagController extends ControllerBase {
public function deleteUserFlaggingCollection(Request $request) {
// dpm($request);
$post_data = json_decode( $request->getContent(),TRUE);
$flagid = $post_data['flagid'];
$flagcollid = $post_data['flagcollid'];
$flagcoll = $this->flaglists->getFlaggingCollectionById($flagid);
$flagcoll = $this->flagListsService->getFlaggingCollectionById($flagcollid);
// dump($flagcoll);
$flagcoll->delete();
// TODO: warning, sometimes relatedFlag deos not exists
@@ -101,9 +132,36 @@ class MaterioFlagController extends ControllerBase {
$data = [
// 'result' => $flag,
'id' => $flagid
'id' => $flagcollid
];
return new JsonResponse($data);
}
// 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);
// }
}