ElkArte Community

Extending Elk => Addons => Addons ideas and questions => Topic started by: Burke Knight on June 28, 2015, 03:48:58 pm

Title: ManageSettings....
Post by: Burke Knight on June 28, 2015, 03:48:58 pm
I'm trying to work on a very small mod.
Since I liked the mod Global Headers anf Footers for SMF, but SMFHacks made it so many edits and files, I have decided to make a very simple one like it, for ElkArte.

However, since I can't do hooks, I need to do edits. I managed to find all that needed to do, except what would go into SMF's ManageSettings.php file.

Code: [Select]
	<file name="$sourcedir/ManageSettings.php">
<operation>
<search position="replace"><![CDATA[// Mod authors, add any settings UNDER this line. Include a comma at the end of the line and don't remove this statement!!]]></search>
<add><![CDATA[// Mod authors, add any settings UNDER this line. Include a comma at the end of the line and don't remove this statement!!
                array('text', 'global_head', '30'),
array('text', 'global_mid', '30'),
array('text', 'global_foot', '30'),
'',
]]></add>
</operation>
</file>

Where, and how would I do that?  ;D
Title: Re: ManageSettings....
Post by: Joshua Dickerson on June 28, 2015, 04:04:47 pm
If you can't do something with a hook, please post what it is that you are trying to do. Obviously, templates aren't pluggable (yet).

You probably want to use the hooks in https://github.com/elkarte/Elkarte/blob/development/sources/admin/AddonSettings.controller.php
Title: Re: ManageSettings....
Post by: Burke Knight on June 28, 2015, 04:15:48 pm
I can't do anything with hooks.
I have learning disabilities, and am lucky I can do what I can... LOL

Hooks, look like nothing but gibberish to me.

Here is the file edits I have for my mod:

Code: [Select]
	<file name="$themedir/index.template.php">
<operation>
<search position="after"><![CDATA[</head>]]></search>
<add><![CDATA[if(!empty($modSettings['global_head']))
echo '
<div>', $modSettings['global_head'], '</div>
]]></add>
</operation>

<operation>
<search position="after"><![CDATA[ // Show the navigation tree.
theme_linktree();]]></search>
<add><![CDATA[if(!empty($modSettings['global_head']))
echo '
<div>', $modSettings['global_head'], '</div>
]]></add>
</operation>

<operation>
<search position="before"><![CDATA[</body>]]></search>
<add><![CDATA[if(!empty($modSettings['global_foot']))
echo '
<div>', $modSettings['global_foot'], '</div>
]]></add>
</operation>
</file>

<file name="$languagedir/Addons.english.php">
<operation>
<search position="end" />
<add><![CDATA[
$txt['global_head'] = 'Add content to the <head></head> tags:';
$txt['global_mid'] = 'Add content below linktree:';
$txt['global_foot'] = 'Add content to the footer:';
]]></add>
</operation>
</file>

<file name="$sourcedir/ManageSettings.php">
<operation>
<search position="replace"><![CDATA[// Mod authors, add any settings UNDER this line. Include a comma at the end of the line and don't remove this statement!!]]></search>
<add><![CDATA[// Mod authors, add any settings UNDER this line. Include a comma at the end of the line and don't remove this statement!!
                array('text', 'global_head', '30'),
array('text', 'global_mid', '30'),
array('text', 'global_foot', '30'),
'',
]]></add>
</operation>
</file>
Title: Re: ManageSettings....
Post by: ahrasis on June 28, 2015, 09:59:31 pm
I think you can insert that as top and bottom layers but I cant exactly understand the guide given by @emanuele last time. Basically (I think), create your two templates and insert them as layers. The settings and language parts should be easy. I'll try to give them a look when I sit before my pc.
Title: Re: ManageSettings....
Post by: Burke Knight on June 28, 2015, 10:14:09 pm
Well, the template and language edits, are okay. I found the files needed to do them.
I just can't figure out how to get the settings in the addons misc section... LOL
Title: Re: ManageSettings....
Post by: emanuele on June 29, 2015, 03:14:15 pm
I guess the function you are looking for is:
https://github.com/elkarte/Elkarte/blob/9bfcf0a52afa37e193a29d2771558abaecfd7696/sources/admin/AddonSettings.controller.php#L140
Title: Re: ManageSettings....
Post by: Burke Knight on June 29, 2015, 05:21:29 pm
1 problem with that, @emanuele
I still can't do hooks, and I see no way to do the edits there.
So, someone is going to have to help Hook this addon.... LOL
Title: Re: ManageSettings....
Post by: emanuele on June 29, 2015, 06:16:43 pm
Quote from: Burke Knight – I still can't do hooks, and I see no way to do the edits there.
Why you don't see any way?
Isn't that a piece of code like any other? ;)

ETA: just to be a little more clear, what I would do is search for:
Code: [Select]
);

// Add new settings with a nice hook.
(tabs lost in the copy&paste from github)
Title: Re: ManageSettings....
Post by: Burke Knight on June 29, 2015, 06:30:39 pm
What I meant, is how do I do the edits, in non-hook form?
I have no idea how to write out a file for hooks.
It's one reason, why I never got my mods updated.... LOL

I need to add these lines:

Code: [Select]
		array('text', 'global_head', '30'),
array('text', 'global_mid', '30'),
array('text', 'global_foot', '30'),

Unless you mean to replace what you posted with:
Code: [Select]
		array('text', 'global_head', '30'),
array('text', 'global_mid', '30'),
array('text', 'global_foot', '30'),
);

// Add new settings with a nice hook.

EDIT: That worked on test forum!

Now, in the modification xml file, I take it, I need to adjust: $sourcedir/AddonSettings.controller.php
to this, maybe: $sourcedir/admin/AddonSettings.controller.php
Title: Re: ManageSettings....
Post by: emanuele on June 29, 2015, 06:56:02 pm
One thing I missed: in Elk there is not $sourcedir and alike, but constants like "SOURCEDIR", "THEMEDIR", "SUBSDIR", "ADMINDIR", "LANGUAGEDIR", etc.
The full list should be the one in index.php at:
https://github.com/elkarte/Elkarte/blob/9bfcf0a52afa37e193a29d2771558abaecfd7696/index.php#L66

The difference is that, instead of:
Code: [Select]
<file name="$themedir/index.template.php">
you'll have to write:
Code: [Select]
<file name="THEMEDIR/index.template.php">

And of course, if one day you'll be curious about hooks, let me know your questions and doubts (side note: I think that starting from 1.1 they will become easier to grasp (I hope), because it will be possible to use the approach "put the file in that place and everything will automagically work", anyway it's still a bit early for that :P).
Title: Re: ManageSettings....
Post by: Burke Knight on June 29, 2015, 07:13:11 pm
Yeah, I inadvertently found that one out! LOL :)

Okay, I'm getting TPE on the index.template edit, and can't find out why:

Code: [Select]
syntax error, unexpected 'id' (T_STRING), expecting ',' or ';'

Code: [Select]
<file name="THEMEDIR/index.template.php">
<operation>
<search position="replace"><![CDATA[ echo '
</head>]]></search>
<add><![CDATA[
if(!empty($modSettings['global_head']))
echo '
<div>', $modSettings['global_head'], '</div>

</head>]]></add>
</operation>

<operation>
<search position="before"><![CDATA[ // Show the navigation tree.
theme_linktree();]]></search>
<add><![CDATA[if(!empty($modSettings['global_mid']))
echo '
<div>', $modSettings['global_mid'], '</div>';
]]></add>
</operation>

<operation>
<search position="before"><![CDATA[</body>]]></search>
<add><![CDATA[if(!empty($modSettings['global_foot']))
echo '
<div>', $modSettings['global_foot'], '</div>
]]></add>
</operation>
</file>

Any help on finding what I did wrong, would be appreciated.
Title: Re: ManageSettings....
Post by: ahrasis on June 29, 2015, 09:33:15 pm
Shouldn't the last edit operation be after?
Title: Re: ManageSettings....
Post by: Burke Knight on June 29, 2015, 09:36:57 pm
EDITED:


Well, got it fixed, and running on a test site nicely![1]

Here's what it's all about now, since I added a few changes:




I'll be getting it onto GitHub soon, and will do the addon site pull request when can. :)

On GitHub now, waiting for the post to be merged.  ;D
Title: Re: ManageSettings....
Post by: ahrasis on June 30, 2015, 01:21:59 pm
Nice work @Burke Knight . Now let's see if we can make this Global Header And Footer (GHF) mod into a hook mod.[1] We shall create files for it like GHF.subs.php, GHF.hooks.php etc.[2]

First part is the header part. I think we can call that in $context['html_headers']. In GHF.subs.php (whatever you want to name it), create a function for that so it the file will look something like this:
Code: [Select]
<?php
/**
 *
 * @author  Your name, email etc
 * @license Your license
 * @mod     Your mod name
 *
 */

if (!defined('ELK'))
die('No access...');

function GHF_Main()
{
// Globalize what you need
global $context, $modSettings;

// Load what you need
// eg. loadLanguage('GHF');

// Add things in header
if(!empty($modSettings['global_head'])) {
$context['html_headers'] .= ' . $modSettings['global_head'] . ';
}
}

?>

For footer, I think you can use $context['insert_after_template'] using the same principle as above.[3] Just modify the above function like this:

Code: [Select]
	// Add things in header
if(!empty($modSettings['global_head'])) {
$context['html_headers'] .= ' . $modSettings['global_head'] . ';
}
// Add things in footer
if(!empty($modSettings['global_foot'])) {
$context['insert_after_template'] .= ' . $modSettings['global_foot'] . ';
}

I am not so sure about the middle one after the linktree though I supposed you can use either addAfter or buffer. To use buffer, you can add another function like this:

Code: [Select]
function GHF_Mid(&$buffer)
{
// Globalize what you need
global $modSettings, $context;

if (isset($_REQUEST['xml']) || $context['current_action'] == 'printpage') return $buffer;

$ghf = array();

// Description
if(!empty($modSettings['global_mid'])) {
// Define the old and the new title
$title_old = '<div id="main_content_section"><a id="skipnav"></a>';
$title_new = '<br /><div>' . $modSettings['global_mid'] . '</div>
<div id="main_content_section"><a id="skipnav"></a>';
$gfh[$title_old] = $title_new;
}
// Now let's change the title, if we're allowed to
return str_replace(array_keys($gfh), array_values($gfh), $buffer);
}

Or if you wish to use layer addAfter you can create another function[4] something like this:

Code: [Select]
function GHF_middle()
{
// Globalize what you need

// Load what you need
// Add things in middle
if(!empty($modSettings['global_mid'])) {
loadTemplate('GHF);
$template_layers->addAfter('template_body_above', 'GHF_middle');
}
}

That cover your changes in the template file. Your language file should be a simple GHF.english.php and put all the contents inside it.

Code: [Select]
<?php
/**
 *
 * @author  Your name, email etc
 * @license Your license
 * @mod     Your mod name
 *
 */

$txt['global_head'] = 'Add content to the <head></head> tags:';
$txt['global_mid'] = 'Add content below linktree:';
$txt['global_foot'] = 'Add content to the footer:';
$txt['global_copy'] = 'Add custom copyright to the footer:';

?>

Your addon settings can be made inside the GHF.subs.php by adding another function:

Code: [Select]
function GHF_Settings(&$config_vars)
{
// Load the language
loadLanguage('GHF');

// Add the settings
$ghf = array(
array('large_text', 'global_head', '4'),
array('large_text', 'global_mid', '4'),
array('large_text', 'global_foot', '4'),
array('text', 'global_copy', '30'),
'',
);

// Insert after all available slice.
$first = array_slice($config_vars, 0);
$config_vars = array_merge($first, $ghf);
}

Then you hook file, GHF.hooks.php.

Code: [Select]
<?php
/**
 *
 * @author  Your name, email etc
 * @license Your license
 * @mod     Your mod name
 *
 */

// If we have found SSI.php and we are outside of ELK, then we are running standalone.
if (file_exists(dirname(__FILE__) . '/SSI.php') && !defined('ELK'))
require_once(dirname(__FILE__) . '/SSI.php');

// If we are outside ELK and can't find SSI.php, then throw an error
elseif (!defined('ELK'))
die('<b>Error:</b> Cannot install - please verify you put this file in the same place as Elkarte\'s SSI.php.');

if (ELK == 'SSI')
db_extend('packages');

// Define the hooks
$hook_functions = array(
'integrate_buffer' => 'GHF_Mid|SOURCEDIR/addons/GHF.subs.php',
'integrate_load_theme' => 'GHF_Main|SOURCEDIR/addons/GHF.subs.php',
'integrate_general_mod_settings' => 'GHF_Settings|SOURCEDIR/addons/GHF.subs.php',
);

// Adding or removing them?
if (!empty($context['uninstalling']))
$call = 'remove_integration_function';
else
$call = 'add_integration_function';

// Do the deed
foreach ($hook_functions as $hook => $function)
$call($hook, $function);

if (ELK == 'SSI')
   echo 'Congratulations! You have successfully installed this mod!';
?>

Lastly, you should have something like this in your package-info.xml
Code: [Select]
	<install for="1.0 - 1.0.99">
<readme type="file" parsebbc="true">GHF.readme.txt</readme>
<require-file name="GHF.english.php" destination="LANGUAGEDIR/english" />
<require-file name="GHF.subs.php" destination="SOURCEDIR/addons" />
<code>GHF.hooks.php</code>
<redirect url="?action=admin;area=addonsettings" timeout="499">Redirecting to Addons Settings...</redirect>
</install>
<uninstall for="1.0 - 1.0.99">
<readme parsebbc="true">GHF.readme.txt</readme>
<remove-file name="LANGUAGEDIR/english/GHF.english.php" />
<remove-file name="SOURCEDIR/addons/GHF.subs.php" />
<code>GHF.hooks.php</code>
<redirect url="?action=admin;area=packages" timeout="499">Redirecting to Package Manager...</redirect>
</uninstall>

That are the rough ideas on making it a hook mod. However, I haven't tested it yet.
Up to the extend of what I know ony. Do correct me if I am wrong.
This is an old style until I understand the other integration methods. Lol.
Is this correct @emanuele as I haven't used this before?
Not quite practical to me since you must create own template file for this.
Title: Re: ManageSettings....
Post by: Burke Knight on June 30, 2015, 01:34:14 pm
Well, there went my demo site... LOL
Tried it, and WSOD after install. :(
Title: Re: ManageSettings....
Post by: ahrasis on June 30, 2015, 02:05:05 pm
Delete GHF.subs.php in sources/addons. Then uninstall.

Fixes:
1. Mid not main (in hooks file):
Code: [Select]
'integrate_buffer' => 'GHF_Mid|SOURCEDIR/addons/GHF.subs.php',
2. Remove the parenthesis to (in subs file):
Code: [Select]
	// Add things in header
if (!empty($modSettings['global_head'])) {
$context['html_headers'] .= $modSettings['global_head'];
}
// Add things in footer
if (!empty($modSettings['global_foot'])) {
$context['insert_after_template'] .= $modSettings['global_foot'];
}

That should work. ;)
Title: Re: ManageSettings....
Post by: Jorin on July 01, 2015, 01:21:50 am
Hm, I will try this when it is a real modification package.  ;)
Title: Re: ManageSettings....
Post by: Burke Knight on July 01, 2015, 01:38:46 am
Quote from: ahrasis – Delete GHF.subs.php in sources/addons. Then uninstall.

Fixes:
1. Mid not main (in hooks file):
Code: [Select]
'integrate_buffer' => 'GHF_Mid|SOURCEDIR/addons/GHF.subs.php',
2. Remove the parenthesis to (in subs file):
Code: [Select]
	// Add things in header
if (!empty($modSettings['global_head'])) {
$context['html_headers'] .= $modSettings['global_head'];
}
// Add things in footer
if (!empty($modSettings['global_foot'])) {
$context['insert_after_template'] .= $modSettings['global_foot'];
}

That should work. ;)


Do I keep the . before the = ?

EDIT: removed them, and it installs fine.
However, the footer is under the footer area's div, and not inside it.
The copyright does not show. (We both seemed to have missed it...LOL)

Rest appears to be fine, so at least we are 60% there. :P

Can see at: http://bke.cc.nf
The Big BKE logos are what I added to the Mid and Footer. The favicon, is what added to Head.
Title: Re: ManageSettings....
Post by: ahrasis on July 01, 2015, 06:01:00 am
I didn't see code for copyright in your modification file when I read it at the github yesterday. May be I missed it. I will look into it again when I am on my pc.

I think you can use insert layer approach to insert a layer inside footer but I haven't mastered this one yet. But may be just another buffer will be for the footer. I'll think about it later.

I think you will still need the . but I forget why. :)
Title: Re: ManageSettings....
Post by: emanuele on July 01, 2015, 07:14:43 am
If you mean the .=, then yes. Always use ".=" when adding stuff to html_headers because others may add something as well and not using the "." you'd scrap everything away.

Re layers: I may find some time to write down an example later today.
Title: Re: ManageSettings....
Post by: ahrasis on July 02, 2015, 06:01:31 am
I was not sure which place you are trying to put your global footer, before or after the default footer. Now I think it should be before but inside it. You can try changing the first two functions as follows:
Code: [Select]
function GHF_Main()
{
 // Globalize what you need
 global $context, $modSettings;
 
 // Load what you need
 // eg. loadLanguage('GHF');

 // Add things in header
 if (!empty($modSettings['global_head'])) {
 $context['html_headers'] .= $modSettings['global_head'];
 }
}

function GHF_Mid(&$buffer)
{
 // Globalize what you need
 global $modSettings, $context;

 if (isset($_REQUEST['xml']) || $context['current_action'] == 'printpage') return $buffer;

 $ghf = array();

 // Description
 if(!empty($modSettings['global_mid'])) {
 // Insert the global_mid
 $ghf_old1 = '<div id="main_content_section"><a id="skipnav"></a>';
 $ghf_new1 = '<br /><div>' . $modSettings['global_mid'] . '</div>
 <div id="main_content_section"><a id="skipnav"></a>';
 $gfh[$ghf_old1] = $ghf_new1;
 
 // Insert the global_foot
 $ghf_old2 = '<div id="footer_section"><a id="bot"></a>';
 $ghf_new2 = '<div id="footer_section"><a id="bot"></a>
 <div>' . $modSettings['global_foot'] . '</div>';
 $gfh[$ghf_old2] = $ghf_new2;
 
 // Insert the global_copy
 $ghf_old3 = '<li class="copyright">';
 $ghf_new3 = '<li class="copyright">' . $modSettings['global_copy'] . '<br />';
 $gfh[$ghf_old3] = $ghf_new3;
 }
 // Now let's change the title, if we're allowed to
 return str_replace(array_keys($gfh), array_values($gfh), $buffer);
}

You should now have global footer before but inside the default footer and global copy as well.

I think the best practice would be adding layers. Let's wait for @emanuele 's note for that.
Title: Re: ManageSettings....
Post by: emanuele on July 02, 2015, 06:15:21 am
Side note: the "insert_after_template", means really after template, so just before the closing body tag.

Either way, now that I think about it, and judging by the replacements made by @Burke Knight, I feel using layers is not really an option, because there is not a good place to hook into, so... go for edits for the moment.
Title: Re: ManageSettings....
Post by: Burke Knight on July 02, 2015, 06:49:12 am
@ahrasis

Works, not exactly where had the footer one on the older version, but more where wanted it, so double bonus. :)


@Spuds

I noticed, even though I left out the part about hooks in the submission of original, it says on addon site:
QuoteThis addon is 100% hooks based requiring no source edits to install.

However, this new package, will be, so not big issue on this one, but just letting you know it messed up.
Now, how do I update an addon entry?
Title: Re: ManageSettings....
Post by: emanuele on July 02, 2015, 07:52:29 am
You can just update the page, commit the changes and send the PR.