MaterioFlagController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace Drupal\materio_flag\Controller;
  3. use Drupal\Core\Controller\ControllerBase;
  4. use Symfony\Component\DependencyInjection\ContainerInterface;
  5. use Drupal\Core\Session\AccountProxy;
  6. use Drupal\Core\Session\AccountProxyInterface;
  7. use Drupal\Core\Entity\EntityTypeManagerInterface;
  8. use Drupal\flag_lists\FlagListsService;
  9. use Drupal\flag_lists\Entity\FlaggingCollection;
  10. use Drupal\flag_lists\Controller\ActionLinkController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. /**
  14. * Class AjaxHomeController.
  15. */
  16. class MaterioFlagController extends ControllerBase {
  17. /*
  18. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  19. */
  20. protected $entityTypeManager;
  21. /**
  22. * @var \Drupal\flag_lists\FlagListsService
  23. */
  24. protected $flagListsService;
  25. /**
  26. * @var \Drupal\user\User
  27. */
  28. protected $user;
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public static function create(ContainerInterface $container) {
  33. return new static(
  34. $container->get('entity_type.manager'),
  35. $container->get('flaglists'),
  36. $container->get('current_user')
  37. );
  38. }
  39. /**
  40. * Constructs a new MaterioFlagController object.
  41. */
  42. public function __construct(EntityTypeManagerInterface $entity_type_manager, FlagListsService $flag_lists_service, AccountProxyInterface $account) {
  43. $this->entityTypeManager = $entity_type_manager;
  44. $this->flagListsService = $flag_lists_service;
  45. $this->user = $account;
  46. }
  47. /**
  48. * Hello.
  49. *
  50. * @return string
  51. * Return Hello string.
  52. */
  53. public function getUsersFlaggingCollections() {
  54. $colls = $this->flagListsService->getUsersFlaggingCollections();
  55. $data = [];
  56. foreach ($colls as $id => $collection) {
  57. $flag_id = $collection->getRelatedFlag()->id();
  58. // get the items
  59. $itemsids = $this->flagListsService->getFlagListItemIds($flag_id,$id);
  60. $items = [];
  61. foreach ($this->flagListsService->getFlagListItems($itemsids) as $id => $item) {
  62. // $items[] = array(
  63. // 'id' => $id,
  64. // 'name' => $item->getName(),
  65. // 'connected_entity_id' => $item->getConnectedEntityId(),
  66. // );
  67. $items[] = (int)$item->getConnectedEntityId();
  68. }
  69. // $items_uuids = [];
  70. // foreach ($items as $nid) {
  71. // $node = $this->entityTypeManager->getStorage('node')->load($nid);
  72. // $items_uuids[] = $node->uuid();
  73. // }
  74. $data[$collection->id()] = array(
  75. "id" => $collection->id(),
  76. "name" => $collection->getName(),
  77. "flag_id" => $flag_id,
  78. "items" => $items
  79. // "items_uuids" => $items_uuids
  80. );
  81. }
  82. return new JsonResponse($data);
  83. }
  84. public function createUserFlaggingCollection(Request $request) {
  85. // dpm($request);
  86. $post_data = json_decode( $request->getContent(),TRUE);
  87. $name = $post_data['name'];
  88. $newFlagColl = FlaggingCollection::Create([
  89. 'type' => 'flagging_collection_type',
  90. 'name' => $name,
  91. 'user_id' => $this->user->id(),
  92. 'templateflag' => 'dossier'
  93. ]);
  94. $newFlagColl->save();
  95. $data = [
  96. 'status' => $newFlagColl->get('status')->value,
  97. 'id' => $newFlagColl->id(),
  98. 'name' => $newFlagColl->getName(),
  99. 'newflagcoll_toarray' => $newFlagColl->toArray()
  100. ];
  101. return new JsonResponse($data);
  102. }
  103. public function deleteUserFlaggingCollection(Request $request) {
  104. // dpm($request);
  105. $post_data = json_decode( $request->getContent(),TRUE);
  106. $flagcollid = $post_data['flagcollid'];
  107. $flagcoll = $this->flagListsService->getFlaggingCollectionById($flagcollid);
  108. $relatedflag = $flagcoll->getRelatedFlag();
  109. // flaglistitems are not deleted with the flagcoll
  110. $itemsids = $this->flagListsService->getFlagListItemIds($relatedflag->id(),$flagcoll->id());
  111. foreach ($this->flagListsService->getFlagListItems($itemsids) as $item) {
  112. $item->delete();
  113. }
  114. // TODO: warning, sometimes relatedFlag deos not exists
  115. // $flag = $flagcoll->getRelatedFlag();
  116. // finaly delete the flag collection
  117. $flagcoll->delete();
  118. $data = [
  119. // 'result' => $flag,
  120. 'id' => $flagcollid
  121. ];
  122. return new JsonResponse($data);
  123. }
  124. // public function flag(Request $request) {
  125. // // dpm($request);
  126. // $post_data = json_decode( $request->getContent(),TRUE);
  127. // $flagid = $post_data['flagid'];
  128. // $nid = $post_data['nid'];
  129. // $flagcollid = $post_data['flagcollid'];
  130. //
  131. // $actionLinkController = new ActionLinkController();
  132. // // FlagInterface $flag, $entity_id, $flag_list
  133. // $status = $actionLinkController->flag($flagid, $nid, $flagcollid);
  134. //
  135. // // $flagcoll = $this->flagListsService->getFlaggingCollectionById($flagid);
  136. // // // dump($flagcoll);
  137. // // $flagcoll->delete();
  138. // // // TODO: warning, sometimes relatedFlag deos not exists
  139. // // // $flag = $flagcoll->getRelatedFlag();
  140. //
  141. // $data = [
  142. // 'status' => $flag,
  143. // 'flagid' => $flagid,
  144. // 'nid' => $nid,
  145. // 'flagcollid' => $flagcollid
  146. // ];
  147. // return new JsonResponse($data);
  148. // }
  149. }