View source
<?php
namespace Drupal\cmis;
use Drupal\cmis\CmisConnectionApi;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\HtmlCommand;
use Symfony\Component\HttpFoundation\Response;
use Drupal\cmis\CmisElement;
class CmisBrowser {
protected $config;
protected $connection;
protected $data;
protected $breadcrumbs = [];
protected $folder_id;
protected $current;
protected $popup;
protected $cacheable;
public function __construct($config = '', $folder_id = '') {
if (!empty($config)) {
$this
->init($config, $folder_id);
}
}
public function ajaxCall($config, $folder_id) {
$this
->init($config, $folder_id);
if ($this->connection && !empty($this->current) && ($browse = $this
->browse())) {
$response = new AjaxResponse();
$content = render($browse);
$response
->addCommand(new HtmlCommand('#cmis-browser-wrapper', $content));
return $response;
}
}
public function getDocument($config = '', $document_id = '') {
$this
->init($config, $document_id, 'cmis:document');
if ($this->connection && !empty($this->current) && $this->current
->getBaseTypeId()
->__toString() == 'cmis:document') {
$id = $this->current
->getId();
try {
$content = $this->current
->getContentStream($id);
} catch (CMISException $e) {
$headers = [
'' => 'HTTP/1.1 503 Service unavailable',
];
$response = new Response($content, 503, $headers);
$response
->send();
exit;
}
$mime = $this->current
->getContentStreamMimeType();
$headers = [
'Cache-Control' => 'no-cache, must-revalidate',
'Content-type' => $mime,
'Content-Disposition' => 'attachment; filename="' . $this->current
->getName() . '"',
];
$response = new Response($content, 200, $headers);
$response
->send();
print $content;
exit;
}
}
public function getDocumentProperties() {
if ($this->connection && !empty($this->current)) {
$type_id = $this->current
->getBaseTypeId()
->__toString();
$path = [];
if ($type_id == 'cmis:document') {
$url = \Drupal\Core\Url::fromUserInput('/cmis/document/' . $this->config . '/' . $this->current
->getId());
$path = \Drupal\Core\Link::fromTextAndUrl(t('Download'), $url)
->toRenderable();
}
return [
'#theme' => 'cmis_content_properties',
'#object' => $this->current,
'#download' => render($path),
];
}
}
private function init($config, $folder_id) {
$this->config = $config;
$this->folder_id = $folder_id;
$this->connection = new CmisConnectionApi($this->config);
if (!empty($this->connection
->getHttpInvoker())) {
$popup = \Drupal::request()->query
->get('type');
$this->popup = $popup == 'popup';
$this->connection
->setDefaultParameters();
if (empty($this->folder_id)) {
$root_folder = $this->connection
->getRootFolder();
$this->folder_id = $root_folder
->getId();
$this->current = $root_folder;
}
else {
$this->current = $this->connection
->getObjectById($this->folder_id);
}
}
}
public function getCurrent() {
return $this->current;
}
public function getConnection() {
return $this->connection;
}
private function setParameters() {
if ($this->connection) {
$this->connection
->setDefaultParameters();
}
}
public function browse($reset = FALSE) {
if ($this->connection && !empty($this->current)) {
$this
->setBreadcrumbs($this->current, 'last');
$this
->printFolderContent($this->current);
$table_header = array(
t('Name'),
t('Details'),
t('Author'),
t('Created'),
t('Description'),
t('Operation'),
);
$browse = [
'#theme' => 'cmis_browser',
'#header' => $table_header,
'#elements' => $this->data,
'#breadcrumbs' => $this->breadcrumbs,
'#operations' => $this
->prepareOperations(),
'#attached' => [
'library' => [
'cmis/cmis-browser',
],
],
];
return $browse;
}
return [];
}
private function prepareOperations() {
if (!\Drupal::currentUser()
->hasPermission('access cmis operations')) {
return '';
}
$routes = [
'/cmis/browser-create-folder/' => t('Create folder'),
'/cmis/browser-upload-document/' => t('Add document'),
];
$links = [];
foreach ($routes as $route => $title) {
$url = \Drupal\Core\Url::fromUserInput($route . $this->config . '/' . $this->current
->getId());
$link_options = array(
'attributes' => array(
'class' => [
'use-ajax',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => \Drupal\Component\Serialization\Json::encode([
'height' => 400,
'width' => 700,
]),
),
);
$url
->setOptions($link_options);
$path = \Drupal\Core\Link::fromTextAndUrl($title, $url)
->toRenderable();
$links[] = [
'#markup' => render($path),
'#wrapper_attributes' => [
'class' => [
'object-properties',
],
],
];
}
$list = [
'#theme' => 'item_list',
'#items' => $links,
'#type' => 'ul',
];
return render($list);
}
protected function printFolderContent(\Dkd\PhpCmis\Data\FolderInterface $folder) {
$root = $this->connection
->getRootFolder();
$element = new CmisElement($this->config, $this->popup, $this->current, '', $root
->getId());
foreach ($folder
->getChildren() as $children) {
$element
->setElement('browser', $children);
$this->data[] = $element
->getDAta();
}
}
protected function setBreadcrumbs($folder, $class = '') {
$name = $folder
->getName();
$id = $folder
->getId();
$this
->setBreadcrumb($name, $id, $class);
if ($parent = $folder
->getFolderParent()) {
$this
->setBreadcrumbs($parent);
}
else {
$this->breadcrumbs[0]['#wrapper_attributes']['class'] = [
'first',
];
}
}
protected function setBreadcrumb($label, $id = '', $class) {
$path = '/cmis/browser/nojs/' . $this->config;
if (!empty($id)) {
$path .= '/' . $id;
}
$url = \Drupal\Core\Url::fromUserInput($path);
$link_options = array(
'attributes' => array(
'class' => array(
'use-ajax',
),
),
);
if ($this->popup) {
$link_options['query'] = [
'type' => 'popup',
];
}
$url
->setOptions($link_options);
$item = [
'value' => \Drupal\Core\Link::fromTextAndUrl($label, $url)
->toRenderable(),
'#wrapper_attributes' => [
'class' => [
$class,
],
],
];
array_unshift($this->breadcrumbs, $item);
}
}