OJS OCS OMP OHS

You are viewing the PKP Support Forum | PKP Home Wiki



Sorting arrays

OCS development discussion, enhancement requests, third-party patches and plug-ins.

Moderators: jmacgreg, michael

Forum rules
The Public Knowledge Project Support Forum is moving to http://forum.pkp.sfu.ca

This forum will be maintained permanently as an archived historical resource, but all new questions should be added to the new forum. Questions will no longer be monitored on this old forum after March 30, 2015.

Sorting arrays

Postby spatialguru » Wed Jun 18, 2008 4:01 pm

Two sorting questions here... one on presentation and one on saving to the db.

I'm familiar with how to do sorts at a DAO level, but in some cases it seems I might need to sort in the template side of things.

I'm trying to reorder sched_conf_setting->sponsors based on one key in the sponsors array. This is so one on the organizingTeam page it will list sponsors in a particular order using an existing attribute in the array. I can't find where the array is past from PHP to the template - so I can't intercept it there.

Following from the above, is there a way to do the sorting in the template itself?

Finally, I want to reorder the array in the database to, so that the step1 settings form shows the sponsors in a certain order too. Likewise here I can't find how/where/when to apply a reorder in PHP.

Any tips on any of these? I've added a rank/order attribute to the step1 settings now and am anxious to try it out :)

Tyler
spatialguru
 
Posts: 112
Joined: Mon Jun 25, 2007 1:23 pm
Location: British Columbia, Canada

Re: Sorting arrays

Postby spatialguru » Wed Jun 18, 2008 5:09 pm

I've got the presentation side figured out (but not the saving of settings).

Here's what I did - please jump in an correct me if there is a better way.

Create smarty modifier:
/lib/smarty/plugins/modifier.sortby.php

Code: Select all
<?php
#
# sorts an array of named arrays by the supplied fields
#   code by dholmes at jccc d0t net
#   taken from http://au.php.net/function.uasort
# modified by cablehead, messju and pscs at http://www.phpinsider.com/smarty-forum

function array_sort_by_fields(&$data, $sortby){
      static $sort_funcs = array();

    if (empty($sort_funcs[$sortby]))
    {
        $code = "\$c=0;";
        foreach (split(',', $sortby) as $key)
        {
           $d = '1';
              if (substr($key, 0, 1) == '-')
              {
                 $d = '-1';
                 $key = substr($key, 1);
              }
              if (substr($key, 0, 1) == '#')
              {
                 $key = substr($key, 1);
               $code .= "if ( ( \$c = (\$a['$key'] - \$b['$key'])) != 0 ) return $d * \$c;\n";
              }
              else
              {
               $code .= "if ( (\$c = strcasecmp(\$a['$key'],\$b['$key'])) != 0 ) return $d * \$c;\n";
            }
        }
        $code .= 'return $c;';
        $sort_func = $sort_funcs[$sortby] = create_function('$a, $b', $code);
    }
    else
    {
        $sort_func = $sort_funcs[$sortby];
    }
    uasort($data, $sort_func);
}

#
# Modifier: sortby - allows arrays of named arrays to be sorted by a given field
#
function smarty_modifier_sortby($arrData,$sortfields) {
   array_sort_by_fields($arrData,$sortfields);
   return $arrData;
}

$smarty->register_modifier( "sortby", "smarty_modifier_sortby" );
?>


Then a minor modification in my template code adding the sortby function and the key name to sort with.
block.tpl:
Code: Select all
...
        {foreach from=$sponsors|@sortby:rank item=sponsor}
...


Worked like a charm!

Next I want to use the same approach but have it sort the sponsor form fields in manager/schedConfSetup/1 - but this is more challenging for me to understand. I'd like to save and load the values from the db using the sort of the "rank" field.

Any tips on how I can intercept at this part?

Tyler
spatialguru
 
Posts: 112
Joined: Mon Jun 25, 2007 1:23 pm
Location: British Columbia, Canada


Return to OCS Development

Who is online

Users browsing this forum: No registered users and 0 guests