<?php

  $batch_filename = '/ad/tshirts/orders/current-batch/batch.xml';



  // Validation information
  // Email regex from http://stackoverflow.com/a/12026863/
  $valid_fits = array('straight-cut', 'fitted');
  $valid_colors = array('maroon', 'gray', 'blue', 'green');
  $valid_sizes = array('YXS', 'YS', 'YM', 'YL', 'YXL', 'S', 'M', 'L', 'XL', '2XL', '3XL', '4XL');
  $valid_fitted_sizes = array('S', 'M', 'L', 'XL', '2XL', '3XL');
  $valid_statuses = array('undergraduate', 'grad student', 'faculty', 'staff', 'other');
  $valid_pays = array('normal_pay', 'electronic_pay', 'special_pay');
  $valid_email = '/^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iD';



  // Create a new random id
  function new_guid()
  {
    return trim('id' . html_entity_decode(file_get_contents('/proc/sys/kernel/random/uuid')));
  }



  // Write a new element to the batch
  function write_new_element($element_type, $array_of_data)
  {
    // Acquire an exclusive lock on the batch file
    global $batch_filename;
    $batch_file_pointer = fopen($batch_filename, 'r+');
    flock($batch_file_pointer, LOCK_EX);
    if(!$batch_file_pointer)
    {
      exit('The batch file could not be opened.');
    }

    // Load the batch file
    $batch_xml = simplexml_load_file($batch_filename);

    // Add the data in a new element
    if(in_array($element_type, array('customer', 'order_cancel', 'payment', 'pickup', 'order')))
    {
      // Add the element
      $element = $batch_xml->addChild($element_type);
      $element_guid = new_guid();
      $element->addAttribute('guid', $element_guid);
      foreach($array_of_data as $child => $value)
      {
        if($child != 'shirts')
        {
          $element->addChild($child, $value);
        }
        else
        { 
          $shirts = $element->addChild($child);
          foreach($value as $shirt_array)
          {
            $shirt_xml = $shirts->addChild('shirt');
            $shirt_xml->addChild('size', $shirt_array['size']);
            $shirt_xml->addChild('color', $shirt_array['color']);
            $shirt_xml->addChild('fit', $shirt_array['fit']);
          }
        }
      }
    }
    else
    {
      exit('Not a valid element type to add.');
    }

    // Preserves formatting
    // http://stackoverflow.com/a/798986
    $dom = new DOMDocument('1.0');
    $dom->preserveWhiteSpace = false;
    $dom->formatOutput = true;
    $dom->loadXML($batch_xml->asXML());
    $dom->save($batch_filename);
    
    // Release the lock
    flock($batch_file_pointer, LOCK_UN);
    fclose($batch_file_pointer);
    
    // Return the guid of the element that was added
    return $element_guid;
  }







?>