Friday, October 26, 2012

How to render zoo records easily

Zoo - one of the most popular Joomla extension has a view which shows just a single record, that is of course full view. Zoo doesn't have a view which shows a list of records of particular submission type. So what if you want to show a list of zoo items of particular type in your extension.
You must be thinking what's the big deal, based on submission id just fetch the records & show it how you want. Wait a minute. Its not as easy as you think as Zoo uses JSON method to store record's which is quite difficult to render zoo elements. So its quite complicated to get zoo items details from database by executing queries.
Even if you manage it won't be robust cause to get related records from table you have to deal with hard coded 36 key string, which zoo uses as field identifier. So lets get away from this hassle.
Here is the snippet to render the details of zoo records quite easily.

$zapp = App::getInstance('zoo');
$items = $app->table->item->all(array('conditions' => 'id = '.$Zoo_Item_id));
//$Zoo_Item_id must be zoo item id of which you want to render details

foreach($items as $item)
{       
   foreach ($item->getElements() as $id => $element)
   {
      echo $element->render();
} }
Here you can see how we are showing Zoo related records in table. We have added link for full view of respective Zoo Item as well.

Sunday, October 7, 2012

How to construct link to edit single zoo record


I'm writing this post because its quite tricky to create a link which edits a zoo record based on zoo item id unless you know how exactly zoo construct hashes.
When we asked on zoo forum they didn't reply with the following solution. I must admit that zoo component is excellent but their support is very poor & you know zoo support is paid! Okay leave it & lets move to the link.
Stop thinking that I'll pass zoo id to the zoo's my submission menu link to create edit record link. What?? have you tried it?? Does it says Hashes did not match. Alright Don't worry you are the right place.
Its very easy, you just need to pass submission type of zoo item & zoo item id itself to the getSubmissionHash API which returns you respective hash for the item.

$zapp = App::getInstance('zoo');
$items = $zapp->table->item->all(array('conditions' => 'id =$zoo_item_id)); 
//$zoo_item_id must be zoo record id which you want to edit.
$type = $items[$item->id]->type;
// get submission type of item
$hashRelated = $zapp->submission->getSubmissionHash(1,$type, $item->id); 
 // Here is the exact hash for the record

$href = JRoute::_(JURI::Base()."index.php?option=com_zoo&view=submission&layout=submission&submission_id=1&type_id=$type&item_id=$item->id&submission_hash=$hashRelated"); // Construct link as zoo 

echo '<a href=".$href.">Edit Record</a>';