EntityAlbumUtility.php
<?php
Package::Load( 'RiderHelp.REsort' );
/**
* EntityAlbumUtility
* @package Panda
* @subpackage Samples
* @author Sergeyfast
*/
class EntityAlbumUtility {
/**
* Prepare Album Data for Template
* @static
* @param Entity $entity
* @return array
*/
public static function PrepareAlbumsData( Entity $entity ) {
$result = array();
if ( $entity->albums === null ) {
$entity->albums = array();
}
foreach( $entity->albums as $album ) {
$photos = array();
if ( $album->photos === null ) {
$album->photos = array();
}
$index = 0;
foreach( $album->photos as $photo ) {
$photoItem = array(
'id' => $photo->entityPhotoId
, 'title' => $photo->title
, 'num' => $index ++
);
if ( !empty( $photo->bigImageId ) ) {
$photoItem['bigImage'] = array(
'id' => $photo->bigImage->fileId
, 'name' => $photo->bigImage->title
, 'src' => $photo->bigImage->path
);
}
if ( !empty( $photo->smallImageId ) ) {
$photoItem['smallImage'] = array(
'id' => $photo->smallImage->fileId
, 'name' => $photo->smallImage->title
, 'src' => $photo->smallImage->path
);
}
$photos[] = $photoItem;
}
$result[] = array(
'id' => $album->entityAlbumId
, 'title' => $album->title
, 'description' => $album->description
, 'photos' => $photos
);
}
return $result;
}
/**
* Save Albums to Database
* @static
* @param \Entity $entity
* @param \Entity $originalEntity with lists and photos
* @return bool
*/
public static function SaveAlbums( Entity $entity, Entity $originalEntity = null ) {
$result = true;
$albumOrderNumber = 0;
$originalAlbumIds = !empty( $originalEntity->albums ) ? ArrayHelper::GetObjectsFieldValues( $originalEntity->albums, array( 'entityAlbumId' ) ) : array();
foreach( $entity->albums as $album ) {
$album->entityId = $entity->entityId;
$album->statusId = 1;
$album->orderNumber = $albumOrderNumber ++;
if ( empty( $album->entityAlbumId ) ) {
$result = EntityAlbumFactory::Add( $album, array( BaseFactory::WithReturningKeys => true ) );
} else {
$result = EntityAlbumFactory::Update( $album );
// remove album
if ( isset( $originalAlbumIds[$album->entityAlbumId ] ) ) {
unset( $originalAlbumIds[ $album->entityAlbumId ] );
}
}
if ( !empty( $album->photos ) ) {
$photoOrderNumber = 0;
foreach( $album->photos as $photo ) {
$photo->entityAlbumId = $album->entityAlbumId;
$photo->orderNumber = $photoOrderNumber ++;
$photo->statusId = 1;
}
}
$originalPhotos = array();
if ( !empty( $originalEntity )
&& !empty( $originalEntity->albums )
&& !empty( $originalEntity->albums[$album->entityAlbumId] )
&& !empty( $originalEntity->albums[$album->entityAlbumId]->photos ) )
{
$originalPhotos = $originalEntity->albums[$album->entityAlbumId]->photos;
}
$result = $result && EntityPhotoFactory::SaveArray( $album->photos, $originalPhotos );
}
// delete old albums
if ( !empty( $originalAlbumIds ) ) {
$a = new EntityAlbum();
$a->statusId = 3;
EntityAlbumFactory::UpdateByMask( $a, array( 'statusId' ), array( '_entityAlbumId' => $originalAlbumIds ) );
}
return $result;
}
/**
* Reformat Errors Array
* @static
* @param $errors
* @param $unusedFields
* @return array
*/
private static function filterUnusedErrorFields( $errors, $unusedFields ) {
$result = array();
if ( !empty( $errors['fields'] ) ) {
foreach( $unusedFields as $unused ) {
if ( !empty( $errors['fields'][$unused] ) ) {
unset( $errors['fields'][$unused] );
}
}
$result = !empty( $errors['fields'] ) ? $errors['fields'] : array();
return $result;
}
return $result;
}
/**
* Validate Albums
* @static
* @param EntityAlbum[] $albums
* @return array
*/
public static function ValidateAlbums( $albums ) {
$result = array();
if ( empty( $albums ) ) {
return $result;
}
$unusedAlbumFields = array( 'entityId', 'orderNumber', 'statusId' );
$unusedPhotoFields = array( 'entityAlbumId', 'orderNumber', 'statusId' );
foreach( $albums as $albumIndex => $album ) {
$errors = EntityAlbumFactory::Validate( $album );
$errors = self::filterUnusedErrorFields( $errors, $unusedAlbumFields );
if ( ! empty( $errors ) ) {
$result[$albumIndex] = $errors;
}
if ( !empty( $album->photos ) ) {
foreach( $album->photos as $photoIndex => $photo ) {
$errors = EntityPhotoFactory::Validate( $photo );
$errors = self::filterUnusedErrorFields( $errors, $unusedPhotoFields );
if ( ! empty( $errors ) ) {
$result[$albumIndex]['photos'][$photoIndex] = $errors;
}
}
} else {
$result[$albumIndex]['photos'] = 'empty';
}
}
return $result;
}
}
?>