Guest User

class.ext_update.php

a guest
Dec 14th, 2015
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.66 KB | None | 0 0
  1. <?php
  2. use TYPO3\CMS\Core\Utility\GeneralUtility;
  3.  
  4. class ext_update
  5. {
  6.     /**
  7.      * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface
  8.      */
  9.     protected $objectManager;
  10.  
  11.     /**
  12.      * frontendUserGroupRepository
  13.      *
  14.      * @var \TYPO3\CMS\Extbase\Domain\Repository\FrontendUserGroupRepository
  15.      * @ inject
  16.      */
  17.     protected $frontendUserGroupRepository;
  18.  
  19.     /**
  20.      * Checks whether the "UPDATE!" menu item should be
  21.      * shown.
  22.      *
  23.      * @return boolean
  24.      */
  25.     function access() {
  26.         # @var \TYPO3\CMS\Extbase\Validation\ValidatorResolver $validatorResolver
  27.         $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('Tx_Extbase_Object_ObjectManager');
  28.         $this->frontendUserGroupRepository = $this->objectManager->get('\TYPO3\CMS\Extbase\Domain\Repository\FrontendUserGroupRepository');
  29.  
  30.         $querySettings = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('\TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings');
  31.         $querySettings->setRespectStoragePage(false);
  32. #       $querySettings->setStoragePageIds(array(2,28));
  33.  
  34.         $this->frontendUserGroupRepository->setDefaultQuerySettings($querySettings);
  35.  
  36.         if (
  37.             (0 == $this->frontendUserGroupRepository->countByTitle('GROUP1')) or
  38.             (0 == $this->frontendUserGroupRepository->countByTitle('GROUP2'))
  39.         )
  40.             return true; // Update necessary
  41.         else
  42.             return false; // Update not necessary
  43.     }
  44.  
  45.     /**
  46.      * Main method that is called whenever UPDATE! was clicked.
  47.      *
  48.      * @return string HTML to display
  49.      */
  50.     function main() {
  51.         $operation = GeneralUtility::_GP('operation');
  52.         if ($operation) {
  53.             // TODO: Use the value from the input field as PID
  54.             $this->getFrontendUserGroupByName('GROUP1', 2);
  55.             $this->getFrontendUserGroupByName('GROUP2', 2);
  56.             $out[] = 'Update performed!';
  57.         } else {
  58.             $out[] = '<form action="' . GeneralUtility::linkThisScript() . '" method="post" class="container-fluid">';
  59.             $out[] = '<label>Page UID for new Groups to be created on</label>';
  60.             $out[] = '<input name="pid" type="number" min="1" value=""/><br/>';
  61.             $out[] = '<button name="operation" value="UPDATE">UPDATE</button>';
  62.             $out[] = '</form>';
  63.         }
  64.  
  65.         return implode(LF, $out);
  66.     }
  67.  
  68.     // Helper function
  69.     function getFrontendUserGroupByName($frontendUserGroupTitle, $pid) {
  70.         /** @noinspection PhpUndefinedMethodInspection */
  71.         if (0 == $this->frontendUserGroupRepository->countByTitle('GENSER')) {
  72.             $fe_group = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
  73.                 '\\TYPO3\\CMS\\Extbase\\Domain\\Model\\FrontendUserGroup', $frontendUserGroupTitle
  74.             );
  75.             $fe_group->setPid($pid); // pageID where the group should be created
  76.             $this->frontendUserGroupRepository->add($fe_group);
  77.         }
  78.         return;
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment