CLOCK code analysis Part 2

Posted on July 20th, 2012 by Andrew Beeken

Following on from the previous post, we’re going to take a look at the prototype for the record building module. This module contains a slightly different version of the comparison module which builds a form into the resulting output:

function compare( $elements, $default ){
		//Read each source into an array
		//These are stored in sources.csv, representing:
		//	{translator file},{uri}
		//Long term, these will be derived from our index database
		$source_file = fopen( "sources.csv", "r" );

		while (( $data = fgetcsv($source_file, 1000, ",")) !== FALSE ) {
			$sources[] = $data;


		//d($sources); //#DEBUG
		foreach( $sources as $source ) :
			$trans_file = $source[0].".csv";
			$bibs[$source[0]] = translate( $source[1],$trans_file ); //Translating all of our data from our sources
		//d($bibs); //#DEBUG

		$output = '<form action="build.php" method="post">';
		$output .= '<input type="hidden" name="records" value="yes" />';
		$output .= '<ul class="compared">'; //Set up our blank output

		//For each element
		foreach( $elements as $element ) :
		//	1. Add the default source to the output
			$output .= "<li>";
			$output .= "<h5>" . $element . "</h5>";

			foreach( $bibs[$default][$element] as $the_default ) :
				$output .= "<input type='checkbox' name='". $element ."[]' value='". $the_default ."' checked /> ". $the_default ."<br />";

		//	2. For each source not default
		//		2a. Compare element to default

			foreach( $sources as $source ) :
				if ( $source[0] != $default ) : //If this isn't the default source
					//echo $source[0]; #DEBUG
					///echo $element; #DEBUG
					foreach( $bibs[$source[0]][$element] as $the_element ) :
						if( in_array( $the_element, $bibs[$default][$element] ) ) :
							//Do nothing
						else :
		//		2b. Add element to output if different
							$output .= '<span class="diff"><input type="checkbox" name="'. $element .'[]" value="'. $the_element .'" /> <strong>' . $source[2] . ' says -> </strong>' . $the_element . '<br /></span>';

			$output .= "</li>";


		$output .= "</ul>";
		$output .= '<input type="submit" value="Go!" />';
		$output .= "</form>";

		return $output;

This form demonstrates how a cataloguer can refine their records based on results from other institutes by selecting which records they want to keep and which they want to swap out for other records. The attached screenshot shows how this looks in practice.

It is worth remembering that this function is purely for demonstrative processes only and does nothing practical other than showing what the form would look like and how a cataloguer could build records.

Tags: , , , , , , ,

Comments are closed.