summaryrefslogtreecommitdiffstats
path: root/includes/admin/views/giglog_import_gigs.php
blob: e523deecfe68fcfd12ef5fcfee4aa7dce621dfee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php

// SPDX-FileCopyrightText: 2021 Andrea Chirulescu <andrea.chirulescu@gmail.com>
// SPDX-FileCopyrightText: 2021 Harald Eilertsen <haraldei@anduin.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

if ( !class_exists( 'GiglogAdmin_ImportGigsPage' ) ) {
    require_once __DIR__ . '/../../concert.php';
    require_once __DIR__ . '/../../venue.php';

    class GiglogAdmin_ImportGigsPage {
        static function render_html(): void {
            ?>
            <div class="wrap">
                <h1>Import gigs</h1>
                <p>Import gig data from a tab separated data file.</p>
                <form action="<?php menu_page_url( 'giglog_import' ) ?>" enctype="multipart/form-data" method="post">
                    <?php wp_nonce_field( plugin_basename( __FILE__ ), 'giglog_import_nonce' ); ?>
                    <label for="giglog_import_file">File: </label>
                    <input type="file" name="giglog_import_file" id="giglog_import_file">
                    <?php submit_button(); ?>
                </form>
            </div>
            <?php
        }

        static function submit_form(): void {
            if ('POST' === $_SERVER['REQUEST_METHOD'] && current_user_can('upload_files') && !empty($_FILES['giglog_import_file']['tmp_name'])) {
                if (isset($_POST['giglog_import_nonce']) && wp_verify_nonce($_POST['giglog_import_nonce'], plugin_basename( __FILE__ )) ) {
                    GiglogAdmin_ImportGigsPage::process_upload($_FILES['giglog_import_file']);
                }
                else {
                    header("{$_SERVER['SERVER_PROTOCOL']} 403 Forbidden");
                    wp_die('CSRF validation failed.', 403);
                }
            }
        }

        /**
        * Imports concert data from a file with tab separated values.
        *
        * The file must contain the following columns each separated by _one_
        * tab character:
        *
        *   1. Concertname
        *   2. Venuename or numeric venue id
        *   3. Concert date
        *   4. Ticket link
        *   5. Event info link
        *
        * Empty lines are ignored.
        *
        * @return void
        *
        * @param array<int, mixed>
        */
        static function process_upload(array $file): void {
            global $wpdb;
            $newconcert= [];
            $fo = new SplFileObject($file['tmp_name']);
            $importerrors = '';
            $rid=0;
            foreach ($fo as $line) {
                $rid++;
                $line = trim( $line );
                if ( empty($line) ) {
                    // Skip empty lines
                    continue;
                }
                $resultArray = explode("\t", $line);
                if (empty(trim($resultArray[0])))
                { $importerrors.= 'Row '.$rid.' is missing concert name!'."<br>";
                    continue;
                }
                else    if (empty(trim($resultArray[1])))
                { $importerrors.= 'Row '.$rid.' is missing venue name!'."<br>";
                    continue;
                }

                else    if (empty(trim($resultArray[2])))
                { $importerrors.= 'Row '.$rid.' is missing city name!' ."<br>";
                    continue;
                }

                else {
                    $condate     = date('Y-m-d', strtotime($resultArray[3]));
                    if ($condate<date("Y-m-d"))
                    {$importerrors.= 'Row '.$rid.' has date in the past!' ."<br>";
                        continue; }

                        else {


                            $cname       = trim($resultArray[0]);
                            $venue       = trim($resultArray[1]);


                            if (is_numeric($venue)) {
                                $venue = GiglogAdmin_Venue::get($venue);
                            }
                            else {
                                $venue = GiglogAdmin_Venue::find_or_create($venue,trim($resultArray[2]));
                            }


                            $ticketlink  = trim($resultArray[4]);
                            $eventlink   = trim($resultArray[5]);




                            try {
                                GiglogAdmin_Concert::create($cname, $venue->id(), $condate, $ticketlink, $eventlink);

                            }


                            catch(Exception $e) {
                                $importerrors.= 'Row '.$rid.' is duplicate (or failed due unknown error)!'."<br>";
                            }
                        }
                    }

                }
                if (!empty($importerrors)) echo  ($importerrors);
                else echo ('All rows imported ok');
            }
        }
    }