News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Using PHP code to close a MOD

Started by bigjoe11a, October 31, 2012, 08:49:06 PM

Previous topic - Next topic

bigjoe11a

I have an admin section, Where hr/she can open of close the mod. For some reason, It just doesn't work. and it makes no sense, It should work.



if($context['bjblog']['settings']['open'] == 'Closed') {

//Then I redirect to a template.

}


I used this option before on other PHP scripts and it's all ways work before. Any one have any ideas why this doesn't work in SMF 2.0.2

SMF Forums http://www.df-barracks.com Where gamers go.

mashby

I know I am not really a coder, but what does this mean?
if($context['bjblog']['settings']['open']
I read the snippet of code as:
If the door is open and it's equal to closed, run this code which is a contradiction. When is an open door closed?
Always be a little kinder than necessary.
- James M. Barrie

Suki

Dunno what exactly do you want to do, dunno what MOD is either.

What you are doing is to assume your var exists already and it has a "Closed" string as a value, this isn't always the case, you need to check for the var existence, always before actually using it:

if (!empty($context['bjblog']['settings']['open']) && $context['bjblog']['settings']['open'] == 'Closed')


BTW, you can turn that var to a boolean value, either true or false since it can only be 2 possible variables, open or closed.
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

bigjoe11a

#3
Quote from: monster mashby on October 31, 2012, 08:56:59 PM
I know I am not really a coder, but what does this mean?
if($context['bjblog']['settings']['open']
I read the snippet of code as:
If the door is open and it's equal to closed, run this code which is a contradiction. When is an open door closed?

well it's like this. So I mite a printing error and I'll fix that. The idea is that the admin can Open or Close the MOD when ever he wants too. When it's closed. The MOD is closed to every one and no one can enter or use the MOD, unless the admin Opens it again.



if($context['bjblog_settings']['open']
$context['sub_template'] = 'bjclosed';
else
$context['sub_template'] = 'main';


Get the idea


Edit: Fix code tags.
SMF Forums http://www.df-barracks.com Where gamers go.

bigjoe11a

Quote from: La Catrina on October 31, 2012, 08:58:16 PM
Dunno what exactly do you want to do, dunno what MOD is either.

What you are doing is to assume your var exists already and it has a "Closed" string as a value, this isn't always the case, you need to check for the var existence, always before actually using it:

if (!empty($context['bjblog']['settings']['open']) && $context['bjblog']['settings']['open'] == 'Closed')


BTW, you can turn that var to a boolean value, either true or false since it can only be 2 possible variables, open or closed.


Well the value will all exist. The Admin can set this in the Admin template. This is a new mod, That I been working on for the last 2 weeks. There's just some of these small problems I need to get working again. Like I said. The idea is for the Admin is that he can Close or Open the MOD, When the mode is Open. Any one can enter unless a permission changes that and denies that user access.

If the option needs to be turned in to a bool. Then I can do that too. I'll try your idea and let you know.
SMF Forums http://www.df-barracks.com Where gamers go.

bigjoe11a

I tried changing it to a bool, and that doesn't seem to work all so. So I'm stump on this part of my mod.
SMF Forums http://www.df-barracks.com Where gamers go.

Suki

No, the value will not exists until the admin sets it or you set it.  Changing the var to a boolean surly won't help at al to your problem, it just make life easier for you.

Put your whole code here.
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

bigjoe11a

ok, here's how I load the settings


function LoadBJSettings() {
   
    global $context, $smcFunc;   
   
    $requestper = $smcFunc['db_query']('', '
    SELECT * FROM {db_prefix}bjblog_settings
    ',
    array()
    );
       
    $rowper = $smcFunc['db_fetch_assoc']($requestper);
   
        $context['bjblog_Settings']['groups'] = $rowper['a_groups'];
        $context['bjblog_Settings']['open'] = $rowper['open'];
        $context['bjblog_Settings']['can_guest'] = $rowper['can_guest'];
        $context['bjblog_Settings']['btotal'] = $rowper['btotal'];
        $context['bjblog_Settings']['comm'] = $rowper['comm'];
        $context['bjblog_Settings']['plimits'] = $rowper['plimits'];
        //per_page
        $context['bjblog_Settings']['per_page'] = $rowper['per_page'];
        $context['bjblog_Settings']['per_comm'] = $rowper['per_comm'];
       
        if($context['bjblog_Settings']['open'] == 'Open')
        $context['bjblog_Settings']['open'] = true;
        else
        $context['bjblog_Settings']['open'] = false;
       
        $smcFunc['db_free_result']($requestper);
}


and here's how I use the setting with the script loads


if(!$context['bjblog_Settings']['open']) {       
        $context['sub_template'] = 'bjclosed';
    }

SMF Forums http://www.df-barracks.com Where gamers go.

Suki

Is there a good reason as to why you aren't using plain regular $modSettings var for your settings?
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

bigjoe11a

Quote from: Suki on November 01, 2012, 09:16:16 AM
Is there a good reason as to why you aren't using plain regular $modSettings var for your settings?

Thank You. The reason is I haven't been able to find out how to use them. google and smf forums I been coming up blank, Can you show me how $modSettings work.

SMF Forums http://www.df-barracks.com Where gamers go.

Suki

Pick any mod that has settings on it, all of them uses $modSettings, for example:  http://custom.simplemachines.org/mods/index.php?mod=1929

Although it uses hooks, the principle is the same,  it really depends on how you want to build your settings and where but the basics is as follows:


$config_vars[] = $txt['Activity_Bar_title'];
$config_vars[] = array('check', 'Activity_Bar_enable');
$config_vars[] = array('check', 'Activity_Bar_show_in_posts');
$config_vars[] = array('check', 'Activity_Bar_show_in_profile');
$config_vars[] = array('text', 'Activity_Bar_label');
$config_vars[] = array('int', 'Activity_Bar_timeframe');
$config_vars[] = array('int', 'Activity_Bar_max_posts');
$config_vars[] = array('int', 'Activity_Bar_max_width');
$config_vars[] = '';


This is how you declare settings and in this case I used a hook to show those settings on the Modification and Settings page in the admin panel, thats all you need to do, SMF will take care of the saving and fetching for you, now you only need to use your settings as

$modSettings['Activity_Bar_max_width']

either way, you always need to check for the var existence first.
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

bigjoe11a

If I under stand you. I all ready some thing like this. In a hook. I fount a mod that uses hooks. and I use that sample to add my hooks and I didn't get any errors when I ran the add_action_hook.php

I guess it's related to permissions so I don't under stand what this is for. Or how to use it. Do you know of any one that can go step by step to show me what too do. See I all ready added 4 permissions in the hook. I just don't know how to add it to admin groups page so I can see and change the settings.

How ever this ManageSettings['']; I have no idea on what to do. I guess I missed some thing else when I added my hook, is that right.



SMF Forums http://www.df-barracks.com Where gamers go.

Suki

Just use the activity bar mod as an example, modify it to your own needs, change the settings to your own settings, change the name and var names to your own.

I don't know of anyone who can guide you step by step, best thing you can do is break things in local, thats how 95% of us learned.

There is nothing wrong with copying someone else work as long as you do an effort to actually understand why it was made like that. Eventually you will be able to figure it out things on your own, I still get surprised at how things in SMF works, even though I'm a developer, there is always something new to learn.
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

bigjoe11a

Ok, Where would I add them to my mod. In the ManageSettings.php file. Or ? and How would I add them to the admin page so I can set them when I need too.

Ok, I'll go over the mod again. I just don't under stand any of that or what should I do next. So I will need help on that part of it.

1) Where do I need to add that code at.
2) How do I add these settings to the Admin section page so I can change them

Thanks
SMF Forums http://www.df-barracks.com Where gamers go.

Suki

Check Sources/ManageSettings.php  that file is where most of the settings are done.

The specific part for the Modifications and Settings page is this bit:

$config_vars = array(
      // Mod authors, add any settings UNDER this line. Include a comma at the end of the line and don't remove this statement!!
   );

inside that array you can add your own settings following the example I gave you.
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

bigjoe11a

Well I tried this like 10 times making changes to the code and running the add_action_hook.php file and I still get no results. I mean nothing works. and I have no idea why or why this doesn't work. Let me show you what I have so far.

here's the add_action_hook.php

<?php
// If SSI.php is in the same place as this file, and SMF isn't defined, this is being run standalone.
if (file_exists(dirname(__FILE__) . '/SSI.php') && !defined('SMF'))
require_once(dirname(__FILE__) . '/SSI.php');
// Hmm... no SSI.php and no SMF?
elseif (!defined('SMF'))
die('<b>Error:</b> Cannot install - please verify you put this in the same place as SMF\'s index.php.');

add_integration_function('integrate_pre_include''$sourcedir/Subs-bjblog.php');
add_integration_function('integrate_actions''bjblog_add_hook');
add_integration_function('integrate_load_permissions''BJBlogAddPermissions');
?>



and here's how my Subs-bjblog.php file is all set up.


<?php
if (!defined('SMF'))
die('Hacking attempt...');

function 
BJBlog_add_hook(&$actionArray) {
$actionArray['bjblog'] = array('bjblog.php''bjblogMain');
}

function 
bjAddPermissions(&$permissionGroups, &$permissionList, &$leftPermissionGroups, &$hiddenPermissions, &$relabelPermissions) {

loadLanguage('BJBlog');

$permissionList['membergroup'] = array_merge(
array(
'post_bjblog' => array(false'bjblog''bjblog'),
'comm_bjblog' => array(false'bjblog''bjblog'),
'blog_bjblog' => array(false'bjblog''bjblog'),
'view_bjblog' => array(false'bjblog''bjblog'),
),
$permissionList['membergroup']
);

}

function 
BJBlog_load_permissions(&$permissionGroups, &$permissionList, &$leftPermissionGroups, &$hiddenPermissions, &$relabelPermissions)
{
global $context;

$permissionList['membergroup'] += array(
'view_bjblog' => array(false'bjblog''bjblog'),
'blog_bjblog' => array(false'bjblog''bjblog'),
'post_bjblog' => array(false'bjblog''bjblog'),
'comm_bjblog' => array(false'bjblog''bjblog'),        
);
    
}

function 
BJBlog_Settings(&$config_vars) {
    
        global 
$txt;
    
    
$config_vars[] = $txt['bjblog_header'];    
    
$config_vars[] = array('check''enabled_bjblog');
    
}


function 
BJBlog_who() {
    
    global 
$txt;
    
    
$BJBLOG '<a href="http://www.df-barracks.com">' $txt['bjblog_verison'].'</a>';
    
    return 
$BJBLOG;
    
    
}


?>



So I'm lost on this and don't know what to do, So what am I missing.

and here is how I have my
SMF Forums http://www.df-barracks.com Where gamers go.

Matthew K.

The function names are in camel case in some areas...and not in other areas. Match case.

bigjoe11a

Ok, Now I'm getting some of the idea, It created a new blank permission. How ever it didn't create a settings that I added. And in the permissions it created 4 blank entries. So what am I missing.


Here's an image of what it looks like. It's missing the header and the permission for each title. How do I add them.

SMF Forums http://www.df-barracks.com Where gamers go.

Matthew K.


bigjoe11a

You mean the $txt[''] = '';. They are in my Modifation.english.php file and I all so added them to a BJBlog.english.php file all so. Why, So I guess i am missing some thing.

SMF Forums http://www.df-barracks.com Where gamers go.

Advertisement: