deadbydawn
Member
Hi all,
I am attempting to create an add-on that would allow me to make unique preference options for my users. For example, some of them do not like the Facebook/Twitter integration (mostly the calls to external sites) and so I'd like to give them a method to opt-out.
I began experimenting a little and I am most of the way there, but I have a few questions about saving, and the methodology I am using to approach this (and the sanity therein).
I added a column to the xf_user_option table called "enable_ext_social_includes "which holds a boolean value. I was happy to see that this is automatically populated amongst the $visitor array on every page, so it makes for an easy way to always know. The other benefit to using the existing table and not creating my own is that I don't have to do any JOIN's or extra DB calls to get the variable.
Now that I have this in the database, it was simple enough to create my own template to display the user option on the account preferences template which gives the checkbox option in the same format as used in the rest of the template:
I then created an event listener and a ControllerPublic php page with the following stolen from the core Account.php Controller:
I was hoping I could override this function and simply add another line into the $settings array as follows to save any changes to that checkbox:
But doing so elicits the following error when trying to save my user preferences:
The field 'enable_ext_social_includes' was not recognised.
So I assume that there is some form of sanitizing being done in the core where the available fields in the xf_user_option table are hardcoded.. and this is a path I don't want to go down, of course. So my question to you is..
is there a better way? is my logic inherently flawed in thinking it wise to use the existing table rather than create my own? and if so, got any good pointers on how to do JOINs in a clever way since I'm a sql noob?
Thanks all!
I am attempting to create an add-on that would allow me to make unique preference options for my users. For example, some of them do not like the Facebook/Twitter integration (mostly the calls to external sites) and so I'd like to give them a method to opt-out.
I began experimenting a little and I am most of the way there, but I have a few questions about saving, and the methodology I am using to approach this (and the sanity therein).
I added a column to the xf_user_option table called "enable_ext_social_includes "which holds a boolean value. I was happy to see that this is automatically populated amongst the $visitor array on every page, so it makes for an easy way to always know. The other benefit to using the existing table and not creating my own is that I don't have to do any JOIN's or extra DB calls to get the variable.
Now that I have this in the database, it was simple enough to create my own template to display the user option on the account preferences template which gives the checkbox option in the same format as used in the rest of the template:
HTML:
<dl class="ctrlUnit">
<dt></dt>
<dd>
<ul>
<li><label for="ctrl_enable_ext_social_includes"><input type="checkbox" name="enable_ext_social_includes" value="1" id="ctrl_enable_ext_social_includes" {xen:checked "{$visitor.enable_ext_social_includes}"} /> Allow external includes (javascript) from popular social networks (facebook, twitter, etc)</label></li>
</ul>
</dd>
</dl>
I then created an event listener and a ControllerPublic php page with the following stolen from the core Account.php Controller:
PHP:
<?php
class DBDPreferences_ControllerPublic_DBDPreferences extends XFCP_DBDPreferences_ControllerPublic_DBDPreferences
{
public function actionPreferencesSave()
{
$this->_assertPostOnly();
$settings = $this->_input->filter(array(
//user
'language_id' => XenForo_Input::UINT,
'style_id' => XenForo_Input::UINT,
'visible' => XenForo_Input::UINT,
'timezone' => XenForo_Input::STRING,
//user_option
'content_show_signature' => XenForo_Input::UINT,
'enable_rte' => XenForo_Input::UINT,
));
if ($this->_input->filterSingle('default_watch_state', XenForo_Input::UINT))
{
$settings['default_watch_state'] = ($this->_input->filterSingle('default_watch_state_email', XenForo_Input::UINT)
? 'watch_email'
: 'watch_no_email'
);
}
else
{
$settings['default_watch_state'] = '';
}
if (!$this->_saveVisitorSettings($settings, $errors))
{
return $this->responseError($errors);
}
return $this->responseRedirect(
XenForo_ControllerResponse_Redirect::SUCCESS,
XenForo_Link::buildPublicLink('account/preferences')
);
}
}
I was hoping I could override this function and simply add another line into the $settings array as follows to save any changes to that checkbox:
PHP:
.......
'content_show_signature' => XenForo_Input::UINT,
'enable_rte' => XenForo_Input::UINT,
'enable_ext_social_includes' => XenForo_Input::UINT;
));
But doing so elicits the following error when trying to save my user preferences:
The field 'enable_ext_social_includes' was not recognised.
So I assume that there is some form of sanitizing being done in the core where the available fields in the xf_user_option table are hardcoded.. and this is a path I don't want to go down, of course. So my question to you is..
is there a better way? is my logic inherently flawed in thinking it wise to use the existing table rather than create my own? and if so, got any good pointers on how to do JOINs in a clever way since I'm a sql noob?
Thanks all!