View source
<?php
namespace Drupal\media_acquiadam;
use cweagans\webdam\Entity\Asset;
use Drupal\Core\Database\Connection;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AssetData implements AssetDataInterface, ContainerInjectionInterface {
protected $connection;
public function __construct(Connection $connection) {
$this->connection = $connection;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('database'));
}
public function delete($assetID = NULL, $name = NULL) {
$query = $this->connection
->delete('acquiadam_assets_data');
if (isset($assetID)) {
$query
->condition('asset_id', (array) $assetID, 'IN');
}
if (isset($name)) {
$query
->condition('name', (array) $name, 'IN');
}
$query
->execute();
}
public function isUpdatedAsset(Asset $asset, $saveUpdatedVersion = TRUE) {
$current_version = intval($this
->get($asset->id, 'version'));
$new_version = intval($asset->version);
return $new_version !== $current_version;
}
public function get($assetID = NULL, $name = NULL) {
$query = $this->connection
->select('acquiadam_assets_data', 'ad')
->fields('ad');
if (isset($assetID)) {
$query
->condition('asset_id', $assetID);
}
if (isset($name)) {
$query
->condition('name', $name);
}
$result = $query
->execute();
if (isset($assetID) && isset($name)) {
$result = $result
->fetchAllAssoc('asset_id');
if (isset($result[$assetID])) {
return $result[$assetID]->serialized ? unserialize($result[$assetID]->value) : $result[$assetID]->value;
}
return NULL;
}
$return = [];
if (isset($assetID)) {
foreach ($result as $record) {
$return[$record->name] = $record->serialized ? unserialize($record->value) : $record->value;
}
return $return;
}
if (isset($name)) {
foreach ($result as $record) {
$return[$record->asset_id] = $record->serialized ? unserialize($record->value) : $record->value;
}
return $return;
}
foreach ($result as $record) {
$return[$record->asset_id][$record->name] = $record->serialized ? unserialize($record->value) : $record->value;
}
return $return;
}
public function set($assetID, $name, $value) {
$serialized = (int) (!is_scalar($value));
if ($serialized) {
$value = serialize($value);
}
$this->connection
->merge('acquiadam_assets_data')
->keys([
'asset_id' => $assetID,
'name' => $name,
])
->fields([
'value' => $value,
'serialized' => $serialized,
])
->execute();
}
}