Advertisement
mstranieri

Untitled

Mar 10th, 2021
706
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.04 KB | None | 0 0
  1.  
  2.  
  3. <?php
  4.  
  5. require_once("./wp-load.php");
  6. include 'vendor/autoload.php';
  7.  
  8. ini_set('display_errors', 1);
  9. ini_set('display_startup_errors', 1);
  10. error_reporting(E_ALL);
  11.  
  12.  
  13. /* blocco upload */
  14. if(isset($_POST["submit"])) {
  15.     $target_dir = "uploads/";
  16.     $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
  17.  
  18.     if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
  19.         echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.<br /><br />";
  20.  
  21.         $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
  22.         $reader->setReadDataOnly(false);
  23.  
  24.         $spreadsheet = $reader->load( "./uploads/" . $_FILES["fileToUpload"]["name"]);
  25.         $worksheet = $spreadsheet->getActiveSheet();
  26.  
  27.         $columnA = 'A';
  28.         $columnB = 'B';
  29.         $columnC = 'C';
  30.         $columnD = 'D';
  31.         $columnE = 'E';
  32.         $columnF = 'F';
  33.         $columnG = 'G';
  34.         $columnH = 'H';
  35.         $columnI = 'I';
  36.         $lastRow = $worksheet->getHighestRow();
  37.  
  38.  
  39.         //@mauri il problema è qui!
  40.         for ($row = 1; $row <= $lastRow; $row++) {
  41.  
  42.             //A
  43.             $cell = $worksheet->getCell($columnA.$row)->getFormattedValue();
  44.             echo $cell . "; ";
  45.  
  46.             //B
  47.             $cell = $worksheet->getCell($columnB.$row)->getFormattedValue();
  48.             echo $cell . "; ";
  49.  
  50.             //C
  51.             $cell = $worksheet->getCell($columnC.$row)->getFormattedValue();
  52.             echo $cell . "; ";
  53.  
  54.             //D
  55.             $cell = $worksheet->getCell($columnD.$row)->getFormattedValue();
  56.             echo $cell . "; ";
  57.  
  58.             //E
  59.             $cell = $worksheet->getCell($columnE.$row)->getFormattedValue();
  60.             echo $cell . "; ";
  61.  
  62.             //F
  63.             $cell = $worksheet->getCell($columnF.$row)->getFormattedValue();
  64.             echo $cell . "; ";
  65.  
  66.             //G
  67.             $cell = $worksheet->getCell($columnG.$row)->getFormattedValue();
  68.             echo $cell . "; ";
  69.  
  70.             //H
  71.             $cell = $worksheet->getCell($columnH.$row)->getFormattedValue();
  72.             echo $cell . "; ";
  73.  
  74.             //I
  75.             $cell = $worksheet->getCell($columnI.$row)->getFormattedValue();
  76.             echo $cell . "; ";
  77.  
  78.             echo "<br /><br />";
  79.  
  80.         }
  81.  
  82.  
  83.  
  84.     }
  85.  
  86.     echo '<br /><br /><a href="/inoutpost.php">Torna indietro</a>';
  87.     die();
  88. }
  89. /* fine blocco upload */
  90.  
  91.  
  92.  
  93.  
  94. if($_GET && $_GET['cat']) {
  95.     $cat = $_GET['cat'];
  96. }
  97.  
  98. if($_GET && $_GET['download']) {
  99.     $download = $_GET['download'];
  100. }
  101.  
  102.  
  103. $traduttore_html_to_rich = new \PhpOffice\PhpSpreadsheet\Helper\Html();
  104. $PhpSpreadsheet_per_scrittura = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
  105. $oggetto_excel = $PhpSpreadsheet_per_scrittura->getActiveSheet();
  106.  
  107. get_header(); ?>
  108. <?php $mystique_option = mystique_global_var_declare('kid_option');?>
  109. <br />
  110. <h1>Import / Export WP to XLSX</h1>
  111. <br />
  112. <form method="post" action="#" enctype="multipart/form-data">
  113. <input type="file" name="fileToUpload" id="fileToUpload" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
  114. <input type="submit" name="submit" value="IMPORTA" style="position: relative; padding: 15px; color: #fff; background: #ae0018;" />
  115. </form>
  116. <br /><hr><br />
  117.  
  118. <br />
  119. <select id="cats">
  120. <?php
  121. $categories = get_categories();
  122. echo '<option value="">---</option>';
  123. foreach($categories as $category) {
  124. ?>
  125.  
  126.     <option <?php if(isset($cat) && $cat == $category->term_id) { echo 'selected'; } ?> value="<?=$category->term_id?>"><?=$category->name?></option>
  127.  
  128. <?php } ?>
  129. </select>
  130.  
  131. <a href="/inoutpost.php?cat=<?=$cat?>&download=true" style="position: absolute; right: 0; padding: 15px; color: #fff; background: #ae0018;">SCARICA EXCEL</a>
  132.  
  133.  
  134. <?php
  135.  
  136. echo "<br /><br />";
  137.  
  138.  
  139. if(isset($cat) && $cat != "") {
  140.     $args = array(
  141.       'post_type' => 'post' ,
  142.       'orderby' => 'date' ,
  143.       'order' => 'DESC' ,
  144.       'post_status' => 'publish',
  145.       'posts_per_page' => -1,
  146.       'cat'         => $cat,
  147.     );
  148. } else {
  149.     $args = array(
  150.       'post_type' => 'post' ,
  151.       'orderby' => 'date' ,
  152.       'order' => 'DESC' ,
  153.       'post_status' => 'publish',
  154.       'posts_per_page' => -1
  155.     );
  156. }
  157.  
  158.  
  159. $q = new WP_Query($args);
  160. if ( $q->have_posts() ) {
  161.  
  162.     if(isset($download) && $download == "true") {
  163.         $i = 1;
  164.  
  165.         $cella_da_scrivere = $oggetto_excel->getCell('A'.($i));
  166.         $cella_da_scrivere->setValue("ID");
  167.         $cella_da_scrivere = $oggetto_excel->getCell('B'.($i));
  168.         $cella_da_scrivere->setValue("TITOLO ITA");
  169.         $cella_da_scrivere = $oggetto_excel->getCell('C'.($i));
  170.         $cella_da_scrivere->setValue("TITOLO FRA");
  171.         $cella_da_scrivere = $oggetto_excel->getCell('D'.($i));
  172.         $cella_da_scrivere->setValue("TRAMA");
  173.         $cella_da_scrivere = $oggetto_excel->getCell('E'.($i));
  174.         $cella_da_scrivere->setValue("ABSTRACT");
  175.         $cella_da_scrivere = $oggetto_excel->getCell('F'.($i));
  176.         $cella_da_scrivere->setValue("BIO AUTORE");
  177.         $cella_da_scrivere = $oggetto_excel->getCell('G'.($i));
  178.         $cella_da_scrivere->setValue("DIRITTI ESTERI");
  179.         $cella_da_scrivere = $oggetto_excel->getCell('H'.($i));
  180.         $cella_da_scrivere->setValue("LUOGO PUBBLICAZIONE");
  181.         $cella_da_scrivere = $oggetto_excel->getCell('I'.($i));
  182.         $cella_da_scrivere->setValue("PREMI");
  183.  
  184.         while ($q->have_posts()) : $q->the_post();
  185.  
  186.             $cella_da_scrivere = $oggetto_excel->getCell('A'.($i+1));  
  187.             $cella_da_scrivere->setValue(get_the_ID());
  188.             // $cella_da_scrivere->getStyle('A'.($i+1))->getProtection()->setLocked(\PhpOffice\PhpSpreadsheet\Style\Protection::PROTECTION_PROTECTED);
  189.             $cella_da_scrivere->getStyle('A'.($i+1))->getProtection()->setLocked(PHPExcel_Style_Protection::PROTECTION_PROTECTED);
  190.  
  191.             $cella_da_scrivere = $oggetto_excel->getCell('B'.($i+1));  
  192.             $rich_value = $traduttore_html_to_rich->toRichTextObject(get_the_title());
  193.             echo '<pre>';
  194.             print_r ($rich_value);
  195.             echo '</pre>';
  196.             $cella_da_scrivere->setValue($rich_value);
  197.  
  198.             $cella_da_scrivere = $oggetto_excel->getCell('D'.($i+1));  
  199.             $rich_value = $traduttore_html_to_rich->toRichTextObject(str_replace("\\", "", get_field('trama')));
  200.             echo '<pre>';
  201.             print_r ($rich_value);
  202.             echo '</pre>';
  203.             $cella_da_scrivere->setValue($rich_value);
  204.  
  205.             $cella_da_scrivere = $oggetto_excel->getCell('E'.($i+1));  
  206.             $rich_value = $traduttore_html_to_rich->toRichTextObject(str_replace("\\", "", get_field('abstract')));
  207.             echo '<pre>';
  208.             print_r ($rich_value);
  209.             echo '</pre>';
  210.             $cella_da_scrivere->setValue($rich_value);
  211.  
  212.             $cella_da_scrivere = $oggetto_excel->getCell('F'.($i+1));  
  213.             $rich_value = $traduttore_html_to_rich->toRichTextObject(str_replace("\\", "", get_field('bio_autore')));
  214.             echo '<pre>';
  215.             print_r ($rich_value);
  216.             echo '</pre>';
  217.             $cella_da_scrivere->setValue($rich_value);
  218.  
  219.             $cella_da_scrivere = $oggetto_excel->getCell('G'.($i+1));  
  220.             $rich_value = $traduttore_html_to_rich->toRichTextObject(str_replace("\\", "", get_field('diritti_esteri_gia_venduti')));
  221.             echo '<pre>';
  222.             print_r ($rich_value);
  223.             echo '</pre>';
  224.             $cella_da_scrivere->setValue($rich_value);
  225.  
  226.             $cella_da_scrivere = $oggetto_excel->getCell('H'.($i+1));  
  227.             $rich_value = $traduttore_html_to_rich->toRichTextObject(str_replace("\\", "", get_field('luogo_pubblicazione')));
  228.             echo '<pre>';
  229.             print_r ($rich_value);
  230.             echo '</pre>';
  231.             $cella_da_scrivere->setValue($rich_value);
  232.  
  233.             $cella_da_scrivere = $oggetto_excel->getCell('I'.($i+1));  
  234.             $rich_value = $traduttore_html_to_rich->toRichTextObject(str_replace("\\", "", get_field('premi')));
  235.             echo '<pre>';
  236.             print_r ($rich_value);
  237.             echo '</pre>';
  238.             $cella_da_scrivere->setValue($rich_value);
  239.  
  240.             $i++;
  241.         endwhile;
  242.  
  243.         $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($PhpSpreadsheet_per_scrittura, 'Xlsx');
  244.         $writer->save('export_' . $cat . "_" . date('d-m-y') . '.xlsx');
  245.  
  246.         downloadFile('/','export_' . $cat . "_" . date('d-m-y') . '.xlsx');
  247.  
  248.  
  249.     } else {
  250.        while ($q->have_posts()) : $q->the_post();
  251.            
  252.             echo "<strong>ID:</strong> " . get_the_ID();
  253.             echo "<br />";
  254.  
  255.             echo "<strong>TITOLO:</strong> " . get_the_title();
  256.             echo "<br />";
  257.  
  258.             echo "<strong>TRAMA:</strong> " . str_replace("\\", "", get_field('trama'));
  259.             echo "<br />";
  260.  
  261.             echo "<strong>ABSTRACT:</strong> " . str_replace("\\", "", get_field('abstract'));
  262.             echo "<br />";
  263.  
  264.             echo "<strong>BIO AUTORE:</strong> " . str_replace("\\", "", get_field('bio_autore'));
  265.             echo "<br />";
  266.  
  267.             echo "<strong>DIRITTI ESTERI:</strong> " . str_replace("\\", "", get_field('diritti_esteri_gia_venduti'));
  268.             echo "<br />";
  269.  
  270.             echo "<strong>LUOGO PUBBLICAZIONE:</strong> " . str_replace("\\", "", get_field('luogo_pubblicazione'));
  271.             echo "<br />";
  272.  
  273.             echo "<strong>PREMI:</strong> " . str_replace("\\", "", get_field('premi'));
  274.             echo "<br />";
  275.  
  276.             echo "<br /><hr><br />";
  277.         endwhile;
  278.     }
  279.     wp_reset_postdata();
  280. }
  281.  
  282. ?>
  283.  
  284. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" crossorigin="anonymous"></script>
  285. <script>
  286.  
  287. $('#cats').on('change', function() {
  288.     self.location = 'inoutpost.php?cat=' + this.value;
  289. });
  290.  
  291. </script>
  292. <?php
  293. get_footer();
  294.  
  295. function downloadFile($path,$name) {
  296.     echo '<script>window.location.href = "' . $path . $name . '"</script>';
  297. }
  298.  
  299. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement