code/models/PostcardMetadataField.php
/**
     * Populates the item for a dropdown. Can be called recursivley.
     *
     * @param  String  $nextUrl
     * @param  integer $sortOrder
     * @param  String $errorMessage
     */
Populates the item for a dropdown. Can be called recursivley.
Populates the item for a dropdown . Can be called recursivley .
protected function populateDropdownFromVocab($nextUrl="", $sortOrder=1, $errorMessage="")
    {
        $results = null;
        $status = 0;
        $statusMessage = "";
        if ($nextUrl) {
            list($results, $status, $statusMessage) = $this->makeCurlRequest($nextUrl);
        } else {
            $deleteResult = $this->deleteCurrentItems();
            if (!$deleteResult) {
                $errorMessage .= "Error deleting current items, can't reload. ";
            } else {
                list($results, $status, $statusMessage) = $this->makeCurlRequest($this->DropdownVocabularyUrl . '?_sort=prefLabel&_pageSize=50');
            }
        }
        if ($status == 200 && $results) {
            $xml = "";
            try {
                $xml = new SimpleXMLElement($results);
            }
            catch (Exception $e) {
                $errorMessage .= 'The XML failed to parse. ';
            }
            if ($xml) {
                if (isset($xml->items)) {
                    foreach($xml->items->item as $anItem) {
                        if (isset($anItem->prefLabel)) {
                            $dropdownEntry = DropdownEntry::create();
                            $dropdownEntry->PostcardMetadataFieldID = $this->ID;
                            $dropdownEntry->Key = trim($anItem->prefLabel);
                            $dropdownEntry->Label = trim($anItem->prefLabel);
                            $dropdownEntry->SortOrder = $sortOrder;
                            $dropdownEntry->write();
                            $sortOrder ++;
                        }
                    }
                    if (isset($xml->next)) {
                        $errorMessage .= $this->populateDropdownFromVocab($xml->next['href'], $sortOrder, $errorMessage);
                    }
                } else {
                    $errorMessage .= "No items found in the XML so can't populate the dropdown items. ";
                }
            } else {
                $errorMessage .= 'No XML to process. ';
            }
        } else {
            $errorMessage .= 'Error getting data from the vocab server. ' . $status . ' : ' . $statusMessage . '. ';
        }
        return $errorMessage;
    }
protected function populateDropdownFromVocab ( $ nextUrl = " " , $ sortOrder = 1 , $ errorMessage = " " ) { $ results = null ; $ status = 0 ; $ statusMessage = " " ; if ( $ nextUrl ) { list ( $ results , $ status , $ statusMessage ) = $ this -> makeCurlRequest ( $ nextUrl ) ; } else { $ deleteResult = $ this -> deleteCurrentItems ( ) ; if ( ! $ deleteResult ) { $ errorMessage .= " Error deleting current items, can't reload.  " ; } else { list ( $ results , $ status , $ statusMessage ) = $ this -> makeCurlRequest ( $ this -> DropdownVocabularyUrl . '?_sort=prefLabel&_pageSize=50' ) ; } } if ( $ status == 200 && $ results ) { $ xml = " " ; try { $ xml = new SimpleXMLElement ( $ results ) ; } catch ( Exception $ e ) { $ errorMessage .= 'The XML failed to parse. ' ; } if ( $ xml ) { if ( isset ( $ xml -> items ) ) { foreach ( $ xml -> items -> item as $ anItem ) { if ( isset ( $ anItem -> prefLabel ) ) { $ dropdownEntry = DropdownEntry :: create ( ) ; $ dropdownEntry -> PostcardMetadataFieldID = $ this -> ID ; $ dropdownEntry -> Key = trim ( $ anItem -> prefLabel ) ; $ dropdownEntry -> Label = trim ( $ anItem -> prefLabel ) ; $ dropdownEntry -> SortOrder = $ sortOrder ; $ dropdownEntry -> write ( ) ; $ sortOrder ++ ; } } if ( isset ( $ xml -> next ) ) { $ errorMessage .= $ this -> populateDropdownFromVocab ( $ xml -> next [ 'href' ] , $ sortOrder , $ errorMessage ) ; } } else { $ errorMessage .= " No items found in the XML so can't populate the dropdown items.  " ; } } else { $ errorMessage .= 'No XML to process. ' ; } } else { $ errorMessage .= 'Error getting data from the vocab server. ' . $ status . ' : ' . $ statusMessage . '. ' ; } return $ errorMessage ; }
Populates the item for a dropdown.
Populates the item for a dropdown .
// This function is called recursively because the vocab data is paginated. // The first time the nextUrl is empty so the base url to the vocab server is used and some parameters added. // If in the response there is a next URL then we call this function again passing that in to load the next page of data. // Delete all current entries for this dropdown as we are going to re-populate the list from the vocab. // If there was an error deleting then throw exception as don't want to double-up the entries. // Add parameters to sort the results by label and get the maximum allowed number of records (50) per request. // If no error then process the output // Parse the XML. // If have some measurements. // Loop through the items. // Get the prefLabel which is the label to sell. // Create a dropdown entry object, set its properties, and then write. // Now check for a 'next' tag in the results, if there is then there is more than 1 // page of results so we need to get that link then recursivley call this function // Ensure that the error message is