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 * @param string $project The google project
65 * @param string $client The google client
66 * @param string $secret The google secret
68 public function __construct(ManagerRegistry
$doctrine, RouterInterface
$router, SluggerUtil
$slugger, TranslatorInterface
$translator, string $locale, string $project, string $client, string $secret) {
69 //Call parent constructor
70 parent
::__construct($doctrine, $router, $slugger, $translator, $locale);
73 $this->client
= new Client(
75 'application_name' => $project,
76 'client_id' => $client,
77 'client_secret' => $secret,
78 'redirect_uri' => $this->router
->generate('rapsys_air_google_callback', [], UrlGeneratorInterface
::ABSOLUTE_URL
),
79 'scopes' => $this->scopes
,
80 'access_type' => 'offline',
81 #'login_hint' => $user->getMail(),
82 //XXX: see https://stackoverflow.com/questions/10827920/not-receiving-google-oauth-refresh-token
83 #'approval_prompt' => 'force'
88 //Set Markdown instance
89 $this->markdown
= new DefaultMarkdown
;
93 * Configure attribute command
95 protected function configure() {
99 ->setName('rapsysair:calendar2')
100 //Set description shown with bin/console list
101 ->setDescription('Synchronize sessions in users\' calendar')
102 //Set description shown with bin/console --help airlibre:attribute
103 ->setHelp('This command synchronize sessions in users\' google calendar');
107 * Process the attribution
109 protected function execute(InputInterface
$input, OutputInterface
$output): int {
110 //Iterate on google tokens
111 foreach($tokens = $this->doctrine
->getRepository(GoogleToken
::class)->findAllIndexed() as $tid => $token) {
112 //Iterate on google calendars
113 foreach($calendars = $token['calendars'] as $cid => $calendar) {
115 $this->period
= new \
DatePeriod(
116 //Start from last week
117 new \
DateTime('-1 week'),
118 //Iterate on each day
119 new \
DateInterval('P1D'),
120 //End with next 2 week
121 new \
DateTime('+2 week')
124 #$calendar['synchronized']
127 //TODO: see if we may be smarter here ?
129 //TODO: load all calendar events here ?
131 //Iterate on sessions to update
132 foreach($sessions = $this->doctrine
->getRepository(Session
::class)->findAllByUserIdSynchronized($token['uid'], $calendar['synchronized']) as $session) {
133 //TODO: insert/update/delete events here ?
136 //TODO: delete remaining events here ?
140 //TODO: get user filter ? (users_subscriptions+users_dances)
142 //TODO: XXX: or fetch directly the events updated since synchronized + matching rubscriptions and/or dances
151 GROUP_CONCAT(us.user_id) AS users
155 GROUP_CONCAT(ud.dance_id) AS dances
164 GROUP_CONCAT(c.id) AS cids,
165 GROUP_CONCAT(c.mail) AS cmails,
166 GROUP_CONCAT(c.summary) AS csummaries,
167 GROUP_CONCAT(c.synchronized) AS csynchronizeds
168 FROM google_tokens AS t
169 JOIN google_calendars AS c ON (c.google_token_id = t.id)
174 LEFT JOIN users_dances AS ud ON (ud.user_id = a.user_id)
179 LEFT JOIN users_subscriptions AS us ON (us.subscriber_id = b.user_id)
183 #$sessions = $this->doctrine->getRepository(Session::class)->findAllByDanceUserModified($filter['dance'], $filter['user'], $calendar['synchronized']);
184 //Iterate on google tokens
185 foreach($tokens as $token) {
186 //TODO: clear google client cache
187 //TODO: set google token
188 //Iterate on google calendars
189 foreach($calendars as $calendar) {
190 //Fetch sessions to sync
191 $sessions = $this->doctrine
->getRepository(Session
::class)->findAllByDanceUserModified($filter['dance'], $filter['user'], $calendar['synchronized']);
196 return self
::SUCCESS
;