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

altmetric on article #45

Open
wants to merge 4 commits into
base: 8.x-1.x-dev
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ name: JOURNAL ARTICLE DETAIL
description: All article detail page functionality
package: 'JCOREX'
type: module
core: 8.x
core_version_requirement: ^9 || ^10
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
journal_article_detail.usage_stats_settings_form:
title: 'Article Usage Stats Config'
route_name: journal_article_detail.usage_stats_settings_form
description: 'A description for the menu entry'
parent: highwire_core.admin_index
weight: 100
Original file line number Diff line number Diff line change
Expand Up @@ -86,31 +86,42 @@ function journal_article_detail_node_view(array &$build, EntityInterface $entity
}
// Add the usage-stats pseudofield
if ($display->getComponent('hwjma_usage_stats')) {
$filter_tabs = '<a href="/aa">ADD</a>';
$block_manager = \Drupal::service('plugin.manager.block');
$usageStatsConfig = \Drupal::config('journal_article_detail.settings');
$usageStatsDefaultView = $usageStatsConfig->get('default_view');
$usageStatsDateFilter = $usageStatsConfig->get('date_filters');
$filterTabData = '';
$filterTabValues = [
'ArticleLifetime' => 'Article Lifetime',
'LastSixMonths' => 'Last 6 Months',
'ThisMonth' => 'This Month'
];
foreach ($usageStatsDateFilter as $key => $value) {
if (!empty($value)) {
$filterTabData .= '<a href="/journal_article_detail?type='.$key.'&nid='.$entity->get('nid')->getString().'" class="btn btn button js-form-submit form-submit use-ajax">'.$filterTabValues[$value].'</a>';
}
}
$filter_tabs = '<div><h2>Usage statistics:</h2><p>'.$filterTabData.'</p></div>';
$form = \Drupal::formBuilder()->getForm('Drupal\journal_article_detail\Form\UsageStatsDateRangeForm');
$build['hwjma_usage_stats']['custom_tabs'] = [
'#type' => 'markup',
'#markup' => $filter_tabs,
];
$build['hwjma_usage_stats']['custom_form'] = $form;
$block_manager = \Drupal::service('plugin.manager.block');
$plugin_block = $block_manager->createInstance('hwjma_usage_stats', [
'query_type' => 'monthly',
'query_type' => 'ArticleLifetime',
'views' => ['abstract', 'full', 'pdf', 'total'],
'date_format' => 'custom',
'custom_date_format' => 'M Y',
'limit' => '',
'fromDate' => '',
'toDate' => '',
]);
$plugin_block->setContextValue('node', $entity);
$render = $plugin_block->build();
if (!empty($render)) {
$build['hwjma_usage_stats'] = $render;
}
/* $block['hwjma_usage_stats'] = [
'#theme' => 'hwjma_usage_stats_data',
'#filter_tabs' => $filter_tabs,
'#usage_stats_data' => $render,
];

$build['hwjma_usage_stats'] = [
'#type' => 'fieldset',
'#attributes' => ['class' => ['usage-stats-container']],
'usage_stats' => $plugin_block->build()
];*/
if (!empty($render)) {
$build['hwjma_usage_stats']['data'] = $render;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ journal_article_detail.altmetrics_data:
_title: 'Altmetric Data'
requirements:
_permission: 'access content'

journal_article_detail.usage_stats_settings_form:
path: '/admin/config/highware/usage_stats_settings'
defaults:
_form: '\Drupal\journal_article_detail\Form\UsageStatsSettingsForm'
_title: 'UsageStatsSettingsForm'
requirements:
_permission: 'access administration pages'
options:
_admin_route: TRUE
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,38 @@ public function content(Request $request) {
if (!$request->isXmlHttpRequest()) {
throw new NotFoundHttpException();
}

$response = new AjaxResponse();

$type = \Drupal::request()->query->get('type');
$nid = \Drupal::request()->query->get('nid');

$fromDate = '';
$toDate = '';
if ($type == 'LastSixMonths') {
$fromDate = date("Y-m-d", mktime(0, 0, 0, date("m")-6, date("d"), date("Y")));
$toDate = date("Y-m-d");
} elseif ($type == 'ThisMonth') {
$fromDate = date("Y-m-01");
$toDate = date("Y-m-d");
}
$build = [];
$block_manager = \Drupal::service('plugin.manager.block');
$plugin_block = $block_manager->createInstance('hwjma_usage_stats', [
'query_type' => $type,
'views' => ['abstract', 'full', 'pdf', 'total'],
'views' => ['abstract', 'full','pdf', 'total'],
'date_format' => 'custom',
'custom_date_format' => 'M Y',
'limit' => '',
'fromDate' => $fromDate,
'toDate' => $toDate,
]);
$plugin_block->setContextValue('node', $nid);
$render = $plugin_block->build();
if (!empty($render)) {
$build = $render;
}

$Selector = '.ajax-wrapper'; //See: https://www.w3schools.com/cssref/css_selectors.asp
$content = '<p>Changed !!!</p>'; /*The content that will be replace the matched element(s), either a render array or an HTML string.*/
$settings = ['my-setting' => 'setting',]; /*An array of JavaScript settings to be passed to any attached behaviors.*/
$response->addCommand(new ReplaceCommand($Selector, $build, $settings));
$Selector = '.ajax-wrapper';
$content = '<p>Changed !!!</p>';
$settings = ['my-setting' => 'setting',];
$response->addCommand(new ReplaceCommand($Selector, $build, $settings));
return $response;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
/**
* @file
* Contains Drupal\journal_article_detail\Form\UsageStatsDateRangeForm.
*/

namespace Drupal\journal_article_detail\Form;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Ajax\ReplaceCommand;
use Symfony\Component\HttpFoundation\Request;

/**
* Class UsageStatsDateRangeForm.
*
* @package Drupal\journal_article_detail\Form
*/
class UsageStatsDateRangeForm extends FormBase {

/**
* {@inheritdoc}
*/
public function getFormId() {
return 'usage_stats_date_range_form';
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['filter_by_date'] = array(
'#type' => 'fieldset',
'#title' => 'Select a custom date range',
'#title' => 'Select a custom date range for the past year',
'#collapsible' => TRUE,
);

$form['filter_by_date']['start_date'] = array(
'#type' => 'date',
'#date_format' => 'Y-m-d',
'#prefix' => '<div class="filter-by-date">',
);

$form['filter_by_date']['end_date'] = array(
'#type' => 'date',
'#date_format' => 'Y-m-d',
'#prefix' => '<span class="date-spacer"> to </span>',
'#suffix' => '</div>',
);

$form['actions'] = [
'#type' => 'button',
'#value' => $this->t('Submit'),
'#ajax' => [
'callback' => '::setMessage',
],
];

return $form;
}

/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
}

/**
*
*/
public function setMessage(array $form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$build = [];
$block_manager = \Drupal::service('plugin.manager.block');
$plugin_block = $block_manager->createInstance('hwjma_usage_stats', [
'query_type' => $type,
'views' => ['abstract', 'full', 'pdf', 'total'],
'date_format' => 'custom',
'custom_date_format' => 'M Y',
'limit' => '',
'fromDate' => $form_state->getValue('start_date'),
'toDate' => $form_state->getValue('end_date'),
]);
$plugin_block->setContextValue('node', 102);
$render = $plugin_block->build();
if (!empty($render)) {
$build = $render;
}
$Selector = '.ajax-wrapper';
$content = '<p>Changed !!!</p>';
$settings = ['my-setting' => 'setting',];
$response->addCommand(new ReplaceCommand($Selector, $build, $settings));
return $response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

/**
* @file
* Contains Drupal\journal_article_detail\Form\UsageStatsSettingsForm.
*/

namespace Drupal\journal_article_detail\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
* Class UsageStatsSettingsForm.
*
* @package Drupal\journal_article_detail\Form
*/
class UsageStatsSettingsForm extends ConfigFormBase {

/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'journal_article_detail.settings',
];
}

/**
* {@inheritdoc}
*/
public function getFormId() {
return 'usage_stats_settings_form';
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('journal_article_detail.settings');

$form['default_view'] = [
'#type' => 'radios',
'#title' => $this->t('DEFAULT VIEW'),
'#options' => [
0 => $this->t('Article lifetime'),
1 => $this->t('Last 6 Months'),
2 => $this->t('This Month'),
],
'#default_value' => !empty($config->get('default_view')) ? $config->get('default_view') : 0,
];

$form['date_filters'] = array(
'#type' => 'checkboxes',
'#title' => $this->t('DATE FILTERS'),
'#options' => array('ArticleLifetime' => $this->t('Article lifetime'), 'LastSixMonths' => $this->t('Last 6 Months'), 'ThisMonth' => $this->t('This Month')),
'#default_value' => !empty($config->get('date_filters')) ? $config->get('date_filters') : array('ArticleLifetime', 'LastSixMonths', 'ThisMonth'),
);

$form['display_settings'] = [
'#type' => 'radios',
'#title' => $this->t('DISPLAY SETTINGS'),
'#options' => [
0 => $this->t('Chart Only'),
1 => $this->t('Table Only'),
2 => $this->t('Both Chart and Table'),
],
'#default_value' => !empty($config->get('display_settings')) ? $config->get('display_settings') : 1,
'#attributes' => ['data-default-value' => 1],
];

$form['metric_types'] = array(
'#type' => 'checkboxes',
'#title' => $this->t('SELECT WHICH TYPE OF METRICS TO SHOW ON THE GRAPH AND TABLE.'),
'#options' => array('Abstract' => $this->t('Abstract'), 'Full' => $this->t('Full'), 'PDF' => $this->t('PDF')),
'#default_value' => !empty($config->get('metric_types')) ? $config->get('metric_types') : array('Abstract', 'Full', 'PDF'),
);

return parent::buildForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);

$this->config('journal_article_detail.settings')
->set('default_view', $form_state->getValue('default_view'))
->set('date_filters', $form_state->getValue('date_filters'))
->set('display_settings', $form_state->getValue('display_settings'))
->set('metric_types', $form_state->getValue('metric_types'))
->save();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,21 @@ public static function create(ContainerInterface $container, array $configuratio
*/
public function build() {
$build = [];

try {
$node = $this->getContextValue('node');
}
catch (\Exception $e) {
return $build;
}
// Get the node from context data.
$doi = $node->get('doi')->getString(); // dd($doi);
$doi = '10.1503/cmaj.220966';//$node->get('doi')->getString(); // dd($doi);
$altmetrics_data = '<div data-badge-details="right" data-badge-type="medium-donut" data-doi="'.$doi.'" data-hide-no-mentions="true" class="altmetric-embed"></div>';
//$aa = '<div class="altmetric-embed" data-badge-type="donut" data-doi="10.1038/nature.2012.9872"></div>';

$build = [
'#theme' => 'hwjma_alt_metrics',
'#altmetrics_data' => $altmetrics_data
];
];
$build['#attached']['library'][] = 'journal_article_detail/altmetrics';

return $build;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ public function build() {
$issue = $issue[0]['value'];
$volume = !$node->get('volume')->isEmpty() ? $node->get('volume')->getString() : '';
$date_released = $node->get('date_epub_original')->getValue()['0']['original'];

$build = [
'#theme' => 'hwjma_info_metrics_tab',
'#journal_title' => $journal_title,
Expand Down
Loading