Contact us form on custom page

Discussion forum for Extension Writers regarding Extension Development.
User avatar
R3gi
Registered User
Posts: 103
Joined: Mon May 05, 2014 4:33 pm
Location: Czech Republic

Contact us form on custom page

Post by R3gi » Sun Feb 28, 2016 6:56 pm

Hello,
I am working on an extension for my forum which will insert "about" page which should (except other things) include the Contact us form.

I have copied even the whole content (including the contact form) of memberlist_email.html:

Code: Select all

<!-- IF S_CONTACT_ADMIN-->
<h2 class="titlespace">{L_CONTACT_ADMIN}</h2>
<!-- ELSEIF S_SEND_USER -->
<h2 class="titlespace">{L_SEND_EMAIL_USER}</h2>
<!-- ELSE -->
<h2 class="titlespace">{L_EMAIL_TOPIC}</h2>
<!-- ENDIF -->

<form method="post" action="{S_POST_ACTION}" id="post">

	<!-- IF CONTACT_INFO -->
	<div class="panel">
		<div class="inner">
			<div class="postbody">
				<div class="content">
					{CONTACT_INFO}
				</div>
			</div>
		</div>
	</div>
		<br class="clear" />
	<!-- ENDIF -->

	<div class="panel">
		<div class="inner">
	<div class="content">

		<!-- IF ERROR_MESSAGE --><p class="error">{ERROR_MESSAGE}</p><!-- ENDIF -->
		<fieldset class="fields2">
		<!-- IF S_SEND_USER -->
			<dl>
				<dt><label>{L_RECIPIENT}{L_COLON}</label></dt>
				<dd><strong>{USERNAME_FULL}</strong></dd>
			</dl>
			<dl>
				<dt><label for="subject">{L_SUBJECT}{L_COLON}</label></dt>
				<dd><input class="inputbox autowidth" type="text" name="subject" id="subject" size="50" tabindex="1" value="{SUBJECT}" /></dd>
			</dl>
		<!-- ELSEIF S_CONTACT_ADMIN-->
			<dl>
				<dt><label>{L_RECIPIENT}{L_COLON}</label></dt>
				<dd><strong>{L_ADMINISTRATOR}</strong></dd>
			</dl>
			<!-- IF not S_IS_REGISTERED -->
			<dl>
				<dt><label for="email">{L_SENDER_EMAIL_ADDRESS}{L_COLON}</label></dt>
				<dd><input class="inputbox autowidth" type="text" name="email" id="email" size="50" maxlength="100" tabindex="1" value="{EMAIL}" /></dd>
			</dl>
			<dl>
				<dt><label for="name">{L_SENDER_NAME}{L_COLON}</label></dt>
				<dd><input class="inputbox autowidth" type="text" name="name" id="name" size="50" tabindex="2" value="{NAME}" /></dd>
			</dl>
			<!-- ENDIF -->
			<dl>
				<dt><label for="subject">{L_SUBJECT}{L_COLON}</label></dt>
				<dd><input class="inputbox autowidth" type="text" name="subject" id="subject" size="50" tabindex="3" value="{SUBJECT}" /></dd>
			</dl>
		<!-- ELSE -->
			<dl>
				<dt><label for="email">{L_EMAIL_ADDRESS}{L_COLON}</label></dt>
				<dd><input class="inputbox autowidth" type="email" name="email" id="email" size="50" maxlength="100" tabindex="2" value="{EMAIL}" /></dd>
			</dl>
			<dl>
				<dt><label for="name">{L_REAL_NAME}{L_COLON}</label></dt>
				<dd><input class="inputbox autowidth" type="text" name="name" id="name" size="50" tabindex="3" value="{NAME}" /></dd>
			</dl>
			<dl>
				<dt><label for="lang">{L_DEST_LANG}{L_COLON}</label><br />
					<span>{L_DEST_LANG_EXPLAIN}</span></dt>
				<dd><select name="lang">{S_LANG_OPTIONS}</select></dd>
			</dl>
		<!-- ENDIF -->
		<dl>
			<dt><label for="message">{L_MESSAGE_BODY}{L_COLON}</label><br />
			<span>{L_EMAIL_BODY_EXPLAIN}</span></dt>
			<dd><textarea class="inputbox" name="message" id="message" rows="15" cols="76" tabindex="4">{MESSAGE}</textarea></dd>
		</dl>
		<!-- IF S_REGISTERED_USER -->
		<dl>
			<dt>&nbsp;</dt>
			<dd><label for="cc_sender"><input type="checkbox" name="cc_sender" id="cc_sender" value="1" checked="checked" tabindex="5" /> {L_CC_SENDER}</label></dd>
		</dl>
		<!-- ENDIF -->
		</fieldset>
	</div>

	</div>
</div>

<div class="panel">
	<div class="inner">
	<div class="content">
		<fieldset class="submit-buttons">
			<input type="submit" tabindex="6" name="submit" class="button1" value="{L_SEND_EMAIL}" />
		</fieldset>
	</div>
	</div>
{S_FORM_TOKEN}
</div>

</form>
When I place it to my page, some of it's language strings doesn't appear correctly (placeholders are not replaced by the localization) and the form doesn't work as expected.

Please, can someone explain me why it doesn't work and what should I do to make it work?
Thank you!

User avatar
david63
Registered User
Posts: 16798
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: Contact us form on custom page

Post by david63 » Sun Feb 28, 2016 9:14 pm

I would guess that the language file for the contact form is not being loaded into your custom .php page.
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

User avatar
R3gi
Registered User
Posts: 103
Joined: Mon May 05, 2014 4:33 pm
Location: Czech Republic

Re: Contact us form on custom page

Post by R3gi » Sun Feb 28, 2016 9:29 pm

Well, my custom language strings and all other strings are displayed without any problem. How should I define it?
Contact_form.png
But there's not only problem with the strings, the form doesn't work at all. Also it looks a bit different from the contact form which appears on the usual contact page – see the fields.
Contact_forms.png
I thought it may be enough to copy and paste the default contact form from prosilver to make it work. Maybe there's some restriction in the system for it's placement (or it's not universal)?

User avatar
3Di
Former Team Member
Posts: 14467
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

Re: Contact us form on custom page

Post by 3Di » Mon Feb 29, 2016 2:30 am

Where is your PHP side of the medal? Your listener.
Please PM me only to request paid works. Thx.
Want to compensate me for my interest? Donate
My development's activity º PhpStorm's proud user
Extensions, Scripts, MOD porting, Update/Upgrades
✒️ Black Friday 2019 @ The Studio ▪️◾️

User avatar
R3gi
Registered User
Posts: 103
Joined: Mon May 05, 2014 4:33 pm
Location: Czech Republic

Re: Contact us form on custom page

Post by R3gi » Mon Feb 29, 2016 12:01 pm

I am not a php developer. Here is my listener which includes the load language function as described in the ACME demo.

Code: Select all

<?php
/**
*
* @package About
* @copyright (c) 2016 R3gi
* @license GNU General Public License v2
*
*/

namespace R3gi\about\event;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Event listener
 */
class main_listener implements EventSubscriberInterface
{
    static public function getSubscribedEvents()
    {
        return array(
            'core.user_setup'  => 'load_language_on_setup',
            'core.page_header' => 'add_page_link',
        );
    }

    /* @var \phpbb\controller\helper */
    protected $helper;

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

    /**
     * Constructor
     *
     * @param \phpbb\controller\helper $helper
     * @param \phpbb\template\template $template
     */
    public function __construct(\phpbb\controller\helper $helper, \phpbb\template\template $template)
    {
        $this->helper = $helper;
        $this->template = $template;
    }

    public function load_language_on_setup($event)
    {
        $lang_set_ext = $event['lang_set_ext'];
        $lang_set_ext[] = array(
            'ext_name' => 'R3gi/about',
            'lang_set' => 'lang', /* Name of the language file */
        );
        $event['lang_set_ext'] = $lang_set_ext;
    }

    public function add_page_link($event)
    {
        $page_address_name = 'about'; /* Name of the page (part of the address)*/
        $this->template->assign_vars(array(
            'U_ABOUT' => $this->helper->route('R3gi_about_route', array('name' => $page_address_name)),
        ));
    }
}

User avatar
RMcGirr83
Recognised Extension Developer
Posts: 21034
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr
Contact:

Re: Contact us form on custom page

Post by RMcGirr83 » Mon Feb 29, 2016 2:30 pm

You need to include the controller from the contact us form.
In times of change, learners inherit the earth, while the learned find themselves beautifully equipped to deal with a world that no longer exists - Eric Hoffer
Former Modifications/Extensions Team Member | My extensions
Appreciate the extensions/mods/support then buy me a beer
All requests for support via PM will be ignored

User avatar
R3gi
Registered User
Posts: 103
Joined: Mon May 05, 2014 4:33 pm
Location: Czech Republic

Re: Contact us form on custom page

Post by R3gi » Tue Mar 01, 2016 5:22 pm

OK, I've probably located desired code in the phpbb\message\message.php file, but how and where should I include it in order to make the form alive? And why do I have to include it, isn't it a core phpBB file? Why it isn't loaded somehow forever?
I've found a few guides which describes controllers (#1, #2), but none of them helped me and the first one is very confusing.
I am a novice, but I would like to learn to build extensions for phpBB so I wouldn't have to mess up the system files and the root directory as before.

User avatar
RMcGirr83
Recognised Extension Developer
Posts: 21034
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr
Contact:

Re: Contact us form on custom page

Post by RMcGirr83 » Wed Mar 02, 2016 11:54 am

The contact form uses this in the memberlist.php file

Code: Select all

		else if ($mode === 'contactadmin')
		{
			$form_name = 'admin';
		}
		else
		{
			trigger_error('NO_EMAIL');
		}
		$form = $phpbb_container->get('message.form.' . $form_name);
so look at phpbb/message/admin_form.php file. That is the controller file I was speaking of.
In times of change, learners inherit the earth, while the learned find themselves beautifully equipped to deal with a world that no longer exists - Eric Hoffer
Former Modifications/Extensions Team Member | My extensions
Appreciate the extensions/mods/support then buy me a beer
All requests for support via PM will be ignored

User avatar
R3gi
Registered User
Posts: 103
Joined: Mon May 05, 2014 4:33 pm
Location: Czech Republic

Re: Contact us form on custom page

Post by R3gi » Wed Mar 02, 2016 2:07 pm

Hmm, I still can't make it work. Could you please give me an idiot's step by step guide? What should I place where?
I've spend 2 hours searching how to include a file to the listener, found this include($this->phpbb_root_path . '/phpbb/message/admin_form.' . $this->php_ext);, but it gives me an error

Code: Select all

[phpBB Debug] PHP Warning: in file [ROOT]/ext/R3gi/about/event/main_listener.php on line 17: include(includes/functions_posting.php): failed to open stream: No such file or directory
[phpBB Debug] PHP Warning: in file [ROOT]/ext/R3gi/about/event/main_listener.php on line 17: include(): Failed opening 'includes/functions_posting.php' for inclusion (include_path='.;C:/XAMPP/php/PEAR')

User avatar
david63
Registered User
Posts: 16798
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: Contact us form on custom page

Post by david63 » Wed Mar 02, 2016 2:28 pm

Have you injected $phpbb_root_path and $php_ext into your listener?
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

User avatar
R3gi
Registered User
Posts: 103
Joined: Mon May 05, 2014 4:33 pm
Location: Czech Republic

Re: Contact us form on custom page

Post by R3gi » Wed Mar 02, 2016 2:45 pm

I've tried, but it probably doesn't work :cry:

Code: Select all

define('IN_PHPBB', true);
$phpbb_root_path = './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
The whole main_listener.php file:

Code: Select all

<?php
/**
*
* @package About
* @copyright (c) 2016 R3gi
* @license GNU General Public License v2
*
*/

namespace R3gi\about\event;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;

define('IN_PHPBB', true);
$phpbb_root_path = './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);

include($phpbb_root_path . 'phpbb/message/admin_form.' . $phpEx);

/**
 * Event listener
 */
class main_listener implements EventSubscriberInterface
{
    static public function getSubscribedEvents()
    {
        return array(
            'core.user_setup'  => 'load_language_on_setup',
            'core.page_header' => 'add_page_link',
        );
    }

    /* @var \phpbb\controller\helper */
    protected $helper;

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

    /**
     * Constructor
     *
     * @param \phpbb\controller\helper $helper
     * @param \phpbb\template\template $template
     */
    public function __construct(\phpbb\controller\helper $helper, \phpbb\template\template $template)
    {
        $this->helper = $helper;
        $this->template = $template;
    }

    public function load_language_on_setup($event)
    {
        $lang_set_ext = $event['lang_set_ext'];
        $lang_set_ext[] = array(
            'ext_name' => 'R3gi/about',
            'lang_set' => 'lang', /* Name of the language file */
        );
        $event['lang_set_ext'] = $lang_set_ext;
    }

    public function add_page_link($event)
    {
        $page_address_name = 'about'; /* Name of the page (part of the address)*/
		/* Note for admins: to remove app.php from the URL, set "Enable URL Rewriting" to "Yes" in ACP.*/
        $this->template->assign_vars(array(
            'U_ABOUT' => $this->helper->route('R3gi_about_route', array('name' => $page_address_name)),
        ));
    }
}

User avatar
david63
Registered User
Posts: 16798
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: Contact us form on custom page

Post by david63 » Wed Mar 02, 2016 2:57 pm

I am not sure where it is that you are trying to use this function but your code should be something like this

Code: Select all

<?php
/**
*
* @package About
* @copyright (c) 2016 R3gi
* @license GNU General Public License v2
*
*/

namespace R3gi\about\event;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;


/**
 * Event listener
 */
class main_listener implements EventSubscriberInterface
{
	/** @var \phpbb\controller\helper */
    protected $helper;

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

	/** @var string phpBB root path */
	protected $root_path;

	/** @var string PHP extension */
	protected $phpEx;

    /**
    * Constructor
    *
    * @param \phpbb\controller\helper $helper
    * @param \phpbb\template\template $template
	* @param string 				   $root_path
	* @param string 				   $php_ext
	*
	* @access public
	*/
    public function __construct(\phpbb\controller\helper $helper, \phpbb\template\template $template, $root_path, $php_ext)
    {
        $this->helper		= $helper;
        $this->template		= $template;
		$this->root_path	= $root_path;
		$this->phpEx		= $php_ext;
    }

    static public function getSubscribedEvents()
    {
        return array(
            'core.user_setup'  => 'load_language_on_setup',
            'core.page_header' => 'add_page_link',
        );
    }

    public function load_language_on_setup($event)
    {
        $lang_set_ext = $event['lang_set_ext'];
        $lang_set_ext[] = array(
            'ext_name' => 'R3gi/about',
            'lang_set' => 'lang', /* Name of the language file */
        );
        $event['lang_set_ext'] = $lang_set_ext;
    }

    public function add_page_link($event)
    {
		include($this->root_path . 'phpbb/message/admin_form.' . $this->phpEx);

        $page_address_name = 'about'; /* Name of the page (part of the address)*/
      /* Note for admins: to remove app.php from the URL, set "Enable URL Rewriting" to "Yes" in ACP.*/
        $this->template->assign_vars(array(
            'U_ABOUT' => $this->helper->route('R3gi_about_route', array('name' => $page_address_name)),
        ));
    }
}
You will also have to add $root_path & $php_ext into your services.yml file.

Also when including another file you should test to see if it is already loaded
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

User avatar
R3gi
Registered User
Posts: 103
Joined: Mon May 05, 2014 4:33 pm
Location: Czech Republic

Re: Contact us form on custom page

Post by R3gi » Wed Mar 02, 2016 4:07 pm

Thank you very much! This helps me a lot. It's a bit harder then I expected.
So now the main_listener.php looks like this:

Code: Select all

<?php
/**
*
* @package About
* @copyright (c) 2016 R3gi
* @license GNU General Public License v2
*
*/

namespace R3gi\about\event;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;


/**
 * Event listener
 */
class main_listener implements EventSubscriberInterface
{
    /** @var \phpbb\controller\helper */
    protected $helper;

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

    /** @var string phpBB root path */
    protected $root_path;

    /** @var string PHP extension */
    protected $phpEx;

    /**
     * Constructor
     *
     * @param \phpbb\controller\helper $helper
     * @param \phpbb\template\template $template
     * @param string                $root_path
     * @param string                $php_ext
     *
     * @access public
     */
    public function __construct(\phpbb\controller\helper $helper, \phpbb\template\template $template, $root_path, $php_ext)
    {
        $this->helper = $helper;
        $this->template = $template;
        $this->root_path = $root_path;
        $this->phpEx = $php_ext;
    }

    static public function getSubscribedEvents()
    {
        return array(
            'core.user_setup'  => 'load_language_on_setup',
            'core.page_header' => 'add_page_link',
        );
    }

    public function load_language_on_setup($event)
    {
        $lang_set_ext = $event['lang_set_ext'];
        $lang_set_ext[] = array(
            'ext_name' => 'R3gi/about',
            'lang_set' => 'lang', /* Name of the language file */
        );
        $event['lang_set_ext'] = $lang_set_ext;
    }

    public function add_page_link($event)
    {
        include($this->root_path . 'phpbb/message/admin_form.' . $this->phpEx);

        $page_address_name = 'about'; /* Name of the page (part of the address)*/
		/* Note for admins: to remove app.php from the URL, set "Enable URL Rewriting" to "Yes" in ACP.*/
        $this->template->assign_vars(array(
            'U_ABOUT' => $this->helper->route('R3gi_about_route', array('name' => $page_address_name)),
        ));
		
		/* Test if the file was included */
		$file_to_test = 'admin_form.php';
		if(!@include($file_to_test)) {
			$error_msg = "Failed to include '$file_to_test'.";
            echo($error_msg);
			//trigger_error($error_msg, E_USER_ERROR);
        }
    }
}
and the services.yml

Code: Select all

services:
    R3gi.about.controller:
        class: R3gi\about\controller\main_controller
        arguments:
            - '@config'
            - '@controller.helper'
            - '@template'
            - '@user'
    R3gi.about.listener:
        class: R3gi\about\event\main_listener
        arguments:
            - '@controller.helper'
            - '@template'
            - '%core.root_path%'
            - '%core.php_ext%'
        tags:
            - { name: event.listener }
But according the test I've added to the end of the listener the file is not included and the contact form still doesn't work…

Code: Select all

		/* Test if the file was included */
		$file_to_test = 'admin_form.php';
		if(!@include($file_to_test)) {
			$error_msg = "Failed to include '$file_to_test'.";
            echo($error_msg);
			//trigger_error($error_msg, E_USER_ERROR);
        }

User avatar
david63
Registered User
Posts: 16798
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Name: David Wood
Contact:

Re: Contact us form on custom page

Post by david63 » Wed Mar 02, 2016 4:26 pm

Sorry - I just realised what you are trying to do.

You do not use the include for a class file, you would only use that for something like a functions file.

What you have to do is inject the classes that you want to use into your controller/listener.
David
Remember: You only know what you know and - you don't know what you don't know!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored

User avatar
RMcGirr83
Recognised Extension Developer
Posts: 21034
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr
Contact:

Re: Contact us form on custom page

Post by RMcGirr83 » Wed Mar 02, 2016 4:32 pm

Which is exactly how memberlist.php does it.
In times of change, learners inherit the earth, while the learned find themselves beautifully equipped to deal with a world that no longer exists - Eric Hoffer
Former Modifications/Extensions Team Member | My extensions
Appreciate the extensions/mods/support then buy me a beer
All requests for support via PM will be ignored

Post Reply

Return to “Extension Writers Discussion”