2

Before anything, I must say this code works as well for the English language, but in Persian it never works.

I use this code on model/celebrities.php. This code shows my frontend model:

class AvisamoviedbModelCelebrities extends ListModel
{

    protected function populateState($ordering = null, $direction = null)
    {
        $app     = Factory::getApplication('site');
        $params = $app->getParams();
        $this->setState('list.start', $app->input->get('limitstart',0,'unit'));
        $limit  = $params->get('celebrities_limit');
        $this->setState('list.limit', $limit);
    }
    /**
     * Method to get a JDatabaseQuery object for retrieving the data set from a database.
     *
     * @return  JDatabaseQuery   A JDatabaseQuery object to retrieve the data set.
     *
     * @since   1.0.0
     */
    public function getListQuery($overrideLimits = false)
    {
        if (Factory::getApplication()->isClient('site')) {
            // Get Params
            $app             = Factory::getApplication();
            $params           = $app->getMenu()->getActive()->getParams(); // get the active item
            $order_by         = $params->get('order_by', '');
            $celebrity_type = $params->get('celebrities_type', '');
            $gender         = $params->get('gender', '');
        }

        $inputs = Factory::getApplication()->input;
        $alphaindex = $inputs->get('alphaindex', '', 'WORD');

        //for celebrities per page limit
        $cparams = ComponentHelper::getParams('com_avisamoviedb');
        $start     = $app->input->get('start', 0, 'INT');
        $limit   = $cparams->get('celebrities_limit');

        $db = $this->getDbo();
        $query = $db->getQuery(true);


        $query->select(array('a.*'));
        $query->from($db->quoteName('#__avisamoviedb_celebrities', 'a'));

        if (Factory::getApplication()->isClient('site')) {

            if ($alphaindex) {
                $query->where($db->quoteName('a.title') . " LIKE " . $db->quote(($alphaindex) . '%'));
            }

            $query->where($db->quoteName('a.published') . " = " . $db->quote('1'));
            $query->setLimit($limit, $start);

            if ($order_by == 'desc') {
                $query->order($db->quoteName('a.created') . ' DESC');
            } elseif ($order_by == 'asc') {
                $query->order($db->quoteName('a.created') . ' ASC');
            } elseif ($order_by == 'featured') {
                $query->where($db->quoteName('a.featured') . ' = 1');
                $query->order($db->quoteName('a.ordering') . ' DESC');
            } else {
                $query->order($db->quoteName('a.ordering') . ' DESC');
            }

            if ($celebrity_type == 'actors') {
                $query->where('a.celebrity_type in (' . $db->quote('actor') . ',' . $db->quote('both') . ')');
            } elseif ($celebrity_type == 'directors') {
                $query->where('a.celebrity_type in (' . $db->quote('director') . ',' . $db->quote('both') . ')');
            }

            if ($gender == 'male') {
                $query->where($db->quoteName('a.gender') . ' = ' . $db->quote('male'));
            } elseif ($gender == 'female') {
                $query->where($db->quoteName('a.gender') . ' = ' . $db->quote('female'));
            } elseif ($gender == 'others') {
                $query->where($db->quoteName('a.gender') . ' = ' . $db->quote('others'));
            }

            //Language
            $query->where('a.language in (' . $db->quote(Factory::getLanguage()->getTag()) . ',' . $db->quote('*') . ')');
            //Access
            $query->where($db->quoteName('a.access') . " IN (" . implode(',', Factory::getUser()->getAuthorisedViewLevels()) . ")");
        } else {
            $query->order($db->quoteName('a.ordering') . ' DESC');
        }

        return $query;
    }

    /**
     * get total celebrity
     *
     * @return mixed
     */
    public static function getCountCelebrities()
    {
        $db = Factory::getDbo();
        $query = $db->getQuery(true);
        $query->select(array('COUNT(a.id) as total_celebrities'));
        $query->from($db->quoteName('#__avisamoviedb_celebrities', 'a'));
        $query->where($db->quoteName('a.published') . " = " . $db->quote('1'));
        $db->setQuery($query);
        return $db->loadObject();
    }
}

And this code to site/view/celebrities/view.html.php:

class AvisamoviedbViewCelebrities extends HtmlView
{

    protected $items;
    protected $pagination;

    /**
     * Display the view
     *
     * @param   string    The name of the template file to parse; automatically searches through the template paths.
     *
     * @return  mixed  A string if successful, otherwise an Error object.
     */
    public function display($tpl = null)
    {
        $model = $this->getModel();
        $this->pagination  = $model->getPagination();
        $this->items = $model->getItems();

        $params = ComponentHelper::getParams('com_avisamoviedb');
        $this->columns = $params->get('celebrities_columns', 4);

        // Get model
        $this->total_celebrities = $model->getCountCelebrities()->total_celebrities ?? 0;
        $this->alphabets = ['ا', 'ب', 'پ', 'ت', 'ث', 'ج', 'چ', 'ح', 'خ', 'د', 'ذ', 'ر', 'ز', 'ژ', 'س', 'ش', 'ص', 'ض', 'ط', 'ظ', 'ع', 'غ', 'ف', 'ق', 'ک', 'گ', 'ل', 'م', 'ن', 'و', 'ه', 'ی'];

        foreach ($this->items as &$this->item) {
            $this->item->url = Route::_('index.php?option=com_avisamoviedb&view=celebrity&id=' . $this->item->id . ':' . $this->item->alias . AvisamoviedbHelper::getItemid('celebrities'));
        }

        return parent::display($tpl = null);
    }
}

And this one to site/view/celebrities/tmpl/default.php:

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Uri\Uri;

$input = Factory::getApplication()->input;
$Itemid = $input->get('Itemid', 0, 'INT');
$alphaindex = $input->get('alphaindex', 'all', 'WORD');

$allAlphaUrl = 'index.php?option=com_avisamoviedb&view=celebrities&Itemid=' . $Itemid;

if ($alphaindex) {
    $alphaUrl = 'index.php?option=com_avisamoviedb&view=celebrities&alphaindex=' . $alphaindex . '&Itemid=' . $Itemid;
} else {
    $alphaUrl = 'index.php?option=com_avisamoviedb&view=celebrities&Itemid=' . $Itemid;
}

?>

<div id="com-avisa-moviedb" class="avisamoviedb avisa-moviedb avisa-moviedb-view-celebrities">

    <div class="moviedb-filters">
        <div class="pull-left">
            <ul>
                <li class="<?php echo ($alphaindex == 'all') ? 'active' : ''; ?>">
                    <a href="<?php echo Route::_($allAlphaUrl); ?>"><?php echo Text::_('COM_AVISAMOVIEDB_ALL'); ?></a>
                </li>
                <?php foreach ($this->alphabets as $alphabet) { ?>
                    <li class="<?php echo ($alphaindex == $alphabet) ? 'active' : ''; ?>">
                        <a href="<?php echo Route::_($alphaUrl . '&alphaindex=' . $alphabet); ?>">
                            <?php echo $alphabet; ?>
                        </a>
                    </li>
                <?php } ?>
            </ul>
        </div>
        <div class="pull-right">
            <?php echo Text::_('COM_AVISAMOVIEDB_TOTAL'); ?>:
            <strong><?php echo $this->total_celebrities; ?></strong>
        </div>
    </div>

    <?php if (!count($this->items)) { ?>
        <div class="alert alert-warning">
            <?php echo Text::_('COM_AVISAMOVIEDB_NOTHING_FOUND'); ?>
        </div>
    <?php } ?>

    <?php foreach (array_chunk($this->items, $this->columns) as $this->items) { ?>
        <div class="avisamoviedb-row">
            <?php foreach ($this->items as $celebrity) { ?>
                <div class="item avisamoviedb-col-sm-<?php echo round(12 / $this->columns); ?>">
                    <div class="celebritie-poster">
                        <img src="<?php echo Uri::root() . $celebrity->profile_image; ?>" alt="<?php echo $celebrity->title; ?>">
                        <a href="<?php echo $celebrity->url; ?>" class="play-icon"><i class="avisamoviedb-icon-celebrities"></i></a>
                    </div>
                    <div class="celebritie-details">
                        <div class="celebritie-name">
                            <a href="<?php echo $celebrity->url; ?>">
                                <h4 class="celebritie-title"><?php echo $celebrity->title; ?></h4>
                            </a>
                            <span><?php echo $celebrity->designation; ?></span>
                        </div>
                    </div>
                </div>
            <?php } ?>
        </div>
    <?php } ?>

    <?php
        if ($this->pagination->pagesTotal > 1) { ?>
        <?php echo $this->pagination->getPagesLinks(); ?>
    <?php } ?>

</div>

But this code doesn’t filter my celebrities in my site. What’s wrong?

Check example

Here you can see I choose 'ص', but I can see all items. All these items must hide... And if I choose the right alphabet, these items must show. This code must work as some filter.

1 Answer 1

4

Per documentation, WORD filter does not allow unicode characters:

WORD: A string containing A-Z or underscores only (not case sensitive)

https://api.joomla.org/framework-2/classes/Joomla-Filter-InputFilter.html

You may need to get unfiltered input using RAW filter and filter it yourself using a suitable regular expression. Since you already have a list of allowed values, you can use in_array() instead to determine whether input value is valid:

$characters = ['ا', 'ب', 'پ', 'ت', 'ث', 'ج', 'چ', 'ح', 'خ', 'د', 'ذ', 'ر', 'ز', 'ژ', 'س', 'ش', 'ص', 'ض', 'ط', 'ظ', 'ع', 'غ', 'ف', 'ق', 'ک', 'گ', 'ل', 'م', 'ن', 'و', 'ه', 'ی'];
$alphaindex = $inputs->get('alphaindex', '', 'RAW');

if (in_array($alphaindex, $characters, true))
{
    $query->where($db->quoteName('a.title') . " LIKE " . $db->quote(($alphaindex) . '%'));
}
3
  • ...and this is why we should always ask Joomla question on JSE instead of Stack Overflow. Oct 9 at 8:22
  • @mickmackusa u ask me ask here and i have not permision delete my question from SOF ... Oct 10 at 6:56
  • The SO moderators have appropriately deleted the cross-posted question. Your permission is not required for such Stack Exchange Network housekeeping. You expect this kind of outcome if you cross-post in the future, @Ali. Oct 10 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.