MaterioFlagController.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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\flag_lists\FlagListsService;
  8. use Drupal\flag_lists\Entity\FlaggingCollection;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. /**
  12. * Class AjaxHomeController.
  13. */
  14. class MaterioFlagController extends ControllerBase {
  15. /**
  16. * @var \Drupal\flag_lists\FlagListsService
  17. */
  18. protected $flaglists;
  19. /**
  20. * @var \Drupal\user\User
  21. */
  22. protected $user;
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function create(ContainerInterface $container) {
  27. return new static(
  28. $container->get('flaglists'),
  29. $container->get('current_user')
  30. );
  31. }
  32. /**
  33. * Constructs a new MaterioFlagController object.
  34. */
  35. public function __construct(FlagListsService $flag_lists_service, AccountProxyInterface $account) {
  36. $this->flaglists = $flag_lists_service;
  37. $this->user = $account;
  38. }
  39. /**
  40. * Hello.
  41. *
  42. * @return string
  43. * Return Hello string.
  44. */
  45. public function getUsersFlaggingCollections() {
  46. $colls = $this->flaglists->getUsersFlaggingCollections();
  47. $data = [];
  48. foreach ($colls as $id => $collection) {
  49. $data[] = array(
  50. "id" => $id,
  51. "name" => $collection->getName()
  52. );
  53. }
  54. return new JsonResponse($data);
  55. }
  56. public function createUserFlaggingCollection(Request $request) {
  57. // dpm($request);
  58. $post_data = json_decode( $request->getContent(),TRUE);
  59. $name = $post_data['name'];
  60. $newFlagColl = FlaggingCollection::Create([
  61. 'type' => 'flagging_collection_type',
  62. 'name' => $name,
  63. 'user_id' => $this->user->id(),
  64. 'templateflag' => 'dossier'
  65. ]);
  66. $newFlagColl->save();
  67. $data = [
  68. 'status' => $newFlagColl->get('status')->value,
  69. 'id' => $newFlagColl->id(),
  70. 'name' => $newFlagColl->getName(),
  71. 'newflagcoll_toarray' => $newFlagColl->toArray()
  72. ];
  73. return new JsonResponse($data);
  74. }
  75. public function deleteUserFlaggingCollection(Request $request) {
  76. // dpm($request);
  77. $post_data = json_decode( $request->getContent(),TRUE);
  78. $flagid = $post_data['flagid'];
  79. $flagcoll = $this->flaglists->getFlaggingCollectionById($flagid);
  80. $result = $flagcoll->delete();
  81. $data = [
  82. 'result' => $result,
  83. 'id' => $flagid
  84. ];
  85. return new JsonResponse($data);
  86. }
  87. }