You are here

public static function PhotosAlbum::resetCount in Album Photos 6.0.x

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

Update count.

Parameters

bool $cron: If this is being called from cron.

1 call to PhotosAlbum::resetCount()
photos_cron in ./photos.module
Implements hook_cron().

File

src/PhotosAlbum.php, line 553

Class

PhotosAlbum
Create an album object.

Namespace

Drupal\photos

Code

public static function resetCount($cron = FALSE) {
  PhotosAlbum::setCount('site_album');
  PhotosAlbum::setCount('site_image');
  $time = $cron ? 7200 : 0;

  // @todo optimize. Check if new images since last count.
  // @todo this only works if cron has not run in 2 hours?
  $cron_last = \Drupal::state()
    ->get('system.cron_last', 0);
  if (\Drupal::time()
    ->getRequestTime() - $cron_last > $time) {
    $db = \Drupal::database();
    $result = $db
      ->query('SELECT uid FROM {users} WHERE uid != 0');
    foreach ($result as $t) {
      PhotosAlbum::setCount('user_image', $t->uid);
      PhotosAlbum::setCount('user_album', $t->uid);
    }
    $result = $db
      ->query('SELECT album_id FROM {photos_album}');
    foreach ($result as $t) {
      PhotosAlbum::setCount('node_album', $t->album_id);
    }
  }
}