PhotosStatisticsUpdateController.php in Album Photos 8.5
File
src/Controller/PhotosStatisticsUpdateController.php
View source
<?php
namespace Drupal\photos\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Database\Connection;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
class PhotosStatisticsUpdateController extends ControllerBase {
protected $connection;
public function __construct(Connection $connection) {
$this->connection = $connection;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('database'));
}
public function updateCount() {
$photosImageCountDisabled = $this
->config('photos.settings')
->get('photos_image_count');
$json = [
'count' => 1,
];
if (!$photosImageCountDisabled) {
$id = filter_input(INPUT_POST, 'id', FILTER_VALIDATE_INT);
if ($id) {
$json['count'] = $this
->recordView($id);
}
}
return new JsonResponse($json);
}
public function recordView($id) {
$count = 1;
try {
$this->connection
->merge('photos_count')
->keys([
'cid' => $id,
'type' => 'image_views',
])
->fields([
'value' => $count,
'changed' => \Drupal::time()
->getRequestTime(),
])
->expression('value', 'value + :count', [
':count' => $count,
])
->execute();
$count = $this->connection
->select('photos_count', 'c')
->fields('c', [
'value',
])
->condition('c.cid', $id)
->condition('c.type', 'image_views')
->execute()
->fetchField();
return $count;
} catch (\Exception $e) {
\Drupal::logger('photos')
->notice('Image view statistics failed.');
watchdog_exception('photos', $e);
return $count;
}
}
}