Skip to content

Commit

Permalink
Merge branch 'release/1.1.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
maddhatter committed Mar 16, 2015
2 parents c59a7fe + f2ef990 commit 516e867
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 5 deletions.
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ $calendar = \Calendar::addEvents($events) //add an array with addEvents
'color' => '#800',
])->setOptions([ //set fullcalendar options
'firstDay' => 1
]);
])->setCallbacks([ //set fullcalendar callback options (will not be JSON encoded)
'viewRender' => 'function() {alert("Callbacks!");}'
]);

return view('hello', compact('calendar'));
```
Expand Down
85 changes: 81 additions & 4 deletions src/MaddHatter/LaravelFullcalendar/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ class Calendar
*/
protected $userOptions = [];

/**
* User defined callback options
*
* @var array
*/
protected $callbacks = [];

/**
* @param Factory $view
* @param EventCollection $eventCollection
Expand Down Expand Up @@ -175,16 +182,86 @@ public function getOptions()
}

/**
* Get options+events JSON
* Set fullcalendar callback options
*
* @param array $callbacks
* @return $this
*/
public function setCallbacks(array $callbacks)
{
$this->callbacks = $callbacks;

return $this;
}

/**
* Get the callbacks currently defined
*
* @return array
*/
public function getCallbacks()
{
return $this->callbacks;
}

/**
* Get options+events JSON
*
* @return string
*/
protected function getOptionsJson()
{
$options = $this->getOptions();
$options = $this->getOptions();
$placeholders = $this->getCallbackPlaceholders();
$parameters = array_merge($options, $placeholders);

$parameters['events'] = $this->eventCollection->toArray();

$json = json_encode($parameters);

if ($placeholders) {
return $this->replaceCallbackPlaceholders($json, $placeholders);
}

return $json;

}

/**
* Generate placeholders for callbacks, will be replaced after JSON encoding
*
* @return array
*/
protected function getCallbackPlaceholders()
{
$callbacks = $this->getCallbacks();
$placeholders = [];

$options['events'] = $this->eventCollection->toArray();
foreach ($callbacks as $name => $callback) {
$placeholders[$name] = '[' . md5($callback) . ']';
}

return $placeholders;
}

/**
* Replace placeholders with non-JSON encoded values
*
* @param $json
* @param $placeholders
* @return string
*/
protected function replaceCallbackPlaceholders($json, $placeholders)
{
$search = [];
$replace = [];

return json_encode($options);
foreach ($placeholders as $name => $placeholder) {
$search[] = '"' . $placeholder . '"';
$replace[] = $this->getCallbacks()[$name];
}

return str_replace($search, $replace, $json);
}

}

0 comments on commit 516e867

Please sign in to comment.