Skip to content

Commit

Permalink
Merge pull request #82 from jweiland-net/featureApi
Browse files Browse the repository at this point in the history
Add public API
  • Loading branch information
pascal20997 committed Jun 9, 2021
2 parents 293ac7a + 1591a06 commit f81cc83
Show file tree
Hide file tree
Showing 13 changed files with 492 additions and 28 deletions.
10 changes: 2 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Adapted from https://github.com/TYPO3GmbH/blog/blob/master/.github/workflows/ci.yml
name: CI

on: [push,pull_request]
on: [pull_request]

jobs:
build:
Expand Down Expand Up @@ -54,7 +54,6 @@ jobs:
- name: Install dependencies with typo3/cms-core:${{ matrix.typo3 }}
run: |
composer require typo3/cms-core:${{ matrix.typo3 }} --no-progress
composer require typo3/cms-extensionmanager:${{ matrix.typo3 }} --no-progress
git checkout composer.json
- name: php-cs-fixer
run: composer ci:php:fixer
Expand All @@ -63,10 +62,7 @@ jobs:
run: composer ci:php:lint

- name: Unit Tests
run: |
sudo timedatectl set-timezone Europe/Berlin
sudo systemctl start mysql.service
composer ci:tests:unit
run: composer ci:tests:unit

- name: Functional Tests
env:
Expand All @@ -76,6 +72,4 @@ jobs:
typo3DatabaseHost: 127.0.0.1
typo3DatabasePort: ${{ job.services.mysql.ports['3306'] }}
run: |
sudo timedatectl set-timezone Europe/Berlin
sudo systemctl start mysql.service
composer ci:tests:functional
43 changes: 25 additions & 18 deletions Classes/Domain/Repository/PeriodRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

namespace JWeiland\Reserve\Domain\Repository;

use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
use TYPO3\CMS\Extbase\Persistence\Repository;
Expand All @@ -25,6 +24,13 @@ class PeriodRepository extends Repository
'end' => QueryInterface::ORDER_ASCENDING
];

public function initializeObject()
{
$querySettings = $this->objectManager->get(Typo3QuerySettings::class);
$querySettings->setRespectStoragePage(false);
$this->setDefaultQuerySettings($querySettings);
}

/**
* @param array $uid
* @return QueryResultInterface
Expand All @@ -35,7 +41,6 @@ public function findByFacilityUids(array $uids): QueryResultInterface
$query = $query->matching(
$query->in('facility', $uids)
);
$query->getQuerySettings()->setRespectStoragePage(false);
return $query->execute();
}

Expand All @@ -52,7 +57,23 @@ public function findByDate(\DateTime $date, int $facilityUid): QueryResultInterf
$query->equals('facility', $facilityUid)
)
);
$query->getQuerySettings()->setRespectStoragePage(false);
return $query->execute();
}

public function findByDateAndBegin(\DateTime $dateTime, int $facilityUid): QueryResultInterface
{
$date = clone $dateTime;
$date->setTime(0, 0);
$begin = clone $dateTime;
$begin->setDate(1970, 1, 1);
$query = $this->createQuery();
$query->matching(
$query->logicalAnd(
$query->equals('facility', $facilityUid),
$query->equals('date', $date->getTimestamp()),
$query->equals('begin', $begin->getTimestamp())
)
);
return $query->execute();
}

Expand Down Expand Up @@ -82,18 +103,4 @@ public function findUpcomingAndRunningByFacilityUids(array $uids): QueryResultIn
);
return $query->execute();
}

public function findByFacilityUdddids(array $uids): QueryResultInterface
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_reserve_domain_model_period');
$queryBuilder
->select('*')
->from('tx_reserve_domain_model_period', 'p')
->leftJoin('p', 'tx_reserve_domain_model_facility', 'f')
->where($queryBuilder->expr()->in('f.uid', $queryBuilder->createNamedParameter($uids)));
$query = $this->createQuery();
$query->getQuerySettings()->setRespectStoragePage(false);
$query->statement($queryBuilder);
return $query->execute();
}
}
57 changes: 57 additions & 0 deletions Classes/Service/ReserveService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

/*
* This file is part of the package jweiland/reserve.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace JWeiland\Reserve\Service;

use JWeiland\Reserve\Domain\Repository\PeriodRepository;

/**
* Official public API class for ext:reserve
*
* @api
*/
class ReserveService
{
/**
* @var PeriodRepository
*/
protected $periodRepository;

public function __construct(PeriodRepository $periodRepository)
{
$this->periodRepository = $periodRepository;
}

/**
* @param int $facilityUid
* @param \DateTime $dateTime DateTime that contains the date of field "date" and time of field "begin"
* @return array
*/
public function findPeriodsByDateAndBegin(int $facilityUid, \DateTime $dateTime): array
{
return $this->periodRepository->findByDateAndBegin($dateTime, $facilityUid)->toArray();
}

/**
* @param int $facilityUid
* @param \DateTime $dateTimeOfPeriod DateTime that contains the date of field "date" and time of field "begin"
* @return int|null
*/
public function getRemainingParticipants(int $facilityUid, \DateTime $dateTimeOfPeriod): ?int
{
$periods = $this->findPeriodsByDateAndBegin($facilityUid, $dateTimeOfPeriod);
$remainingParticipants = null;
if ($periods) {
$remainingParticipants = reset($periods)->getRemainingParticipants();
}
return $remainingParticipants;
}
}
20 changes: 20 additions & 0 deletions Documentation/Extend reserve/Index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.. include:: ../Includes.txt

.. _extend-reserve:

==============
Extend reserve
==============

The class `JWeiland\Reserve\Service\ReserveService` is the official public API for ext:reserve.
You can use this class in your own extension to get some information like remaining participants of a period.

If you want even more, then you can use the other Service classes, Repositories and so on too but make sure that some
functionality can change in upcoming versions. We try to keep compatibility but sometimes breaking changes are required.


.. toctree::
:maxdepth: 2

ReserveService/Index
UpdateTemplates/Index
99 changes: 99 additions & 0 deletions Documentation/Extend reserve/ReserveService/Index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
.. include:: ../Includes.txt

.. reserve-service:
===============
Reserve service
===============

The class `JWeiland\Reserve\Service\ReserveService` is the official public API for ext:reserve.
You can use this class in your own extension to get some information like remaining participants of a period.

The following example shows a controller that uses FlexForms or TypoScript for Facility and DateTime selection. This selection will be used
to get the remaining participants of a matching period.

.. code-block:: php
<?php
declare(strict_types=1);
/*
* This file is part of the package my/example.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/
namespace My\Example\Controller;
use JWeiland\Reserve\Service\ReserveService;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
class ExampleController extends ActionController
{
/**
* @var ReserveService
*/
protected $reserveService;
public function __construct(ReserveService $reserveService)
{
$this->reserveService = $reserveService;
}
public function showAction(): void
{
$dateTime = new \DateTime();
$dateTime->setTimestamp((int)$this->settings['dateTimeOfEvent']);
$this->view->assign(
'remainingParticipants',
$this->reserveService->getRemainingParticipants((int)$this->settings['facility'], $dateTime)
);
}
}
The FlexForms.xml may look like this.

.. code-block:: xml
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<T3DataStructure>
<sheets>
<sDEF>
<ROOT>
<TCEforms>
<sheetTitle>Main</sheetTitle>
</TCEforms>
<type>array</type>
<el>
<settings.dateTimeOfEvent>
<label>Date of the event</label>
<config>
<type>input</type>
<size>10</size>
<renderType>inputDateTime</renderType>
<eval>datetime</eval>
<default>0</default>
</config>
</settings.dateTimeOfEvent>
<settings.facility>
<TCEforms>
<label>Choose a Facility</label>
<config>
<type>group</type>
<internal_type>db</internal_type>
<allowed>tx_reserve_domain_model_facility</allowed>
<maxitems>1</maxitems>
<minitems>1</minitems>
<size>1</size>
<default>0</default>
</config>
</TCEforms>
</settings.facility>
</el>
</ROOT>
</sDEF>
</sheets>
</T3DataStructure>
47 changes: 47 additions & 0 deletions Documentation/Extend reserve/UpdateTemplates/Index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
.. include:: ../Includes.txt

.. _update-templates:

================
Update templates
================

This extension uses fluid templates so you are able to override them like in most other TYPO3 extensions.

Override template using TypoScript
----------------------------------

Do you have a site package? Put the templates into your site package extension. Otherwise you can put them into fileadmin if you really want.

.. code-block:: typoscript
plugin.tx_reserve {
view {
templateRootPaths >
templateRootPaths {
0 = EXT:reserve/Resources/Private/Templates/
1 = EXT:site_package/Resources/Private/Extensions/reserve/Templates/
}
partialRootPaths >
partialRootPaths {
0 = EXT:reserve/Resources/Private/Partials/
1 = EXT:site_package/Resources/Private/Extensions/reserve/Partials/
}
layoutRootPaths >
layoutRootPaths {
0 = EXT:reserve/Resources/Private/Layouts/
1 = EXT:site_package/Resources/Private/Extensions/reserve/Layouts/
}
}
}
Override template using constant editor
---------------------------------------

TYPO3 Backend > Templates > Constant Editor

.. figure:: ../../Images/TemplatesConstantEditor.png
:class: with-shadow
:alt: Constant editor
:width: 500px
Binary file added Documentation/Images/TemplatesConstantEditor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions Documentation/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ Just click on "Edit me on GitHub" on the top right to submit your change request
Introduction/Index
Installation/Index
Configuration/Index
Extend reserve/Index
Changelog/Index
Sitemap
2 changes: 1 addition & 1 deletion Tests/Functional/Command/RemovePastPeriodsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected function setUp(): void
// set date to past date for this tests!
GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('tx_reserve_domain_model_period')
->update('tx_reserve_domain_model_period', ['date' => '1608768000'], ['uid' => 1]);
->update('tx_reserve_domain_model_period', ['date' => (new \DateTime('yesterday midnight'))->getTimestamp()], ['uid' => 1]);
}

/**
Expand Down
24 changes: 24 additions & 0 deletions Tests/Functional/Fixtures/example_facility_with_period.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,28 @@
<booking_begin>1596106800</booking_begin>
<date>2051218800</date>
</tx_reserve_domain_model_period>

<tx_reserve_domain_model_period>
<uid>2</uid>
<pid>1</pid>
<facility>1</facility>
<begin>70200</begin>
<end>78300</end>
<max_participants>50</max_participants>
<max_participants_per_order>3</max_participants_per_order>
<booking_begin>1623142380</booking_begin>
<date>2051478000</date>
</tx_reserve_domain_model_period>

<tx_reserve_domain_model_period>
<uid>3</uid>
<pid>1</pid>
<facility>1</facility>
<begin>60300</begin>
<end>72000</end>
<max_participants>50</max_participants>
<max_participants_per_order>3</max_participants_per_order>
<booking_begin>1623142380</booking_begin>
<date>2051391600</date>
</tx_reserve_domain_model_period>
</dataset>
Loading

0 comments on commit f81cc83

Please sign in to comment.