Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: Julius_2000 on September 29, 2022, 01:00:27 PM

Title: Make unread PMs a link
Post by: Julius_2000 on September 29, 2022, 01:00:27 PM
Hi,

all my attempts to make PMs in the popup window in the pm_menu a link akin to Alerts somehow failed.

In personal;essages.template.php I tried enclosing the <div class="unread_notify"> with an <a></a> elemnt but to no avail. All I'm getting is multiple links in weird spots inside the unread_notify div.

Quote<div class="pm_unread">';

   if (empty($context['unread_pms']))
      echo '
         <div class="no_unread">', $txt['pm_no_unread'], '</div>';
   else
   {
      foreach ($context['unread_pms'] as $id_pm => $pm_details)
         echo '
         <a href="PM url"> <div class="unread_notify">
            <div class="unread_notify_image">
               ', !empty($pm_details['member']) ? $pm_details['member']['avatar']['image'] : '', '
            </div>
            <div class="details">
               <div class="subject">', $pm_details['pm_link'], '</div>
               <div class="sender">
                  ', $pm_details['replied_to_you'] ? '<span class="main_icons replied centericon" style="margin-right: 4px" title="' . $txt['pm_you_were_replied_to'] . '"></span>' : '<span class="main_icons im_off centericon" style="margin-right: 4px" title="' . $txt['pm_was_sent_to_you'] . '"></span>',
                  !empty($pm_details['member']) ? $pm_details['member']['link'] : $pm_details['member_from'], ' - ', $pm_details['time'], '
               </div>
            </div>
         </div></a>';
   }
echo '
     </div><!-- #pm_unread -->';

Could anyone tell me how to do it? I would like to click just on the whole PM div instead of the small subject link, which is rather inconvenient on mobiles.

Any help would be greatly appreciated!
Title: Re: Make unread PMs a link
Post by: Diego Andrés on September 29, 2022, 01:51:59 PM
I suppose that'd be because you can't use divs inside anchors.
Would need to replace them with span
Title: Re: Make unread PMs a link
Post by: Julius_2000 on September 29, 2022, 03:38:40 PM
Quote from: Diego Andrés on September 29, 2022, 01:51:59 PMI suppose that'd be because you can't use divs inside anchors.
Would need to replace them with span

Quote from: Diego Andrés on September 29, 2022, 01:51:59 PMI suppose that'd be because you can't use divs inside anchors.
Would need to replace them with span

Hm, that's weird. In profile.template.php Alerts does exactly that and takes no issue with div inside an achor...

But when I do that in template_pm_popup() the anchor is not set where I placed it. For example:
Quote<div class="pm_unread">';

   if (empty($context['unread_pms']))
      echo '
         <div class="no_unread">', $txt['pm_no_unread'], '</div>';
   else
   {   
            foreach ($context['unread_pms'] as $id_pm => $pm_details)
      
              echo '
         <a><div class="unread_notify"></a>
            <div class="unread_notify_image">
               ', !empty($pm_details['member']) ? $pm_details['member']['avatar']['image'] : '', '
            </div>
            <div class="details">
               <div class="subject">', $pm_details['pm_link'], '</div>
               <div class="sender">
                  ', $pm_details['replied_to_you'] ? '<span class="main_icons replied centericon" style="margin-right: 4px" title="' . $txt['pm_you_were_replied_to'] . '"></span>' : '<span class="main_icons im_off centericon" style="margin-right: 4px" title="' . $txt['pm_was_sent_to_you'] . '"></span>',
                  !empty($pm_details['member']) ? $pm_details['member']['link'] : $pm_details['member_from'], ' - ', $pm_details['time'], '
               </div>
            </div>
         </div>';
   }

Produces this:
pm_menu.jpg

The original Alerts in profile.template.php does it like this:
<div class="alerts_unread">';

if (empty($context['unread_alerts']))
template_alerts_all_read();

else
{
foreach ($context['unread_alerts'] as $id_alert => $details)
{
echo '
<', !$details['show_links'] ? 'a href="' . $scripturl . '?action=profile;area=showalerts;alert=' . $id_alert . '" onclick="this.classList.add(\'alert_read\')"' : 'div', ' class="unread_notify">
<div class="unread_notify_image">
', empty($details['sender']['avatar']['image']) ? '' : $details['sender']['avatar']['image'] . '
', $details['icon'], '
</div>
<div class="details">
<span class="alert_text">', $details['text'], '</span> - <span class="alert_time">', $details['time'], '</span>
</div>
</', !$details['show_links'] ? 'a' : 'div', '>';
}
}

echo '
</div><!-- .alerts_unread -->
Title: Re: Make unread PMs a link
Post by: Diego Andrés on September 29, 2022, 03:42:00 PM
I'm on the phone but make sure you are properly wrapping all the elements including the closing tags.
In that example, the browser will not properly render anything how you want it because you are wrapping only the opening tag.
Title: Re: Make unread PMs a link
Post by: Julius_2000 on September 29, 2022, 04:17:34 PM
Quote from: Diego Andrés on September 29, 2022, 03:42:00 PMI'm on the phone but make sure you are properly wrapping all the elements including the closing tags.
In that example, the browser will not properly render anything how you want it because you are wrapping only the opening tag.

You're right, that specific example was wrong. But when I do this e.g.:

Quote<div class="pm_unread">';

    if (empty($context['unread_pms']))
        echo '
            <div class="no_unread">', $txt['pm_no_unread'], '</div>';
    else
    {
        foreach ($context['unread_pms'] as $id_pm => $pm_details)
       
                echo '
            <a><div class="unread_notify">
                <div class="unread_notify_image">
                    ', !empty($pm_details['member']) ? $pm_details['member']['avatar']['image'] : '', '
                </div>
                <div class="details">
                    <div class="subject">', $pm_details['pm_link'], '</div>
                    <div class="sender">
                        ', $pm_details['replied_to_you'] ? '<span class="main_icons replied centericon" style="margin-right: 4px" title="' . $txt['pm_you_were_replied_to'] . '"></span>' : '<span class="main_icons im_off centericon" style="margin-right: 4px" title="' . $txt['pm_was_sent_to_you'] . '"></span>',
                        !empty($pm_details['member']) ? $pm_details['member']['link'] : $pm_details['member_from'], ' - ', $pm_details['time'], '
                    </div>
                </div>
            </div></a>';
       
    }

    echo '
        </div><!-- #pm_unread -->';
Shouldn't every unread PM now be a link? Instead, the anchor gets placed like this in multiple places:

pm_menu_2.jpg

What am I doing wrong?
Title: Re: Make unread PMs a link
Post by: Diego Andrés on September 30, 2022, 05:02:56 PM
I'll have a peek today once I have some free time.
Title: Re: Make unread PMs a link
Post by: Antechinus on September 30, 2022, 05:19:59 PM
Quote from: Diego Andrés on September 29, 2022, 01:51:59 PMI suppose that'd be because you can't use divs inside anchors.
That restriction was removed in HTML5, IIRC. :)
Title: Re: Make unread PMs a link
Post by: Julius_2000 on October 01, 2022, 04:27:07 PM
Quote from: Diego Andrés on September 30, 2022, 05:02:56 PMI'll have a peek today once I have some free time.
Oh that would be great, thank you! Is there a reason it wasn't done this way in the first place, like Alerts?
Title: Re: Make unread PMs a link
Post by: Diego Andrés on October 01, 2022, 05:33:47 PM
Quote from: Julius_2000 on October 01, 2022, 04:27:07 PM
Quote from: Diego Andrés on September 30, 2022, 05:02:56 PMI'll have a peek today once I have some free time.
Oh that would be great, thank you! Is there a reason it wasn't done this way in the first place, like Alerts?

It's very simple actually, alerts were added later  :laugh:

Regarding this topic.
The issue is also quite simple. What you are doing is correct, but you are forgetting that the pm's already have links inside the content, and you definitely cannot wrap anchors inside each other.
You'll need to replace the links with regular text.

$pm_details['pm_link'] ---> $pm_details['subject']
$pm_details['member']['link'] ---> $pm_details['member']['name']

I'm unsure if I'm missing something else...
Title: Re: Make unread PMs a link
Post by: Julius_2000 on October 02, 2022, 07:31:16 AM
Muchas gracias, Diego! That sort of worked. ['subject'] is not suffcient though because it doesn't output anything. Adding ['name']  doesn't work.

The only thing now would be to know how $pm_details for $pm_details['pm_link'] and $pm_details['pm_subject'] is coded so I can set the href for the anchor and fetch the name of the PM subject. Because currently, this variable puts out the entire closed link for each post.

This is what I found in ..Sources/PersonalMessages.template.php

$row['pm_link'] = '<a href="' . $scripturl . '?action=pm;f=inbox;pmsg=' . $row['id_pm'] . '">

I can't find a similar line in the the ..Themes/PersonalMessages.template.php So I guess I would have to adapt the variable for pmsg.
Title: Re: Make unread PMs a link
Post by: Julius_2000 on October 02, 2022, 08:38:13 AM
Correction, $pm_details['subject']  does work.

Edit:
I think I figured it out:
<a href="' . $scripturl . '?action=pm;f=inbox;msg=' . $pm_details['id_pm'] . '">

This seems to work:
{
foreach ($context['unread_pms'] as $id_pm => $pm_details)
echo '<a href="' . $scripturl . '?action=pm;f=inbox;msg=' . $pm_details['id_pm'] . '">
<div class="unread_notify">
<div class="unread_notify_image">
', !empty($pm_details['member']) ? $pm_details['member']['avatar']['image'] : '', '
</div>
<div class="details">
<div class="subject">',  $pm_details['subject'] , '</div>
<div class="sender">
', $pm_details['replied_to_you'] ? '<span class="main_icons replied centericon" style="margin-right: 4px" title="' . $txt['pm_you_were_replied_to'] . '"></span>' : '<span class="main_icons im_off centericon" style="margin-right: 4px" title="' . $txt['pm_was_sent_to_you'] . '"></span>',
!empty($pm_details['member']) ? $pm_details['member']['name'] : $pm_details['member_from'], ' - ', $pm_details['time'], '
</div>
</div>
</div></a>';
}
Title: Re: Make unread PMs a link
Post by: Julius_2000 on October 02, 2022, 09:49:06 AM
Sorry for the triple post:
Somehow the anchor definiton above seemed a bid buggy since it seemed that it jumped only to the first message I clicked but then kept jumping to that first clicked PM when clicked on other unread PMs.
This anchor seems working, though:
<a href="' . $scripturl . '?action=pm;pmid='.$pm_details['id_pm'].';kstart;f=inbox;start=0;sort=date;desc#msg=' . $pm_details['id_pm'] . '">

Is there a way to also make the link jump to the PM content like when I click on a PM in inbox?