157 lines
4.4 KiB
PHP
157 lines
4.4 KiB
PHP
<?php
|
|
|
|
class MediaHandler{
|
|
|
|
|
|
/**
|
|
*
|
|
* @param HasMediaInterface $news
|
|
* @param MediaInterface $media
|
|
*
|
|
* Ritorna un array coi le reference dei media della news
|
|
*/
|
|
public static function getMedias(HasMediaInterface $model){
|
|
$newsReference = $model->getMediaReferences();
|
|
$rval = array();
|
|
// var_dump($newsReference);
|
|
$rval = GlobalVariables::get("dao")->query("MediaModel",array('id'=>array('$in'=>$newsReference)),array());
|
|
// var_dump($rval);
|
|
return $rval;
|
|
}
|
|
|
|
|
|
/**
|
|
* Aggiunge il media al model e controlla se il model ha un main media e se non lo ha glielo assegna
|
|
*
|
|
* @param HasMediaInterface $model
|
|
* @param MediaInterface $media
|
|
* @throws MediaException
|
|
*/
|
|
public static function addMedia(HasMediaInterface $model, MediaInterface $media){
|
|
if(!$model->hasProperty("id")){
|
|
throw new MediaException("Invalid HasMediaInterface passed to MediaHandler::addMedia (no property ID)");
|
|
}
|
|
if(!$media->hasProperty("id")){
|
|
throw new MediaException("Invalid MediaInterface passed to MediaHandler::addMedia (no property ID)");
|
|
}
|
|
|
|
try{
|
|
$mediaArr = array();
|
|
$mediaArr = $model->getMediaReferences();
|
|
if(!in_array($media->getReference(), $mediaArr)){
|
|
$mediaArr[] = $media->getReference();
|
|
$model->setMediaReferences($mediaArr);
|
|
|
|
GlobalVariables::get("dao")->save($model);
|
|
|
|
MediaHandler::setOwnerToMedia($model, $media);
|
|
// $media->setOwnerReference($model);
|
|
GlobalVariables::get("dao")->save($media);
|
|
}
|
|
}catch(CoreException $ex){
|
|
throw new MediaException("Error while saving in MediaHandler::addMedia (".$ex->getMessage().")");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Elimina l'id del media dal model e deleta il media
|
|
*
|
|
* @param HasMediaInterface $model
|
|
* @param MediaInterface $media
|
|
* @throws MediaException
|
|
*/
|
|
public static function deleteMedia(HasMediaInterface $model, MediaInterface $media){
|
|
try{
|
|
|
|
$mediaArr = array();
|
|
$mediaArr = $model->getMediaReferences();
|
|
$mediaId = $media->getReference();
|
|
|
|
// var_dump($mediaArr);
|
|
if(sizeof($mediaArr)>0){
|
|
$key = array_search($mediaId, $mediaArr);
|
|
if($key !== false){
|
|
unset($mediaArr[$key]);
|
|
$model->setMediaReferences($mediaArr);
|
|
|
|
if($model->getMainMediaReference() == $mediaId){
|
|
// echo "UGUALE!";
|
|
$model->setMainMedia(null);
|
|
}
|
|
|
|
GlobalVariables::get("dao")->save($model);
|
|
|
|
GlobalVariables::get("dao")->delete($media);
|
|
}
|
|
else{
|
|
throw new MediaException("Trying to delete invalid media from a model in MediaHandler::deleteMedia");
|
|
}
|
|
}
|
|
else{
|
|
throw new MediaException("This HasMediaInterface has no media in it '".$model->id."' sizeof on mediaReference failed in MediaHandler::deleteMedia");
|
|
}
|
|
|
|
}catch(CoreException $ex){
|
|
throw new MediaException("Error while deleting in MediaHandler::deleteMedia (".$ex->getMessage().")");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Setta l'owner al MediaModel
|
|
* @param HasMediaInterface $model
|
|
* @param MediaModel $media
|
|
*/
|
|
public static function setOwnerToMedia(HasMediaInterface $model, MediaModel $media){
|
|
$media->setOwnerReference($model);
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param MediaInterface $media
|
|
* @throws MediaException
|
|
* @return DaoSaveable
|
|
*/
|
|
public static function getMediaOwnerFromMedia(MediaInterface $media){
|
|
$ownerRef = $media->getOwnerReference();
|
|
try{
|
|
$owner = GlobalVariables::get("dao")->getFirst($ownerRef->class,array("id"=>$ownerRef->id));
|
|
return $owner;
|
|
}catch(CoreException $ex){
|
|
throw new MediaException("Invalid Media Reference trying get Media Owner in MediaHandler::getMEdiaOwner");
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param HasMediaInterface $model
|
|
* @param MediaInterface $media
|
|
* @throws MediaException
|
|
*/
|
|
public static function setMainMediaToModel(HasMediaInterface $model, MediaInterface $media){
|
|
try{
|
|
$model->setMainMedia($media);
|
|
GlobalVariables::get("dao")->save($model);
|
|
}catch(CoreException $ex){
|
|
throw new MediaException("Error while trying to save an HasMediaInterface in ModelHandler::setMainMedia");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Prende in ingresso un HasMediaInterface ritorna un array di un elemento array("mainMedia"=>"&value");
|
|
*
|
|
* @param HasMediaInterface $model
|
|
* @return MediaModel
|
|
*/
|
|
public static function getMainMediaFromModel(HasMediaInterface $model){
|
|
$mainMedia = $model->getMainMediaReference();
|
|
$tmp = null;
|
|
if(!is_null($mainMedia)){
|
|
$tmp = GlobalVariables::get("dao")->getFirst("MediaModel",array("id"=>$mainMedia));
|
|
}
|
|
// var_dump($tmp);
|
|
return $tmp;
|
|
}
|
|
|
|
}
|
|
|
|
?>
|