You are here

public function FlagController::getUserFlaggings in Heartbeat 8

Hello.

Return value

string Return Hello string.

1 string reference to 'FlagController::getUserFlaggings'
heartbeat.routing.yml in ./heartbeat.routing.yml
heartbeat.routing.yml

File

src/Controller/FlagController.php, line 54

Class

FlagController
Class FlagController.

Namespace

Drupal\heartbeat\Controller

Code

public function getUserFlaggings() {
  $entity_type = \Drupal::request()->request
    ->get('entity_type');
  $entity_id = \Drupal::request()->request
    ->get('entity_id');
  $flag_id = \Drupal::request()->request
    ->get('flag_id');
  $alt_flag_id = $flag_id === 'heartbeat_like' ? 'jihad_flag' : 'heartbeat_like';

  //      $flag_id === 'jihad_flag' ? 'heartbeat_like' : null;
  $uid = \Drupal::request()->request
    ->get('uid');
  $response = new Response();
  $altResponseData = null;
  if ($flag_id !== null) {
    $responseData = $this->entityQuery
      ->get("flagging")
      ->condition("flag_id", $flag_id, "=")
      ->condition("entity_type", $entity_type, "=")
      ->condition("entity_id", $entity_id)
      ->condition("uid", $uid, "=")
      ->execute() > 0;
    if (!$responseData) {
      $altResponseData = $this->entityQuery
        ->get("flagging")
        ->condition("flag_id", $alt_flag_id, "=")
        ->condition("entity_type", $entity_type, "=")
        ->condition("entity_id", $entity_id)
        ->condition("uid", $uid, "=")
        ->execute() > 0;
    }
  }
  else {
    $responseData = null;
    $altResponseData = null;
  }
  if (!$responseData && $altResponseData) {
    $altFlag = $this->flagService
      ->getFlagById($alt_flag_id);
    $entity = $this->entityTypeManager
      ->getStorage($entity_type)
      ->load($entity_id);
    $this->flagService
      ->unflag($altFlag, $entity, \Drupal::currentUser()
      ->getAccount());
  }
  $response
    ->setContent(json_encode(array(
    'flagged' => $responseData,
    'alt_flagged' => $altResponseData,
  )));
  $response->headers
    ->set('Content-Type', 'application/json');
  return $response;
}