Fork me on GitHub

A push in the right direction for my first module please  Bottom

  • I'm creating a new module for paid membership, called "Membership". This is my first module in PN and I'm basing it on the Example module supplied with PN0.750-RC3. I'm following the Module Development Guide for what it's worth and I'll add comments to that as I come across useful stuff. At the moment I have a problem.

    I have created my pntables.php with the Membership_pntables function defining my tables.

    I have created my pninit.php with the code to create the tables using the Membership_pninit function.

    I have updated the pnversion.php file and when I regenerate the Module list under Admin, everything is shown as I expect it to.

    When I initialise the module, it says it's worked. However, the tables aren't created. In fact if I remove the pninit.php file and initialise the module it still says it's initialised okay. Same if I remove the pntables.php file. I have a feeling it's not calling the right script.

    What am I doing wrong :?: It doesn't bode well for the rest of the development does it?

    P.S. A forum for help in creating modules would be really useful :)
  • A couple of tips and tricks.

    Firstly ensure there aren't any syntax errors in your pntables/pninit files. The quickest way to check this is to call the two files directly in a browser i.e. http://www.yoursite.com/module/yourmod/pninit.php. Assuming you've display_errors on in your PHP configuration (check this via php.ini or a phpinfo report) then any syntax errors will be displayed.

    Secondly check the SQL is valid. Just prior to excuting the SQL ($dbconn->execute) add the following code

    Code

    echo $sql;
    die;


    This will echo the generated SQL and halt the script. Copy and paste the resultant SQL in phpMyAdmin and see if this gives you anything useful.

    -Mark

    --
    Visit My homepage and Zikula themes.
  • Mark,

    Excellent. I had a couple of spare brackets lurking around in my pninit.php. Still not working, but I'm on the right track. Strange that it says it has initialised okay though, when the code is pants. A bug perhaps?
    Ta meester.

    UPD. All sorted now. I'm sure this won't be the last post about this module. I'm updating the MDG with helpful tips as I go.
  • A bug - well not really..... The way the PHP lanugage handles includes doesn't really give too much scope for any error hanldling. The include function doesn't return a result than can be checked. You have to go the whole hog and have a full error trapping system - this is in the works though....

    So at the moment an include failing due to a syntax error in the included file is the same as the include failing due to no file being present. And if there is no pninit file present then we assume there's no init script needed for this module (rather than force a dummy script) and thus the module should initialise fine....

    Please send me any updated docs for the MDG - also bear in mind that not all of the MDG has been implemented as yet (hence PN is at 0.7x....). The MDG will need a good update for the .8x release.

    -Mark

    --
    Visit My homepage and Zikula themes.
  • Okay, understand that.

    I'm just adding comments to the MDG as I go, rather than formalising any documentation. Hopefully that'll help people developing modules now and can be used in any future changes to the MDG.
  • Mark,
    Can't the system use something like

    Code

    $return = include($file);

    and check the return? The documentation indicates this is possible, and that by default the return is 1 on success (true).

    Martin
  • Martin,

    Your right.... i'd missed that in the docs. Every application i've used (PN included) doesn't use the return value from an include. I'll test this and add it to .8x (and .7x depending on when I get to it).

    Thanks for pointing that out.

    -Mark

    --
    Visit My homepage and Zikula themes.
  • No prob. Glad I can teach an old dog a new trick (hope that doesn't sound like an insult :) )
    Even without it, if you need to initialise a module, surely the init function has to exist, and similarly with Updating/Deleting, so if function_exists should be able to at least report failure if it doesn't. Better than it reporting success when nothing has happened.
  • I just tested this in modules/Modules/pnadminapi.php

    Code

    // Module initialisation function
        $osdir = pnVarPrepForOS($modinfo['directory']);
        $success=@include("modules/$osdir/pninit.php");
    if ($success==false) {
                 pnSessionSetVar('errormsg', _MODINITFAILED);
                return false;
            }
        @include("modules/$osdir/pnlang/" . pnVarPrepForOS(pnUserGetLang()) . "/init.php");
        $func = $modinfo['name'] . '_init';
        if (function_exists($func)) {
            if ($func() != true) {
                return false;
            }
        } else {
            pnSessionSetVar('errormsg', _MODINITFAILED2);
            return false;
        }

    and added this to the language file modules/Modules/lang/eng/admin.php

    Code

    define('_MODINITFAILED','Module initialisation failed, no init file present.');
    define('_MODINITFAILED2','Module initialisation failed, init function not found.');


    Now it warns me that the initialisation failed and why.
  • msandersen

    Even without it, if you need to initialise a module, surely the init function has to exist, and similarly with Updating/Deleting, so if function_exists should be able to at least report failure if it doesn't. Better than it reporting success when nothing has happened.



    Well that was the problem... A simple module may not need to create any tables, set any module vars, register any hooks etc. so PN has never required a init script - a look at the exiting codebase confirms this. So it's not as simple as a function_exists check.

    But since we can check for a successful include there's a simple solution. I'd never got that far down the include documentation. Thanks for the patch - i'll include this in the codebase this weeknd.

    'old dog' - ouch!.... I'm only 31..... ;)

    -Mark

    --
    Visit My homepage and Zikula themes.
  • "old dog" relatively speaking! In terms of PN developers, I expect you are! (at least since the split). You are 3 years younger than me, so what does that make me? In terms of programming, though, I'm an amateur, I just play around a bit for fun.

    You could make it a requirement to at least have the function for New-type modules, even if it just returns true; a module like PostJump virtually doesn't do anything in initialising (well, it sets one variable), but it has the function.
    Or just post a warning that it was initialised in absence of the init file/function. That would suffice for developers, too.
    Or maybe just have the feature for a developer Debug mode in Settings.
  • How far have you got down the file_exists function you young whipper snappers?
  • My arthritis is playing up... Rain is coming. Now, where did I put my teef?

This list is based on users active over the last 60 minutes.