fileName; } public function setFileName($value) { $this->fileName =$value; } //== INTERNAL VARIABLES ======================================================= //variable: the collection of querystring variable pairs, e.g. "id=5" private $querystringVariables = null; //== CONSTRUCTORS ======================================================================= //constructor: function __construct($fileName) { //save parameters $this->fileName = $fileName; } //== PUBLIC METHODS ======================================================================= //method: adds a queryvariable public function addQuerystringVariable($name,$value) { //create it $querystringVariable = $name . '=' . $value; //add it $this->querystringVariables[] = $querystringVariable; } //method: public function render() { $r = ""; //build it $r .= $this->fileName . $this->buildQueryString(); return $r; } //== PRIVATE METHODS ======================================================================= //method: creates the querystring, e.g. "" or "?id=5" or "?id=5&status=on" private function buildQuerystring() { $r = ""; //if there are no variables, then don't build it if(count($this->querystringVariables) == 0) { //do nothing, the querystring will be blank } else { //go through each and build "id=5&count=66" string $count = 1; foreach($this->querystringVariables as $querystringVariable) { //add the variable $r .= $querystringVariable; //add the "&" if we are not on the last one if($count != count($this->querystringVariables)) { $r .= '&'; } //increment $count++; } //add the "?" (there is at least one variable here) $r = '?' . $r; } return $r; } } ?>collectionOfNames; } public function setCollectionOfNames($value) { $this->collectionOfNames =$value; } //property: CollectionOfValues: collection of values, e.g. "John" "Taylor" "35" private $collectionOfValues; public function getCollectionOfValues() { return $this->collectionOfValues; } public function setCollectionOfValues($value) { $this->collectionOfValues =$value; } //== INTERNAL VARIABLES ======================================================= //variable: the separator, e.g. "|�|" as in "firstName|�|John" private $separator; //variable: the collection of page state variables objects which we use to determine which values are allowed for the calling page, and defaults, etc. private $pageStateVariables; //== CONSTRUCTORS ======================================================================= //constructor: basic function __construct($pageStateVariables) { //save parameters $this->pageStateVariables = $pageStateVariables; //initialize values $this->initialize(); } //== PUBLIC METHODS ======================================================================= //method: defines the two sets of arrays as the name and value variables public function add($variableNameToAdd, $variableValueToAdd) { //variables $variableHasBeenProcessed = false; //loop through all variables, and if you find one with the same name then CHANGE its value, otherwise ADD the name and value at the end of the list for($x=0; $x <= $this->getCount() - 1; $x++) { //variables $variableName = $this->collectionOfNames[$x]; $variableValue = $this->collectionOfValues[$x]; //if this variable has the same name, then just change its value, and get out since we need to add no more if(qstr::areEqual($variableName,$variableNameToAdd)) { //variable names are the same so just change the value $this->collectionOfValues[$x] = $variableValueToAdd; //record that variable has been processed $variableHasBeenProcessed = true; //get out since we have already recorded this variable break; } } //if we didn't find the variable going through all of them (if this variable was not in our collection yet), then simply add it now if(!$variableHasBeenProcessed) { //add name $this->collectionOfNames[] = $variableNameToAdd; //add value $this->collectionOfValues[] = $variableValueToAdd; } } //method: parses through a standard URL string and fills the name value pairs public function fillNameValuePairsFromUrl($urlWithQuerystring) { //get the variable part off (e.g. id=383&displayTitle=yes&orderBy=rank) $querystring = trim(qstr::getTextAfterMarker($urlWithQuerystring,"?")); //if there is valid variable line content (e.g. id=383&displayTitle=yes&orderBy=rank), then process it if(!qstr::isEmpty($querystring) && qstr::contains($querystring,"=")) { //break up the pairs $pairs = explode("&",$querystring); //process the pairs $this->addFromVariablePairArray($pairs); } } //method: convert the current form variables into name value pairs public function fillNameValuePairsFromCurrentForm() { //get the current form variables as an array of pairs, e.g. "id=5" "kind=large" //TODO: this should be eventually change to GetFormVariables which is an array that will not have a problem if the value contains a "=" $formVariablePairs = qsys::getFormVariablesAsPairs(); //add the valid form variables to the internal collection of variables $this->addFromVariablePairArray($formVariablePairs); } //method: defines the two sets of arrays as the name and value variables public function displayTest() { $r = ""; //loop through for($x=0; $x <= $this->getCount() - 1; $x++) { //variables $name = $this->collectionOfNames[$x]; $value = $this->collectionOfValues[$x]; //show $r .= $name . " = " . $value . qstr::BR(); } //space after $r .= qstr::BR(); return $r; } //method: gets the number of vairables public function getCount() { //both collections will always have the same number of variables return count($this->collectionOfNames); } //method: takes a smart url and fills it with all the current variables public function fillSmartUrl($smartUrl) { //variables $numberOfVariables = $this->getCount(); //go throuh all return variables and add them to the url that is going back for($x=0; $x <= $numberOfVariables - 1; $x++) { //variables $variableName = $this->collectionOfNames[$x]; $variableValue = $this->collectionOfValues[$x]; //add variable $smartUrl->addQuerystringVariable($variableName, $variableValue); } } //method: takes a collection of name and value pairs and adds them, overriding any same-named variables which existed public function addNameValuePairObject($nameValuePairObject) { //variables $pairArrayToAdd = $this->getNamesAndValuesAsPairArray($nameValuePairObject); //process the pairs $this->addFromVariablePairArray($pairArrayToAdd); } //method: returns the names and values as an array of pairs, e.g. "city=berlin" "format=withIcons" public function getNamesAndValuesAsPairArray($nameValuePairCollection) { $ra = null; //loop through all internal name/values for($x=0; $x <= $nameValuePairCollection->getCount() - 1; $x++) { //variables $variableName = $nameValuePairCollection->m_CollectionOfNames[$x]; $variableValue = $nameValuePairCollection->m_CollectionOfValues[$x]; //define pair $pair = $variableName . " = " . $variableValue; //show $ra[] = $pair; } return $ra; } //method: gets the value of a specific variable public function getVariableValue($desiredVariableName) { $r = ""; //loop through all internal name/values for($x=0; $x <= $this->getCount() - 1; $x++) { //variables $variableName = $this->collectionOfNames[$x]; $variableValue = $this->collectionOfValues[$x]; //if this is it, then get the value if(qstr::areEqual($variableName,$desiredVariableName)) { //save the value $r .= $variableValue; //get out since we already have it break; } } return $r; } //== PRIVATE METHODS ======================================================================= //method: initialize necessary values, e.g. of internal variables private function initialize() { //define separator $this->separator = "|�|"; } //method: process pair arrays like "kind=large" "city=berlin" by comparing them to the page state variable filter and adding only the valid ones, and overwriting any variables that have the same name (only the last one counts) private function addFromVariablePairArray($pairs) { //run through the pairs and store them in the collections if(count($pairs) > 0) { //loop through each pair foreach($pairs as $pair) { //break up the pair $parts = explode("=", $pair); //variables $variableName = trim($parts[0]); $variableValue = trim($parts[1]); //add name and value to the collection, but only IF it is a registered variable for the current page (page) being processed if($this->variableIsRegistered($variableName)) { //it is a registered variable for this page, so add it to the collection $this->add($variableName,$variableValue); } else { //just ignore unregistered variables for now but you could program here the ability to capture "illegal variables" that are sent, for security and debugging purposes } } } } //method: returns whether or not a variable is a valid page state varaible for this page private function variableIsRegistered($variableName) { $rb = false; //loop through the page state variables and see if this variable name is in there if(count($this->pageStateVariables->getCollection()) > 0) { //loop through foreach($this->pageStateVariables->getCollection() as $pageStateVariable) { //variables $pageStateVariableIdCode = $pageStateVariable->IdCode(); //check it if(qstr::areEqual($variableName,$pageStateVariableIdCode)) { //it is registered, so note it $rb = true; //get out now since we already know it matches break; } } } return $rb; } } ?>output = $output; //initialize variables, etc. $this->initialize(); } //== PUBLIC METHODS ======================================================================= //method: combine all variables sent, process them and then create a new return url so we can go back to the page with the new variables in the querystring public function process() { //get the calling page url variables $this->getCallingPageUrlVariables(); //get the form variables sent from calling page $this->getFormVariables(); //get the querystring variables sent in querystring $this->getQuerystringVariables(); //resolve the return variables $this->resolveReturnVariables(); //do any custom editing on the outgoing page state variables here $this->doCustomProcessing(); //build returning url string $this->buildReturningUrlString(); } //method: for testing, shows all internal variables public function showInternalsForTesting() { $r = ""; //calling page url variables $r .= 'calling page url variables' . qstr::BR(); $r .= $this->callingPageUrlVariables->displayTest(); //form variables $r .= 'form variables' . qstr::BR(); $r .= $this->formVariables->displayTest(); //querystring variables $r .= 'querystring variables' . qstr::BR(); $r .= $this->querystringVariables->displayTest(); //return page $r .= 'the page we are going back to is: ' . $this->returnUrl . qstr::BR(); return $r; } //method: returns the return url to which we go after all variables are processed public function returnUrlWithNewVariablesInQueryString() { $r = ""; //build it $r .= $this->returnUrl; return $r; } //== PRIVATE METHODS ======================================================================= //method: initialize variables etc. private function initialize() { //fill the page state variables for the current calling page $this->getPageStateVariables(); //define calling page url variables collection $this->callingPageUrlVariables = new PageStateNameValuePairs($this->pageStateVariables); //define form variables collection $this->formVariables = new PageStateNameValuePairs($this->pageStateVariables); //define querystring variables collection $this->querystringVariables = new PageStateNameValuePairs($this->pageStateVariables); //define return variables collection $this->returnVariables = new PageStateNameValuePairs($this->pageStateVariables); } //method: fill the collection with page state variables for calling page private function getPageStateVariables() { //variables $callingPage = qsys::getCallingPageFileName(); $callingPageIdCode = qsys::removeExtensionFromFileName($callingPage); //get the calling page by itself, e.g. http://nnnn/nnnnn/pageStateShowcase.php5 (without any querystring variables), we use this as the id code for pages $this->callingPageUrl = qsys::getCallingPageWithoutQueryString(); //now get the calling page from that, e.g. pageStateShowcase.php5 $this->callingPage = qstr::getTextAfterLastMarkerSoft($this->callingPageUrl,"/"); //use the calling page to get the page state variables $dql = "pageIdCode = " . $callingPageIdCode; $this->pageStateVariables = new PageStateVariables($this->output, $dql); } //method: gets the calling page url variables private function getCallingPageUrlVariables() { //get the full url which contains the calling page url variables $callingPageWithUrlVariables = qsys::getCallingPage(); //define collection of variables coming from the url in the calling page $this->callingPageUrlVariables->fillNameValuePairsFromUrl($callingPageWithUrlVariables); } //method: gets the form variables where were sent private function getFormVariables() { //define collection of variables coming from the form $this->formVariables->fillNameValuePairsFromCurrentForm(); } //method: gets the querystring variables which were sent e.g. from links on calling page private function getQuerystringVariables() { //get the calling page url with variables in the url $currentPageUrlWithQuerystring = qsys::currentPageWithQuerystring(); //define collection of variables coming from the url in the calling page $this->querystringVariables->fillNameValuePairsFromUrl($currentPageUrlWithQuerystring); } //method: builds the PageStateNameValuePairs object of all variables which are going to be sent back to the calling page in the URL, these variables are built by resolving the others with the pagestate variables private function resolveReturnVariables() { $r = ""; //add calling page url variables $this->returnVariables->addNameValuePairObject($this->callingPageUrlVariables); //add form variables $this->returnVariables->addNameValuePairObject($this->formVariables); //add querystring variables $this->returnVariables->addNameValuePairObject($this->querystringVariables); return $r; } //== STATIC METHODS ======================================================================= //method: gets the current value of this variable based on the default value and the value which overrides it in the querystring public function getVariableValue($variableIdCode) { $r = ""; //get the current file name $currentPageIdCode = qsys::getCurrentPageIdCode(); //get default value $defaultValue = PageStateVariable::buildDefaultValue($currentPageIdCode, $variableIdCode); //get value from querystring $querystringValue = qsys::getQuerystringVariable($variableIdCode); //now process the value: if nothing else, it should equal the default value $r = $defaultValue; //if there IS a querystring value then override if(!qstr::isEmpty($querystringValue)) { //override value with the querystring $r = $querystringValue; } return $r; } //method: display a drop down with an item, pagestateline is something like "itemTypes:idCode,name" public static function dropDownWithItem($output, $variableName, $pageStateLine, $dql, $textAfter = "", $optionLine = "") { $r = ""; //get the item id code from the pagestateline, e.g. "itemTypes" out of "itemTypes:idCode,name" $itemTypeIdCode = qstr::getTextBeforeMarkerSoft($pageStateLine,":"); //get the value and name field variables, e.g. "idCode,name" out of "itemTypes:idCode,name" $valueAndNameFields = qstr::getTextAfterMarkerSoft($pageStateLine,":"); $parts = explode(",",$valueAndNameFields); $valueFieldIdCode = $parts[0]; $nameFieldIdCode = $parts[1]; //instantiate the collection of item type here $items = Items::instantiate($output,$itemTypeIdCode,$dql); //loop through the items and make the two arrays if(count($items->getCollection()) > 0) { foreach($items->getCollection() as $item) { //variables $value =$item->getPropertyValue($valueFieldIdCode); $name =$item->getPropertyValue($nameFieldIdCode); //add to arrays $valuesArray[] = $value; $namesArray[] = $name; } } //build the dropdown $r .= PageState::buildRawDropDown($output, $variableName, $valuesArray, $namesArray, $textAfter, $optionLine); return $r; } //method: displays a drop down, pagestateline is something like "ISO|Clean Machine|Used Machine|Network", optionLine is e.g. "excludeIfNotSelected=search results" public static function dropDown($output, $variableName, $pageStateLine, $criteriaLine = "", $textAfter = "", $optionLine = "") { $r = ""; //if he wants to display an item, then call a specific function for that (we allow this for simplicity sake at the presenation level -- always just called DropDown(...)) if(qstr::contains($pageStateLine, ":")) { return PageState::dropDownWithItem($output, $variableName, $pageStateLine, $criteriaLine, $textAfter, $optionLine); } //define the value and text array variables $valuesArray = null; $namesArray = null; //parse the page state line PageState::parsePageStateLine($pageStateLine,&$valuesArray, &$namesArray); $r .= PageState::buildRawDropDown($output, $variableName, $valuesArray, $namesArray, $textAfter, $optionLine); return $r; } //method: display a text box with button, e.g. used for search boxes so that the search string is always passed back as string (convenient for links) public function textBox($output, $variableName, $buttonText) { $r = ""; //get current search text $searchText = PageState::getVariableValue("searchText"); //start page state form $formName = "form" . qstr::forcePascalNotation($variableName); $r .= PageState::beginForm($formName); //build it $r .= ' '; //start page state form $r .= PageState::endForm(); return $r; } //method: takes the two lists of variables value/name and builds the drop down public static function buildRawDropDown($output, $variableName, $valuesArray, $namesArray, $textAfter = "", $optionLine = "") { $r = ""; //variables $currentVariableValue = PageState::getVariableValue($variableName); $excludedIfNotSelectedAndIncludesThisText = qstr::getParameterValue("excludedIfNotSelectedAndIncludesThisText",$optionLine); $excludedIfNotSelectedAndIncludesThisText2 = qstr::getParameterValue("excludedIfNotSelectedAndIncludesThisText2",$optionLine); //define number of choices $numberOfChoices = count($valuesArray); //start page state form $formName = "form" . qstr::forcePascalNotation($variableName); $r .= PageState::beginForm($formName); //start dropdown html $r .= '' . $textAfter . qstr::NEW_LINE(); //start page state form $r .= PageState::endForm(); return $r; } //method: converts a page state line (e.g. "Sort by Date|Sort by Title|Sort by Score") into two arrays of values and texts public static function parsePageStateLine($pageStateLine, &$valuesArray, &$namesArray) { //break up the page state line $parts = explode("|",$pageStateLine); //go through all parts and build the values and texts arrays //make sure there is at least one if(count($parts) > 0) { //loop through foreach($parts as $part) { //add to values array $valuesArray[] = qstr::forceCamelNotation($part); //add to texts array $namesArray[] = $part; } } } //method: sends the HTML for the beginning of the page state form (the form tag) public static function beginForm($formName) { $r = ""; //build it $r .= '
' . qstr::NEW_LINE(); return $r; } //method: sends the HTML for the ending of the page state form (the form tag) public static function endForm() { $r = ""; //build it $r .= '
' . qstr::NEW_LINE(); return $r; } //method: builds the url to which the page will be sent back, including all processed variables which maintain the state private function buildReturningUrlString() { //build the url to go back to $smartUrl = new SmartUrl($this->callingPage); //create the parameter string for the smart url from the collected variables $this->returnVariables->fillSmartUrl($smartUrl); //build the actual url string $this->returnUrl = $smartUrl->render(); } //method: this is a page to call methods which do custom processing on various forms private function doCustomProcessing() { //blank out search text if necessary $this->handleChangingDisplayGroups(); //if searching, display the "Searching..." display group $this->handleSearching(); } //method: if they changed the display group, then blanks out the search text private function handleChangingDisplayGroups() { //get the previous display group id code $previousDisplayGroupIdCode = $this->callingPageUrlVariables->getVariableValue("displayGroupIdCode"); //get the chosen display group id code $chosenDisplayGroupIdCode = $this->returnVariables->getVariableValue("displayGroupIdCode"); //if they are different, then blank out the search text if(!qstr::areEqual($previousDisplayGroupIdCode, $chosenDisplayGroupIdCode)) { //delete the search text and id //todo: make the delete method in pageStateNameValuePairs, but for now, ADD a blank variable $this->returnVariables->add("searchText",""); $this->returnVariables->add("id",""); } } //method: make sure there is a display group when searching private function handleSearching() { //get the chosen display group id code $searchText = $this->returnVariables->getVariableValue("searchText"); //get the display group id code to switch it to if we are searching //todo: fix this so that it is more integrated, since here we are assuming that the search page is the default search page, e.g. "meetingRooms.php" $displayGroupIdCode = $this->getDisplayGroupIdCodeFromCurrentFileName(); //if they are different, then blank out the search text if(!qstr::isEmpty($searchText)) { //change the display group to "Searching..." $this->returnVariables->add("displayGroupIdCode", $displayGroupIdCode); //also blank out id $this->returnVariables->add("id",""); } } //method: this assumes that searchign is being done from a display page (which is usually the case if searchText is called "searchText"), so get display group id code, e.g. "meetingRooms-searchResults" private function getDisplayGroupIdCodeFromCurrentFileName() { $r = ""; //get the display group id for searching, e.g. "meetingRooms.php" $callingPageFileName = qsys::getCallingPage(); //take off the .php extension, e.g. "meetingRooms" $pluralItemIdCode = qstr::chopRight(".php",$callingPageFileName); //now define the appropriate display group id code, e.g. "meetingRooms-searchResults" $displayGroupIdCode = $pluralItemIdCode . "-searchResults"; //assign it to the outgoing text $r = $displayGroupIdCode; return $r; } } ?>