You are here

public function PhotosImage::pager in Album Photos 8.4

Photos image view pager block.

File

src/PhotosImage.php, line 271

Class

PhotosImage
Create images object.

Namespace

Drupal\photos

Code

public function pager($id, $type = 'pid') {
  $fid = $this->fid;
  $db = \Drupal::database();
  $query = $db
    ->select('file_managed', 'f');
  $query
    ->join('photos_image', 'p', 'f.fid = p.fid');
  $query
    ->fields('p', [
    'pid',
  ])
    ->fields('f', [
    'fid',
    'uri',
    'filename',
  ]);

  // Default order by fid.
  $order = [
    'column' => 'f.fid',
    'sort' => 'DESC',
  ];
  if ($type == 'pid') {

    // Viewing album.
    // Order images by album settings.
    $db = \Drupal::database();
    $album_data = $db
      ->query('SELECT data FROM {photos_album} WHERE pid = :pid', [
      ':pid' => $id,
    ])
      ->fetchField();
    $album_data = unserialize($album_data);
    $default_order = \Drupal::config('photos.settings')
      ->get('photos_display_imageorder');
    $image_order = isset($album_data['imageorder']) ? $album_data['imageorder'] : $default_order;
    $order = explode('|', $image_order);
    $order = PhotosAlbum::orderValueChange($order[0], $order[1]);
    $query
      ->condition('p.pid', $id);
  }
  elseif ($type == 'uid') {

    // Viewing all user images.
    $query
      ->condition('f.uid', $id);
  }
  else {

    // Show all.
  }
  $query
    ->orderBy($order['column'], $order['sort']);
  if ($order['column'] != 'f.fid') {
    $query
      ->orderBy('f.fid', 'DESC');
  }
  $result = $query
    ->execute();
  $style_name = \Drupal::config('photos.settings')
    ->get('photos_pager_imagesize');
  $stop = $t['next'] = 0;
  $num = 0;
  $previous_image_fid = NULL;
  foreach ($result as $image) {
    $num++;
    $image_view = '';
    if ($stop == 1) {
      if (isset($image->fid)) {
        $photos_image = new PhotosImage($image->fid);
        $image_view = $photos_image
          ->view($style_name);
      }
      $t['next_view'] = $image_view;

      // Next image.
      $t['next_url'] = Url::fromUri('base:photos/image/' . $image->fid)
        ->toString();
      break;
    }
    if ($image->fid == $fid) {
      if (isset($image->fid)) {
        $photos_image = new PhotosImage($image->fid);
        $image_view = $photos_image
          ->view($style_name);
      }
      $t['current_view'] = $image_view;
      $stop = 1;
    }
    else {
      $previous_image_fid = $image->fid;
    }
  }
  if ($previous_image_fid) {
    $photos_image = new PhotosImage($previous_image_fid);
    $image_view = $photos_image
      ->view($style_name);
    $t['prev'] = $photos_image;
    $t['prev']->view = $image_view;
    $t['prev_view'] = $t['prev']->view;

    // Previous image.
    $t['prev_url'] = Url::fromUri('base:photos/image/' . $previous_image_fid)
      ->toString();
  }
  return $t;
}