Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nova Support (search and sorting) #138

Closed
u12206050 opened this issue Mar 26, 2020 · 2 comments
Closed

Nova Support (search and sorting) #138

u12206050 opened this issue Mar 26, 2020 · 2 comments
Labels

Comments

@u12206050
Copy link

In order for translations to work in nova and not break global search and sorting of columns that are translated update your app/Nova/Resource.php by adding the following code which overwrites the search and ordering functions:

protected static function applyTranslation($query, $columns, $callback, $values = null) {
        $base = $query->getModel();
        $baseTable = $base->getTable();
        $translatedTable = false;
        $attrs = $base->translatedAttributes;
        if (!empty($attrs)) {
            $translatedClass = static::$model . "Translation";
            $T = new $translatedClass();
            $translatedTable = $T->getTable();
            $translatedTableId = "$translatedTable." . str_replace('_translations', '_id', $translatedTable);

            $subQuery = DB::table($translatedTable)->select($translatedTableId);
            foreach ($columns as $column) {
                if (in_array($column, $attrs)) {
                    $subQuery->addSelect($column);
                }
            }
            $subQuery->where('locale', '=', app()->getLocale());
            $subQuery->orWhere('locale', '=', config('translatable.fallback_locale'));

            $query->joinSub($subQuery, $translatedTable, function ($join) use ($translatedTable, $translatedTableId, $baseTable) {
                $join->on("$translatedTableId", '=', "$baseTable.id");
            });
        }

        foreach ($columns as $column) {
            if ($translatedTable && in_array($column, $attrs)) {
                $nsColumn = "$translatedTable.$column";
            } else $nsColumn = "$baseTable.$column";

            if (isset($values)) $callback($query, $nsColumn, $values[$column]);
            else $callback($query, $nsColumn);
        }

        return $query;
    }

    /**
     * Apply the search query to the query.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @param  string  $search
     * @return \Illuminate\Database\Eloquent\Builder
     */
    protected static function applySearch($query, $search)
    {
        $connectionType = $query->getModel()->getConnection()->getDriverName();
        $likeOperator = $connectionType == 'pgsql' ? 'ilike' : 'like';

        $searchable = static::searchableColumns();
        if (empty($searchable)) return $query;

        return static::applyTranslation($query, $searchable, function($query, $column) use ($likeOperator, $search) {
            $query->orWhere($column, $likeOperator, '%'.$search.'%');
        });
    }

    /**
     * Apply any applicable orderings to the query.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @param  array  $orderings
     * @return \Illuminate\Database\Eloquent\Builder
     */
    protected static function applyOrderings($query, array $orderings)
    {
        $orderings = array_filter($orderings);

        if (empty($orderings)) {
            return empty($query->getQuery()->orders)
                        ? $query->latest($query->getModel()->getQualifiedKeyName())
                        : $query;
        }

        return static::applyTranslation($query, array_keys($orderings), function($query, $column, $direction) {
            $query->orderBy($column, $direction);
        }, $orderings);
    }
@stale
Copy link

stale bot commented Apr 9, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Apr 9, 2020
@stale
Copy link

stale bot commented May 7, 2020

This issue has been automatically closed because it has not had recent activity. Thank you for your contributions.

@stale stale bot closed this as completed May 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

1 participant