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
;
39 * Synchronize sessions in users' calendar
41 class Calendar2Command
extends Command
{
45 * Shown with bin/console list
47 protected string $description = 'Synchronize sessions in users\' calendar';
52 * Shown with bin/console --help rapsysair:calendar2
54 protected string $help = 'This command synchronize sessions in users\' google calendar';
57 * Set markdown instance
59 #private DefaultMarkdown $markdown;
62 * Set date period instance
64 #private \DatePeriod $period;
69 public function __construct(protected ManagerRegistry
$doctrine, protected string $locale, protected RouterInterface
$router, protected SluggerUtil
$slugger, protected TranslatorInterface
$translator, protected Client
$google, protected DefaultMarkdown
$markdown) {
70 //Call parent constructor
71 parent
::__construct($this->doctrine
, $this->locale
, $this->router
, $this->slugger
, $this->translator
);
73 //Replace google client redirect uri
74 $this->google
->setRedirectUri($this->router
->generate($this->google
->getRedirectUri(), [], UrlGeneratorInterface
::ABSOLUTE_URL
));
78 $this->client = new Client(
80 'application_name' => $_ENV['RAPSYSAIR_GOOGLE_PROJECT'],
81 'client_id' => $_ENV['GOOGLE_CLIENT_ID'],
82 'client_secret' => $_ENV['GOOGLE_CLIENT_SECRET'],
83 'redirect_uri' => $this->router->generate('rapsysair_google_callback', [], UrlGeneratorInterface::ABSOLUTE_URL),
84 'scopes' => $this->scopes,
85 'access_type' => 'offline',
86 #'login_hint' => $user->getMail(),
87 //XXX: see https://stackoverflow.com/questions/10827920/not-receiving-google-oauth-refresh-token
88 #'approval_prompt' => 'force'
93 //Set Markdown instance
94 $this->markdown = new DefaultMarkdown;*/
98 * Configure attribute command
100 /*protected function configure() {
101 //Configure the class
104 ->setName('rapsysair:calendar2')
105 //Set description shown with bin/console list
106 ->setDescription('Synchronize sessions in users\' calendar')
107 //Set description shown with bin/console --help airlibre:attribute
108 ->setHelp('This command synchronize sessions in users\' google calendar');
112 * Process the attribution
114 protected function execute(InputInterface
$input, OutputInterface
$output): int {
116 $period = new \
DatePeriod(
117 //Start from last week
118 new \
DateTime('-1 week'),
119 //Iterate on each day
120 new \
DateInterval('P1D'),
121 //End with next 2 week
122 new \
DateTime('+2 week')
125 //Iterate on google tokens
126 foreach($tokens = $this->doctrine
->getRepository(GoogleToken
::class)->findAllIndexed() as $tid => $token) {
127 //Iterate on google calendars
128 foreach($calendars = $token['calendars'] as $cid => $calendar) {
129 #$calendar['synchronized']
132 //TODO: see if we may be smarter here ?
134 //TODO: load all calendar events here ?
136 //Iterate on sessions to update
137 foreach($sessions = $this->doctrine
->getRepository(Session
::class)->findAllByUserIdSynchronized($token['uid'], $calendar['synchronized']) as $session) {
138 //TODO: insert/update/delete events here ?
141 //TODO: delete remaining events here ?
145 //TODO: get user filter ? (users_subscriptions+users_dances)
147 //TODO: XXX: or fetch directly the events updated since synchronized + matching rubscriptions and/or dances
156 GROUP_CONCAT(us.user_id) AS users
160 GROUP_CONCAT(ud.dance_id) AS dances
169 GROUP_CONCAT(c.id) AS cids,
170 GROUP_CONCAT(c.mail) AS cmails,
171 GROUP_CONCAT(c.summary) AS csummaries,
172 GROUP_CONCAT(c.synchronized) AS csynchronizeds
173 FROM google_tokens AS t
174 JOIN google_calendars AS c ON (c.google_token_id = t.id)
179 LEFT JOIN users_dances AS ud ON (ud.user_id = a.user_id)
184 LEFT JOIN users_subscriptions AS us ON (us.subscriber_id = b.user_id)
188 #$sessions = $this->doctrine->getRepository(Session::class)->findAllByDanceUserModified($filter['dance'], $filter['user'], $calendar['synchronized']);
189 //Iterate on google tokens
190 foreach($tokens as $token) {
191 //TODO: clear google client cache
192 //TODO: set google token
193 //Iterate on google calendars
194 foreach($calendars as $calendar) {
195 //Fetch sessions to sync
196 $sessions = $this->doctrine
->getRepository(Session
::class)->findAllByDanceUserModified($filter['dance'], $filter['user'], $calendar['synchronized']);
201 return self
::SUCCESS
;