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.

10 comments:

  1. very very cool, thank you

    ReplyDelete
  2. Your Welcome Alex...

    ReplyDelete
  3. Sure Mihail.. Let me know whether it works for you.

    ReplyDelete
  4. Hi, can you (or someone of you guys) help me to locate where to insert the function get_cdts_fields? What does "customer component controler" exactly stand for?

    ReplyDelete
  5. "Customer component controller" means Joomla components developed by you not Joomla native components. If your site contains only native Joomla components & you can go for com_users component file like your site/components/com_user/controller.php. Make sure you change the path as well.

    ReplyDelete
  6. Hi Amol,

    I've tried implementing this code but to no avail have I
    succeeded. I know Yootheme implemented code to do this in one of the
    updates to ZOO, but I am running a Joomla site v1.5. Would you perhaps
    know which file is responsible for showing the drop down elements? I tried inserting your code into site/components/com_user/controller.php as suggested but nothing happened... Any assistance would be greatly appreciated. Love your blog!

    ReplyDelete
  7. Dear jkMan,
    What you have tried yet? Inserting code into users controller is not enough you need to follow the steps as i told above. Let me know what exactly the problem you're getting.

    ReplyDelete
  8. To resolve the previous issue I had, I found an updated version of ZOO and inserted the new code into the old code for the import.php file. The issue I now have is that the 'select' elements are not automatically assigned to their options in the blog items.
    To elaborate, if a column of data is uploaded as a 'select' type, when looking at the blog items, the elements are all set to the default '-Select X-' option. What could cause this?

    ReplyDelete
  9. wally domingo tucent ramirezNovember 25, 2014 at 5:20 PM

    can you help me i want so badly to import related items!
    will this post help, i dont know nothing about code....

    ReplyDelete