You are here

public function BlockService::getPositioning in Opigno dashboard 3.x

Returns positioning.

Parameters

int|string|null $uid: The user ID to get the positioning for.

bool $default: Should the default positioning be used or not.

bool $user_default: Should the user default positioning be used or not.

Return value

array|\Symfony\Component\HttpFoundation\JsonResponse Blocks positioning.

Overrides BlockServiceInterface::getPositioning

File

src/BlockService.php, line 297

Class

BlockService
The dashboard block manager service definition.

Namespace

Drupal\opigno_dashboard

Code

public function getPositioning($uid = NULL, bool $default = FALSE, bool $user_default = FALSE) {
  if (empty($uid)) {
    $uid = $this->currentUser
      ->id();
  }
  $availables = $this
    ->getAvailableBlocks();

  // Get default configuration.
  $config_default = $this->configFactory
    ->get('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 = $this->database
      ->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 = $result->columns ?? $this
      ->getDefaultLayoutId();
  }
  if (!$positions) {
    if (!empty($default_positions)) {
      $positions = $default_positions;
      $columns = $default_columns;
    }
    else {
      $positions = json_decode($this
        ->getDefaultDashboardConfig(), TRUE);
      $columns = $this
        ->getDefaultLayoutId();
    }
  }

  // Get mandatory blocks.
  $mandatory_blocks = $this->configFactory
    ->get('opigno_dashboard.settings')
    ->get('blocks');
  if (!empty($mandatory_blocks)) {
    $mandatory_blocks = array_filter($mandatory_blocks, function ($block) {
      return $block['available'] && $block['mandatory'];
    });
  }

  // Keep all mandatory blocks.
  $mandatory = $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);
    }
  }

  // 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) {
    $available_values = array_values($availables);
    $entities = array_merge([
      $available_values,
    ], $positions);
    $positions = $entities ?: array_merge([
      $available_values,
    ], [
      [],
      [],
      [],
    ]);
  }

  // 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' => $availables[$mandatory_block['availables_key']]['admin_label'] ?? '',
          'id' => $id,
          'mandatory' => TRUE,
        ]);
      }
    }
  }
  $columns = $columns ?? $this
    ->getDefaultLayoutId();
  $this
    ->clearEmptyPositions($positions, $availables_keys);
  if ($default) {
    return [
      'positions' => $positions,
      'columns' => $columns,
    ];
  }
  else {
    return new JsonResponse([
      'positions' => $positions,
      'columns' => $columns,
    ]);
  }
}