first commit
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
/**
|
||||
* @file classes/highlight/Collector.php
|
||||
*
|
||||
* Copyright (c) 2014-2023 Simon Fraser University
|
||||
* Copyright (c) 2000-2023 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class Collector
|
||||
*
|
||||
* @brief A helper class to configure a Query Builder to get a collection of highlights
|
||||
*/
|
||||
|
||||
namespace PKP\highlight;
|
||||
|
||||
use APP\facades\Repo;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\LazyCollection;
|
||||
use PKP\core\interfaces\CollectorInterface;
|
||||
use PKP\plugins\Hook;
|
||||
|
||||
/**
|
||||
* @template T of Highlight
|
||||
*/
|
||||
class Collector implements CollectorInterface
|
||||
{
|
||||
public const SITE_ONLY = 'site';
|
||||
public const SITE_AND_CONTEXTS = 'all';
|
||||
|
||||
public DAO $dao;
|
||||
public ?array $contextIds = null;
|
||||
public ?string $includeSite = null;
|
||||
public ?int $count = null;
|
||||
public ?int $offset = null;
|
||||
|
||||
public function __construct(DAO $dao)
|
||||
{
|
||||
$this->dao = $dao;
|
||||
}
|
||||
|
||||
/** @copydoc DAO::getCount() */
|
||||
public function getCount(): int
|
||||
{
|
||||
return $this->dao->getCount($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc DAO::getIds()
|
||||
* @return Collection<int,int>
|
||||
*/
|
||||
public function getIds(): Collection
|
||||
{
|
||||
return $this->dao->getIds($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc DAO::getMany()
|
||||
* @return LazyCollection<int,T>
|
||||
*/
|
||||
public function getMany(): LazyCollection
|
||||
{
|
||||
return $this->dao->getMany($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @copydoc Repository::deleteMany()
|
||||
*/
|
||||
public function deleteMany(): void
|
||||
{
|
||||
Repo::highlight()->deleteMany($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter highlights by one or more contexts
|
||||
*/
|
||||
public function filterByContextIds(?array $contextIds): self
|
||||
{
|
||||
$this->contextIds = $contextIds;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Include site-level highlights in the collection
|
||||
*/
|
||||
public function withSiteHighlights(?string $includeMethod = self::SITE_AND_CONTEXTS): self
|
||||
{
|
||||
$this->includeSite = $includeMethod;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Limit the number of objects retrieved
|
||||
*/
|
||||
public function limit(?int $count): self
|
||||
{
|
||||
$this->count = $count;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Offset the number of objects retrieved, for example to
|
||||
* retrieve the second page of contents
|
||||
*/
|
||||
public function offset(?int $offset): self
|
||||
{
|
||||
$this->offset = $offset;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getQueryBuilder(): Builder
|
||||
{
|
||||
$qb = DB::table($this->dao->table . ' as h')
|
||||
->select(['h.*'])
|
||||
->when(isset($this->contextIds) && $this->includeSite !== self::SITE_ONLY, function($qb) {
|
||||
$qb->whereIn('h.' . $this->dao->parentKeyColumn, $this->contextIds);
|
||||
if ($this->includeSite === self::SITE_AND_CONTEXTS) {
|
||||
$qb->orWhereNull('h.' . $this->dao->parentKeyColumn);
|
||||
}
|
||||
}, function($qb) {
|
||||
if ($this->includeSite === self::SITE_ONLY) {
|
||||
$qb->whereNull('h.' . $this->dao->parentKeyColumn);
|
||||
}
|
||||
})
|
||||
->when(isset($this->count), function($qb) {
|
||||
$qb->limit($this->count);
|
||||
})
|
||||
->when(isset($this->offset), function($qb) {
|
||||
$qb->offset($this->offset);
|
||||
})
|
||||
->orderBy('h.sequence', 'asc');
|
||||
|
||||
Hook::run('Highlight::Collector', [&$qb, $this]);
|
||||
|
||||
return $qb;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
/**
|
||||
* @file classes/highlight/DAO.php
|
||||
*
|
||||
* Copyright (c) 2014-2023 Simon Fraser University
|
||||
* Copyright (c) 2000-2023 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class DAO
|
||||
*
|
||||
* @brief Read and write highlights to the database.
|
||||
*/
|
||||
|
||||
namespace PKP\highlight;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\LazyCollection;
|
||||
use PKP\core\EntityDAO;
|
||||
|
||||
class DAO extends EntityDAO
|
||||
{
|
||||
public $schema = \PKP\services\PKPSchemaService::SCHEMA_HIGHLIGHT;
|
||||
public $table = 'highlights';
|
||||
public $settingsTable = 'highlight_settings';
|
||||
public $primaryKeyColumn = 'highlight_id';
|
||||
public $parentKeyColumn = 'context_id';
|
||||
public $primaryTableColumns = [
|
||||
'id' => 'highlight_id',
|
||||
'contextId' => 'context_id',
|
||||
'sequence' => 'sequence',
|
||||
'url' => 'url',
|
||||
];
|
||||
|
||||
/**
|
||||
* Instantiate a new Highlight
|
||||
*/
|
||||
public function newDataObject(): Highlight
|
||||
{
|
||||
return app(Highlight::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a highlight exists
|
||||
*/
|
||||
public function exists(int $id, ?int $contextId): bool
|
||||
{
|
||||
return DB::table($this->table)
|
||||
->where($this->primaryKeyColumn, $id)
|
||||
->where($this->parentKeyColumn, $contextId)
|
||||
->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a highlight
|
||||
*/
|
||||
public function get(int $id, ?int $contextId): ?Highlight
|
||||
{
|
||||
$row = DB::table($this->table)
|
||||
->where($this->primaryKeyColumn, $id)
|
||||
->where($this->parentKeyColumn, $contextId)
|
||||
->first();
|
||||
return $row ? $this->fromRow($row) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of highlights matching the configured query
|
||||
*/
|
||||
public function getCount(Collector $query): int
|
||||
{
|
||||
return $query
|
||||
->getQueryBuilder()
|
||||
->get('a.' . $this->primaryKeyColumn)
|
||||
->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of ids matching the configured query
|
||||
*
|
||||
* @return Collection<int,int>
|
||||
*/
|
||||
public function getIds(Collector $query): Collection
|
||||
{
|
||||
return $query
|
||||
->getQueryBuilder()
|
||||
->pluck('a.' . $this->primaryKeyColumn);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a collection of highlights matching the configured query
|
||||
*
|
||||
* @return LazyCollection<int,T>
|
||||
*/
|
||||
public function getMany(Collector $query): LazyCollection
|
||||
{
|
||||
$rows = $query
|
||||
->getQueryBuilder()
|
||||
->get();
|
||||
|
||||
return LazyCollection::make(function () use ($rows) {
|
||||
foreach ($rows as $row) {
|
||||
yield $row->highlight_id => $this->fromRow($row);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function insert(Highlight $highlight): int
|
||||
{
|
||||
return parent::_insert($highlight);
|
||||
}
|
||||
|
||||
public function update(Highlight $highlight)
|
||||
{
|
||||
parent::_update($highlight);
|
||||
}
|
||||
|
||||
public function delete(Highlight $highlight)
|
||||
{
|
||||
parent::_delete($highlight);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the largest sequence value for a given context
|
||||
*/
|
||||
public function getLastSequence(?int $contextId = null): ?int
|
||||
{
|
||||
return DB::table($this->table)
|
||||
->when(
|
||||
$contextId,
|
||||
fn($qb) => $qb->where('context_id', $contextId),
|
||||
fn($qb) => $qb->whereNull('context_id')
|
||||
)
|
||||
->orderBy('sequence', 'desc')
|
||||
->first('sequence')
|
||||
?->sequence;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
<?php
|
||||
/**
|
||||
* @file classes/highlight/Highlight.php
|
||||
*
|
||||
* Copyright (c) 2014-2023 Simon Fraser University
|
||||
* Copyright (c) 2000-2023 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class Highlight
|
||||
*
|
||||
* @ingroup highlight
|
||||
*
|
||||
* @see DAO
|
||||
*
|
||||
* @brief The Highlight class implements the abstract data model of a context or site-level highlight.
|
||||
*/
|
||||
|
||||
namespace PKP\highlight;
|
||||
|
||||
use APP\core\Application;
|
||||
use APP\facades\Repo;
|
||||
use APP\file\PublicFileManager;
|
||||
use PKP\core\DataObject;
|
||||
|
||||
class Highlight extends DataObject
|
||||
{
|
||||
|
||||
public function getContextId(): ?int
|
||||
{
|
||||
return $this->getData('contextId');
|
||||
}
|
||||
|
||||
public function setContextId(?int $contextId): void
|
||||
{
|
||||
$this->setData('contextId', $contextId);
|
||||
}
|
||||
|
||||
public function getLocalizedDescription(?string $locale = null): ?string
|
||||
{
|
||||
return $this->getLocalizedData('description', $locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $description
|
||||
*/
|
||||
public function setDescription(array $description): void
|
||||
{
|
||||
$this->setData('description', $description);
|
||||
}
|
||||
|
||||
public function setLocalizedDescription(string $description, string $locale): void
|
||||
{
|
||||
$this->setData('description', $description, $locale);
|
||||
}
|
||||
|
||||
public function getImage(): ?array
|
||||
{
|
||||
return $this->getData('image');
|
||||
}
|
||||
|
||||
public function setImage($image): void
|
||||
{
|
||||
$this->setData('image', $image);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the full URL to the image
|
||||
*
|
||||
* @param bool $withTimestamp Pass true to include a query argument with a timestamp
|
||||
* of the date the image was uploaded in order to workaround cache bugs in browsers
|
||||
*/
|
||||
public function getImageUrl(bool $withTimestamp = true): string
|
||||
{
|
||||
$image = $this->getImage();
|
||||
|
||||
if (!$image) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$filename = $image['uploadName'];
|
||||
if ($withTimestamp) {
|
||||
$filename .= '?'. strtotime($image['dateUploaded']);
|
||||
}
|
||||
|
||||
$publicFileManager = new PublicFileManager();
|
||||
|
||||
return join('/', [
|
||||
Application::get()->getRequest()->getBaseUrl(),
|
||||
$this->getContextId()
|
||||
? $publicFileManager->getContextFilesPath($this->getContextId())
|
||||
: $publicFileManager->getSiteFilesPath(),
|
||||
Repo::highlight()->getImageSubdirectory(),
|
||||
$filename
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the alt text for the image
|
||||
*/
|
||||
public function getImageAltText(): string
|
||||
{
|
||||
$image = $this->getImage();
|
||||
|
||||
if (!$image || !$image['altText']) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $image['altText'];
|
||||
}
|
||||
|
||||
public function getSequence(): ?int
|
||||
{
|
||||
return $this->getData('sequence');
|
||||
}
|
||||
|
||||
public function setSequence($sequence): void
|
||||
{
|
||||
$this->setData('sequence', $sequence);
|
||||
}
|
||||
|
||||
public function getLocalizedTitle(?string $locale = null): string
|
||||
{
|
||||
return $this->getLocalizedData('title', $locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $title
|
||||
*/
|
||||
public function setTitle(array $title): void
|
||||
{
|
||||
$this->setData('title', $title);
|
||||
}
|
||||
|
||||
public function setLocalizedTitle(string $title, string $locale): void
|
||||
{
|
||||
$this->setData('title', $title, $locale);
|
||||
}
|
||||
|
||||
public function getUrl(): ?string
|
||||
{
|
||||
return $this->getData('url');
|
||||
}
|
||||
|
||||
public function setUrl(string $url): void
|
||||
{
|
||||
$this->setData('url', $url);
|
||||
}
|
||||
|
||||
public function getLocalizedUrlText(?string $locale = null): string
|
||||
{
|
||||
return $this->getLocalizedData('urlText', $locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $urlText
|
||||
*/
|
||||
public function setUrlText(array $urlText): void
|
||||
{
|
||||
$this->setData('urlText', $urlText);
|
||||
}
|
||||
|
||||
public function setLocalizedUrlText(string $urlText, string $locale): void
|
||||
{
|
||||
$this->setData('urlText', $urlText, $locale);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,359 @@
|
||||
<?php
|
||||
/**
|
||||
* @file classes/highlight/Repository.php
|
||||
*
|
||||
* Copyright (c) 2014-2023 Simon Fraser University
|
||||
* Copyright (c) 2000-2023 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class Repository
|
||||
*
|
||||
* @brief A repository to find and manage highlights.
|
||||
*/
|
||||
|
||||
namespace PKP\highlight;
|
||||
|
||||
use APP\core\Application;
|
||||
use APP\core\Request;
|
||||
use APP\file\PublicFileManager;
|
||||
use Exception;
|
||||
use PKP\context\Context;
|
||||
use PKP\core\Core;
|
||||
use PKP\core\exceptions\StoreTemporaryFileException;
|
||||
use PKP\file\FileManager;
|
||||
use PKP\file\TemporaryFile;
|
||||
use PKP\file\TemporaryFileManager;
|
||||
use PKP\plugins\Hook;
|
||||
use PKP\services\PKPSchemaService;
|
||||
use PKP\validation\ValidatorFactory;
|
||||
|
||||
class Repository
|
||||
{
|
||||
/** @var DAO $dao */
|
||||
public $dao;
|
||||
|
||||
/** @var string $schemaMap The name of the class to map this entity to its schema */
|
||||
public $schemaMap = maps\Schema::class;
|
||||
|
||||
/** @var Request $request */
|
||||
protected $request;
|
||||
|
||||
/** @var PKPSchemaService<Highlight> $schemaService */
|
||||
protected $schemaService;
|
||||
|
||||
|
||||
public function __construct(DAO $dao, Request $request, PKPSchemaService $schemaService)
|
||||
{
|
||||
$this->dao = $dao;
|
||||
$this->request = $request;
|
||||
$this->schemaService = $schemaService;
|
||||
}
|
||||
|
||||
/** @copydoc DAO::newDataObject() */
|
||||
public function newDataObject(array $params = []): Highlight
|
||||
{
|
||||
$object = $this->dao->newDataObject();
|
||||
if (!empty($params)) {
|
||||
$object->setAllData($params);
|
||||
}
|
||||
return $object;
|
||||
}
|
||||
|
||||
/** @copydoc DAO::get() */
|
||||
public function get(int $id, ?int $contextId): ?Highlight
|
||||
{
|
||||
return $this->dao->get($id, $contextId);
|
||||
}
|
||||
|
||||
/** @copydoc DAO::exists() */
|
||||
public function exists(int $id, ?int $contextId): bool
|
||||
{
|
||||
return $this->dao->exists($id, $contextId);
|
||||
}
|
||||
|
||||
/** @copydoc DAO::getCollector() */
|
||||
public function getCollector(): Collector
|
||||
{
|
||||
return app(Collector::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an instance of the map class for mapping
|
||||
* highlights to their schema
|
||||
*/
|
||||
public function getSchemaMap(): maps\Schema
|
||||
{
|
||||
return app('maps')->withExtensions($this->schemaMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate properties for a higlhight
|
||||
*
|
||||
* Perform validation checks on data used to add or edit a highlight.
|
||||
*
|
||||
* @param array $props A key/value array with the new data to validate
|
||||
* @return array A key/value array with validation errors. Empty if no errors
|
||||
*/
|
||||
public function validate(?Highlight $object, array $props, ?Context $context): array
|
||||
{
|
||||
$site = Application::get()->getRequest()->getSite();
|
||||
$allowedLocales = $context ? $context->getSupportedFormLocales() : $site->getSupportedLocales();
|
||||
$primaryLocale = $context ? $context->getPrimaryLocale() : $site->getPrimaryLocale();
|
||||
|
||||
$validator = ValidatorFactory::make(
|
||||
$props,
|
||||
$this->schemaService->getValidationRules($this->dao->schema, $allowedLocales),
|
||||
[
|
||||
'dateExpire.date_format' => __('stats.dateRange.invalidDate'),
|
||||
]
|
||||
);
|
||||
|
||||
// Check required fields
|
||||
ValidatorFactory::required(
|
||||
$validator,
|
||||
$object,
|
||||
$this->schemaService->getRequiredProps($this->dao->schema),
|
||||
$this->schemaService->getMultilingualProps($this->dao->schema),
|
||||
$allowedLocales,
|
||||
$primaryLocale
|
||||
);
|
||||
|
||||
// Check for input from disallowed locales
|
||||
ValidatorFactory::allowedLocales($validator, $this->schemaService->getMultilingualProps($this->dao->schema), $allowedLocales);
|
||||
|
||||
|
||||
// If a new file has been uploaded, check that the temporary file exists and
|
||||
// the current user owns it
|
||||
$user = Application::get()->getRequest()->getUser();
|
||||
ValidatorFactory::temporaryFilesExist(
|
||||
$validator,
|
||||
['image'],
|
||||
[],
|
||||
$props,
|
||||
$allowedLocales,
|
||||
$user ? $user->getId() : null
|
||||
);
|
||||
|
||||
$errors = [];
|
||||
|
||||
if ($validator->fails()) {
|
||||
$errors = $this->schemaService->formatValidationErrors($validator->errors());
|
||||
}
|
||||
|
||||
Hook::run('Highlight::validate', [&$errors, $object, $props, $context]);
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a highlight
|
||||
*/
|
||||
public function add(Highlight $highlight): int
|
||||
{
|
||||
if (!$highlight->getSequence()) {
|
||||
$highlight->setSequence(
|
||||
$this->getNextSequence($highlight->getContextId())
|
||||
);
|
||||
}
|
||||
|
||||
$id = $this->dao->insert($highlight);
|
||||
|
||||
$highlight = $this->get($id, $highlight->getContextId());
|
||||
|
||||
if ($highlight->getImage()) {
|
||||
$this->handleImageUpload($highlight);
|
||||
}
|
||||
|
||||
Hook::run('Highlight::add', [$highlight]);
|
||||
|
||||
return $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit a highlight
|
||||
*/
|
||||
public function edit(Highlight $highlight, array $params): void
|
||||
{
|
||||
$newHighlight = clone $highlight;
|
||||
$newHighlight->setAllData(array_merge($newHighlight->_data, $params));
|
||||
|
||||
Hook::run('Highlight::edit', [$newHighlight, $highlight, $params]);
|
||||
|
||||
$this->dao->update($newHighlight);
|
||||
|
||||
$image = $newHighlight->getImage();
|
||||
$hasNewImage = $image && $image['temporaryFileId'];
|
||||
|
||||
if ((!$image || $hasNewImage) && $highlight->getImage()) {
|
||||
$this->deleteImage($highlight);
|
||||
}
|
||||
|
||||
if ($hasNewImage) {
|
||||
$this->handleImageUpload($newHighlight);
|
||||
}
|
||||
}
|
||||
|
||||
/** @copydoc DAO::delete() */
|
||||
public function delete(Highlight $highlight): void
|
||||
{
|
||||
Hook::run('Highlight::delete::before', [$highlight]);
|
||||
|
||||
if ($highlight->getImage()) {
|
||||
$this->deleteImage($highlight);
|
||||
}
|
||||
|
||||
$this->dao->delete($highlight);
|
||||
|
||||
Hook::run('Highlight::delete', [$highlight]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a collection of highlights
|
||||
*/
|
||||
public function deleteMany(Collector $collector)
|
||||
{
|
||||
foreach ($collector->getMany() as $highlight) {
|
||||
$this->delete($highlight);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the next sequence for a highlight in the context
|
||||
*
|
||||
* This gets the correct sequence value to put a highlight last
|
||||
*/
|
||||
public function getNextSequence(?int $contextId = null): int
|
||||
{
|
||||
$lastSequence = $this->dao->getLastSequence($contextId);
|
||||
return is_null($lastSequence)
|
||||
? 1
|
||||
: $lastSequence + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* The subdirectory where highlight images are stored
|
||||
*/
|
||||
public function getImageSubdirectory(): string
|
||||
{
|
||||
return 'highlights';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the base URL for highlight file uploads
|
||||
*/
|
||||
public function getFileUploadBaseUrl(?Context $context = null): string
|
||||
{
|
||||
return join('/', [
|
||||
Application::get()->getRequest()->getPublicFilesUrl($context),
|
||||
$this->getImageSubdirectory(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle image uploads
|
||||
*
|
||||
* @throws StoreTemporaryFileException
|
||||
*/
|
||||
protected function handleImageUpload(Highlight $highlight): void
|
||||
{
|
||||
$image = $highlight->getImage();
|
||||
if ($image && $image['temporaryFileId']) {
|
||||
$user = Application::get()->getRequest()->getUser();
|
||||
$image = $highlight->getImage();
|
||||
$temporaryFileManager = new TemporaryFileManager();
|
||||
$temporaryFile = $temporaryFileManager->getFile((int) $image['temporaryFileId'], $user?->getId());
|
||||
$filepath = $this->getImageSubdirectory() . '/' . $this->getImageFilename($highlight, $temporaryFile);
|
||||
if ($this->storeTemporaryFile($temporaryFile, $filepath, $user?->getId(), $highlight)) {
|
||||
$highlight->setImage(
|
||||
$this->getImageData($highlight, $temporaryFile)
|
||||
);
|
||||
$this->dao->update($highlight);
|
||||
} else {
|
||||
$this->delete($highlight);
|
||||
throw new StoreTemporaryFileException($temporaryFile, $filepath, $user, $highlight);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a temporary file upload in the public files directory
|
||||
*
|
||||
* @return bool Whether or not the operation was successful
|
||||
*/
|
||||
protected function storeTemporaryFile(TemporaryFile $temporaryFile, string $filepath, ?int $userId, Highlight $highlight): bool
|
||||
{
|
||||
$publicFileManager = new PublicFileManager();
|
||||
$temporaryFileManager = new TemporaryFileManager();
|
||||
|
||||
if ($highlight->getContextId()) {
|
||||
$result = $publicFileManager->copyContextFile(
|
||||
$highlight->getContextId(),
|
||||
$temporaryFile->getFilePath(),
|
||||
$filepath
|
||||
);
|
||||
} else {
|
||||
$result = $publicFileManager->copySiteFile(
|
||||
$temporaryFile->getFilePath(),
|
||||
$filepath
|
||||
);
|
||||
}
|
||||
|
||||
if (!$result) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$temporaryFileManager->deleteById($temporaryFile->getId(), $userId);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the data array for a temporary file that has just been stored
|
||||
*
|
||||
* @return array Data about the image, like the upload name, alt text, and date uploaded
|
||||
*/
|
||||
protected function getImageData(Highlight $highlight, TemporaryFile $temporaryFile): array
|
||||
{
|
||||
$image = $highlight->getImage();
|
||||
|
||||
return [
|
||||
'name' => $temporaryFile->getOriginalFileName(),
|
||||
'uploadName' => $this->getImageFilename($highlight, $temporaryFile),
|
||||
'dateUploaded' => Core::getCurrentDate(),
|
||||
'altText' => !empty($image['altText']) ? $image['altText'] : '',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the filename of the image upload
|
||||
*/
|
||||
protected function getImageFilename(Highlight $highlight, TemporaryFile $temporaryFile): string
|
||||
{
|
||||
$fileManager = new FileManager();
|
||||
|
||||
return $highlight->getId()
|
||||
. $fileManager->getImageExtension($temporaryFile->getFileType());
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the image related to highlight
|
||||
*/
|
||||
protected function deleteImage(Highlight $highlight): void
|
||||
{
|
||||
$image = $highlight->getImage();
|
||||
if ($image && $image['uploadName']) {
|
||||
$publicFileManager = new PublicFileManager();
|
||||
$filesPath = $highlight->getContextId()
|
||||
? $publicFileManager->getContextFilesPath($highlight->getContextId())
|
||||
: $publicFileManager->getSiteFilesPath();
|
||||
|
||||
$publicFileManager->deleteByPath(
|
||||
join('/', [
|
||||
$filesPath,
|
||||
$this->getImageSubdirectory(),
|
||||
$image['uploadName'],
|
||||
])
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
/**
|
||||
* @file classes/highlight/maps/Schema.php
|
||||
*
|
||||
* Copyright (c) 2014-2023 Simon Fraser University
|
||||
* Copyright (c) 2000-2023 John Willinsky
|
||||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
|
||||
*
|
||||
* @class Schema
|
||||
*
|
||||
* @brief Map highlights to the properties defined in the highlight schema
|
||||
*/
|
||||
|
||||
namespace PKP\highlight\maps;
|
||||
|
||||
use Illuminate\Support\Enumerable;
|
||||
use PKP\highlight\Highlight;
|
||||
use PKP\services\PKPSchemaService;
|
||||
|
||||
class Schema extends \PKP\core\maps\Schema
|
||||
{
|
||||
public Enumerable $collection;
|
||||
|
||||
public string $schema = PKPSchemaService::SCHEMA_HIGHLIGHT;
|
||||
|
||||
/**
|
||||
* Map a highlight
|
||||
*
|
||||
* Includes all properties in the highlight schema.
|
||||
*/
|
||||
public function map(Highlight $item): array
|
||||
{
|
||||
return $this->mapByProperties($this->getProps(), $item);
|
||||
}
|
||||
|
||||
/**
|
||||
* Summarize a highlight
|
||||
*
|
||||
* Includes properties with the apiSummary flag in the highlight schema.
|
||||
*/
|
||||
public function summarize(Highlight $item): array
|
||||
{
|
||||
return $this->mapByProperties($this->getSummaryProps(), $item);
|
||||
}
|
||||
|
||||
/**
|
||||
* Map a collection of Highlights
|
||||
*
|
||||
* @see self::map
|
||||
*/
|
||||
public function mapMany(Enumerable $collection): Enumerable
|
||||
{
|
||||
$this->collection = $collection;
|
||||
return $collection->map(function ($item) {
|
||||
return $this->map($item);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Summarize a collection of Highlights
|
||||
*
|
||||
* @see self::summarize
|
||||
*/
|
||||
public function summarizeMany(Enumerable $collection): Enumerable
|
||||
{
|
||||
$this->collection = $collection;
|
||||
return $collection->map(function ($item) {
|
||||
return $this->summarize($item);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Map schema properties of an Highlight to an assoc array
|
||||
*/
|
||||
protected function mapByProperties(array $props, Highlight $item): array
|
||||
{
|
||||
$output = [];
|
||||
foreach ($props as $prop) {
|
||||
switch ($prop) {
|
||||
case '_href':
|
||||
$output[$prop] = $this->getApiUrl('highlights/' . $item->getId());
|
||||
break;
|
||||
default:
|
||||
$output[$prop] = $item->getData($prop);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$output = $this->schemaService->addMissingMultilingualValues($this->schema, $output, $this->getSupportedFormLocales());
|
||||
|
||||
ksort($output);
|
||||
|
||||
return $this->withExtensions($output, $item);
|
||||
}
|
||||
|
||||
protected function getSupportedFormLocales(): array
|
||||
{
|
||||
return $this->context?->getSupportedFormLocales()
|
||||
?? $this->request->getSite()->getSupportedLocales();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user