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