Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: Xarkurai on January 17, 2017, 06:07:48 PM

Title: Is ssi_recentEvents not working or am I doing it wrong?
Post by: Xarkurai on January 17, 2017, 06:07:48 PM
Hello there!

I'm trying to make ssi_recentEvents work for me but for some reason it doesn't show the upcoming events.
Is it me that is doing the wrong code or is it simply not working?

My code:

<?php
$test
= ssi_recentEvents($max_events = 7, $output_method = 'array');

foreach ($test as $event)
{
if ($event['can_edit'])
echo '
<a href="'
. $event['modify_href'] . '" style="color: #ff0000;">*</a> ';

echo '
'
. $event['link'] . (!$event['is_last'] ? ', ' : '');
}
?>


I took it directly from the SSI example.
ssi_todaysEvents however, works when I use it.

Thanks in advance!
Title: Re: Is ssi_recentEvents not working or am I doing it wrong?
Post by: Xarkurai on January 20, 2017, 08:42:57 PM
Bump, anyone?
Title: Re: Is ssi_recentEvents not working or am I doing it wrong?
Post by: vbgamer45 on January 20, 2017, 09:07:24 PM
Try
$test = ssi_recentEvents(7, 'array');

You can see what what test is returning by print_r($test);
Title: Re: Is ssi_recentEvents not working or am I doing it wrong?
Post by: Xarkurai on January 21, 2017, 08:10:22 AM
Hi,

Thanks for the reply.
It simply returns "Array ( )" and nothing more.
Title: Re: Is ssi_recentEvents not working or am I doing it wrong?
Post by: Kindred on January 21, 2017, 09:18:02 AM
What is your site URL?
Title: Re: Is ssi_recentEvents not working or am I doing it wrong?
Post by: Xarkurai on January 21, 2017, 10:16:21 AM
I'm not having it online at the moment, using it on WAMP for coding a new frontpage.
I bounced on it while trying.
I can provide any file if needed.
Title: Re: Is ssi_recentEvents not working or am I doing it wrong?
Post by: Kindred on January 21, 2017, 10:19:03 AM
check your ssi_examples.php on your site.

If the recentEvents is not generating anything, even in there, then your issue goes even deeper than SSI...  or maybe you don't actually HAVE any events in the calendar?

Title: Re: Is ssi_recentEvents not working or am I doing it wrong?
Post by: Xarkurai on January 21, 2017, 10:29:35 AM
<?php ssi_todaysEvents(); ?>

Works, it shows events: http://puu.sh/turpa/513628066f.png.

<?php ssi_recentEvents(); ?>

Shows nothing: http://puu.sh/turq5/3636daa9a2.png.

Events are in the calendar, I generated 6 for the testing purpose.

Edit: uninstalling a few modifications to see if one of them is causing a problem..
Edit2: nope.

Edit 3: tested it on my "live"' website, not working either: http://okamilegion.com/ssi_examples.php
Edit 4: It only shows up when the event is at the day itself. Is this intended?

Edit5 (phew): So I'm not sure if it was intended to only show the event of the day itself. I don't really think it makes much sense that way.
Looking deeper into the SSI, I found that the SQL prevents it.
At some point there is this:
WHERE cal.start_date <= {date:current_date}
AND cal.end_date >= {date:current_date}
AND (cal.id_board = {int:no_board} OR {query_wanna_see_board})

which limits it pretty hard. In other words: I found the cause so I can fix it. Thanks. But then again, I would like to know if this was the original intention for the SSI function?

Thank you for the responses though! It got me to the cause :).
Title: Re: Is ssi_recentEvents not working or am I doing it wrong?
Post by: Xarkurai on January 21, 2017, 11:09:18 AM
Not to bump but to post my solution to it.
At the function ssi_recentEvents($max_events = 7, $output_method = 'echo'), look at the SQL and change:


WHERE cal.start_date <= {date:current_date}
AND cal.end_date >= {date:current_date}
AND (cal.id_board = {int:no_board} OR {query_wanna_see_board})

To this:

WHERE cal.start_date >= {date:current_date}
AND (cal.id_board = {int:no_board} OR {query_wanna_see_board})


To make it work:

<?php
$result
= ssi_recentEvents(6,'array');
foreach ($result as $mday => $array)
foreach ($array as $event)
{
if ($event['can_edit'])
echo '
<a href="'
. $event['modify_href'] . '" style="color: #ff0000;">*</a> ';

echo '
'
. $event['link'] . (!$event['is_last'] ? ', ' : '');
}
?>


Please correct me if I'm wrong somewhere, I'm still learning about coding.
This works for me, showing all upcoming events.

Cheers.
Title: Re: Is ssi_recentEvents not working or am I doing it wrong?
Post by: Chen Zhen on January 21, 2017, 01:50:55 PM
You can also rewrite the function to allow for a specific span of time:

/**
* Show the most recent events
* @param int $max_events The maximum number of events to show
* @param string $output_method The output method. If 'echo', displays the events, otherwise returns an array of info about them.
* @param string $end_date The period of time added to the current date. ie. 'year=1', 'month=2', 'day=6'. Default value is the same day.
* @return void|array Displays the events or returns an array of info about them, depending on output_method.
*/
function ssi_recentEvents($max_events = 7, $output_method = 'echo', $end_date = 'today')
{
global $user_info, $scripturl, $modSettings, $txt, $context, $smcFunc;

if (empty($modSettings['cal_enabled']) || !allowedTo('calendar_view'))
return;

$end_date = strtolower(preg_replace('/\s+/', '', $end_date));
$datespan = strtok($end_date, '=');
$amount = abs((int) filter_var($end_date, FILTER_SANITIZE_NUMBER_INT));

if (in_array($datespan, array('day', 'month', 'year')) && $amount > 0)
$timespan = strftime('%Y-%m-%d', strtotime(date('Y-m-d', forum_time(false)) . ' + ' . $amount . ' ' . $datespan));
else
$timespan = strftime('%Y-%m-%d', forum_time(false));

// Find all events which are happening in the near future that the member can see.
$request = $smcFunc['db_query']('', '
SELECT
cal.id_event, cal.start_date, cal.end_date, cal.title, cal.id_member, cal.id_topic,
cal.id_board, t.id_first_msg, t.approved
FROM {db_prefix}calendar AS cal
LEFT JOIN {db_prefix}boards AS b ON (b.id_board = cal.id_board)
LEFT JOIN {db_prefix}topics AS t ON (t.id_topic = cal.id_topic)
WHERE cal.start_date >= {date:current_date}
AND cal.end_date <= {date:end_date}
AND (cal.id_board = {int:no_board} OR {query_wanna_see_board})
ORDER BY cal.start_date DESC
LIMIT ' . $max_events,
array(
'current_date' => strftime('%Y-%m-%d', forum_time(false)),
'end_date' => $timespan,
'no_board' => 0,
)
);
$return = array();
$duplicates = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
// Check if we've already come by an event linked to this same topic with the same title... and don't display it if we have.
if (!empty($duplicates[$row['title'] . $row['id_topic']]))
continue;

// Censor the title.
censorText($row['title']);

if ($row['start_date'] < strftime('%Y-%m-%d', forum_time(false)))
$date = strftime('%Y-%m-%d', forum_time(false));
else
$date = $row['start_date'];

// If the topic it is attached to is not approved then don't link it.
if (!empty($row['id_first_msg']) && !$row['approved'])
$row['id_board'] = $row['id_topic'] = $row['id_first_msg'] = 0;

$return[$date][] = array(
'id' => $row['id_event'],
'title' => $row['title'],
'can_edit' => allowedTo('calendar_edit_any') || ($row['id_member'] == $user_info['id'] && allowedTo('calendar_edit_own')),
'modify_href' => $scripturl . '?action=' . ($row['id_board'] == 0 ? 'calendar;sa=post;' : 'post;msg=' . $row['id_first_msg'] . ';topic=' . $row['id_topic'] . '.0;calendar;') . 'eventid=' . $row['id_event'] . ';' . $context['session_var'] . '=' . $context['session_id'],
'href' => $row['id_board'] == 0 ? '' : $scripturl . '?topic=' . $row['id_topic'] . '.0',
'link' => $row['id_board'] == 0 ? $row['title'] : '<a href="' . $scripturl . '?topic=' . $row['id_topic'] . '.0">' . $row['title'] . '</a>',
'start_date' => $row['start_date'],
'end_date' => $row['end_date'],
'is_last' => false
);

// Let's not show this one again, huh?
$duplicates[$row['title'] . $row['id_topic']] = true;
}
$smcFunc['db_free_result']($request);

foreach ($return as $mday => $array)
$return[$mday][count($array) - 1]['is_last'] = true;

// If mods want to do somthing with this list of events, let them do that now.
call_integration_hook('integrate_ssi_recentEvents', array(&$return));

if ($output_method != 'echo' || empty($return))
return $return;

// Well the output method is echo.
echo '
<span class="event">' . $txt['events'] . '</span> ';
foreach ($return as $mday => $array)
foreach ($array as $event)
{
if ($event['can_edit'])
echo '
<a href="' . $event['modify_href'] . '" style="color: #ff0000;">*</a> ';

echo '
' . $event['link'] . (!$event['is_last'] ? ', ' : '');
}
}


When calling the function add another parameter for spanning a number of: day, month or year.

$test = ssi_recentEvents(7, 'array', 'month=6');
Title: Re: Is ssi_recentEvents not working or am I doing it wrong?
Post by: Xarkurai on January 21, 2017, 02:05:02 PM
Ha, that seems to be an interesting way to do it as well.
I also changed
$return[$date][] = array( to
$return[] = array(
as well.
So foreach ($result as $mday => $array) is not necessary.

It felt a lot easier to write the output.