EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
record collection getApiOutput method
Yuri Kuznetsov4559f2746e5f
diff --git a/application/Espo/Controllers/LastViewed.php b/application/Espo/Controllers/LastViewed.phpindex 338b1fc5d5..df76359c49 100644--- a/application/Espo/Controllers/LastViewed.php+++ b/application/Espo/Controllers/LastViewed.php@@ -35,6 +35,9 @@ use Espo\Tools\ActionHistory\Service as Service;use stdClass;+/**+ * @noinspection PhpUnused+ */class LastViewed{public function __construct(private SearchParamsFetcher $searchParamsFetcher, private Service $service)@@ -49,9 +52,6 @@ class LastViewed$result = $this->service->getLastViewed($maxSize, $offset);- return (object) [- 'total' => $result->getTotal(),- 'list' => $result->getValueMapList(),- ];+ return $result->toApiOutput();}}diff --git a/application/Espo/Controllers/Notification.php b/application/Espo/Controllers/Notification.phpindex 9fbe08e84e..e48d10bdbd 100644--- a/application/Espo/Controllers/Notification.php+++ b/application/Espo/Controllers/Notification.php@@ -83,10 +83,7 @@ class Notification extends RecordBase$recordCollection = $this->getNotificationService()->get($userId, $searchParams);- return (object) [- 'total' => $recordCollection->getTotal(),- 'list' => $recordCollection->getValueMapList(),- ];+ return $recordCollection->toApiOutput();}public function getActionNotReadCount(): intdiff --git a/application/Espo/Controllers/Stream.php b/application/Espo/Controllers/Stream.phpindex 3be5fff2ec..88f7083266 100644--- a/application/Espo/Controllers/Stream.php+++ b/application/Espo/Controllers/Stream.php@@ -79,12 +79,12 @@ class Stream$reactionsCheckDate = DateTime::createNow();- return (object) [- 'total' => $collection->getTotal(),- 'list' => $collection->getValueMapList(),- 'reactionsCheckDate' => $reactionsCheckDate->toString(),- 'updatedReactions' => $this->getReactionUpdates($request, $id),- ];+ $output = $collection->toApiOutput();++ $output->reactionsCheckDate = $reactionsCheckDate->toString();+ $output->updatedReactions = $this->getReactionUpdates($request, $id);++ return $output;}if ($id === null) {@@ -94,11 +94,11 @@ class Stream$collection = $this->service->find($scope, $id, $searchParams);$pinnedCollection = $this->service->getPinned($scope, $id);- return (object) [- 'total' => $collection->getTotal(),- 'list' => $collection->getValueMapList(),- 'pinnedList' => $pinnedCollection->getValueMapList(),- ];+ $output = $collection->toApiOutput();++ $output->pinnedList = $pinnedCollection->getValueMapList();++ return $output;}/**@@ -126,10 +126,7 @@ class Stream$this->userRecordService->find($id, $searchParams) :$this->service->find($scope, $id ?? '', $searchParams);- return (object) [- 'total' => $result->getTotal(),- 'list' => $result->getValueMapList(),- ];+ return $result->toApiOutput();}/**@@ -150,10 +147,7 @@ class Stream$result = $this->service->findUpdates($scope, $id, $searchParams);- return (object) [- 'total' => $result->getTotal(),- 'list' => $result->getValueMapList(),- ];+ return $result->toApiOutput();}/**diff --git a/application/Espo/Core/Controllers/Record.php b/application/Espo/Core/Controllers/Record.phpindex 07e613f68b..8b6c237048 100644--- a/application/Espo/Core/Controllers/Record.php+++ b/application/Espo/Core/Controllers/Record.php@@ -31,7 +31,6 @@ namespace Espo\Core\Controllers;use Espo\Core\Exceptions\BadRequest;use Espo\Core\Api\Request;-use Espo\Core\Exceptions\Error;use Espo\Core\Exceptions\Forbidden;use Espo\Core\Exceptions\NotFound;use Espo\Core\Exceptions\NotFoundSilent;@@ -65,12 +64,9 @@ class Record extends RecordBase$searchParams = $this->fetchSearchParamsFromRequest($request);- $recordCollection = $this->getRecordService()->findLinked($id, $link, $searchParams);+ $result = $this->getRecordService()->findLinked($id, $link, $searchParams);- return (object) [- 'total' => $recordCollection->getTotal(),- 'list' => $recordCollection->getValueMapList(),- ];+ return $result->toApiOutput();}/**diff --git a/application/Espo/Core/Controllers/RecordBase.php b/application/Espo/Core/Controllers/RecordBase.phpindex 1c17b619eb..0d4c2ddea0 100644--- a/application/Espo/Core/Controllers/RecordBase.php+++ b/application/Espo/Core/Controllers/RecordBase.php@@ -255,10 +255,7 @@ class RecordBase extends Base implements$recordCollection = $this->getRecordService()->find($searchParams, $findParams);- return (object) [- 'total' => $recordCollection->getTotal(),- 'list' => $recordCollection->getValueMapList(),- ];+ return $recordCollection->toApiOutput();}/**diff --git a/application/Espo/Core/Record/Collection.php b/application/Espo/Core/Record/Collection.phpindex 8fb693928b..8d1732a68e 100644--- a/application/Espo/Core/Record/Collection.php+++ b/application/Espo/Core/Record/Collection.php@@ -30,6 +30,7 @@namespace Espo\Core\Record;use Espo\ORM\Collection as OrmCollection;+use Espo\ORM\Entity;use Espo\ORM\EntityCollection;use stdClass;@@ -37,7 +38,7 @@ use stdClass;/*** Contains an ORM collection and total number of records.*- * @template-covariant TEntity of \Espo\ORM\Entity+ * @template-covariant TEntity of Entity*/class Collection{@@ -100,7 +101,7 @@ class Collection/*** Create.*- * @template CEntity of \Espo\ORM\Entity+ * @template CEntity of Entity* @param OrmCollection<CEntity> $collection* @return self<CEntity>*/@@ -112,7 +113,7 @@ class Collection/*** Create w/o count.*- * @template CEntity of \Espo\ORM\Entity+ * @template CEntity of Entity* @param OrmCollection<CEntity> $collection* @return self<CEntity>*/@@ -132,4 +133,17 @@ class Collectionreturn new self($collection, self::TOTAL_HAS_NO_MORE);}++ /**+ * To API output. To be used in API actions.+ *+ * @since 9.1.0+ */+ public function toApiOutput(): stdClass+ {+ return (object) [+ 'total' => $this->getTotal(),+ 'list' => $this->getValueMapList(),+ ];+ }}diff --git a/application/Espo/Modules/Crm/Tools/Activities/Api/Get.php b/application/Espo/Modules/Crm/Tools/Activities/Api/Get.phpindex 8f95e03a14..9f4241fecd 100644--- a/application/Espo/Modules/Crm/Tools/Activities/Api/Get.php+++ b/application/Espo/Modules/Crm/Tools/Activities/Api/Get.php@@ -78,13 +78,10 @@ class Get implements Action$fetchParams = new ActivitiesFetchParams($maxSize, $offset, $targetEntityType);- $recordCollection = $type === 'history' ?+ $result = $type === 'history' ?$this->service->getHistory($parentType, $id, $fetchParams) :$this->service->getActivities($parentType, $id, $fetchParams);- return ResponseComposer::json([- 'total' => $recordCollection->getTotal(),- 'list' => $recordCollection->getValueMapList(),- ]);+ return ResponseComposer::json($result->toApiOutput());}}diff --git a/application/Espo/Modules/Crm/Tools/Activities/Api/GetListTyped.php b/application/Espo/Modules/Crm/Tools/Activities/Api/GetListTyped.phpindex 3be3c2d43e..a36cbb82d9 100644--- a/application/Espo/Modules/Crm/Tools/Activities/Api/GetListTyped.php+++ b/application/Espo/Modules/Crm/Tools/Activities/Api/GetListTyped.php@@ -81,9 +81,6 @@ class GetListTyped implements Action$searchParams);- return ResponseComposer::json([- 'total' => $result->getTotal(),- 'list' => $result->getValueMapList(),- ]);+ return ResponseComposer::json($result->toApiOutput());}}diff --git a/application/Espo/Modules/Crm/Tools/Activities/Api/GetUpcoming.php b/application/Espo/Modules/Crm/Tools/Activities/Api/GetUpcoming.phpindex 8c2c289d0e..15dc96d88d 100644--- a/application/Espo/Modules/Crm/Tools/Activities/Api/GetUpcoming.php+++ b/application/Espo/Modules/Crm/Tools/Activities/Api/GetUpcoming.php@@ -59,12 +59,9 @@ class GetUpcoming implements Action$params = $this->fetchParams($request);- $recordCollection = $this->service->get($userId, $params);+ $result = $this->service->get($userId, $params);- return ResponseComposer::json([- 'total' => $recordCollection->getTotal(),- 'list' => $recordCollection->getValueMapList(),- ]);+ return ResponseComposer::json($result->toApiOutput());}/**diff --git a/application/Espo/Modules/Crm/Tools/TargetList/Api/GetOptedOut.php b/application/Espo/Modules/Crm/Tools/TargetList/Api/GetOptedOut.phpindex 880dc0d6b9..f1f66fb1a9 100644--- a/application/Espo/Modules/Crm/Tools/TargetList/Api/GetOptedOut.php+++ b/application/Espo/Modules/Crm/Tools/TargetList/Api/GetOptedOut.php@@ -65,11 +65,8 @@ class GetOptedOut implements Action$searchParams = $this->searchParamsFetcher->fetch($request);- $collection = $this->service->find($id, $searchParams);+ $result = $this->service->find($id, $searchParams);- return ResponseComposer::json((object) [- 'total' => $collection->getTotal(),- 'list' => $collection->getValueMapList(),- ]);+ return ResponseComposer::json($result->toApiOutput());}}diff --git a/application/Espo/Tools/GlobalSearch/Api/Get.php b/application/Espo/Tools/GlobalSearch/Api/Get.phpindex ea7cae7f57..87119f1fc5 100644--- a/application/Espo/Tools/GlobalSearch/Api/Get.php+++ b/application/Espo/Tools/GlobalSearch/Api/Get.php@@ -59,9 +59,6 @@ class Get implements Action$result = $this->service->find($query, $offset, $maxSize);- return ResponseComposer::json([- 'total' => $result->getTotal(),- 'list' => $result->getValueMapList(),- ]);+ return ResponseComposer::json($result->toApiOutput());}}diff --git a/application/Espo/Tools/Stream/Api/GetFollowers.php b/application/Espo/Tools/Stream/Api/GetFollowers.phpindex 6771f68ff9..903bd19f2e 100644--- a/application/Espo/Tools/Stream/Api/GetFollowers.php+++ b/application/Espo/Tools/Stream/Api/GetFollowers.php@@ -67,9 +67,6 @@ class GetFollowers implements Action$collection = $this->service->find($entityType, $id, $searchParams);- return ResponseComposer::json((object) [- 'total' => $collection->getTotal(),- 'list' => $collection->getValueMapList(),- ]);+ return ResponseComposer::json($collection->toApiOutput());}}diff --git a/application/Espo/Tools/Stream/Api/GetGlobal.php b/application/Espo/Tools/Stream/Api/GetGlobal.phpindex a40898ac43..f581f6a308 100644--- a/application/Espo/Tools/Stream/Api/GetGlobal.php+++ b/application/Espo/Tools/Stream/Api/GetGlobal.php@@ -61,12 +61,9 @@ class GetGlobal implements Action$searchParams = $this->fetchSearchParams($request);- $collection = $this->service->find($searchParams);+ $result = $this->service->find($searchParams);- return ResponseComposer::json([- 'list' => $collection->getValueMapList(),- 'total' => $collection->getTotal(),- ]);+ return ResponseComposer::json($result->toApiOutput());}/**diff --git a/application/Espo/Tools/Stream/Api/GetOwn.php b/application/Espo/Tools/Stream/Api/GetOwn.phpindex 04b713b10b..dd5687f350 100644--- a/application/Espo/Tools/Stream/Api/GetOwn.php+++ b/application/Espo/Tools/Stream/Api/GetOwn.php@@ -61,10 +61,7 @@ class GetOwn implements Action$collection = $this->service->findOwn($userId, $searchParams);- return ResponseComposer::json([- 'list' => $collection->getValueMapList(),- 'total' => $collection->getTotal(),- ]);+ return ResponseComposer::json($collection->toApiOutput());}/**diff --git a/application/Espo/Tools/User/Api/PostRecordUsersAccess.php b/application/Espo/Tools/User/Api/PostRecordUsersAccess.phpindex d544776072..396f79d623 100644--- a/application/Espo/Tools/User/Api/PostRecordUsersAccess.php+++ b/application/Espo/Tools/User/Api/PostRecordUsersAccess.php@@ -57,11 +57,8 @@ class PostRecordUsersAccess implements Action$entity = $this->entityProvider->get($entityType, $id);- $collection = $this->usersAccessService->get($entity, $searchParams);+ $result = $this->usersAccessService->get($entity, $searchParams);- return ResponseComposer::json([- 'list' => $collection->getValueMapList(),- 'total' => $collection->getTotal(),- ]);+ return ResponseComposer::json($result->toApiOutput());}}