Skip to content

Commit

Permalink
Merge branch 'master' of github.com:DieSchittigs/contao-content-api-b…
Browse files Browse the repository at this point in the history
…undle
  • Loading branch information
saibotd committed May 8, 2019
2 parents ece0f34 + aa0dcb2 commit 46e81be
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 38 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"php": "^5.6|^7.0",
"contao/core-bundle": "^4.4"
},
"license": "MIT License",
"license": "MIT",
"authors": [
{
"name": "Tobias Duehr",
Expand Down
7 changes: 4 additions & 3 deletions src/Controller/ContentApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace DieSchittigs\ContaoContentApiBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use DieSchittigs\ContaoContentApiBundle\FrontendApi;
Expand All @@ -19,7 +18,9 @@ private function handle(Request $request){
$readers = $this->getParameter('content_api_readers');
$this->frontendApi = new FrontendApi($readers);
$this->container->get('contao.framework')->initialize();
return $this->json($this->frontendApi->handle($request));
$response = $this->json($this->frontendApi->handle($request));
$response->headers->add(['Access-Control-Allow-Origin' => '*']);
return $response;
}

/**
Expand Down
53 changes: 24 additions & 29 deletions src/Resources/contao/classes/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,34 @@
namespace DieSchittigs\ContaoContentApiBundle;

use Contao\FilesModel;
use Contao\ImageSizeModel;
use Contao\ImageSizeItemModel;
use Contao\Image;
use Contao\Controller;

class FileHelper
{
public static function file($uuid, $size = null)
{
$model = FilesModel::findByUuid($uuid);
if(!$model) return null;
$result = Helper::toObj($model);
unset($result->pid);
unset($result->uuid);
$result->path = '/' . $result->path;
if($size && count($size) == 3){
if(is_numeric($size[2])){
$result->size = Helper::toObj(ImageSizeModel::findOneById($size[2]));
if($result->size){
$result->size->subSizes = Helper::toObj(ImageSizeItemModel::findVisibleByPid($result->size->id));
}
} else {
$result->size = [
'width' => $size[0],
'height' => $size[1],
'resizeMode' => $size[2]
];
}
try{
$result->resizedSrc = Image::create($uuid, $size)->executeResize()->getResizedPath();
} catch (\Exception $e){
$result->resizedSrc = null;
}
}
return $result;
$result = new \stdClass;
$model = FilesModel::findByUuid($uuid);
if(!$model) return null;
if(!is_string($size)) $size = \serialize($size);
$image = [
'id' => $model->id,
'uuid' => $model->uuid,
'name' => $model->name,
'singleSRC' => $model->path,
'size' => $size,
'filesModel' => $model
];
Controller::addImageToTemplate($result, $image);
$result->src = "/$result->src";
$result->singleSRC = "/$result->singleSRC";
$result->picture['img']['src'] = '/' . $result->picture['img']['src'];
$result->picture['img']['srcset'] = '/' . $result->picture['img']['srcset'];
foreach($result->picture['sources'] as &$source){
$source['src'] = '/' . $source['src'];
$source['srcset'] = '/' . $source['srcset'];
}
return $result;
}

private static function children($uuid, $depth = 0){
Expand Down Expand Up @@ -66,3 +60,4 @@ public static function get($path, $depth = 1)
return $result;
}
}

2 changes: 1 addition & 1 deletion src/Resources/contao/classes/PageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class PageHelper
public static function getSubPages($pageId)
{
$subPages = [];
$pages = PageModel::findPublishedByPid($pageId);
$pages = PageModel::findPublishedByPid($pageId, ['order' => 'sorting']);
if (!$pages) {
return $subPages;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Resources/contao/classes/SitemapHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ class SitemapHelper
{
public static function getSitemap($lang = null)
{
if (!$lang) $lang = Helper::defaultLang();
$sitemap = [];
$rootPages = PageModel::findPublishedRootPages(['order' => 'sorting ASC']);
foreach($rootPages as $rootPage){
if($rootPage->language == $lang) break;
if($lang && $rootPage->language != $lang) continue;
$rootPage->loadDetails();
$sitemap = array_merge($sitemap, PageHelper::getSubPages($rootPage->id));
}
$rootPage->loadDetails();
return PageHelper::getSubPages($rootPage->id);
return $sitemap;
}
}

0 comments on commit 46e81be

Please sign in to comment.