PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/camila/db/import.inc.php

https://github.com/umbecr/camilaframework
PHP | 227 lines | 204 code | 7 blank | 16 comment | 0 complexity | 56b50d48da3f008859cdc3cd6d0c8905 MD5 | raw file
  1. <?php
  2. /* This File is part of Camila PHP Framework
  3. Copyright (C) 2006-2011 Umberto Bresciani
  4. Camila PHP Framework is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. Camila PHP Framework is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with Camila PHP Framework; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  15. require_once(CAMILA_LIB_DIR . '/csv_bv/class.csv_bv.php');
  16. require_once(CAMILA_DIR.'datagrid/configurator.class.php');
  17. function CSV_import($file, $table, $db) {
  18. $result = Array();
  19. $csv = new csv_bv($file, ',', '"' , '\\');
  20. $csv->SkipEmptyRows(true);
  21. // Remove leading and trailing \s and \t.
  22. $csv->TrimFields(false);
  23. $header = $csv->NextLine();
  24. $rs = $db->Execute('select * from ' . $table);
  25. if (!$rs) {
  26. $result['result'] = 0; //failure
  27. $result['error'] = $db->ErrorMsg();
  28. } else {
  29. $fcount = 0;
  30. $icount = 0;
  31. $metatypes = Array();
  32. $xml_parser = xml_parser_create();
  33. if (($fp = fopen(str_replace('.csv', '.xml', $file), 'r'))) {
  34. $data = fread($fp, filesize($file));
  35. fclose($fp);
  36. xml_parse_into_struct($xml_parser, $data, $vals, $index);
  37. xml_parser_free($xml_parser);
  38. if (is_array($index['FIELD']))
  39. foreach ($index['FIELD'] as $value) {
  40. $metatypes[$vals[$value]['attributes']['NAME']]=$vals[$value]['attributes']['TYPE'];
  41. }
  42. } else {
  43. for ($i = 0; $i < $rs->FieldCount(); $i++) {
  44. $curr_field = $rs->FetchField($i);
  45. $curr = $curr_field->name;
  46. $type = $rs->MetaType($curr_field->type);
  47. $metatypes[$curr]=$type;
  48. }
  49. }
  50. $m = 3;//camila_get_translation('camila.dateformat.monthpos');
  51. $d = 0;//camila_get_translation('camila.dateformat.daypos');
  52. $y = 6;//camila_get_translation('camila.dateformat.yearpos');
  53. while ($arr_data = $csv->NextLine()){
  54. $record = array();
  55. $count = 0;
  56. foreach ($header as $value) {
  57. if ($metatypes[$value] == 'D') {
  58. if ($arr_data[$count] != '')
  59. $record[$value] = $db->BindDate(substr($arr_data[$count],$y,4).'-'.substr($arr_data[$count],$m,2).'-'.substr($arr_data[$count],$d,2));
  60. } else
  61. $record[$value] = $arr_data[$count];
  62. $count++;
  63. }
  64. $insertSQL = $db->GetInsertSQL($rs, $record);
  65. $icount++;
  66. $rs2 = $db->Execute($insertSQL); # Insert the record into the database
  67. if (!$rs2)
  68. $fcount++;
  69. }
  70. if ($fcount>0) {
  71. $result['result'] = 1; //errors
  72. $result['processed'] = $icount;
  73. $result['failed'] = $fcount;
  74. $result['skipped'] = $csv->SkippedRowCount();
  75. } else {
  76. $result['result'] = 2; //success
  77. $result['processed'] = $icount;
  78. $result['failed'] = $fcount;
  79. $result['skipped'] = $csv->SkippedRowCount();
  80. }
  81. }
  82. return $result;
  83. }
  84. function XLS_import($file, $table, $db) {
  85. $result = Array();
  86. $configurator = new configurator();
  87. $configurator->filename = $file;
  88. $configurator->sheetnum = 0;
  89. $configurator->interactive = false;
  90. $db->SetFetchMode(ADODB_FETCH_ASSOC);
  91. $configurator->db = $db;
  92. $filename=basename($file);
  93. $sequence = intval(substr($filename,0,strpos($filename,'_')));
  94. $id = $configurator->xls_read($sequence);
  95. $result['result'] = 2; //success
  96. $result['processed'] = 1;
  97. $result['failed'] = 0;
  98. return $result;
  99. }
  100. function XLS_import2($file, $table, $db) {
  101. $filename=basename($file);
  102. $sequence = intval(substr($filename,0,strpos($filename,'_')));
  103. $res = $db->Execute('select id from ' . CAMILA_TABLE_WORKT . ' where filename='.$db->qstr($filename));
  104. if ($res === false)
  105. camila_error_page(camila_get_translation('camila.sqlerror') . ' ' . $db->ErrorMsg());
  106. $id = $res->fields['id'];
  107. $result = Array();
  108. $configurator = new configurator();
  109. $configurator->filename = $file;
  110. $configurator->sheetnum = 0;
  111. $configurator->interactive = false;
  112. $db->SetFetchMode(ADODB_FETCH_ASSOC);
  113. $configurator->db = $db;
  114. $configurator->configure_table($id, false);
  115. $record = Array();
  116. $res = $db->Execute('select * from ' . CAMILA_TABLE_WORKT . ' where id='.$db->qstr($id));
  117. if ($res === false)
  118. camila_error_page(camila_get_translation('camila.sqlerror') . ' ' . $db->ErrorMsg());
  119. $record['short_title'] = $res->fields['short_title'];
  120. $record['full_title'] = $res->fields['full_title'];
  121. $record['label_order']=$sequence;
  122. $scriptname = $res->fields['scriptname'];
  123. $category = $res->fields['category'];
  124. $updateSQL = $db->AutoExecute(CAMILA_TABLE_PLANG, $record, 'UPDATE', 'page_url='.$db->qstr($scriptname) . ' and lang='.$db->qstr($_REQUEST['lang']));
  125. if (!$updateSQL) {
  126. camila_information_text(camila_get_translation('camila.worktable.db.error'));
  127. }
  128. if (trim($category) != '')
  129. {
  130. $res = $db->Execute('select page_url from ' . CAMILA_TABLE_PLANG . ' where short_title='.$db->qstr($category) . ' AND page_url LIKE '.$db->qstr('cf_app.php?cat%') . ' and lang='.$db->qstr($_REQUEST['lang']));
  131. if ($res === false)
  132. camila_error_page(camila_get_translation('camila.sqlerror') . ' ' . $db->ErrorMsg());
  133. $cat_url = '-';
  134. if ($res->RecordCount() == 0)
  135. {
  136. $res = $db->Execute('select page_url from ' . CAMILA_TABLE_PLANG . ' where full_title<>'.$db->qstr('-') . ' AND page_url LIKE '.$db->qstr('cf_app.php?cat%') . ' and lang='.$db->qstr($_REQUEST['lang']));
  137. if ($res === false)
  138. camila_error_page(camila_get_translation('camila.sqlerror') . ' ' . $db->ErrorMsg());
  139. $cat_url = $res->fields['page_url'];
  140. $record = Array();
  141. $record['label_order'] = $sequence;
  142. $record['short_title'] = $category;
  143. $record['full_title'] = '-';
  144. $updateSQL = $db->AutoExecute(CAMILA_TABLE_PLANG, $record, 'UPDATE', 'page_url='.$db->qstr($cat_url) . ' and lang='.$db->qstr($_REQUEST['lang']));
  145. if (!$updateSQL)
  146. camila_information_text(camila_get_translation('camila.worktable.db.error'));
  147. }
  148. else
  149. {
  150. $cat_url = $res->fields['page_url'];
  151. }
  152. $record = Array();
  153. $record['parent'] = $cat_url;
  154. $updateSQL = $db->AutoExecute(CAMILA_TABLE_PAGES, $record, 'UPDATE', 'url='.$db->qstr($scriptname));
  155. if (!$updateSQL)
  156. camila_information_text(camila_get_translation('camila.worktable.db.error'));
  157. $record = Array();
  158. $record['visible'] = 'yes';
  159. $updateSQL = $db->AutoExecute(CAMILA_TABLE_PAGES, $record, 'UPDATE', 'url='.$db->qstr($cat_url));
  160. if (!$updateSQL)
  161. camila_information_text(camila_get_translation('camila.worktable.db.error'));
  162. }
  163. $result['result'] = 2; //success
  164. $result['processed'] = 1;
  165. $result['failed'] = 0;
  166. return $result;
  167. }
  168. ?>