Skip to content

Instantly share code, notes, and snippets.

@dhensby
Created December 13, 2017 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhensby/484deddd3db8d4e116e23a5f268d0ee7 to your computer and use it in GitHub Desktop.
Save dhensby/484deddd3db8d4e116e23a5f268d0ee7 to your computer and use it in GitHub Desktop.
SPIKE Find and replace for SilverStripe
<?php
use SilverStripe\Admin\LeftAndMain;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse_Exception;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\TextField;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\View\ArrayData;
use SilverStripe\View\Parsers\Diff;
use SilverStripe\View\Requirements;
use SilverStripe\View\SSViewer;
class SimpsonsAdmin extends LeftAndMain
{
private static $menu_title = 'Simpsons';
private static $url_segment = 'springidled';
private static $allowed_actions = [
'SearchForm',
'accept',
];
protected function init()
{
parent::init();
Requirements::css('silverstripe/cms: client/dist/styles/bundle.css');
}
/**
* @return Form
*/
public function SearchForm()
{
return Form::create(
$this,
__FUNCTION__,
FieldList::create(
TextField::create('q', 'Find'),
TextField::create('r', 'Replace')
),
FieldList::create(
FormAction::create('', 'Go!')
)
)->setFormMethod('GET')->setFormAction($this->Link(''))
->loadDataFrom($this->getRequest()->getVars());
}
/**
* @param array $data
* @param Form $form
* @param HTTPRequest $request
*/
public function doSearch($data, $form, $request)
{
$this->getResponse()->addHeader('Content-Type', 'text/plain');
$data = $form->getData();
//find all records with search term
$list = SiteTree::get();
$filter = [];
foreach (['Title', 'Content'] as $column) {
$filter[$column . ':PartialMatch'] = $data['q'];
}
$list = $list->filterAny($filter);
$this->getResponse()->setBody(var_export($list->toArray(), true));
return $this->getResponse();
}
protected function getPagesByMatch($query)
{
$list = SiteTree::get();
$filter = [];
foreach (['Title', 'Content'] as $column) {
$filter[$column . ':PartialMatch'] = $query;
}
return $list->filterAny($filter);
}
public function ListView()
{
$data = $this->getRequest()->getVars();
if (empty($data['q'])) {
return;
}
//find all records with search term
$list = $this->getPagesByMatch($data['q']);
$results = [];
foreach ($list as $result) {
$results[] = ArrayData::create([
'Title' => DBField::create_field('HTMLFragment', Diff::compareHTML($result->Title, str_ireplace($data['q'], $data['r'], $result->Title))),
'Content' => DBField::create_field('HTMLFragment', Diff::compareHTML($result->Content, str_ireplace($data['q'], $data['r'], $result->Content))),
'Link' => static::join_links($this->Link('accept/' . $result->ID), '?' . http_build_query([
'q' => $data['q'],
'r' => $data['r'],
])),
'CMSEditLink' => $result->CMSEditLink(),
]);
}
$response = SSViewer::execute_template('SimpsonsAdmin_Results', ArrayData::create(['Results' => ArrayList::create($results)]));
return $response;
}
/**
* @param HTTPRequest $request
* @throws HTTPResponse_Exception
*/
public function accept($request)
{
$id = $request->latestParam('ID');
$query = $request->getVar('q');
$replace = $request->getVar('r');
if (!$id || !($page = SiteTree::get()->byID($id))) {
throw new HTTPResponse_Exception('', 404);
}
foreach (['Title', 'Content'] as $field) {
$page->$field = str_ireplace($query, $replace, $page->$field);
}
$page->write();
return $this->redirectBack();
}
}
<div class="cms-content flexbox-area-grow fill-width fill-height $BaseCSSClasses" data-layout-type="border" data-pjax-fragment="Content">
<div class="cms-content-tools fill-height cms-panel cms-panel-layout">
<div class="flexbox-area-grow panel--scrollable cms-panel-content">
$SearchForm
</div>
</div>
<div class="fill-height flexbox-area-grow CMSPageHistoryController panel panel--padded panel--scrollable flexbox-area-grow cms-content-fields">
$ListView
</div>
</div>
<h1>Results</h1>
<% if not $Results %>
<h2>Sorry, there are no results for your search</h2>
<% else %>
<table>
<thead>
<tr>
<th>Title</th>
<th>Content</th>
<th></th>
</tr>
</thead>
<tbody>
<% loop $Results %>
<tr>
<td>$Title</td>
<td>$Content</td>
<td><a href="$Link">Accept</a> / <a href="$CMSEditLink">Amend</a></td>
</tr>
<% end_loop %>
</tbody>
</table>
<% end_if %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment