Last active
April 21, 2016 15:30
-
-
Save bobdenotter/6732168 to your computer and use it in GitHub Desktop.
extension to get JSON(p) from Bolt. use as `/json/pages?limit=1&callback=c`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// JsonInterface Extension for Bolt, by Bob den Otter | |
namespace JsonInterface; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
class Extension extends \Bolt\BaseExtension | |
{ | |
/** | |
* Info block for Sitemap Extension. | |
*/ | |
function info() | |
{ | |
$data = array( | |
'name' => "JsonInterface", | |
'description' => "An extension to output JSON data structures of your content for your Bolt website.", | |
'author' => "Bob den Otter", | |
'link' => "http://bolt.cm", | |
'version' => "0.8", | |
'required_bolt_version' => "1.2.0", | |
'highest_bolt_version' => "1.2.0", | |
'type' => "General", | |
'first_releasedate' => "2013-07-19", | |
'latest_releasedate' => "2013-09-04", | |
'dependancies' => "", | |
'priority' => 10 | |
); | |
return $data; | |
} | |
/** | |
* Initialize Sitemap. Called during bootstrap phase. | |
*/ | |
function initialize() | |
{ | |
// Set up the routes for the sitemap.. | |
$this->app->match("/json", array($this, 'json')); | |
} | |
public function json() | |
{ | |
// Make sure we output no extra 'fluff'.. | |
$this->app['extensions']->clearSnippetQueue(); | |
$this->app['extensions']->disableJquery(); | |
$this->app['debugbar'] = false; | |
$notice = "OK"; | |
//\util::var_dump($_GET); | |
if (!empty($_GET['contenttype'])) { | |
$contenttype = $this->app['storage']->getContenttype($_GET['contenttype']); | |
if (empty($contenttype)) { | |
$notice = "Contenttype '". safeString($_GET['contenttype']) . "' does not exist."; | |
} | |
} else { | |
$notice = "You should pass a valid contenttype slug."; | |
} | |
if (!empty($_GET['limit'])) { | |
$limit = intval($_GET['limit']); | |
} else { | |
$limit = 10; | |
} | |
if (!empty($_GET['order'])) { | |
$order = safeString($_GET['order']); | |
} else { | |
$order = "id"; | |
} | |
$records = $this->app['storage']->getContent( | |
$contenttype['slug'], | |
array('limit' => $limit, 'order' => $order) | |
); | |
$content = array('notice' => ''); | |
if (!empty($records)) { | |
foreach($records as $record) { | |
$content['records'][ $record->id ] = array( | |
"values" => $record->values, | |
"taxonomy" => $record->taxonomy, | |
"relation" => $record->relation | |
); | |
} | |
} else { | |
$notice = "No records matched or no records present"; | |
} | |
$content['notice'] = $notice; | |
$headers = array('Content-Type' => 'application/javascript; charset=utf-8'); | |
if (!empty($_GET['callback'])) { | |
$body = sprintf("%s(%s);", safeString($_GET['callback']) , json_encode($content)); | |
} else { | |
$body = json_encode($content); | |
} | |
return new Response($body, 200, $headers); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment