Question

I'm attempting to write a plugin for my website that runs on the Ushahidi mapping platform.

Ushahidi uses Kohana. The version I'm running on uses Kohana 2.

The majority of pages on the website are called report pages. Here is an example one:

https://trashswag.com/reports/view/1106

This page pulls a lot of data from the database. For example, the report title "Coffee Table" is populated and must be done so from the database.

My goal is to add a meta description tag to each report page:

<meta name="description" content=[report_title]."at".[report_location] />

All of the data I am seeking exists in a table MyDatabase.incident

First step is report_title. Each report page should dynamically pull in it's own report title from the database and add it tot he meta description tag.

The view that generates the html of each report page is called detail.php. Detail.php must already pull int he report title for it appears on the page (see example website link above). I scrolled through detail.php to find out whereabouts and how the platform did this. I found this on line 17:

<h1 class="report-title"><?php echo html::escape($incident_title);

Here is a gist of detail.php: https://gist.github.com/anonymous/a2cbf012c7abcfee694e

This snippet above must add the existing report title that is displayed near the top of the page to the left just under the main nav.

I would like to grab that same content - "Coffee Table" and add it to my meta tag. I'm doing this via a plugin I am attempting to build. The plugin is added via a hook and if I try to simply echo $incident_title it is not defined since, presumably, it's in a different script.

I cannot see or understand how the Ushahidi platform, which runs on Kohana2, manages to set this variable.

In trial and error I managed to get the following snippet to extract the report title of the first report in the database:

$incititle = ORM::factory('incident',1)->incident_title;

The thing that I need to change is the number 1. For example, the link I provided above should populate with:

$incititle = ORM::factory('incident',1106)->incident_title;

How would I edit this snippet of code so that it dynamically adds the incident id for each report page:

$incititle = ORM::factory('incident',[What goes here?])->incident_title;
Was it helpful?

Solution

Kohana 2 is to old, so I can't find doc, but you need param id from request. For Ko3 it be:

// Can be used anywhere:
$id = Request::instance()->param('id');
// From within a controller:
$this->request->param('key_name');

Id should be the number form https://trashswag.com/reports/view/1106. It's usualy id, but sometimes developers change name this variable... Check it in routings rules.

So in your plugin, you should first retrieve the appropriate record, and then assign a value to a View. The fastest (though not the safest) way would be to assign global.

$incident = ORM::factory('incident',Request::instance()->param('id'));
View:set_global('incident_title',$incident->incident_title);
View:set_global('report_title',$incident->report_title);
View:set_global('report_location',$incident->report_location);
//Uou have meta in `incident_meta` column?
//View:set_global('incident_meta',$incident->incident_meta);

And now you can use $report_location, etc... variables in you Views

If you don't have columns report_location, etc in incident table - you need get this values from right table. Show structure...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top