You are here

public function DashboardController::getPositioning in Opigno dashboard 8

Same name and namespace in other branches
  1. 3.x src/Controller/DashboardController.php \Drupal\opigno_dashboard\Controller\DashboardController::getPositioning()

Returns positioning.

3 calls to DashboardController::getPositioning()
DashboardController::getDefaultPositioning in src/Controller/DashboardController.php
Returns default positioning.
DashboardController::getUserDefaultPositioning in src/Controller/DashboardController.php
Returns user default positioning.
DashboardController::restoreToDefaultAll in src/Controller/DashboardController.php
1 string reference to 'DashboardController::getPositioning'
opigno_dashboard.routing.yml in ./opigno_dashboard.routing.yml
opigno_dashboard.routing.yml

File

src/Controller/DashboardController.php, line 22

Class

DashboardController
Controller for all the actions of the Learning Path manager app.

Namespace

Drupal\opigno_dashboard\Controller

Code

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();

  // Get default configuration.
  $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;
    }
  }

  // Get mandatory blocks.
  $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'];
    });
  }

  // Keep all mandatory blocks.
  $mandatory = !empty($mandatory_blocks) ? $mandatory_blocks : [];

  // Remove blocks not availables.
  $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;
      }

      // Filter unused mandatory blocks.
      if (!empty($mandatory_blocks) && isset($mandatory_blocks[$row['id']])) {
        unset($mandatory_blocks[$row['id']]);
      }

      // Add mandatory property to positions blocks.
      $positions[$key1][$key2]['mandatory'] = $mandatory && array_key_exists($row['id'], $mandatory) ? TRUE : FALSE;
    }
  }

  // Remove block already used.
  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]);
        }
      }
    }

    // Save mandatory blocks key from "availables" array.
    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),
    ], [
      [],
      [],
      [],
    ]);
  }

  // Add unused mandatory blocks.
  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);
  }
}