Skip to content

Commit

Permalink
Merge pull request #1109 from J9rem/fix/edit-handler-save-metadata
Browse files Browse the repository at this point in the history
Fix/edit handler save metadata
  • Loading branch information
mrflos authored Oct 19, 2023
2 parents 9c32fe2 + 25b6001 commit ef4dd73
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 17 deletions.
3 changes: 3 additions & 0 deletions includes/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ services:
YesWiki\Core\Service\CommentService:
tags:
- { name: yeswiki.event_subscriber }
YesWiki\Core\Service\ThemeManager:
tags:
- { name: yeswiki.event_subscriber }
1 change: 1 addition & 0 deletions includes/services/PageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ public function deleteOrphaned($tag)
$this->dbService->query("DELETE FROM {$this->dbService->prefixTable('links')} WHERE from_tag='{$this->dbService->escape($tag)}' ");
$this->dbService->query("DELETE FROM {$this->dbService->prefixTable('acls')} WHERE page_tag='{$this->dbService->escape($tag)}' ");
$this->dbService->query("DELETE FROM {$this->dbService->prefixTable('triples')} WHERE `resource`='{$this->dbService->escape($tag)}' and `property`='".TripleStore::TYPE_URI."' and `value`='".EntryManager::TRIPLES_ENTRY_ID."'");
$this->dbService->query("DELETE FROM {$this->dbService->prefixTable('triples')} WHERE `resource`='{$this->dbService->escape($tag)}' and `property`='http://outils-reseaux.org/_vocabulary/metadata'");
$this->dbService->query("DELETE FROM {$this->dbService->prefixTable('referrers')} WHERE page_tag='{$this->dbService->escape($tag)}' ");
$this->tagsManager->deleteAll($tag);

Expand Down
52 changes: 51 additions & 1 deletion includes/services/ThemeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
namespace YesWiki\Core\Service;

use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use YesWiki\Wiki;
use YesWiki\Core\Entity\Event;
use YesWiki\Core\Service\Performer;
use YesWiki\Core\Service\PageManager;
use YesWiki\Core\Service\TemplateEngine;
use YesWiki\Security\Controller\SecurityController;
use YesWiki\Templates\Service\Utils;

class ThemeManager
class ThemeManager implements EventSubscriberInterface
{
public const CUSTOM_CSS_PRESETS_PATH = 'custom/css-presets';
public const CUSTOM_CSS_PRESETS_PREFIX = 'custom/';
Expand Down Expand Up @@ -46,6 +49,7 @@ class ThemeManager
protected $favorites;
protected $fileContent;
protected $fileLoaded;
protected $pageManager;
protected $params;
protected $performer;
protected $securityController;
Expand All @@ -59,9 +63,17 @@ class ThemeManager
protected $utils;
protected $wiki;

public static function getSubscribedEvents()
{
return [
'page.created' => 'saveMetadataIfNeeded',
];
}

public function __construct(
Wiki $wiki,
TemplateEngine $twig,
PageManager $pageManager,
ParameterBagInterface $params,
Performer $performer,
SecurityController $securityController,
Expand All @@ -80,6 +92,7 @@ public function __construct(
];
$this->fileContent = null;
$this->fileLoaded = false;
$this->pageManager = $pageManager;
$this->params = $params;
$this->performer = $performer;
$this->securityController = $securityController;
Expand Down Expand Up @@ -884,4 +897,41 @@ protected function importFontFile(string $family, string $style, string $weight,

return $url;
}

/**
* save metadata for new page if needed
* @param Event $event
*/
public function saveMetadataIfNeeded(Event $event)
{
$data = $event->getData();
if (!empty($data['data']['tag'])
&& !empty($_POST["newpage"])
&& isset($_POST['theme'])) {
$tag = $data['data']['tag'];
$previousMetadata = $this->pageManager->getMetadata($tag);

$tagIsCurrentPage = (
!empty($_GET['wiki'])
&& is_string($_GET['wiki'])
&& explode('/',$_GET['wiki'],2)[0] === $tag
) || explode('/',array_key_first($_GET),2)[0] === $tag;

if (empty($previousMetadata) // only if no previous metadata
&& $tagIsCurrentPage){
$metadata = [
'theme' => $_POST["theme"],
'style' => $_POST["style"] ?? CSS_PAR_DEFAUT ,
'squelette' => $_POST["squelette"] ?? SQUELETTE_PAR_DEFAUT ,
'bgimg' => $_POST["bgimg"] ?? null
];
foreach (ThemeManager::SPECIAL_METADATA as $metadataName) {
if (!empty($_POST[$metadataName])) {
$metadata[$metadataName] = $_POST[$metadataName];
}
}
$this->pageManager->setMetadata($tag, $metadata);
}
}
}
}
21 changes: 21 additions & 0 deletions templates/handlers/edit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,27 @@
{{ renderAction('aceditor', { saveButton: true, name: 'body', value: body })|raw }}
{% endif %}

{% if request.newpage is same as '1' and request.theme is not empty %}
<input type="hidden" name="newpage" value="1"/>
{% for key in [
'theme',
'style',
'squelette',
'bgimg',
'PageFooter',
'PageHeader',
'PageTitre',
'PageRapideHaut',
'PageMenuHaut',
'PageMenu',
'favorite_preset'
] %}
{% if request[key] is not empty %}
<input type="hidden" name="{{key}}" value="{{request[key]}}"/>
{% endif %}
{% endfor %}
{% endif %}

{# Filled by tools/tags/handles/page/edit__ #}
{# TODO: stop using all those edit__, and put directly the code here #}
<div class="tags-container"></div>
Expand Down
16 changes: 0 additions & 16 deletions tools/templates/handlers/page/__edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,6 @@
die("acc&egrave;s direct interdit");
}

// Sauvegarde des metas
if (isset($_GET["newpage"]) && $_GET["newpage"]==1 && isset($_GET["theme"]) && !isset($this->page['metadatas']['theme'])) {
$metadata = [
'theme' => $_GET["theme"],
'style' => $_GET["style"] ?? CSS_PAR_DEFAUT ,
'squelette' => $_GET["squelette"] ?? SQUELETTE_PAR_DEFAUT ,
'bgimg' => $_GET["bgimg"] ?? null
];
foreach (ThemeManager::SPECIAL_METADATA as $metadataName) {
if (!empty($_GET[$metadataName])) {
$metadata[$metadataName] = $_GET[$metadataName];
}
}
$this->SaveMetaDatas($this->GetPageTag(), $metadata);
}

// Si une valeur de body est passee en paramétre GET (et pas POST) on l'ajoute en titre dans la nouvelle page vierge
if (isset($_GET["body"]) && !isset($_POST["body"])) {
$_POST["body"] = '======'.$_GET["body"].'======';
Expand Down

0 comments on commit ef4dd73

Please sign in to comment.