<?php
namespace Whater\Application\UseCase\Whater;
use Whater\Application\UseCase\AbstractCommandHandler;
use Whater\Application\UseCase\Whater\CommandRequest\AppListMyWhaterPointsCommand;
use Whater\Domain\Whater\Repository\WhaterPointRepositoryInterface;
use Pagerfanta\Pagerfanta;
use Hateoas\HateoasBuilder;
use Whater\Domain\User\Model\Role;
/**
* Class AppListMyWhaterPointsHandler
*
* @package Whater\Application\UseCase\Whater
*/
class AppListMyWhaterPointsHandler extends AbstractCommandHandler
{
/**
* @var WhaterPointRepositoryInterface
*/
private $repositoryWhaterPoint;
/**
* AppListMyWhaterPointsHandler constructor.
* @param WhaterPointRepositoryInterface $repositoryWhaterPoint
*/
public function __construct(WhaterPointRepositoryInterface $repositoryWhaterPoint)
{
$this->repositoryWhaterPoint = $repositoryWhaterPoint;
}
/**
* @param AppListMyWhaterPointsCommand $appListMyWhaterPointsCommand
* @return Pagerfanta
*/
public function handle(AppListMyWhaterPointsCommand $appListMyWhaterPointsCommand): Pagerfanta
{
//$this->logger->debug("handle appListMyWhaterPointsCommand");
$sort = array_combine(
explode(',', $appListMyWhaterPointsCommand->orderParameter()) ?? [],
explode(',', $appListMyWhaterPointsCommand->orderValue()) ?? []
);
$limit = $appListMyWhaterPointsCommand->limit() ?? AppListMyWhaterPointsCommand::LIMIT;
$page = $appListMyWhaterPointsCommand->page() ?? AppListMyWhaterPointsCommand::PAGE;
$globalFilter = null;
$hasGeopositionFilter = null;
$hasDistributionNetworkFilter = null;
$countryIds = null;
$countryIso = null;
$countryIso3 = null;
$countryAreaIds = null;
$townIdFilter = null;
$townSlugFilter = null;
$townNameFilter = null;
$countryAreaNameFilter = null;
$distributionNetworkNameFilter = null;
$distributionNetworkIdFilter = null;
$distributionNetworkTypeFilter = null;
$whaterPointNameFilter = null;
$whaterPointStatusFilter = null;
$whaterDeviceFilter = null;
$whaterpointTypeFilter = null;
$validatedByAppTypeFilter = null;
$userResponsableIdFilter = null;
$hasProductFilter = null;
$createdByIdFilter = $this->getGrantedUser()->id();
// $grantUser = $appListMyWhaterPointsCommand->user();
// if (!$grantUser->hasRole(Role::ROLE_ADMIN)) {
// if ($grantUser->whaterOrganization() != null) {
// // filtra por licencia
// $countryIds = [];
// $countryAreaIds = [];
// $now = new \DateTime();
// foreach ($grantUser->whaterOrganization()->organizationLicenses() as $organizationLicense) {
// if ($organizationLicense->startDate() < $now && $organizationLicense->endDate() > $now) {
// foreach ($organizationLicense->countries() as $country) {
// // $this->logger->debug('countryId: ' . $country->id() . ' ' . $country->name());
// array_push($countryIds, $country->id());
// }
// foreach ($organizationLicense->countryAreas() as $countryArea) {
// // $this->logger->debug('countryAreaIds: ' . $countryArea->id() . ' ' . $countryArea->name());
// array_push($countryAreaIds, $countryArea->id());
// }
// }
// }
// if (count($countryIds) == 0 && count($countryAreaIds) > 0) {
// $countryIds = null;
// } else if (count($countryIds) > 0 && count($countryAreaIds) == 0) {
// $countryAreaIds = null;
// }
// } else {
// //solo muestra 1 municipio de forma individual
// if ($grantUser->town() != null) {
// $townSlugFilter = $grantUser->town()->slug();
// } else {
// $townSlugFilter = 'any-town-selected';
// }
// }
// }
$filterParameters = explode(',', $appListMyWhaterPointsCommand->filterParameter());
$filterValues = json_decode($appListMyWhaterPointsCommand->filterValue());
$i = 0;
foreach ($filterParameters as $filterParameter) {
if ($filterParameter == "tablesearch") {
$globalFilter = $filterValues[$i];
} elseif ($filterParameter == "country_iso") {
$countryIso = $filterValues[$i];
} elseif ($filterParameter == "country_iso3") {
$countryIso3 = $filterValues[$i];
} elseif ($filterParameter == "town_slug") {
$townSlugFilter = $filterValues[$i];
$countryIds = null;
$countryAreaIds = null;
} elseif ($filterParameter == "town_uuid") {
$townIdFilter = $filterValues[$i];
$countryIds = null;
$countryAreaIds = null;
} elseif ($filterParameter == "town_name") {
$townNameFilter = $filterValues[$i];
} elseif ($filterParameter == "whater_point_status") {
$whaterPointStatusFilter = $filterValues[$i];
if ($whaterPointStatusFilter == "ALL") {
$whaterPointStatusFilter = null;
}
} elseif ($filterParameter == "country_area_name") {
$countryAreaNameFilter = $filterValues[$i];
} elseif ($filterParameter == "distribution_network_uuid") {
$distributionNetworkIdFilter = $filterValues[$i];
} elseif ($filterParameter == "whater_point_name") {
$whaterPointNameFilter = $filterValues[$i];
} elseif ($filterParameter == "whater_point_type") {
$whaterpointTypeFilter = $filterValues[$i];
if ($whaterpointTypeFilter == "ALL") {
$whaterPointStatusFilter = null;
}
} elseif ($filterParameter == "distribution_network_name") {
$distributionNetworkNameFilter = $filterValues[$i];
} elseif ($filterParameter == "has_geoposition") {
$hasGeopositionFilter = $filterValues[$i] == "true" ? true : false;
} elseif ($filterParameter == "whater_point_distribution_network_type") {
$distributionNetworkTypeFilter = $filterValues[$i];
} elseif ($filterParameter == "has_distribution_network") {
$hasDistributionNetworkFilter = $filterValues[$i] == "true" ? true : false;
} elseif ($filterParameter == "whater_device_uuid") {
$whaterDeviceFilter = $filterValues[$i];
} elseif ($filterParameter == "whater_point_validated_by_app") {
$validatedByAppTypeFilter = $filterValues[$i];
} elseif ($filterParameter == "has_product") {
$hasProductFilter = $filterValues[$i];
}
$i++;
}
$paginatedCollection = $this->repositoryWhaterPoint->findAllPaginated(
$sort,
$limit,
$page,
$countryIds,
$countryAreaIds,
$countryAreaNameFilter,
$countryIso,
$countryIso3,
null,
$townIdFilter,
$townSlugFilter,
$townNameFilter,
null,
null,
$whaterPointNameFilter,
$whaterPointStatusFilter,
null,
$distributionNetworkIdFilter,
$distributionNetworkNameFilter,
$hasGeopositionFilter,
$hasDistributionNetworkFilter,
$whaterDeviceFilter,
$globalFilter,
null,
$whaterpointTypeFilter,
$distributionNetworkTypeFilter,
$userResponsableIdFilter,
$createdByIdFilter,
$validatedByAppTypeFilter,
$hasProductFilter
);
$paginatedCollection->setMaxPerPage($limit);
$paginatedCollection->setCurrentPage($page);
return $paginatedCollection;
}
}