Skip to content

Commit aeb53b0

Browse files
committed
add basic search functionality via friendsofcake/search
1 parent 1aadd43 commit aeb53b0

8 files changed

Lines changed: 99 additions & 8 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"cakephp/cakephp": "5.2.*",
1010
"cakephp/migrations": "^4.0.0",
1111
"cakephp/plugin-installer": "^2.0",
12+
"friendsofcake/search": "^7.5",
1213
"knplabs/packagist-api": "^2.1",
1314
"mobiledetect/mobiledetectlib": "^4.8.03"
1415
},

composer.lock

Lines changed: 60 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/form-templates.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
* Custom templates for pagination elements.
66
*/
77
return [
8-
'button' => '<button class="bg-red-800 p-2 text-white rounded-sm"{{attrs}}>{{text}}</button>',
8+
'button' => '<button class="bg-red-800 hover:cursor-pointer p-2 text-white rounded-sm"{{attrs}}>{{text}}</button>',
99
'checkbox' => '<input class="justify-self-start" type="checkbox" name="{{name}}" value="{{value}}"{{attrs}}>',
1010
'checkboxWrapper' => '<div class="col-span-2 col-start-2 flex gap-2">{{label}}</div>',
1111
'error' => '<div class="col-start-2 col-end-4 text-red-700" id="{{id}}">{{content}}</div>',
12-
'input' => '<input class="col-span-2 border border-slate-500 rounded-sm max-w-md min-w-4 p-1" type="{{type}}" name="{{name}}"{{attrs}}>',
13-
'inputContainer' => '<div class="group py-3 grid grid-cols-3 gap-2">{{content}}</div>',
14-
'inputContainerError' => '<div class="group py-3 grid grid-cols-3 gap-2 text-red-700">{{content}}{{error}}</div>',
12+
'input' => '<input class="col-span-2 border h-full border-slate-500 rounded-sm max-w-md min-w-4 p-2" type="{{type}}" name="{{name}}"{{attrs}}>',
13+
'inputContainer' => '<div class="group gap-2">{{content}}</div>',
14+
'inputContainerError' => '<div class="group gap-2 text-red-700">{{content}}{{error}}</div>',
1515
'label' => "<label class=\"group-has-[input:required]:after:content-['_*'] group-has-[input:required]:after:text-red-700\" {{attrs}}>{{text}}</label>",
1616
// Not actually a nested label.
1717
'nestingLabel' => "{{hidden}}<label class=\"group-has-[input:required]:after:content-['_*'] group-has-[input:required]:after:text-red-700\" {{attrs}}>{{text}}</label>{{input}}",

config/plugins.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@
3333
'Migrations' => ['onlyCli' => true],
3434

3535
// Additional plugins here
36+
'Search',
3637
];

src/Controller/AppController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function initialize(): void
4242
parent::initialize();
4343

4444
$this->loadComponent('Flash');
45+
$this->loadComponent('Search.Search');
4546

4647
/*
4748
* Enable the following component for recommended CakePHP form protection settings.

src/Controller/PackagesController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class PackagesController extends AppController
1717
*/
1818
public function index()
1919
{
20-
$query = $this->Packages->find();
20+
$query = $this->Packages
21+
->find('search', search: $this->request->getQueryParams());
2122
$packages = $this->paginate($query);
2223

2324
$this->set(compact('packages'));

src/Model/Table/PackagesTable.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33

44
namespace App\Model\Table;
55

6-
use Cake\ORM\Query\SelectQuery;
7-
use Cake\ORM\RulesChecker;
86
use Cake\ORM\Table;
97
use Cake\Validation\Validator;
8+
use Search\Manager;
109

1110
/**
1211
* Packages Model
@@ -40,6 +39,8 @@ public function initialize(array $config): void
4039
$this->setTable('packages');
4140
$this->setDisplayField('package');
4241
$this->setPrimaryKey('id');
42+
43+
$this->addBehavior('Search.Search');
4344
}
4445

4546
/**
@@ -85,4 +86,25 @@ public function validationDefault(Validator $validator): Validator
8586

8687
return $validator;
8788
}
89+
90+
/**
91+
* @return \Search\Manager
92+
*/
93+
public function searchManager(): Manager
94+
{
95+
/** @var \Search\Model\Behavior\SearchBehavior $search */
96+
$search = $this->getBehavior('Search');
97+
$searchManager = $search->searchManager();
98+
$searchManager->add('search', 'Search.Like', [
99+
'before' => true,
100+
'after' => true,
101+
'fieldMode' => 'OR',
102+
'comparison' => 'LIKE',
103+
'wildcardAny' => '*',
104+
'wildcardOne' => '?',
105+
'fields' => ['package', 'description'],
106+
]);
107+
108+
return $searchManager;
109+
}
88110
}

templates/Packages/index.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
<div>
99
<div class="flex justify-between items-center my-8">
1010
<h3 class="text-xl"><?= __('Packages') ?></h3>
11+
<?php
12+
echo $this->Form->create(null, ['valueSources' => 'query', 'class' => 'flex gap-4']);
13+
echo $this->Form->control('search', ['label' => false,]);
14+
echo $this->Form->button('Search', ['type' => 'submit']);
15+
echo $this->Form->end();
16+
?>
1117
<div class="flex gap-4">
1218
<p><?= __('Sort by:') ?></p>
1319
<div class="flex gap-4">

0 commit comments

Comments
 (0)