View source
<?php
namespace Drupal\opigno_dashboard\Controller;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\opigno_dashboard\BlockServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
class DashboardController extends ControllerBase {
protected $blockService;
protected $database;
protected $userEntityQuery = NULL;
public function __construct(BlockServiceInterface $block_service, AccountInterface $account, MessengerInterface $messenger, Connection $database, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
$this->blockService = $block_service;
$this->currentUser = $account;
$this->messenger = $messenger;
$this->database = $database;
$this->configFactory = $config_factory;
try {
$this->userEntityQuery = $entity_type_manager
->getStorage('user')
->getQuery();
} catch (PluginNotFoundException|InvalidPluginDefinitionException $e) {
watchdog_exception('opigno_dashboard_exception', $e);
}
}
public static function create(ContainerInterface $container) {
return new static($container
->get('opigno_dashboard.block'), $container
->get('current_user'), $container
->get('messenger'), $container
->get('database'), $container
->get('config.factory'), $container
->get('entity_type.manager'));
}
public function dashboardDefaultBlocks() {
return [];
}
public function getPositioning($uid = NULL, bool $default = FALSE, bool $user_default = FALSE) {
return $this->blockService
->getPositioning($uid, $default, $user_default);
}
public function setPositioning(Request $request) : JsonResponse {
$datas = json_decode($request
->getContent());
unset($datas->positions[0]);
try {
$this->database
->merge('opigno_dashboard_positioning')
->key([
'uid' => $this->currentUser
->id(),
])
->fields([
'columns' => (int) $datas->columns,
])
->fields([
'positions' => json_encode($datas->positions),
])
->execute();
} catch (\Exception $e) {
watchdog_exception('opigno_dashboard_exception', $e);
}
return new JsonResponse(NULL, Response::HTTP_OK);
}
public function getBlocksContents() {
$data = $this->blockService
->getDashboardBlocksContents();
return new JsonResponse([
'blocks' => $data['blocks'],
'drupalSettings' => $data['attachments']['drupalSettings'] ?? [],
]);
}
public function getDefaultPositioning() : JsonResponse {
$positioning = $this->blockService
->getPositioning(NULL, TRUE);
return new JsonResponse([
'positions' => $positioning['positions'],
'columns' => $positioning['columns'],
], Response::HTTP_OK);
}
public function setDefaultPositioning(Request $request) : JsonResponse {
$datas = json_decode($request
->getContent());
unset($datas->positions[0]);
if (!empty($datas->positions)) {
foreach ($datas->positions as &$position) {
if (!empty($position)) {
foreach ($position as &$block) {
$block->admin_label = str_replace("'", "`", $block->admin_label);
}
}
}
}
try {
$config = $this->configFactory
->getEditable('opigno_dashboard.default.settings');
$config
->set('positions', json_encode($datas->positions));
$config
->set('columns', (int) $datas->columns);
$config
->save();
} catch (\Exception $e) {
watchdog_exception('opigno_dashboard_exception', $e);
$this->messenger
->addMessage($e
->getMessage(), 'error');
}
return new JsonResponse();
}
public function restoreToDefaultAll() : JsonResponse {
$positioning = $this->blockService
->getPositioning(NULL, TRUE, TRUE);
unset($positioning['positions'][0]);
if (!$this->userEntityQuery instanceof QueryInterface) {
return new JsonResponse(NULL, 400);
}
$uids = $this->userEntityQuery
->execute();
unset($uids[0]);
if (!$uids) {
return new JsonResponse(NULL, 400);
}
foreach ($uids as $uid) {
try {
$this->database
->merge('opigno_dashboard_positioning')
->key([
'uid' => $uid,
])
->fields([
'columns' => (int) $positioning['columns'],
'positions' => json_encode($positioning['positions']),
])
->execute();
} catch (\Exception $e) {
watchdog_exception('opigno_dashboard_exception', $e);
}
}
return new JsonResponse();
}
}