<?php

require_once 'session.php';
require_once 'shared.php';
header("Location: " . getJumpToSfdcUrlPrefix() . 'setup%2Fbuild%2FrunAllApexTests.apexp');
Beispiel #2
0
/**
 * Print screen for user to enter
 * field mapping values. This is used
 * for all PUT functions.
 *
 * @param $action
 * @param $csvArray
 */
function setFieldMappings($action, $csvArray)
{
    $currRecord = null;
    $id = isset($_REQUEST['id']) ? trim(htmlspecialchars($_REQUEST['id'], ENT_QUOTES)) : null;
    if (requiresObject($action)) {
        // if an object is not explicitly given, infer from the id, if that was given
        if (!isset($_REQUEST['default_object'])) {
            WorkbenchContext::get()->setDefaultObject(WorkbenchContext::get()->getObjectTypeByKeyPrefixOrId($id));
        }
        if (WorkbenchContext::get()->getDefaultObject()) {
            $objectType = WorkbenchContext::get()->getDefaultObject();
            $describeSObjectResult = WorkbenchContext::get()->describeSObjects($objectType);
            if (!$csvArray && !empty($id)) {
                $currRecord = queryCurrentRecord($describeSObjectResult, $id);
                if ($currRecord == null) {
                    displayUploadFileWithObjectSelectionForm($action, $id, "An existing " . WorkbenchContext::get()->getDefaultObject() . " could not found with the id '{$id}'. Confirm both the object type and id are correct.");
                    exit;
                }
            }
        } else {
            if (!$csvArray && !empty($_REQUEST['id'])) {
                displayUploadFileWithObjectSelectionForm($action, $id, "The object type of id '{$id}' is unknown. Choose an object type and confirm the id is correct.");
            } else {
                displayUploadFileWithObjectSelectionForm($action, $id, "Must choose an object to {$action}.");
            }
            exit;
        }
    } else {
        if (!$csvArray && $id) {
            $objectType = WorkbenchContext::get()->getObjectTypeByKeyPrefixOrId($id);
            if ($objectType) {
                $describeSObjectResult = WorkbenchContext::get()->describeSObjects($objectType);
                $currRecord = queryCurrentRecord($describeSObjectResult, $id);
            } else {
                $currRecord = new SObject();
                $currRecord->fields = new StdClass();
                $currRecord->fields->Id = $id;
            }
        }
    }
    if ($currRecord != null && isset($describeSObjectResult)) {
        $isDeleted = isset($currRecord->fields->IsDeleted) && $currRecord->fields->IsDeleted == "true";
        $dmlActions = array("update" => !$isDeleted && $describeSObjectResult->updateable, "delete" => !$isDeleted && $describeSObjectResult->deletable, "undelete" => $isDeleted && $describeSObjectResult->undeletable, "purge" => $isDeleted && $describeSObjectResult->undeletable);
        if (isset($dmlActions[$action]) && !$dmlActions[$action]) {
            displayWarning("It does not seem like this record can be " . $action . "d. Are you sure you wish to continue?");
        }
    }
    print "<form method='POST' action=''>" . getCsrfFormTag();
    if ($action == 'upsert') {
        print "<p class='instructions'>Choose the Salesforce field to use as the External Id. Be sure to also map this field below:</p>\n";
        print "<table class='fieldMapping'><tr>\n";
        print "<td style='color: red;'>External Id</td>";
        print "<td><select name='_ext_id' style='width: 100%;'>\n";
        foreach ($describeSObjectResult->fields as $fields => $field) {
            if ($field->idLookup) {
                //limit the fields to only those with the idLookup property set to true. Corrected Issue #10
                print " <option value='{$field->name}'";
                if ($field->name == 'Id') {
                    print " selected='true'";
                }
                print ">{$field->name}</option>\n";
            }
        }
        print "</select></td></tr></table>\n";
    }
    //end if upsert
    if ($action == "retrieve") {
        print "<h3 style='margin:0px;'><a href='describe.php?id={$id}' style='text-decoration:none; color:inherit;'>" . $objectType . "</a></h3>";
        print "<h1 style='margin-top:0px;'>" . (isset($currRecord->fields->Name) ? htmlspecialchars($currRecord->fields->Name) : $currRecord->fields->Id) . "</h1>";
        foreach ($dmlActions as $dmlAction => $enabled) {
            print "<input type=\"button\" onclick=\"window.location.href='{$dmlAction}.php?sourceType=singleRecord&id={$id}'\" value=\"" . ucfirst($dmlAction) . "\" " . (!$enabled ? "disabled=disabled" : "") . "/>&nbsp;&nbsp;";
        }
        if (WorkbenchConfig::get()->value('linkIdToUi')) {
            $uiViewable = isset($describeSObjectResult->urlDetail) || in_array($objectType, array("Dashboard", "Report", "Division", "BusinessHours", "BrandTemplate"));
            print "<input type=\"button\" onclick=\"window.open('" . getJumpToSfdcUrlPrefix() . "{$id}')\" value=\"View in Salesforce\" " . ($uiViewable ? "" : "disabled=disabled") . "/>&nbsp;&nbsp;";
        }
        print "<p/>";
    } else {
        if ($csvArray) {
            $instructions = "Map the Salesforce fields to the columns from the uploaded CSV:";
        } else {
            if (requiresObject($action)) {
                $instructions = "Provide values for the " . WorkbenchContext::get()->getDefaultObject() . " fields below:";
            } else {
                $instructions = "Confirm the id to {$action} below:";
            }
        }
        print "<p class='instructions'>{$instructions}</p>\n";
    }
    print "<table class='fieldMapping'>\n";
    print "<tr><th>Field</th>";
    if ($csvArray) {
        print "<th>CSV Field</th>";
    } else {
        print "<th>Value</th>";
    }
    if (WorkbenchConfig::get()->value("showReferenceBy") && ($action == 'insert' || $action == 'update' || $action == 'upsert')) {
        print "<th onmouseover=\"Tip('For fields that reference other objects, external ids from the foreign objects provided can be automatically matched to their corresponding primary ids. Use this column to select the object and field by which to perform the Smart Lookup. If left unselected, standard lookup using the primary id will be performed. If this field is disabled, only standard lookup is available because the foreign object contains no external ids.')\">Smart Lookup &nbsp; <img align='absmiddle' src='" . getPathToStaticResource('/images/help16.png') . "'/></th>";
    }
    print "</tr>\n";
    if ($action == 'insert') {
        foreach ($describeSObjectResult->fields as $field) {
            if ($field->createable) {
                printPutFieldForMapping($field, $csvArray, true, null);
            }
        }
    }
    if ($action == 'update') {
        printPutFieldForMappingId($csvArray, true, $currRecord);
        foreach ($describeSObjectResult->fields as $field) {
            if ($field->updateable) {
                printPutFieldForMapping($field, $csvArray, true, $currRecord);
            }
        }
    }
    if ($action == 'upsert') {
        printPutFieldForMappingId($csvArray, true, $currRecord);
        foreach ($describeSObjectResult->fields as $field) {
            if ($field->updateable && $field->createable) {
                printPutFieldForMapping($field, $csvArray, true, $currRecord);
            }
        }
    }
    if ($action == 'delete' || $action == 'undelete' || $action == 'purge') {
        printPutFieldForMappingId($csvArray, false, $currRecord);
    }
    if ($action == "retrieve") {
        foreach ($describeSObjectResult->fields as $field) {
            // fix an IIS issue where the id is not in fields
            if (!isset($currRecord->fields->Id) && isset($currRecord->Id)) {
                $currRecord->fields->Id = $currRecord->Id;
            }
            printPutFieldForMapping($field, false, false, $currRecord, false);
        }
    }
    print "</table>\n";
    if ($csvArray) {
        print "<p><input type='submit' name='action' value='Map Fields' />\n";
        print "<input type='button' value='Preview CSV' onClick='window.open(" . '"csv_preview.php"' . ")'></p>\n";
    } else {
        if ($action !== "retrieve") {
            print "<input type='hidden' name='sourceType' value='singleRecord' />\n";
            print "<p><input type='submit' name='action' value='Confirm " . ucwords($action) . "' />\n";
        }
    }
    print "</form>\n";
}
function addLinksToIds($inputStr)
{
    $idMatcher = "/\\b(\\w{4}000\\w{11})\\b/";
    $uiHref = "href='" . getJumpToSfdcUrlPrefix() . "\$1' target='sfdcUi' ";
    $dmlTip = "";
    if (WorkbenchConfig::get()->value("showIdActionsHover")) {
        $tipWidth = 0;
        $dmlTip = "onmouseover=\"Tip('Choose an action:<br/>";
        $dmlActions = array("update", "delete", "undelete", "purge");
        foreach ($dmlActions as $dmlAction) {
            $tipWidth += 55;
            $dmlTip .= "<a href=\\'{$dmlAction}.php?sourceType=singleRecord&id=\$1\\'>" . ucfirst($dmlAction) . "</a>&nbsp;&nbsp;";
        }
        if (WorkbenchConfig::get()->value('linkIdToUi')) {
            $tipWidth += 125;
            $dmlTip .= "<a " . str_replace("'", "\\'", $uiHref) . ">View in Salesforce</a>&nbsp;&nbsp;";
        }
        $dmlTip .= "', STICKY, true, WIDTH, {$tipWidth})\"";
    }
    return preg_replace($idMatcher, "<a href='retrieve.php?id=\$1' {$dmlTip}>\$1</a>", $inputStr);
}