. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Statistics\Repository; use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Statistics\Google\ChartMedia; use Fisharebest\Webtrees\Statistics\Repository\Interfaces\MediaRepositoryInterface; use Fisharebest\Webtrees\Tree; use Illuminate\Database\Capsule\Manager as DB; use Illuminate\Database\Query\Builder; /** * A repository providing methods for media type related statistics. */ class MediaRepository implements MediaRepositoryInterface { /** * @var Tree */ private $tree; /** * Available media types. */ private const MEDIA_TYPE_ALL = 'all'; private const MEDIA_TYPE_AUDIO = 'audio'; private const MEDIA_TYPE_BOOK = 'book'; private const MEDIA_TYPE_CARD = 'card'; private const MEDIA_TYPE_CERTIFICATE = 'certificate'; private const MEDIA_TYPE_COAT = 'coat'; private const MEDIA_TYPE_DOCUMENT = 'document'; private const MEDIA_TYPE_ELECTRONIC = 'electronic'; private const MEDIA_TYPE_FICHE = 'fiche'; private const MEDIA_TYPE_FILM = 'film'; private const MEDIA_TYPE_MAGAZINE = 'magazine'; private const MEDIA_TYPE_MANUSCRIPT = 'manuscript'; private const MEDIA_TYPE_MAP = 'map'; private const MEDIA_TYPE_NEWSPAPER = 'newspaper'; private const MEDIA_TYPE_PAINTING = 'painting'; private const MEDIA_TYPE_PHOTO = 'photo'; private const MEDIA_TYPE_TOMBSTONE = 'tombstone'; private const MEDIA_TYPE_VIDEO = 'video'; private const MEDIA_TYPE_OTHER = 'other'; private const MEDIA_TYPE_UNKNOWN = 'unknown'; /** * List of GEDCOM media types. * * @var string[] */ private const MEDIA_TYPES = [ self::MEDIA_TYPE_AUDIO, self::MEDIA_TYPE_BOOK, self::MEDIA_TYPE_CARD, self::MEDIA_TYPE_CERTIFICATE, self::MEDIA_TYPE_COAT, self::MEDIA_TYPE_DOCUMENT, self::MEDIA_TYPE_ELECTRONIC, self::MEDIA_TYPE_FICHE, self::MEDIA_TYPE_FILM, self::MEDIA_TYPE_MAGAZINE, self::MEDIA_TYPE_MANUSCRIPT, self::MEDIA_TYPE_MAP, self::MEDIA_TYPE_NEWSPAPER, self::MEDIA_TYPE_PAINTING, self::MEDIA_TYPE_PHOTO, self::MEDIA_TYPE_TOMBSTONE, self::MEDIA_TYPE_VIDEO, self::MEDIA_TYPE_OTHER, ]; /** * Constructor. * * @param Tree $tree */ public function __construct(Tree $tree) { $this->tree = $tree; } /** * Returns the number of media records of the given type. * * @param string $type The media type to query * * @return int */ private function totalMediaTypeQuery(string $type): int { if (($type !== self::MEDIA_TYPE_ALL) && ($type !== self::MEDIA_TYPE_UNKNOWN) && !\in_array($type, self::MEDIA_TYPES, true) ) { return 0; } $query = DB::table('media') ->where('m_file', '=', $this->tree->id()); if ($type !== self::MEDIA_TYPE_ALL) { if ($type === self::MEDIA_TYPE_UNKNOWN) { // There has to be a better way then this :( foreach (self::MEDIA_TYPES as $t) { // Use function to add brackets $query->where(static function (Builder $query) use ($t): void { $query->where('m_gedcom', 'not like', '%3 TYPE ' . $t . '%') ->where('m_gedcom', 'not like', '%1 _TYPE ' . $t . '%'); }); } } else { // Use function to add brackets $query->where(static function (Builder $query) use ($type): void { $query->where('m_gedcom', 'like', '%3 TYPE ' . $type . '%') ->orWhere('m_gedcom', 'like', '%1 _TYPE ' . $type . '%'); }); } } return $query->count(); } /** * @inheritDoc */ public function totalMedia(): string { return I18N::number($this->totalMediaTypeQuery(self::MEDIA_TYPE_ALL)); } /** * @inheritDoc */ public function totalMediaAudio(): string { return I18N::number($this->totalMediaTypeQuery(self::MEDIA_TYPE_AUDIO)); } /** * @inheritDoc */ public function totalMediaBook(): string { return I18N::number($this->totalMediaTypeQuery(self::MEDIA_TYPE_BOOK)); } /** * @inheritDoc */ public function totalMediaCard(): string { return I18N::number($this->totalMediaTypeQuery(self::MEDIA_TYPE_CARD)); } /** * @inheritDoc */ public function totalMediaCertificate(): string { return I18N::number($this->totalMediaTypeQuery(self::MEDIA_TYPE_CERTIFICATE)); } /** * @inheritDoc */ public function totalMediaCoatOfArms(): string { return I18N::number($this->totalMediaTypeQuery(self::MEDIA_TYPE_COAT)); } /** * @inheritDoc */ public function totalMediaDocument(): string { return I18N::number($this->totalMediaTypeQuery(self::MEDIA_TYPE_DOCUMENT)); } /** * @inheritDoc */ public function totalMediaElectronic(): string { return I18N::number($this->totalMediaTypeQuery(self::MEDIA_TYPE_ELECTRONIC)); } /** * @inheritDoc */ public function totalMediaFiche(): string { return I18N::number($this->totalMediaTypeQuery(self::MEDIA_TYPE_FICHE)); } /** * @inheritDoc */ public function totalMediaFilm(): string { return I18N::number($this->totalMediaTypeQuery(self::MEDIA_TYPE_FILM)); } /** * @inheritDoc */ public function totalMediaMagazine(): string { return I18N::number($this->totalMediaTypeQuery(self::MEDIA_TYPE_MAGAZINE)); } /** * @inheritDoc */ public function totalMediaManuscript(): string { return I18N::number($this->totalMediaTypeQuery(self::MEDIA_TYPE_MANUSCRIPT)); } /** * @inheritDoc */ public function totalMediaMap(): string { return I18N::number($this->totalMediaTypeQuery(self::MEDIA_TYPE_MAP)); } /** * @inheritDoc */ public function totalMediaNewspaper(): string { return I18N::number($this->totalMediaTypeQuery(self::MEDIA_TYPE_NEWSPAPER)); } /** * @inheritDoc */ public function totalMediaPainting(): string { return I18N::number($this->totalMediaTypeQuery(self::MEDIA_TYPE_PAINTING)); } /** * @inheritDoc */ public function totalMediaPhoto(): string { return I18N::number($this->totalMediaTypeQuery(self::MEDIA_TYPE_PHOTO)); } /** * @inheritDoc */ public function totalMediaTombstone(): string { return I18N::number($this->totalMediaTypeQuery(self::MEDIA_TYPE_TOMBSTONE)); } /** * @inheritDoc */ public function totalMediaVideo(): string { return I18N::number($this->totalMediaTypeQuery(self::MEDIA_TYPE_VIDEO)); } /** * @inheritDoc */ public function totalMediaOther(): string { return I18N::number($this->totalMediaTypeQuery(self::MEDIA_TYPE_OTHER)); } /** * @inheritDoc */ public function totalMediaUnknown(): string { return I18N::number($this->totalMediaTypeQuery(self::MEDIA_TYPE_UNKNOWN)); } /** * Returns a sorted list of media types and their total counts. * * @param int $tot The total number of media files * * @return array */ private function getSortedMediaTypeList(int $tot): array { $media = []; $c = 0; $max = 0; foreach (self::MEDIA_TYPES as $type) { $count = $this->totalMediaTypeQuery($type); if ($count > 0) { $media[$type] = $count; if ($count > $max) { $max = $count; } $c += $count; } } $count = $this->totalMediaTypeQuery(self::MEDIA_TYPE_UNKNOWN); if ($count > 0) { $media[self::MEDIA_TYPE_UNKNOWN] = $tot - $c; if ($tot - $c > $max) { $max = $count; } } if (\count($media) > 10 && ($max / $tot) > 0.6) { arsort($media); $media = \array_slice($media, 0, 10); $c = $tot; foreach ($media as $cm) { $c -= $cm; } if (isset($media[self::MEDIA_TYPE_OTHER])) { $media[self::MEDIA_TYPE_OTHER] += $c; } else { $media[self::MEDIA_TYPE_OTHER] = $c; } } asort($media); return $media; } /** * @inheritDoc */ public function chartMedia(string $color_from = null, string $color_to = null): string { $tot = $this->totalMediaTypeQuery(self::MEDIA_TYPE_ALL); $media = $this->getSortedMediaTypeList($tot); return (new ChartMedia()) ->chartMedia($media, $color_from, $color_to); } }