Skip to content

Commit

Permalink
Add additional parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
maddhatter committed Oct 23, 2015
2 parents 2bf4983 + ac17d8e commit fc6c07c
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 9 deletions.
54 changes: 53 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ $event = \Calendar::event(
true, //full day event?
'2015-02-14', //start time, must be a DateTime object or valid DateTime format (http://bit.ly/1z7QWbg)
'2015-02-14' //end time, must be a DateTime object or valid DateTime format (http://bit.ly/1z7QWbg),
1 //optional event ID
1, //optional event ID
[
'url' => 'http://full-calendar.io'
]
);
```
#### Implementing `Event` Interface
Expand Down Expand Up @@ -131,6 +134,55 @@ class EventModel extends Eloquent implements \MaddHatter\LaravelFullcalendar\Ide

```

### Additional Event Parameters

If you want to add [additional parameters](http://fullcalendar.io/docs/event_data/Event_Object) to your events, there are two options:

#### Using `Calendar::event()`

Pass an array of `'parameter' => 'value'` pairs as the 6th parameter to `Calendar::event()`:

```php
$event = \Calendar::event(
"Valentine's Day", //event title
true, //full day event?
'2015-02-14', //start time, must be a DateTime object or valid DateTime format (http://bit.ly/1z7QWbg)
'2015-02-14' //end time, must be a DateTime object or valid DateTime format (http://bit.ly/1z7QWbg),
1, //optional event ID
[
'url' => 'http://full-calendar.io',
//any other full-calendar supported parameters
]
);

```

#### Add an `getEventOptions` method to your event class

```php
<?php
class CalendarEvent extends \Illuminate\Database\Eloquent\Model implements \MaddHatter\LaravelFullcalendar\Event
{
//...

/**
* Optional FullCalendar.io settings for this event
*
* @return array
*/
public function getEventOptions()
{
return [
'color' => $this->background_color,
//etc
];
}

//...
}

```

### Create a Calendar
To create a calendar, in your route or controller, create your event(s), then pass them to `Calendar::addEvent()` or `Calendar::addEvents()` (to add an array of events). `addEvent()` and `addEvents()` can be used fluently (chained together). Their second parameter accepts an array of valid [FullCalendar Event Object parameters](http://fullcalendar.io/docs/event_data/Event_Object/).

Expand Down
7 changes: 4 additions & 3 deletions src/MaddHatter/LaravelFullcalendar/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,13 @@ public function __construct(Factory $view, EventCollection $eventCollection)
* @param string $isAllDay
* @param string|DateTime $start If string, must be valid datetime format: http://bit.ly/1z7QWbg
* @param string|DateTime $end If string, must be valid datetime format: http://bit.ly/1z7QWbg
* @param string $id event Id
* @param string $id event Id
* @param array $options
* @return SimpleEvent
*/
public static function event($title, $isAllDay, $start, $end, $id = null)
public static function event($title, $isAllDay, $start, $end, $id = null, $options = [])
{
return new SimpleEvent($title, $isAllDay, $start, $end, $id);
return new SimpleEvent($title, $isAllDay, $start, $end, $id, $options);
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/MaddHatter/LaravelFullcalendar/EventCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ public function toArray()

private function convertToArray(Event $event, array $customAttributes = [])
{
return array_merge([
$eventArray = [
'id' => $this->getEventId($event),
'title' => $event->getTitle(),
'allDay' => $event->isAllDay(),
'start' => $event->getStart()->format('c'),
'end' => $event->getEnd()->format('c'),
], $customAttributes);
];

$eventOptions = method_exists($event, 'getEventOptions') ? $event->getEventOptions() : [];

return array_merge($eventArray, $eventOptions, $customAttributes);
}

private function getEventId(Event $event)
Expand Down
23 changes: 20 additions & 3 deletions src/MaddHatter/LaravelFullcalendar/SimpleEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,27 @@ class SimpleEvent implements IdentifiableEvent
*/
public $end;

/**
* @var array
*/
private $options;

/**
* @param string $title
* @param bool $isAllDay
* @param string|DateTime $start If string, must be valid datetime format: http://bit.ly/1z7QWbg
* @param string|DateTime $end If string, must be valid datetime format: http://bit.ly/1z7QWbg
* @param int|string|null $id
* @param array $options
*/
public function __construct($title, $isAllDay, $start, $end, $id = null)
public function __construct($title, $isAllDay, $start, $end, $id = null, $options = [])
{
$this->title = $title;
$this->isAllDay = $isAllDay;
$this->start = $start instanceof DateTime ? $start : new DateTime($start);
$this->end = $end instanceof DateTime ? $end : new DateTime($end);
$this->end = $start instanceof DateTime ? $end : new DateTime($end);
$this->id = $id;
$this->options = $options;
}

/**
Expand Down Expand Up @@ -102,4 +109,14 @@ public function getEnd()
{
return $this->end;
}
}

/**
* Get the optional event options
*
* @return array
*/
public function getEventOptions()
{
return $this->options;
}
}

0 comments on commit fc6c07c

Please sign in to comment.