You are here

public static function PhotosAlbum::setCount in Album Photos 8.5

Same name and namespace in other branches
  1. 8.4 src/PhotosAlbum.php \Drupal\photos\PhotosAlbum::setCount()
  2. 6.0.x src/PhotosAlbum.php \Drupal\photos\PhotosAlbum::setCount()

Calculate number of $type.

10 calls to PhotosAlbum::setCount()
PhotosAlbum::resetCount in src/PhotosAlbum.php
Update count.
PhotosDirectoryImportForm::finishedMovingImageFiles in src/Form/PhotosDirectoryImportForm.php
Finished batch operation moving image files.
PhotosImage::delete in src/PhotosImage.php
Delete image.
PhotosImageAddForm::save in src/Form/PhotosImageAddForm.php
Form submission handler for the 'save' action.
PhotosImageEditForm::save in src/Form/PhotosImageEditForm.php
Form submission handler for the 'save' action.

... See full list

File

src/PhotosAlbum.php, line 553

Class

PhotosAlbum
Create an album object.

Namespace

Drupal\photos

Code

public static function setCount($type, $id = 0) {
  $db = \Drupal::database();
  $requestTime = \Drupal::time()
    ->getRequestTime();
  switch ($type) {
    case 'user_image':
      $count = $db
        ->query('SELECT count(p.id) FROM {photos_image_field_data} p WHERE p.uid = :id', [
        ':id' => $id,
      ])
        ->fetchField();
      $query = $db
        ->update('photos_count');
      $query
        ->fields([
        'value' => $count,
        'changed' => $requestTime,
      ]);
      $query
        ->condition('cid', $id);
      $query
        ->condition('type', $type);
      $affected_rows = $query
        ->execute();
      if (!$affected_rows) {
        $db
          ->insert('photos_count')
          ->fields([
          'cid' => $id,
          'changed' => $requestTime,
          'type' => $type,
          'value' => $count,
        ])
          ->execute();
      }

      // Clear cache tags.
      Cache::invalidateTags([
        'photos:image:user:' . $id,
      ]);
      break;
    case 'user_album':
      $count = $db
        ->query('SELECT count(p.album_id) FROM {photos_album} p INNER JOIN {node_field_data} n ON p.album_id = n.nid WHERE n.uid = :uid', [
        ':uid' => $id,
      ])
        ->fetchField();
      $query = $db
        ->update('photos_count')
        ->fields([
        'value' => $count,
        'changed' => $requestTime,
      ])
        ->condition('cid', $id)
        ->condition('type', $type);
      $affected_rows = $query
        ->execute();
      if (!$affected_rows) {
        $db
          ->insert('photos_count')
          ->fields([
          'cid' => $id,
          'changed' => $requestTime,
          'type' => $type,
          'value' => $count,
        ])
          ->execute();
      }

      // Clear cache tags.
      Cache::invalidateTags([
        'photos:album:user:' . $id,
      ]);
      break;
    case 'site_album':
      $count = $db
        ->query('SELECT COUNT(album_id) FROM {photos_album}')
        ->fetchField();
      $query = $db
        ->update('photos_count')
        ->fields([
        'value' => $count,
        'changed' => $requestTime,
      ])
        ->condition('cid', 0)
        ->condition('type', $type);
      $affected_rows = $query
        ->execute();
      if (!$affected_rows) {
        $db
          ->insert('photos_count')
          ->fields([
          'cid' => 0,
          'changed' => $requestTime,
          'type' => $type,
          'value' => $count,
        ])
          ->execute();
      }
      break;
    case 'site_image':
      $count = $db
        ->query('SELECT COUNT(id) FROM {photos_image_field_data}')
        ->fetchField();
      $query = $db
        ->update('photos_count')
        ->fields([
        'value' => $count,
        'changed' => $requestTime,
      ])
        ->condition('cid', 0)
        ->condition('type', $type);
      $affected_rows = $query
        ->execute();
      if (!$affected_rows) {
        $db
          ->insert('photos_count')
          ->fields([
          'cid' => 0,
          'changed' => $requestTime,
          'type' => $type,
          'value' => $count,
        ])
          ->execute();
      }

      // Clear cache tags.
      Cache::invalidateTags([
        'photos:image:recent',
      ]);
      break;
    case 'node_album':
      $count = $db
        ->query("SELECT COUNT(id) FROM {photos_image_field_data} WHERE album_id = :album_id", [
        ':album_id' => $id,
      ])
        ->fetchField();
      $db
        ->update('photos_album')
        ->fields([
        'count' => $count,
      ])
        ->condition('album_id', $id)
        ->execute();
      break;
  }
}