Skip to content

Commit

Permalink
Merge branch 'master' into cake-5
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Oct 11, 2023
2 parents 31b08ca + 28f1bcd commit ef8d52f
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Build Status](https://img.shields.io/github/workflow/status/FriendsOfCake/crud-view/CI/master?style=flat-square)](https://github.com/FriendsOfCake/crud-view/actions?query=workflow%3ACI+branch%3Amaster)
[![Build Status](https://img.shields.io/github/actions/workflow/status/FriendsOfCake/crud-view/ci.yml?branch=master&style=flat-square)](https://github.com/FriendsOfCake/crud-view/actions?query=workflow%3ACI+branch%3Amaster)
[![Total Downloads](https://img.shields.io/packagist/dt/FriendsOfCake/crud-view.svg?style=flat-square)](https://packagist.org/packages/FriendsOfCake/crud-view)
[![Latest Stable Version](https://img.shields.io/packagist/v/friendsofcake/crud-view.svg?style=flat-square)](https://packagist.org/packages/FriendsOfCake/crud-view)
[![Documentation Status](https://readthedocs.org/projects/crud-view/badge/?version=latest&style=flat-square)](https://readthedocs.org/projects/crud-view/?badge=latest)
Expand Down
8 changes: 4 additions & 4 deletions docs/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
alabaster==0.7.12
Babel==2.9.1
cakephp-theme==1.1.3
certifi==2022.12.7
certifi==2023.7.22
chardet==3.0.4
docutils==0.15.2
idna==2.8
imagesize==1.1.0
Jinja2==2.11.3
MarkupSafe==1.1.1
packaging==19.2
Pygments==2.7.4
Pygments==2.15.0
pyparsing==2.4.5
pytz==2019.3
requests==2.22.0
requests==2.31.0
six==1.13.0
snowballstemmer==2.0.0
Sphinx==2.2.1
Expand All @@ -24,4 +24,4 @@ sphinxcontrib-jsmath==1.0.1
sphinxcontrib-phpdomain==0.6.2
sphinxcontrib-qthelp==1.0.2
sphinxcontrib-serializinghtml==1.1.3
urllib3==1.26.5
urllib3==1.26.17
2 changes: 1 addition & 1 deletion src/Listener/ViewListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function beforePaginate(EventInterface $event): void
}

if (!$event->getSubject()->query->getContain()) {
$event->getSubject()->query->contain($this->_getRelatedModels(['manyToOne', 'oneToOne']));
$event->getSubject()->query->contain($this->_getRelatedModels(['belongsTo', 'hasOne']));
}
}

Expand Down
23 changes: 16 additions & 7 deletions src/Listener/ViewSearchListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ public function fields(): array
$input['type'] = 'select';
}

/** @psalm-suppress PossiblyUndefinedArrayOffset */
if ($input['type'] === 'select') {
$input += ['empty' => true];
}

if (!empty($input['options'])) {
$input['empty'] ??= $this->getPlaceholder($field);
if (empty($input['class']) && !$config['select2']) {
Expand All @@ -139,6 +144,7 @@ public function fields(): array
$input['class'] = 'autocomplete';
}

/** @psalm-suppress PossiblyUndefinedArrayOffset */
if (
!empty($input['class'])
&& strpos($input['class'], 'autocomplete') !== false
Expand Down Expand Up @@ -166,17 +172,20 @@ public function fields(): array
}

$urlArgs = [];

$fieldKeys = $input['fields'] ?? ['id' => $field, 'value' => $field];
if (is_array($fieldKeys)) {
foreach ($fieldKeys as $key => $val) {
$urlArgs[$key] = $val;
if (!isset($input['data-url'])) {
$urlArgs = [];

$fieldKeys = $input['fields'] ?? ['id' => $field, 'value' => $field];
if (is_array($fieldKeys)) {
foreach ($fieldKeys as $key => $val) {
$urlArgs[$key] = $val;
}
}

$input['data-url'] = Router::url(['action' => 'lookup', '_ext' => 'json', '?' => $urlArgs]);
}

unset($input['fields']);
$url = array_merge(['action' => 'lookup', '_ext' => 'json'], ['?' => $urlArgs]);
$input['data-url'] = Router::url($url);

$fields[$field] = $input;
}
Expand Down
6 changes: 3 additions & 3 deletions src/View/Helper/CrudViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

namespace CrudView\View\Helper;

use Cake\Database\Schema\TableSchemaInterface;
use Cake\Datasource\EntityInterface;
use Cake\Datasource\SchemaInterface;
use Cake\Utility\Inflector;
use Cake\Utility\Text;
use Cake\View\Helper;
Expand Down Expand Up @@ -381,9 +381,9 @@ public function currentModel(): string
/**
* Get model schema.
*
* @return \Cake\Database\Schema\TableSchemaInterface
* @return \Cake\Datasource\SchemaInterface
*/
public function schema(): TableSchemaInterface
public function schema(): SchemaInterface
{
return $this->getViewVar('modelSchema');
}
Expand Down
1 change: 1 addition & 0 deletions src/View/Widget/DateTimeWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public function render(array $data, ContextInterface $context): string

$clearIcon = $this->clearIcon;
$toggleIcon = $this->calendarIcon;
/** @psalm-suppress PossiblyUndefinedArrayOffset */
if (isset($datetimePicker['iconClass'])) {
$toggleIcon = $datetimePicker['iconClass'];
unset($datetimePicker['iconClass']);
Expand Down
26 changes: 26 additions & 0 deletions tests/Fixture/BlogsFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);

namespace CrudView\Test\Fixture;

use Cake\TestSuite\Fixture\TestFixture;

class BlogsFixture extends TestFixture
{
public $fields = [
'id' => ['type' => 'integer'],
'is_active' => ['type' => 'boolean', 'default' => true, 'null' => false],
'name' => ['type' => 'string', 'length' => 255, 'null' => false],
'body' => ['type' => 'text', 'null' => false],
'user_id' => ['type' => 'integer'],
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
];

public $records = [
['name' => '1st post', 'body' => '1st post body', 'user_id' => 1],
['name' => '2nd post', 'body' => '2nd post body', 'user_id' => 1],
['name' => '3rd post', 'body' => '3rd post body', 'user_id' => 1],
['name' => '4th post', 'body' => '4th post body', 'user_id' => 1],
['name' => '5th post', 'body' => '5th post body', 'user_id' => 1],
];
}
105 changes: 105 additions & 0 deletions tests/TestCase/Listener/ViewSearchListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
declare(strict_types=1);

namespace CrudView\Test\TestCase\Listener;

use Cake\Controller\Controller;
use Cake\Http\ServerRequest;
use Cake\Routing\Route\DashedRoute;
use Cake\Routing\Router;
use Cake\TestSuite\TestCase;
use CrudView\Listener\ViewSearchListener;

/**
* Test case for ViewSearchListener.
*/
class ViewSearchListenerTest extends TestCase
{
protected $fixtures = ['plugin.CrudView.Blogs'];

/**
* @var \Cake\Controller\Controller;
*/
protected $controller;

/**
* @var \CrudView\Listener\ViewSearchListener
*/
protected $listener;

public function setUp(): void
{
$routesBuilder = Router::createRouteBuilder('/');
$routesBuilder->setRouteClass(DashedRoute::class);
$routesBuilder->connect('/{controller}/{action}/*', [])
->setExtensions(['json']);

$request = new ServerRequest([
'url' => '/blogs/index',
'params' => ['controller' => 'Blogs', 'action' => 'index', 'plugin' => null, '_ext' => null],
]);

$this->controller = new Controller($request, null, 'Blogs');

$this->listener = new ViewSearchListener($this->controller);

Router::setRequest($request);
}

public function testFields()
{
$this->listener->setConfig(['fields' => [
'name',
'is_active',
'user_id',
'custom_select' => ['empty' => false, 'type' => 'select'],
]]);

$fields = $this->listener->fields();
$expected = [
'name' => [
'required' => false,
'type' => 'select',
'value' => null,
'class' => 'autocomplete',
'data-url' => '/blogs/lookup.json?id=name&value=name',
'data-input-type' => 'text',
'data-tags' => 'true',
'data-allow-clear' => 'true',
'data-placeholder' => '',
],
'is_active' => [
'required' => false,
'type' => 'select',
'value' => null,
'empty' => true,
'options' => ['No', 'Yes'],
],
'user_id' => [
'required' => false,
'type' => 'select',
'empty' => true,
'value' => null,
'class' => 'autocomplete',
'data-url' => '/blogs/lookup.json?id=user_id&value=user_id',
],
'custom_select' => [
'required' => false,
'type' => 'select',
'empty' => false,
'value' => null,
'class' => 'autocomplete',
'data-url' => '/blogs/lookup.json?id=custom_select&value=custom_select',
],
];
$this->assertEquals($expected, $fields);

$this->listener->setConfig([
'fields' => ['name' => ['data-url' => '/custom']],
], null, true);

$fields = $this->listener->fields();
$expected['name']['data-url'] = '/custom';
$this->assertEquals($expected, $fields);
}
}

0 comments on commit ef8d52f

Please sign in to comment.