1 <?php 
declare(strict_types
=1); 
   4  * This file is part of the Rapsys AirBundle package. 
   6  * (c) Raphaël Gertz <symfony@rapsys.eu> 
   8  * For the full copyright and license information, please view the LICENSE 
   9  * file that was distributed with this source code. 
  12 namespace Rapsys\AirBundle\Command
; 
  14 use Doctrine\Persistence\ManagerRegistry
; 
  17 use Google\Service\Calendar
; 
  18 use Google\Service\Oauth2
; 
  20 use Symfony\Component\Cache\Adapter\FilesystemAdapter
; 
  21 use Symfony\Component\Console\Input\InputInterface
; 
  22 use Symfony\Component\Console\Output\OutputInterface
; 
  23 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
; 
  24 use Symfony\Component\Routing\RouterInterface
; 
  25 use Symfony\Contracts\Translation\TranslatorInterface
; 
  27 use Twig\Extra\Markdown\DefaultMarkdown
; 
  29 use Rapsys\AirBundle\Command
; 
  30 use Rapsys\AirBundle\Entity\GoogleCalendar
; 
  31 use Rapsys\AirBundle\Entity\GoogleToken
; 
  32 use Rapsys\AirBundle\Entity\Session
; 
  34 use Rapsys\PackBundle\Util\SluggerUtil
; 
  36 class Calendar2Command 
extends Command 
{ 
  38          * Set google client scopes 
  40         private array $scopes = [ 
  41                 Calendar
::CALENDAR_EVENTS
, 
  43                 Oauth2
::USERINFO_EMAIL
 
  47          * Set google client instance 
  49         private Client 
$client; 
  52          * Set markdown instance 
  54         private DefaultMarkdown 
$markdown; 
  57          * Set date period instance 
  59         private \DatePeriod 
$period; 
  64         public function __construct(ManagerRegistry 
$doctrine, string $locale, RouterInterface 
$router, SluggerUtil 
$slugger, TranslatorInterface 
$translator) { 
  65                 //Call parent constructor 
  66                 parent
::__construct($doctrine, $locale, $router, $slugger, $translator); 
  69                 $this->client 
= new Client( 
  71                                 'application_name' => $_ENV['RAPSYSAIR_GOOGLE_PROJECT'], 
  72                                 'client_id' => $_ENV['RAPSYSAIR_GOOGLE_CLIENT'], 
  73                                 'client_secret' => $_ENV['RAPSYSAIR_GOOGLE_SECRET'], 
  74                                 'redirect_uri' => $this->router
->generate('rapsysair_google_callback', [], UrlGeneratorInterface
::ABSOLUTE_URL
), 
  75                                 'scopes' => $this->scopes
, 
  76                                 'access_type' => 'offline', 
  77                                 #'login_hint' => $user->getMail(), 
  78                                 //XXX: see https://stackoverflow.com/questions/10827920/not-receiving-google-oauth-refresh-token 
  79                                 #'approval_prompt' => 'force' 
  84                 //Set Markdown instance 
  85                 $this->markdown 
= new DefaultMarkdown
; 
  89          * Configure attribute command 
  91         protected function configure() { 
  95                         ->setName('rapsysair:calendar2') 
  96                         //Set description shown with bin/console list 
  97                         ->setDescription('Synchronize sessions in users\' calendar') 
  98                         //Set description shown with bin/console --help airlibre:attribute 
  99                         ->setHelp('This command synchronize sessions in users\' google calendar'); 
 103          * Process the attribution 
 105         protected function execute(InputInterface 
$input, OutputInterface 
$output): int { 
 106                 //Iterate on google tokens 
 107                 foreach($tokens = $this->doctrine
->getRepository(GoogleToken
::class)->findAllIndexed() as $tid => $token) { 
 108                         //Iterate on google calendars 
 109                         foreach($calendars = $token['calendars'] as $cid => $calendar) { 
 111                                 $this->period 
= new \
DatePeriod( 
 112                                         //Start from last week 
 113                                         new \
DateTime('-1 week'), 
 114                                         //Iterate on each day 
 115                                         new \
DateInterval('P1D'), 
 116                                         //End with next 2 week 
 117                                         new \
DateTime('+2 week') 
 120                                 #$calendar['synchronized'] 
 123                                 //TODO: see if we may be smarter here ? 
 125                                 //TODO: load all calendar events here ? 
 127                                 //Iterate on sessions to update 
 128                                 foreach($sessions = $this->doctrine
->getRepository(Session
::class)->findAllByUserIdSynchronized($token['uid'], $calendar['synchronized']) as $session) { 
 129                                         //TODO: insert/update/delete events here ? 
 132                                 //TODO: delete remaining events here ? 
 136                 //TODO: get user filter ? (users_subscriptions+users_dances) 
 138                 //TODO: XXX: or fetch directly the events updated since synchronized + matching rubscriptions and/or dances 
 147         GROUP_CONCAT(us.user_id) AS users 
 151                 GROUP_CONCAT(ud.dance_id) AS dances 
 160                         GROUP_CONCAT(c.id) AS cids, 
 161                         GROUP_CONCAT(c.mail) AS cmails, 
 162                         GROUP_CONCAT(c.summary) AS csummaries, 
 163                         GROUP_CONCAT(c.synchronized) AS csynchronizeds 
 164                 FROM google_tokens AS t 
 165                 JOIN google_calendars AS c ON (c.google_token_id = t.id) 
 170         LEFT JOIN users_dances AS ud ON (ud.user_id = a.user_id) 
 175 LEFT JOIN users_subscriptions AS us ON (us.subscriber_id = b.user_id) 
 179                 #$sessions = $this->doctrine->getRepository(Session::class)->findAllByDanceUserModified($filter['dance'], $filter['user'], $calendar['synchronized']); 
 180                 //Iterate on google tokens 
 181                 foreach($tokens as $token) { 
 182                         //TODO: clear google client cache 
 183                         //TODO: set google token 
 184                         //Iterate on google calendars 
 185                         foreach($calendars as $calendar) { 
 186                                 //Fetch sessions to sync 
 187                                 $sessions = $this->doctrine
->getRepository(Session
::class)->findAllByDanceUserModified($filter['dance'], $filter['user'], $calendar['synchronized']); 
 192                 return self
::SUCCESS
;