<?php
/**
 * Module javascript 
 */
header ( "Content-Type: text/javascript; charset=utf-8" );
header ( "Access-Control-Allow-Origin: *" );
header ( "Access-Control-Allow-Headers: content-type" );

if (isset ( $_GET ["suboperation"] ) && is_string ( $_GET ["suboperation"] ) && trim ( $_GET ["suboperation"] ) != "") {
  if ($_GET ["suboperation"] == "examples") {
    $output = array ();
    // collect examples
    $output ["examples"] = array ();
    $directory = SITE_LAYOUT_DIR . "examples" . DIRECTORY_SEPARATOR;
    if (is_dir ( $directory )) {
      if ($dh = opendir ( $directory )) {
        $tmp_sort = array ();
        while ( ($file = readdir ( $dh )) !== false ) {
          if (is_file ( $directory . $file ) && preg_match ( "/^([0-9]+[0-9a-z_]*\.)?([a-z0-9\_]+)\.(html|php)$/i", $file, $match )) {
            $name = str_replace ( "_", " ", $match [2] );
            if (preg_match ( "/^(([0-9]+)(_[0-9a-z]+)?(_[0-9a-z]+)?)\.$/i", $match [1], $submatches )) {
              $code = $submatches [1];
            } else {
              $code = "";
            }
            $output ["examples"] [] = array (
                "title" => trim ( $name ),
                "code" => $code,
                "url" => SITE_LOCATION . LAYOUT_DIR . "/examples/" . $file 
            );
            $tmp_sort [] = $code;
          }
        }
        closedir ( $dh );
        array_multisort ( $tmp_sort, $output ["examples"] );
      }
    }
    // collect expansions
    $output ["expansion"] = $configuration->getExpansions ();
    // collect configurations
    $output ["solr"] = array ();
    foreach ( $configuration->solr as $key => $value ) {
      if (preg_match ( "/^[a-z0-9]+$/i", $key )) {
        $output ["solr"] [$key] = $value;
      }
    }
    echo (json_encode ( $output ));
  }
}
exit ();
?>