class BrightcovePlaylistEntityController in Brightcove Video Connect 7.7
Same name and namespace in other branches
- 7.6 brightcove.playlist.inc \BrightcovePlaylistEntityController
Entity controller class for Brightcove client.
Hierarchy
- class \DrupalDefaultEntityController implements DrupalEntityControllerInterface
- class \EntityAPIController implements EntityAPIControllerRevisionableInterface
Expanded class hierarchy of BrightcovePlaylistEntityController
1 string reference to 'BrightcovePlaylistEntityController'
- brightcove_entity_info in ./
brightcove.module - Implements hook_entity_info().
File
- ./
brightcove.playlist.inc, line 10 - Brightcove playlist related functions.
View source
class BrightcovePlaylistEntityController extends EntityAPIController {
/**
* Overwrites EntityAPIController::create().
*/
public function create(array $values = []) {
// Add is_new property if it is not set.
$values += [
'is_new' => TRUE,
];
brightcove_load_lib();
$playlist_entity = new Entity($values, $this->entityType);
if (empty($playlist_entity->client) || !$playlist_entity->client instanceof Entity) {
$playlist_entity->client = entity_create('brightcove_client', []);
}
if (empty($playlist_entity->playlist) || !$playlist_entity->playlist instanceof \Brightcove\Object\Playlist) {
$playlist_entity->playlist = new \Brightcove\Object\Playlist();
}
return $playlist_entity;
}
/**
* Overwrites EntityAPIController::save().
*/
public function save($playlist_entity, DatabaseTransaction $transaction = NULL) {
if (!isset($playlist_entity->client)) {
watchdog('brightcove', 'Saving playlist failed due to missing client property.', [], WATCHDOG_ERROR);
drupal_set_message(t('Saving playlist failed due to missing client property.'), 'error');
return;
}
$bcid = $playlist_entity->client->bcid;
brightcove_try(function () use ($playlist_entity, $bcid) {
/** @var \Brightcove\API\CMS $cms */
list($cms, ) = brightcove_create_classes($playlist_entity->client);
if (!empty($playlist_entity->bpid)) {
field_attach_update('brightcove_playlist', $playlist_entity);
$cms
->updatePlaylist($playlist_entity->playlist);
brightcove_invalidate_cache("brightcove:playlist:{$playlist_entity->playlist->getId()}:{$bcid}");
}
else {
field_attach_insert('brightcove_playlist', $playlist_entity);
$cms
->createPlaylist($playlist_entity->playlist);
}
$name = $playlist_entity->playlist
->getName();
watchdog('brightcove', 'Saving brightcove playlist "@name" was successful.', [
'@name' => $name,
], WATCHDOG_INFO);
drupal_set_message(t('Saving brightcove playlist "@name" was successful.', [
'@name' => $name,
]), 'status');
}, function (\Brightcove\API\Exception\APIException $ex) {
throw $ex;
});
brightcove_invalidate_cache('brightcove:playlist:list', TRUE);
}
/**
* Overwrites DrupalDefaultEntityController::load().
*
* @param array $ids
* Array of ids. The format for an id is "1234-5" where 1234 is
* a Playlist->getId() and 5 is a Client->bcid.
* @param $conditions
*
* @return \Brightcove\Object\Playlist[] An array of Playlist objects, keyed by the playlist id.
*/
public function load($ids = [], $conditions = []) {
$playlists = [];
$entity_info = entity_get_info('brightcove_playlist');
foreach ($ids as $id) {
$playlist_ids = explode('-', $id);
if (count($playlist_ids) != 2) {
continue;
}
$playlist_id = $playlist_ids[0];
$bcid = $playlist_ids[1];
$playlist = brightcove_load_playlist($playlist_id, $bcid);
if ($playlist) {
$values = [
$entity_info['entity keys']['id'] => $id,
'playlist_id' => $playlist_id,
$entity_info['entity keys']['label'] => $playlist
->getName(),
$entity_info['entity keys']['bundle'] => $playlist
->getType() == 'EXPLICIT' ? BRIGHTCOVE_PLAYLIST_TYPE_MANUAL : BRIGHTCOVE_PLAYLIST_TYPE_SMART,
'playlist' => $playlist,
'client' => brightcove_client_load($bcid),
];
$playlist_entity = entity_create('brightcove_playlist', $values);
$playlists[$id] = $playlist_entity;
}
}
field_attach_load('brightcove_playlist', $playlists);
return $playlists;
}
/**
* Overwrites DrupalDefaultEntityController::delete().
*/
public function delete($ids, DatabaseTransaction $transaction = NULL) {
$entities = $ids ? $this
->load($ids) : FALSE;
if (!$entities) {
// Do nothing in case invalid or no ids have been passed.
return;
}
foreach ($entities as $playlist_entity) {
/** @var \Brightcove\Object\Playlist $playlist */
$playlist = $playlist_entity->playlist;
brightcove_try(function () use ($playlist, $playlist_entity) {
/** @var \Brightcove\API\CMS $cms */
list($cms, ) = brightcove_create_classes($playlist_entity->client);
$cms
->deletePlaylist($playlist
->getId());
// deletePlaylist() fails silently in case the client has no permission to
// delete the playlist so we check here if it has been really removed or
// not.
try {
$cms
->getPlaylist($playlist
->getId());
watchdog('brightcove', 'Deleting the playlist @playlist has failed. Maybe the brightcove client has not enough permissions.', [
'@playlist' => $playlist
->getId(),
], WATCHDOG_ERROR);
drupal_set_message(t('Deleting the playlist @playlist has failed. Maybe the brightcove client has not enough permissions.', [
'@playlist' => $playlist
->getId(),
]), 'error');
} catch (Exception $e) {
// Invalidate playlist cache.
brightcove_invalidate_cache('brightcove:playlist', TRUE);
}
});
}
}
}