code/pagetypes/MetadataPostcardEntryPage.php
/**
     * Processes submits of the metadata entry form.
     * @param  Array $data
     * @param  Form $form
     */
Processes submits of the metadata entry form.
@param  Array $data
@param  Form $form
Processes submits of the metadata entry form . @param Array $data @param Form $form
public function sendMetadataForm($data, $form)
    {
        $apiParams = array();
        foreach($this->Fields()->sort('SortOrder', 'asc') as $field) {
            $fieldName = str_replace(' ', '_', $field->Label);
            if ($field->FieldType != 'PLACEHOLDER') {
                if (isset($data[$fieldName])) {
                    if ($field->DropdownOtherOption && $data[$fieldName] == 'other') {
                        if (isset($data["${fieldName}_other"])) {
                            $apiParams[] = array('xmlname' => $field->DublinCoreFieldType()->XmlName, 'value' => trim($data["${fieldName}_other"]));
                        } else {
                            $apiParams[] = array('xmlname' => $field->DublinCoreFieldType()->XmlName, 'value' => trim($data[$fieldName]));
                        }
                    } else {
                        if ($field->FieldType == 'KEYWORDS') {
                            $keywordsString = 'Keywords: ';
                            if (trim($field->KeywordsValue)) {
                                $keywordsString .= rtrim(trim($field->KeywordsValue), ',') . ', ';
                            }
                            if (trim($data[$fieldName])) {
                                $keywordsString .= trim($data[$fieldName]);
                            }
                            $apiParams[] = array('xmlname' => $field->DublinCoreFieldType()->XmlName, 'value' => $keywordsString);
                        } else {
                            $apiParams[] = array('xmlname' => $field->DublinCoreFieldType()->XmlName, 'value' => trim($data[$fieldName]));
                        }
                    }
                }
            } else {
                $apiParams[] = array('xmlname' => $field->DublinCoreFieldType()->XmlName, 'value' => $field->PlaceholderValue);
            }
        }
        $api = new MetadataPushApi($this->CataloguePushUrl, $this->CatalogueUsername, $this->CataloguePassword);
        $apiError = false;
        $apiErrorMessage = "";
        $newRecordId = "";
        try {
            $newRecordId = $api->execute($apiParams);
        }
        catch (Exception $exception) {
            $apiError = true;
            $apiErrorMessage = $exception->getMessage();
        }
        if ($apiError) {
            Session::set("FormData.{$form->getName()}.data", $data);
            $errorMessage = nl2br(str_replace('{ERROR}', $apiErrorMessage, $this->PushFailureMessage));
            $form->sessionMessage($errorMessage, 'bad', false);
            return $this->redirectBack();
        } else {
            $newRecordLink = $this->CatalogueViewUrl . $newRecordId;
            $additionalMessageEmail = null;
            $additionalMessageText = null;
            if (!empty($data['AdditionalMessage'])) {
                $additionalMessageEmail = $data['AdditionalMessageEmail'];
                $additionalMessageText = $data['AdditionalMessageText'];
            }
            foreach($this->Curators() as $curator) {
                $this->EmailCurator($curator->Name, $curator->Email, $this->CuratorEmailSubject, $this->CuratorEmailBody, $newRecordLink, $additionalMessageEmail, $additionalMessageText);
            }
            Session::clear("FormData.{$form->getName()}.data");
            $successMessage = nl2br(str_replace('{LINK}', "<a href='" . $newRecordLink . "' target='_blank'>" . $newRecordLink . "</a>", $this->PushSuccessMessage));
            $form->sessionMessage($successMessage, 'good', false);
            $createdRecords = Session::get('PostcardRecordsCreated_' . $this->ID);
            if (count($createdRecords) == 0) {
                $this->EmailCoordinator($data);
            }
            $createdRecords[] = $newRecordLink;
            Session::set('PostcardRecordsCreated_' . $this->ID, $createdRecords);
            return $this->redirectBack();
        }
    }
public function sendMetadataForm ( $ data , $ form ) { $ apiParams = array ( ) ; foreach ( $ this -> Fields ( ) -> sort ( 'SortOrder' , 'asc' ) as $ field ) { $ fieldName = str_replace ( ' ' , '_' , $ field -> Label ) ; if ( $ field -> FieldType != 'PLACEHOLDER' ) { if ( isset ( $ data [ $ fieldName ] ) ) { if ( $ field -> DropdownOtherOption && $ data [ $ fieldName ] == 'other' ) { if ( isset ( $ data [ " $ { fieldName } _other " ] ) ) { $ apiParams [ ] = array ( 'xmlname' => $ field -> DublinCoreFieldType ( ) -> XmlName , 'value' => trim ( $ data [ " $ { fieldName } _other " ] ) ) ; } else { $ apiParams [ ] = array ( 'xmlname' => $ field -> DublinCoreFieldType ( ) -> XmlName , 'value' => trim ( $ data [ $ fieldName ] ) ) ; } } else { if ( $ field -> FieldType == 'KEYWORDS' ) { $ keywordsString = 'Keywords: ' ; if ( trim ( $ field -> KeywordsValue ) ) { $ keywordsString .= rtrim ( trim ( $ field -> KeywordsValue ) , ',' ) . ', ' ; } if ( trim ( $ data [ $ fieldName ] ) ) { $ keywordsString .= trim ( $ data [ $ fieldName ] ) ; } $ apiParams [ ] = array ( 'xmlname' => $ field -> DublinCoreFieldType ( ) -> XmlName , 'value' => $ keywordsString ) ; } else { $ apiParams [ ] = array ( 'xmlname' => $ field -> DublinCoreFieldType ( ) -> XmlName , 'value' => trim ( $ data [ $ fieldName ] ) ) ; } } } } else { $ apiParams [ ] = array ( 'xmlname' => $ field -> DublinCoreFieldType ( ) -> XmlName , 'value' => $ field -> PlaceholderValue ) ; } } $ api = new MetadataPushApi ( $ this -> CataloguePushUrl , $ this -> CatalogueUsername , $ this -> CataloguePassword ) ; $ apiError = false ; $ apiErrorMessage = " " ; $ newRecordId = " " ; try { $ newRecordId = $ api -> execute ( $ apiParams ) ; } catch ( Exception $ exception ) { $ apiError = true ; $ apiErrorMessage = $ exception -> getMessage ( ) ; } if ( $ apiError ) { Session :: set ( " FormData. { $ form -> getName ( ) } .data " , $ data ) ; $ errorMessage = nl2br ( str_replace ( '{ERROR}' , $ apiErrorMessage , $ this -> PushFailureMessage ) ) ; $ form -> sessionMessage ( $ errorMessage , 'bad' , false ) ; return $ this -> redirectBack ( ) ; } else { $ newRecordLink = $ this -> CatalogueViewUrl . $ newRecordId ; $ additionalMessageEmail = null ; $ additionalMessageText = null ; if ( ! empty ( $ data [ 'AdditionalMessage' ] ) ) { $ additionalMessageEmail = $ data [ 'AdditionalMessageEmail' ] ; $ additionalMessageText = $ data [ 'AdditionalMessageText' ] ; } foreach ( $ this -> Curators ( ) as $ curator ) { $ this -> EmailCurator ( $ curator -> Name , $ curator -> Email , $ this -> CuratorEmailSubject , $ this -> CuratorEmailBody , $ newRecordLink , $ additionalMessageEmail , $ additionalMessageText ) ; } Session :: clear ( " FormData. { $ form -> getName ( ) } .data " ) ; $ successMessage = nl2br ( str_replace ( '{LINK}' , " <a href=' " . $ newRecordLink . " ' target='_blank'> " . $ newRecordLink . " </a> " , $ this -> PushSuccessMessage ) ) ; $ form -> sessionMessage ( $ successMessage , 'good' , false ) ; $ createdRecords = Session :: get ( 'PostcardRecordsCreated_' . $ this -> ID ) ; if ( count ( $ createdRecords ) == 0 ) { $ this -> EmailCoordinator ( $ data ) ; } $ createdRecords [ ] = $ newRecordLink ; Session :: set ( 'PostcardRecordsCreated_' . $ this -> ID , $ createdRecords ) ; return $ this -> redirectBack ( ) ; } }
Processes submits of the metadata entry form.
Processes submits of the metadata entry form .
// Specify array for the params to send to the API. Can't be associative and keyed by the xmlname // as the Dublin core specification allows for multiple of the same field. // Loop through the defined fields grabbing the POSTed data for it, or if a placeholder // then just adding it and the specified value to the apiParams. // Like when the fields are looped through to create the form, str_replace the label to create the fieldname. // If the field type is not a placeholder then get the data POSTed for it. // Check if the field is a dropdown and the 'other' option has been checked for it // if so then if the value selected is 'other' we need to grab the value out of the _other // field for it and pop that in the API params instead of the value selected in the dropdown. // Check if the field is a Keywords field. If so we need to append the string "Keywords: " // along with the keywords the admin specified to those entered by the user. // If the admin entered some keywords (which is optional) add with one comma afterwards. // If the user entered some keywords then also add these on. // Finally set the keywords for the parameter to send to the catalogue. // Else if regular field then just add its value to the params keyed by the XMLName. // If it is placeholder then add it along with the admin defined placeholder value to the API params. // Create an object of the MetadataPushAPI type sending the needed info to the constructor and then call the // execute command passing the apiParams. We need to try/catch it as it will throw and exception if there is // an issue or return the identifer of the newly created record if successful. // If the push was not successful, then we need to retain the user's input on the form i.e. re-display it with all fields re-populated. // Remember the user's data, this will cause the form to re-populate. // Get the failure error message defined in the CMS and replace the speical {ERROR} variable with // the error message from the Catalogue API or HTML error code message. // Populate the error message to display on the form when it is re-loaded. // Return the user back to the form page they just posted. // No error, build link to view the new record which is displayed on the page and also included in the email. // See if the AdditionalMessage checkbox is ticked, if so then get the value of the email user address // and User email message fields as this needs to be included in the email to the curator. // Email the curators. // Ensure that any form data in the session is cleared. // Get the success message replacing the link placeholder with a link to the newly created record. // Set the message in the session to this. // Also add the link to the newly created record to an array in the session so we can display these on the form. // The links should be page specific which is why it ID is included. // If there are no records already in the current session then call function to check for the hidden fields on // the form which provide the information required to email a project coordinator, and if they exist sent an email. // Add the new reocrd to the created records array and then set back in the session. // Return the user back to the form. The redirect back will take the user back to the form // with the original URL parameters which means the fields with parameters on the URL will be // populated again like they want.