You are here

class SocialAlbumController in Open Social 10.2.x

Same name and namespace in other branches
  1. 10.3.x modules/social_features/social_album/src/Controller/SocialAlbumController.php \Drupal\social_album\Controller\SocialAlbumController
  2. 10.0.x modules/social_features/social_album/src/Controller/SocialAlbumController.php \Drupal\social_album\Controller\SocialAlbumController
  3. 10.1.x modules/social_features/social_album/src/Controller/SocialAlbumController.php \Drupal\social_album\Controller\SocialAlbumController

Returns responses for Album routes.

@package Drupal\social_album\Controller

Hierarchy

Expanded class hierarchy of SocialAlbumController

2 files declare their use of SocialAlbumController
SocialAlbumGroupAccess.php in modules/social_features/social_album/src/Plugin/views/access/SocialAlbumGroupAccess.php
SocialAlbumUserAccess.php in modules/social_features/social_album/src/Plugin/views/access/SocialAlbumUserAccess.php

File

modules/social_features/social_album/src/Controller/SocialAlbumController.php, line 24

Namespace

Drupal\social_album\Controller
View source
class SocialAlbumController extends ControllerBase {

  /**
   * The current active database's master connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * SocialAlbumController constructor.
   *
   * @param \Drupal\Core\StringTranslation\TranslationInterface $translation
   *   The string translation service.
   * @param \Drupal\Core\Database\Connection $database
   *   The current active database's master connection.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Entity\EntityFormBuilderInterface $entity_form_builder
   *   The entity form builder.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory service.
   */
  public function __construct(TranslationInterface $translation, Connection $database, EntityTypeManagerInterface $entity_type_manager, EntityFormBuilderInterface $entity_form_builder, AccountInterface $current_user, ConfigFactoryInterface $config_factory) {
    $this
      ->setStringTranslation($translation);
    $this->database = $database;
    $this->entityTypeManager = $entity_type_manager;
    $this->entityFormBuilder = $entity_form_builder;
    $this->currentUser = $current_user;
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('string_translation'), $container
      ->get('database'), $container
      ->get('entity_type.manager'), $container
      ->get('entity.form_builder'), $container
      ->get('current_user'), $container
      ->get('config.factory'));
  }

  /**
   * Provides a generic title callback for the first post of the album.
   *
   * @param \Drupal\node\NodeInterface $node
   *   The node object.
   *
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
   *   The title to page of the post.
   */
  public function title(NodeInterface $node) {
    return $this
      ->t('Add images to album @name', [
      '@name' => $node
        ->label(),
    ]);
  }

  /**
   * Provides a page with images slider.
   *
   * @param \Drupal\node\NodeInterface $node
   *   The node object.
   * @param \Drupal\social_post\Entity\PostInterface $post
   *   The post entity object.
   * @param int $fid
   *   The file entity ID.
   *
   * @return array
   *   The renderable array.
   */
  public function viewImage(NodeInterface $node, PostInterface $post, $fid) {
    $query = $this->database
      ->select('post__field_post_image', 'i')
      ->fields('i', [
      'field_post_image_target_id',
    ]);
    $query
      ->innerJoin('post__field_album', 'a', 'a.entity_id = i.entity_id');
    $query
      ->condition('a.field_album_target_id', $node
      ->id());
    $query
      ->innerJoin('post_field_data', 'p', 'p.id = a.entity_id');
    $query
      ->fields('p', [
      'id',
    ]);
    $query
      ->orderBy('p.created');
    $query
      ->orderBy('i.delta');
    $items = [
      FALSE => [],
      TRUE => [],
    ];
    $found = FALSE;

    /** @var \Drupal\file\FileStorageInterface $storage */
    $storage = $this
      ->entityTypeManager()
      ->getStorage('file');
    foreach ($query
      ->execute()
      ->fetchAllKeyed() as $file_id => $post_id) {
      if (!$found && $file_id == $fid) {
        $found = TRUE;
      }

      /** @var \Drupal\file\FileInterface $file */
      $file = $storage
        ->load($file_id);
      $items[$found][] = [
        'url' => Url::fromUri(file_create_url($file
          ->getFileUri()))
          ->setAbsolute()
          ->toString(),
        'pid' => $post_id,
      ];
    }
    return [
      '#theme' => 'social_album_post',
      '#items' => array_merge($items[TRUE], $items[FALSE]),
      '#album' => $node
        ->label(),
    ];
  }

  /**
   * Provides a page with a form for deleting image from post and post view.
   *
   * @param \Drupal\node\NodeInterface $node
   *   The node object.
   * @param \Drupal\social_post\Entity\PostInterface $post
   *   The post entity object.
   * @param int $fid
   *   The file entity ID.
   *
   * @return array
   *   The renderable array.
   */
  public function deleteImage(NodeInterface $node, PostInterface $post, $fid) {
    return [
      'form' => $this
        ->entityFormBuilder()
        ->getForm($post, 'delete_image', [
        'fid' => $fid,
      ]),
      'view' => $this
        ->entityTypeManager()
        ->getViewBuilder('post')
        ->view($post, 'featured'),
    ];
  }

  /**
   * Set the current group as the default value of the group field.
   *
   * @param \Drupal\group\Entity\GroupInterface $group
   *   The group object.
   *
   * @return array
   *   The renderable array.
   */
  public function add(GroupInterface $group) {
    $node = $this
      ->entityTypeManager()
      ->getStorage('node')
      ->create([
      'type' => 'album',
      'groups' => $group,
    ]);
    return $this
      ->entityFormBuilder()
      ->getForm($node);
  }

  /**
   * Checks access to the form of a post which will be linked to the album.
   *
   * @param \Drupal\node\NodeInterface $node
   *   The node object.
   *
   * @return bool
   *   TRUE if it's an album node.
   */
  protected function checkAlbumAccess(NodeInterface $node) {
    return $node
      ->bundle() === 'album';
  }

  /**
   * Checks access to the page for adding an image to an album.
   *
   * @param \Drupal\node\NodeInterface $node
   *   The node entity object.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public function checkAddImageAccess(NodeInterface $node) {
    if ($this
      ->checkAlbumAccess($node)) {
      $account = $this
        ->currentUser();
      if ($node
        ->getOwnerId() === $account
        ->id()) {
        return AccessResult::allowed();
      }
      elseif (!$node->field_album_creators
        ->isEmpty() && $node->field_album_creators->value) {

        /** @var \Drupal\group\Entity\Storage\GroupContentStorageInterface $storage */
        $storage = $this
          ->entityTypeManager()
          ->getStorage('group_content');
        if ($group_content = $storage
          ->loadByEntity($node)) {

          /** @var \Drupal\group\Entity\GroupInterface $group */
          $group = reset($group_content)
            ->getGroup();
          return AccessResult::allowedIf($group
            ->getMember($account) !== FALSE);
        }
      }
    }
    return AccessResult::forbidden();
  }

  /**
   * Checks access to the page for viewing the image from the post.
   *
   * @param \Drupal\node\NodeInterface $node
   *   The node entity object.
   * @param \Drupal\social_post\Entity\PostInterface $post
   *   The post entity object.
   * @param int $fid
   *   The file entity ID.
   * @param string $operation
   *   (optional) The operation to be performed. Defaults to view.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public function checkViewImageAccess(NodeInterface $node, PostInterface $post, $fid, $operation = 'view') {
    if ($this
      ->checkAlbumAccess($node) && $post
      ->access($operation) && $post
      ->bundle() === 'photo' && !$post->field_album
      ->isEmpty() && $post->field_album->target_id === $node
      ->id() && !$post->field_post_image
      ->isEmpty()) {
      foreach ($post->field_post_image
        ->getValue() as $item) {
        if ($item['target_id'] === $fid) {
          return AccessResult::allowed();
        }
      }
    }
    return AccessResult::forbidden();
  }

  /**
   * Checks access to the page for deleting the image from the post.
   *
   * @param \Drupal\node\NodeInterface $node
   *   The node entity object.
   * @param \Drupal\social_post\Entity\PostInterface $post
   *   The post entity object.
   * @param int $fid
   *   The file entity ID.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public function checkDeleteImageAccess(NodeInterface $node, PostInterface $post, $fid) {
    $access = $this
      ->checkViewImageAccess($node, $post, $fid, 'delete');
    if ($access
      ->isAllowed()) {
      $access = $access
        ->andIf(AccessResult::allowedIf($post
        ->getOwnerId() === $this
        ->currentUser()
        ->id()));
    }
    return $access;
  }

  /**
   * Checks access to the user albums page.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public function checkUserAlbumsAccess() {
    $status = $this
      ->config('social_album.settings')
      ->get('status');
    return AccessResult::allowedIf(!empty($status));
  }

  /**
   * Checks access to create an album in a group.
   *
   * @param \Drupal\group\Entity\GroupInterface $group
   *   The group object.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  protected function checkGroupAccess(GroupInterface $group) {
    $is_allow = $group
      ->getGroupType()
      ->hasContentPlugin('group_node:album');
    return AccessResult::allowedIf($is_allow);
  }

  /**
   * Checks access to the group albums page.
   *
   * @param \Drupal\group\Entity\GroupInterface $group
   *   The group object.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public function checkGroupAlbumsAccess(GroupInterface $group) {
    $access = $this
      ->checkUserAlbumsAccess();
    return $access
      ->isForbidden() ? $access : $this
      ->checkGroupAccess($group);
  }

  /**
   * Checks access to the group album creation page.
   *
   * @param \Drupal\group\Entity\GroupInterface $group
   *   The group object.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public function checkGroupAlbumAccess(GroupInterface $group) {
    $access = $this
      ->checkGroupAccess($group);
    if ($access
      ->isAllowed()) {
      return $group
        ->getGroupType()
        ->getContentPlugin('group_node:album')
        ->createEntityAccess($group, $this
        ->currentUser())
        ->andIf($this
        ->checkUserAlbumsAccess());
    }
    return $access;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route.
ControllerBase::state protected function Returns the state storage service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
SocialAlbumController::$database protected property The current active database's master connection.
SocialAlbumController::add public function Set the current group as the default value of the group field.
SocialAlbumController::checkAddImageAccess public function Checks access to the page for adding an image to an album.
SocialAlbumController::checkAlbumAccess protected function Checks access to the form of a post which will be linked to the album.
SocialAlbumController::checkDeleteImageAccess public function Checks access to the page for deleting the image from the post.
SocialAlbumController::checkGroupAccess protected function Checks access to create an album in a group.
SocialAlbumController::checkGroupAlbumAccess public function Checks access to the group album creation page.
SocialAlbumController::checkGroupAlbumsAccess public function Checks access to the group albums page.
SocialAlbumController::checkUserAlbumsAccess public function Checks access to the user albums page.
SocialAlbumController::checkViewImageAccess public function Checks access to the page for viewing the image from the post.
SocialAlbumController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
SocialAlbumController::deleteImage public function Provides a page with a form for deleting image from post and post view.
SocialAlbumController::title public function Provides a generic title callback for the first post of the album.
SocialAlbumController::viewImage public function Provides a page with images slider.
SocialAlbumController::__construct public function SocialAlbumController constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.