Skip to content

Commit

Permalink
Merge pull request #29 from akrys/feature/27-Bildtausch
Browse files Browse the repository at this point in the history
Feature/27 bildtausch
+ bugfixes 
+ Vorbereitung auf PHP8
  • Loading branch information
akrys authored Nov 2, 2023
2 parents d4ec31e + 9ee239d commit 56ab975
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 32 deletions.
5 changes: 3 additions & 2 deletions lib/Cli/Read.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
namespace FriendsOfRedaxo\addon\MediapoolExif\Cli;

use FriendsOfRedaxo\addon\MediapoolExif\Enum\MediaFetchMode;
use FriendsOfRedaxo\addon\MediapoolExif\Exif;
use FriendsOfRedaxo\addon\MediapoolExif\MediapoolExif;
use rex_console_command;
Expand Down Expand Up @@ -50,10 +51,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
$updateAll = $input->getOption('all');
$updateSilent = $input->getOption('silent');

$mode = Exif::GETMEDIA_MODE_NULL_ONLY;
$mode = MediaFetchMode::NULL_ONLY;
if ($updateAll) {
if ($updateSilent || $io->confirm('You are going to update all files. Are you sure?', false)) {
$mode = Exif::GETMEDIA_MODE_ALL;
$mode = MediaFetchMode::ALL;
}
}
$files = Exif::getMediaToRead($mode);
Expand Down
25 changes: 25 additions & 0 deletions lib/Enum/Format.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
*/
namespace FriendsOfRedaxo\addon\MediapoolExif\Enum;

/**
* Datei für ...
*
* @version 1.0 / 2023-11-02
* @author akrys
*/

/**
* Description of Format
*
* @author akrys
*/
class /* enum */ Format /* :string */
{
const /*case*/ RAW = 'numeric';
const /*case*/ READABLE = 'readable';
}
26 changes: 26 additions & 0 deletions lib/Enum/MediaFetchMode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
*/

namespace FriendsOfRedaxo\addon\MediapoolExif\Enum;

/**
* Datei für ...
*
* @version 1.0 / 2023-11-02
* @author akrys
*/

/**
* Description of MediaFetchMode
*
* @author akrys
*/
class /*enum*/ MediaFetchMode /*:int*/
{
const /*case*/ NULL_ONLY = 1000;
const /*case*/ ALL = 1001;
}
31 changes: 31 additions & 0 deletions lib/Enum/ReturnMode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
*/

namespace FriendsOfRedaxo\addon\MediapoolExif\Enum;

/**
* Datei für ...
*
* @version 1.0 / 2023-11-02
* @author akrys
*/

/**
* Description of Mode
*
* @author akrys
*/
class /*enum*/ ReturnMode /*:int*/
{
const /*case*/ THROW_EXCEPTION = 1000; //should be default
const /*case*/ RETURN_NULL = 1001;
const /*case*/ RETURN_FALSE = 1002;
const /*case*/ RETURN_ZERO = 1003;
const /*case*/ RETURN_MINUS = 1004;
const /*case*/ RETURN_EMPTY_STRING = 1005;
const /*case*/ RETURN_EMPTY_ARRAY = 1006;
}
17 changes: 13 additions & 4 deletions lib/Exif.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,23 @@
*/
class Exif
{
/** @deprecated use FriendsOfRedaxo\addon\MediapoolExi\Enum\Mode::THROW_EXCEPTION */
const MODE_THROW_EXCEPTION = 1000; //should be default
/** @deprecated use FriendsOfRedaxo\addon\MediapoolExi\Enum\Mode::RETURN_NULL */
const MODE_RETURN_NULL = 1001;
/** @deprecated use FriendsOfRedaxo\addon\MediapoolExi\Enum\Mode::RETURN_FALSE */
const MODE_RETURN_FALSE = 1002;
/** @deprecated use FriendsOfRedaxo\addon\MediapoolExi\Enum\Mode::RETURN_ZERO */
const MODE_RETURN_ZERO = 1003;
/** @deprecated use FriendsOfRedaxo\addon\MediapoolExi\Enum\Mode::RETURN_MINUS */
const MODE_RETURN_MINUS = 1004;
/** @deprecated use FriendsOfRedaxo\addon\MediapoolExi\Enum\Mode::RETURN_EMPTY_STRING */
const MODE_RETURN_EMPTY_STRING = 1005;
/** @deprecated use FriendsOfRedaxo\addon\MediapoolExi\Enum\Mode::RETURN_EMPTY_ARRAY */
const MODE_RETURN_EMPTY_ARRAY = 1006;
/** @deprecated use FriendsOfRedaxo\addon\MediapoolExi\Enum\MediaFetchMode::NULL_ONLY */
const GETMEDIA_MODE_NULL_ONLY = 1000;
/** @deprecated use FriendsOfRedaxo\addon\MediapoolExi\Enum\MediaFetchMode::All */
const GETMEDIA_MODE_ALL = 1001;

/**
Expand All @@ -35,7 +44,7 @@ class Exif
* @param \FriendsOfRedaxo\addon\MediapoolExif\rex_media $media
* @return ExifData
*/
public static function get(rex_media $media, $mode = self::MODE_THROW_EXCEPTION): ExifData
public static function get(rex_media $media, /*Enum\Mode*/ $mode = Enum\ReturnMode::THROW_EXCEPTION): ExifData
{
return new ExifData($media, $mode);
}
Expand All @@ -45,15 +54,15 @@ public static function get(rex_media $media, $mode = self::MODE_THROW_EXCEPTION)
* @param type $mode
* @return array
*/
public static function getMediaToRead($mode = self::GETMEDIA_MODE_NULL_ONLY): array
public static function getMediaToRead(/*Enum\MediaFetchMode*/ $mode = Enum\MediaFetchMode::NULL_ONLY): array
{
$rexSQL = rex_sql::factory();

switch ($mode) {
case self::GETMEDIA_MODE_ALL:
case Enum\MediaFetchMode::ALL:
$sql = 'select filename from rex_media';
break;
case self::GETMEDIA_MODE_NULL_ONLY:
case Enum\MediaFetchMode::NULL_ONLY:
default:
$sql = 'select filename from rex_media where exif is null';
break;
Expand Down
22 changes: 13 additions & 9 deletions lib/ExifData.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
namespace FriendsOfRedaxo\addon\MediapoolExif;

use Exception;
use FriendsOfRedaxo\addon\MediapoolExif\Enum\ReturnMode;
use FriendsOfRedaxo\addon\MediapoolExif\Exception\NotFoundException;
use FriendsOfRedaxo\addon\MediapoolExif\Exif;
use FriendsOfRedaxo\addon\MediapoolExif\Format\FormatInterface;
use rex_media;

Expand Down Expand Up @@ -71,8 +71,12 @@ public function __construct(rex_media $media, int $mode = null)
{
$this->media = $media;
$this->exif = json_decode($this->media->getValue('exif'), true);
if (!$this->exif) {
$this->exif = [];
}

if ($mode === null) {
$mode = Exif::MODE_THROW_EXCEPTION;
$mode = ReturnMode::THROW_EXCEPTION;
}
$this->mode = $mode;
}
Expand Down Expand Up @@ -127,25 +131,25 @@ private function handleExcption(Exception $exception)
$return = '';

switch ($this->mode) {
case Exif::MODE_RETURN_NULL:
case ReturnMode::RETURN_NULL:
$return = null;
break;
case Exif::MODE_RETURN_FALSE:
case ReturnMode::RETURN_FALSE:
$return = false;
break;
case Exif::MODE_RETURN_ZERO:
case ReturnMode::RETURN_ZERO:
$return = 0;
break;
case Exif::MODE_RETURN_MINUS:
case ReturnMode::RETURN_MINUS:
$return = -1;
break;
case Exif::MODE_RETURN_EMPTY_STRING:
case ReturnMode::RETURN_EMPTY_STRING:
$return = '';
break;
case Exif::MODE_RETURN_EMPTY_ARRAY:
case ReturnMode::RETURN_EMPTY_ARRAY:
$return = [];
break;
case Exif::MODE_THROW_EXCEPTION:
case ReturnMode::THROW_EXCEPTION:
default:
throw $exception;
}
Expand Down
9 changes: 6 additions & 3 deletions lib/Format/Camera.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace FriendsOfRedaxo\addon\MediapoolExif\Format;

use Exception;
use FriendsOfRedaxo\addon\MediapoolExif\Enum\Format;
use FriendsOfRedaxo\addon\MediapoolExif\Exception\InvalidFormatExcption;
use FriendsOfRedaxo\addon\MediapoolExif\Format\Camera\Aperture;
use FriendsOfRedaxo\addon\MediapoolExif\Format\Camera\Exposure;
Expand All @@ -21,10 +22,12 @@
*
* @author akrys
*/
class Camera
extends FormatInterface
class Camera extends FormatInterface
{
/** @deprecated use \FriendsOfRedaxo\addon\MediapoolExif\Enum\Format::RAW */
const TYPE_NUMERIC = 'numeric';

/** @deprecated use \FriendsOfRedaxo\addon\MediapoolExif\Enum\Format::READABLE */
const TYPE_READABLE = 'readable';

/**
Expand All @@ -41,7 +44,7 @@ public function format()

$format = $this->format;
if ($format === null) {
$format = self::TYPE_READABLE;
$format = Format::READABLE;
}

if (is_callable([$this, $format])) {
Expand Down
6 changes: 3 additions & 3 deletions lib/Format/Camera/Aperture.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace FriendsOfRedaxo\addon\MediapoolExif\Format\Camera;

use Exception;
use FriendsOfRedaxo\addon\MediapoolExif\Format\Camera;
use FriendsOfRedaxo\addon\MediapoolExif\Enum\Format;
use FriendsOfRedaxo\addon\MediapoolExif\Format\FormatInterface;

/**
Expand All @@ -34,10 +34,10 @@ public function format()

$data = explode('/', $this->data['FNumber']);
switch ($this->format) {
case Camera::TYPE_READABLE:
case Format::READABLE:
return 'f/'.number_format((float) $data[0] / (float) $data[1], 1);
break;
case Camera::TYPE_NUMERIC:
case Format::RAW:
return number_format((float) $data[0] / (float) $data[1], 1);
break;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Format/Camera/Exposure.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace FriendsOfRedaxo\addon\MediapoolExif\Format\Camera;

use Exception;
use FriendsOfRedaxo\addon\MediapoolExif\Format\Camera;
use FriendsOfRedaxo\addon\MediapoolExif\Enum\Format;
use FriendsOfRedaxo\addon\MediapoolExif\Format\FormatInterface;

/**
Expand All @@ -33,10 +33,10 @@ class Exposure

$data = explode('/', $this->data['ExposureTime']);
switch ($this->format) {
case Camera::TYPE_READABLE:
case Format::READABLE:
return $data[0].'/'.$data[1].' s';
break;
case Camera::TYPE_NUMERIC:
case Format::RAW:
return (float) $data[0] / (float) $data[1];
break;
}
Expand Down
9 changes: 4 additions & 5 deletions lib/Format/Camera/Length.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace FriendsOfRedaxo\addon\MediapoolExif\Format\Camera;

use Exception;
use FriendsOfRedaxo\addon\MediapoolExif\Format\Camera;
use FriendsOfRedaxo\addon\MediapoolExif\Enum\Format;
use FriendsOfRedaxo\addon\MediapoolExif\Format\FormatInterface;

/**
Expand All @@ -23,8 +23,7 @@
*
* @author akrys
*/
class Length
extends FormatInterface
class Length extends FormatInterface
{

/**
Expand All @@ -41,10 +40,10 @@ public function format()
$data = explode('/', $this->data['FocalLength']);
$value = (float) $data[0] / (float) $data[1];
switch ($this->format) {
case Camera::TYPE_READABLE:
case Format::READABLE:
return $value.' mm';
break;
case Camera::TYPE_NUMERIC:
case Format::RAW:
return $value;
break;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Format/FormatInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ abstract class FormatInterface
* @param array $data
* @param string $format
*/
public function __construct(array $data, string $format = null)
public function __construct(array $data, string /*\FriendsOfRedaxo\addon\MediapoolExif\Enum\Format*/ $format = null)
{
$this->data = $data;
$this->format = $format;
Expand Down Expand Up @@ -71,7 +71,7 @@ public function __construct(array $data, string $format = null)
* @return \FriendsOfRedaxo\addon\MediapoolExif\Format\className
* @throws InvalidFormatExcption
*/
public static function get($data, $type, $format = null): FormatInterface
public static function get($data, $type, /*\FriendsOfRedaxo\addon\MediapoolExif\Enum\Format*/ $format = null): FormatInterface
{
$className = __NAMESPACE__.'\\'.ucfirst($type);
if (class_exists($className)) {
Expand Down
31 changes: 31 additions & 0 deletions lib/MediapoolExif.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class MediapoolExif
*/
public static function processUploadedMedia(rex_extension_point $ep)
{
$oldMedia = rex_media::get($ep->getParam('filename'));
if ($data = static::getDataByFilename($ep->getParam('filename'))) {
$qry = "SELECT * FROM `".rex::getTablePrefix()."media` WHERE `filename` = '".$ep->getParam('filename')."'";
$sql = rex_sql::factory();
Expand Down Expand Up @@ -81,6 +82,10 @@ public static function processUploadedMedia(rex_extension_point $ep)
}
unset($field, $value, $key);

if (self::exifHasChanged($data['exif'], $oldMedia->getValue('exif'))) {
$update['exif'] = '`exif`='.$sql->escape($data['exif']);
}

if (!empty($update)) {
$qry = "UPDATE `".rex::getTablePrefix()."media` SET ".join(", ", array_values($update))." WHERE `filename` = '".$ep->getParam('filename')."'";

Expand All @@ -105,6 +110,32 @@ public static function processUploadedMedia(rex_extension_point $ep)
unset($data);
}

/**
* Check ob die EXIF-Daten sich geändert haben.
*
* @param string $new
* @param string|null $old
* @return bool
*/
private static function exifHasChanged(string $new, ?string $old): bool
{
$newArray = json_decode($new, true);
$oldArray = json_decode($old, true);

/**
* FileDateTime ändert sich immer.
* Hieße: wir ändern das exif-Feld, obwohl sich nichts relevantes geändert hat.
*/
unset(
$newArray['FileDateTime'], $oldArray['FileDateTime']
);

$newString = json_encode($newArray);
$oldString = json_encode($oldArray);

return $newString !== $oldString;
}

/**
* Daten au der Datei holen
* @param string $filename
Expand Down
2 changes: 1 addition & 1 deletion package.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package: mediapool_exif
version: '2.0.1'
version: '2.1.0'
author: 'FriendsOfREDAXO'
supportpage: https://github.com/FriendsOfREDAXO/mediapool_exif

Expand Down

0 comments on commit 56ab975

Please sign in to comment.