Monday, December 3, 2012

Importing all fields using Zoo CSV Importer


I don't know why Zoo skips radio, dropdown fields when it comes to CSV importer/Exporter.
We have used Zoo in our one of the project where we wanted to import records having almost everything from text to radio n dropdown even Related Items Pro fields as well. Lets check how to allow zoo to import Radio, Dropdown first. You should read this blog only if you want to hack some zoo files. You can't override these files.
You have to hack just two files which are as follows

Step 1 : Show all fields in dropdown of fields.
Go to Your Site/administrator/components/com_zoo/views/configuration/tmpl/importcsv.php
Add following the code jQuery snippet.


$('.type').change(function()
{
   
   var type = $(this).val();
   
   jQuery.ajax
   ({ 
   
    type : 'POST',
                url: 'index.php?option=com_import&task=get_cdts_fields&type='+type, 
                dataType : 'json',
                cache : 'false',
                success: function(data)
                {
   if(data)
   {
           $(".assign").append('<optgroup label="'+type+'" id="extra_fields">');
           for (i=0;i<data.length;i++)
           {
             var newarray = data[i].split(",");
             $(".assign").append(new Option(newarray[0], newarray[1])); 
           }
          }
          }
               
    });
   
   
});

Now paste the following function into your custom components controller as it is.
function get_cdts_fields()
{
  
 $config_type = JRequest::getVar('type');
 $file = file_get_contents(JPATH_SITE.DS.'media/zoo/applications/blog/types/'.$config_type.'.config');
 $decode = json_decode($file,true);
   
 $Pro_Fields = array();
 $i=0;
 foreach($decode['elements'] as $k=>$ele)
 {
  if($ele['type'] == "select" || $ele['type'] == "imagepro" || $ele['type'] == "relateditemspro" || $ele['type'] =="radio")
  {
   $Pro_Fields[$i] = $ele['name'].','.$k;
   $i++;
  }
 }
  
 echo json_encode($Pro_Fields); 
 jexit();

}

You can see all fields under selected type option group only if you done step first correctly.
Atlast you can see all fields as option in dropdown. So that you can map fields easily.
Step - II Tell zoo to bind all fields like radio, select etc
Open Your Site/administrator/components/com_zoo/helpers/import.php file & add following cases besides other cases in importCSV function. Make sure you have copy of original file.

case 'select':
case 'radio':
   $ele = array('option'=>array('0' => $data[$column]));
   $elements[$assignment]->bindData($ele);
 
break;


//For Image Pro you can add following case if your using imagepro field

case 'imagepro':
                 
$element_data = array();
$images = explode('|',$data[$column]); // We have used Pipe(|) to separate images from one another.
$i = 0;
foreach($images as $img)
{
 $img_ele[] = array('file'=>$img, 
      'title' => '', 
      'file2' => '',
      'spotlight_effect' => '', 
      'caption' => ''
      );
 $i++;
}
$elements[$assignment]->bindData($img_ele);
break; 



We have imported Related Pro fields as well but its completely hard-coded & client requirement dependent.

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>';

Thursday, July 19, 2012

Facebook Comments For Blogspot

My colleague had written some code so that his blog visitors can add comments on his blog post using Facebook account. He told me the procedure but it was not handy cause at the end of every blog post need to paste some code. Y'day at noon i just got bored from daily work & thought of fb commenting came in my mind & searched for something which allows to comment on blog post using Facebook easily. Many times i messed cause nothing was straight forward. Finally i gone through many blogs & done it.
I'm writing this blog post in a simple & easiest way so that bloggers will get it. I have explained in simple words for non developers cause real bloggers are non-developers, i don't mind if developers also check & implement.
Relax guys..! its an easy..! not a big deal. Please go through the following 3 straight forward steps if you want to allow your blog visitors to comment using Facebook account. After all its good idea to let your blog visitors comment using facebook cause now a days every one using fb daily & therefore your blog will get popular on facebook.
1) Do you know how to create facebook app..?
You know then just create a new app & skip this stage. open this link. Click on Create New App button, a popup will appear. Write app name like amol's blog & continue. Enter correct captach, a form will appear. Add blogspot.com infront of 'Add Domains' textbox. Click on 'Website with Facebook Login', Enter your blogs link in 'Site URL' textbox & save changes.
Copy App ID like this 330763300338577.
2) Add meta Tag Click on templates
Click on templates -> Edit HTML. Add following <meta content='YOUR_FACEBOOK_APPLICATION_ID' property='fb:app_id'/>. Replace YOUR_FACEBOOK_APPLICATION_ID with your App id. E.g <meta content='241008529352104' property='fb:app_id'/> Check this image.

3) Add Facebook Comment
Click on templates -> Edit HTML. Find this code <div class='post-footer-line post-footer-line-3'> in template. (Use the keyboard shortcut Ctrl + F). What you didn't find above code. Okay check Expand Widget Templates & again find it. This time you will get it easily. Paste the following code exactly below the above line.

<b:if cond='data:blog.pageType == "item"'>
<div id="fb-root"></div>
<script>
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>
<fb:comments width='450' colorscheme='light' expr:title='data:post.title' expr:href='data:post.canonicalUrl' expr:xid='data:post.id'/> </b:if>
You can change width & colorscheme for facebook comment box.

Wednesday, May 2, 2012

How to create logout link in Joomla


How to create logout link in Joomla 1.7
Joomla 1.7 Logout link
Its very simple to create logout link in joomla 1.7. You can create it in your extensions as shown below.
<?php 
$token = JUtility::getToken();
$linkout = JRoute::_("index.php?option=com_users&task=user.logout&".$token."=1");
?>
<a href="<?php echo $linkout; ?>"> Logout </a>
How to redirect user on a particular link after logout in Joomla 1.7?
If you want to redirect Joomla user on a link not on home page then use the following code.
<?php
$url = JRoute::_(JURI::Base().'index.php?option=com_users'); // here i'm redirecting to login view.
?>
<a href="index.php?option=com_users&task=user.logout&<?php echo JUtility::getToken(); ?>=1&return=<?php echo base64_encode($url); ?>">Logout</a>
How to create logout menu item for Joomla 1.7 ?
To create a menu-item for logout in Joomla 1.7. You should add task in either your custom component.
Paste this code in your components controller file. Add a new menu item as Sytem link, external url & add your task url in link. like index.php?option=com_YOUR_COMPONENT_NAME&task=logout
function logout()
{
$app = JFactory::getApplication();
$user = JFactory::getUser();
$user_id = $user->get($user->id);
$app->logout($user_id, array());
$app->redirect(JURI::base()); // you can redirect anywhere
}
However you achive this by using plugin.

Wednesday, April 11, 2012

How to delete Joomla users programmatically

You want to delete Joomla users pro-grammatically (in any extensions like component). Leave it dude its easy just get user id & execute delete query on users & user_usergroup_map tables. Thats it. Okay you are right but don't you like to reduce the code? Just pass userid to getInstance & call delete method, that's it. You can see following function i have written for delete joomla 1.7 users.

function deleteuser()
{
$userid = JRequest::getInt('id'); // getting user id from url
$instance = JUser::getInstance($userid);
if($userid)
{
if($instance->delete())
{
return true;
}
}
}

Hope it will help you. However this is working fine for Joomla 1.7 . If you have any other ways you are welcome.

Friday, March 2, 2012

Infinite loop detected in JError - Joomla

Y'day i was moving my Joomla 1.7 local site on live server. Everything was going correct & when i opened my site i got this error "Infinite loop detected in JError". I haven't done anything wrong then why this error ocurred. Same thing i tried one more time but same problem.
I googled this error. Somewhere told that it was hosting problem so call hosting support & they will solve this problem & somewhere was go to such n such line number in Joomla folder n replace those lines with these n all. Somewhere told that you have entered wrong database either username, password or database name in configuration file, but i have entered correct database details & checked 10 times whether i did any mistake.
At last decided to write to hosting support team. It was not their problem then how are they going to solve it. While waiting reply from hosting i checked whether i assigned correct user to correct database. Opened Cpanel & assigned correctly & it worked hushhh....
Finally the coclusion is that if you are getting "Infinite loop detected in JError" error. Then just check whether you have assigned corret user to correct database. That was it. I am glad that i solved this error myself. I am sharing this through my blog cause i didn't get correct answer & i'll be happy if this help you.

Wednesday, February 8, 2012

True Joomla Related Article module

Recently i wanted a module which shows list of related articles. You will say "ohh..! no problem you will get it easily from joomla extensions." Yeah thats right but lets clear what exactly 'related article' means. Ok related article means those articles which are from same categories. Correct, isn't it? That is what my requirement was. So i didn't get any module which fullfil my 'Related Articles' defination. So created 'True Related Articles' module for Joomla 1.7 which does what i explained. Based on the article id from url it pulls list of articles from same category. If you want it you can download here.

Sunday, January 22, 2012

User - Domain Restriction for Joomla and Jomsocial

Recently I was asked to create a plugin which allows new user to register with specific list of email-domains. For e.g. i want to allow to register only those users who have gmail addresses.
I was not aware of whether the site is using Jomsocial.
So created two plugins one user plugin for Joomla registration & community plugin for Jomsoical.
Added snippet for jomsocial on onRegisterValidate event & for joomla its onBeforeStoreUser.
Compared entered email domain with the list of email-domains configuration parameter of the plugin.
I was thinking of creating just a plugin instead of two plugin but didn't get jomsocial data on joomla's onBeforeStoreUser event. That's why created two plugin.
You can download these plugins here for Jomsocial & Joomla