View source
<?php
namespace Drupal\tft\Controller;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\file\Entity\File;
use Drupal\group\Entity\Group;
use Drupal\group\Entity\GroupInterface;
use Drupal\media\Entity\Media;
use Drupal\media\MediaInterface;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\TermInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class TFTController extends ControllerBase {
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') {
$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),
],
];
}
}
protected function operation_links($type, $id, $media = NULL, $gid = NULL) {
$links = [];
$tempstore = \Drupal::service('user.private_tempstore')
->get('tft');
$query = 'destination=' . $tempstore
->get('q');
switch ($type) {
case 'folder':
$group = Group::load($gid);
$user = $this
->currentUser();
$edit = FALSE;
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':
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,
];
}
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 {
$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),
];
}
}
}
if (empty($elements)) {
$content[] = [
'',
'',
'',
'',
'',
];
}
return $content;
}
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'),
];
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();
if ($user
->hasPermission('create media')) {
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',
],
];
}
}
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,
];
}
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),
];
}
protected function get_folder_operation_links($tid, $gid = NULL) {
$items = [];
$parent_tid = _tft_get_parent_tid($tid);
$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',
],
];
$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,
];
}
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();
}
}
if (!_tft_term_access($tid)) {
throw new AccessDeniedHttpException();
}
if ($tid) {
$term = Term::load($tid);
$name = $term
->getName();
}
else {
$name = $this
->t("Root");
}
$tempstore = \Drupal::service('user.private_tempstore')
->get('tft');
$base_path = base_path();
$base_path = strlen($base_path) == '/' ? '' : substr($base_path, 0, -1);
$tempstore
->set('q', $base_path . \Drupal::service('path.current')
->getPath());
$tempstore
->set('root_tid', $tid);
$path = drupal_get_path('module', 'tft');
return [
[
'#theme' => 'tft_folder_menu',
'#name' => $name,
'#path' => $path,
'#ops_links' => $this
->get_folder_operation_links($tid, $gid),
],
[
'#type' => 'container',
'#attributes' => [
'id' => 'folder-content-container',
],
'content' => $this::content_table($tid, $gid),
],
'#attached' => [
'library' => [
'tft/tft',
],
'drupalSettings' => [
'tftDirectory' => $path,
],
],
];
}
public function downloadFile(MediaInterface $media) {
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();
}
$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')) {
$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();
}
public function listDirectory(TermInterface $taxonomy_term = NULL) {
$tid = isset($taxonomy_term) ? $taxonomy_term
->id() : 'all';
return $this
->tft($tid);
}
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());
}
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();
}
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),
]);
}
}