Alerts

Started by mickjav, November 22, 2024, 03:57:42 PM

Previous topic - Next topic

mickjav

Hi, Have been trying to get the alerts I set for my mod to have a title but not having any luck

I've tried to get what I needed from profile view PHP, but i haven't been able to work it out.

I'm using the following code for the hook

function music_fetch_alerts(&$alerts, &$formats)
{
global $scripturl, $settings;

foreach ($alerts as $alert_id => $alert)
{
if ($alert['content_type'] == 'likefans')
{
$alerts[$alert_id]['icon'] = '<span class="alert_icon main_icons like"></span>';
$alerts[$alert_id]['extra']['content_link'] = $scripturl . $alert['extra']['fans_link'];
$alerts[$alert_id]['extra']['content_subject'] = $alert['extra']['subject'];
}
}
}

This doesn't create any errors

The function to create alerts::

Code () Select
function FansSendLikeAlert($topic, $artist, $message)
{
global $smcFunc, $sourcedir, $user_info;

$id = (int) (isset($_REQUEST['id']) ? $_REQUEST['id'] : 0);

if(empty($id))
fatal_lang_error('Music_fan_error_no_topic', false);

//We need the owner of the Donation to give the points to
$results = $smcFunc['db_query']('', '
SELECT m.id_member
FROM {db_prefix}fan_messages AS m
WHERE m.msg_id = {int:this_message}',
[
'this_message' => $id
]
);

list ($owner) = $smcFunc['db_fetch_row']($results);
$smcFunc['db_free_result']($results);

require_once($sourcedir . '/Subs-Notify.php');

$prefs = getNotifyPrefs($owner, 'fans_liked', true);

if (empty($prefs[$owner]['fans_liked']))
{
return true;
}

$request = $smcFunc['db_query']('', '
SELECT id_alert
FROM {db_prefix}user_alerts
WHERE id_member = {int:id_member}
AND is_read = 0
AND content_type = {string:content_type}
AND content_id = {int:content_id}
AND content_action = {string:content_action}',
array(
'id_member' => $owner,
'content_type' => 'likefans',
'content_id' => $user_info['id'],
'content_action' => 'fans_liked',
)
);

if ($smcFunc['db_num_rows']($request) > 0)
{
return true;
}

$smcFunc['db_free_result']($request);

$smcFunc['db_insert']('insert',
'{db_prefix}user_alerts',
array(
'alert_time' => 'int',
'id_member' => 'int',
'id_member_started' => 'int',
'member_name' => 'string',
'content_type' => 'string',
'content_id' => 'int',
'content_action' => 'string',
'is_read' => 'int',
'extra' => 'string'
),
array(
time(),
$owner,
$user_info['id'],
$user_info['name'],
'likefans',
$user_info['id'],
'fans_liked',
0,
$smcFunc['json_encode'](array('fans_link' => '?action=music;area=fans;sa=topic;top='.$topic.';art='.$artist,
'subject' => $message))
),

array('id_alert')
);

updateMemberData($owner, array('alerts' => '+'));
}

The popup works correctly and takes me to my fan forum but the alerts list has no link.




Bugo

Try to use the second parameter, $formats. Example:

    /**
     * @hook integrate_fetch_alerts
     */
    public function fetchAlerts(array &$alerts, array &$formats): void
    {
        global $user_info;

        if (empty($alerts))
            return;

        foreach ($alerts as $id => $alert) {
            if ($alert['content_action'] === 'some_action') {
                if ($alert['sender_id'] !== $user_info['id']) {
                    $alerts[$id]['icon'] = '<span class="alert_icon main_icons some_icon"></span>';

                    // $txt['alert_some_action'] = 'Short description of this action';
                    // $txt['alert_item_some_action'] = '{member_link} did something {some_action_format}';
                    $formats['some_action_format'] = [
                        'required' => ['content_subject', 'content_link'],
                        'link'     => '<a href="%2$s">%1$s</a>',
                        'text'     => '<strong>%1$s</strong>'
                    ];
                } else {
                    unset($alerts[$id]);
                }
            }
        }
    }

mickjav

Thanks Bugo,

Have tried the code and played with it after not working correctly.

The old code when clicking the alert in the popup took me to my mod page, but the updated code takes me to the alerts list page, where I'm unable to use a link as it doesn't have a description set for the link.

ps

I'm using a class for my functions with php 8.1.30

thanks

mickjav

I just tried updating the language to include the link but the popup didn't like it, worked perfectly for main screen.

$txt['alert_likefans_fans_liked'] = '{member_link} liked your post <a href="{content_link}">View Post</a>

Bugo

Does the "View" button in the alerts list also not work?

mickjav

I think the alerts list view link worked ok, but will redo it tomorrow so I can double-check as not 100%


mickjav

Quote from: Bugo on November 23, 2024, 12:43:47 PMDoes the "View" button in the alerts list also not work?

Updating language file to:
$txt['alert_likefans_fans_liked'] = '{member_link} liked your post <a href="{content_link}">View Fan Forum Post</a>';

The link in main alerts list works perfectly but as with previous post image the member popup has the same effect.

Bugo

Still, I think that indicating the link like this in language files is not very good.

For example, in Light Portal, the corresponding entries for notifications look like this:

// Alerts
$txt['alert_page_comment'] = 'When my page gets a comment';
$txt['alert_new_comment_page_comment'] = '{gender, select,
    female {{member_link} left a comment {content_subject}}
    male {{member_link} left a comment {content_subject}}
    other {{member_link} left a comment {content_subject}}
}';

integrate_fetch_alerts:
foreach ($alerts as $id => $alert) {
  if (in_array($alert['content_action'], AlertAction::names())) {
    $icon = $alert['content_action'] === AlertAction::PAGE_COMMENT->name() ? 'im_off' : 'im_on';
    $icon = $alert['content_action'] === AlertAction::PAGE_UNAPPROVED->name() ? 'news' : $icon;

    if ($alert['sender_id'] !== User::$info['id']) {
      $alerts[$id]['icon'] = Str::html('span', ['class' => 'alert_icon main_icons ' . $icon]);
      $alerts[$id]['text'] = Lang::getTxt(
        'alert_' . $alert['content_type'] . '_' . $alert['content_action'],
        ['gender' => $alert['extra']['sender_gender']]
      );

      $link = Config::$scripturl . '?action=profile;u=' . $alert['sender_id'];

      $substitutions = [
        '{member_link}' => $alert['sender_id'] && $alert['show_links']
          ? Str::html('a')->href($link)->setText($alert['sender_name'])
          : Str::html('strong')->setText($alert['sender_name']),
        '{content_subject}' => '(' . $alert['extra']['content_subject'] . ')'
      ];

      $alerts[$id]['text'] = strtr($alerts[$id]['text'], $substitutions);
      $alerts[$id]['target_href'] = $alert['extra']['content_link'];
    } else {
      unset($alerts[$id]);
    }
  }
}

Thus, if I suddenly need the post title to become a link, I can change `{content_subject}` to this:

'{content_subject}' => '(<a href="' . $alert['extra']['content_link']. '">' . $alert['extra']['content_subject'] . '</a>)'

mickjav

Yeah, I agree about the language file link, Was just playing with it lol

Thanks for the code, I'll see if I can something working with it.

I'll post if I get it working.

All the best mick

Advertisement: