JFIF $ d d 7 * Z qn )ZP 0 !1 A "#a$2Qr D8 a Ri [f \m I ykI w0 c u F c Rı?lO7 к _f˓ [C $殷WF< _W ԣ s Kc ëI zyQy /_LK ℂ ;C",p F A : /] =H ~ , l s / 9 ć: [=/#f ; ) x { ٛE Q )~ = 𘙲r*2~ a _V = ' ku m F D } KYYC )( { * g& f `툪 r y ` = ^ c J. I ]( * `wq 1dđ #̩ ͑ 0 ; H ] u 搂 @:~ ו K L N s h } O I R* 8 :2 ! lD JV o ( 3 = M ( z Ȱ+i *N A r6K n S l ) ! J J Ӂ* % ݉?|D } d5: eP0 R; {$X' x F @ .ÊB {,WJ uQ ɲRI ;9 Q E琯 6 2fT .DU J ;*c P A\IL Nj! J ۱+ O\ ͔ ] ޒ S J ȧ c%A N ol ՎprU L Zԛe r E2= X D Xg VQeӓ k y P7 U* o m Q I s ,K `) 6\G3t?pgjr m ۛجw luGt f h 9uyP 0D;Uڽ"OXl i f $ ) &|ML 0 Z rm1 [ H XP l P R0 'G=i 2N +0 e 2 ]] 9 VT P O 7 h(F * 癈 ' = Q VZD F,d ߬ ~ T X G[ `le 69C R(!S2! P < 0 x < !1AQ "Raq 02Br #S CTb ? Ζ "]mH5WR 7 k.ۛ ! } Q~ + yԏ z |@ T20 S~ K e k *zF f^2X *( @ 8 r ? C I uI|֓>^Ex LgNU Y+{.R Ѫ τV YT D I 6 2 '8Y2 7'\T P.6 d &˦ @V q i |8 -O Ε ] ʔ U= T L 8= ;6c | !qfF3 a ů& ~$ l} 'NW U s$U k^S V : U#6w + +s &r+n ڐ { @ 29gL u " T Ù M=6 (^ "7 r}=6Yݾ lC u hquymp Ǧ G jhs ǜNl ɻ} o 7 #S6 a w 4 ! OS r D 5 7 % | ?x> L | / nD 6?/8 w # [ ) L 7 +6〼T A T g ! %5 Mm Z /c -{1_Je "| ^$ 'O & ޱմ < H vNqϡQ$ < 6 &Nݛ ֨tǻN (9 n ru~'όN sMG& ,uH Kw *Ī 5@ u6 BԒ i YN * f 0 _ GhKW u lT <nj- 0{ M L~ | /a v\ E = ?1 \ɟuq Ђ Y O a K ޡP q $ H8o Q˼EV ~ k 0 h' L Pp M -M" m^ !6l nմ 쮝C U T . ! :vMǨb h ډ Vc r @~p # !1QAa q ? AL0L x LU 4 d ; x " 16ad0 7.\ߋ 0 = P l v/ " c A Ay A|2pO8"d MV #1] = \ " ˡ ] k v I9 d ۓ & q ) ǐq E ꞅ v "P )q X &Q З non]я u ג./ ; gZ$ M QР 2M ]h ni 0 P $ێ 3CɏGjώy : m c # S_ l@ '_ \m|C 2`K _ 1 cIvGc $? PCQ S+D ? K aײ k Fr b 8 ύ NEدg ĝ U 3 6 #Ik+ v { v i ( ވ ? a e) X xy/*@Q Ǽ Ծ ) $ IWO l c G; !i k{ gOŀ L ('x T rb $ w) R $ & N1E td U3Uȉ1p M"N* (DNy d96.(jQ) X 5 c QɎMy W ? Q *!R > 6=7)Xj 5`J ] e 8%t ! + ' ! 1Q 5 !1 AQaq ё #2 "0BRb ? G t^ ## .l lQ T $ v , ,m 㵜5u bV =sY+ @d { N ! dn O < .- B; _w J t 6;Q Jd. Q c%p{ 1 ,sND dF H I0 Гo X ш e黅X ۢ F :)[ F G X ƹ / w _c MeD ,ʡc c . W D tA $ j@ : ) - #u c1< @ۗ9F ) KJ- hpP] _ x [ q B lb p ʖ w q " LFGd ƶ * s +ډ_Zc " ? % t[I P 6 J ]# =ɺ V v v CGs Gh1 > )6 |e y ? L ӣ m,4 G W Ui ` ] uJVo VDG< SB 6ϏQ @ TiU l y O U 0 kfV ~ ~}S Z @ *WU Ui ## ; s /[ = ! 7}" W N ] ' (L ! ~ y5 g9T̅J kbM' +s :S +B) v @ M j e C f j E 0 Y\Qn z G 1 д~ W o{T 9?` Rm yhsy3 !HAD ] m c1 ~ 2 L S u7x T ;j$ `} 4-> L# vzŏ I L S ֭ T { r jGK C ; b pU= - ` Bs K. S F w4 M q] Z dH S 0 ) tLg
<?php
namespace AmeliaBooking\Infrastructure\Repository\Location;
use AmeliaBooking\Domain\Collection\Collection;
use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
use AmeliaBooking\Domain\Entity\Location\Location;
use AmeliaBooking\Domain\Factory\Bookable\Service\ServiceFactory;
use AmeliaBooking\Domain\Repository\Location\LocationRepositoryInterface;
use AmeliaBooking\Domain\Services\DateTime\DateTimeService;
use AmeliaBooking\Domain\ValueObjects\String\Status;
use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
use AmeliaBooking\Infrastructure\Connection;
use AmeliaBooking\Infrastructure\Repository\AbstractRepository;
use AmeliaBooking\Domain\Factory\Location\LocationFactory;
use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Bookable\CategoriesTable;
use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Bookable\ServicesTable;
use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Booking\AppointmentsTable;
use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Booking\EventsPeriodsTable;
use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Booking\EventsTable;
use AmeliaBooking\Infrastructure\WP\InstallActions\DB\User\Provider\ProvidersLocationTable;
use AmeliaBooking\Infrastructure\WP\InstallActions\DB\User\Provider\ProvidersPeriodLocationTable;
use AmeliaBooking\Infrastructure\WP\InstallActions\DB\User\Provider\ProvidersPeriodTable;
use AmeliaBooking\Infrastructure\WP\InstallActions\DB\User\Provider\ProvidersServiceTable;
use AmeliaBooking\Infrastructure\WP\InstallActions\DB\User\UsersTable;
/**
* Class LocationRepositoryInterface
*
* @package AmeliaBooking\Infrastructure\Repository
*/
class LocationRepository extends AbstractRepository implements LocationRepositoryInterface
{
public const FACTORY = LocationFactory::class;
public const SERVICE_FACTORY = ServiceFactory::class;
/** @var string */
protected $providerServicesTable;
/** @var string */
protected $providerLocationTable;
/** @var string */
protected $servicesTable;
/** @var string */
protected $locationViewsTable;
/**
* @param Connection $connection
* @param string $table
* @param string $providerLocationTable
* @param string $providerServicesTable
* @param string $servicesTable
* @param $locationViewsTable
*/
public function __construct(
Connection $connection,
$table,
$providerLocationTable,
$providerServicesTable,
$servicesTable,
$locationViewsTable
) {
parent::__construct($connection, $table);
$this->providerServicesTable = $providerServicesTable;
$this->providerLocationTable = $providerLocationTable;
$this->servicesTable = $servicesTable;
$this->locationViewsTable = $locationViewsTable;
}
/**
* @param Location $location
*
* @return int
* @throws QueryExecutionException
*/
public function add($location)
{
$data = $location->toArray();
$params = [
':status' => $data['status'],
':name' => $data['name'],
':description' => $data['description'],
':address' => $data['address'],
':phone' => $data['phone'],
':latitude' => $data['latitude'],
':longitude' => $data['longitude'],
':pictureFullPath' => $data['pictureFullPath'],
':pictureThumbPath' => $data['pictureThumbPath'],
':pin' => $data['pin'],
':translations' => $data['translations'],
':countryPhoneIso' => isset($data['countryPhoneIso']) ? $data['countryPhoneIso'] : null
];
try {
$statement = $this->connection->prepare(
"INSERT INTO {$this->table}
(
`status`,
`name`,
`description`,
`address`,
`phone`,
`latitude`,
`longitude`,
`pictureFullPath`,
`pictureThumbPath`,
`pin`,
`translations`,
`countryPhoneIso`
)
VALUES (
:status,
:name,
:description,
:address,
:phone,
:latitude,
:longitude,
:pictureFullPath,
:pictureThumbPath,
:pin,
:translations,
:countryPhoneIso
)"
);
$statement->execute($params);
} catch (\Exception $e) {
throw new QueryExecutionException('Unable to add data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e);
}
return $this->connection->lastInsertId();
}
/**
* @param int $id
* @param Location $location
*
* @return bool
* @throws QueryExecutionException
*/
public function update($id, $location)
{
$data = $location->toArray();
$params = [
':status' => $data['status'],
':name' => $data['name'],
':description' => $data['description'],
':address' => $data['address'],
':phone' => $data['phone'],
':latitude' => $data['latitude'],
':longitude' => $data['longitude'],
':pictureFullPath' => $data['pictureFullPath'],
':pictureThumbPath' => $data['pictureThumbPath'],
':pin' => $data['pin'],
':translations' => $data['translations'],
':id' => $id,
':countryPhoneIso' => isset($data['countryPhoneIso']) ? $data['countryPhoneIso'] : null
];
try {
$statement = $this->connection->prepare(
"UPDATE {$this->table}
SET `status` = :status, `name` = :name, `description` = :description, `address` = :address,
`phone` = :phone, `latitude` = :latitude, `longitude` = :longitude,
`pictureFullPath` = :pictureFullPath, `pictureThumbPath` = :pictureThumbPath,
`pin` = :pin, `translations` = :translations, `countryPhoneIso` = :countryPhoneIso
WHERE id = :id"
);
$statement->execute($params);
return true;
} catch (\Exception $e) {
throw new QueryExecutionException('Unable to save data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e);
}
}
/**
* @return Collection
* @throws InvalidArgumentException
* @throws QueryExecutionException
*/
public function getAllOrderedByName()
{
try {
$statement = $this->connection->query(
"SELECT * FROM {$this->table} ORDER BY name"
);
$rows = $statement->fetchAll();
} catch (\Exception $e) {
throw new QueryExecutionException('Unable to get data from ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e);
}
$items = new Collection();
foreach ($rows as $row) {
$items->addItem(call_user_func([static::FACTORY, 'create'], $row), $row['id']);
}
return $items;
}
/**
* @param array $criteria
* @param int $itemsPerPage
*
* @return Collection
* @throws InvalidArgumentException
* @throws QueryExecutionException
*/
public function getFiltered($criteria, $itemsPerPage)
{
$params = [];
$order = '';
if (!empty($criteria['sort'])) {
$orderColumn = $criteria['sort']['field'];
$orderDirection = $criteria['sort']['order'];
$order = "ORDER BY {$orderColumn} {$orderDirection}";
}
$search = '';
if (!empty($criteria['search'])) {
$terms = preg_split('/\s+/', trim($criteria['search']));
$termIndex = 0;
$where = [];
foreach ($terms as $term) {
$param = ":search{$termIndex}";
$params[$param] = "%{$term}%";
$where[] = "(
l.name LIKE {$param}
OR l.address LIKE {$param}
OR l.id LIKE {$param}
)";
$termIndex++;
}
$search = ' AND (' . implode(' AND ', $where) . ')';
}
$services = '';
if (!empty($criteria['services'])) {
foreach ((array)$criteria['services'] as $index => $value) {
++$index;
$services .= ':service' . $index . ', ';
$params[':service' . $index] = $value;
}
$services = ' AND s.id IN (' . rtrim($services, ', ') . ')';
}
$status = '';
if (isset($criteria['status'])) {
$status = ' AND l.status = :status';
$params[':status'] = $criteria['status'];
}
$limit = $this->getLimit(
!empty($criteria['page']) ? (int)$criteria['page'] : 0,
(int)$itemsPerPage
);
try {
$statement = $this->connection->prepare(
"SELECT
l.id,
l.status,
l.name,
l.description,
l.address,
l.phone,
l.latitude,
l.longitude,
l.pictureFullPath,
l.pictureThumbPath,
l.pin,
l.translations,
l.countryPhoneIso
FROM {$this->table} l
LEFT JOIN {$this->providerLocationTable} pl ON pl.locationId = l.id
LEFT JOIN {$this->providerServicesTable} ps ON ps.userId = pl.userId
LEFT JOIN {$this->servicesTable} s ON s.id = ps.serviceId
WHERE 1 = 1 $search $services $status
GROUP BY l.id
{$order}
{$limit}"
);
$statement->execute($params);
$rows = $statement->fetchAll();
} catch (\Exception $e) {
throw new QueryExecutionException('Unable to get data from ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e);
}
$items = [];
foreach ($rows as $row) {
$items[] = call_user_func([static::FACTORY, 'create'], $row);
}
return new Collection($items);
}
/**
* @param $criteria
*
* @return array
* @throws InvalidArgumentException
* @throws QueryExecutionException
*/
public function getCount($criteria)
{
$providerLocationTable = ProvidersLocationTable::getTableName();
$providerServicesTable = ProvidersServiceTable::getTableName();
$servicesTable = ServicesTable::getTableName();
$params = [];
$search = '';
if (!empty($criteria['search'])) {
$terms = preg_split('/\s+/', trim($criteria['search']));
$termIndex = 0;
$where = [];
foreach ($terms as $term) {
$param = ":search{$termIndex}";
$params[$param] = "%{$term}%";
$where[] = "(
l.name LIKE {$param}
OR l.address LIKE {$param}
OR l.id LIKE {$param}
)";
$termIndex++;
}
$search = ' AND (' . implode(' AND ', $where) . ')';
}
$services = '';
if (!empty($criteria['services'])) {
foreach ((array)$criteria['services'] as $index => $value) {
++$index;
$services .= ':service' . $index . ', ';
$params[':service' . $index] = $value;
}
$services = ' AND s.id IN (' . rtrim($services, ', ') . ')';
}
try {
$statement = $this->connection->prepare(
"SELECT COUNT(*) as count
FROM (
SELECT l.id
FROM {$this->table} l
LEFT JOIN {$providerLocationTable} pl ON pl.locationId = l.id
LEFT JOIN {$providerServicesTable} ps ON ps.userId = pl.userId
LEFT JOIN {$servicesTable} s ON s.id = ps.serviceId
WHERE l.status IN (:visibleStatus, :hiddenStatus) $search $services
GROUP BY l.id
) as t"
);
$params[':visibleStatus'] = Status::VISIBLE;
$params[':hiddenStatus'] = Status::HIDDEN;
$statement->execute($params);
$rows = $statement->fetch()['count'];
} catch (\Exception $e) {
throw new QueryExecutionException('Unable to get data from ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e);
}
return $rows;
}
/**
* @param $id
*
* @return Collection
* @throws InvalidArgumentException
* @throws QueryExecutionException
*/
public function getServicesById($id)
{
$params = [
':id' => $id
];
try {
$statement = $this->connection->prepare(
"
SELECT s.*
FROM {$this->table} l
INNER JOIN {$this->providerLocationTable} pl ON pl.locationId = l.id
INNER JOIN {$this->providerServicesTable} ps ON ps.userId = pl.userId
INNER JOIN {$this->servicesTable} s ON s.id = ps.serviceId
WHERE l.id = :id
GROUP BY s.id"
);
$statement->execute($params);
$rows = $statement->fetchAll();
} catch (\Exception $e) {
throw new QueryExecutionException('Unable to get data from ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e);
}
$items = [];
foreach ($rows as $row) {
$items[] = call_user_func([static::SERVICE_FACTORY, 'create'], $row);
}
return new Collection($items);
}
/**
* Return an array of locations with the number of appointments for the given date period.
* Keys of the array are Locations IDs.
*
* @param $criteria
*
* @return array
* @throws QueryExecutionException
* @throws \AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException
*/
public function getAllNumberOfAppointments($criteria)
{
$userTable = UsersTable::getTableName();
$appointmentTable = AppointmentsTable::getTableName();
$params = [];
$where = [];
if ($criteria['dates']) {
$where[] = "(a.bookingStart BETWEEN :bookingFrom AND :bookingTo)";
$params[':bookingFrom'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]);
$params[':bookingTo'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][1]);
}
if (isset($criteria['status'])) {
$where[] = 'l.status = :status';
$params[':status'] = $criteria['status'];
}
$where = $where ? 'WHERE ' . implode(' AND ', $where) : '';
try {
$statement = $this->connection->prepare(
"SELECT
l.id,
l.name,
COUNT(l.id) AS appointments
FROM {$this->table} l
INNER JOIN {$this->providerLocationTable} pl ON pl.locationId = l.id
INNER JOIN {$userTable} u ON u.id = pl.userId
INNER JOIN {$appointmentTable} a ON u.id = a.providerId
$where
GROUP BY l.id"
);
$statement->execute($params);
$rows = $statement->fetchAll();
} catch (\Exception $e) {
throw new QueryExecutionException('Unable to get data from ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e);
}
$result = [];
foreach ($rows as $row) {
$result[$row['id']] = $row;
}
return $result;
}
/**
* Return an array of locations with the number of views for the given date period.
* Keys of the array are Locations IDs.
*
* @param $criteria
*
* @return array
* @throws QueryExecutionException
*/
public function getAllNumberOfViews($criteria)
{
$params = [];
$where = [];
if ($criteria['dates']) {
$where[] = "(lv.date BETWEEN :bookingFrom AND :bookingTo)";
$params[':bookingFrom'] = explode(' ', $criteria['dates'][0])[0];
$params[':bookingTo'] = explode(' ', $criteria['dates'][1])[0];
}
if (isset($criteria['status'])) {
$where[] = 'l.status = :status';
$params[':status'] = $criteria['status'];
}
$where = $where ? 'WHERE ' . implode(' AND ', $where) : '';
try {
$statement = $this->connection->prepare(
"SELECT
l.id,
l.name,
SUM(lv.views) AS views
FROM {$this->table} l
INNER JOIN {$this->locationViewsTable} lv ON lv.locationId = l.id
$where
GROUP BY l.id"
);
$statement->execute($params);
$rows = $statement->fetchAll();
} catch (\Exception $e) {
throw new QueryExecutionException('Unable to get data from ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e);
}
$result = [];
foreach ($rows as $row) {
$result[$row['id']] = $row;
}
return $result;
}
/**
* @param $locationId
*
* @return boolean
* @throws QueryExecutionException
*/
public function addViewStats($locationId)
{
$date = DateTimeService::getNowDate();
$params = [
':locationId' => $locationId,
':date' => $date,
':views' => 1
];
try {
// Check if there is already data for this provider for this date
$statement = $this->connection->prepare(
"SELECT COUNT(*) AS count
FROM {$this->locationViewsTable} AS pv
WHERE pv.locationId = :locationId
AND pv.date = :date"
);
$statement->bindParam(':locationId', $locationId);
$statement->bindParam(':date', $date);
$statement->execute();
$count = $statement->fetch()['count'];
if (!$count) {
$statement = $this->connection->prepare(
"INSERT INTO {$this->locationViewsTable}
(`locationId`, `date`, `views`)
VALUES
(:locationId, :date, :views)"
);
} else {
$statement = $this->connection->prepare(
"UPDATE {$this->locationViewsTable} pv SET pv.views = pv.views + :views
WHERE pv.locationId = :locationId
AND pv.date = :date"
);
}
$statement->execute($params);
} catch (\Exception $e) {
throw new QueryExecutionException('Unable to add data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e);
}
return true;
}
/**
* @param int $locationId
*
* @return mixed
* @throws QueryExecutionException
*/
public function deleteViewStats($locationId)
{
$params = [
':locationId' => $locationId,
];
try {
$statement = $this->connection->prepare(
"DELETE FROM {$this->locationViewsTable} WHERE locationId = :locationId"
);
$statement->execute($params);
return true;
} catch (\Exception $e) {
throw new QueryExecutionException('Unable to delete data from ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e);
}
}
/**
* @param $id
*
* @return Collection
* @throws InvalidArgumentException
* @throws QueryExecutionException
*/
public function getByIdWithEntities($id)
{
$usersTable = UsersTable::getTableName();
$providerPeriodsTable = ProvidersPeriodTable::getTableName();
$providerPeriodsLocations = ProvidersPeriodLocationTable::getTableName();
$eventsTable = EventsTable::getTableName();
$categoriesTable = CategoriesTable::getTableName();
$eventPeriodsTable = EventsPeriodsTable::getTableName();
$params = [
':id' => $id,
':eventLocationId' => $id
];
try {
$statement = $this->connection->prepare("
SELECT
l.id AS location_id,
l.status AS location_status,
l.name AS location_name,
l.description AS location_description,
l.address AS location_address,
l.phone AS location_phone,
l.latitude AS location_latitude,
l.longitude AS location_longitude,
l.pictureFullPath AS location_pictureFullPath,
l.pictureThumbPath AS location_pictureThumbPath,
l.pin AS location_pin,
l.translations AS location_translations,
pu.id AS provider_id,
pu.firstName AS provider_firstName,
pu.lastName AS provider_lastName,
pu.email AS provider_email,
pu.phone AS provider_phone,
pu.pictureThumbPath AS provider_pictureThumbPath,
pu.pictureFullPath AS provider_pictureFullPath,
s.id AS service_id,
s.name AS service_name,
s.color AS service_color,
c.id AS category_id,
c.name AS category_name,
e.id AS event_id,
e.name AS event_name,
e.color AS event_color,
ep.id AS event_periodId,
ep.periodStart AS event_periodStart,
ep.periodEnd AS event_periodEnd,
ep.zoomMeeting AS event_periodZoomMeeting,
ep.lessonSpace AS event_periodLessonSpace,
ep.googleMeetUrl AS event_googleMeetUrl
FROM {$this->table} l
LEFT JOIN {$this->providerLocationTable} pl ON pl.locationId = l.id
LEFT JOIN {$providerPeriodsTable} pp ON pp.locationId = l.id
LEFT JOIN {$providerPeriodsLocations} ppl ON ppl.locationId = l.id
LEFT JOIN {$usersTable} pu ON pu.id = pl.userId
LEFT JOIN {$this->providerServicesTable} ps ON ps.userId = pl.userId
LEFT JOIN (SELECT * FROM {$this->servicesTable} LIMIT 11) s ON s.id = ps.serviceId
LEFT JOIN {$categoriesTable} c ON c.id = s.categoryId
LEFT JOIN (SELECT * FROM {$eventsTable} WHERE locationId = :eventLocationId LIMIT 11 ) e ON e.locationId = l.id
LEFT JOIN {$eventPeriodsTable} ep ON ep.eventId = e.id
WHERE l.id = :id
");
$statement->execute($params);
$rows = $statement->fetchAll();
} catch (\Exception $e) {
throw new QueryExecutionException('Unable to get data from ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e);
}
return call_user_func([static::FACTORY, 'createCollection'], $rows);
}
}