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\Entity\Session
;
30 use Rapsys\AirBundle\Entity\GoogleCalendar
;
31 use Rapsys\AirBundle\Entity\GoogleToken
;
33 use Rapsys\PackBundle\Util\SluggerUtil
;
35 class Calendar2Command
extends Command
{
37 * Set google client scopes
39 private array $scopes = [
40 Calendar
::CALENDAR_EVENTS
,
42 Oauth2
::USERINFO_EMAIL
46 * Set google client instance
48 private Client
$client;
51 * Set markdown instance
53 private DefaultMarkdown
$markdown;
56 * Set date period instance
58 private \DatePeriod
$period;
63 * @param string $project The google project
64 * @param string $client The google client
65 * @param string $secret The google secret
67 public function __construct(ManagerRegistry
$doctrine, RouterInterface
$router, SluggerUtil
$slugger, TranslatorInterface
$translator, string $locale, string $project, string $client, string $secret) {
68 //Call parent constructor
69 parent
::__construct($doctrine, $router, $slugger, $translator, $locale);
72 $this->client
= new Client(
74 'application_name' => $project,
75 'client_id' => $client,
76 'client_secret' => $secret,
77 'redirect_uri' => $this->router
->generate('rapsys_air_google_callback', [], UrlGeneratorInterface
::ABSOLUTE_URL
),
78 'scopes' => $this->scopes
,
79 'access_type' => 'offline',
80 #'login_hint' => $user->getMail(),
81 //XXX: see https://stackoverflow.com/questions/10827920/not-receiving-google-oauth-refresh-token
82 #'approval_prompt' => 'force'
87 //Set Markdown instance
88 $this->markdown
= new DefaultMarkdown
;
92 * Configure attribute command
94 protected function configure() {
98 ->setName('rapsysair:calendar2')
99 //Set description shown with bin/console list
100 ->setDescription('Synchronize sessions in users\' calendar')
101 //Set description shown with bin/console --help airlibre:attribute
102 ->setHelp('This command synchronize sessions in users\' google calendar');
106 * Process the attribution
108 protected function execute(InputInterface
$input, OutputInterface
$output): int {
109 //Iterate on google tokens
110 foreach($tokens = $this->doctrine
->getRepository(GoogleToken
::class)->findAllIndexed() as $tid => $token) {
111 //Iterate on google calendars
112 foreach($calendars = $token['calendars'] as $cid => $calendar) {
114 $this->period
= new \
DatePeriod(
115 //Start from last week
116 new \
DateTime('-1 week'),
117 //Iterate on each day
118 new \
DateInterval('P1D'),
119 //End with next 2 week
120 new \
DateTime('+2 week')
123 #$calendar['synchronized']
126 //TODO: see if we may be smarter here ?
128 //TODO: load all calendar events here ?
130 //Iterate on sessions to update
131 foreach($sessions = $this->doctrine
->getRepository(Session
::class)->findAllByUserIdSynchronized($token['uid'], $calendar['synchronized']) as $session) {
132 //TODO: insert/update/delete events here ?
135 //TODO: delete remaining events here ?
139 //TODO: get user filter ? (users_subscriptions+users_dances)
141 //TODO: XXX: or fetch directly the events updated since synchronized + matching rubscriptions and/or dances
150 GROUP_CONCAT(us.user_id) AS users
154 GROUP_CONCAT(ud.dance_id) AS dances
163 GROUP_CONCAT(c.id) AS cids,
164 GROUP_CONCAT(c.mail) AS cmails,
165 GROUP_CONCAT(c.summary) AS csummaries,
166 GROUP_CONCAT(c.synchronized) AS csynchronizeds
167 FROM google_tokens AS t
168 JOIN google_calendars AS c ON (c.google_token_id = t.id)
173 LEFT JOIN users_dances AS ud ON (ud.user_id = a.user_id)
178 LEFT JOIN users_subscriptions AS us ON (us.subscriber_id = b.user_id)
182 #$sessions = $this->doctrine->getRepository(Session::class)->findAllByDanceUserModified($filter['dance'], $filter['user'], $calendar['synchronized']);
183 //Iterate on google tokens
184 foreach($tokens as $token) {
185 //TODO: clear google client cache
186 //TODO: set google token
187 //Iterate on google calendars
188 foreach($calendars as $calendar) {
189 //Fetch sessions to sync
190 $sessions = $this->doctrine
->getRepository(Session
::class)->findAllByDanceUserModified($filter['dance'], $filter['user'], $calendar['synchronized']);
195 return self
::SUCCESS
;