Guides:Organizing Content
From Kaltura Wiki
Contents |
[edit] Why should I use a gallery?
There are many ways to view the content uploaded to Kaltura. The most common is to use a gallery of thumbnails that represent a list of the videos. Because there might be a large amount of videos to display, it is common to filter the list by a specific criteria and add the ability to scroll through the set of thumbnails. Clicking the thumbnails can navigate to a Kaltura video player page to display the video.
There are several possibilities for criteria, such as:
- Filter by a specific user – this is used in user profile pages.
- Filter by tags – if tagged properly, the videos can be found using keywords.
- Filter by date – can view all videos from a given month.
Even after choosing the criteria, there can still be many videos to display.
There are 3 important parameters that can help ensure optimal usability:
- Size of the gallery “page” – number of thumbnails displayed per page
- The gallery page number (starting at 1)
- The order of the results – for example, thumbnails are displayed in a descending order by views – the most viewed will be displayed first.
The solution combines 3 elements:
- Establish a ks (Kaltura session)
- Retrieving the data from the Kaltura system according to the criteria, page size, page number and order. This will be done using the KalturaClient with the listEntries service.
- Display the result using HTML and JavaScript.
[edit] PHP Code Example
There is some JavaScript that should include a function that handles the click of the thumbnail -
<script type="text/javascript" >
function entryClicked ( entry_id )
{
window.location = "./player.php?entry_id=" + entry_id ;
}
</script>
The location is a page on your site that can handle this entry_id (most probably use a Kaltura player to play it).
For the PHP part to work - be sure to include the kaltura_client.php:
require_once("kaltura_client.php");
The PHP part of the code will establish a ks (kaltura session), retrieve the data according to the chosen criteria and finally display it in an HTML table.
[edit] Establish a Kaltura Session (KS)
Bellow is a helpful function that initializes the configuration with data from the partner. It can be placed in a helper file (call it kaltura_helper.php) that will be included from the gallery page.
function kaltura_init_config ()
{
global $partner_id,$subp_id ,$secret ,$admin_secret , $service_url ;
$conf = new KalturaConfiguration( $partner_id , $subp_id );
$conf->partnerId = $partner_id;
$conf->subPartnerId = $subp_id;
$conf->secret = $secret;
$conf->adminSecret = $admin_secret;
$conf->serviceUrl = ”http://www.kaltura.com”;
$conf->setLogger( new KalturaDemoLogger());
return $conf;
}
Code in the gallery page:
$conf = kaltura_init_config(); $user = new KalturaSessionUser(); $user->userId = "1"; $cl = new KalturaClient($conf); $res =$cl->startAdmin($user, $conf->adminSecret , null); $ks = $cl->getKs();
The parameter ks will hold a string that represents a Kaltura session – an essential part of calling the services.
Read more about the Kaltura Session.
[edit] Retrieve Data
// create a filter to define what exactly we want to be in the list $filter = new KalturaEntryFilter (); $filter->inMediaType = "1,2,5,6"; // allow clips of mediaType 1=video, 2=images, 5=audio, 6=roughcuts. Separate the choice with ',' and no spaces // order the results by the creation data descending $filter->orderBy = KalturaEntryFilter::ORDER_BY_CREATED_AT_DESC; // or ascending : // $filter->orderBy = KalturaEntryFilter::ORDER_BY_CREATED_AT_ASC; $page = $_REQUEST[‘page’]; // read the current page from the request. // choose the page_size to be some number that will fit the area you would like to display the thumbnails gallery $page_size = 20; // page=1 is the first page if ( $page < 1 ) $page = 1; $detailed = false; $res = $cl->listentries ( $user , $filter , $detailed , $page_size , $page ); $count = @$res["result"]["count"]; $entries = @$res["result"]["entries"]; if ( ! $entries ) $entries =array();
[edit] Display an HTML table
A helper function should be used for creating the pager for the gallery. It can be placed in the helper file described above and included in the gallery page.
function kaltura_createPagerForGallery ( $p , $current_page , $page_size , $count ,$js_callback_paging_clicked )
{
$p = (int)$p;
$a = $p * $page_size + 1;
$b = (($p+1) * $page_size) ;
$b = min ( $b , $count ) ;// don't let the page-end be bigger than the total count
$page_to_goto = $p + 1;
if ( $page_to_goto == $current_page )
{
$str = "[<a title='{$page_to_goto}' href='javascript:{$js_callback_paging_clicked} ($page_to_goto)'>{$a}-{$b}</a>] ";
// $str = "[{$a}-{$b}] ";
}
else
$str = "<a title='{$page_to_goto}' href='javascript:{$js_callback_paging_clicked} ($page_to_goto)'>{$a}-{$b}</a> ";
return $str;
}
The code in the gallery page:
// set the number of images in a single row of the table
$images_in_row = 5;
$pager_string = "";
$start_page = max ( 1 , $page - 5 );
$very_last_page = (int)($count / $page_size);
$end_page = min ( $very_last_page , $start_page + 10 );
for ( $p=$start_page ; $p < $end_page ; $p++)
{
$pager_string .= kaltura_createPagerForGallery ( $p , $page , $page_size , $count , "entryClicked");
}
$before_page_string = "";
$after_page_string = "";
// add page 0 if not in list
if ( $start_page > 0 ) $before_page_string .= kaltura_createPagerForGallery ( 0, $page ,
$page_size , $count , "entryClicked" ) ;
// have some dots if there is a real gap between 0 and the rest
if ( $start_page > 1 ) $before_page_string .= "..."; if ( $end_page < $very_last_page -1 ) $after_page_string .= "...";
//add last page if lot in list
if ( $end_page < $very_last_page ) $after_page_string .= kaltura_createPagerForGallery ( $very_last_page , $page , $page_size, $count, "entryClicked");
// combine all pager strings into one
$pager_string = "total count [ " . $count . "] " . $before_page_string . $pager_string . $after_page_string;
$gallery_html = "<table><tr>";
$i=0;
foreach ( $entries as $entry )
{
$name = $entry["name"];
$type = $entry["type"];
$id = $entry["id"];
$kshow_id = $entry["kshowId"];
$display = $entry['thumbnailUrl'] ?
"<img width='120' height='90' src='" . $entry['thumbnailUrl'] . "' title='" .
$id . " ". $name . "' >" :
"<div>” . $id . " ". $name . "</div>”;
// create a link to the player for this specific entry. entryClicked is the name of the javascript function to call.
$gallery_html .= "<td style='overflow:hidden; vertical-align:top ; style='width:130px; height:100px;'><a href='javascript:entryClicked (\"$kshow_id\" , \"$id\" , $page )'>{$display}</a>" .
"<br>$name" .
"</td>";
++$i;
if ( $i % $images_in_row == 0 ) $gallery_html .= "</tr><tr>";
}
$gallery_html .= "</tr></table>";
// display it all:
echo $pager_string . "<br>" . $gallery_html;

