123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- namespace Drupal\materio_flag\Controller;
- use Drupal\Core\Controller\ControllerBase;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use Drupal\Core\Session\AccountProxy;
- use Drupal\Core\Session\AccountProxyInterface;
- use Drupal\flag_lists\FlagListsService;
- 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;
- /**
- * @var \Drupal\user\User
- */
- protected $user;
- /**
- * {@inheritdoc}
- */
- public static function create(ContainerInterface $container) {
- return new static(
- $container->get('flaglists'),
- $container->get('current_user')
- );
- }
- /**
- * 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);
- }
- 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);
- }
- public function deleteUserFlaggingCollection(Request $request) {
- // dpm($request);
- $post_data = json_decode( $request->getContent(),TRUE);
- $flagid = $post_data['flagid'];
- $flagcoll = $this->flaglists->getFlaggingCollectionById($flagid);
- // dump($flagcoll);
- $flagcoll->delete();
- // TODO: warning, sometimes relatedFlag deos not exists
- // $flag = $flagcoll->getRelatedFlag();
- $data = [
- // 'result' => $flag,
- 'id' => $flagid
- ];
- return new JsonResponse($data);
- }
- }
|