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

  • 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). – mickmackusa May 2 at 22:22
  • ...something like this joomla.stackexchange.com/a/24931/12352 but without all of the noisy debugging code. – mickmackusa May 2 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. – mickmackusa May 4 at 10:59
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.

| improve this answer | |
  • Good idea but it doesn't work. Perhaps this error window does not support formatted text – Midhat Apr 30 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. – Robbie Jackson Apr 30 at 19:42
  • I was using Super User too. – Midhat May 1 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. – Robbie Jackson May 1 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? – Robbie Jackson May 3 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>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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