Quick Parsing Primer

Revised: March 30, 2015
The latest documentation can be found on the Project Wiki. The information below may be out of date.
Parsing currently happens in ajax-callbacks/ajax-parser.php. Two arrays of parser objects are automatically built from the files in parser-class-lib/preparse and parser-class-lib/parse. These arrays are cached in $_SESSION, so you may need to close & relaunch your browser to enable a new module.

Preparse Objects

Preparse object inherit from parser-class-lib/Parser.php. The check() method is called for every available preparse object. If check() returns true, parse() is called for that object. The return value of parse() replaces the current user input.

The following sets the toggletax value in session, then removes the trigger prefix from the input string. Includes are omitted for simplicity; see any module included in the release for greater detail.

Example Preparse Object

	class TaxShift extends Parser {
		function check($str){	
			if (substr($str,0,3) == "1TN")
				return True;
			return False;
		}

		function parse($str){
			global $CORE_LOCAL;
			$CORE_LOCAL->set("toggletax",1);
			return substr($str,3);
		}
	}
	

Parse Objects

Parse objects also inherit from parser-class-lib/Parser.php. Here, the check() method is called for each object in turn until one returns true. The parse() method is called for that class and input processing stops. Parse objects are slightly more complicated because the parse() function must return an array.

The return value of parse() in this case should be a keyed array. This structure converts easily to a JSON, which in turn makes passing data to javascript more reliable and well-defined. The base class, Parser, provides a method default_json() that generates a proper array with sane defaults.

Return value structure
main_frame URL string, default false. If this is set, the browser moves to the specified URL. This option overrides all others.
target CSS selector, default ".baseHeight". If this is set, output will be written to the current screen inside the element specified. Any JQuery selector is valid here.
output HTML string, default false. If set, this is written in the element specified by target.
redraw_footer boolean, default false. If set, the footer values (you saved, total, etc) are re-drawn.
receipt string receipt type, default false. If set, prints the given receipt type.
scale string scale input, default false. If set, this updates the HTML scale display accordingly.
udpmsg string, default false. If set, this string is streamed directly to the scale driver via UDP. Experimental.
retry string, default false. If set, the string is reparsed in 70 milliseconds. I "waiting for the scale" on by weight items is the only actual use for this.

Parse Examples

It's not as bad as it sounds. Here's an example of a parse object that sends the browser to a new screen when "CAB" is entered (again, includes omitted):
	class CabCoupon extends Parser {
		function check($str){
			if ($str == "CAB") return True;
			return False;
		}

		function parse($str){
			$return_value = $this->default_json();
			$return_value['main_frame'] = MiscLib::base_url()."gui-modules/CabDisplay.php";	
			return $return_value;
		}
	}
	
Here's another example. This example prints a partial receipt & displays a notification:
	class PartialPrint extends Parser {
		function check($str){
			if ($str == "PRP") return True;
			return False;
		}

		function parse($str){
			$return_value = $this->default_json();
			$return_value['output'] = DisplayLib::boxMsg("Printing Receipt");
			$return_value['receipt'] = "partial";
			return $return_value;
		}
	}
	

Testing

One downside (depending on perspective) of the AJAX system with JSON returns is your code has to be clean. Any PHP warnings or errors result in invalid JSON objects. The form in the "test" directory ( /IS4C/pos/is4c-nf/test/index.php ) lets you post input to the parser and see exactly what's being output. Useful for debugging. Try it at: Parse Tester , treating the textbox as though it were the one on pos2.php. (Have at least a login page open in the same browser in order establish session vars.)


Change Log:

22Aug12 Eric Lee In "Testing" added some text and a link to Parse Tester.
20Aug12 Andy Theuninck