2018-01-25 12:56:15 -05:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2018-01-25 12:56:15 -05:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2018-01-25 12:56:15 -05:00
|
|
|
*/
|
|
|
|
|
namespace OCP\AppFramework\Http\Template;
|
|
|
|
|
|
2018-02-09 09:12:49 -05:00
|
|
|
use InvalidArgumentException;
|
2023-06-14 02:56:42 -04:00
|
|
|
use OCP\AppFramework\Http;
|
2018-01-25 12:56:15 -05:00
|
|
|
use OCP\AppFramework\Http\TemplateResponse;
|
2024-08-28 07:15:56 -04:00
|
|
|
use OCP\IInitialStateService;
|
2018-01-25 12:56:15 -05:00
|
|
|
|
2018-02-09 03:41:04 -05:00
|
|
|
/**
|
|
|
|
|
* Class PublicTemplateResponse
|
|
|
|
|
*
|
|
|
|
|
* @since 14.0.0
|
2023-06-14 02:56:42 -04:00
|
|
|
* @template H of array<string, mixed>
|
2024-12-16 10:20:48 -05:00
|
|
|
* @template S of Http::STATUS_*
|
|
|
|
|
* @template-extends TemplateResponse<Http::STATUS_*, array<string, mixed>>
|
2018-02-09 03:41:04 -05:00
|
|
|
*/
|
2018-01-25 12:56:15 -05:00
|
|
|
class PublicTemplateResponse extends TemplateResponse {
|
|
|
|
|
private $headerTitle = '';
|
|
|
|
|
private $headerDetails = '';
|
2024-08-28 07:15:56 -04:00
|
|
|
/** @var IMenuAction[] */
|
2018-01-25 12:56:15 -05:00
|
|
|
private $headerActions = [];
|
2018-04-05 06:22:01 -04:00
|
|
|
private $footerVisible = true;
|
2018-01-25 12:56:15 -05:00
|
|
|
|
2018-02-09 03:41:04 -05:00
|
|
|
/**
|
|
|
|
|
* PublicTemplateResponse constructor.
|
|
|
|
|
*
|
|
|
|
|
* @param string $appName
|
|
|
|
|
* @param string $templateName
|
|
|
|
|
* @param array $params
|
2023-06-14 02:56:42 -04:00
|
|
|
* @param S $status
|
|
|
|
|
* @param H $headers
|
2018-02-09 03:41:04 -05:00
|
|
|
* @since 14.0.0
|
|
|
|
|
*/
|
2024-08-28 07:15:56 -04:00
|
|
|
public function __construct(
|
|
|
|
|
string $appName,
|
|
|
|
|
string $templateName,
|
|
|
|
|
array $params = [],
|
|
|
|
|
$status = Http::STATUS_OK,
|
|
|
|
|
array $headers = [],
|
|
|
|
|
) {
|
2023-06-14 02:56:42 -04:00
|
|
|
parent::__construct($appName, $templateName, $params, 'public', $status, $headers);
|
2024-08-28 07:15:56 -04:00
|
|
|
\OCP\Util::addScript('core', 'public-page-menu');
|
2025-04-29 10:47:27 -04:00
|
|
|
\OCP\Util::addScript('core', 'public-page-user-menu');
|
2024-08-28 07:15:56 -04:00
|
|
|
|
|
|
|
|
$state = \OCP\Server::get(IInitialStateService::class);
|
|
|
|
|
$state->provideLazyInitialState('core', 'public-page-menu', function () {
|
|
|
|
|
$response = [];
|
|
|
|
|
foreach ($this->headerActions as $action) {
|
|
|
|
|
// First try in it is a custom action that provides rendered HTML
|
|
|
|
|
$rendered = $action->render();
|
|
|
|
|
if ($rendered === '') {
|
|
|
|
|
// If simple action, add the response data
|
|
|
|
|
if ($action instanceof SimpleMenuAction) {
|
|
|
|
|
$response[] = $action->getData();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// custom action so add the rendered output
|
|
|
|
|
$response[] = [
|
|
|
|
|
'id' => $action->getId(),
|
|
|
|
|
'label' => $action->getLabel(),
|
|
|
|
|
'html' => $rendered,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $response;
|
|
|
|
|
});
|
2018-01-25 12:56:15 -05:00
|
|
|
}
|
|
|
|
|
|
2018-02-09 03:41:04 -05:00
|
|
|
/**
|
|
|
|
|
* @param string $title
|
|
|
|
|
* @since 14.0.0
|
|
|
|
|
*/
|
2018-01-25 12:56:15 -05:00
|
|
|
public function setHeaderTitle(string $title) {
|
|
|
|
|
$this->headerTitle = $title;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-09 03:41:04 -05:00
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
* @since 14.0.0
|
|
|
|
|
*/
|
2018-01-25 12:56:15 -05:00
|
|
|
public function getHeaderTitle(): string {
|
|
|
|
|
return $this->headerTitle;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-09 03:41:04 -05:00
|
|
|
/**
|
|
|
|
|
* @param string $details
|
|
|
|
|
* @since 14.0.0
|
|
|
|
|
*/
|
2018-01-25 12:56:15 -05:00
|
|
|
public function setHeaderDetails(string $details) {
|
|
|
|
|
$this->headerDetails = $details;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-09 03:41:04 -05:00
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
* @since 14.0.0
|
|
|
|
|
*/
|
2018-01-25 12:56:15 -05:00
|
|
|
public function getHeaderDetails(): string {
|
|
|
|
|
return $this->headerDetails;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-09 03:41:04 -05:00
|
|
|
/**
|
|
|
|
|
* @param array $actions
|
|
|
|
|
* @since 14.0.0
|
2018-02-09 09:12:49 -05:00
|
|
|
* @throws InvalidArgumentException
|
2018-02-09 03:41:04 -05:00
|
|
|
*/
|
2018-01-25 12:56:15 -05:00
|
|
|
public function setHeaderActions(array $actions) {
|
|
|
|
|
foreach ($actions as $action) {
|
|
|
|
|
if ($actions instanceof IMenuAction) {
|
2018-02-09 09:12:49 -05:00
|
|
|
throw new InvalidArgumentException('Actions must be of type IMenuAction');
|
2018-01-25 12:56:15 -05:00
|
|
|
}
|
|
|
|
|
$this->headerActions[] = $action;
|
|
|
|
|
}
|
2020-04-09 07:53:40 -04:00
|
|
|
usort($this->headerActions, function (IMenuAction $a, IMenuAction $b) {
|
2021-04-10 08:50:17 -04:00
|
|
|
return $a->getPriority() <=> $b->getPriority();
|
2018-02-09 09:12:49 -05:00
|
|
|
});
|
2018-01-25 12:56:15 -05:00
|
|
|
}
|
|
|
|
|
|
2018-02-09 03:41:04 -05:00
|
|
|
/**
|
|
|
|
|
* @return IMenuAction
|
|
|
|
|
* @since 14.0.0
|
2018-02-09 09:12:49 -05:00
|
|
|
* @throws \Exception
|
2018-02-09 03:41:04 -05:00
|
|
|
*/
|
2018-01-25 12:56:15 -05:00
|
|
|
public function getPrimaryAction(): IMenuAction {
|
2018-02-09 09:12:49 -05:00
|
|
|
if ($this->getActionCount() > 0) {
|
|
|
|
|
return $this->headerActions[0];
|
2018-01-25 12:56:15 -05:00
|
|
|
}
|
2018-02-09 09:12:49 -05:00
|
|
|
throw new \Exception('No header actions have been set');
|
2018-01-25 12:56:15 -05:00
|
|
|
}
|
|
|
|
|
|
2018-02-09 03:41:04 -05:00
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
* @since 14.0.0
|
|
|
|
|
*/
|
2018-01-25 12:56:15 -05:00
|
|
|
public function getActionCount(): int {
|
|
|
|
|
return count($this->headerActions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return IMenuAction[]
|
2018-02-09 03:41:04 -05:00
|
|
|
* @since 14.0.0
|
2018-01-25 12:56:15 -05:00
|
|
|
*/
|
|
|
|
|
public function getOtherActions(): array {
|
2018-02-09 09:12:49 -05:00
|
|
|
return array_slice($this->headerActions, 1);
|
2018-01-25 12:56:15 -05:00
|
|
|
}
|
|
|
|
|
|
2018-04-05 06:22:01 -04:00
|
|
|
/**
|
|
|
|
|
* @since 14.0.0
|
|
|
|
|
*/
|
|
|
|
|
public function setFooterVisible(bool $visible = false) {
|
|
|
|
|
$this->footerVisible = $visible;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @since 14.0.0
|
|
|
|
|
*/
|
|
|
|
|
public function getFooterVisible(): bool {
|
|
|
|
|
return $this->footerVisible;
|
|
|
|
|
}
|
2024-09-04 19:41:13 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
* @since 14.0.0
|
|
|
|
|
*/
|
|
|
|
|
public function render(): string {
|
|
|
|
|
$params = array_merge($this->getParams(), [
|
|
|
|
|
'template' => $this,
|
|
|
|
|
]);
|
|
|
|
|
$this->setParams($params);
|
|
|
|
|
return parent::render();
|
|
|
|
|
}
|
2019-11-22 14:52:10 -05:00
|
|
|
}
|