<?php

require_once('map_json.php');

if ( $_GET['fetch'] == 'all' ) {
    writeAllJson();
}


function fetchJson( $slug = null ) {

  global $db_tables;

  if ( $slug !== null ) {

    $filename = $slug . '.json';
    $content = connectdb( $slug );

    $json = array( $filename, $content );

    return $json;
  }
}

// $test = fetchJson( 'runshop' );

// writeJson( $test  );

function writeJson( $json = array() ) {

  $myfile = fopen( 'json/' . $json['0'], "w") or die("Unable to open file!" . $json['0']);

  fwrite( $myfile, $json['1'] );
  fclose( $myfile );

  echo $json['0'] . ' write complete ' . date('Y-m-d (D) g:m:s A T') . '<br/>';

}

function writeAllJson() {
  global $db_tables;
  foreach ( $db_tables as $slug => $table ) {
    $data = fetchJson( $slug );
    writeJson( $data );
  }

}

?>
