简体   繁体   中英

Typo3 cal (calendar Base) use event objects in foreign extension

I'm working on an extension that's depending on cal base. I fetch events from other sources by crawling their websites.

I'm slowly converting to extbase and I'd like to know if or how it is possible to access cal base events from my extension.

I know that I could just access the tables using mapOnProperty. But then I would have to rewrite the whole logic, too.

I wonder if it's possible to just use the objects of calendar base.

I tried to write a test:

<?php
class CalEventTest extends \TYPO3\CMS\Core\Tests\BaseTestCase {

    /**
      * @test
      */
    public function anInstanceOfCalEventCanBeConstructed() {
            $calEventService = & \TYPO3\CMS\Cal\Utility\Functions::getEventService ();
            // $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Extbase\Object\ObjectManager');
            // $calEventService = $objectManager->get('TYPO3\CMS\Cal\Service\EventService');
            // $calEventService = new TYPO3\CMS\Cal\Service\EventService();
            // $events = $calEventService->findAll(array(1)); 
   }       
}

You see different attempts commented out. They all failed in one or the other way. CalEventService looked promising but it also doesn't work. The error for the last approach is.

Call to a member function isLoggedIn() on null in /var/www/clients/client4/web47/web/typo3conf/ext/cal/Classes/Service/NearbyEventService.php on line 27

I wonder if this approach is possible at all before trying some more (the mentioned error seems connected to the test environment context).

I'm using Typo3 6.2.27 and cal 1.10.1.

Not jet tested, but as the Manual says you should access the EventService like this:

$storagePageID = 123;
/** @var \TYPO3\CMS\Cal\Service\EventService $eventService **/
$eventService = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstanceService('tx_cal_phpicalendar', 'cal_event_model');
$events = $eventService->findAll($storagePageID);

I found the cal API which looks promising

/**
  * @test
  */
public function calApiCanBeAccessed() {
    $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Extbase\Object\ObjectManager');
    $calAPIPre = $objectManager->get('TYPO3\CMS\Cal\Controller\Api');

    $this->assertInstanceOf(TYPO3\CMS\Cal\Controller\Api::class, $calAPIPre);
    $this->assertObjectHasAttribute('prefixId', $calAPIPre);

    $pidList = "1095, 80";

    $calAPI = $calAPIPre->tx_cal_api_without($pidList);

    $this->assertInstanceOf(TYPO3\CMS\Cal\Controller\Api::class, $calAPI);
    $this->assertObjectHasAttribute('prefixId', $calAPI);
    $this->assertInstanceOf(TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class, $calAPI->cObj);
}

and successfully returns an event object

/**
  * @test                                                                                                  
  */
public function calEventCanBeFoundThroughApi() {
    $pidList = "1095, 80";
    $eventUid = '2670';

    $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Extbase\Object\ObjectManager');
    $calAPI = $objectManager->get('TYPO3\CMS\Cal\Controller\Api')->tx_cal_api_without($pidList);

    $event = $calAPI->findEvent($eventUid, 'tx_cal_phpicalendar', $pidList);

    $this->assertInstanceOf(TYPO3\CMS\Cal\Model\Eventmodel::class, $event);
}

internally it seems to use the approach of simulating a TSFE context (if no cObj is present, I didn't try the tx_cal_api_with(&$cObj, &$conf) method using an existing cObj).

Having read an old thread from 2012 ( https://forum.typo3.org/index.php/t/192263/ ) I implemented that in my Typo3 6.2 setting:

<?php
class CalEventTest extends \TYPO3\CMS\Core\Tests\BaseTestCase {

    /**
      * @test
      */
    public function anInstanceOfCalEventCanBeConstructed() {
            $rightsObj = &\TYPO3\CMS\Cal\Utility\Registry::Registry ('basic', 'rightscontroller');
            $rightsObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstanceService('cal_rights_model', 'rights');
            $rightsObj->setDefaultSaveToPage();

            $modelObj = &\TYPO3\CMS\Cal\Utility\Registry::Registry('basic','modelcontroller');
            $modelObj = new \TYPO3\CMS\Cal\Controller\ModelController();

            $viewObj = &\TYPO3\CMS\Cal\Utility\Registry::Registry('basic','viewcontroller');
            $viewObj = new \TYPO3\CMS\Cal\Controller\ViewController();

            $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Extbase\Object\ObjectManager');
            $configurationManager = $objectManager->get('TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface');

            $controller = &\TYPO3\CMS\Cal\Utility\Registry::Registry('basic','controller');
            $controller = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Cal\\Controller\\Controller');

            $cObj = &\TYPO3\CMS\Cal\Utility\Registry::Registry('basic','cobj');
            $cObj = $configurationManager->getContentObject();
            $cObj = new TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer();

            $GLOBALS['TSFE'] = new \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController($GLOBALS['TYPO3_CONF_VARS'], $pid, '0', 1, '', '', '', '');
            $GLOBALS['TSFE']->cObj = $cObj;
            $GLOBALS['TSFE']->sys_page = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Page\\PageRepository');

            $storagePageID = "1095, 80";

            $eventService = & \TYPO3\CMS\Cal\Utility\Functions::getEventService ();

            if ($eventService) {
                    $events = $eventService->findAll($storagePageID);
                    \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('2', $this->getDebugId()."-".__FUNCTION__, -1, array($events, $eventService->getServiceKey ()) );
            }
   }       
}

This works but it I find it extremely ugly. I'll try the cal api next.

(this has been an edit to my question but it's actually an answer)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM