Zikula: A Flexible Open Source Content Management System
home | forum | international support | contact us

Support Forum

Start ::  Extensions and Distributions Support ::  Modules ::  How to make this function API compliant.

Moderated by: Support Team

Bottom
How to make this function API compliant.

  • Link to this postingPosted: 15.05.2003, 01:02
    Profile Homepage
    the-ermine
    rank:
    Freshman Freshman
    registered:
     January 2003
    Status:
    offline
    last visit:
    26.08.04
    Posts:
    26
    I am using this code to sort some of my arrays and I was wondering if someone could help me add it to my pnuserapi.php. Also how would I call it from pnuser.php? It works great puting in side the function but I would like to keep everything compliant if possible. Thanks in advance.

    Code
    //Array Sort Function from http://www.zend.com/codex.php?id=501&single=1
    //metanurb
    function aasort(&$array, $args) {
        foreach($args as $arg) {
            $order_field = substr($arg, 1, strlen($arg));
            foreach($array as $array_row) {
                $sort_array[$order_field][] = $array_row[$order_field];
            }
            $sort_rule .= '$sort_array['.$order_field.'], '.($arg[0] == "+" ? SORT_ASC : SORT_DESC).',';
        }
        eval ("array_multisort($sort_rule".' &$array);');
    }
    /*
    Syntax: aasort($assoc_array, array("+first_key", "-second_key", etc..));

    Example: aasort($db_array, array("+ID", "-AGE", "+NAME&quot icon_wink );
    Where the "+" in front of the keys stands for "ASC" and "-" for "DESC".
    */
  • Link to this postingPosted: 15.05.2003, 13:31
    Profile Homepage
    markwest
    rank:
    Moderator Moderator
    registered:
     March 2002
    Status:
    offline
    last visit:
    26.08.08
    Posts:
    7720
    Functions is the userapi need to be named <module>_userapi_function. The function then takes an arguments array.

    So the function looks like
    Code
    function <yourmodulename_userapi_yourfunctioname($args)
    {
        // Get arguments from argument array - all arguments to this function
        // should be obtained from the $args array, getting them from other
        // places such as the environment is not allowed, as that makes
        // assumptions that will not hold in future versions of PostNuke
        extract($args);

    ......

    }


    The function is then called using a call to pnModAPIFunc('modulename', 'type', 'func', argsarray) having first loaded the user API

    loading and calling a function is

    Code
    ......
        // Load API.  All of the actual work for the creation of the new item is
        // done within the API, so we need to load that in before we can do
        // anything.  If the API fails to load an appropriate error message is
        // posted and the function returns
        if (!pnModAPILoad('module', type')) {
            pnSessionSetVar('
    errormsg', _LOADFAILED);
            return $output->GetOutput();
        }

        // call the apu function
        $mid = pnModAPIFunc('
    module',
                            '
    type',
                            '
    function',
                            array('
    parameter1' => $paramter1,
                                   '
    parameter2' => $paramter2,
                                   etc..... ));
    .......


    -Mark</module>
  • Link to this postingPosted: 15.05.2003, 15:07
    Profile Homepage
    the-ermine
    rank:
    Freshman Freshman
    registered:
     January 2003
    Status:
    offline
    last visit:
    26.08.04
    Posts:
    26
    Hey mark, been a while.. I understand the format it should be in. I just can get the original function converted over and call it properly.. Probably figure it out like always right after I send this post off.. Thanks again.
  • Link to this postingPosted: 15.05.2003, 15:38
    Profile Homepage
    markwest
    rank:
    Moderator Moderator
    registered:
     March 2002
    Status:
    offline
    last visit:
    26.08.08
    Posts:
    7720
    In which case it would be something like

    Code
    function <module>_userapi_aasort($args)
    {
        $returnarray = array();
        foreach($args as $arg) {
            $order_field = substr($arg, 1, strlen($arg));
            foreach($array as $array_row) {
                $sort_array[$order_field][] = $array_row[$order_field];
            }
            $sort_rule .= '$sort_array['.$order_field.'], '.($arg[0] == "+" ? SORT_ASC : SORT_DESC).',';
        }
        eval ("array_multisort($sort_rule".' $returnarray);');

        return $returnarray;
    }


    -Mark
  • Link to this postingPosted: 15.05.2003, 18:47
    Profile Homepage
    the-ermine
    rank:
    Freshman Freshman
    registered:
     January 2003
    Status:
    offline
    last visit:
    26.08.04
    Posts:
    26
    Thanks once again but I'm having a heck of a time getting the arguments over to the API. The function is looking for $array and $args. I have tried

    Code
    $returnarray = pnModAPIFunc('Wdpool',
                            'user',
                            'aasort',
                            array('array' => $fortiebreaking,
                                   'args' => $sortbys));


    I have also tried changing the function to look for $sortby instead of $args and I still get Invalid argument supplied for foreach()
    That is for the frist foreach that looks for the $args or $sortby array.
  • Link to this postingPosted: 15.05.2003, 19:04
    Profile Homepage
    the-ermine
    rank:
    Freshman Freshman
    registered:
     January 2003
    Status:
    offline
    last visit:
    26.08.04
    Posts:
    26
    I got it working. Thanks again for your help Mark.. Here is what I got incase anyone else was following this thread. I wish I would have found this function months ago, would have saved me a lot of time and trouble with sorting these arrays..

    Code
    $sortbys=array("-field1", "+field5");

        // call the apu function
        $returnarray = pnModAPIFunc('modname',
                            'user',
                            'aasort',
                            array('array' => $arraytosort,
                                   'args' => $sortbys));


    Code
    function modname_userapi_aasort($args)
    {
    extract($args);
        $returnarray = $array;
        foreach($args as $arg) {
            $order_field = substr($arg, 1, strlen($arg));
            foreach($array as $array_row) {
                $sort_array[$order_field][] = $array_row[$order_field];
            }
            $sort_rule .= '$sort_array['.$order_field.'], '.($arg[0] == "+" ? SORT_ASC : SORT_DESC).',';
        }
      //  eval ("array_multisort($sort_rule".' $returnarray);');
    eval ("array_multisort($sort_rule".' &$returnarray);');
        return $returnarray;
    }

Start ::  Extensions and Distributions Support ::  Modules ::  How to make this function API compliant.

Main Menu

Extensions Database

Documentation

Development

Login





 


 Log in Problems?
 New User? Sign Up!

Donate to Zikula