Friday, August 30, 2013

Live site - Forget the fear & work with full freedom


We work on live site which must be using by many users of the site over the world wide. Many times problem occurs only on a live site so in such cases you can't do anything on your working instance & you want to debug only on live site to fix the issue. There are many CMS like Joomla, Drupal which easily allows debug or error reporting mode. However this is not just enough in many situation. There is possibility of site getting crashed if you have done any hacks or some patch work & this fails when you only want to see the errors or print any query & die statement.
Yesterday i was suffering from same problem. There was a Joomla site which was live & there were many users online & have to look the code by trial & error basis like print some queries & execute die statement. Simple & sweet idea came in my mind & i just added a parameter like testuser=atpatil in url & based on this parameter debug the code by executing die statement. This won't affect to anyone besides me. In php you can add following code.
if($_GET['testuser'] == "atpatil")
{
echo 'Write or echo anything you want to solve your issue here';
die;
}
When i show the exact problem on live site to my boss. He said, "How can you print query & such coding stuff on live site." When I told about this simple trick he just laughed. Just try this simple trick to debug the code & let me know whether it helps. You can use console.log as well but this blog post is all about die statement.

Sunday, August 4, 2013

How to copy & delete Zoo Items programmatically?

Usually we have to allow Joomla front-end users to delete or copy Zoo Items. However Zoo lacks this functionality.

How to delete zoo records programmatically.

Y'day wanted to build this functionality in one of the Zoo based project. I could delete Zoo Items in few minutes by executing some delete queries on zoo related tables but decided to dig Zoo API. Why to take initiatives if Zoo API handles very effectively. Instead of writing 20-30 lines code, you can finish it in 3-4 lines using Zoo API & its very easy.
$Zoo_Item_Id = 29029; // Zoo Item id which you want to remove.
$app = App::getInstance('zoo'); // Define Zoo app instance.
$item = $app->table->item->get($Zoo_Item_Id); // Get the info of $Zoo_Item_Id
$app->table->item->delete($item); // Removes the $Zoo_Item_Id record from all respective zoo tables.

How to copy Zoo Items Programmatically.

Now lets see how to copy zoo items programmatically. Copying zoo item is not as easy as deleting records. You should have all the details including zoo category of the zoo item which you wanna copy. First you should have all data of a zoo record. We have get item API which gives zoo item's data & getRelatedCategoryIds which tells the relation of a zoo item to the categories.
Lets get the zoo records data first.

$user  = JFactory::getUser(); // Get the current user info.
$app  = App::getInstance('zoo'); // Define Zoo Instance. 
$now   = $app->date->create()->toMySQL(); // Get Current date
$item  = $app->table->item->get($zoo_id); // Get Zoo Item info
$categories = $item->getRelatedCategoryIds(); // Get the Zoo category info
Now We have all the information of Zoo Item that we wanna copy.

$item->id = 0; // set id to 0, to force new item
$item->state = 0; // Set the state of Zoo Item. 1 - publish 0 - Unpublish 
$item->alias = $app->alias->item->getUniqueAlias($zoo_id, 'copy-'.$item->alias); 
// Alias should be unique for each Zoo Item.
$item->name  .= ' ('.JText::_('Copy').')'; // set copied name
$item->created   = $item->modified = $now; // Created & Modified date
$item->created_by  = $item->modified_by = $user->id;
$item->hits   = 0;
$item->setTags($app->table->tag->getItemTags($zoo_id)); // copy tags

$app->table->item->save($item); // Save $item in zoo_item table

$app->category->saveCategoryItemRelations($item->id, $categories);
// Save the category relation in zoo_category table.