[ Index ]
 

Code source de vtiger CRM 5.0.2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/modules/Import/ -> parse_utils.php (source)

   1  <?php
   2  /*********************************************************************************
   3   * The contents of this file are subject to the SugarCRM Public License Version 1.1.2
   4   * ("License"); You may not use this file except in compliance with the
   5   * License. You may obtain a copy of the License at http://www.sugarcrm.com/SPL
   6   * Software distributed under the License is distributed on an  "AS IS"  basis,
   7   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
   8   * the specific language governing rights and limitations under the License.
   9   * The Original Code is:  SugarCRM Open Source
  10   * The Initial Developer of the Original Code is SugarCRM, Inc.
  11   * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.;
  12   * All Rights Reserved.
  13   * Contributor(s): ______________________________________.
  14   ********************************************************************************/
  15  
  16  // takes a string and parses it into one record per line,
  17  // one vtiger_field per delimiter, to a maximum number of lines
  18  // some vtiger_files have a header, some dont.
  19  // keeps track of which vtiger_fields are used
  20  
  21  /**    function used to parse the file 
  22   *    @param string $file_name - file name 
  23   *    @param character $delimiter - delimiter of the csv file
  24   *    @param int $max_lines - maximum number of lines to parse
  25   *    @param int $has_header - if the file has header then 1 otherwise 0
  26   *    @return array $ret_array - return an array which will be "rows"=>&$rows, "field_count"=>$field_count where as &rows is the reference of rows which contains all the parsed rows and $field_count is the number of fields available per row
  27   */
  28  function parse_import($file_name,$delimiter,$max_lines,$has_header)
  29  {
  30      $line_count = 0;
  31  
  32      $field_count = 0;
  33  
  34      $rows = array();
  35  
  36      if (! file_exists($file_name))
  37      {
  38          return -1;
  39      }
  40  
  41      $fh = fopen($file_name,"r");
  42  
  43      if (! $fh)
  44      {
  45          return -1;
  46      }
  47  
  48      while ( (( $fields = fgetcsv($fh, 4096, $delimiter) ) !== FALSE) 
  49          && ( $max_lines == -1 || $line_count < $max_lines)) 
  50      {
  51  
  52          if ( count($fields) == 1 && isset($fields[0]) && $fields[0] == '')
  53          {
  54              break;
  55          }
  56          $this_field_count = count($fields);
  57  
  58          if ( $this_field_count > $field_count)
  59          {
  60              $field_count = $this_field_count;
  61          }
  62  
  63          array_push($rows,$fields);
  64  
  65          $line_count++;
  66  
  67      }
  68  
  69      // got no rows
  70      if ( count($rows) == 0)
  71      {
  72          return -3;
  73      }
  74  
  75      $ret_array = array(
  76          "rows"=>&$rows,
  77          "field_count"=>$field_count
  78      );
  79  
  80      return $ret_array;
  81  
  82  }
  83  
  84  /**    function used to parse the act file 
  85   *    @param string $file_name - file name 
  86   *    @param character $delimiter - delimiter of the csv file
  87   *    @param int $max_lines - maximum number of lines to parse
  88   *    @param int $has_header - if the file has header then 1 otherwise 0
  89   *    @return array $ret_array - return an array which will be "rows"=>&$rows, "field_count"=>$field_count where as &rows is the reference of rows which contains all the parsed rows and $field_count is the number of fields available per row
  90   */
  91  function parse_import_act($file_name,$delimiter,$max_lines,$has_header)
  92  {
  93      $line_count = 0;
  94  
  95      $field_count = 0;
  96  
  97      $rows = array();
  98  
  99      if (! file_exists($file_name))
 100      {
 101          return -1;
 102      }
 103  
 104      $fh = fopen($file_name,"r");
 105  
 106      if (! $fh)
 107      {
 108          return -1;
 109      }
 110  
 111      while ( ($line = fgets($fh, 4096))
 112                  && ( $max_lines == -1 || $line_count < $max_lines) )
 113  
 114      {
 115          
 116          $line = trim($line);
 117          $line = substr_replace($line,"",0,1);
 118          $line = substr_replace($line,"",-1);
 119          $fields = explode("\",\"",$line);
 120  
 121          $this_field_count = count($fields);
 122  
 123          if ( $this_field_count > $field_count)
 124          {
 125              $field_count = $this_field_count;
 126          }
 127  
 128          array_push($rows,$fields);
 129  
 130          $line_count++;
 131  
 132      }
 133  
 134      // got no rows
 135      if ( count($rows) == 0)
 136      {
 137          return -3;
 138      }
 139  
 140      $ret_array = array(
 141          "rows"=>&$rows,
 142          "field_count"=>$field_count
 143      );
 144  
 145      return $ret_array;
 146  
 147  }
 148  ?>


Généré le : Sun Feb 25 10:22:19 2007 par Balluche grâce à PHPXref 0.7