1

I did a modification of one component for a contact, where the name of the file being sent as attachment is compared to the names in the database. Only if the name matches it is allowed to send. If it does not match, an error message should be displayed. The code I added is last 15 lines:

public function exec($upload_directory, $item)
    {
        if (!is_writable($upload_directory))
        {
            return array('error' => JText::_('COM_FOXCONTACT_ERR_DIR_NOT_WRITABLE'));
        }

        $size = $this->getFileSize();
        if ($size == 0)
        {
            return array('error' => JText::_('COM_FOXCONTACT_ERR_FILE_EMPTY'));
        }

        if ($size > constant($item->get('file.size', 'MB100')))
        {
            return array('error' => JText::_('COM_FOXCONTACT_ERR_FILE_TOO_LARGE'));
        }

        $realname = $this->getFileName();

        // My code
        $realname = Normalizer::normalize($realname);
        setlocale(LC_ALL,'bs_BA.UTF-8'); 
        $onlyname = pathinfo($realname, PATHINFO_FILENAME); 

        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select('eio3k_peaks.peak_name');
        $query->from($db->quoteName('#__peaks'));
        $db->setQuery($query);
        $peaks_list = $db->loadColumn();

        if (!in_array($onlyname, $peaks_list))
        {
            return array('error' => JText::_('Name of file <strong>' .$onlyname. ' <strong> is not correct. Pay attention to uppercase/lowercase, spaces, and special characters. More info at this link: https://planinarenje.ba'));
        }
        // End of my code
        ...

I'm trying to make variable $onlyname bold, and link clickable. I tried to make the message as a language constant and as a code, but it does not work.

This is updated code from @Robbie Jackson but with this code get empty message:

// My code
        $realname = Normalizer::normalize($realname);
        setlocale(LC_ALL,'bs_BA.UTF-8'); 
        $onlyname = pathinfo($realname, PATHINFO_FILENAME); 

        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select('eio3k_peaks.peak_name');
        $query->from($db->quoteName('#__peaks'));
        $db->setQuery($query);
        $peaks_list = $db->loadColumn();

        if (!in_array($onlyname, $peaks_list))
    {
        JFactory::getApplication()->enqueueMessage(JText::_('Name of file <strong>' .$onlyname. ' </strong> is not correct', 'error')); return array('error' => ' ');
    }
        // End of my code
        ...

On this link you can see printscreen with old and new code https://emreza.org/a.jpg

3
  • 1
    Perhaps I am misunderstanding your code or there is some relevant code missing, but it appears that you are validating the incoming filename by extracting ALL peak names from the db, then checking for one peakname in the result set. Unless you need all of the peak names to be returned, this should be optimised. You should write a WHERE clause in your query to search for the exact name and return the COUNT(1) and use loadResult() to only generate a single-value result set. Then you only need to check for zero or one (in other words a truthy|falsey result, no in_array() call). Commented May 2, 2020 at 22:22
  • ...something like this joomla.stackexchange.com/a/24931/12352 but without all of the noisy debugging code. Commented May 2, 2020 at 22:25
  • 1
    Please specify what you mean by "does not work". When asking for debugging support, please include some diagnostic details like printing relevant variable to screen to make sure that they contain the expected data. Commented May 4, 2020 at 10:59

2 Answers 2

1

Joomla language overrides seem to accept html ok, so just add the markup into your override.

Language overrides are described at https://docs.joomla.org/J3.x:Language_Overrides_in_Joomla

Putting text like

Your file is <strong>too large</strong>.  
Please see <a href="https://docs.joomla.org/J3.x:Language_Overrides_in_Joomla">here</a>

worked for me.

12
  • Good idea but it doesn't work. Perhaps this error window does not support formatted text
    – Midhat
    Commented Apr 30, 2020 at 10:34
  • @Midhat can you check which user account you were setting these up with? (I was using Super User and it worked ok). If you look in Global Configuration / Text Filters you see the sort of filtering of html tags which are applied to different user groups. If you enter the text then what you see in that box after you hit save is the filtered text. Do you still get the tags removed if you use Super User and you have the global config setting at No Filtering for Super Users? I think these filter settings apply here. Commented Apr 30, 2020 at 19:42
  • I was using Super User too.
    – Midhat
    Commented May 1, 2020 at 19:56
  • @Midhat could you add into your question exactly what HTML you want to appear, and therefore what you were entering into the override text field? Also what text then appeared in the field when you saved the override. (ie if some html tags were filtered out). Thanks. I presume you checked the Global Configuration / Text Filters tab to confirm that the Super User setting was No Filtering, yes? I've definitely got it working on my instance, so it's got to be doable on yours too. Commented May 1, 2020 at 21:33
  • 1
    That's really odd. Could you update your code in the question with what you've now got when you included those 2 lines? Commented May 3, 2020 at 18:19
0

Is that what you need?

return array('error' => "Name of file <strong><a href='your_link'>" .$onlyname. "</a> </strong> is not correct. Pay attention to uppercase/lowercase, spaces, and special characters. More info at this link: https://planinarenje.ba");

And if you want to use language constant:

return array('error' => "Name of file ".JText::_('NAMEONLY')." is not correct. Pay attention to uppercase/lowercase, spaces, and special characters. More info at this link: https://planinarenje.ba");

Your contant language:

NAMEONLY="<strong><a href='your_link'>name </a></strong>"

or

array('error' => JText::_("Name of file <strong><a href='your_link'>".$onlyname."</a></strong> is not correct. Pay attention to uppercase/lowercase, spaces, and special characters. More info at this link: https://planinarenje.ba") );

You can't display links on Javascript alerts, instead you can show the results on a div.

You can do in your server:

echo  "<div id='result'>Name of file <strong><a href='your_link'>" .$onlyname. "</a> </strong> is not correct. Pay attention to uppercase/lowercase, spaces, and special characters. More info at this link: https://planinarenje.ba')<div>";

And the page you are trying to show the response on:

success ajax function:

 success: function (data){
            var result= $(data).find("#result").text();
            $("#error").empty().append(result);

         }

Insert this where you want the error to be displayed.

 <div id="error"></div>
11
  • I tried this and it doesn't work. This is the result: &lt;b&gt;&lt;a href="page.com"&gt;MyMessage&lt;/a&gt;&lt;/…;
    – Midhat
    Commented Apr 28, 2020 at 13:28
  • What's the link are you using? Commented Apr 28, 2020 at 13:28
  • print_r( array('error' => '<b><a href="your-link">'. JText::_("YOUR-LANGUAGE-CONSTANT").'</a></b>')); do it and see the result. Commented Apr 28, 2020 at 13:35
  • 1
    thanks for trying
    – Midhat
    Commented Apr 28, 2020 at 13:56
  • 1
    You are welcome. Good luck. Commented Apr 28, 2020 at 13:56

Your Answer

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

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