<?php
// Client.php
// Copyright P.Kassel, M.Seliger, G.Trietsch, all Rights Reserved
namespace Docuform\Api\Server;use Docuform\Api\TableMapper;use Docuform\Database\DbSQL;use Docuform\Api\Exception\ApiHttpForbiddenException;use Docuform\Lib\ApiUuid;use DI\Container;use Docuform\Lib\ValidateParam;class Client extends TableMapper{public function getColumnMappings():array{return[['name'=>'ClientVersion','type'=>DbSQL::cst_intornull,'alias'=>'version'],['name'=>'DeviceManagerId','type'=>DbSQL::cst_intornull,'alias'=>'dealerId'],['name'=>'FSMClientId','type'=>DbSQL::cst_text,'alias'=>'fsmClientId'],['name'=>'LastTransferUTC','type'=>DbSQL::cst_date,'alias'=>'lastTransfer'],['name'=>'LastUpdateDoneUTC','type'=>DbSQL::cst_date,'alias'=>'lastUpdate'],['name'=>'LastUpdateQueryUTC','type'=>DbSQL::cst_date,'alias'=>'lastUpdateQuery'],['name'=>'RestApiUrl','type'=>DbSQL::cst_text,'alias'=>'clientApiUrl'],['name'=>'sUUID','type'=>DbSQL::cst_uuid,'alias'=>'uuid']];}public function getParameterConstraints(string $method):array{if($method==='GET')return[['name'=>'Uuid','type'=>ValidateParam::UUID]];return[];}public static function readAll(Container $container,int $customerId,?string $uuid=null):Customer|array{$serverApi=$container->get('RestApi');$request=$serverApi->getRequest();$customer=$serverApi->getCustomer();if(!empty($customerId)&&$customer===null){throw new ApiHttpForbiddenException($request);}(new self($container))->checkGetParameters($request,$request->getQueryParams());$user=$container->get('user');$database=$container->get('database');$condition='';$params=[];if(!empty($customerId)){$condition='UserId=?';$params[]=$customerId;}elseif($user->isCustomer){$condition='UserId=?';$params[]=$user->id;}if($user->isDealer){$condition.=(($condition!=='')?' AND ':'').'DeviceManagerId=?';$params[]=$user->id;}else{$condition.=(($condition!=='')?' AND ':'').'DeviceManagerId IS NOT NULL';}if(!empty($uuid)){$condition.=' AND sUUID=?';$params[]=ApiUuid::toHex($uuid);}elseif(!empty($request->getQueryParams()['clientUuid'])){$condition.=' AND sUUID=?';$params[]=ApiUuid::toHex($request->getQueryParams()['clientUuid']);}$sql='SELECT DISTINCT '.(new self($container))->getColumnNamesAsString().' FROM V_CLIENTS WHERE '.$condition;$result=$database->FetchObjects(($uuid===null),$sql,$params,__CLASS__,[$container]);return $result;}public function __isset($name){return parent::__isset($name)||in_array($name,['clientApiUrl','clientApiEnabled']);}public function __set(string $name,mixed $value){parent::__set($name,$value);}}