function performAction_organizations($getPage_connection2)
{
    if ($_SESSION["action"] == "create") {
        createOrganization($getPage_connection2);
    } else {
        if ($_SESSION["action"] == "invite") {
            inviteNation($getPage_connection2);
        } else {
            if ($_SESSION["action"] == "kick") {
                kickNation($getPage_connection2);
            } else {
                if ($_SESSION["action"] == "appoint") {
                    appointManager($getPage_connection2);
                } else {
                    if ($_SESSION["action"] == "dismiss") {
                        dismissManager($getPage_connection2);
                    } else {
                        if ($_SESSION["action"] == "leave") {
                            leaveOrganization($getPage_connection2);
                        } else {
                            if ($_SESSION["action"] == "join") {
                                joinOrganization($getPage_connection2);
                            } else {
                                if ($_SESSION["action"] == "decline") {
                                    declineOrganization($getPage_connection2);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    // else if
}
function build_postcard_mods_data($repository, $postcardData)
{
    $mods = "<?xml version=\"1.0\"?>";
    $mods .= "<mods xmlns=\"http://www.loc.gov/mods/v3\" xmlns:marmot=\"http://marmot.org/local_mods_extension\" xmlns:mods=\"http://www.loc.gov/mods/v3\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">";
    $mods .= "<titleInfo>\r\n";
    $mods .= "<title>" . htmlspecialchars($postcardData['title']) . "</title>\r\n";
    $mods .= "</titleInfo>\r\n";
    $mods .= "<mods:genre>postcard</mods:genre>\r\n";
    $mods .= "<identifier>" . htmlspecialchars($postcardData['acsnId']) . "</identifier>\r\n";
    $mods .= "<identifier>" . htmlspecialchars($postcardData['itemId']) . "</identifier>\r\n";
    if (strlen($postcardData['dateCreated']) > 0) {
        $mods .= "<originInfo>\r\n";
        $qualifer = '';
        if (preg_match('/ca./', $postcardData['dateCreated'])) {
            $qualifer = 'approximate';
            $postcardData['dateCreated'] = trim(str_replace('ca.', '', $postcardData['dateCreated']));
        } elseif (preg_match('/circa/', $postcardData['dateCreated'])) {
            $qualifer = 'approximate';
            $postcardData['dateCreated'] = trim(str_replace('circa', '', $postcardData['dateCreated']));
        } elseif (preg_match("/'s/", $postcardData['dateCreated'])) {
            $qualifer = 'approximate';
            $postcardData['dateCreated'] = trim(str_replace("'s", '', $postcardData['dateCreated']));
        } elseif (preg_match('/\\d{4}s/', $postcardData['dateCreated'])) {
            $qualifer = 'approximate';
            $postcardData['dateCreated'] = trim(str_replace("s", '', $postcardData['dateCreated']));
        }
        if (preg_match('/^([\\d-]+)\\/([\\d-]+)$/', $postcardData['dateCreated'], $matches)) {
            $mods .= "<dateCreated point='start' qualifier='{$qualifer}'>" . htmlspecialchars($matches[1]) . "</dateCreated>\r\n";
            $mods .= "<dateCreated point='end' qualifier='{$qualifer}'>" . htmlspecialchars($matches[2]) . "</dateCreated>\r\n";
        } elseif (preg_match('/^\\d+\\/\\d+\\/\\d+$/', $postcardData['dateCreated'], $matches)) {
            $mods .= "<dateCreated point='start' qualifier='{$qualifer}'>" . htmlspecialchars($postcardData['dateCreated']) . "</dateCreated>\r\n";
        } elseif (preg_match('/^\\d+-\\d+-\\d+$/', $postcardData['dateCreated'], $matches)) {
            $mods .= "<dateCreated point='start' qualifier='{$qualifer}'>" . htmlspecialchars($postcardData['dateCreated']) . "</dateCreated>\r\n";
        } elseif (preg_match('/^(\\d{4})-(\\d{4})$/', $postcardData['dateCreated'], $matches)) {
            $mods .= "<dateCreated point='start' qualifier='{$qualifer}'>" . htmlspecialchars($matches[1]) . "</dateCreated>\r\n";
            $mods .= "<dateCreated point='end' qualifier='{$qualifer}'>" . htmlspecialchars($matches[2]) . "</dateCreated>\r\n";
        } else {
            $mods .= "<dateCreated point='start' qualifier='{$qualifer}'>" . htmlspecialchars($postcardData['dateCreated']) . "</dateCreated>\r\n";
        }
        $mods .= "</originInfo>\r\n";
    }
    $mods .= "<abstract>" . htmlspecialchars($postcardData['description']) . "</abstract>\r\n";
    $allSubjects = array();
    foreach ($postcardData['lc_subjects'] as $fullSubject) {
        $subjectParts = explode('|', $fullSubject);
        //Always add the first part of the subject |a
        //$allSubjects[$subjectParts[0]] = $subjectParts[0];
        $fullSubject = $subjectParts[0];
        for ($i = 1; $i < count($subjectParts); $i++) {
            $subjectWithoutIndicator = substr($subjectParts[$i], 1);
            $subfield = substr($subjectParts[$i], 0, 1);
            switch ($subfield) {
                case 'z':
                    //Ignore geographic portions of the LC subject since we have that as part of related entity
                    break;
                case 'v':
                    //Add form
                //Add form
                case 'x':
                    //Add general subdivision
                    $subjectWithForm = $subjectParts[0] . ' -- ' . $subjectWithoutIndicator;
                    $fullSubject .= ' -- ' . $subjectWithoutIndicator;
                    $allSubjects[$subjectWithForm] = $subjectWithForm;
                    break;
                default:
                    echo "Unhandled subdivision {$subfield}";
            }
        }
        $allSubjects[$fullSubject] = $fullSubject;
    }
    foreach ($allSubjects as $subject) {
        $mods .= "<subject authority='lcsh'>\r\n";
        $mods .= "<topic>" . htmlspecialchars($subject) . "</topic>\r\n";
        $mods .= "</subject>\r\n";
    }
    $mods .= "<language>\r\n";
    $mods .= "<languageTerm authority='iso639-2b' type='code'>English</languageTerm>\r\n";
    $mods .= "</language>\r\n";
    $mods .= "<physicalDescription>\r\n";
    $mods .= "<extent>" . htmlspecialchars($postcardData['itemName']) . "</extent>\r\n";
    $mods .= "<form type='material'>" . htmlspecialchars($postcardData['medium']) . "</form>\r\n";
    $mods .= "</physicalDescription>\r\n";
    $mods .= "<physicalLocation>" . htmlspecialchars($postcardData['volumeNumber'] . ' ' . $postcardData['photoNumber']) . "</physicalLocation>\r\n";
    $mods .= "<extension>\r\n";
    $mods .= "<marmot:marmotLocal>\r\n";
    $mods .= "<marmot:correspondence>\r\n";
    $mods .= "<marmot:postcardPublisherNumber>" . htmlspecialchars($postcardData['pubPhotoNumber']) . "</marmot:postcardPublisherNumber>\r\n";
    $mods .= "</marmot:correspondence>\r\n";
    foreach ($postcardData['creators'] as $creator) {
        //reverse firstname and last name for people
        $creatorNameNormalized = trim($creator['name']);
        if (stripos($creator['type'], 'person') !== false) {
            if (strpos($creatorNameNormalized, ',') > 0) {
                $names = explode(',', $creatorNameNormalized, 2);
                $creatorNameNormalized = trim($names[1] . ' ' . $names[0]);
            }
        }
        $entityPID = doesEntityExist($creatorNameNormalized);
        //If entity does not exist, we should create it
        if ($entityPID == false) {
            if (stripos($creator['type'], 'person') !== false) {
                $entityPID = createPerson($repository, $creatorNameNormalized);
            } else {
                $entityPID = createOrganization($repository, $creatorNameNormalized);
            }
        }
        if (stripos($creator['type'], 'donor') !== false) {
            $mods .= "<marmot:relatedPersonOrg>\r\n";
            $mods .= "<marmot:role>donor</marmot:role>\r\n";
            $mods .= "<marmot:entityPid>{$entityPID}</marmot:entityPid>\r\n";
            $mods .= "<marmot:entityTitle>" . htmlspecialchars($creatorNameNormalized) . "</marmot:entityTitle>\r\n";
            $mods .= "</marmot:relatedPersonOrg>\r\n";
        } elseif (stripos($creator['type'], 'photographer') !== false) {
            $mods .= "<marmot:hasCreator role='photographer'>\r\n";
            $mods .= "<marmot:entityPid>{$entityPID}</marmot:entityPid>\r\n";
            $mods .= "<marmot:entityTitle>" . htmlspecialchars($creatorNameNormalized) . "</marmot:entityTitle>\r\n";
            $mods .= "</marmot:hasCreator>\r\n";
        } elseif (stripos($creator['type'], 'artist') !== false) {
            $mods .= "<marmot:hasCreator role='artist'>\r\n";
            $mods .= "<marmot:entityPid>{$entityPID}</marmot:entityPid>\r\n";
            $mods .= "<marmot:entityTitle>" . htmlspecialchars($creatorNameNormalized) . "</marmot:entityTitle>\r\n";
            $mods .= "</marmot:hasCreator>\r\n";
        } elseif (stripos($creator['type'], 'author') !== false) {
            $mods .= "<marmot:hasCreator role='author'>\r\n";
            $mods .= "<marmot:entityPid>{$entityPID}</marmot:entityPid>\r\n";
            $mods .= "<marmot:entityTitle>" . htmlspecialchars($creatorNameNormalized) . "</marmot:entityTitle>\r\n";
            $mods .= "</marmot:hasCreator>\r\n";
        } elseif (stripos($creator['type'], 'owner') !== false) {
            $mods .= "<marmot:relatedPersonOrg>\r\n";
            $mods .= "<marmot:role>owner</marmot:role>\r\n";
            $mods .= "<marmot:entityPid>{$entityPID}</marmot:entityPid>\r\n";
            $mods .= "<marmot:entityTitle>" . htmlspecialchars($creatorNameNormalized) . "</marmot:entityTitle>\r\n";
            $mods .= "</marmot:relatedPersonOrg>\r\n";
        } elseif (stripos($creator['type'], 'publisher') !== false) {
            $mods .= "<marmot:relatedPersonOrg>\r\n";
            $mods .= "<marmot:role>publisher</marmot:role>\r\n";
            $mods .= "<marmot:entityPid>{$entityPID}</marmot:entityPid>\r\n";
            $mods .= "<marmot:entityTitle>" . htmlspecialchars($creatorNameNormalized) . "</marmot:entityTitle>\r\n";
            $mods .= "</marmot:relatedPersonOrg>\r\n";
        } elseif (stripos($creator['type'], 'producer') !== false) {
            $mods .= "<marmot:hasCreator role='producer'>\r\n";
            $mods .= "<marmot:entityPid>{$entityPID}</marmot:entityPid>\r\n";
            $mods .= "<marmot:entityTitle>" . htmlspecialchars($creatorNameNormalized) . "</marmot:entityTitle>\r\n";
            $mods .= "</marmot:hasCreator>\r\n";
        } else {
            echo "Unhandled creator type " . $creator['type'];
        }
    }
    foreach ($postcardData['related_people'] as $person) {
        $creatorNameNormalized = $person;
        if (strpos($creatorNameNormalized, ',') > 0) {
            $names = explode(',', $creatorNameNormalized, 2);
            $creatorNameNormalized = trim($names[1] . ' ' . $names[0]);
        }
        $entityPID = doesEntityExist($creatorNameNormalized);
        //If entity does not exist, we should create it
        if ($entityPID == false) {
            $entityPID = createPerson($repository, $creatorNameNormalized);
        }
        $mods .= "<marmot:relatedPersonOrg>\r\n";
        $mods .= "<marmot:entityPid>{$entityPID}</marmot:entityPid>\r\n";
        $mods .= "<marmot:entityTitle>" . htmlspecialchars($creatorNameNormalized) . "</marmot:entityTitle>\r\n";
        $mods .= "</marmot:relatedPersonOrg>\r\n";
    }
    foreach ($postcardData['related_organizations'] as $organization) {
        $entityPID = doesEntityExist($organization);
        //If entity does not exist, we should create it
        if ($entityPID == false) {
            $entityPID = createOrganization($repository, $organization);
        }
        $mods .= "<marmot:picturedEntity>\r\n";
        $mods .= "<marmot:entityPid>{$entityPID}</marmot:entityPid>\r\n";
        $mods .= "<marmot:entityTitle>" . htmlspecialchars($organization) . "</marmot:entityTitle>\r\n";
        $mods .= "</marmot:picturedEntity>\r\n";
    }
    foreach ($postcardData['geo_place_ids'] as $placeId) {
        //Find the place in solr based on information entered as external data
        //mods_extension_marmotLocal_externalLink_fortLewisGeoPlaces_link_mlt
        list($entityPID, $title) = findPlaceByFortLewisId($placeId);
        if ($entityPID != false) {
            $mods .= "<marmot:picturedEntity>\r\n";
            $mods .= "<marmot:entityPid>{$entityPID}</marmot:entityPid>\r\n";
            $mods .= "<marmot:entityTitle>" . htmlspecialchars($title) . "</marmot:entityTitle>\r\n";
            $mods .= "</marmot:picturedEntity>\r\n";
        }
    }
    $mods .= "<marmot:pikaOptions>\r\n";
    $mods .= "<marmot:includeInPika>yes</marmot:includeInPika>\r\n";
    $mods .= "<marmot:showInSearchResults>yes</marmot:showInSearchResults>\r\n";
    $mods .= "</marmot:pikaOptions>\r\n";
    $mods .= "</marmot:marmotLocal>\r\n";
    $mods .= "</extension>\r\n";
    $mods .= "<mods:accessCondition>\r\n";
    $mods .= "<marmot:rightsStatement>\r\n";
    $mods .= "The Center of Southwest Studies is not aware of any U.S. copyright or any other restrictions in the postcards in this collection.  However, some of the content may be protected by the U.S.  Copyright Law (Title 17, U.S.C.) and/or by the copyright or neighboring-rights laws of other nations.  Additionally, the reproduction of some materials may be restricted by privacy and/or publicity rights.";
    $mods .= "</marmot:rightsStatement>\r\n";
    $mods .= "</mods:accessCondition>\r\n";
    $mods .= "</mods>";
    return $mods;
}
 if (strlen($copyRightStatement) > 0) {
     $copyrightHolderName = '';
     if (stripos($copyRightStatement, 'Eagle County Historical Society') !== false) {
         $copyrightHolderName = 'Eagle County Historical Society';
     } else {
         echo "Unknown copyright holder: {$copyRightStatement} \r\n<br/>";
         fwrite($logFile, "Unknown copyright holder: {$copyRightStatement} \r\n");
     }
     $accessCondition = $MODSxml->createElement('accessCondition');
     $accessCondition->appendChild($MODSxml->createElementNS('http://marmot.org/local_mods_extension', 'typeOfStatement', 'local'));
     $accessCondition->appendChild($MODSxml->createElementNS('http://marmot.org/local_mods_extension', 'rightsStatement', $copyRightStatement));
     if ($copyrightHolderName) {
         $rightsHolderElement = $MODSxml->createElementNS('http://marmot.org/local_mods_extension', 'rightsHolder');
         $rightsHolderPid = doesEntityExist($copyrightHolderName);
         if (!$rightsHolderPid) {
             $rightsHolderPid = createOrganization($repository, $copyrightHolderName);
         }
         $rightsHolderElement->appendChild($MODSxml->createElementNS('http://marmot.org/local_mods_extension', 'entityPid', $rightsHolderPid));
         $rightsHolderElement->appendChild($MODSxml->createElementNS('http://marmot.org/local_mods_extension', 'entityTitle', $copyrightHolderName));
         $accessCondition->appendChild($rightsHolderElement);
     }
     $modsNode->insertBefore($accessCondition, $nameNode);
 }
 $provenance = (string) $exportedItem->provenance;
 if (strlen($provenance) > 0) {
     $noteNode = $MODSxml->createElement('notes', str_replace('&', '&amp;', $provenance));
     if ($nameNode != null) {
         $modsNode->replaceChild($noteNode, $nameNode);
     } else {
         $modsNode->insertBefore($noteNode, $extension);
     }