Skip to content

Commit

Permalink
Working
Browse files Browse the repository at this point in the history
  • Loading branch information
bobdenotter committed Sep 8, 2023
1 parent 60f1828 commit 9aebe9c
Show file tree
Hide file tree
Showing 11 changed files with 273 additions and 11 deletions.
3 changes: 2 additions & 1 deletion config/bolt/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ caching:
frontend_menu: ~
backend_menu: ~
files_index: ~
list_format: true

# The theme to use.
#
Expand Down Expand Up @@ -256,4 +257,4 @@ reset_password_settings:
user_show_sort&filter: true

# Use Ajaxy saving ("Stay on page"), or traditional saving
ajaxy_saving: true
ajaxy_saving: false
1 change: 1 addition & 0 deletions config/bolt/contenttypes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pages:
singular_name: Page
title_format: '{heading} - {subheading}'
excerpt_format: '📦 {teaser}'
list_format: '{contenttype} Nº {id}: {title} - {status}'
fields:
heading:
type: text
Expand Down
62 changes: 62 additions & 0 deletions src/Command/ClearListFormatCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace Bolt\Command;

use Bolt\Utils\ListFormatHelper;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class ClearListFormatCommand extends Command
{
/** @var string */
protected static $defaultName = 'cache:clear-list-format';

/** @var ListFormatHelper */
private $listFormatHelper;

public function __construct(ListFormatHelper $listFormatHelper)
{
$this->listFormatHelper = $listFormatHelper;

parent::__construct();
}

/**
* {@inheritdoc}
*/
protected function configure(): void
{
$this
->setDescription('Clear Bolt\'s cached ListFormat data. ')
->setHelp(
<<<'HELP'
The <info>%command.name%</info> command clears the `list_format` and `title` columns in the database. Be sure to run `bolt:update-list-format` afterwards.
HELP
);
}

/**
* This method is executed after initialize(). It usually contains the logic
* to execute to complete this command task.
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

$io->text('// Clearing the Bolt Content `list_format` and `title` columns.');

$success = $this->listFormatHelper->clearColumns();

if ($success) {
$io->success('Cleared successfully. Be sure to run `bolt:update-list-format` next.');
} else {
$io->warning('Not all the rows could be cleared successfully. Remove them manually');
}

return Command::SUCCESS;
}
}
64 changes: 64 additions & 0 deletions src/Command/UpdateListFormatCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace Bolt\Command;

use Bolt\Utils\ListFormatHelper;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class UpdateListFormatCommand extends Command
{
/** @var string */
protected static $defaultName = 'bolt:update-list-format';

public function __construct(ListFormatHelper $listFormatHelper)
{
$this->listFormatHelper = $listFormatHelper;

Check failure on line 21 in src/Command/UpdateListFormatCommand.php

View workflow job for this annotation

GitHub Actions / PHPStan

Access to an undefined property Bolt\Command\UpdateListFormatCommand::$listFormatHelper.

Check failure on line 21 in src/Command/UpdateListFormatCommand.php

View workflow job for this annotation

GitHub Actions / PHPStan

Access to an undefined property Bolt\Command\UpdateListFormatCommand::$listFormatHelper.

parent::__construct();
}

/**
* {@inheritdoc}
*/
protected function configure(): void
{
$this
->setDescription('Update Bolt\'s cached ListFormat data. ')
->setHelp(
<<<'HELP'
The <info>%command.name%</info> updates clears the `list_format` and `title` columns in the database.
HELP
);
// ->addArgument('amount', InputArgument::OPTIONAL, 'The number of rows to update in one invocation', 100);

}

/**
* This method is executed after initialize(). It usually contains the logic
* to execute to complete this command task.
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

$io->text('// Clearing the Bolt Content `list_format` and `title` columns.');

$amount = 10000;

$success = $this->listFormatHelper->updateColumns($amount);

Check failure on line 54 in src/Command/UpdateListFormatCommand.php

View workflow job for this annotation

GitHub Actions / PHPStan

Access to an undefined property Bolt\Command\UpdateListFormatCommand::$listFormatHelper.

Check failure on line 54 in src/Command/UpdateListFormatCommand.php

View workflow job for this annotation

GitHub Actions / PHPStan

Access to an undefined property Bolt\Command\UpdateListFormatCommand::$listFormatHelper.

if ($success) {
$io->success('Rows updated successfully.');
} else {
$io->warning('Not all the rows could be cleared successfully. Remove them manually');
}

return Command::SUCCESS;
}
}
8 changes: 5 additions & 3 deletions src/Configuration/Parser/ContentTypesParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,15 @@ protected function parseContentType($key, array $contentType): ?ContentType
}

// Make sure title_format is set
if (isset($contentType['title_format'])) {
$contentType['title_format'] = $contentType['title_format'];
} else {
if (! isset($contentType['title_format'])) {
$fields = $contentType['fields']['slug']['uses'];
$contentType['title_format'] = '{' . implode('} {', $fields) . '}';
}

if (! isset($contentType['list_format'])) {
$contentType['list_format'] = '[{contenttype} Nº{id} - {status}] {title}';
}

// Make sure taxonomy is an array.
if (isset($contentType['taxonomy'])) {
$contentType['taxonomy'] = (array) $contentType['taxonomy'];
Expand Down
9 changes: 8 additions & 1 deletion src/DataFixtures/ContentFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Bolt\Entity\Field\SelectField;
use Bolt\Enum\Statuses;
use Bolt\Repository\FieldRepository;
use Bolt\Twig\ContentExtension;
use Bolt\Utils\FakeContent;
use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
Expand Down Expand Up @@ -45,7 +46,10 @@ class ContentFixtures extends BaseFixture implements DependentFixtureInterface,
/** @var TagAwareCacheInterface */
private $cache;

public function __construct(Config $config, FileLocations $fileLocations, TagAwareCacheInterface $cache, string $defaultLocale)
/** @var ContentExtension */
private $contentExtension;

public function __construct(Config $config, FileLocations $fileLocations, TagAwareCacheInterface $cache, string $defaultLocale, ContentExtension $contentExtension)
{
$this->config = $config;
$this->faker = Factory::create();
Expand All @@ -58,6 +62,7 @@ public function __construct(Config $config, FileLocations $fileLocations, TagAwa
$this->fileLocations = $fileLocations;
$this->defaultLocale = $defaultLocale;
$this->cache = $cache;
$this->contentExtension = $contentExtension;
}

public function getDependencies()
Expand Down Expand Up @@ -115,6 +120,8 @@ private function loadContent(ObjectManager $manager): void
$content->setModifiedAt($this->faker->dateTimeBetween('-1 year'));
$content->setPublishedAt($this->faker->dateTimeBetween('-1 year'));

$content->setContentExtension($this->contentExtension);

$preset = $this->getPreset($contentType['slug']);

if ($i === 1 || ! empty($preset)) {
Expand Down
44 changes: 44 additions & 0 deletions src/Entity/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Bolt\Entity\Field\SetField;
use Bolt\Enum\Statuses;
use Bolt\Repository\FieldRepository;
use Bolt\Utils\ContentHelper;
use DateTimeZone;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
Expand Down Expand Up @@ -121,6 +122,16 @@ class Content
*/
private $depublishedAt = null;

/**
* @ORM\Column(type="string", length=191, nullable=true)
*/
private $title;

/**
* @ORM\Column(type="string", length=191, nullable=true)
*/
private $listFormat;

/**
* @var Collection|Field[]
*
Expand Down Expand Up @@ -165,6 +176,11 @@ class Content
*/
private $relationsToThisContent;

/**
* @var ContentHelper
*/
private $contentHelper;

Check failure on line 182 in src/Entity/Content.php

View workflow job for this annotation

GitHub Actions / PHPStan

Property Bolt\Entity\Content::$contentHelper is never written, only read.

Check failure on line 182 in src/Entity/Content.php

View workflow job for this annotation

GitHub Actions / PHPStan

Property Bolt\Entity\Content::$contentHelper is never written, only read.

public function __construct(?ContentType $contentTypeDefinition = null)
{
$this->createdAt = $this->convertToUTCFromLocal(new \DateTime());
Expand Down Expand Up @@ -872,4 +888,32 @@ private function getFieldValuesFromDefinition(): ?array

return $fieldValues;
}

/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function setTitle(): self
{
$this->title = $this->getExtras()['title'];

return $this;
}

public function getListFormat(): ?string
{
return $this->listFormat;
}

/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function setListFormat(): self
{
$this->listFormat = $this->getExtras()['listFormat'];

return $this;
}

}
1 change: 1 addition & 0 deletions src/Entity/ContentExtrasTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function getExtras(): array
'title' => $this->contentExtension->getAnyTitle($content, 80),
'image' => $this->contentExtension->getImage($content, true),
'excerpt' => $this->contentExtension->getExcerpt($content),
'listFormat' => $this->contentExtension->getListFormat($content),
'link' => $this->contentExtension->getLink($content),
'editLink' => $this->contentExtension->getEditLink($content),
'statusLink' => $this->contentExtension->getStatusLink($content),
Expand Down
11 changes: 10 additions & 1 deletion src/Twig/ContentExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ public function getExcerpt($content, int $length = 280, bool $includeTitle = fal
}

if (ContentHelper::isSuitable($content, 'excerpt_format')) {
$excerpt = $this->contentHelper->get($content, $content->getDefinition()->get('excerpt_format'), $this->requestStack->getCurrentRequest()->getLocale());
$locale = $this->requestStack->getCurrentRequest() ? $this->requestStack->getCurrentRequest()->getLocale() : null;
$excerpt = $this->contentHelper->get($content, $content->getDefinition()->get('excerpt_format'), $locale);
} else {
$excerpt = $this->getFieldBasedExcerpt($content, $length, $includeTitle);
}
Expand All @@ -284,6 +285,14 @@ public function getExcerpt($content, int $length = 280, bool $includeTitle = fal
return $pre . Excerpt::getExcerpt($excerpt, $length, $focus) . $post;
}

public function getListFormat($content)
{
$listFormat = $this->contentHelper->get($content, $content->getDefinition()->get('list_format'));

return $listFormat;
}


private function getFieldBasedExcerpt(Content $content, int $length, bool $includeTitle = false): string
{
$excerptParts = [];
Expand Down
73 changes: 73 additions & 0 deletions src/Utils/ListFormatHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Bolt\Utils;

use Bolt\Configuration\Config;
use Bolt\Repository\ContentRepository;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManagerInterface;

class ListFormatHelper
{
/** @var Config */
private $config;

Check failure on line 13 in src/Utils/ListFormatHelper.php

View workflow job for this annotation

GitHub Actions / PHPStan

Property Bolt\Utils\ListFormatHelper::$config is never read, only written.

Check failure on line 13 in src/Utils/ListFormatHelper.php

View workflow job for this annotation

GitHub Actions / PHPStan

Property Bolt\Utils\ListFormatHelper::$config is never read, only written.

/** @var Connection */
private $connection;

/** @var string */
private $prefix;

/** @var ContentRepository */
private $contentRepository;

/** @var EntityManagerInterface */
private $em;

public function __construct(Config $config, Connection $connection, ContentRepository $contentRepository, string $tablePrefix = 'bolt_', EntityManagerInterface $em)

Check failure on line 27 in src/Utils/ListFormatHelper.php

View workflow job for this annotation

GitHub Actions / PHPStan

Deprecated in PHP 8.0: Required parameter $em follows optional parameter $tablePrefix.
{
$this->config = $config;
$this->connection = $connection;
$this->prefix = $tablePrefix;
$this->contentRepository = $contentRepository;
$this->em = $em;
}

public function clearColumns(): bool
{
$query = sprintf('UPDATE %scontent SET title = "", list_format = ""', $this->prefix);
$result = $this->connection->executeQuery($query);

return (bool) $result;
}

public function updateColumns(int $limit = 100): bool
{
$query = sprintf('SELECT id FROM %scontent WHERE title = "" OR list_format = "" LIMIT %d', $this->prefix, $limit);

$rows = $this->connection->fetchAllAssociative($query);

$counter = 0;

foreach ($rows as $row) {
$record = $this->contentRepository->findOneById($row['id']);

$record->setListFormat();
$this->em->persist($record);


$counter++;
if ($counter > 20) {
echo '.';
$this->em->flush();
$counter = 0;
}

}

$this->em->flush();

return true;
}

}
Loading

0 comments on commit 9aebe9c

Please sign in to comment.