From 06c3e0559b90547a7377459bb6bb6a74a992758b Mon Sep 17 00:00:00 2001 From: afornerot Date: Wed, 30 Sep 2020 17:02:14 +0200 Subject: [PATCH] ajout calendars dans api rest --- .../CoreBundle/Command/TestRestCommand.php | 4 +- .../CoreBundle/Controller/RestController.php | 38 +++- .../Controller/PagewidgetController.php | 164 +----------------- .../Repository/CalendareventRepository.php | 85 +++++++++ 4 files changed, 126 insertions(+), 165 deletions(-) diff --git a/src/ninegate-1.0/src/Cadoles/CoreBundle/Command/TestRestCommand.php b/src/ninegate-1.0/src/Cadoles/CoreBundle/Command/TestRestCommand.php index 0f70233c..a96a182f 100644 --- a/src/ninegate-1.0/src/Cadoles/CoreBundle/Command/TestRestCommand.php +++ b/src/ninegate-1.0/src/Cadoles/CoreBundle/Command/TestRestCommand.php @@ -58,10 +58,10 @@ class TestRestCommand extends Command // key = parametre obligatoire // key = clé d'accès du l'api // only = paramétre optionnel - // only = liste des informations désirés = user, items, bookmarks, groups, alerts + // only = liste des informations désirés = user, items, bookmarks, groups, alerts, calendars $apiurl = $url."/rest/user/".$login; $this->writeln($apiurl); - $response = \Unirest\Request::post($apiurl,$headers,["key"=>$masterapikey,"only"=>"user,items"]); + $response = \Unirest\Request::post($apiurl,$headers,["key"=>$masterapikey,"only"=>"user,calendars"]); dump($response->body); $this->writeln(''); diff --git a/src/ninegate-1.0/src/Cadoles/CoreBundle/Controller/RestController.php b/src/ninegate-1.0/src/Cadoles/CoreBundle/Controller/RestController.php index e0182c72..e0e693a3 100644 --- a/src/ninegate-1.0/src/Cadoles/CoreBundle/Controller/RestController.php +++ b/src/ninegate-1.0/src/Cadoles/CoreBundle/Controller/RestController.php @@ -34,7 +34,7 @@ class RestController extends Controller // key = parametre obligatoire // key = clé d'accès de l'api // only = paramétre optionnel - // only = liste des informations désirés = user, items, bookmarks, groups, alerts + // only = liste des informations désirés = user, items, bookmarks, groups, alerts, calendars public function userAction($login, Request $request) { $key=$request->get('key'); @@ -65,6 +65,8 @@ class RestController extends Controller if($only==""||in_array("groups",$only)) $output["groups"] = []; if($only==""||in_array("alerts",$only)) $output["alerts"] = []; if($only==""||in_array("alerts",$only)) $output["alertcategorys"] = []; + if($only==""||in_array("calendars",$only)) $output["calendars"] = []; + if($only==""||in_array("calendars",$only)) $output["calendarevents"] = []; $bookmarks=null; $items=null; @@ -94,7 +96,7 @@ class RestController extends Controller $tmp["url"] = $bookmark->getUrl(); $tmp["target"] = $bookmark->getTarget(); $tmp["order"] = 0; - $tmp["color"] = ($bookmark->getColor()?$bookmark->getColor():$this->get('session')->get('color')["main"]); + $tmp["color"] = "#".($bookmark->getColor()?$bookmark->getColor():$this->get('session')->get('color')["main"]); $tmp["icon"] = $weburl.($bookmark->getIcon()?$bookmark->getIcon()->getLabel():"uploads/icon/icon_pin.png"); array_push($output["bookmarks"],$tmp); @@ -111,7 +113,7 @@ class RestController extends Controller $tmp["url"] = $item->getUrl(); $tmp["target"] = $item->getTarget(); $tmp["order"] = $item->getRoworder(); - $tmp["color"] = ($item->getColor()?$item->getColor():$this->get('session')->get('color')["main"]); + $tmp["color"] = "#".($item->getColor()?$item->getColor():$this->get('session')->get('color')["main"]); $tmp["icon"] = $weburl.($item->getIcon()?$item->getIcon()->getLabel():"uploads/icon/icon_pin.png"); $tmp["essential"] = $item->getEssential(); $tmp["category"] = $item->getItemcategory()->getId(); @@ -126,7 +128,7 @@ class RestController extends Controller $tmp["id"] = $itemcategory->getId(); $tmp["title"] = $itemcategory->getLabel(); $tmp["order"] = $itemcategory->getRoworder(); - $tmp["color"] = ($itemcategory->getColor()?$itemcategory->getColor():$this->get('session')->get('color')["main"]); + $tmp["color"] = "#".($itemcategory->getColor()?$itemcategory->getColor():$this->get('session')->get('color')["main"]); array_push($output["itemcategorys"],$tmp); } @@ -162,13 +164,39 @@ class RestController extends Controller $tmp=[]; $tmp["id"] = $alertcategory->getId(); $tmp["title"] = $alertcategory->getLabel(); - $tmp["color"] = ($alertcategory->getColor()?$alertcategory->getColor():$this->get('session')->get('color')["main"]); + $tmp["color"] = "#".($alertcategory->getColor()?$alertcategory->getColor():$this->get('session')->get('color')["main"]); $tmp["icon"] = $weburl.($alertcategory->getIcon()?$alertcategory->getIcon()->getLabel():"uploads/icon/icon_pin.png"); array_push($output["alertcategorys"],$tmp); } } + if($only==""||in_array("calendars",$only)) { + + $events=$em->getRepository("CadolesPortalBundle:Calendarevent")->getUserCalendarevents($user,$this->get('session')->get('color')["main"],null,null,$firstcalendar); + foreach($events as $event) { + $tmp=[]; + $tmp["id"] = $event["id"]; + $tmp["title"] = $event["title"]; + $tmp["description"] = $event["description"]; + $tmp['start'] = $event["start"]; + $tmp['end'] = $event["end"]; + $tmp['allday'] = $event["allDay"]; + $tmp['calendar'] = $event["calendar"]; + + array_push($output["calendarevents"],$tmp); + + $tmp=[]; + $tmp["id"] = $event["calendar"]; + $tmp["title"] = $event["name"]; + $tmp["color"] = $event["color"]; + + if(!in_array($tmp,$output["calendars"])) + array_push($output["calendars"],$tmp); + } + } + + // Retour diff --git a/src/ninegate-1.0/src/Cadoles/PortalBundle/Controller/PagewidgetController.php b/src/ninegate-1.0/src/Cadoles/PortalBundle/Controller/PagewidgetController.php index d9945446..ef287842 100644 --- a/src/ninegate-1.0/src/Cadoles/PortalBundle/Controller/PagewidgetController.php +++ b/src/ninegate-1.0/src/Cadoles/PortalBundle/Controller/PagewidgetController.php @@ -849,6 +849,7 @@ class PagewidgetController extends Controller // Récupération des paramétres du widget $idalertcategory=null; + $alertcategoryfilter=null; foreach($entity->getParameter()["fields"] as $parameter) { switch($parameter["id"]) { case "alertcategory": @@ -863,88 +864,8 @@ class PagewidgetController extends Controller // Profilage $user=$this->getUser(); - $roles=($user?$user->getRoles():["ROLE_ANONYME"]); - $niveau01=($user?$user->getNiveau01():null); - $groups=($user?$user->getGroups():[]); + $alertsordered=$em->getRepository("CadolesPortalBundle:Alert")->getUserAlerts($user,$idalertcategory,$alertcategoryfilter); - // Initialisation du calcul des alerts - $alerts=new ArrayCollection(); - - // Récupération des alerts par rôles - foreach($roles as $role) { - $qb = $em->createQueryBuilder(); - $qb->select('alert') - ->from("CadolesPortalBundle:Alert", 'alert') - ->where($qb->expr()->like('alert.roles', $qb->expr()->literal("%$role%"))) - ->andWhere('alert.publishedat <= :today') - ->andWhere($qb->expr()->orX( - $qb->expr()->gt('alert.unpublishedat', ':today'), - $qb->expr()->isNull('alert.unpublishedat') - )) - ->setParameter('today', date('Y-m-d')); - - if($idalertcategory && $alertcategoryfilter) { - $qb->andWhere("alert.alertcategory=:alertcategory") - ->setParameter("alertcategory",$alertcategoryfilter); - } - $alertsroles=$qb->getQuery()->getResult(); - foreach($alertsroles as $alertrole) { - if(!$alerts->contains($alertrole)&&!$alertrole->getReaders()->contains($this->getUser())) $alerts->add($alertrole); - } - } - - // Récupération par Niveau01 - $qb = $em->createQueryBuilder(); - $qb->select('alert') - ->from("CadolesPortalBundle:Alert", 'alert') - ->where(":niveau01 MEMBER OF alert.niveau01s") - ->andWhere('alert.publishedat <= :today') - ->andWhere($qb->expr()->orX( - $qb->expr()->gt('alert.unpublishedat', ':today'), - $qb->expr()->isNull('alert.unpublishedat') - )) - ->setParameter("niveau01",$niveau01) - ->setParameter('today', date('Y-m-d')); - - if($idalertcategory && $alertcategoryfilter) { - $qb->andWhere("alert.alertcategory=:alertcategory") - ->setParameter("alertcategory",$alertcategoryfilter); - } - $alertsniveau01s=$qb->getQuery()->getResult(); - foreach($alertsniveau01s as $alertniveau01) { - if(!$alerts->contains($alertniveau01)&&!$alertniveau01->getReaders()->contains($this->getUser())) $alerts->add($alertniveau01); - } - - // Récupération des alerts par group - foreach($groups as $group) { - $qb = $em->createQueryBuilder(); - $qb->select('alert') - ->from("CadolesPortalBundle:Alert", 'alert') - ->where(":group MEMBER OF alert.groups") - ->andWhere('alert.publishedat <= :today') - ->andWhere($qb->expr()->orX( - $qb->expr()->gt('alert.unpublishedat', ':today'), - $qb->expr()->isNull('alert.unpublishedat') - )) - ->setParameter("group",$group->getGroup()) - ->setParameter('today', date('Y-m-d')); - - if($idalertcategory && $alertcategoryfilter) { - $qb->andWhere("alert.alertcategory=:alertcategory") - ->setParameter("alertcategory",$alertcategoryfilter); - } - $alertsgroups=$qb->getQuery()->getResult(); - foreach($alertsgroups as $alertgroup) { - if(!$alerts->contains($alertgroup)&&!$alertgroup->getReaders()->contains($this->getUser())) $alerts->add($alertgroup); - } - } - - // Trie des alerts - $alertsordered = $alerts->getIterator(); - $alertsordered->uasort(function ($first, $second) { - return (int) $first->getRowOrder() > (int) $second->getRowOrder() ? 1 : -1; - }); - // Render return $this->render($this->labelentity.':viewalert.html.twig', [ 'entity' => $entity, @@ -1720,84 +1641,11 @@ class PagewidgetController extends Controller // Préference utilisateur $this->getPreference($entity); - - // On récupère soit les calendriers du group en cours soit l'ensemble des calendriers de l'utilisateur - if($usage=="group") { - $calendars=$em->getRepository("CadolesPortalBundle:Calendar")->getCalendarsGroup($this->getUser(),$group); - if($calendars) $firstcalendar=$calendars[0]->getId(); - } - else { - $this->getDoctrine()->getRepository("CadolesPortalBundle:Calendar")->getCalendarsUser($this->getUser(),$calendarssuser,$calendarsadmin,$calendarsshared); - $calendars=array_merge($calendarssuser,$calendarsadmin->toArray(),$calendarsshared); - $firstcalendar="all"; - } - - // Début & fin - $start=new \DateTime(); - $start->setTime(0,0); - - $end=new \DateTime(); - $end->setTime(0,0); - $end->add(new \DateInterval('P3M')); - $end->modify('last day of this month'); - - $events=[]; - $eventids=[]; - - foreach($calendars as $calendar) { - $calendarevents = $this->getDoctrine()->getRepository("CadolesPortalBundle:Calendarevent")->getCalendareventByDate($calendar,$start,$end); - - foreach($calendarevents as $calendarevent) { - $tmp=[]; - $tmp['id'] = strval($calendarevent->getId()); - $tmp['title'] = $calendarevent->getName(); - $tmp['description'] = $calendarevent->getDescription(); - $tmp['start'] = $calendarevent->getStart(); - $tmp['end'] = $calendarevent->getEnd(); - $tmp['color'] = "#".(is_null($calendar->getColor())?$this->get('session')->get('color')["main"]:$calendar->getColor()); - $tmp['allDay'] = $calendarevent->getAllday(); - $tmp['calendar'] = $calendar->getId(); - $tmp['editable'] = $calendar->getCanupdate(); - - // Si évènement sur la journée on enlève une 1 seconde à la date de fin - if($tmp['allDay']) - $tmp['end']->sub(new \DateInterval('PT1S')); - - array_push($events,$tmp); - array_push($eventids,$tmp["id"]); - } - } - - // On récupére les évenement partagé de l'utilisateur - if($this->getUser()) { - $calendarevents = $this->getDoctrine()->getRepository("CadolesPortalBundle:Calendarevent")->getCalendareventGroupsShareByDate($this->getUser(),$start,$end); - foreach($calendarevents as $calendarevent) { - if($usage=="group") { - if(!$calendarevent->getGroups()->contains($groupentity)) continue; - } - - $tmp=[]; - $tmp['id'] = strval($calendarevent->getId()); - $tmp['title'] = $calendarevent->getName(); - $tmp['description'] = $calendarevent->getDescription(); - $tmp['start'] = $calendarevent->getStart(); - $tmp['end'] = $calendarevent->getEnd(); - $tmp['color'] = "#".(is_null($calendar->getColor())?$this->get('session')->get('color')["main"]:$calendar->getColor()); - $tmp['allDay'] = $calendarevent->getAllday(); - $tmp['calendar'] = $calendar->getId(); - $tmp['editable'] = $calendar->getCanupdate(); - - // Si évènement sur la journée on enlève une 1 seconde à la date de fin - if($tmp['allDay']) - $tmp['end']->sub(new \DateInterval('PT1S')); - - if(!in_array($tmp['id'],$eventids)) { - array_push($events,$tmp); - array_push($eventids,$tmp["id"]); - } - } - } + // Récupérer les events de l'utilisateur + $user=$this->getUser(); + $events=$em->getRepository("CadolesPortalBundle:Calendarevent")->getUserCalendarevents($user,$this->get('session')->get('color')["main"],$usage,$group,$firstcalendar); + // Render return $this->render($this->labelentity.':viewcalendar.html.twig', [ 'entity' => $entity, diff --git a/src/ninegate-1.0/src/Cadoles/PortalBundle/Repository/CalendareventRepository.php b/src/ninegate-1.0/src/Cadoles/PortalBundle/Repository/CalendareventRepository.php index c9d66baa..a1849471 100644 --- a/src/ninegate-1.0/src/Cadoles/PortalBundle/Repository/CalendareventRepository.php +++ b/src/ninegate-1.0/src/Cadoles/PortalBundle/Repository/CalendareventRepository.php @@ -93,4 +93,89 @@ class CalendareventRepository extends EntityRepository return $calendareventshared; } + + public function getUserCalendarevents($user,$sessioncolor,$usage,$group,&$firstcalendar) { + $em=$this->getEntityManager(); + + // On récupère soit les calendriers du group en cours soit l'ensemble des calendriers de l'utilisateur + if($usage=="group") { + $calendars=$em->getRepository("CadolesPortalBundle:Calendar")->getCalendarsGroup($user,$group); + if($calendars) $firstcalendar=$calendars[0]->getId(); + } + else { + $em->getRepository("CadolesPortalBundle:Calendar")->getCalendarsUser($user,$calendarssuser,$calendarsadmin,$calendarsshared); + $calendars=array_merge($calendarssuser,$calendarsadmin->toArray(),$calendarsshared); + $firstcalendar="all"; + } + + // Début & fin + $start=new \DateTime(); + $start->setTime(0,0); + + $end=new \DateTime(); + $end->setTime(0,0); + $end->add(new \DateInterval('P3M')); + $end->modify('last day of this month'); + + $events=[]; + $eventids=[]; + + foreach($calendars as $calendar) { + $calendarevents = $em->getRepository("CadolesPortalBundle:Calendarevent")->getCalendareventByDate($calendar,$start,$end); + + foreach($calendarevents as $calendarevent) { + $tmp=[]; + $tmp['id'] = strval($calendarevent->getId()); + $tmp['title'] = $calendarevent->getName(); + $tmp['description'] = $calendarevent->getDescription(); + $tmp['start'] = $calendarevent->getStart(); + $tmp['end'] = $calendarevent->getEnd(); + $tmp['color'] = "#".(is_null($calendar->getColor())?$sessioncolor:$calendar->getColor()); + $tmp['allDay'] = $calendarevent->getAllday(); + $tmp['calendar'] = $calendar->getId(); + $tmp['name'] = $calendar->getName(); + $tmp['editable'] = $calendar->getCanupdate(); + + // Si évènement sur la journée on enlève une 1 seconde à la date de fin + if($tmp['allDay']) + $tmp['end']->sub(new \DateInterval('PT1S')); + + array_push($events,$tmp); + array_push($eventids,$tmp["id"]); + + } + } + + // On récupére les évenement partagé de l'utilisateur + if($user) { + $calendarevents = $em->getRepository("CadolesPortalBundle:Calendarevent")->getCalendareventGroupsShareByDate($user,$start,$end); + foreach($calendarevents as $calendarevent) { + if($usage=="group") { + if(!$calendarevent->getGroups()->contains($groupentity)) continue; + } + + $tmp=[]; + $tmp['id'] = strval($calendarevent->getId()); + $tmp['title'] = $calendarevent->getName(); + $tmp['description'] = $calendarevent->getDescription(); + $tmp['start'] = $calendarevent->getStart(); + $tmp['end'] = $calendarevent->getEnd(); + $tmp['color'] = "#".(is_null($calendar->getColor())?$sessioncolor:$calendar->getColor()); + $tmp['allDay'] = $calendarevent->getAllday(); + $tmp['calendar'] = $calendar->getId(); + $tmp['name'] = $calendar->getName(); + $tmp['editable'] = $calendar->getCanupdate(); + + // Si évènement sur la journée on enlève une 1 seconde à la date de fin + if($tmp['allDay']) + $tmp['end']->sub(new \DateInterval('PT1S')); + + if(!in_array($tmp['id'],$eventids)) { + array_push($events,$tmp); + array_push($eventids,$tmp["id"]); + } + } + } + return $events; + } }