You are here

class TFTController in Taxonomy File Tree 8

Same name and namespace in other branches
  1. 3.x src/Controller/TFTController.php \Drupal\tft\Controller\TFTController

Class TFTController.

Hierarchy

Expanded class hierarchy of TFTController

File

src/Controller/TFTController.php, line 26

Namespace

Drupal\tft\Controller
View source
class TFTController extends ControllerBase {

  /**
   * Format a folder or file link.
   *
   * @return array
   *   Render array with the formatted link
   */
  protected function link($item, $mime, $type = NULL) {
    if ($mime == 'folder') {
      return [
        'data' => [
          '#type' => 'link',
          '#title' => $item['name'],
          '#url' => Url::fromUri("internal:#term/{$item['id']}"),
          '#attributes' => [
            'class' => 'folder-folder-link',
            'id' => "tid-{$item['id']}",
          ],
        ],
      ];
    }
    elseif ($type && $type == 'record') {

      // Moxtra recording file.
      // Get the filefield icon.
      $icon_class = file_icon_class($mime);
      return [
        'data' => [
          '#type' => 'link',
          '#title' => $item['name'],
          '#url' => Url::fromUri("internal:/tft/download/file/{$item['id']}"),
          '#attributes' => [
            'class' => "file {$icon_class}",
            'target' => '_blank',
          ],
        ],
      ];
    }
    else {
      $media = Media::load($item['id']);
      $view_builder = \Drupal::entityTypeManager()
        ->getViewBuilder($media
        ->getEntityTypeId());
      return [
        'data' => [
          $view_builder
            ->view($media),
        ],
      ];
    }
  }

  /**
   * Return an <ul> with links for the current folder.
   *
   * @return array
   *   The render array
   */
  protected function operation_links($type, $id, $media = NULL, $gid = NULL) {
    $links = [];

    /** @var \Drupal\Core\TempStore\PrivateTempStore $tempstore */
    $tempstore = \Drupal::service('user.private_tempstore')
      ->get('tft');
    $query = 'destination=' . $tempstore
      ->get('q');
    switch ($type) {
      case 'folder':

        /** @var \Drupal\group\Entity\GroupInterface $group */
        $group = Group::load($gid);
        $user = $this
          ->currentUser();
        $edit = FALSE;

        // Hide edit link if the user has no access.
        if ($user
          ->hasPermission(TFT_ADD_TERMS) || $group && $group
          ->hasPermission(TFT_ADD_TERMS, $user)) {
          $edit = TRUE;
          $links[] = [
            '#type' => 'link',
            '#title' => $this
              ->t("edit"),
            '#url' => Url::fromUri("internal:/tft/term/edit/{$id}?" . $query),
            '#attributes' => [
              'class' => 'ops-link term-edit-link',
            ],
          ];
        }
        if ($user
          ->hasPermission(TFT_DELETE_TERMS) || $group && $group
          ->hasPermission(TFT_DELETE_TERMS, $user)) {
          if ($edit) {
            $links[] = [
              '#markup' => ' | ',
            ];
          }
          $links[] = [
            '#type' => 'link',
            '#title' => $this
              ->t("delete"),
            '#url' => Url::fromUri("internal:/tft/term/delete/{$id}?" . $query),
            '#attributes' => [
              'class' => 'ops-link term-edit-link',
            ],
          ];
        }
        break;
      case 'file':

        /** @var \Drupal\media\Entity\Media $media */
        if ($media
          ->access('update')) {
          $links[] = [
            '#type' => 'link',
            '#title' => $this
              ->t("edit"),
            '#url' => Url::fromUri("internal:/media/{$id}/edit?" . $query),
            '#attributes' => [
              'class' => 'ops-link node-edit-link',
            ],
          ];
          $links[] = [
            '#markup' => ' | ',
          ];
        }
        $links[] = [
          '#type' => 'link',
          '#title' => $this
            ->t("more info"),
          '#url' => Url::fromUri("internal:/media/{$id}"),
          '#attributes' => [
            'class' => 'ops-link',
          ],
        ];
        break;
    }
    return [
      'data' => $links,
    ];
  }

  /**
   * Returns folder content.
   *
   * @return array
   *   The folder content
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  protected function get_content($tid, $gid = NULL) {
    $content = [];
    $elements = _tft_folder_content($tid, FALSE, $gid);
    foreach ($elements as $element) {
      if ($element['type'] == 'term') {
        $content[] = [
          $this
            ->link($element, 'folder'),
          '',
          '',
          $this
            ->t("Folder"),
          $this
            ->operation_links('folder', $element['id'], NULL, $gid),
        ];
      }
      else {

        /** @var \Drupal\media\Entity\Media $media */
        $media = Media::load($element['id']);
        $user = $media
          ->getOwner();
        $fids = $media
          ->get('tft_file')
          ->getValue();
        if (!empty($fids)) {
          $fid = reset($fids)['target_id'];
          $file = File::load($fid);
          $file_name = $file
            ->getFilename();
          $file_name_parts = explode('.', $file_name);
          $file_extension = end($file_name_parts);
          $content[] = [
            $this
              ->link($element, $file
              ->getMimeType()),
            $user
              ->getDisplayName(),
            date('d/m/Y H:i', $media
              ->getChangedTime()),
            $this
              ->t('@type file', [
              '@type' => strtoupper($file_extension),
            ]),
            $this
              ->operation_links('file', $element['id'], $media, $gid),
          ];
        }
        elseif (!empty($link = $media
          ->get('opigno_moxtra_recording_link')
          ->getValue())) {
          $content[] = [
            $this
              ->link($element, 'video/mp4', 'record'),
            $user
              ->getDisplayName(),
            date('d/m/Y H:i', $media
              ->getChangedTime()),
            $this
              ->t('MP4 file'),
            $this
              ->operation_links('file', $element['id'], $media, $gid),
          ];
        }
      }
    }

    // Fix error in jquery.tablesorter if table is empty.
    if (empty($elements)) {
      $content[] = [
        '',
        '',
        '',
        '',
        '',
      ];
    }
    return $content;
  }

  /**
   * Render the add file and add folder links.
   */
  protected function add_content_links($tid = 0, $gid = NULL) {
    $items = [];
    $tempstore = \Drupal::service('user.private_tempstore')
      ->get('tft');
    $add_file_query = [
      'destination' => $tempstore
        ->get('q'),
    ];
    $add_term_query = [
      'destination' => $tempstore
        ->get('q'),
    ];

    // Do we have a tid ?
    if ($tid) {
      $add_file_query['tid'] = $tid;
      $add_term_query['parent'] = $tid;
      if (!$gid) {
        $gid = _tft_get_group_gid($tid);
      }
    }
    $group = Group::load($gid);
    $user = $this
      ->currentUser();

    // Can the user create files ?
    if ($user
      ->hasPermission('create media')) {

      // Can they add files in this context ?
      if ($user
        ->hasPermission(TFT_ADD_FILE) || $group && $group
        ->hasPermission(TFT_ADD_FILE, $user)) {
        $query = UrlHelper::buildQuery(array_reverse($add_file_query));
        $items[] = [
          '#wrapper_attributes' => [
            'class' => 'folder-add-content-link',
          ],
          '#type' => 'link',
          '#title' => $this
            ->t("Add a file"),
          '#url' => Url::fromUri("internal:/media/add/tft_file?{$query}"),
          '#attributes' => [
            'id' => 'add-child-file',
          ],
        ];
      }
    }

    // Can the user add terms anywhere, only under Group or never ?
    if ($user
      ->hasPermission(TFT_ADD_TERMS) || $group && $group
      ->hasPermission(TFT_ADD_TERMS, $user)) {
      $query = UrlHelper::buildQuery(array_reverse($add_term_query));
      $items[] = [
        '#wrapper_attributes' => [
          'class' => 'folder-add-content-link',
        ],
        '#type' => 'link',
        '#title' => $this
          ->t("Add a folder"),
        '#url' => Url::fromUri("internal:/tft/term/add?{$query}"),
        '#attributes' => [
          'id' => 'add-child-folder',
        ],
      ];
    }
    return [
      '#theme' => 'item_list',
      '#list_type' => 'ul',
      '#attributes' => [
        'id' => 'folder-add-content-links',
      ],
      '#items' => $items,
    ];
  }

  /**
   * Get the folder content in HTML table form.
   *
   * @return array
   *   The render array.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  protected function content_table($tid, $gid = NULL) {
    $headers = [
      [
        'id' => 'table-th-name',
        'data' => $this
          ->t('Name'),
      ],
      [
        'id' => 'table-th-loaded-by',
        'data' => $this
          ->t('Loaded by'),
      ],
      [
        'id' => 'table-th-date',
        'data' => $this
          ->t('Last modified'),
      ],
      [
        'id' => 'table-th-type',
        'data' => $this
          ->t('Type'),
      ],
      [
        'id' => 'table-th-ops',
        'data' => $this
          ->t('Operations'),
      ],
    ];
    return [
      [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'table-responsive',
          ],
        ],
        'table' => [
          '#type' => 'table',
          '#header' => $headers,
          '#rows' => $this
            ->get_content($tid, $gid),
        ],
      ],
      $this
        ->add_content_links($tid, $gid),
    ];
  }

  /**
   * Return an <ul> with links for the current folder.
   *
   * @return array
   *   The render array.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  protected function get_folder_operation_links($tid, $gid = NULL) {
    $items = [];

    // First link: got to parent.
    $parent_tid = _tft_get_parent_tid($tid);

    /** @var \Drupal\Core\TempStore\PrivateTempStore $tempstore */
    $tempstore = \Drupal::service('user.private_tempstore')
      ->get('tft');
    $root_tid = $tempstore
      ->get('root_tid');
    $query = 'destination=' . $tempstore
      ->get('q');
    $disabled = FALSE;
    if ($parent_tid > -1 && $tid != $root_tid) {
      if (!_tft_term_access($parent_tid)) {
        $disabled = TRUE;
      }
    }
    else {
      $disabled = TRUE;
    }
    $class = $disabled ? 'disabled' : '';
    $fragment = $disabled ? '#' : "#term/{$parent_tid}";
    $items[] = [
      '#wrapper_attributes' => [
        'id' => 'tft-back',
        'class' => 'folder-menu-ops-link first',
      ],
      '#type' => 'link',
      '#title' => t("parent folder"),
      '#url' => Url::fromUri("internal:{$fragment}"),
      '#attributes' => [
        'class' => $class,
        'id' => 'tft-back-link',
      ],
    ];

    // Third link: reorder child terms.
    $uri = "/tft/terms/reorder/{$tid}?{$query}";
    $group = Group::load($gid);
    $user = $this
      ->currentUser();
    if ($user
      ->hasPermission(TFT_REORDER_TERMS) || $group && $group
      ->hasPermission(TFT_REORDER_TERMS, $user)) {
      $items[] = [
        '#wrapper_attributes' => [
          'id' => 'manage-folders',
          'class' => 'folder-menu-ops-link',
        ],
        '#type' => 'link',
        '#title' => $this
          ->t("reorder elements"),
        '#url' => Url::fromUri('internal:' . $uri),
      ];
    }
    return [
      '#theme' => 'item_list',
      '#list_type' => 'ul',
      '#attributes' => [
        'class' => 'tabs primary',
        'id' => 'folder-menu-ops-links',
      ],
      '#items' => $items,
    ];
  }

  /**
   * File explorer.
   */
  protected function tft($tid = 'all', $gid = NULL) {
    if ($tid == 'all' || !(int) $tid) {
      if ($this
        ->currentUser()
        ->hasPermission(TFT_ACCESS_FULL_TREE)) {
        $tid = 0;
      }
      else {
        throw new AccessDeniedHttpException();
      }
    }

    // Check if the user has access to this tree.
    if (!_tft_term_access($tid)) {
      throw new AccessDeniedHttpException();
    }
    if ($tid) {
      $term = Term::load($tid);
      $name = $term
        ->getName();
    }
    else {
      $name = $this
        ->t("Root");
    }

    /** @var \Drupal\Core\TempStore\PrivateTempStore $tempstore */
    $tempstore = \Drupal::service('user.private_tempstore')
      ->get('tft');
    $base_path = base_path();
    $base_path = strlen($base_path) == '/' ? '' : substr($base_path, 0, -1);

    // Store the URL query. Need the current path for some AJAX callbacks.
    $tempstore
      ->set('q', $base_path . \Drupal::service('path.current')
      ->getPath());

    // Store the current term tid.
    $tempstore
      ->set('root_tid', $tid);
    $path = drupal_get_path('module', 'tft');
    return [
      // Get the themed title bar.
      [
        '#theme' => 'tft_folder_menu',
        '#name' => $name,
        '#path' => $path,
        '#ops_links' => $this
          ->get_folder_operation_links($tid, $gid),
      ],
      // Prepare the folder content area.
      [
        '#type' => 'container',
        '#attributes' => [
          'id' => 'folder-content-container',
        ],
        'content' => $this::content_table($tid, $gid),
      ],
      // Add CSS and Javascript files.
      '#attached' => [
        'library' => [
          'tft/tft',
        ],
        'drupalSettings' => [
          'tftDirectory' => $path,
        ],
      ],
    ];
  }

  /**
   * Downloads file.
   *
   * @param \Drupal\media\MediaInterface $media
   *   Media entity.
   *
   * @return \Drupal\Core\Routing\TrustedRedirectResponse|\Symfony\Component\HttpFoundation\BinaryFileResponse
   *   Response.
   */
  public function downloadFile(MediaInterface $media) {

    // Check if the user has access to group entity.
    if (!empty($media->tft_folder)) {
      $tid = $media
        ->get('tft_folder')
        ->getString();
      if (!empty($tid) && !_tft_term_access($tid)) {
        throw new AccessDeniedHttpException();
      }
    }
    $fids = $media
      ->get('tft_file')
      ->getValue();
    if (!empty($fids)) {
      $fid = reset($fids)['target_id'];
      $file = File::load($fid);
      if (!$file) {
        throw new NotFoundHttpException();
      }
      if (!($file
        ->access('view') && $file
        ->access('download'))) {
        throw new AccessDeniedHttpException();
      }

      // Let other modules provide headers and control access to the file.
      $headers = $this
        ->moduleHandler()
        ->invokeAll('file_download', [
        $file
          ->getFileUri(),
      ]);
      if (in_array(-1, $headers) || empty($headers)) {
        throw new AccessDeniedHttpException();
      }
      $file_name = $file
        ->getFilename();
      $headers = [
        'Content-Type' => $file
          ->getMimeType(),
        'Content-Disposition' => 'attachment; filename="' . $file_name . '"',
      ];
      if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
        $headers['Cache-Control'] = 'must-revalidate, post-check=0, pre-check=0';
        $headers['Pragma'] = 'public';
      }
      else {
        $headers['Pragma'] = 'no-cache';
      }
      return new BinaryFileResponse($file
        ->getFileUri(), 200, $headers);
    }
    elseif (!empty($link = $media
      ->get('opigno_moxtra_recording_link')
      ->getValue())) {
      if (\Drupal::hasService('opigno_moxtra.connector')) {

        /** @var \Drupal\opigno_moxtra\MoxtraConnector $opigno_api */
        $opigno_api = \Drupal::service('opigno_moxtra.connector');
        $token = $opigno_api
          ->getToken($media
          ->getOwnerId());
        $url = $link[0]['uri'] . "&access_token={$token}";
        return new TrustedRedirectResponse($url);
      }
    }
    throw new NotFoundHttpException();
  }

  /**
   * Returns directory list.
   *
   * @param \Drupal\taxonomy\TermInterface|null $taxonomy_term
   *   Term.
   *
   * @return array
   *   Render array.
   */
  public function listDirectory(TermInterface $taxonomy_term = NULL) {
    $tid = isset($taxonomy_term) ? $taxonomy_term
      ->id() : 'all';
    return $this
      ->tft($tid);
  }

  /**
   * Returns group list.
   *
   * @param \Drupal\group\Entity\GroupInterface $group
   *   Group.
   *
   * @return array
   *   Render array.
   */
  public function listGroup(GroupInterface $group) {
    $tid = _tft_get_group_tid($group
      ->id());
    if (!$tid) {
      return [
        '#markup' => $this
          ->t("No term was found for this group ! Please contact your system administrator."),
      ];
    }
    return $this
      ->tft($tid, $group
      ->id());
  }

  /**
   * Returns folder access flag.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   User account.
   *
   * @return \Drupal\Core\Access\AccessResult
   *   Access flag.
   */
  public function accessAjaxGetFolder(AccountInterface $account) {
    $tid = $_GET['tid'];
    $term = Term::load($tid);
    if (isset($term) && $term
      ->access('view', $account)) {
      return AccessResult::allowed();
    }
    return AccessResult::forbidden();
  }

  /**
   * Returns folder.
   *
   * @return \Symfony\Component\HttpFoundation\JsonResponse
   *   Response.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function ajaxGetFolder() {
    $tid = $_GET['tid'];
    $gid = _tft_get_group_gid($tid);
    $renderer = \Drupal::service('renderer');
    $data = $this
      ->content_table($tid, $gid);
    $ops_links = $this
      ->get_folder_operation_links($tid, $gid);
    return new JsonResponse([
      'data' => $renderer
        ->renderRoot($data),
      'parent' => _tft_get_parent_tid($tid, $gid),
      'ops_links' => $renderer
        ->renderRoot($ops_links),
    ]);
  }

}

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::$entityManager protected property The entity manager.
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::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 40
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
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. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator 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. 29
MessengerTrait::messenger public function Gets the messenger. 29
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.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
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.
TFTController::accessAjaxGetFolder public function Returns folder access flag.
TFTController::add_content_links protected function Render the add file and add folder links.
TFTController::ajaxGetFolder public function Returns folder.
TFTController::content_table protected function Get the folder content in HTML table form.
TFTController::downloadFile public function Downloads file.
TFTController::get_content protected function Returns folder content.
TFTController::get_folder_operation_links protected function Return an <ul> with links for the current folder.
TFTController::link protected function Format a folder or file link.
TFTController::listDirectory public function Returns directory list.
TFTController::listGroup public function Returns group list.
TFTController::operation_links protected function Return an <ul> with links for the current folder.
TFTController::tft protected function File explorer.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.