Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Aiheen aloitti: Biology Forums - lokakuu 22, 2013, 07:56:48 IP

Otsikko: How do I add an if statement here:
Kirjoitti: Biology Forums - lokakuu 22, 2013, 07:56:48 IP
$normal_buttons = array
(
'reply' => array('text' => 146, 'image' => 'reply.gif', 'lang' => true, 'url' => $scripturl . '?action=post;topic=' . $context['current_topic'] . '.' . $context['start'] . ';num_replies=' . $context['num_replies']),
'new_topic' => array('text' => 'smf258', 'image' => 'new_topic.gif', 'lang' => true, 'url' => $scripturl . '?action=post;board=' . $context['current_board'] . '.0'),


What like to add an if statement between the first ( and 'new_topic'. So:

$normal_buttons = array
(

if(SOMETHING)

'reply' => array('text' => 146, 'image' => 'reply.gif', 'lang' => true, 'url' => $scripturl . '?action=post;topic=' . $context['current_topic'] . '.' . $context['start'] . ';num_replies=' . $context['num_replies']),

ELSE

'someotherreply' => array('text' => 146, 'image' => 'reply.gif', 'lang' => true, 'url' => $scripturl . '?action=post;topic=' . $context['current_topic'] . '.' . $context['start'] . ';num_replies=' . $context['num_replies']),

'new_topic' => array('text' => 'smf258', 'image' => 'new_topic.gif', 'lang' => true, 'url' => $scripturl . '?action=post;board=' . $context['current_board'] . '.0'),
Otsikko: Re: How do I add an if statement here:
Kirjoitti: All Colours Sam - lokakuu 22, 2013, 07:59:04 IP
You can add a single key on any already created array:

$normal_buttons['reply']

if (something)
$normal_buttons['reply'] = ....


else
$normal_buttons['reply'] = ....
Otsikko: Re: How do I add an if statement here:
Kirjoitti: Biology Forums - lokakuu 22, 2013, 08:01:46 IP
However,

$normal_buttons = array
(


Is quite large. I was hoping to add a small IF statement within the array.
Otsikko: Re: How do I add an if statement here:
Kirjoitti: Arantor - lokakuu 22, 2013, 08:02:37 IP
You can't add a statement inside another statement.

It would, as ever, help to know the context because I can think of at least three other ways this could be done and the best one relates to what you're trying to do.
Otsikko: Re: How do I add an if statement here:
Kirjoitti: Biology Forums - lokakuu 22, 2013, 08:04:47 IP
$normal_buttons = array
(
'reply' => array('text' => 146, 'image' => 'reply.gif', 'lang' => true, 'url' => $scripturl . '?action=post;topic=' . $context['current_topic'] . '.' . $context['start'] . ';num_replies=' . $context['num_replies']),
'new_topic' => array('text' => 'smf258', 'image' => 'new_topic.gif', 'lang' => true, 'url' => $scripturl . '?action=post;board=' . $context['current_board'] . '.0'),
'bookmark' => array('test' => 'can_make_bookmarks', 'text' => 'bookmark_add', 'lang' => true, 'url' => $scripturl . '?action=bookmarks;sa=add;topic=' . $context['current_topic']),
'notify' => array('test' => 'can_mark_notify', 'text' => $context['is_marked_notify'] ? 999 : 125, 'image' => $context['is_marked_notify'] ? 'unnotify.gif' : 'notify.gif', 'lang' => true, 'custom' => 'onclick="return confirm(\'' . ($context['is_marked_notify'] ? $txt['notification_disable_topic'] : $txt['notification_enable_topic']) . '\');"', 'url' => $scripturl . '?action=notify;sa=' . ($context['is_marked_notify'] ? 'off' : 'on') . ';topic=' . $context['current_topic'] . '.' . $context['start'] . ';sesc=' . $context['session_id']),
'custom' => array(),
'send' => array('test' => 'can_send_topic', 'text' => 707, 'image' => 'sendtopic.gif', 'lang' => true, 'url' => $scripturl . '?action=sendtopic;topic=' . $context['current_topic'] . '.0'),
'print' => array('text' => 465, 'image' => 'print.gif', 'lang' => true, 'custom' => 'target="_blank"', 'url' => $scripturl . '?action=printpage;topic=' . $context['current_topic'] . '.0'),
);


There is the array...

I want to add, IF($CONTEXT['can_reply']) show 'reply' => array('text' => 146, 'image' => 'reply.gif', 'lang' => true, 'url' => $scripturl . '?action=post;topic=' . $context['current_topic'] . '.' . $context['start'] . ';num_replies=' . $context['num_replies']), if not, show nothing.
Otsikko: Re: How do I add an if statement here:
Kirjoitti: Arantor - lokakuu 22, 2013, 08:08:22 IP
So, only show the reply button if a user can reply, yes?
Otsikko: Re: How do I add an if statement here:
Kirjoitti: Biology Forums - lokakuu 22, 2013, 08:09:25 IP
Lainaus käyttäjältä: Arantor - lokakuu 22, 2013, 08:08:22 IP
So, only show the reply button if a user can reply, yes?

Yes.
Otsikko: Re: How do I add an if statement here:
Kirjoitti: Arantor - lokakuu 22, 2013, 08:11:59 IP
That's rather ironic, you're putting SMF 1.1.x back to how SMF 1.1.x ships.

There is already a mechanism in the button system for doing just that. Replace that one line with:

'reply' => array('test' => 'can_reply', 'text' => 146, 'image' => 'reply.gif', 'lang' => true, 'url' => $scripturl . '?action=post;topic=' . $context['current_topic'] . '.' . $context['start'] . ';num_replies=' . $context['num_replies']),

The 'test' parameter checks the matching $context variable, in this case $context['can_reply'].

But THIS is why I asked why, so that the best option can be done.
Otsikko: Re: How do I add an if statement here:
Kirjoitti: Biology Forums - lokakuu 22, 2013, 08:13:16 IP
Lainaus käyttäjältä: Arantor - lokakuu 22, 2013, 08:11:59 IP
That's rather ironic, you're putting SMF 1.1.x back to how SMF 1.1.x ships.

There is already a mechanism in the button system for doing just that. Replace that one line with:

'reply' => array('test' => 'can_reply', 'text' => 146, 'image' => 'reply.gif', 'lang' => true, 'url' => $scripturl . '?action=post;topic=' . $context['current_topic'] . '.' . $context['start'] . ';num_replies=' . $context['num_replies']),

The 'test' parameter checks the matching $context variable, in this case $context['can_reply'].

But THIS is why I asked why, so that the best option can be done.

Very true, I was wondering what TEST meant... Thank you, Arantor.