aggiunto metodo per salvare i media con file associato

correzioni varie al dao/model quando usano campo id/_id
This commit is contained in:
Riccardo Di Dato
2016-01-14 16:58:19 +01:00
parent 8063a4e72d
commit f226de3b31
8 changed files with 117 additions and 18 deletions
@@ -16,8 +16,10 @@ abstract class DaoSaveableDefaultImplementation implements DaoSaveable {
} }
public function __set($attr,$value){ public function __set($attr,$value){
if (strcmp($attr,"id")==0){ if (strcmp($attr,"id")==0 || strcmp($attr,"_id")==0){
$attr = "_id"; if (strcmp($attr,"id")==0){
$attr = "_id";
}
if (!($value instanceof MongoId)){ if (!($value instanceof MongoId)){
$value = new MongoId($value); $value = new MongoId($value);
} }
@@ -26,10 +28,23 @@ abstract class DaoSaveableDefaultImplementation implements DaoSaveable {
} }
public function &__get($attr){ public function &__get($attr){
if (strcmp($attr,"id")==0){ $rval = null;
$attr = "_id"; if (strcmp($attr,"id")==0 || strcmp($attr,"_id")==0){
if (strcmp($attr,"id")==0){
$attr = "_id";
}
if ($this->hasProperty($attr)){
$idf = '$id';
$rval = &$this->saveableObject->$attr->$idf;
}
else {
$rval = null;
}
} }
$rval = &$this->saveableObject->$attr; else {
$rval = &$this->saveableObject->$attr;
}
return $rval; return $rval;
} }
+4
View File
@@ -58,6 +58,10 @@ class GenericDao{
unset($useFilter["id"]); unset($useFilter["id"]);
} }
if (array_key_exists("_id",$useFilter) && is_string($useFilter["_id"])){
$useFilter["_id"] = new MongoId($useFilter["_id"]);
}
$cursor = $collection->find($useFilter); $cursor = $collection->find($useFilter);
if (array_key_exists("sort",$options)){ if (array_key_exists("sort",$options)){
@@ -1,7 +1,6 @@
<?php <?php
class DaoMediaHandler implements MediaHandlerInterface{ class DaoMediaHandler implements MediaHandlerInterface{
/** /**
* *
@@ -152,6 +151,17 @@ class DaoMediaHandler implements MediaHandlerInterface{
return $tmp; return $tmp;
} }
public static function createNewFileBasedMedia($saveableObj, $filePath){
$basepath = GlobalVariables::get("config")->paths->media;
$info = pathinfo($filePath);
$media = new MediaModel($saveableObj);
$media->extension = $info["extension"];
GlobalVariables::get("dao")->save($media);
SFSManager::copyFile($filePath, $basepath."/".$media->id.".".$media->extension);
return $media;
}
} }
?> ?>
+16 -4
View File
@@ -21,22 +21,31 @@ class MediaModel extends Model implements MediaInterface{
public function __construct($saveableObject = null){ public function __construct($saveableObject = null){
parent::__construct($saveableObject); parent::__construct($saveableObject);
$this->setMediaRenderer();
}
public function setMediaRenderer(){
if($this->hasProperty("mediaType")){ if($this->hasProperty("mediaType")){
if ($this->mediaType == self::TYPE_HTML5){ if ($this->mediaType == self::TYPE_HTML5){
$this->mediaRenderer = new MediaHtml5($this); if ($this->hasProperty("code")){
$this->mediaRenderer = new MediaHtml5($this->code);
}
else {
throw new MediaException("Media of type 'HTML5' has no code field");
}
} }
if ($this->mediaType == self::TYPE_LOCALVIDEO){ if ($this->mediaType == self::TYPE_LOCALVIDEO){
$this->mediaRenderer = new MediaLocalVideo($this); $this->mediaRenderer = new MediaLocalVideo($this);
} }
if ($this->mediaType == self::TYPE_IMAGE){ if ($this->mediaType == self::TYPE_IMAGE){
$this->mediaRenderer = new MediaImage($this); $this->mediaRenderer = new MediaImage($this->id);
} }
if ($this->mediaType == self::TYPE_PDF){ if ($this->mediaType == self::TYPE_PDF){
$this->mediaRenderer = new MediaPdf($this); $this->mediaRenderer = new MediaPdf($this);
} }
if ($this->mediaType == self::TYPE_YOUTUBE){ if ($this->mediaType == self::TYPE_YOUTUBE){
$this->mediaRenderer = new MediaYoutube($this); $this->mediaRenderer = new MediaYoutube($this);
} }
} }
else{ else{
throw new MediaException("Missing mediatype for Media in MediaModel construct"); throw new MediaException("Missing mediatype for Media in MediaModel construct");
@@ -72,6 +81,9 @@ class MediaModel extends Model implements MediaInterface{
return $this->id; return $this->id;
} }
public function updateFromSaveableObj($saveableObject){
parent::updateFromSaveableObj($saveableObject);
$this->setMediaRenderer();
}
} }
?> ?>
@@ -1,14 +1,14 @@
<?php <?php
class MediaHtml5 implements MediaRendererInterface{ class MediaHtml5 implements MediaRendererInterface{
private $modelRef; private $code;
public function __construct(MediaModel $model){ public function __construct($code){
$this->modelRef = $model; $this->code = $code;
} }
public function asText(){ public function asText(){
return $this->modelRef->code; return $this->code;
} }
public function render(){ public function render(){
@@ -1,13 +1,15 @@
<?php <?php
class MediaImage implements MediaRendererInterface{ class MediaImage implements MediaRendererInterface{
private $modelRef; private $id;
public function __construct(MediaModel $model){ public function __construct($id){
$this->modelRef = $model; $this->id = $id;
} }
public function asText(){ public function asText(){
return $this->modelRef->path; return '<a href="'.GUIHandler::getBaseUrl().'getImage.php?id='.$this->id.'&size=original">'.
'<img src="'.GUIHandler::getBaseUrl().'getImage.php?id='.$this->id.'&size=small"/>'.
'</a>';
} }
public function render(){ public function render(){
+54
View File
@@ -0,0 +1,54 @@
<?php
/*
Author: Riccardo Di Dato
Creation Date: 14/gen/2016
*/
require_once(dirname(__FILE__)."/load.php");
if (array_key_exists("id",$_GET)) {
if (array_key_exists('size',$_GET)){
$size=$_GET['size'];
}
else {
$size = "small";
}
$img = GlobalVariables::get("dao")->getFirst("MediaModel",array("id"=>$_GET['id']));
if (!is_null($img)){
$basepath = GlobalVariables::get("config")->paths->media;
$thumb = new Imagick($basepath."/".$img->id.".".$img->extension);
if ($size == "small"){
$thumb->thumbnailimage(0, 100);
}
header('Content-type: image/png');
echo $thumb;
// switch ($size){
// case "big":
// $img = ThumbnailGenerator::renderBigThumbnail($content);
// break;
// case "med":
// $img = ThumbnailGenerator::renderMediumThumbnail($content);
// break;
// case "small":
// $img = ThumbnailGenerator::renderSmallThumbnail($content);
// break;
// case "mini":
// $img = ThumbnailGenerator::renderMiniThumbnail($content);
// break;
// case "topbar":
// header("Cache-Control: no-cache, must-revalidate");
// $img = ThumbnailGenerator::renderThumbnail($content,150,0);
// break;
// default:
// $img = ThumbnailGenerator::renderOriginal($content);
// break;
// }
}
}
?>
+2
View File
@@ -57,6 +57,8 @@ try {
DaoMediaHandler::deleteMedia($news,$media2); DaoMediaHandler::deleteMedia($news,$media2);
echo "<br><br>"; echo "<br><br>";
var_dump($news); var_dump($news);
// var_dump($media->id);
// echo "<br><br>"; // echo "<br><br>";
// var_dump($media); // var_dump($media);