Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: build_and_test
on:
push:
branches: [ main, develop ]
branches: [ main, develop, feature/pre-new-admin ]
pull_request:
branches: [ main, develop ]
branches: [ main, develop, feature/pre-new-admin ]

jobs:
test:
Expand Down Expand Up @@ -61,7 +61,7 @@ jobs:
${{ runner.os }}-yarn-

- name: Install Composer dependencies
run: COMPOSER_ROOT_VERSION=dev-develop composer install --no-interaction --prefer-dist
run: COMPOSER_ROOT_VERSION=dev-feature/pre-new-admin composer install --no-interaction --prefer-dist

- name: Install Yarn dependencies
run: yarn install
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/migration_numbering.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: migration_numbering
on:
pull_request:
push:
branches: [ main, develop ]
branches: [ main, develop, feature/pre-new-admin ]
paths:
- 'src/Database/Migration/**'
- 'composer.json'
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/static_analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:
pull_request:
# Run on push, but only if PHP files have changed
push:
branches: [ main, develop ]
branches: [ main, develop, feature/pre-new-admin ]
paths:
- '**.php'
- 'composer.json'
Expand Down Expand Up @@ -36,7 +36,7 @@ jobs:
${{ runner.os }}-composer-

- name: Install Composer dependencies
run: COMPOSER_ROOT_VERSION=dev-develop composer install --no-interaction --prefer-dist
run: COMPOSER_ROOT_VERSION=dev-feature/pre-new-admin composer install --no-interaction --prefer-dist

- name: Run PHPStan
run: composer analyse
Expand Down
3 changes: 2 additions & 1 deletion .phpstan/config.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
parameters:
level: 10
paths:
- ../admin/controllers
- ../src
- ../tests
bootstrapFiles:
Expand All @@ -9,4 +10,4 @@ parameters:
- ../vendor/nails/common/helpers/url.php
- ../vendor/nails/common/src/Common/CodeIgniter/Core/Common.php
- ../vendor/nails/module-auth/helpers/user.php
- ../vendor/nails/module-admin/helpers/permission.php
- ../vendor/nails/module-admin/src/Controller/Base.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?php

namespace Nails\FindReplace\Admin\Controller;
namespace Nails\Admin\FindReplace;

use Nails\Admin\Controller\Base;
use Nails\Admin\Factory\Nav;
use Nails\Admin\Helper;
use Nails\Common\Service\Input;
use Nails\Common\Service\MetaData;
use Nails\Common\Service\Output;
use Nails\Common\Service\PDODatabase;
use Nails\Factory;
use Nails\FindReplace\Admin\OutcomeNotice;
use Nails\FindReplace\Admin\Permission;
use Nails\FindReplace\Admin\Url;
use Nails\FindReplace\Constants;
use Nails\FindReplace\Exclusion\Pattern;
use Nails\FindReplace\Factory\Outcome;
Expand All @@ -30,9 +32,19 @@ class FindReplace extends Base
{
const PER_PAGE = 50;

/**
* The view data, shared with the admin router by reference.
*
* @var array<string, mixed>
*/
public $data;

/**
* @return Nav|array<Nav>|null
*/
public static function announce(): Nav|array|null
{
if (!userHasPermission(Permission\Search::class)) {
if (!userHasPermission(Permission::SEARCH)) {
return null;
}

Expand All @@ -45,6 +57,22 @@ public static function announce(): Nav|array|null
return $nav;
}

/**
* @return array<string, string>
*/
public static function permissions(): array
{
return [
'search' => 'Can search, view results, export CSV, and manage your own saved searches',
'replace' => 'Can apply find-replace replacements and revert your own changes',
];
}

public static function url(string $action = ''): string
{
return Url::to($action);
}

public function index(): void
{
$this->requireSearchPermission();
Expand All @@ -54,10 +82,8 @@ public function index(): void
$this->oUserFeedback->warning(htmlspecialchars($warning, ENT_QUOTES, 'UTF-8'));
}

$this
->addBreadcrumb('Utilities')
->addBreadcrumb('Find & Replace')
->loadView('index');
$this->title('Find &amp; Replace');
Helper::loadView('index');
}

public function create(): void
Expand Down Expand Up @@ -94,23 +120,6 @@ public function create(): void
redirect(self::url('results') . '?id=' . $id);
}

public function status(): void
{
$this->requireSearchPermission();
$search = $this->requireOwned();
$payload = [
'status' => $search->status(),
'tables_scanned' => $search->tablesScanned(),
'tables_total' => $search->tablesTotal(),
'result_count' => $search->resultCount(),
'error' => $search->errorMessage(),
];
$json = json_encode($payload, JSON_THROW_ON_ERROR);
$this->output()
->setContentType('application/json')
->setOutput($json);
}

public function results(): void
{
$this->requireSearchPermission();
Expand Down Expand Up @@ -155,20 +164,17 @@ public function results(): void
);
}

$this
->addBreadcrumb('Utilities')
->addBreadcrumb('Find & Replace', self::url())
->addBreadcrumb($search->term())
->setData('search', $search)
->setData('rows', $rows)
->setData('facets', $facets)
->setData('facetOptions', $this->facetOptions((int) $search->id))
->setData('badgeKeys', $this->registry()->badges())
->setData('outstanding', $outstanding)
->setData('pagination', Helper::paginationObject($page, self::PER_PAGE, $total))
->setData('total', $total)
->setData('canReplace', userHasPermission(Permission\Replace::class))
->loadView('results');
$this->title('Find &amp; Replace: ' . htmlspecialchars($search->term(), ENT_QUOTES, 'UTF-8'));
$this->data['search'] = $search;
$this->data['rows'] = $rows;
$this->data['facets'] = $facets;
$this->data['facetOptions'] = $this->facetOptions((int) $search->id);
$this->data['badgeKeys'] = $this->registry()->badges();
$this->data['outstanding'] = $outstanding;
$this->data['pagination'] = Helper::paginationObject($page, self::PER_PAGE, $total);
$this->data['total'] = $total;
$this->data['canReplace'] = userHasPermission(Permission::REPLACE);
Helper::loadView('results');
}

public function export(): void
Expand Down Expand Up @@ -318,13 +324,10 @@ public function changes(): void
$runs[$row->getRunId()][] = $row;
}

$this
->addBreadcrumb('Utilities')
->addBreadcrumb('Find & Replace', self::url())
->addBreadcrumb('My changes')
->setData('runs', $runs)
->setData('canReplace', userHasPermission(Permission\Replace::class))
->loadView('changes');
$this->title('Find &amp; Replace: My changes');
$this->data['runs'] = $runs;
$this->data['canReplace'] = userHasPermission(Permission::REPLACE);
Helper::loadView('changes');
}

public function revert(): void
Expand Down Expand Up @@ -373,9 +376,21 @@ public function revert(): void
redirect(self::url('changes'));
}

/**
* The admin header prints the title as HTML, so callers escape it.
*/
private function title(string $html): void
{
$page = $this->data['page'] ?? null;

if ($page instanceof MetaData) {
$page->title = $html;
}
}

private function requireSearchPermission(): void
{
if (!userHasPermission(Permission\Search::class)) {
if (!userHasPermission(Permission::SEARCH)) {
unauthorised();
exit;
}
Expand All @@ -385,7 +400,7 @@ private function requireReplacePermission(): void
{
$this->requireSearchPermission();

if (!userHasPermission(Permission\Replace::class)) {
if (!userHasPermission(Permission::REPLACE)) {
unauthorised();
exit;
}
Expand Down
6 changes: 3 additions & 3 deletions admin/views/FindReplace/changes.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use Nails\FindReplace\Admin\Controller\FindReplace;
use Nails\FindReplace\Admin\Url;
use Nails\FindReplace\Factory\ChangeRecord;

/** @var array<string, list<ChangeRecord>> $runs */
Expand All @@ -24,7 +24,7 @@
<?php

if ($canReplace) {
echo form_open(FindReplace::url('revert'), ['class' => 'form-inline']);
echo form_open(Url::to('revert'), ['class' => 'form-inline']);
?>
<input type="hidden" name="run_id" value="<?=htmlspecialchars($runId)?>">
<button type="submit" class="btn btn-sm btn-default">Revert run</button>
Expand Down Expand Up @@ -63,7 +63,7 @@
<?php

if ($canReplace) {
echo form_open(FindReplace::url('revert'));
echo form_open(Url::to('revert'));
?>
<input type="hidden" name="change_id" value="<?=$row->getId()?>">
<button type="submit" class="btn btn-sm btn-default">Revert</button>
Expand Down
18 changes: 12 additions & 6 deletions admin/views/FindReplace/results.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use Nails\Admin\Helper;
use Nails\FindReplace\Admin\Controller\FindReplace;
use Nails\FindReplace\Admin\Url;
use Nails\FindReplace\Factory\StoredResult;
use Nails\FindReplace\Model\Search;
use Nails\FindReplace\Resource;
Expand All @@ -28,6 +28,7 @@
$scanned = $search->tablesScanned();
$totalTables = $search->tablesTotal();
$running = $search->status() === Search::STATUS_RUNNING;
$percent = $totalTables > 0 ? min(100, (int) round($scanned / $totalTables * 100)) : 0;

?>
<fieldset class="find-replace-detail"<?php
Expand Down Expand Up @@ -109,7 +110,12 @@
Waiting for a worker to start this search.
</div>
<p data-find-replace-scanning<?=$running ? '' : ' hidden'?>>Scanning tables.</p>
<div class="js-admin-progress" data-value="<?=$scanned?>" data-max="<?=$totalTables?>"<?=$running ? '' : ' hidden'?>></div>
<div class="find-replace-meter" data-find-replace-meter<?=$running ? '' : ' hidden'?>>
<span class="find-replace-meter__track" role="progressbar" aria-valuemin="0" aria-valuemax="<?=$totalTables?>" aria-valuenow="<?=$scanned?>">
<span class="find-replace-meter__bar" style="width: <?=$percent?>%"></span>
</span>
<span class="find-replace-meter__label"><?=$scanned?> / <?=$totalTables?> tables</span>
</div>
<?php
}

Expand All @@ -125,7 +131,7 @@
<?php

if ($search->status() === Search::STATUS_FAILED) {
echo form_open(FindReplace::url('rerun'));
echo form_open(Url::to('rerun'));
?>
<input type="hidden" name="id" value="<?=$id?>">
<button type="submit" class="btn btn-primary">Run again</button>
Expand All @@ -146,7 +152,7 @@
if ($hasFilters) {
?>
<div class="search clearfix">
<?=form_open(FindReplace::url('results'), ['method' => 'GET'])?>
<?=form_open(Url::to('results'), ['method' => 'GET'])?>
<input type="hidden" name="id" value="<?=$id?>">
<?php

Expand Down Expand Up @@ -195,7 +201,7 @@
?>
<div class="actions">
<button type="submit" class="btn btn-xs btn-primary">Search</button>
<?=anchor(FindReplace::url('results') . '?id=' . $id, 'Reset', 'class="btn btn-xs btn-default"')?>
<?=anchor(Url::to('results') . '?id=' . $id, 'Reset', 'class="btn btn-xs btn-default"')?>
</div>
<?=form_close()?>
</div>
Expand All @@ -204,7 +210,7 @@

echo Helper::loadPagination($pagination);

echo form_open(FindReplace::url('replace'), [
echo form_open(Url::to('replace'), [
'id' => 'find-replace-results',
'data-filtered' => $filtered ? '1' : '0',
])?>
Expand Down
2 changes: 1 addition & 1 deletion assets/css/admin.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading