View source
<?php
namespace Drupal\opigno_dashboard\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
class DashboardController extends ControllerBase {
public function dashboardDefaultBlocks() {
return [];
}
public function getPositioning($uid = NULL, $default = FALSE, $user_default = FALSE) {
if (empty($uid)) {
$current_user = \Drupal::currentUser();
if (!$current_user) {
return new JsonResponse(NULL, Response::HTTP_BAD_REQUEST);
}
$uid = $current_user
->id();
}
$availables = \Drupal::service('opigno_dashboard.block')
->getAvailableBlocks();
$connection = \Drupal::database();
$config_default = $this
->config('opigno_dashboard.default.settings');
$default_positions = json_decode($config_default
->get('positions'), TRUE);
$default_columns = $config_default
->get('columns');
if ($default) {
$positions = $default_positions;
$columns = $default_columns;
}
else {
$query = $connection
->select('opigno_dashboard_positioning', 'p')
->fields('p', [
'columns',
'positions',
])
->condition('p.uid', $uid);
$result = $query
->execute()
->fetchObject();
$positions = FALSE;
if (!empty($result->positions)) {
$positions = json_decode($result->positions, TRUE);
}
$columns = !empty($result->columns) ? $result->columns : 3;
}
if (!$positions) {
if (!empty($default_positions)) {
$positions = $default_positions;
$columns = $default_columns;
}
else {
$positions = json_decode(OPIGNO_DASHBOARD_DEFAULT_CONFIG, TRUE);
$columns = 3;
}
}
$config = $this
->config('opigno_dashboard.settings');
$mandatory_blocks = $config
->get('blocks');
if (!empty($mandatory_blocks)) {
$mandatory_blocks = array_filter($mandatory_blocks, function ($block) {
return $block['available'] && $block['mandatory'];
});
}
$mandatory = !empty($mandatory_blocks) ? $mandatory_blocks : [];
$availables_keys = [];
foreach ($availables as $available) {
$availables_keys[$available['id']] = $available['id'];
}
foreach ($positions as $key1 => $column) {
foreach ($column as $key2 => $row) {
if (!in_array($row['id'], $availables_keys)) {
unset($positions[$key1][$key2]);
continue;
}
if (!empty($mandatory_blocks) && isset($mandatory_blocks[$row['id']])) {
unset($mandatory_blocks[$row['id']]);
}
$positions[$key1][$key2]['mandatory'] = $mandatory && array_key_exists($row['id'], $mandatory) ? TRUE : FALSE;
}
}
foreach ($availables as $key => $value) {
foreach ($positions as $column) {
foreach ($column as $row) {
if (isset($row['id']) && isset($value['id']) && $row['id'] == $value['id']) {
unset($availables[$key]);
}
}
}
if (!empty($mandatory_blocks) && array_key_exists($value['id'], $mandatory_blocks)) {
$mandatory_blocks[$value['id']]['availables_key'] = $key;
}
}
if (!$user_default) {
$entities = array_merge([
array_values($availables),
], $positions);
$positions = $entities ? $entities : array_merge([
array_values($availables),
], [
[],
[],
[],
]);
}
if (!empty($mandatory_blocks)) {
foreach ($mandatory_blocks as $id => $mandatory_block) {
if (!empty($mandatory_block['availables_key'])) {
array_unshift($positions[1], [
'admin_label' => isset($availables[$mandatory_block['availables_key']]['admin_label']) ? $availables[$mandatory_block['availables_key']]['admin_label'] : '',
'id' => $id,
'mandatory' => TRUE,
]);
}
}
}
$columns = !empty($columns) ? $columns : 3;
$this
->clearEmptyPositions($positions, $availables_keys);
if ($default) {
return [
'positions' => $positions,
'columns' => $columns,
];
}
else {
return new JsonResponse([
'positions' => $positions,
'columns' => $columns,
], Response::HTTP_OK);
}
}
private function clearEmptyPositions(array &$positions, array $availables_keys) {
foreach ($positions as $c_key => $columns) {
if (!is_array($columns)) {
continue;
}
foreach ($columns as $key => $position) {
if (!is_array($columns)) {
continue;
}
if (!isset($position['id']) || isset($position['id']) && !in_array($position['id'], $availables_keys)) {
unset($positions[$c_key][$key]);
}
}
$positions[$c_key] = array_values($positions[$c_key]);
}
}
public function setPositioning(Request $request) {
$datas = json_decode($request
->getContent());
$connection = \Drupal::database();
unset($datas->positions[0]);
$connection
->merge('opigno_dashboard_positioning')
->key([
'uid' => \Drupal::currentUser()
->id(),
])
->fields([
'columns' => (int) $datas->columns,
])
->fields([
'positions' => json_encode($datas->positions),
])
->execute();
return new JsonResponse(NULL, Response::HTTP_OK);
}
public function getBlocksContents() {
$blocks = \Drupal::service('opigno_dashboard.block')
->getDashboardBlocksContents();
return new JsonResponse([
'blocks' => $blocks,
], Response::HTTP_OK);
}
public function getUserDefaultPositioning() {
$positioning = $this
->getPositioning(NULL, TRUE, TRUE);
return $positioning;
}
public function getDefaultPositioning() {
$positioning = $this
->getPositioning(NULL, TRUE);
return new JsonResponse([
'positions' => $positioning['positions'],
'columns' => $positioning['columns'],
], Response::HTTP_OK);
}
public function setDefaultPositioning(Request $request) {
$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 = \Drupal::configFactory()
->getEditable('opigno_dashboard.default.settings');
$config
->set('positions', json_encode($datas->positions));
$config
->set('columns', (int) $datas->columns);
$config
->save();
} catch (\Exception $e) {
\Drupal::logger('opigno_dashboard')
->error($e
->getMessage());
\Drupal::messenger()
->addMessage($e
->getMessage(), 'error');
}
return new JsonResponse(NULL, Response::HTTP_OK);
}
public function restoreToDefaultAll() {
$positioning = $this
->getPositioning(NULL, TRUE, TRUE);
unset($positioning['positions'][0]);
$connection = \Drupal::database();
$uids = \Drupal::entityQuery('user')
->execute();
unset($uids[0]);
if ($uids) {
foreach ($uids as $uid) {
$connection
->merge('opigno_dashboard_positioning')
->key([
'uid' => $uid,
])
->fields([
'columns' => (int) $positioning['columns'],
'positions' => json_encode($positioning['positions']),
])
->execute();
}
}
return new JsonResponse(NULL, Response::HTTP_OK);
}
}