Confirm_box call causes error

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
EVO_VV
Registered User
Posts: 55
Joined: Tue Feb 13, 2018 3:11 pm

Confirm_box call causes error

Post by EVO_VV »

I'm currently working on an Extension to add a button to the 'Posting buttons' the function of which is to move a Post to another pre-determined Topic.
So far the code works and performs the action correctly including the logging of the action and re-syncing the affected Topics etc.

However allowing something like this to occur without a 'Confirmation' is not good practice so I attempted to use a 'confirm_box' call to achieve that.

$confirmed_ok = confirm_box(false, 'MOVE');

Currently I cannot get the box to appear because I get an Error reported halting execution.

The error displayed is multiple warnings in user.php on line 619 call to get_user something referencing a user_timezone and a fatal error in functions.php on the call to function page_header(....);
Checking further in the page_header function the line that triggers the error is :-

extract($phpbb_dispatcher->trigger_event('core.page_header', compact($vars)));

Before this line there is no error (testing with inserting an echo "here; die();) but directly after that line the error occurs.

My first thought is that because I am calling this function outside of the core code that I do not have access to $phpbb_dispatcher.
I looked at the other Extensions I use on the board and found that the 'Thanks for Posts' one used that particular service.
So I copied what was done there in their services.yml and listener.php to mine but that did not solve the problem.
Instead it gave me another error saying that the instances configured and called did not match.

So my question is am I right in that to eliminate (or at least get a step further in debugging) the initial error I need to configure an instance of a 'dispatcher' and if so could someone point me in the right direction as to which one to use and how to use it.
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: Confirm_box call causes error

Post by mrgoldy »

It would once again, really help if you would actually post the error and the code.

Moreover,
Some information about the confirm_box(): https://wiki.phpbb.com/Function.confirm_box

Code: Select all

		if (confirm_box(true))
		{
			// Perform the action ...
			
			
			
			// Log the action
			$this->log->add('user', $this->user->data['user_id'], $this->user->ip, 'The log message', array('reportee_id' => $this->user->data['user_id']));

			// Show success message
			$this->helper->message($this->lang->lang('The success message'));
		}
		else
		{
			confirm_box(false, 'The confirmation message', build_hidden_fields(array(
				// fill in correct hidden fields to get to the 'true' part of the confirm box
				// For example if the $action variable has to be equal to 'move' then:
				// 'action'	=> 'move',
			)));

			// Use a redirect to take the user back to the previous page
			// if the user chose to cancel from the confirmation page.
			redirect('The redirect url');
		}
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
EVO_VV
Registered User
Posts: 55
Joined: Tue Feb 13, 2018 3:11 pm

Re: Confirm_box call causes error

Post by EVO_VV »

Thanks for taking the time to have a look at this problem I am having.
I did not post more code as there really was not much else other than the call to confirm_box(....); which is where the error occurred and I assumed it would be something simple that I was missing.

What I am trying to achieve is to get a 'Confirm Box' to appear so that if the user clicks OK then the relevant code is executed and returns to the page they were on if Cancel is clicked.

I tried using that 'If' condition and the same error appeared so it looks like something is missing somewhere but what I do not know.
I also do not understand exactly what it does other than provide a 'Confirm Box' if 'OK has not already been clicked and to execute code if 'OK' has been clicked. Therefore, as far as I can see, there must have been a 'Confirm Box' earlier in the code for the first condition in the 'if' to ever be true.

Here is the code I used to test and the errors received :-

Code: Select all

public function my_move_post($event)
	{

		$move_reason = $this->request->variable('move_reason', '', true);

		if ($move_reason != ("Obsolete Post" ||"Empty Topic"))
		{
			return;  // Working as expected to check if the event is actually for this extension
		}

		$post_id = $event['post_id'];
		$topic_id = $this->functions->get_topic_id($topic_id, $post_id);
		$forum_id = $event['forum_id'];


		if ($move_reason == "Obsolete Post")
		{
			$s_hidden_fields = array(
				'action'	=> 'move',
			);
			
		$l_obsolete = 'MOVE';
		
		if (confirm_box(true))
		{
			// Perform the action ...
			
//		echo "Yes do it";  // This is displayed correctly when uncommented with no errors
//		die();
			// Log the action
			// Show success message
		}
		else
		{
//		echo "Box should show next";  // This is displayed correctly when uncommented with no errors
//		die();
			confirm_box(false, $l_obsolete, $s_hidden_fields);


// ERRORS OCCUR HERE

		echo "No don't do it";
		die();
			// Use a redirect to take the user back to the previous page
		}
	  }
	}
The errors I get are :-

Code: Select all

[phpBB Debug] PHP Warning: in file [ROOT]/phpbb/user.php on line 619: DateTime::setTimezone() expects parameter 1 to be DateTimeZone, null given
Fatal error: Call to a member function getName() on null in /opt/lampp/htdocs/public_html/forum_new/includes/functions.php on line 4319
The first warning is shown many times which might indicate that it is a loop of some kind and possibly involving $user data.
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: Confirm_box call causes error

Post by mrgoldy »

To what event are you subscribing in your event listener?
Do you have a github repository that is viewable?

Cause your code still shows a few issues.

$topic_id = $this->functions->get_topic_id($topic_id, $post_id);
The first parameter for the get_topic_id() function ($topic_id) is not set/defined anywhere.

$s_hidden_fields is not set if $move_reason is not "Obsolete Post".

After researching a bit:
- https://tracker.phpbb.com/browse/PHPBB3-13426
- viewtopic.php?f=461&t=2321911

Check your user's timezone are set.
Make sure your vendor folder has the correct permission set (755).
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
EVO_VV
Registered User
Posts: 55
Joined: Tue Feb 13, 2018 3:11 pm

Re: Confirm_box call causes error

Post by EVO_VV »

Thank you again for trying to assist me with this problem.

The Event that I am subscribing to is core.modify_posting_parameters

I do not have a github repository as I have only ever used github for downloading some extensions that are not available via phpBB.

The extension code works exactly as desired without the addition of the attempts to get a 'Confirmation Box' to show up before the relevant code is executed.
The possible $topic_id problem you mentioned is not a problem as it is returned from the call to get_topic_id() and is set correctly.
Do I not need to add this to the parameters on calling the function ?

I actually have two buttons that are inserted hence the if ($move_reason != ("Obsolete Post" ||"Empty Topic")) but at the moment I am only working on the first case to get a user 'Confirmation' before the relevant code is executed.
Once I get that working than it can be adjusted for the other case.

As far as I have been able to test both the user_id and user_timezone are correct and available in functions.php during execution on triggering the event.

Vendor folder has permissions of :-
Owner Can view and Modify
Group Can view content
Others Can view content
Which I believe is 755

What I am trying to achieve here is that after a user clicks on one of my inserted buttons they are presented with an "Are You Sure" prompt and if they click on the 'YES' option then the relevant code is executed and they are redirected to where they need to be. Or if the 'NO' option is clicked then they are returned to exactly where they were before that clicked my button.

How do I make confirm_body.html appear and access the user input to determine what action to take in the code ?

Actually my ultimate goal is to have an 'Ajax' pop up instead of confirm_body.html but It is good practice to allow for Java not being available and if I cannot get the simple case to work what chance do I stand with Java of which I have less knowledge.
EVO_VV
Registered User
Posts: 55
Joined: Tue Feb 13, 2018 3:11 pm

Re: Confirm_box call causes error

Post by EVO_VV »

Still cannot get the Confirm Box to show up.
Following the code execution by using the 'echo "got here"; die();' I have found that the errors as quoted in the previous post occur when the following line is executed in functions.php (around line 4168) :-

extract($phpbb_dispatcher->trigger_event('core.page_header', compact($vars)));

This line is in the page_header function called by the confirm_box function around line 2175.

page_header((!isset($user->lang[$title])) ? $user->lang['CONFIRM'] : $user->lang[$title]);

$user->lang[$title] is set to a standard phpBB one - 'MOVE' which when echo'd at this point in the code execution shows "Move" as it should.
As far as I can see there is only one parameter passed to this function which according to the initial call should be set to a value whatever the case.
Therefore I am at a loss to understand why the error should appear here.

The problem must be something basic I am missing as I can see examples of a Confirm Box used in other working extensions which I have examined and tried to learn from and are working fine in the environment I am testing my extension in.
From the documentation I have been able to find or been advised to look at it seems that a call to confirm_box(false, $title); should be sufficient to elicit the display of a Confirmation Box but I cannot seem to get that to happen.

If I am on a page that shows the Posts in a Topic and I click upon the Button to delete a particular post I get a Confirmation Box, my button is successfully added to the same row of buttons and on clicking I want the same (or actually simpler - just are you sure Yes or No).
How do I achieve this in an extension ?
EVO_VV
Registered User
Posts: 55
Joined: Tue Feb 13, 2018 3:11 pm

Re: Confirm_box call causes error

Post by EVO_VV »

I gather, from previous posts I have checked looking to see if anyone else has had similar problems, that either a link to somewhere so that the complete code can be seen or that complete code should be provided as the problems could stem from code not posted but exists.

To this end I have created a new extension using the skeleton (v 1.0.6) with the minimal content to show the problem I am having in the hope that someone can tell me what I am missing or doing incorrectly.
I'm using 3.2.1 on a LAMPP stack on my PC for testing.
Hopefully this will all go in one post and to that end I have omitted the 'Normal' parts of the files that have no impact upon the executing of the files such as the first few lines with the copyright and license stuff.

services.yml

Code: Select all

services.yml
services:
    evo.confirmed..listener:
        class: evo\confirmed\event\listener
        arguments:
            - '@dbal.conn'
            - '@request'
            - '%core.root_path%'
            - '%core.php_ext%'
            - '@user'
            - '@template'
            - '@config'
            - '@dispatcher'
        tags:
            - { name: event.listener }
There are probably more services than needed as I have added some to see if anything obvious (to me) was missing. But it is quite possible that I am missing a needed one just don't know which one.

listener.php

Code: Select all

namespace evo\confirmed\event;

/**
 * @ignore
 */
use phpbb\db\driver\driver_interface as db_interface;
use phpbb\request\request_interface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use phpbb\user;
use phpbb\template\template;
use phpbb\config\config;

/**
 * Confirmed Event listener.
 */
class listener implements EventSubscriberInterface
{

	/** @var db_interface */
	protected $db;
	
	/** @var request_interface */
	protected $request;
	
	/** @var string phpbb_root_path */
	protected $phpbb_root_path;

	/** @var string phpEx */
	protected $php_ext;
	
	/** @var functions */
	protected $functions;

	/** @var user */
	protected $user;

	/** @var \template */
	protected $template;

	/** @var config */
	protected $config;

	/** @var \phpbb\event\dispatcher_interface */
	protected $phpbb_dispatcher;

	/**
 	* Constructor
	*
	* @param db_interface				$db
	* @param request_interface			$request
	* @param string					$phpbb_root_path
	* @param string					$php_ex
	* @param user					$user
	* @param template				$template
	* @param config					$config
	* @param \phpbb\event\dispatcher_interface	$phpbb_dispatcher
	*/
	
	public function __construct(
		db_interface $db,
		request_interface $request,
		$phpbb_root_path,
		$php_ext,
		user $user,
		template $template,
		config $config,
		\phpbb\event\dispatcher_interface $phpbb_dispatcher
		)
	{
		$this->db			= $db;
		$this->request			= $request;
		$this->phpbb_root_path		= $phpbb_root_path;
		$this->php_ext			= $php_ext;
		$this->user			= $user;
		$this->template			= $template;
		$this->config			= $config;
		$this->phpbb_dispatcher		= $phpbb_dispatcher;
	}


	static public function getSubscribedEvents()
	{
		return array(
			'core.user_setup'			=> 'load_language_on_setup',
			'core.viewtopic_modify_post_row'	=> 'insert_post_buttons',
			'core.modify_posting_parameters'	=> 'lb_move_post',
		);
	}

	/**
	 *
	 * @param \phpbb\event\data	$event	Event object
	 */

	public function load_language_on_setup($event)
	{
		$lang_set_ext = $event['lang_set_ext'];
		$lang_set_ext[] = array(
			'ext_name' => 'evo/confirmed',
			'lang_set' => 'common',
		);
		$event['lang_set_ext'] = $lang_set_ext;
	}

	public function insert_post_buttons($event)
	{

		$row = $event['row'];
		$forum_id = (int) $row['forum_id'];
		$post_id = (int) $row['post_id'];

		$postrow = $event['post_row'];

			$postrow = array_merge($postrow, array(
				'U_DEADPOSTS' => append_sid("{$this->phpbb_root_path}posting.$this->php_ext", 'mode=move&f=' . $forum_id . '&p=' . $post_id . '&move_reason=Obsolete Post'),
			));
		$event['post_row'] = $postrow;
	}

	public function lb_move_post($event)
	{
		$move_reason = $this->request->variable('move_reason', '', true);

		// Test to check if this event is for this extension
		if ($move_reason != "Obsolete Post")
		{
			return;  // Working as expected
		}

		// This extension's button was clicked so go ahead and do what needs to be done
		$title = 'MOVE';
		$redirect = append_sid("{$this->phpbb_root_path}viewtopic.$this->php_ext", 'f=' . $forum_id . '&p=' . $post_id . '&#p' . $post_id);
		$s_hidden_fields = build_hidden_fields(array(
			'post_id'		=> $post_id,
			'to_topic_id'		=> 6149,
			't'			=> $topic_id,
			'submit'		=> true,
			'redirect'		=> $redirect
		));

		if (!function_exists('confirm_box'))
		{
			include($this->phpbb_root_path . 'includes/functions.' . $this->php_ext);
		}
//echo "here"; NO ERROR SHOWN AT THIS POINT 'here' DISPLAYED CORRECTLY
//die();
//		$confirmed = confirm_box(false); CAUSES ERROR
//		$confirmed = confirm_box(false, $title); CAUSES ERROR
		$confirmed = confirm_box(false, $title, $s_hidden_fields); // CAUSES ERROR
//echo "here";  // NEVER GETS HERE
//die();
if ($confirmed)
{
echo "Do It"; // Code to perform the required actions - user clicked YES
}else{
echo "Cancel"; // Operation cancelled - user clicked NO
}
die();

	}
}
As far as I can see from the documentation the confirm_box function should work with just the first three parameters passed but I cannot get the Box to appear.
The $title is passed correctly so it could be that there is something in the $s_hidden_fields that I am missing.
I have also tried to follow the examples in other working extensions but the confirm_box(true) part of the if condition never gets executed and it always fails on the confirm_box(false) line

All I am trying to achieve is to give the user the opportunity, after they have clicked my button, the chance to cancel the operation should they have clicked it in error.

Just to complete the code, although as the button appears and is recognised when clicked, I don't think this has any bearing on the problem, here is the rest of the code :-

language/en/common.php

Code: Select all

if (empty($lang) || !is_array($lang))
{
	$lang = array();
}

$lang = array_merge($lang, array(
	'DEADPOSTS'			=> 'Move post to All Dead Posts',
));
styles/prosilver/temlate/event/viewtopic_body_post_buttons_after.html

Code: Select all

      <a href="{postrow.U_DEADPOSTS}" title="{L_DEADPOSTS}" class="button button-icon-only">
      <i class="icon fa-chain-broken fa-fw" style="font-size:24px" aria-hidden="true"></i>
      <span class="sr-only">{L_DEADPOSTS}</span>
      </a>
The error I get is 26 lines of :-
[phpBB Debug] PHP Warning: in file [ROOT]/phpbb/user.php on line 619: DateTime::setTimezone() expects parameter 1 to be DateTimeZone, null given
And then :-
Fatal error: Call to a member function getName() on null in /opt/lampp/htdocs/public_html/forum_new/includes/functions.php on line 4321

Any assistance will be greatly appreciated.
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: Confirm_box call causes error

Post by mrgoldy »

I'm beginning to understad what you're trying to do.
So you're not working during the actual editing of a post, but just inside a viewtopic.

Then it might be better to use a route and a controller.
I've put together a basic set up for you, to work with:

[The extension zip has been deactivated and can no longer be displayed.]


Confirm box works.
- https://prnt.sc/j7ubke
- https://prnt.sc/j7ubo1
- https://prnt.sc/j7ubqr (as there is no code in the true part of the confirm box, this is correct)
Also working when people press 'No'.

Moreover on the reason, I would add a 'reason_id' to it. And move accordingly. For example. 'obsolete post' is reason_id 1, etc.
Cause you were adding the string "Obsolete Post" to the url. Spaces are converted to %20, etc. It's not the best practise.

Oh, and please get a GitHub account. Makes code sharing (especially full repositories) so much easier!
It's really hard to look at pieces of code on here. And more often than not, the error/problem is not in it but in a different file.
We need to be able to have a look at the complete code.
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
EVO_VV
Registered User
Posts: 55
Joined: Tue Feb 13, 2018 3:11 pm

Re: Confirm_box call causes error

Post by EVO_VV »

Thank you so much for taking the time to look at that.
Now I have something to look at to find out and understand what I was missing.
I will get a GitHub account and use it for any future requests for assistance.
Post Reply

Return to “Extension Writers Discussion”