mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-24 02:13:15 +00:00
Added RecordReferenceCollection utility method
This commit is contained in:
@@ -20,6 +20,7 @@ use Alchemy\Phrasea\Model\Entities\Order;
|
||||
use Alchemy\Phrasea\Order\OrderElementTransformer;
|
||||
use Alchemy\Phrasea\Order\OrderFiller;
|
||||
use Alchemy\Phrasea\Order\OrderTransformer;
|
||||
use Alchemy\Phrasea\Record\RecordReferenceCollection;
|
||||
use Assert\Assertion;
|
||||
use Assert\InvalidArgumentException;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
@@ -45,8 +46,7 @@ class OrderController extends Controller
|
||||
{
|
||||
$data = $this->decodeJsonBody($request, 'orders.json#/definitions/order_request');
|
||||
|
||||
$requestedRecords = $this->toRequestedRecordsArray($data->data->records);
|
||||
$availableRecords = $this->fetchRecords($requestedRecords);
|
||||
$availableRecords = $this->toRequestedRecords($data->data->records);
|
||||
$records = $this->filterOrderableRecords($availableRecords);
|
||||
|
||||
$recordRequest = new RecordsRequest($records, new ArrayCollection($availableRecords), null, RecordsRequest::FLATTEN_YES);
|
||||
@@ -132,9 +132,9 @@ class OrderController extends Controller
|
||||
|
||||
/**
|
||||
* @param array $records
|
||||
* @return array
|
||||
* @return \record_adapter[]
|
||||
*/
|
||||
private function toRequestedRecordsArray(array $records)
|
||||
private function toRequestedRecords(array $records)
|
||||
{
|
||||
$requestedRecords = [];
|
||||
|
||||
@@ -145,38 +145,7 @@ class OrderController extends Controller
|
||||
];
|
||||
}
|
||||
|
||||
return $requestedRecords;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $recordIds
|
||||
* @return \record_adapter[]
|
||||
*/
|
||||
private function fetchRecords(array $recordIds)
|
||||
{
|
||||
$perDataboxRecords = [];
|
||||
|
||||
foreach ($recordIds as $index => $record) {
|
||||
if (!isset($perDataboxRecords[$record['databox_id']])) {
|
||||
$perDataboxRecords[$record['databox_id']] = [];
|
||||
}
|
||||
|
||||
$perDataboxRecords[$record['databox_id']][$record['record_id']] = $index;
|
||||
}
|
||||
|
||||
$records = [];
|
||||
|
||||
foreach ($perDataboxRecords as $databoxId => $recordIndexes) {
|
||||
$repository = $this->findDataboxById($databoxId)->getRecordRepository();
|
||||
|
||||
foreach ($repository->findByRecordIds(array_keys($perDataboxRecords[$databoxId])) as $record) {
|
||||
$records[$recordIndexes[$record->getRecordId()]] = $record;
|
||||
}
|
||||
}
|
||||
|
||||
ksort($records);
|
||||
|
||||
return $records;
|
||||
return RecordReferenceCollection::fromArrayOfArray($requestedRecords)->toRecords($this->getApplicationBox());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -187,9 +156,15 @@ class OrderController extends Controller
|
||||
{
|
||||
$acl = $this->getAclForUser();
|
||||
|
||||
return array_filter($records, function (\record_adapter $record) use ($acl) {
|
||||
return $acl->has_right_on_base($record->getBaseId(), 'cancmd');
|
||||
});
|
||||
$filtered = [];
|
||||
|
||||
foreach ($records as $index => $record) {
|
||||
if ($acl->has_right_on_base($record->getBaseId(), 'cancmd')) {
|
||||
$filtered[$index] = $record;
|
||||
}
|
||||
}
|
||||
|
||||
return $filtered;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -53,11 +53,11 @@ use Alchemy\Phrasea\Model\Repositories\FeedEntryRepository;
|
||||
use Alchemy\Phrasea\Model\Repositories\FeedRepository;
|
||||
use Alchemy\Phrasea\Model\Repositories\LazaretFileRepository;
|
||||
use Alchemy\Phrasea\Model\Repositories\TaskRepository;
|
||||
use Alchemy\Phrasea\Record\RecordReferenceCollection;
|
||||
use Alchemy\Phrasea\SearchEngine\SearchEngineInterface;
|
||||
use Alchemy\Phrasea\SearchEngine\SearchEngineLogger;
|
||||
use Alchemy\Phrasea\SearchEngine\SearchEngineOptions;
|
||||
use Alchemy\Phrasea\SearchEngine\SearchEngineResult;
|
||||
use Alchemy\Phrasea\SearchEngine\SearchEngineResultToRecordsConverter;
|
||||
use Alchemy\Phrasea\SearchEngine\SearchEngineSuggestion;
|
||||
use Alchemy\Phrasea\Status\StatusStructure;
|
||||
use Alchemy\Phrasea\TaskManager\LiveInformation;
|
||||
@@ -1065,10 +1065,10 @@ class V1Controller extends Controller
|
||||
|
||||
$ret['results'] = ['records' => [], 'stories' => []];
|
||||
|
||||
$converter = new SearchEngineResultToRecordsConverter($this->getApplicationBox());
|
||||
|
||||
/** @var SearchEngineResult $search_result */
|
||||
foreach ($converter->convert($search_result->getResults()) as $record) {
|
||||
$references = new RecordReferenceCollection($search_result->getResults());
|
||||
|
||||
foreach ($references->toRecords($this->getApplicationBox()) as $record) {
|
||||
if ($record->isStory()) {
|
||||
$ret['results']['stories'][] = $this->listStory($request, $record);
|
||||
} else {
|
||||
|
||||
@@ -2,13 +2,8 @@
|
||||
|
||||
namespace Alchemy\Phrasea\Model;
|
||||
|
||||
interface RecordInterface
|
||||
interface RecordInterface extends RecordReferenceInterface
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getId();
|
||||
|
||||
/**
|
||||
* The unique id of the collection where belong the record.
|
||||
*
|
||||
@@ -26,13 +21,6 @@ interface RecordInterface
|
||||
/** @return \DateTime */
|
||||
public function getCreated();
|
||||
|
||||
/**
|
||||
* The id of the databox where belong the record.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getDataboxId();
|
||||
|
||||
/** @return boolean */
|
||||
public function isStory();
|
||||
|
||||
@@ -42,9 +30,6 @@ interface RecordInterface
|
||||
/** @return string */
|
||||
public function getOriginalName();
|
||||
|
||||
/** @return integer */
|
||||
public function getRecordId();
|
||||
|
||||
/** @return string */
|
||||
public function getSha256();
|
||||
|
||||
|
||||
29
lib/Alchemy/Phrasea/Model/RecordReferenceInterface.php
Normal file
29
lib/Alchemy/Phrasea/Model/RecordReferenceInterface.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Phraseanet
|
||||
*
|
||||
* (c) 2005-2016 Alchemy
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Alchemy\Phrasea\Model;
|
||||
|
||||
interface RecordReferenceInterface
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getId();
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getDataboxId();
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getRecordId();
|
||||
}
|
||||
@@ -10,9 +10,10 @@
|
||||
|
||||
namespace Alchemy\Phrasea\Record;
|
||||
|
||||
use Alchemy\Phrasea\Model\RecordReferenceInterface;
|
||||
use Assert\Assertion;
|
||||
|
||||
class RecordReference
|
||||
class RecordReference implements RecordReferenceInterface
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
|
||||
100
lib/Alchemy/Phrasea/Record/RecordReferenceCollection.php
Normal file
100
lib/Alchemy/Phrasea/Record/RecordReferenceCollection.php
Normal file
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Phraseanet
|
||||
*
|
||||
* (c) 2005-2016 Alchemy
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Alchemy\Phrasea\Record;
|
||||
|
||||
use Assert\Assertion;
|
||||
|
||||
class RecordReferenceCollection implements \IteratorAggregate
|
||||
{
|
||||
/**
|
||||
* @param array<int|string,array> $records
|
||||
* @return RecordReferenceCollection
|
||||
*/
|
||||
public static function fromArrayOfArray($records)
|
||||
{
|
||||
Assertion::allIsArrayAccessible($records);
|
||||
|
||||
$references = [];
|
||||
|
||||
foreach ($records as $index => $record) {
|
||||
if (isset($record['id'])) {
|
||||
$references[$index] = RecordReference::createFromRecordReference($record['id']);
|
||||
} elseif (isset($record['databox_id']) && isset($record['record_id'])) {
|
||||
$references[$index] = RecordReference::createFromDataboxIdAndRecordId($record['databox_id'], $record['record_id']);
|
||||
}
|
||||
}
|
||||
|
||||
return new self($references);
|
||||
}
|
||||
|
||||
/**
|
||||
* @var RecordReference[]
|
||||
*/
|
||||
private $references = [];
|
||||
|
||||
/**
|
||||
* @param RecordReference[] $references
|
||||
*/
|
||||
public function __construct($references)
|
||||
{
|
||||
Assertion::allIsInstanceOf($references, RecordReference::class);
|
||||
|
||||
$this->references = $references;
|
||||
}
|
||||
|
||||
public function getIterator()
|
||||
{
|
||||
return new \ArrayIterator($this->references);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int,array<int,int>>
|
||||
*/
|
||||
public function groupPerDataboxId()
|
||||
{
|
||||
$groups = [];
|
||||
|
||||
foreach ($this->references as $index => $reference) {
|
||||
$databoxId = $reference->getDataboxId();
|
||||
|
||||
if (!isset($groups[$databoxId])) {
|
||||
$groups[$databoxId] = [];
|
||||
}
|
||||
|
||||
$groups[$databoxId][$reference->getRecordId()] = $index;
|
||||
}
|
||||
|
||||
return $groups;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \appbox $appbox
|
||||
* @return \record_adapter[]
|
||||
*/
|
||||
public function toRecords(\appbox $appbox)
|
||||
{
|
||||
$groups = $this->groupPerDataboxId();
|
||||
|
||||
$records = [];
|
||||
|
||||
foreach ($groups as $databoxId => $recordIds) {
|
||||
$databox = $appbox->get_databox($databoxId);
|
||||
|
||||
foreach ($databox->getRecordRepository()->findByRecordIds(array_keys($recordIds)) as $record) {
|
||||
$records[$recordIds[$record->getRecordId()]] = $record;
|
||||
}
|
||||
}
|
||||
|
||||
ksort($records);
|
||||
|
||||
return array_values($records);
|
||||
}
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Phraseanet
|
||||
*
|
||||
* (c) 2005-2016 Alchemy
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Alchemy\Phrasea\SearchEngine;
|
||||
|
||||
use Alchemy\Phrasea\Model\RecordInterface;
|
||||
use Assert\Assertion;
|
||||
|
||||
class SearchEngineResultToRecordsConverter
|
||||
{
|
||||
/**
|
||||
* @var \appbox
|
||||
*/
|
||||
private $appbox;
|
||||
|
||||
public function __construct(\appbox $appbox)
|
||||
{
|
||||
$this->appbox = $appbox;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RecordInterface[] $records
|
||||
* @return \record_adapter[]
|
||||
*/
|
||||
public function convert($records)
|
||||
{
|
||||
Assertion::allIsInstanceOf($records, RecordInterface::class);
|
||||
|
||||
$perDataboxRecordIds = $this->groupRecordIdsPerDataboxId($records);
|
||||
|
||||
$records = [];
|
||||
|
||||
foreach ($perDataboxRecordIds as $databoxId => $recordIds) {
|
||||
$databox = $this->appbox->get_databox($databoxId);
|
||||
|
||||
foreach ($databox->getRecordRepository()->findByRecordIds(array_keys($recordIds)) as $record) {
|
||||
$records[$recordIds[$record->getRecordId()]] = $record;
|
||||
}
|
||||
}
|
||||
|
||||
ksort($records);
|
||||
|
||||
return $records;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RecordInterface[] $records
|
||||
* @return array[]
|
||||
*/
|
||||
private function groupRecordIdsPerDataboxId($records)
|
||||
{
|
||||
$number = 0;
|
||||
$perDataboxRecordIds = [];
|
||||
|
||||
foreach ($records as $record) {
|
||||
$databoxId = $record->getDataboxId();
|
||||
|
||||
if (!isset($perDataboxRecordIds[$databoxId])) {
|
||||
$perDataboxRecordIds[$databoxId] = [];
|
||||
}
|
||||
|
||||
$perDataboxRecordIds[$databoxId][$record->getRecordId()] = $number++;
|
||||
}
|
||||
|
||||
return $perDataboxRecordIds;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user