]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Import/CsvAutoDetect.php
Release 6.5.0
[Github/sugarcrm.git] / modules / Import / CsvAutoDetect.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38
39 /*********************************************************************************
40  * Description: Class to detect csv file settings (delimiter, enclosure, etc)
41  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42  * All Rights Reserved.
43  ********************************************************************************/
44
45 /* sample usage
46
47     $auto = new CsvAutoDetect('/tmp/_books.csv', 10);
48
49     $delimiter = $enclosure = $heading = false;
50
51     $ret = $auto->getCsvSettings($delimiter, $enclosure);
52     if ($ret) {
53         echo "found delimiter = ".$delimiter."<br>";
54         echo "found enclosure = ".$enclosure."<br>";
55     } else {
56         echo "couldn't find settings<br>";
57     }
58
59     $ret = $auto->hasHeader($heading);
60     if ($ret) {
61         $header = $heading?'true':'false';
62         echo "found heading = ".$header."<br>";
63     } else {
64         echo "couldn't determine header info<br>";
65     }
66
67     $date_format = $auto->getDateFormat();
68     if ($date_format) {
69         echo "found date format=".$date_format."<br>";
70     } else {
71         echo "couldn't find date format<br>";
72     }
73
74     $time_format = $auto->getTimeFormat();
75     if ($time_format) {
76         echo "found time format=".$time_format."<br>";
77     } else {
78         echo "couldn't find time format<br>";
79     }
80
81 */
82
83 require_once('include/parsecsv.lib.php');
84
85 class CsvAutoDetect {
86
87     protected $_parser = null;
88
89     protected $_csv_file = null;
90
91     protected $_max_depth = 15;
92
93     protected $_parsed = false;
94
95     static protected $_date_formats = array(
96         'm/d/Y' => "/^(0?[1-9]|1[012])\/(0?[1-9]|[12][0-9]|3[01])\/\d\d\d\d/", // 12/23/2010 or 3/23/2010
97         'd/m/Y' => "/^(0?[1-9]|[12][0-9]|3[01])\/(0?[1-9]|1[012])\/\d\d\d\d/", // 23/12/2010 or 23/3/2010
98         'Y/m/d' => "/^\d\d\d\d\/(0?[1-9]|1[012])\/(0?[1-9]|[12][0-9]|3[01])/", // 2010/12/23 or 2010/3/23
99         'm-d-Y' => "/^(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])-\d\d\d\d/", // 12-23-2010 or 3-23-2010
100         'd-m-Y' => "/^(0?[1-9]|[12][0-9]|3[01])-(0?[1-9]|1[012])-\d\d\d\d/", // 23-12-2010 or 23-3-2010
101         'Y-m-d' => "/^\d\d\d\d-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])/", // 2010-12-23 or 2010-3-23
102         'm.d.Y' => "/^(0?[1-9]|1[012])\.(0?[1-9]|[12][0-9]|3[01])\.\d\d\d\d/", // 12.23.2010 or 3.23.2010
103         'd.m.Y' => "/^(0?[1-9]|[12][0-9]|3[01])\.(0?[1-9]|1[012])\.\d\d\d\d/", // 23.12.2010 or 23.3.2010
104         'Y.m.d' => "/^\d\d\d\d\.(0?[1-9]|1[012])\.(0?[1-9]|[12][0-9]|3[01])/", // 2010.12.23 or 2010.3.23
105     );
106
107     static protected $_time_formats =  array(
108         'h:ia' => "/(^| )(0?[0-9]|1[0-2]):(0?[0-9]|[1-5][0-9])(:0?[0-9]|[1-5][0-9])?[am|pm]/", // 11:00pm or 11:00:00pm or 9:3pm
109         'h:iA' => "/(^| )(0?[0-9]|1[0-2]):(0?[0-9]|[1-5][0-9])(:0?[0-9]|[1-5][0-9])?[AM|PM]/", // 11:00PM or 11:00:00PM or 9:3PM
110         'h:i a' => "/(^| )(0?[0-9]|1[0-2]):(0?[0-9]|[1-5][0-9])(:0?[0-9]|[1-5][0-9])? [am|pm]/", // 11:00 pm or 11:00:00 pm or 9:3 pm
111         'h:i A' => "/(^| )(0?[0-9]|1[0-2]):(0?[0-9]|[1-5][0-9])(:0?[0-9]|[1-5][0-9])? [AM|PM]/", // 11:00 PM or 11:00:00 PM or 9:3 PM
112         'H:i' => "/(^| )(0?[0-9]|1[0-9]|2[0-4]):(0?[0-9]|[1-5][0-9])(:0?[0-9]|[1-5][0-9])?/", // 23:00 or 23:00:00 or 9:3
113         'h.ia' => "/(^| )(0?[0-9]|1[0-2])\.(0?[0-9]|[1-5][0-9])(\.0?[0-9]|[1-5][0-9])?[am|pm]/", // 11.00pm or 11.00.00pm or 9.3pm
114         'h.iA' => "/(^| )(0?[0-9]|1[0-2])\.(0?[0-9]|[1-5][0-9])(\.0?[0-9]|[1-5][0-9])?[AM|PM]/", // 11.00PM or 11.00.00PM or 9.3PM
115         'h.i a' => "/(^| )(0?[0-9]|1[0-2])\.(0?[0-9]|[1-5][0-9])(\.0?[0-9]|[1-5][0-9])? [am|pm]/", // 11.00 pm or 11.00.00 pm or 9.3 pm
116         'h.i A' => "/(^| )(0?[0-9]|1[0-2])\.(0?[0-9]|[1-5][0-9])(\.0?[0-9]|[1-5][0-9])? [AM|PM]/", // 11.00 PM or 11.00.00 PM or 9.3 PM
117         'H.i' => "/(^| )(0?[0-9]|1[0-9]|2[0-4])\.(0?[0-9]|[1-5][0-9])(\.0?[0-9]|[1-5][0-9])?/", // 23.00 or 23.00.00 or 9.3
118     );
119
120
121     /**
122      * Constructor
123      *
124      * @param string $csv_filename
125      * @param int $max_depth
126      */
127     public function __construct($csv_filename, $max_depth = 2) {
128         $this->_csv_file = $csv_filename;
129
130         $this->_parser = new parseCSV();
131
132         $this->_parser->auto_depth = $max_depth;
133
134         $this->_max_depth = $max_depth;
135     }
136
137
138
139     /**
140      * To get the possible csv settings (delimiter, enclosure).
141      * This function causes CSV to be parsed.
142      * So call this function before calling others.
143      *
144      * @param string $delimiter
145      * @param string $enclosure
146      * @return bool true if settings are found, false otherwise
147      */
148     public function getCsvSettings(&$delimiter, &$enclosure) {
149         // try parsing the file to find possible delimiter and enclosure
150         $this->_parser->heading = false;
151
152         $found_setting = false;
153
154         $singleQuoteParsedOK = $doubleQuoteParsedOK = false;
155         $beginEndWithSingle = $beginEndWithDouble = false;
156
157         // check double quotes first
158         $depth = 1;
159         $enclosure = "\"";
160         $delimiter1 = $this->_parser->auto($this->_csv_file, true, null, null, $enclosure);
161         if (strlen($delimiter1) == 1) { // this means parsing ok
162             $doubleQuoteParsedOK = true;
163             // sometimes it parses ok with either single quote or double quote as enclosure
164             // so we need to make sure the data do not begin and end with the other enclosure
165             foreach ($this->_parser->data as &$row) {
166                 foreach ($row as &$data) {
167                     $len = strlen($data);
168                     // check if it begins and ends with single quotes
169                     // if it does, then it double quotes may not be the enclosure
170                     if ($len>=2 && $data[0] == "'" && $data[$len-1] == "'") {
171                         $beginEndWithSingle = true;
172                         break;
173                     }
174                 }
175                 if ($beginEndWithSingle) {
176                     break;
177                 }
178                 $depth++;
179                 if ($depth > $this->_max_depth) {
180                     break;
181                 }
182             }
183             if (!$beginEndWithSingle) {
184                 $delimiter = $delimiter1;
185                 $found_setting = true;
186             }
187         }
188
189         // check single quotes
190         if (!$found_setting) {
191             $depth = 1;
192             $enclosure = "'";
193             $delimiter2 = $this->_parser->auto($this->_csv_file, true, null, null, $enclosure);
194             if (strlen($delimiter2) == 1) { // this means parsing ok
195                 $singleQuoteParsedOK = true;
196                 foreach ($this->_parser->data as &$row) {
197                     foreach ($row as &$data) {
198                         $len = strlen($data);
199                         // check if it begins and ends with double quotes
200                         // if it does, then it single quotes may not be the enclosure
201                         if ($len>=2 && $data[0] == "\"" && $data[$len-1] == "\"") {
202                             $beginEndWithDouble = true;
203                             break;
204                         }
205                     }
206                     if ($beginEndWithDouble) {
207                         break;
208                     }
209                     $depth++;
210                     if ($depth > $this->_max_depth) {
211                         break;
212                     }
213                 }
214                 if (!$beginEndWithDouble) {
215                     $delimiter = $delimiter2;
216                     $found_setting = true;
217                 }
218             }
219         }
220
221         if (!$found_setting) {
222             // we don't seem to have a perfect enclosure candidate
223             // let's pick one of the possible candidates
224             if ($doubleQuoteParsedOK) {
225                 // if double quotes parsed ok, let's take that
226                 $delimiter = $delimiter1;
227                 $enclosure = "\"";
228                 $found_setting = true;
229             } else if ($singleQuoteParsedOK) {
230                 // otherwise, if single quote parsed ok, let's use it
231                 $delimiter = $delimiter2;
232                 $enclosure = "'";
233                 $found_setting = true;
234             }
235         }
236
237         if (!$found_setting) {
238             return false;
239         }
240
241         $this->_parsed = true;
242
243         return true;
244     }
245
246     /**
247      * To check CSV heading
248      *
249      * @param bool $heading true of it has header, false if not
250      * @return bool true if header is found, false if error
251      */
252     public function hasHeader(&$heading, $module) {
253
254         if (!$this->_parsed) {
255             return false;
256         }
257
258         $total_count = count($this->_parser->data[0]);
259         if ($total_count == 0) {
260             return false;
261         }
262
263         if (!isset($GLOBALS['beanList'][$module])) {
264             return false;
265         }
266
267         $bean = new $GLOBALS['beanList'][$module]();
268
269         $match_count = 0;
270
271         $mod_strings = return_module_language($GLOBALS['current_language'], $module);
272
273         // process only the first row
274         foreach ($this->_parser->data[0] as $val) {
275
276             // bug51433 - everything relies on $val having a value so if it's empty,
277             // we can skip this iteration and not get warnings
278             if( !empty( $val ) )
279             {
280                 foreach ($bean->field_defs as $field_name=>$defs) {
281
282                     // check if the CSV item matches field name
283                     if (!strcasecmp($val, $field_name)) {
284                         $match_count++;
285                         break;
286                     }
287                     // check if the CSV item is part of the label or vice versa
288                     else if (isset($defs['vname']) && isset($mod_strings[$defs['vname']])) {
289                         if (stripos(trim($mod_strings[$defs['vname']],':'), $val) !== false || stripos($val, trim($mod_strings[$defs['vname']],':')) !== false) {
290                             $match_count++;
291                             break;
292                         }
293                     }
294                     else if (isset($defs['vname']) && isset($GLOBALS['app_strings'][$defs['vname']])) {
295                         if (stripos(trim($GLOBALS['app_strings'][$defs['vname']],':'), $val) !== false || stripos($val, trim($GLOBALS['app_strings'][$defs['vname']],':')) !== false) {
296                             $match_count++;
297                             break;
298                         }
299                     }
300                 }
301             }
302         }
303
304         // if more than 50% matched, consider it a header
305         if ($match_count/$total_count >= 0.5) {
306             $heading = true;
307         } else {
308             $heading = false;
309         }
310
311         return true;
312     }
313
314
315     /**
316      * To get the possible format (for date or time)
317      *
318      * @param array $formats
319      * @return mixed possible format if found, false otherwise
320      */
321     protected function getFormat(&$formats) {
322
323         if (!$this->_parsed) {
324             return false;
325         }
326
327         $depth = 1;
328
329         foreach ($this->_parser->data as $row) {
330
331             foreach ($row as $val) {
332
333                 foreach ($formats as $format=>$regex) {
334
335                     $ret = preg_match($regex, $val);
336                     if ($ret) {
337                         return $format;
338                     }
339                 }
340             }
341
342             // give up if reaching max depth
343             $depth++;
344             if ($depth > $this->_max_depth) {
345                 break;
346             }
347         }
348
349         return false;
350     }
351
352
353     /**
354      * To get the possible date format used in the csv file
355      *
356      * @return mixed possible date format if found, false otherwise
357      */
358     public function getDateFormat() {
359
360         $format = $this->getFormat(self::$_date_formats);
361
362         return $format;
363     }
364
365
366     /**
367      * To get the possible time format used in the csv file
368      *
369      * @return mixed possible time format if found, false otherwise
370      */
371     public function getTimeFormat() {
372
373         $format = $this->getFormat(self::$_time_formats);
374
375         return $format;
376     }
377
378 }
379 ?>