ICS
This commit is contained in:
@@ -40,6 +40,7 @@ app_logoutcas:
|
||||
path: /logoutcas
|
||||
defaults: { _controller: App\Controller\SecurityController:logoutcas }
|
||||
|
||||
|
||||
#== Crop =========================================================================================================
|
||||
app_crop01:
|
||||
path: /user/crop01
|
||||
@@ -350,4 +351,9 @@ app_customer_report:
|
||||
|
||||
app_customer_planning:
|
||||
path: /customer/planning/{key}
|
||||
defaults: { _controller: App\Controller\ReportController:planning, access: 'customer' }
|
||||
defaults: { _controller: App\Controller\ReportController:planning, access: 'customer' }
|
||||
|
||||
#== API ===========================================================================================================
|
||||
app_api:
|
||||
path: /api/{key}
|
||||
defaults: { _controller: App\Controller\ApiController:api }
|
@@ -41,6 +41,10 @@ services:
|
||||
public: true
|
||||
class: App\Service\passwordEncoder
|
||||
|
||||
app.ics.service:
|
||||
public: true
|
||||
class: App\Service\icsService
|
||||
|
||||
app.upload.listener:
|
||||
public: true
|
||||
class: App\Service\uploadListener
|
||||
|
53
src/schedule-2.0/src/Controller/ApiController.php
Executable file
53
src/schedule-2.0/src/Controller/ApiController.php
Executable file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Form\FormError;
|
||||
use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
|
||||
use App\Service\icsService;
|
||||
|
||||
class ApiController extends AbstractController
|
||||
{
|
||||
public function api($key,Request $request)
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$user=$em->getRepository("App:User")->findBy(["apikey"=>$key]);
|
||||
if(!$user) {
|
||||
return new Response("Accès refusé", 403);
|
||||
}
|
||||
|
||||
$ics = new icsService();
|
||||
$ics->debug(false);
|
||||
|
||||
|
||||
$content=$ics->writeheader();
|
||||
|
||||
$events=$em->getRepository("App:Event")->findBy(["user"=>$user]);
|
||||
foreach($events as $event) {
|
||||
$task=$event->getTask();
|
||||
$project=$task->getProject();
|
||||
$customer=$project->getCustomer();
|
||||
|
||||
$ics->set(
|
||||
[
|
||||
'allday' => $event->getAllday(),
|
||||
'description' => $event->getDescription(),
|
||||
'dtstart' => $event->getStart()->format("Y-m-d H:i:s"),
|
||||
'dtend' => $event->getEnd()->format("Y-m-d H:i:s"),
|
||||
'summary' => $customer->getName()."-".$project->getName()."-".$task->getName(),
|
||||
'uid' => "schedule".$event->getId()
|
||||
]
|
||||
);
|
||||
|
||||
$content.=$ics->writeevent();
|
||||
}
|
||||
|
||||
$content.=$ics->writefooter();
|
||||
|
||||
return new Response($content);
|
||||
}
|
||||
}
|
@@ -116,6 +116,13 @@ class UserType extends AbstractType
|
||||
]
|
||||
);
|
||||
|
||||
$builder->add('apikey',
|
||||
TextType::class, [
|
||||
"label" =>"Clé Accès",
|
||||
"required" => false
|
||||
]
|
||||
);
|
||||
|
||||
$builder->add('avatar',HiddenType::class, array("empty_data" => "noavatar.png"));
|
||||
}
|
||||
|
||||
|
133
src/schedule-2.0/src/Service/icsService.php
Normal file
133
src/schedule-2.0/src/Service/icsService.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
namespace App\Service;
|
||||
|
||||
class icsService
|
||||
{
|
||||
const DT_FORMAT = 'Ymd\THis';
|
||||
const DT_FORMATDAY = 'Ymd';
|
||||
|
||||
protected $properties = array();
|
||||
private $fgdebug;
|
||||
private $available_properties = array(
|
||||
'description',
|
||||
'dtend',
|
||||
'dtstart',
|
||||
'location',
|
||||
'summary',
|
||||
'url',
|
||||
'allday',
|
||||
'uid'
|
||||
);
|
||||
|
||||
public function __construct() {
|
||||
}
|
||||
|
||||
public function debug($fgdebug = false) {
|
||||
$this->fgdebug=$fgdebug;
|
||||
}
|
||||
|
||||
public function set($key, $val = false) {
|
||||
if (is_array($key)) {
|
||||
foreach ($key as $k => $v) {
|
||||
if(is_null($v)||$v=="") unset( $this->properties[$k]);
|
||||
else $this->set($k, $v);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (in_array($key, $this->available_properties)) {
|
||||
if($key=="allday")
|
||||
$this->properties[$key] = $this->sanitize_val($val, $key, false);
|
||||
else
|
||||
$this->properties[$key] = $this->sanitize_val($val, $key,$this->properties["allday"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function writeheader() {
|
||||
// Build ICS properties - add header
|
||||
$header = array(
|
||||
'BEGIN:VCALENDAR',
|
||||
'VERSION:2.0',
|
||||
'PRODID:-//hacksw/handcal//NONSGML v1.0//EN',
|
||||
'CALSCALE:GREGORIAN',
|
||||
'X-WR-TIMEZONE:Europe/Paris'
|
||||
);
|
||||
|
||||
return $this->exportformat($header);
|
||||
}
|
||||
|
||||
public function writefooter() {
|
||||
// Build ICS properties - add footer
|
||||
$footer[] = 'END:VCALENDAR';
|
||||
|
||||
return $this->exportformat($footer);
|
||||
}
|
||||
|
||||
public function writeevent() {
|
||||
$event[]='BEGIN:VEVENT';
|
||||
|
||||
// Build ICS properties - add header
|
||||
$props = array();
|
||||
foreach($this->properties as $k => $v) {
|
||||
if($k === 'url')
|
||||
$props[strtoupper($k . ';VALUE=URI')] = $v;
|
||||
elseif(($k=='dtstart'||$k=='dtend')&&$this->properties['allday'])
|
||||
$props[strtoupper($k . ';VALUE=DATE')] = $v;
|
||||
else
|
||||
$props[strtoupper($k)] = $v;
|
||||
}
|
||||
|
||||
// Set some default values
|
||||
$props['DTSTAMP'] = $this->format_timestamp('now');
|
||||
if(!isset($props['UID'])) $props['UID'] = uniqid();
|
||||
|
||||
// Append properties
|
||||
foreach ($props as $k => $v) {
|
||||
$event[] = "$k:$v";
|
||||
}
|
||||
|
||||
// Build ICS properties - add footer
|
||||
$event[] = 'END:VEVENT';
|
||||
|
||||
return $this->exportformat($event);
|
||||
}
|
||||
|
||||
private function sanitize_val($val, $key = false, $allday) {
|
||||
switch($key) {
|
||||
case 'dtstamp':
|
||||
$val = $this->format_timestamp($val);
|
||||
break;
|
||||
|
||||
case 'dtend':
|
||||
case 'dtstart':
|
||||
if($allday)
|
||||
$val = $this->format_daystamp($val);
|
||||
else
|
||||
$val = $this->format_timestamp($val);
|
||||
break;
|
||||
|
||||
default:
|
||||
$val = $this->escape_string($val);
|
||||
}
|
||||
|
||||
return $val;
|
||||
}
|
||||
|
||||
private function format_daystamp($timestamp) {
|
||||
$dt = new \DateTime($timestamp, new \DateTimeZone('Europe/Paris'));
|
||||
return $dt->format(self::DT_FORMATDAY);
|
||||
}
|
||||
|
||||
private function format_timestamp($timestamp) {
|
||||
$dt = new \DateTime($timestamp, new \DateTimeZone('Europe/Paris'));
|
||||
return $dt->format(self::DT_FORMAT);
|
||||
}
|
||||
|
||||
private function escape_string($str) {
|
||||
return preg_replace('/([\,;])/','\\\$1', $str);
|
||||
}
|
||||
|
||||
private function exportformat($string) {
|
||||
return implode(($this->fgdebug?"<br>":"\r\n"), $string).($this->fgdebug?"<br>":"\r\n");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user