function pof_importer_suggestionsdriveimport_run($fileId, $saveToDataBase = false)
{
    $service = pof_importer_get_google_service();
    try {
        $file = $service->files->get($fileId);
        echo '<table cellpadding="2" cellspacing="2" border="2">';
        echo '<tr>';
        echo '<th>Otsikko</th>';
        echo '<td>' . $file->getTitle() . '</td>';
        echo '</tr>';
        echo '<tr>';
        echo '<th>Avaa drivess&auml;</th>';
        echo '<td><a href="' . $file->getAlternateLink() . '" target="_blank">' . $file->getAlternateLink() . '</a></td>';
        echo '</tr>';
        echo '<tr>';
        echo '<th>Kuvaus</th>';
        echo '<td>' . $file->getDescription() . '</td>';
        echo '</tr>';
        echo '<tr>';
        echo '<th>Viimeksi muokattu</th>';
        $fileLastModified = strtotime($file->getModifiedDate());
        echo '<td>' . date('d.m.Y', $fileLastModified) . '</td>';
        echo '</tr>';
        echo '<tr>';
        echo '<th>Muokkaaja</th>';
        echo '<td>' . $file->getLastModifyingUserName() . '</td>';
        echo '</tr>';
        echo '</table>';
        $filepath = pof_importer_download_drive_file($service, $file);
        $objReader = pof_imported_get_phpExcel_objReader();
        $objPHPExcel = $objReader->load($filepath);
        $sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
        if (pof_importer_tasksdriveimport_checkThatHeadersMatchSuggestions($sheetData[1])) {
            $headers = $sheetData[1];
            $i = 0;
            foreach ($sheetData as $sheetDataRow) {
                $i++;
                if ($i < 2) {
                    continue;
                }
                pof_importer_suggestionssdriveimport_importRow($sheetDataRow, $i, $saveToDataBase);
            }
        } else {
            echo "<h1>Unvalid excel, or failed to read excel at all.</h1>";
        }
    } catch (Exception $e) {
        echo "An error occurred: " . $e->getMessage();
    }
}
Exemplo n.º 2
0
function pof_importer_suggestionsdriveimport2()
{
    echo '<div class="wrap">';
    echo '<h1>POF Importer, vinkit google drivest&auml;</h1>';
    if (!isset($_POST) || !isset($_POST["drive_file_id"])) {
        $service = pof_importer_get_google_service();
        echo '<form method="post" action="">';
        // Print the names and IDs for up to 10 files.
        $optParams = array('maxResults' => 999, 'q' => "mimeType = 'application/vnd.google-apps.spreadsheet' and (title contains 'vinki' or title contains 'vinkki' or title contains '_vinkit_')", 'orderBy' => 'folder,modifiedDate desc,title');
        $results = $service->files->listFiles($optParams);
        if (count($results->getItems()) == 0) {
            print "No files found.\n";
        } else {
            print "Valitse importoitava tiedosto:<br />";
            echo '<select name="drive_file_id">';
            foreach ($results->getItems() as $file) {
                $fileLastModified = strtotime($file->getModifiedDate());
                printf("<option value=\"%s\">%s (%s)</option>\n", $file->getId(), $file->getTitle(), date('d.m.Y', $fileLastModified));
            }
            echo "</select>";
            echo "<br />";
            print "Valitse ik&auml;kausi: <br />";
            echo '<select name="agegroup">';
            $agegroups = pof_taxonomy_translate_get_agegroups();
            foreach ($agegroups as $agegroup) {
                if ($agegroup->id == 0) {
                    continue;
                }
                printf("<option value=\"%s\">%s</option>\n", $agegroup->id, $agegroup->title);
            }
            echo '</select>';
            echo "<br />";
            echo "<br />";
            echo '<input type="submit" name="Submit" value="Valitse tiedosto" />';
            echo '</form>';
        }
    } else {
        if (!isset($_POST["SaveToDatabase"])) {
            echo '<form method="post" action="">';
            echo '<input type="hidden" name="drive_file_id" value="' . $_POST["drive_file_id"] . '" />';
            echo '<input type="hidden" name="agegroup" value="' . $_POST["agegroup"] . '" />';
            echo '<input type="submit" name="SaveToDatabase" value="Tallenna tietokantaan" />';
            echo '<br /><br />';
            echo '<input type="submit" name="RunAgain" value="Aja uudestaan" />';
            echo '</form>';
            pof_importer_suggestionsdriveimport_run2($_POST["drive_file_id"], $_POST["agegroup"]);
        } else {
            echo '<form method="post" action="">';
            echo '<input type="hidden" name="drive_file_id" value="' . $_POST["drive_file_id"] . '" />';
            echo '<input type="hidden" name="agegroup" value="' . $_POST["agegroup"] . '" />';
            echo '<input type="submit" name="SaveToDatabase" value="Tallenna tietokantaan" />';
            echo '<br /><br />';
            echo '<input type="submit" name="RunAgain" value="Aja uudestaan" />';
            echo '</form>';
            pof_importer_suggestionsdriveimport_run2($_POST["drive_file_id"], $_POST["agegroup"], true);
        }
    }
    echo "</div>";
}
function pof_importer_suggestionsdriveimport_run2($fileId, $agegroup_id, $saveToDataBase = false)
{
    $service = pof_importer_get_google_service();
    $agegroups = pof_taxonomy_translate_get_agegroups();
    $agegroup = new stdClass();
    foreach ($agegroups as $agegroup_tmp) {
        if ($agegroup_tmp->id == $agegroup_id) {
            $agegroup = $agegroup_tmp;
            break;
        }
    }
    try {
        $file = $service->files->get($fileId);
        $lang = substr($file->getTitle(), -2, 2);
        echo '<table cellpadding="2" cellspacing="2" border="2">';
        echo '<tr>';
        echo '<th>Otsikko</th>';
        echo '<td>' . $file->getTitle() . '</td>';
        echo '</tr>';
        echo '<tr>';
        echo '<th>Kieli</th>';
        echo '<td>' . $lang . '</td>';
        echo '</tr>';
        echo '<tr>';
        echo '<th>Avaa drivess&auml;</th>';
        echo '<td><a href="' . $file->getAlternateLink() . '" target="_blank">' . $file->getAlternateLink() . '</a></td>';
        echo '</tr>';
        echo '<tr>';
        echo '<th>Ik&auml;kausi</th>';
        echo '<td>' . $agegroup->title . '</td>';
        echo '</tr>';
        echo '<tr>';
        echo '<th>Kirjoittaja</th>';
        echo '<td>Suomen partiolaiset</td>';
        echo '</tr>';
        echo '<tr>';
        echo '<th>Kuvaus</th>';
        echo '<td>' . $file->getDescription() . '</td>';
        echo '</tr>';
        echo '<tr>';
        echo '<th>Viimeksi muokattu</th>';
        $fileLastModified = strtotime($file->getModifiedDate());
        echo '<td>' . date('d.m.Y', $fileLastModified) . '</td>';
        echo '</tr>';
        echo '<tr>';
        echo '<th>Muokkaaja</th>';
        echo '<td>' . $file->getLastModifyingUserName() . '</td>';
        echo '</tr>';
        echo '</table>';
        $filepath = pof_importer_download_drive_file($service, $file);
        $objReader = pof_imported_get_phpExcel_objReader();
        $objPHPExcel = $objReader->load($filepath);
        $sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
        if (pof_importer_tasksdriveimport_checkThatHeadersMatchSuggestions2($sheetData[1])) {
            $headers = $sheetData[1];
            $i = 0;
            foreach ($sheetData as $sheetDataRow) {
                $i++;
                if ($i < 2) {
                    continue;
                }
                if (trim($sheetDataRow['A']) == '') {
                    continue;
                }
                pof_importer_suggestionssdriveimport_importRow2($sheetDataRow, $i, $agegroup_id, $lang, $saveToDataBase);
            }
        } else {
            echo "<h1>Unvalid excel, or failed to read excel at all.</h1>";
        }
    } catch (Exception $e) {
        echo "An error occurred: " . $e->getMessage();
    }
}