OJS OCS OMP OHS

You are viewing the PKP Support Forum | PKP Home Wiki



Creating a Block

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.

Creating a Block

Postby spatialguru » Wed Jun 11, 2008 10:55 am

Following from this thread I want to make my first sidebar block for OCS.

I've got the basics of the block up and running, templates, formatting, etc. but I'm at a complete loss for how to get at the database. I see other blocks don't use DAO like other parts of the system. Alec mentioned the use of hooks earlier, can anyone give me a pointer as to how to understand this? To keep it simple, all I want is access to the sched_conf_settings table. Is that readily accessible somehow?

Hopefully this thread can encourage others to make their own blocks and plugins too!
spatialguru
 
Posts: 112
Joined: Mon Jun 25, 2007 1:23 pm
Location: British Columbia, Canada

Re: Creating a Block

Postby asmecher » Wed Jun 11, 2008 11:34 am

Hi Tyler,

Actually, using DAOs from plugins is fine -- you can use them just like elsewhere in the system.

Regards,
Alec Smecher
Public Knowledge Project Team
asmecher
 
Posts: 8581
Joined: Wed Aug 10, 2005 12:56 pm

Re: Creating a Block

Postby mcrider » Wed Jun 11, 2008 11:48 am

Tyler,
You can access data in the sched_conf_settings table by doing something like this:
Code: Select all
$schedConf = Request::getSchedConf();
$schedConf->getSetting('closeCommentsDate');

Where the parameter in the getSetting function can be any setting_name from the table.

Hope this helps,
Matt
mcrider
 
Posts: 952
Joined: Mon May 05, 2008 10:29 am
Location: Vancouver, BC

Re: Creating a Block

Postby spatialguru » Tue Jun 17, 2008 4:11 pm

mcrider wrote:Tyler,
You can access data in the sched_conf_settings table by doing something like this:
Code: Select all
$schedConf = Request::getSchedConf();
$schedConf->getSetting('closeCommentsDate');


I'm getting closer, thanks for the comment. Unfortunately it seems I need to learn how to handle arrays a bit better. Even the setting example above seems to return an array for me. Hmmm...

I want to get the $schedConf->getSetting('sponsors') values and then loop through them.

Anymore pointers? :) I'm starting to repeat my mistakes over and over.. heh.
spatialguru
 
Posts: 112
Joined: Mon Jun 25, 2007 1:23 pm
Location: British Columbia, Canada

Re: Creating a Block

Postby asmecher » Tue Jun 17, 2008 4:55 pm

Hi Tyler,

Even the closeCommentsDate comes back as an array? If so, that would be news to me -- however, if you mean the sponsors setting, that one should be fetched using the getLocalizedSetting function. This is because sponsors can be entered in multiple languages.

FYI, the print_r function is useful for debugging. Give it an array and it'll dump out the structure of its contents, regardless of whether it's a string, array, object, etc.

Regards,
Alec Smecher
Public Knowledge Project Team
asmecher
 
Posts: 8581
Joined: Wed Aug 10, 2005 12:56 pm

Re: Creating a Block

Postby spatialguru » Tue Jun 17, 2008 11:41 pm

Thanks for the tips. I'm feeling pretty close now, but still need to find a way to access the contents of the array. print_r didn't tell me anything. Here's my code so far:

SponsorsBlockPlugin.inc.php:
Code: Select all
...
function getContents() {
                $schedConf = Request::getSchedConf();
                $sponsors = $schedConf->getLocalizedSetting('sponsors');
                print_r($sponsors);  // prints nothing, but does when accessing other settings
                return $sponsors;
         }
...


I picked up the getContents approach from some other example. Should I just be doing all of this in my block.tpl instead?

If I had the variable returning properly I know my next challenge would be to iterate through the attributes of the "sponsors" settings array. Here's all I have so far:
Code: Select all
<div class="block" id="sidebarSponsors" style="text-align:center">
        <div class="blockTitle" id="sponsors">{translate key="about.sponsors"}</div>
</div>


I'm not keen on templating yet, so I guess it's where I'm stuck. I've seen two approaches, one loading up the .tpl files and the other focusing on the .inc.php - which approach should I likely need?

Anything jumping out at you guys?

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

Re: Creating a Block

Postby mcrider » Wed Jun 18, 2008 12:29 am

Hi Tyler,

Everywhere else in OCS any request for the sponsors setting uses getSetting(), i.e. not localized. Could you try the same code again with getSetting('sponsors') instead of getLocalizedSetting('sponsors') and see if print_r produces anything? And are you sure you have values in the sched_conf_settings table to pull this data from?

Cheers,
Matt
mcrider
 
Posts: 952
Joined: Mon May 05, 2008 10:29 am
Location: Vancouver, BC

Re: Creating a Block

Postby spatialguru » Wed Jun 18, 2008 7:19 am

Matt,
Yes that worked for me - thanks. I'd swear I tried it before, but hey, I won't complain :)

print_r defintely output the data I have in the db as an array now - great! Now I'm just trying to figure out how to process these items in the block.tpl file. Is using the getContents function in SponsorsBlockPlugin.inc.php the right way to go? It seems that the template prints whatever is returned from it - in this case just the object type "array" is printed.

Here is the block.tpl I'm playing with, copied from the templates/about/conferenceSponsorship.tpl:
Code: Select all
<div class="block" id="sidebarSponsors" style="text-align:center">
        <div class="blockTitle" id="sponsors">{translate key="about.sponsors"}
       {foreach from=$sponsors item=sponsor}
        {if $sponsor.institution}
                {if $sponsor.url}
                        <li><a href="{$sponsor.url|escape}">{$sponsor.institution|escape}</a></li>
                {else}
                        <li>{$sponsor.institution|escape}</li>
                {/if}
                {if $sponsor.logourl}
                        <li style="list-style-type:none"><a href="{$sponsor.url|escape}"><img src="{$sponsor.logourl|escape}"></a></li>
                {/if}
        {/if}
        {/foreach}
</div>
</div>


Basically I want to iterate through what the getContents() returned - but it looks like $sponsor variable doesn't make it back to the template? It simply prints "array".

Getting close, I can feel it! ;)
spatialguru
 
Posts: 112
Joined: Mon Jun 25, 2007 1:23 pm
Location: British Columbia, Canada

One solution...

Postby spatialguru » Wed Jun 18, 2008 8:56 am

So I thought I'd try to do all the processing outside of the template and only pass the final html to the template. I don't think this is ideal, but here's what worked for me. It feels very hackish - so comments welcome!!

I assume I could have assigned my $sponsors array back to the template somehow instead?

Code: Select all
        function getContents() {
                $schedConf = Request::getSchedConf();
                $sponsors = $schedConf->getSetting('sponsors');
                //$templateMgr->assign('sponsors', $sponsors);
                foreach ($sponsors as $sponsor)
                        if ($sponsor['logourl'])
                                $sponsorstrings = $sponsorstrings . '<a href="' . $sponsor['url'] . '"><img src="' . $sponsor['logourl'] . '" width="100%"></a><br>';
                return $sponsorstrings;
         }
Last edited by spatialguru on Wed Jun 18, 2008 9:12 am, edited 1 time in total.
spatialguru
 
Posts: 112
Joined: Mon Jun 25, 2007 1:23 pm
Location: British Columbia, Canada

Re: Creating a Block

Postby asmecher » Wed Jun 18, 2008 9:05 am

Hi Tyler,

A cleaner way to do this would be to use Smarty's {foreach ...} tag (http://www.smarty.net/manual/en/language.function.foreach.php) to do the loop in the template itself. That way you could keep all the HTML there.

Regards,
Alec Smecher
Public Knowledge Project Team
asmecher
 
Posts: 8581
Joined: Wed Aug 10, 2005 12:56 pm

Re: Creating a Block

Postby spatialguru » Wed Jun 18, 2008 9:21 am

That's what I'd like to do, but I can't seem to get the $sponsors array back to the template. It's my total ignorance, but that's where I'm stuck.
spatialguru
 
Posts: 112
Joined: Mon Jun 25, 2007 1:23 pm
Location: British Columbia, Canada

Re: Creating a Block

Postby asmecher » Wed Jun 18, 2008 9:37 am

Hi Tyler,

Yes -- you can use something like this in the block code:
Code: Select all
function getContents() {
        $schedConf =& Request::getSchedConf();
        $sponsors = $schedConf->getSetting('sponsors');
        $templateMgr =& TemplateManager::getManager();
        $templateMgr->assign('sponsors', $sponsors);
        return parent::getContents();
}
I haven't tested this, but it should be OK.

Regards,
Alec Smecher
Public Knowledge Project Team
asmecher
 
Posts: 8581
Joined: Wed Aug 10, 2005 12:56 pm

Re: Creating a Block

Postby spatialguru » Wed Jun 18, 2008 10:23 am

asmecher wrote:
Code: Select all
return parent::getContents();
I haven't tested this, but it should be OK.

Almost perfect - good job! :)

I had to pass it the template mgr too:
Code: Select all
return parent::getContents($templateMgr);


It seems to be basically working - will now develop the template side and report back.

Thanks a lot guys!
spatialguru
 
Posts: 112
Joined: Mon Jun 25, 2007 1:23 pm
Location: British Columbia, Canada

Re: Creating a Block

Postby asmecher » Wed Jun 18, 2008 10:42 am

Hi Tyler,

Ah -- you're also missing the &$templateMgr parameter from the getContents function definition. You can get rid of the TemplateManager::getManager call once you've added this.

Regards,
Alec Smecher
Public Knowledge Project Team
asmecher
 
Posts: 8581
Joined: Wed Aug 10, 2005 12:56 pm

Re: Creating a Block

Postby spatialguru » Wed Jun 18, 2008 10:49 am

Okay, all done now! Thanks a lot. To sum up - here is the final template and PHP code I've got running. See top of this post for the other details.

SponsorsBlockPlugin.inc.php
Code: Select all
...
        function getContents(&$templateMgr) {
                $schedConf = Request::getSchedConf();
                $sponsors = $schedConf->getSetting('sponsors');
                $templateMgr->assign_by_ref('sponsors', $sponsors);
                return parent::getContents($templateMgr);
         }
...


block.tpl
Code: Select all
<div class="block" id="sidebarSponsors" style="text-align:center">
        <div class="blockTitle" id="sponsors" style="text-align:center">{translate key="about.sponsors"}
        {foreach from=$sponsors item=sponsor}
        {if $sponsor.institution}
                {if $sponsor.logourl}
                        <a href="{$sponsor.url|escape}"><img src="{$sponsor.logourl|escape}" width="100%"></a>
                {else}
                        {if $sponsor.url}
                        <a href="{$sponsor.url|escape}">{$sponsor.institution|escape}</a>       
                        {/if}
                {/if}
        {/if}
        {/foreach}
        </div>
</div>


Next I'll want to add another variable to the sponsor settings so I can order the sponsors according to a sponsorship level. That should be fun...

Thanks again - this was a great intro for me to learn and the results look nice too. I'll provide a URL when our site upgrade is completed.

Update: see next page for a screenshot example
Tyler
Last edited by spatialguru on Wed Jun 18, 2008 10:57 am, edited 1 time in total.
spatialguru
 
Posts: 112
Joined: Mon Jun 25, 2007 1:23 pm
Location: British Columbia, Canada

Next

Return to OCS Development

Who is online

Users browsing this forum: No registered users and 1 guest