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
Developer Resources:

Documentation: The OJS Technical Reference and the OJS API Reference are both available from the OJS Documentation page. While these are OJS-specific, the OCS codebase is similar enough to OJS they should be of help. There is also an [url=http://pkp.sfu.ca/ocs_documentation[/url]OCS Documentation[/url] page with some more general documentation that might also be of interest.

Git: You can access our public Git Repository here. Comprehensive Git usage instructions are available on the wiki.

Bugzilla: You can access our Bugzilla report tracker here.

Search: You can use our Google Custom Search to search across our main website, the support forum, and Bugzilla.

Questions and discussion are welcome, but if you have a workflow or usability question you should probably post to the OCS Conference Support and Discussion subforum; if you have a technical support question, try the OCS Technical Support subforum.

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 1 guest

cron