PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/import.php

https://github.com/Chicago/metalicious
PHP | 443 lines | 354 code | 48 blank | 41 comment | 37 complexity | 5e723eac088e93e25e6020c4059b4015 MD5 | raw file
  1. <?php
  2. include dirname(__FILE__) . '/../classes/database.php';
  3. include dirname(__FILE__) . '/../classes/table.php';
  4. include dirname(__FILE__) . '/../classes/variable.php';
  5. function convert_string($string) {
  6. //converts smart quotes / dashes to normal quotes / dashes.
  7. $search = array(chr(145), chr(146), chr(147), chr(148), chr(150), chr(151), chr(152));
  8. $replace = array("'", "'", '"', '"', '-', '-', '-');
  9. return str_replace($search, $replace, $string);
  10. }
  11. include dirname(__FILE__) . '/include/header.php';
  12. ?>
  13. <script>
  14. $(document).ready(function() {
  15. document.getElementsByName('search_criteria')[1].focus();
  16. });
  17. </script>
  18. <h3>Import:</h3>
  19. <?php
  20. if (!isset($_POST['posted'])) {
  21. ?>
  22. Sample Files: <a href="./CSR_Meta.txt">CSR_Meta.txt</a> - <a href="./NSR_TBLS.txt">NSR_TBLS.txt</a><br />
  23. <br />
  24. Choose your Meta Data file and then click on the submit button.
  25. <form action="import.php" method="post" enctype="multipart/form-data">
  26. 1. <input type="file" name="database_descriptions_file" /><br /><br />
  27. <input type="hidden" name="posted" />
  28. 2. <input type="submit" value="submit" />
  29. </form>
  30. <hr />
  31. <br />
  32. Choose your Table Descriptions file and then click on the submit button.
  33. <form action="import.php" method="post" enctype="multipart/form-data">
  34. 1. <input type="file" name="table_descriptions_file" /><br /><br />
  35. 2. Select Overwrite / Skip Existing Items:<br />
  36. <select name="table_descriptions_overwrite_skip" id="table_descriptions_overwrite_skip">
  37. <option value="Overwrite">Overwrite</option>
  38. <option value="Skip">Skip</option>
  39. </select>
  40. <input type="hidden" name="posted" /><br /><br />
  41. 3. <input type="submit" value="submit" />
  42. </form>
  43. <hr />
  44. <br />
  45. Choose your Variables .csv file and then click on the submit button.
  46. <form action="import.php" method="post" enctype="multipart/form-data">
  47. 1. <input type="file" name="variable_descriptions_file" /><br /><br />
  48. 2. Select Overwrite / Skip Existing Items:<br />
  49. <select name="variable_overwrite_skip" id="variable_overwrite_skip">
  50. <option value="Overwrite">Overwrite</option>
  51. <option value="Skip">Skip</option>
  52. </select>
  53. <input type="hidden" name="posted" /><br /><br />
  54. 3. <input type="submit" value="submit" />
  55. </form>
  56. <?php
  57. } else {
  58. if (isset($_FILES['database_descriptions_file'])) {
  59. ?>
  60. <?php // var_dump($_POST, $_FILES); ?>
  61. <?php
  62. //open uploaded file
  63. $handle = fopen($_FILES['database_descriptions_file']['tmp_name'], "r");
  64. //all imported elements
  65. $dbs = array();
  66. $tables = array();
  67. $variables = array();
  68. $parent = "";
  69. //parentheses
  70. $parentheses = 0;
  71. //$count = 500;
  72. echo "<pre>";
  73. //scroll through and parse
  74. while((!feof($handle))) {// && ($count > 0)) {
  75. //$count--;
  76. //get the line
  77. $line = fgets($handle);
  78. echo $line . "<br />";
  79. echo "1";
  80. //open table info
  81. if ($line == "(\r\n") {
  82. echo "1aaa";
  83. $parentheses++;
  84. //$parentheses
  85. continue;
  86. }
  87. echo "2";
  88. //close table info
  89. if ($line == ")\r\n") {
  90. echo "2aaa";
  91. $parentheses--;
  92. continue;
  93. }
  94. echo "3";
  95. //if outside parentheses
  96. if ($parentheses == 0) {
  97. echo "4";
  98. //if there's a period in the line
  99. echo strpos($line, '.');
  100. echo (strpos($line, '.') > -1);
  101. //if (strpos($line, '.') != false) {
  102. if (strpos($line, '.') > -1) {
  103. echo "5";
  104. $exploded_line = explode('.', $line);
  105. //print_r($exploded_line);
  106. //insert into DB array
  107. echo "6";
  108. if (!in_array(ltrim($exploded_line[0]), $dbs)) {
  109. array_push($dbs, ltrim($exploded_line[0]));
  110. }
  111. echo "7";
  112. //insert into Table array
  113. if (!in_array($exploded_line[1], $tables)) {
  114. array_push($tables,
  115. array(ltrim($exploded_line[0]), str_replace("\r\n", "", $exploded_line[1])));
  116. }
  117. echo "8";
  118. //set parent
  119. $parent = str_replace("\r\n", "", $exploded_line[1]);
  120. }
  121. } else {
  122. echo "9";
  123. //clean left whitespace
  124. $line = ltrim($line);
  125. //remove commas
  126. $line = str_replace(",\r\n", "", $line);
  127. //get variable name
  128. $variable_name = substr($line, 0, strpos($line, " "));
  129. echo $variable_name . "********************";
  130. echo "10";
  131. //ignore constraint lines
  132. if (($variable_name != "CONSTRAINT")
  133. && ($variable_name != "REFERENCES")
  134. && ($variable_name != "ON"))
  135. {
  136. echo "11";
  137. //get variable info
  138. $variable_info = ltrim(substr($line, strpos($line, " ")));
  139. //insert into Variables array
  140. array_push($variables, array($parent,
  141. $variable_name,
  142. $variable_info));
  143. echo "12";
  144. }
  145. }
  146. }
  147. echo "</pre>";
  148. fclose($handle);
  149. //summary
  150. echo "DBs: <br /><pre>";
  151. foreach ($dbs as $key => $val) {
  152. print "$key = $val\n";
  153. //open DB
  154. include dirname(__FILE__) . '/../include/dbconnopen.php';
  155. $imported_record = mysqli_query($cnnCDD, "Call 0_Import_Database__Create_Database('"
  156. . addslashes($val)
  157. . "','" //. addslashes($description)
  158. . "','" //. addslashes($business_owner)
  159. . "','" //. addslashes($contact_information)
  160. . "','" //. $data_period
  161. . "','" //. addslashes($software_platform)
  162. . "','" //. addslashes($general_accuracy)
  163. . "','" //. addslashes($comments)
  164. . "','" . $_COOKIE['user_id'] . /*addslashes($creator). */ "')");
  165. if (is_object($imported_record)) { //->num_rows > 0) {
  166. echo "IMPORTED!\r\n";
  167. } else {
  168. echo "NOT IMPORTED... Already exists.\r\n";
  169. }
  170. //close DB
  171. include dirname(__FILE__) . '/../include/dbconnclose.php';
  172. }
  173. //print_r($dbs);
  174. echo "</pre><br />Tables: <br /><pre>";
  175. foreach ($tables as $key => $val) {
  176. print "$key\n";
  177. foreach ($val as $keyy => $vall) {
  178. print " $keyy = $vall\n";
  179. }
  180. //open DB
  181. include dirname(__FILE__) . '/../include/dbconnopen.php';
  182. $imported_record = mysqli_query($cnnCDD, "Call 0_Import_Table__Create_Table('"
  183. . addslashes($val[1])
  184. . "','" //. addslashes($table_description)
  185. . "','" //. addslashes($table_comments)
  186. . "','" . $_COOKIE['user_id'] //. addslashes($creator)
  187. . "','" . addslashes($val[0]) . "')");
  188. if (is_object($imported_record)) { //->num_rows > 0) {
  189. echo "IMPORTED!\r\n";
  190. } else {
  191. echo "NOT IMPORTED... Already exists.\r\n";
  192. }
  193. //close DB
  194. include dirname(__FILE__) . '/../include/dbconnclose.php';
  195. }
  196. //print_r($tables);
  197. echo "</pre><br />Variables: <br /><pre>";
  198. foreach ($variables as $key => $val) {
  199. print "$key\n";
  200. foreach ($val as $keyy => $vall) {
  201. print " $keyy = $vall\n";
  202. }
  203. //open DB
  204. include dirname(__FILE__) . '/../include/dbconnopen.php';
  205. $imported_record = mysqli_query($cnnCDD, "Call 0_Import_Variable__Create_Variable('"
  206. . addslashes($val[1])
  207. . "','" . addslashes($val[2])
  208. . "','" //. addslashes($variable_length)
  209. . "','" //. addslashes($variable_values)
  210. . "','" //. addslashes($variable_example)
  211. . "','" //. addslashes($variable_comments)
  212. . "','" . $_COOKIE['user_id']//. addslashes($creator)
  213. . "','" . addslashes($val[0]) . "')");
  214. if (is_object($imported_record)) { //->num_rows > 0) {
  215. echo "IMPORTED!\r\n";
  216. } else {
  217. echo "NOT IMPORTED... Already exists.\r\n";
  218. }
  219. //close DB
  220. include dirname(__FILE__) . '/../include/dbconnclose.php';
  221. }
  222. //print_r($variables);
  223. echo "</pre>";
  224. echo "<a href=\"import.php\">Import Another</a>";
  225. } else if (isset($_FILES['table_descriptions_file'])) {
  226. if ($_POST['table_descriptions_overwrite_skip'] == 'Overwrite') {
  227. echo "<b>Overwriting Existing Records...</b><br /><br />";
  228. } else {
  229. echo "<b>Skipping Existing Records...</b><br /><br />";
  230. }
  231. //import table descriptions
  232. $handle = fopen($_FILES['table_descriptions_file']['tmp_name'], "r");
  233. //scroll through and parse
  234. while (($data = fgetcsv($handle, ",")) !== false) {
  235. //test for correct formatting
  236. if (isset($data[4])) {
  237. die("This .csv file doesn't seem to be in the correct format. Please go back and try again.");
  238. }
  239. /*
  240. //scroll through and parse
  241. while((!feof($handle))) {// && ($count > 0)) {
  242. //$count--;
  243. //get the line
  244. $line = fgets($handle);
  245. echo $line . "<br />";
  246. $pos = strpos($line, ",");
  247. $table_name = substr($line, 0, $pos);
  248. $table_description = substr($line, ($pos + 1));
  249. */
  250. //open DB
  251. include dirname(__FILE__) . '/../include/dbconnopen.php';
  252. //overwrite / skip existing records
  253. if ($_POST['table_descriptions_overwrite_skip'] == 'Overwrite') {
  254. //OVERWRITE EXISTING RECORDS
  255. $imported_record = mysqli_query($cnnCDD, "Call 0_Import_Table__Table_Descriptions_OVERWRITE('"
  256. . addslashes($data[0])
  257. . "','" . addslashes($data[1])
  258. . "','" . addslashes($data[2])
  259. . "','" . addslashes(((isset($data[3])) ? $data[3] : ""))
  260. . "','" . $_COOKIE['user_id'] //. addslashes($creator)
  261. . "')");
  262. } else if ($_POST['table_descriptions_overwrite_skip'] == 'Skip') {
  263. //SKIP EXISTING RECORDS
  264. $imported_record = mysqli_query($cnnCDD, "Call 0_Import_Table__Table_Descriptions_SKIP('"
  265. . addslashes($data[0])
  266. . "','" . addslashes($data[1])
  267. . "','" . addslashes($data[2])
  268. . "','" . addslashes(((isset($data[3])) ? $data[3] : ""))
  269. . "','" . $_COOKIE['user_id'] //. addslashes($creator)
  270. . "')");
  271. }
  272. echo $data[0] . " -> " . $data[1] . " -> " . $data[2];
  273. if (is_object($imported_record)) { //->num_rows > 0) {
  274. echo "<br />IMPORTED!<br /><br />";
  275. } else {
  276. echo "<br />NOT IMPORTED... Already exists.<br /><br />";
  277. }
  278. //close DB
  279. include dirname(__FILE__) . '/../include/dbconnclose.php';
  280. }
  281. echo "<a href=\"import.php\">Import Another</a>";
  282. //VARIABLE CSV FILE
  283. } else if (isset($_FILES['variable_descriptions_file'])) {
  284. if ($_POST['variable_overwrite_skip'] == 'Overwrite') {
  285. echo "<b>Overwriting Existing Records...</b><br /><br />";
  286. } else {
  287. echo "<b>Skipping Existing Records...</b><br /><br />";
  288. }
  289. //import variable descriptions
  290. $handle = fopen($_FILES['variable_descriptions_file']['tmp_name'], "r");
  291. $row = 1;
  292. //scroll through and parse
  293. while (($data = fgetcsv($handle, ",")) !== false) {
  294. //test for correct formatting
  295. if (!isset($data[4])) {
  296. die("This .csv file doesn't seem to be in the correct format. Please go back and try again.");
  297. }
  298. echo "Database: " . $data[0] . "<br />";
  299. echo "Table: " . $data[1] . "<br />";
  300. echo "Column / Field: " . $data[2] . "<br />";
  301. echo "Type: " . $data[3] . "<br />";
  302. echo "Length: " . $data[4] . "<br />";
  303. echo "Value_Range: " . ((isset($data[5])) ? convert_string($data[5]) : "") . "<br />";
  304. echo "Description: " . ((isset($data[6])) ? $data[6] : "") . "<br />";
  305. echo "Examples: " . ((isset($data[7])) ? $data[7] : "") . "<br />";
  306. echo "Comments: " . ((isset($data[8])) ? $data[8] : "") . "<br /><br />";
  307. //open DB
  308. include dirname(__FILE__) . '/../include/dbconnopen.php';
  309. //overwrite / skip existing records
  310. if ($_POST['variable_overwrite_skip'] == 'Overwrite') {
  311. //OVERWRITE EXISTING RECORDS
  312. $imported_record = mysqli_query($cnnCDD, "Call 1_Import_Variable__Create_Variable_OVERWRITE('"
  313. . addslashes($data[0])
  314. . "','" . addslashes($data[1])
  315. . "','" . addslashes($data[2])
  316. . "','" . addslashes($data[3])
  317. . "','" . addslashes($data[4])
  318. . "','" . addslashes(((isset($data[5])) ? convert_string($data[5]) : ""))
  319. . "','" . addslashes(((isset($data[6])) ? $data[6] : ""))
  320. . "','" . addslashes(((isset($data[7])) ? $data[7] : ""))
  321. . "','" . addslashes(((isset($data[8])) ? $data[8] : ""))
  322. . "','" . $_COOKIE['user_id'] /* . addslashes($creator) */ . "')");
  323. } else {
  324. //SKIP EXISTING RECORDS
  325. $imported_record = mysqli_query($cnnCDD, "Call 1_Import_Variable__Create_Variable_SKIP('"
  326. . addslashes($data[0])
  327. . "','" . addslashes($data[1])
  328. . "','" . addslashes($data[2])
  329. . "','" . addslashes($data[3])
  330. . "','" . addslashes($data[4])
  331. . "','" . addslashes(((isset($data[5])) ? convert_string($data[5]) : ""))
  332. . "','" . addslashes(((isset($data[6])) ? $data[6] : ""))
  333. . "','" . addslashes(((isset($data[7])) ? $data[7] : ""))
  334. . "','" . addslashes(((isset($data[8])) ? $data[8] : ""))
  335. . "','" . $_COOKIE['user_id'] /* . addslashes($creator) */ . "')");
  336. }
  337. if (is_object($imported_record)) { //->num_rows > 0) {
  338. echo "<b style=\"color: #0f0;\">IMPORTED!</b><hr />";
  339. } else {
  340. echo "<b style=\"color: #f00;\">NOT IMPORTED...<br />May already exist or corresponding table / database doesn't exist.</b><hr />";
  341. }
  342. //close DB
  343. include dirname(__FILE__) . '/../include/dbconnclose.php';
  344. }
  345. echo "<a href=\"import.php\">Import Another</a>";
  346. }
  347. }
  348. ?>
  349. <div style="margin-left: 20px;">
  350. <?php
  351. /*
  352. while ($result_element = mysqli_fetch_assoc($search_results)) {
  353. //if element is a database
  354. if ($result_element['Element_Type'] == 'Database') {
  355. $database = Database::get_database_info($result_element['Element_ID']);
  356. ?>
  357. <a href="../database_info.php?database_id=<?php echo $database['Database_ID']; ?>"><i class="icon-th-large"></i> <?php echo $database['Database_Name']; ?></a> (database)<br />
  358. <?php
  359. } else if ($result_element['Element_Type'] == 'Table') {
  360. $table = Table::get_table_info($result_element['Element_ID']);
  361. ?>
  362. <a href="../table_info.php?table_id=<?php echo $table['Table_ID']; ?>"><i class="icon-th"></i> <?php echo $table['Table_Name']; ?></a> (table)<br />
  363. <?php
  364. } else if ($result_element['Element_Type'] == 'Variable') {
  365. $variable = Variable::get_variable_info($result_element['Element_ID']);
  366. ?>
  367. <a href="../variable_info.php?variable_id=<?php echo $variable['Variable_ID']; ?>"><i class="icon-asterisk"></i> <?php echo $variable['Variable_Name']; ?></a> (variable)<br />
  368. <?php
  369. }
  370. }
  371. */
  372. ?>
  373. </div>
  374. <?php include 'include/footer.php'; ?>