Skip to content

Conversation

slowestwind
Copy link

Description

This PR implements accent-insensitive search functionality to address the issue where users cannot find results when searching without accents (e.g., searching 'simoes' to find 'Simões').

Problem

Users in Portuguese-speaking countries often don't type accents when searching, but expect to find results with accented characters. This is especially common in Brazilian Portuguese where users might search for:

  • 'simoes' instead of 'Simões'
  • 'joao' instead of 'João'
  • 'sao paulo' instead of 'São Paulo'

Solution

Added a new configuration option ignore_accents that when enabled, normalizes both search terms and data to their base characters before comparison.

Changes Made

  1. Configuration: Added 'ignore_accents' => false to the search config in datatables.php
  2. Helper Method: Created Helper::normalizeAccents() to convert accented characters to base characters
  3. Config Method: Added Config::isIgnoreAccents() to check if the feature is enabled
  4. Query DataTable: Updated to use database functions for accent normalization in SQL queries
  5. Collection DataTable: Updated to normalize both search terms and data values
  6. Tests: Added comprehensive unit tests for the normalization function

Supported Characters (Portuguese Brazilian)

  • Ã/ã/Á/á/À/à/Â/â → a
  • É/é/Ê/ê → e
  • Í/í → i
  • Ó/ó/Ô/ô/Õ/õ → o
  • Ú/ú → u
  • Ç/ç → c

Usage

Enable the feature in your config:

'search' => [
    'ignore_accents' => true,
    // other settings...
],

When enabled:

  • Searching for 'simoes' will find 'Simões'
  • Searching for 'joao' will find 'João'
  • Searching for 'sao' will find 'São'

Testing

  • Added unit tests covering all accent mappings
  • Tests verify both individual character replacement and full text normalization
  • Backwards compatible - feature is disabled by default

Database Compatibility

The feature uses appropriate database functions:

  • MySQL: REPLACE() function cascade for each accent mapping
  • PostgreSQL: UNACCENT() function if available, fallback to REPLACE()
  • SQLite: REPLACE() function cascade
  • SQL Server: REPLACE() function cascade

Breaking Changes

None - this is a new optional feature that is disabled by default.

Fixes #3249

- Add 'ignore_accents' config option in search settings
- Implement Helper::normalizeAccents() for Portuguese accent normalization
- Add Config::isIgnoreAccents() method to check configuration
- Update QueryDataTable to handle accent normalization in database queries
- Update CollectionDataTable to handle accent normalization in collection filtering
- Add comprehensive unit tests for accent normalization
- Support for Portuguese Brazilian accents: ã/á/à/â/é/ê/í/ó/ô/õ/ú/ç

This allows users to search for 'simoes' and find 'Simões' when the feature is enabled.

Fixes yajra#3249
- Add comprehensive ACCENT_INSENSITIVE_SEARCH.md documentation
- Remove temporary test file
- Includes usage examples, configuration, and performance considerations
- Add controller examples showing Eloquent, Collection, and Query Builder usage
- Add Blade template example with search instructions
- Add migration example with Portuguese test data
- Add route and configuration examples
- Demonstrates real-world usage scenarios
- Add comprehensive summary of all changes made
- Document the complete implementation approach
- List all modified and added files
- Provide usage instructions and verification steps
- Fix missing class brace syntax error in Helper.php
- Refactor getNormalizeAccentsFunction into smaller methods for better maintainability
- Add proper SQL escaping and error handling
- Fix keyword normalization in CollectionDataTable to avoid modification inside loop
- Add comprehensive documentation and parameter validation
- Preserve case sensitivity in normalizeAccents mapping
- Add defensive programming checks for empty values

These changes address potential security hotspots and improve code reliability.
- Replace match() expression with switch statement for PHP 7.4 compatibility
- Change private methods to protected for better extensibility
- Remove temporary validation files

These changes should resolve CI failures related to PHP version compatibility.
- Break long lines in accent mapping for better readability
- Add comments to organize accent character groups
- Follow PSR-12 line length guidelines

This should resolve any remaining linting issues.
- Replace dynamic SQL generation with static string literals to prevent SQL injection
- Add input validation to prevent processing empty/invalid values
- Add early return optimization for strings without accents
- Remove unsafe addslashes() usage in favor of static SQL strings

These changes should resolve SonarQube security hotspots and reliability concerns.
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
2 Security Hotspots
C Reliability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug when filtering words with accents

1 participant