Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRPLDCX-99 Collections #41

Merged
merged 7 commits into from
Oct 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions modules/dcx_collections/dcx_collections.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: DC-X Collections
type: module
description: Integrates DC-X collections to entity browser
core: 8.x
package: Custom
dependencies:
- dcx_migration
4 changes: 4 additions & 0 deletions modules/dcx_collections/dcx_collections.libraries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dcx_collections:
version: 0
js:
js/dcx-collections.js: {}
41 changes: 41 additions & 0 deletions modules/dcx_collections/dcx_collections.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

function dcx_collections_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

if (!empty($form['widget']['dcx_dropzone_widget'])) {
$form['#attached']['library'][] = 'dcx_collections/dcx_collections';
$form['widget']['collections'] = [
'#weight' => -1,
'#type' => 'details',
'#open' => true,
'#title' => Drupal::translation()->translate('Collections'),
];
$collections = \Drupal::service('dcx_integration.client')->getCollections();

$toplevel = array_filter($collections, function ($c) {return $c['parent'] == NULL; });
foreach ($toplevel as $collection) {
_dcx_collections_build_detail($collection, $form['widget']['collections'], $collections);
}
}
}

function _dcx_collections_build_detail($collection, &$detail, $collections) {
$this_detail = [
'#type' => 'details',
'#open' => false,
'#title' => $collection['label'],
'#attributes' => [
'class' => ['dcx-collection'],
'data-id' => $collection['id']
],
];

foreach ($collection['children'] as $id) {
_dcx_collections_build_detail($collections[$id], $this_detail, $collections);
}

// Inject placeholder for image previed loaded by AJAX.
$this_detail['images']['#markup'] = '<span id="dcx-preview-' . $collection['id'] . '"></span>';

$detail[$collection['id']] = $this_detail;
}
15 changes: 15 additions & 0 deletions modules/dcx_collections/dcx_collections.routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
dcx_collections.imagePreview:
path: 'dcx/collection/preview-image/{id}'
defaults:
_controller: '\Drupal\dcx_collections\Controller\Collection::imagePreview'
_title: 'Preview'
requirements:
_permission: 'import from dcx'

dcx_collections.docsOfCollection:
path: 'dcx/collection/{id}'
defaults:
_controller: '\Drupal\dcx_collections\Controller\Collection::docsOfCollection'
_title: 'Doc of collection'
requirements:
_permission: 'import from dcx'
55 changes: 55 additions & 0 deletions modules/dcx_collections/js/dcx-collections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @file
*/

(function ($, Drupal, drupalSettings) {
'use strict';

Drupal.behaviors.dcxCollections = {
attach: function (context, settings) {
var baseUrl = drupalSettings.path.baseUrl == "/"?'':drupalSettings.path.baseUrl;
var collections = $('details.dcx-collection');

$('summary', collections).once('dcx-collection').click(function() {
var summary = $(this);

if (summary.hasClass('processed')) return;
summary.addClass('processed');

var id = summary.parent().attr('data-id');
var previewArea = summary.parent().find('#dcx-preview-' + id);

$.ajax({
'type': 'GET',
'url': baseUrl + '/dcx/collection/' + id,
'previewArea': previewArea,
'success': function(data) {
var previewArea = this.previewArea;
$.each(data, function(i,d) {
var imagePreviewWrapper = $("<span></span>").attr('data-id', d);
previewArea.append(imagePreviewWrapper);
setupPreview(imagePreviewWrapper);
});
}
});
});
}
}

function setupPreview(wrapper) {
var id = wrapper.attr('data-id').replace(/dcxapi:document\//, '');
var baseUrl = drupalSettings.path.baseUrl == "/"?'':drupalSettings.path.baseUrl;
$.ajax({
'type': 'GET',
'url': baseUrl + '/dcx/collection/preview-image/' + id,
'wrapper': wrapper,
'success': function(data) {
this.wrapper.html($('<img>').attr('src', data.url));
this.wrapper.append($('<div>').html(data.filename));
this.wrapper.on('dragstart', function(ev) {
ev.originalEvent.dataTransfer.setData("text/plain", data.id);
});
}
});
}
})(jQuery, Drupal, drupalSettings);
37 changes: 37 additions & 0 deletions modules/dcx_collections/src/Controller/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Drupal\dcx_collections\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\dcx_integration\ClientInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;

class Collection extends ControllerBase {

protected $dcxClient;

public function __construct(ClientInterface $dcxClient) {
$this->dcxClient = $dcxClient;
}

public static function create(ContainerInterface $container) {
return new static(
$container->get('dcx_integration.client')
);
}

public function docsOfCollection($id) {
$doc_ids = $this->dcxClient->getDocsOfCollection($id);
$raw_ids = array_map(function($d) { return preg_replace('#dcxapi:document/#', '', $d);}, $doc_ids);

return new JsonResponse($raw_ids);
}


public function imagePreview($id) {
$json = $this->dcxClient->getPreview("dcxapi:document/$id");

return new JsonResponse($json);
}
}
4 changes: 2 additions & 2 deletions modules/dcx_dropzone_ui/src/Controller/UploadController.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function handleUploads() {
*
* @return JsonResponse
*/
public function batchFinish() {
public static function batchFinish() {
$messages = drupal_render(\Drupal\Core\Render\Element\StatusMessages::renderMessages(NULL));
return new JsonResponse(['markup' => $messages]);
}
Expand All @@ -122,7 +122,7 @@ public function batchFinish() {
* In this case we do not return a redirect response (as it is the default)
* behaviour, but the id of the batch to be able to process it by AJAX.
*/
public function batchRedirectCallback($url, $query_options) {
public static function batchRedirectCallback($url, $query_options) {
return $query_options['query']['id'];
}
}
18 changes: 18 additions & 0 deletions src/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,24 @@ public function pubinfoOnPath($path, $type);
*/
public function removeAllUsage($dcx_id);

/**
* Retrieve collections of the current user
*
* @return array of arrays keyed by collection id.
*/
public function getCollections();

/**
* Return filename and url of a thumbnail for the given (image) document.
*
* @param string $id
*
* @return data array containg filename, url and id.
*
* @throws DcxClientException
*/
public function getPreview($id);

/**
* Removes usage information about the given DC-X ID on the current site, but
* only for the given entity.
Expand Down
102 changes: 102 additions & 0 deletions src/JsonClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -784,4 +784,106 @@ protected function watchdog_exception($type, \Exception $exception, $message = N

$this->logger->log($severity, $message, $variables);
}

/**
* {@inheritdoc}
*/
public function getCollections() {
$params = [
'q' => [
'_mode' => 'my_usertags',
'type_id' => 'usertagtype-default',
'parent_id' => NULL,
'_sort' => 'UTAG_VALUE'
],
's' => [
'properties' => '_label',
'children' => '*',
],
];

$this->api_client->getObject('usertag', $params, $usertags);

$collections = [];
foreach($usertags['entries'] as $usertag) {
$utag_id = preg_replace('#dcxapi:usertag/#', '', $usertag['_id']);
$collections[$usertag['_id']] = [
'label' => $usertag['properties']['_label'],
'id' => $utag_id,
'parent' => NULL,
];

if (isset($usertag['children'])) {
$collections[$usertag['_id']]['children'] = array_map(function($c) { return $c['_id']; }, $usertag['children']);
}
else {
$collections[$usertag['_id']]['children'] = [];
}
}

foreach ($collections as $collection) {
foreach($collection['children'] as $child_id) {
$collections[$child_id]['parent'] = $child_id;
}
}

return $collections;
}

public function getDocsOfCollection($utag_id) {
$doctoutag_params = [
'q[utag_id]' => $utag_id,
's[properties]' => '*',
's[_referenced][dcx:document][s][files]' => '*',
];

$this->api_client->getObject('doctoutag', $doctoutag_params, $docs);

$documents = [];

foreach($docs['entries'] as $doc) {
$documents[] = $doc['properties']['doc_id']['_id'];
}

return $documents;
}


/**
* {@inheritdoc}
*/
public function getPreview($id) {
$json = NULL;

$params = [
's[fields]' => 'Filename',
// All files.
's[files]' => '*',
// Attribute _file_absolute_url of all referenced files in the document.
's[_referenced][dcx:file][s][properties]' => '_file_url_absolute',
];

$url = preg_replace('/^dcxapi:/', '', $id);
$http_status = $this->api_client->getObject($url, $params, $json);

if (200 !== $http_status) {
$exception = new DcxClientException('getObject', $http_status, $url, $params, $json);
$this->watchdog_exception(__METHOD__, $exception);
throw $exception;
}

$variant_types = $this->processAttributeMap(['variants' => ['_files_index', 'variant_type', 'master']], $json);

$thumb_id = $variant_types['variants']['thumbnail'];

$attribute_map = [
'id' => ['_id'],
'filename' => ['fields', 'Filename', 0, 'value'],
'url' => [[$this, 'extractUrl'], 'files', $thumb_id, '_id'],
];

$data = $this->processAttributeMap($attribute_map, $json);

return $data;
}
}