<?
  $id     = 188178;    // A plain/text document
  $collid = 169828;   // A collection
  $query = "Name=m*";      // Search query for test_9
  $host = "gehtnix";
  $username = "annonymous";
  $password = "";

  $connect = hw_connect($host, 418, $username, $password);
  if(hw_error($connect)) {
    echo "ERROR: ".hw_errormsg($connect)."\n";
    exit;
  }

  // Set all test to 'yes' if they shall be executed
  $test_1 = "no";    // Get the text document with id $id and output it
  $test_2 = "no";    // Check if id $id is in $collid
  $test_3 = "no";   // Convert object record to object array and back
  $test_4 = "no";   // Get object record of object with id $id
  $test_5 = "no";   // List children of $collid
  $test_6 = "no";   // List parents of $id
  $test_7 = "no";   // Insert a new text document
  $test_8 = "no";   // Remove the just inserted text document
  $test_9 = "no";   // Searching for objects with Name $query
  $test_10= "no";   // Listing all anchors of a document
  $test_11= "no";   // Creates document from file and outputs it.
  $test_12= "yes";   // Creates document from file and outputs it.

  /* Lists an object array
  */
  function list_attr($attributes) {
//    var_dump($attributes);
//		return;
    for($i=0; $i<count($attributes); $i++) {
      $key = key($attributes);
      switch($key) {
        case "Title":
        case "Description":
          $title_arr = $attributes[$key];
          $cj = count($title_arr);
          printf("%s = ", $key);
          for($j=0; $j<$cj; $j++) {
            $tkey = key($title_arr);
            switch($tkey) {
              case "en":
                $flagfile = "english.gif";
                break;
              case "ge":
                $flagfile = "german.gif";
                break;
              case "du":
                $flagfile = "dutch.gif";
                break;
              default:
                $flagfile = "europe.gif";
            }
            printf("%s:%s; ", $tkey, $title_arr[$tkey]);

            next($title_arr);
          }
          printf("\n");
          break;
        default:
          if(is_array($attributes[$key])) {
             $group_arr = $attributes[$key];
             $cj = count($group_arr);
             for($j=0; $j<$cj; $j++) {
               printf("%s = %s\n", $key, $group_arr[$j]);
               next($group_arr);
             }
           } else {
             printf("%s = %s\n", $key, $attributes[$key]);
           }

        }
      next($attributes);
      }
    echo "\n";
  }

  // Here come the tests
  if($test_1 == "yes") {
    echo "TEST 1 ----------------------------------------------\n";
    echo "Get the text document with id 0x".dechex($id)."\n";
    $doc = hw_gettext($connect, $id);
    if(hw_error($connect)) {
      echo "ERROR: ".hw_errormsg($connect)."\n";
      exit;
    }
    hw_output_document($doc);
    echo "\n";
  }

  if($test_2 == "yes") {
    echo "TEST 2 ----------------------------------------------\n";
    echo "Checking if '$id' (0x".dechex($id).") is in collection '$collid (0x".dechex($collid).")'\n";
    $incoll = hw_incollections($connect, array($id), array($collid), 0);
    if(hw_error($connect)) {
      echo "ERROR: ".hw_errormsg($connect)."\n";
      exit;
    }
    for($i=0; $i<count($incoll); $i++)
      echo $incoll[$i]."\n";
  }

  if($test_3 == "yes") {
    echo "TEST 3 ----------------------------------------------\n";
    echo "Convert an object array into an object record\n";
    $objrec = hw_array2objrec(array("Title"=>"ge:Hier der Titel", "Author"=>"Hier der Autor"));
    echo $objrec."\n";
    $objrec .= "\nTitle=en:Here the title";
    echo "Add another title and convert it back to an object array\n";
    $objarr = hw_objrec2array($objrec);
    list_attr($objarr);
  }

  if($test_4 == "yes") {
    echo "TEST 4 ----------------------------------------------\n";
    echo "Get the object array of document with id 0x".dechex($id)."\n";
    $objrec = hw_getobject($connect, $id);
    if(hw_error($connect)) {
      echo "ERROR: ".hw_errormsg($connect)."\n";
      exit;
    }
    $objarr = hw_objrec2array($objrec);
    list_attr($objarr);
  }

  if($test_5 == "yes") {
    echo "TEST 5 ----------------------------------------------\n";
    echo "List the children of collection 0x".dechex($collid)."\n";
    $children = hw_childrenobj($connect, $collid);
    if(hw_error($connect)) {
      echo "ERROR: ".hw_errormsg($connect)."\n";
      exit;
    }
    $c_children = count($children) - 1;
    for($i=0; $i<$c_children; $i++) {
      $objarr = hw_objrec2array($children[$i]);
      list_attr($objarr);
    }
    list_attr($children[$c_children]);
  }

  if($test_6 == "yes") {
    echo "TEST 6 ----------------------------------------------\n";
    echo "List the parents of object 0x".dechex($id)."\n";
    $parents = hw_getparentsobj($connect, $collid);
    if(hw_error($connect)) {
      echo "ERROR: ".hw_errormsg($connect)."\n";
      exit;
    }
    $c_parents = count($parents) - 1;
    for($i=0; $i<$c_parents; $i++) {
      $objarr = hw_objrec2array($parents[$i]);
      list_attr($objarr);
    }
    list_attr($parents[$c_parents]);
  }

  if($test_7 == "yes") {
    echo "TEST 7 ----------------------------------------------\n";
    echo "Inserting a new text document into 0x".dechex($collid)."\n";
    $objrec = "Type=Document\nDocumentType=text\nName=HWTest\nTitle=en:Component\nMimeType=text/plain\nAuthor=".$username;
    $contents = "Ein bischen Text";
    $doc =  hw_new_document($objrec, $contents, strlen($contents)+1);
    $objid = hw_insertdocument($connect, $collid, $doc);
    if(hw_error($connect)) {
      echo "ERROR: ".hw_errormsg($connect)."\n";
      exit;
    }
    $objrec = hw_getobject($connect, $objid);
    if(hw_error($connect)) {
      echo "ERROR: ".hw_errormsg($connect)."\n";
      exit;
    }
    $objarr = hw_objrec2array($objrec);
    list_attr($objarr);
  }

  if($test_8 == "yes") {
    echo "TEST 8 ----------------------------------------------\n";
    echo "Removing text document just inserted\n";
    $kk[0] = (int) $objid;
    hw_mv($connect, $kk, $collid, 0);
    if(hw_error($connect)) {
      echo "ERROR: ".hw_errormsg($connect)."\n";
      exit;
    }
    echo "If the document was really deleted you should see an error now\n";
    $objrec = hw_getobject($connect, $objid);
    if(hw_error($connect)) {
      echo "ERROR: ".hw_errormsg($connect)."\n";
    } else {
      $objarr = hw_objrec2array($objrec);
      list_attr($objarr);
    }
  }

  if($test_9 == "yes") {
    echo "TEST 9 ----------------------------------------------\n";
    echo "Searching for objects with $query\n";
    $objrecs = hw_getobjectbyqueryobj($connect, $query, -1);
    $c_objrecs = count($objrecs) - 1;
    echo "$c_objrecs found\n";
    for($i=0; $i<$c_objrecs; $i++) {
      $objarr = hw_objrec2array($objrecs[$i]);
      list_attr($objarr);
    }
    list_attr($objrecs[$c_objrecs]);
  }

  if($test_10 == "yes") {
    $anchors = hw_getanchorsobj($connect, $id);
    $countanchors = count($anchors) - 1;
    echo "$countanchors Anchors of Object $id\n";
    for($i=0; $i<$countanchors; $i++) {
      $arr = hw_objrec2array($anchors[$i]);
      list_attr($arr);
    }
	}

  if($test_11 == "yes") {
		$doc = hw_new_document_from_file("ObjectRecord", "hw_document.txt");
    hw_output_document($doc);
	}

  if($test_12 == "yes") {
		$doc = hw_new_document_from_file("ObjectRecord", "hw_document.txt");
		hw_insertanchors($doc, array("Position=0x2 0x7\nObjectID=0x3\nTAnchor=Src\nDest=0x5"), array("ObjectID=0x5\nName=DestDoc"));
    hw_output_document($doc);
	}

  hw_close($connect);
?>
