Skip to content

Commit

Permalink
テーマの初期データ読込を行うとコンテンツ管理の作成日が空欄になる問題を改善 fix #3183
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuring committed Apr 23, 2024
1 parent 53dbbf1 commit 79f7ad1
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 67 deletions.
11 changes: 11 additions & 0 deletions plugins/baser-core/src/BaserCorePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,17 @@ function(RouteBuilder $routes) {
parent::routes($routes);
}

/**
* 初期データ読み込み時の更新処理
* @param array $options
* @return void
*/
public function updateDefaultData($options = []) : void
{
// コンテンツの作成日を更新
$this->updateDateNow('BaserCore.Contents', ['created_date'], [], $options);
}

/**
* services
* @param ContainerInterface $container
Expand Down
6 changes: 5 additions & 1 deletion plugins/baser-core/src/BcPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,11 @@ public function updateDateNow(string $table, array $fields, array $conditions =
$options = array_merge([
'connection' => 'default'
], $options);
$table = TableRegistry::getTableLocator()->get($table, ['connectionName' => $options['connection']]);
$tableOptions = [];
if($options['connection'] && $options['connection'] !== 'default') {
$tableOptions = ['connectionName' => $options['connection']];
}
$table = TableRegistry::getTableLocator()->get($table, $tableOptions);
$beforeSaveEvents = BcUtil::offEvent($table->getEventManager(), 'Model.beforeSave');
$afterSaveEvents = BcUtil::offEvent($table->getEventManager(), 'Model.afterSave');

Expand Down
8 changes: 6 additions & 2 deletions plugins/baser-core/src/Service/BcDatabaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,14 +405,18 @@ protected function _loadDefaultDataPattern($pattern, $theme, $plugin = 'BaserCor
if (!$this->loadCsv(['path' => $file, 'encoding' => 'auto', 'dbConfigKeyName' => $dbConfigKeyName])) {
$this->log(sprintf(__d('baser_core', '%s の読み込みに失敗。'), $file));
$result = false;
} else {
break;
}
} catch(\Throwable $e) {
throw $e;
}
}
}
try {
$pluginClass = Plugin::getCollection()->get($plugin);
if(method_exists($pluginClass, 'updateDefaultData')) {
$pluginClass->updateDefaultData();
}
} catch (\Throwable) {}
return $result;
}

Expand Down
14 changes: 13 additions & 1 deletion plugins/bc-blog/src/BcBlogPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use BaserCore\Model\Table\SitesTable;
use BaserCore\Utility\BcContainerTrait;
use BcBlog\ServiceProvider\BcBlogServiceProvider;
use Cake\Core\Configure;
use Cake\Core\ContainerInterface;
use BaserCore\Annotation\NoTodo;
use BaserCore\Annotation\Checked;
Expand Down Expand Up @@ -48,12 +49,23 @@ public function install($options = []) : bool
{
$result = parent::install($options);
// ブログ記事の投稿日を更新
if(empty($options['db_init'])) {
if(Configure::read('BcEnv.isInstalled') && empty($options['db_init'])) {
$this->updateDateNow('BcBlog.BlogPosts', ['posted'], [], $options);
}
return $result;
}

/**
* 初期データ読み込み時の更新処理
* @param array $options
* @return void
*/
public function updateDefaultData($options = []) : void
{
// ブログ記事の投稿日を更新
$this->updateDateNow('BcBlog.BlogPosts', ['posted'], [], $options);
}

/**
* プラグインをアンインストールする
*
Expand Down
19 changes: 16 additions & 3 deletions plugins/bc-custom-content/src/BcCustomContentPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,25 @@ public function install($options = []): bool
'connection' => 'default'
], $options);
$result = parent::install($options);
$table = TableRegistry::getTableLocator()->get('BcCustomContent.CustomEntries', ['connectionName' => $options['connection']]);
$table->setUp(1);
$this->updateDateNow('BcCustomContent.CustomEntries', ['published'], [], $options);
if(Configure::read('BcEnv.isInstalled') && empty($options['db_init'])) {
$table = TableRegistry::getTableLocator()->get('BcCustomContent.CustomEntries', ['connectionName' => $options['connection']]);
$table->setUp(1);
$this->updateDateNow('BcCustomContent.CustomEntries', ['published'], [], $options);
}
return $result;
}

/**
* 初期データ読み込み時の更新処理
* @param array $options
* @return void
*/
public function updateDefaultData($options = []) : void
{
// エントリーの公開日を更新
$this->updateDateNow('BcCustomContent.CustomEntries', ['published'], [], $options);
}

/**
* services
* @param ContainerInterface $container
Expand Down
53 changes: 1 addition & 52 deletions plugins/bc-installer/src/Service/InstallationsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,61 +357,10 @@ public function setSiteName(string $name)
*/
public function executeDefaultUpdates(): bool
{
$result = true;
if (!$this->_updateContents()) {
$this->log(__d('baser_core', 'コンテンツの更新に失敗しました。'));
$result = false;
}
if (!$this->_updateBlogPosts()) {
$this->log(__d('baser_core', 'ブログ記事の更新に失敗しました。'));
$result = false;
}
/** @var SearchIndexesServiceInterface $searchIndexesService */
$searchIndexesService = $this->getService(SearchIndexesServiceInterface::class);
$searchIndexesService->reconstruct();
return $result;
}

/**
* コンテンツの作成日を更新する
*
* @return bool
* @checked
* @noTodo
*/
protected function _updateContents(): bool
{
$contentsTable = TableRegistry::getTableLocator()->get('BaserCore.Contents');
$contents = $contentsTable->find()->all();
$result = true;
foreach($contents as $content) {
$content->created_date = new FrozenTime();
if (!$contentsTable->save($content)) {
$result = false;
}
}
return $result;
}

/**
* コンテンツの作成日を更新する
*
* @return bool
* @checked
* @noTodo
*/
protected function _updateBlogPosts(): bool
{
$table = TableRegistry::getTableLocator()->get('BcBlog.BlogPosts');
$entities = $table->find()->all();
$result = true;
foreach($entities as $entity) {
$entity->posted = new FrozenTime();
if (!$table->save($entity)) {
$result = false;
}
}
return $result;
return true;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,6 @@ public function testExecuteDefaultUpdates()
$this->assertTrue($result, 'データベースのデータに初期更新に失敗しました');
}

/**
* test _updateContents
*/
public function test_updateContents()
{
$this->markTestIncomplete('このテストは、まだ実装されていません。');
}

/**
* test installCorePlugin
*/
Expand Down

0 comments on commit 79f7ad1

Please sign in to comment.