This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

Export CSV without HTML code

Ask help about existing plugins, or find advice about the way to build up your own.

Export CSV without HTML code

by josvenes » Tue Mar 18, 2014 5:00 pm

I have a plug for csv export
The export works well but some html codes are been exported, like the <br> tag.
a) Is there a general reason why this could be.
b) Is it possible to in the action.class.php whats the fault in the code

I appreciate any help on this!

2 code blocks: action.class.php / indexSucces.php (definition of exportfields)

Code: Select all
<?php

class csvExportActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->materials_types = Doctrine_Core::getTable('MaterialsTypes')->find($request->getParameter('materials_types_id'));
   
    $this->materials_fields = Doctrine_Core::getTable('MaterialsFields')
      ->createQuery()         
      ->addWhere('materials_types_id = ?',$request->getParameter('materials_types_id'))
      ->orderBy('form_tab_id ASC, form_fieldset_id ASC,form_sort_order ASC,name ASC')     
      ->execute();
   
   
    if ($request->isMethod('post') and $request->hasParameter('selected_items_list'))
    {
      $field_separator = $request->getParameter('field_separator',"\t");
      $file_extension = $request->getParameter('file_extension','.csv');
      $file_name = $request->getParameter('file_name');
         
         header("Content-type: Application/octet-stream");     
         header("Content-disposition: attachment; filename=" . $file_name . "." . $file_extension);
         header("Pragma: no-cache");
         header("Expires: 0");
         
      $selected_items_list = explode(',',$request->getParameter('selected_items_list'));                       
     
      $fields = $request->getParameter('fields', array());
      $internal_fields = $request->getParameter('internal_fields', array());
      $fields_block_heading = $request->getParameter('fields_block_heading', array());
               
      $materials_fields  =  MaterialsFields::getFieldsListByMaterialsTypeId($request->getParameter('materials_types_id'), $this->getUser());       
      $fields_list_values_schema = MaterialsFieldsList::getFieldsListValuesSchema($materials_fields);           
      $users_schema = Users::getUsersSchema();           
           
      $content = '';
     
      if(in_array('id',$internal_fields))
        $content = I18NText::__('ID') . $field_separator;
     
      foreach($fields as $fields_id)
      {
        $f = Doctrine_Core::getTable('MaterialsFields')->find($fields_id);
                       
        if(isset($fields_block_heading[$f->getFormTabId()][$f->getFormFieldsetId()]))
        {                   
          $block_heading = $fields_block_heading[$f->getFormTabId()][$f->getFormFieldsetId()];                   
        }
        else
        {
          $block_heading = '';
        }               
       
        $content .= str_replace(array("\n\r","\r","\n",$field_separator),' ',$block_heading . ' '. $f->getName()) . $field_separator;
      }
                 
      if(in_array('parent_materials_types',$internal_fields))
      {
        $parent_materials_types = Doctrine_Core::getTable('MaterialsTypes')->find($this->materials_types->getParentId());
        $content .= $parent_materials_types->getName() . $field_separator;
      }           
                 
      if(in_array('created_by',$internal_fields))
        $content .= I18NText::__('Created by') . $field_separator; 
     
      if(in_array('date_added',$internal_fields))
        $content .= I18NText::__('Date added') . $field_separator;
     
      if(in_array('url',$internal_fields))
        $content .= I18NText::__('Url');
       
      $content .= "\n";
           
      if($file_extension=='.csv')
      {
        echo chr( 0xFF ) . chr( 0xFE ) . mb_convert_encoding( $content, 'UTF-16LE', 'UTF-8' );
      }
      else
      {
        echo $content;
      }
     
      $fields_sum_list = array();
                                                                 
      foreach($selected_items_list as $materials_id)
      {
        $content = '';
         
        if(in_array('id',$internal_fields)) 
         $content .= $materials_id . $field_separator;
       
       
        $value_schema = MaterialsFieldsValues::getFieldValuesSchema($materials_id);
         
        foreach($fields as $fields_id)
        {
           $f = Doctrine_Core::getTable('MaterialsFields')->find($fields_id);
                                               
           if(isset($value_schema[$f->getId()]))
           {
             $value = $value_schema[$f->getId()];         
           }
           else
           {
             $value = '';
           }
           
          //calculating fields sum
          if(in_array($f->getType(),array('input_numeric','input_numeric_sum')))
          {
            if(isset($fields_sum_list[$f->getId()]))
            {
              $fields_sum_list[$f->getId()] += $value;                     
            }
            else
            {
              $fields_sum_list[$f->getId()] = $value;
            }
          }
          else
          {
            $fields_sum_list[$f->getId()] = '';
          }
                     
           $content .=  str_replace(array("\n\r","\r","\n",$field_separator),' ',MaterialsFields::renderMaterialsFieldsValue($value,$f->getType(),'value',$fields_list_values_schema, $users_schema)) . $field_separator;           
         }
         
         $m = Doctrine_Core::getTable('Materials')->find($materials_id);                 
           
         if(in_array('parent_materials_types',$internal_fields))
         {
          $pm = Doctrine_Core::getTable('Materials')->find($m->getParentMaterialsId());
          $pf = MaterialsFields::getHeadingField($pm->getMaterialsTypes()->getId());
         
     
          if(is_object($pf))
          {         
            $content .= MaterialsFields::renderMaterialsFieldsValue(MaterialsFieldsValues::getFieldValue($pf->getId(),$m->getParentMaterialsId()),$pf->getType()) . $field_separator;
          }
          else
          {
            $content .= $field_separator;
          }
         } 
           
         if(in_array('created_by',$internal_fields)) 
           $content .= Users::getUserBySchema($m->getUsersId(),$users_schema) . $field_separator;
           
         if(in_array('date_added',$internal_fields)) 
           $content .= dateController::formatDate($m->getDateAdded()) . $field_separator; 
         
         if(in_array('url',$internal_fields))
          $content .= $this->getController()->genUrl('materialsInfo/index?materials_id=' . $materials_id . '&materials_types_id=' . $request->getParameter('materials_types_id') . '&parent_materials_id=' . $request->getParameter('parent_materials_id'), true);
         
         $content .= "\n";
                                   
         if($file_extension=='.csv')
         {
           echo mb_convert_encoding( $content, 'UTF-16LE', 'UTF-8' );
         }
         else
         {
           echo $content;
         }                 
      }
     
      $content = '';
      if(in_array('id',$internal_fields)) 
         $content = '' . $field_separator;
     
      foreach($fields_sum_list as $v)
      {
        if(strlen($v)>0)
        {         
          $content .=  $v . $field_separator;         
        }
        else
        {
          $content .= '' . $field_separator;
        }
      }
     
      $content .= "\n";
     
      if($file_extension=='.csv')
      {
        echo mb_convert_encoding( $content, 'UTF-16LE', 'UTF-8' );
      }
      else
      {
        echo $content;
      }
                                                 
      exit();
    }   
  }



Code: Select all
<h1><?php echo __('CSV Export') ?></h1>
<form id="multiple_edit" action="<?php echo url_for($sf_context->getModuleName() . '/index?materials_types_id=' . $sf_request->getParameter('materials_types_id') . '&parent_materials_id=' . $sf_request->getParameter('parent_materials_id')) ?>" method="post">
<?php echo input_hidden_tag('selected_items_list') ?>

<table class="contentTable">
  <tr>
    <td><?php echo __('Select fields for export') ?></td>
  </tr>
</table>

<br>

<table class="contentTable">
  <tr>
    <td width="10"><input name="internal_fields[]" type="checkbox" id="internal_fields_id" value="id"></td>
    <td><label for="internal_fields_id"><?php echo __('ID') ?></label></td>
  </tr>
</table> 
<?php

$tab_name = '';
$count = 0;

foreach($materials_fields as $f):
$count++;

if($f->getFormFieldsetId())
{
  $ftab_name = MaterialsTypesFormTabs::getNameById($f->getFormFieldsetId());
}
else
{
  $ftab_name = MaterialsTypesFormTabs::getNameById($f->getFormTabId());
}

$tab_id = $f->getFormTabId() . '_' .$f->getFormFieldsetId();

if($tab_name!=$ftab_name)
{
  if($tab_name!='') echo '</table>';

  $tab_name = $ftab_name;
       
   echo '
  <table class="contentTable" style="margin-top: 10px;" id="export_form_tabs_' . $tab_id . '_fields_table">
   <tr>
    <td width="10"><input  type="checkbox" id="multiple_selected_all" value="" onChange="select_all_items(\'export_form_tabs_' . $tab_id . '_fields_table\')"></td>
    <td>
      <label for=""><b>' . $tab_name . '</b></label><br>
      <small><label for="fields_block_heading_' . $f->getFormTabId() . '_' . $f->getFormFieldsetId() . '">' . __('Include group name to fields heading') . '</label></small> <input value="' . addslashes(trim($tab_name)) . '" type="checkbox" name="fields_block_heading[' . $f->getFormTabId() . '][' . $f->getFormFieldsetId() . ']" id="fields_block_heading_' . $f->getFormTabId() . '_' . $f->getFormFieldsetId() . '">
    </td>
  </tr>
  ';
   
}
elseif($ftab_name=='' and $count==1)
{
  echo '
  <table class="contentTable" style="margin-top: 10px;" id="export_form_tabs_' . $tab_id . '_fields_table">
   <tr>
    <td width="10"><input  type="checkbox" id="multiple_selected_all" value="" onChange="select_all_items(\'export_form_tabs_' . $tab_id . '_fields_table\')"></td>
    <td><b>' . __('Select All') . '</b></td>
  </tr>
  ';
}
?>
  <tr>
    <td width="10"><input class="multiple_selected" name="fields[]" type="checkbox" id="field_<?php echo $f->getId() ?>" value="<?php echo $f->getId() ?>" <?php echo ($f->getListingStatus()==1 ? 'checked="checked"':'')?>></td>
    <td><label for="field_<?php echo $f->getId() ?>"><?php echo $f->getName() ?></label></td>
  </tr>
<?php endforeach ?>

<table class="contentTable"  style="margin-top: 10px;">
<?php if($materials_types->getParentId()>0):

$parent_materials_types = Doctrine_Core::getTable('MaterialsTypes')->find($materials_types->getParentId());
?>
  <tr>
    <td width="10"><input name="internal_fields[]" type="checkbox" id="internal_fields_created_by" value="parent_materials_types" checked="checked"></td>
    <td><label for="internal_fields_parent_materials_types"><?php echo $parent_materials_types->getName() ?></label></td>
  </tr>
<?php endif ?>

  <tr>
    <td width="10"><input name="internal_fields[]" type="checkbox" id="internal_fields_created_by" value="created_by" ></td>
    <td><label for="internal_fields_created_by"><?php echo __('Created By') ?></label></td>
  </tr>
  <tr>
    <td width="10"><input name="internal_fields[]" type="checkbox" id="internal_fields_date_added" value="date_added" ></td>
    <td><label for="internal_fields_date_added"><?php echo __('Date added') ?></label></td>
  </tr>
  <tr>
    <td width="10"><input name="internal_fields[]" type="checkbox" id="internal_fields_url" value="url" ></td>
    <td><label for="internal_fields_url"><?php echo __('Item Url') ?></label></td>
  </tr> 
</table>   
<br>

<table class="contentTable" style="width: auto">
  <tr>
    <td><?php echo __('File name') ?></td>
    <td><?php echo input_tag('file_name',$materials_types->getName() . '-' . date('Y-m-d_H-i'),array('size'=>'40')) ?></td>
    <td><?php echo select_tag('file_extension','', array('choices'=> array('.csv'=>'.csv','.txt'=>'.txt')))?></td>
  </tr>
</table>   
<br>


<input type="submit" value="<?php echo __('Export') ?>">
</form>

<script type="text/javascript">   
  $(function() {           
    $('#multiple_edit').submit(function() {
               
      set_selected_items_list();     
                                               
      if($('#selected_items_list').attr('value')=='')
      {
        alert('<?php echo __("Please select items") ?>');
        return false;
      }         
    });
   
    check_set_selected_items_list('<?php echo __("Please select items") ?>');
               
  });
</script>
josvenes
Junior Member
 
Posts: 1
Joined: Tue Mar 18, 2014 4:51 pm