2019-08-11 15:04:43 -04:00
|
|
|
<?php
|
|
|
|
|
2023-05-17 12:56:55 -04:00
|
|
|
namespace BookStack\Entities\Controllers;
|
2019-08-11 15:04:43 -04:00
|
|
|
|
2024-02-05 12:35:49 -05:00
|
|
|
use BookStack\Entities\Queries\PageQueries;
|
2019-08-11 15:04:43 -04:00
|
|
|
use BookStack\Entities\Repos\PageRepo;
|
|
|
|
use BookStack\Exceptions\NotFoundException;
|
2023-05-18 15:53:39 -04:00
|
|
|
use BookStack\Http\Controller;
|
2019-08-11 15:04:43 -04:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
class PageTemplateController extends Controller
|
|
|
|
{
|
2024-02-05 12:35:49 -05:00
|
|
|
public function __construct(
|
|
|
|
protected PageRepo $pageRepo,
|
|
|
|
protected PageQueries $pageQueries,
|
|
|
|
) {
|
2019-08-11 15:04:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch a list of templates from the system.
|
|
|
|
*/
|
|
|
|
public function list(Request $request)
|
|
|
|
{
|
|
|
|
$page = $request->get('page', 1);
|
|
|
|
$search = $request->get('search', '');
|
2024-02-05 12:35:49 -05:00
|
|
|
$count = 10;
|
|
|
|
|
|
|
|
$query = $this->pageQueries->visibleTemplates()
|
|
|
|
->orderBy('name', 'asc')
|
|
|
|
->skip(($page - 1) * $count)
|
|
|
|
->take($count);
|
|
|
|
|
|
|
|
if ($search) {
|
|
|
|
$query->where('name', 'like', '%' . $search . '%');
|
|
|
|
}
|
|
|
|
|
|
|
|
$templates = $query->paginate($count, ['*'], 'page', $page);
|
|
|
|
$templates->withPath('/templates');
|
2019-08-11 15:04:43 -04:00
|
|
|
|
|
|
|
if ($search) {
|
|
|
|
$templates->appends(['search' => $search]);
|
|
|
|
}
|
|
|
|
|
2021-08-22 08:15:58 -04:00
|
|
|
return view('pages.parts.template-manager-list', [
|
2021-06-26 11:23:15 -04:00
|
|
|
'templates' => $templates,
|
2019-08-11 15:04:43 -04:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the content of a template.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
2019-08-11 15:04:43 -04:00
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
2019-10-05 07:55:01 -04:00
|
|
|
public function get(int $templateId)
|
2019-08-11 15:04:43 -04:00
|
|
|
{
|
2024-02-05 12:35:49 -05:00
|
|
|
$page = $this->pageQueries->findVisibleByIdOrFail($templateId);
|
2019-08-11 15:04:43 -04:00
|
|
|
|
|
|
|
if (!$page->template) {
|
|
|
|
throw new NotFoundException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json([
|
2021-06-26 11:23:15 -04:00
|
|
|
'html' => $page->html,
|
2019-08-11 15:04:43 -04:00
|
|
|
'markdown' => $page->markdown,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|