View source
<?php
use Drupal\block\Entity\Block;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\GeneratedUrl;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\opigno_dashboard\BlockService;
use Drupal\opigno_tour\OpignoTourFunctions;
use Drupal\user\UserInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\opigno_dashboard\Controller\DashboardController;
define('OPIGNO_DASHBOARD_DEFAULT_CONFIG', '{"1":[{"admin_label":"Latest active trainings","id":"views_block:latest_active_trainings-block"}, {"admin_label":"Private Message: Dashboard - Last Threads","id":"views_block:private_message-block_dashboard"}],"2":[{"admin_label":"Opigno calendar block","id":"views_block:opigno_calendar-month_block"},{"admin_label":"Opigno Notifications: Dashboard - Unread Notifications","id":"views_block:opigno_notifications-block_unread_dashboard"}],"3":[]}');
function opigno_dashboard_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.opigno_dashboard':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Opigno dashboard') . '</p>';
return $output;
default:
}
}
function opigno_dashboard_theme() {
return [
'opigno_dashboard' => [
'base hook' => 'region',
'variables' => [
'base_path' => '',
'base_href' => '',
'route_name' => '',
'default_config' => NULL,
],
],
'opigno_dashboard_default' => [
'base hook' => 'region',
'variables' => [
'base_path' => '',
'base_href' => '',
'route_name' => '',
'default_config' => NULL,
],
],
];
}
function opigno_dashboard_preprocess_region(&$variables) {
$request = \Drupal::request();
$route_name = \Drupal::routeMatch()
->getRouteName();
if (in_array($route_name, [
'view.frontpage.page_1',
'opigno_dashboard.dashboard_admin_default_settings',
]) && $variables['region'] == 'content') {
$variables['base_path'] = \Drupal::request()
->getBasePath();
$variables['base_href'] = $request
->getPathInfo();
$variables['route_name'] = $route_name;
if ($route_name == 'opigno_dashboard.dashboard_admin_default_settings') {
$variables['default_config'] = '{}';
}
else {
$dashboard = new DashboardController();
$default_dashboard = $dashboard
->getUserDefaultPositioning();
$positions = json_encode($default_dashboard['positions']);
$columns = $default_dashboard['columns'];
$variables['default_config'] = !empty($positions) ? $positions : OPIGNO_DASHBOARD_DEFAULT_CONFIG;
}
$variables['default_columns'] = !empty($columns) ? $columns : 3;
$manager = \Drupal::service('plugin.manager.menu.local_task');
$cacheability = new CacheableMetadata();
$cacheability
->addCacheableDependency($manager);
$tabs = [
'#theme' => 'menu_local_tasks',
];
$primary = $manager
->getLocalTasks(\Drupal::routeMatch()
->getRouteName(), 0);
$cacheability = $cacheability
->merge($primary['cacheability']);
$tabs += [
'#primary' => count(Element::getVisibleChildren($primary['tabs'])) > 1 ? $primary['tabs'] : [],
];
$secondary = $manager
->getLocalTasks(\Drupal::routeMatch()
->getRouteName(), 1);
$cacheability = $cacheability
->merge($secondary['cacheability']);
$tabs += [
'#secondary' => count(Element::getVisibleChildren($secondary['tabs'])) > 1 ? $secondary['tabs'] : [],
];
$variables['tabs'] = $tabs;
}
}
function opigno_dashboard_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
if (\Drupal::routeMatch()
->getRouteName() == 'view.frontpage.page_1' && in_array('region__content', $suggestions)) {
$suggestions[] = 'opigno_dashboard';
}
if (\Drupal::routeMatch()
->getRouteName() == 'opigno_dashboard.dashboard_admin_default_settings' && in_array('region__content', $suggestions)) {
$suggestions[] = 'opigno_dashboard_default';
}
}
function opigno_dashboard_page_attachments(array &$page) {
$route_name = \Drupal::routeMatch()
->getRouteName();
if ($route_name == 'view.frontpage.page_1' || $route_name == 'opigno_dashboard.dashboard_admin_default_settings') {
$page['#attached']['library'][] = 'opigno_dashboard/ng_app';
$manage_dashboard_access = Drupal::currentUser()
->hasPermission('opigno dashboard layout access');
$page['#attached']['drupalSettings']['manageDashboardAccess'] = $manage_dashboard_access;
}
}
function opigno_dashboard_form_user_login_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$route = \Drupal::routeMatch();
if (strpos($route
->getRouteName(), 'rest.') !== FALSE) {
return;
}
if ($route
->getRouteName() == 'user.login') {
$previous_url = \Drupal::request()->query
->get('prev_path');
if (!empty($previous_url)) {
$tempstore = \Drupal::service('user.private_tempstore')
->get('opigno_dashboard');
$tempstore
->set('prev_path', $previous_url);
}
else {
$tempstore = \Drupal::service('user.private_tempstore')
->get('opigno_dashboard');
$prev_path = $tempstore
->get('prev_path');
if (!empty($prev_path)) {
$form_state
->set('prev_path', $prev_path);
}
}
}
}
function opigno_dashboard_user_login(UserInterface $account) {
$route_name = \Drupal::routeMatch()
->getRouteName();
if (strpos($route_name, 'rest.') !== FALSE || $route_name === 'user.reset.login') {
return;
}
if (!empty($_GET['profile']) && $_GET['profile'] == 'opigno_lms' && !empty($_POST['form_id']) && $_POST['form_id'] == 'install_configure_form') {
return;
}
$previous = \Drupal::service('user.private_tempstore')
->get('opigno_dashboard')
->get('prev_path') ?? NULL;
$params = [];
$default_path = \Drupal::config('system.site')
->get('page.front') ?? '<front>';
$default_route = \Drupal::pathValidator()
->getUrlIfValid($default_path)
->getRouteName();
if (\Drupal::moduleHandler()
->moduleExists('opigno_tour') && OpignoTourFunctions::checkRouteTour($default_route) && !OpignoTourFunctions::isPageUserViewed($default_route, $account
->id())) {
$params = [
'query' => [
'tour' => 1,
],
];
}
$url = $previous ? Url::fromUri('internal:' . $previous)
->toString() : Url::fromRoute('<front>', [], $params)
->toString();
$url = $url instanceof GeneratedUrl ? $url
->getGeneratedUrl() : $url;
$response = new RedirectResponse($url);
$response
->send();
}
function opigno_dashboard_blocks_update() {
$blocks_ids = [
'views_block:opigno_group_members-block_pending',
'views_block:comments_recent-block_1',
'views_block:content_recent-block_1',
'views_block:latest_active_trainings-block',
'views_block:opigno_calendar-month_block',
'views_block:opigno_notifications-block_unread_dashboard',
'views_block:opigno_score_modules-dashboard_notavaluated_modules',
'views_block:private_message-block_dashboard',
'views_block:who_s_new-block_1',
'views_block:who_s_online-who_s_online_block',
];
$config = \Drupal::configFactory();
$block_service = new BlockService();
$blocks = $block_service
->getAllBlocks();
foreach ($blocks_ids as $id) {
$old_id = $block_service
->sanitizeIdOld($id);
$new_id = str_replace('-', '_', $old_id);
if ($block_old = Block::load($old_id)) {
try {
$block_old
->delete();
} catch (Exception $e) {
\Drupal::logger('opigno_dashboard')
->error($e
->getMessage());
}
}
if (!Block::load($new_id)) {
$admin_label = $blocks[$id]['admin_label'];
if (is_object($admin_label)) {
$admin_label = $admin_label
->render();
}
$settings = [
'plugin' => $id,
'region' => 'content',
'id' => $new_id,
'theme' => $config
->get('system.theme')
->get('default'),
'label' => t('Dashboard:') . ' ' . $admin_label,
'visibility' => [
'request_path' => [
'id' => 'request_path',
'pages' => '<front>',
'negate' => FALSE,
'context_mapping' => [],
],
],
'weight' => 0,
];
$values = [];
foreach ([
'region',
'id',
'theme',
'plugin',
'weight',
'visibility',
] as $key) {
$values[$key] = $settings[$key];
unset($settings[$key]);
}
foreach ($values['visibility'] as $id => $visibility) {
$values['visibility'][$id]['id'] = $id;
}
$values['settings'] = $settings;
$block_new = Block::create($values);
try {
$block_new
->save();
} catch (Exception $e) {
\Drupal::logger('opigno_dashboard')
->error($e
->getMessage());
}
}
}
drupal_flush_all_caches();
}
function opigno_dashboard_preprocess_page_title(&$variables) {
if (\Drupal::routeMatch()
->getRouteName() == 'view.frontpage.page_1') {
$variables['title']['#allowed_tags'][] = 'span';
$variables['title']['#markup'] = '<span class="sr-only">' . $variables['title']['#markup'] . '</span>';
}
}
function opigno_dashboard_entity_delete(EntityInterface $entity) {
if ($entity
->getEntityTypeId() == 'block') {
$config = \Drupal::configFactory()
->getEditable('opigno_dashboard.settings');
if ($blocks = $config
->get('blocks')) {
$key = $entity
->getPluginId();
if (key_exists($key, $blocks)) {
unset($blocks[$key]);
$config
->set('blocks', $blocks);
try {
$config
->save();
} catch (\Exception $e) {
\Drupal::logger('opigno_dashboard')
->error($e
->getMessage());
\Drupal::messenger()
->addMessage($e
->getMessage(), 'error');
}
}
}
}
}