OJS OCS OMP OHS

You are viewing the PKP Support Forum | PKP Home Wiki



feedback? plugin to link journals across locales

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

Moderators: jmacgreg, btbell, michael, bdgregg, barbarah, asmecher

Forum rules
Developer Resources:

Documentation: The OJS Technical Reference and the OJS API Reference are both available from the OJS Documentation page.

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 OJS Editorial Support and Discussion subforum; if you have a technical support question, try the OJS Technical Support subforum.

feedback? plugin to link journals across locales

Postby samuelwan » Tue Jan 06, 2009 3:24 pm

I don't have a lot of experience with PHP and I'm new to the OJS framework (which is very nice, btw), so I'm posting my solution here with the hope that someone can suggest to me a better approach, or save themselves some time.

I've created a plugin that generates a horizontal nav menu linking to multiple journals in an OJS installation. In my situation, each journal has a different primary locale. When I visit a non-English journal, the API doesn't easily allow you to retrieve titles for all other journals. The API tries to retrieve each journal's title based on each journal's primary locale, but the title will not appear if the journal hasn't defined a title for all locales, which is prone to human error. For example, if the current journal has a Portuguese primary locale, the title of a journal with Spanish primary locale cannot be retrieved through the API if the user has not defined the Portuguese journal's title in the Spanish locale (or any other locale).

I began by studying the Journal.inc.php and JournalDAO.inc.php files in /classes/journal/. After some frustration with how these classes force all requests through localization (which hides data from other locales), I realized that I would have to create a plugin that duplicates the procedure WITHOUT locale filtering.

Step 1) Journal::getJournals() function does not work for multiple languages
First, my plugin had to recreate the function "&_returnJournalFromRow" from /classes/journal/JournalDAO.inc.php for the *sole* purpose of commenting out the $journal->setPrimaryLocale() call. That setPrimaryLocale function was the culprit which filtered out all other journals, and I'm still not sure why.

Step 2) Journal::getJournalTitle() function does not work for multiple languages
Once I retrieved a list of all enabled journals, I had to work around the Journal::getJournalTitle(). That function will not retrieve a title for journals that haven't defined titles in the current journal's primary locale. Unfortunately, the path of API calls lead to a core DataObject.getData function which ultimately filters by locales, so there's no clean way to bypass this. My solution was to get the journal's raw settings via journal->getSettings(), convert all the locale titles to a numeric array, and retrieve the first index by default.

Code: Select all
import('classes.plugins.GenericPlugin');
class JournalsNavBarPlugin extends GenericPlugin {
...
        // Register the plugin for a custom hook that's called by /templates/common/header.tpl
        function register($category, $path) {
                if (parent::register($category, $path)) {
                        if ( $this->getEnabled() ) {
                                HookRegistry::register('Templates::Common::Header::JournalsNa$
                        }
                        $this->addLocaleData();
                        return true;
                }
                return false;
        }
...
        // Modified code copied from the "&getJournals" function in /classes/journal/JournalDAO.inc.php
        function callback($hookName, &$args) {
                $templateMgr = &TemplateManager::getManager();
                $journalDao = &DAORegistry::getDAO('JournalDAO');
                $result = &$journalDao->retrieve(
                        'SELECT * FROM journals WHERE enabled=1 ORDER BY seq'
                );
                $journals = &new DAOResultFactory($result, $this, 'createJournalFromRow');
                $journalLinks = array();
                while ($journal =& $journals->next()) {
                        // Get raw journal settings and then get the first locale's title
                        $settings = $journal->getSettings();
                        $titleArray = array_values($settings["title"]); // convert to numerically indexed array
                        array_push($journalLinks, array(
                                "title" => $titleArray[0],
                                "url"  => $journal->getPath()
                        ));
                        unset($journal);
                }
                $templateMgr->assign_by_ref('journalLinks', $journalLinks);
                $templateMgr->display($this->getTemplatePath() . 'journalsNavBar.tpl');
        }
...
      // A copy of "&_returnJournalFromRow" function from /classes/journal/JournalDAO.inc.php
      // Note: I had to specifically comment out the setPrimaryLocale call because it filtered out all other journals
         function createJournalFromRow(&$row) {
                $journal = &new Journal();
                $journal->setJournalId($row['journal_id']);
                $journal->setPath($row['path']);
                $journal->setSequence($row['seq']);
                $journal->setEnabled($row['enabled']);
                // $journal->setPrimaryLocale($row['primary_locale']);

                // HookRegistry::call('JournalDAO::_returnJournalFromRow', array(&$journal, &$

                return $journal;
        }


My plugin template simply relies on the {section} tag to loop through an array of titles and urls. I took this approach because couldn't figure out how to reinsert locale-safe titles into the Journals iterator, and don't know enough about Smarty templating to modify the {url} tag:

Code: Select all
<div id="journals_navbar" style="padding-left:10%">
<ul class="menu">
        {section name=link loop=$journalLinks}
                <li>
                        <a href="{url page="index" op=$journalLinks[link].url}">{$journalLink$
                </li>
        {/section}
</ul>
</div>


I've only been working with OJS for a couple of days, and I'm sort of new to PHP, so any feedback or suggestions would be much appreciated.
samuelwan
 
Posts: 7
Joined: Mon Jan 05, 2009 9:11 am

Return to OJS Development

Who is online

Users browsing this forum: Google [Bot] and 1 guest