PageRenderTime 32ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/workbench/metadataDescribeAndList.php

https://code.google.com/
PHP | 150 lines | 124 code | 26 blank | 0 comment | 26 complexity | 1635957d85f402581d4e12fa6d2f0487 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. require_once 'session.php';
  3. require_once 'shared.php';
  4. require_once 'header.php';
  5. print "<p/>";
  6. if (!WorkbenchContext::get()->isApiVersionAtLeast(10.0)) {
  7. displayError("Metadata API not supported prior to version 10.0", false, true);
  8. exit;
  9. }
  10. require_once 'soapclient/SforceMetadataClient.php';
  11. try {
  12. $describeMetadataResult = WorkbenchContext::get()->getMetadataConnection()->describeMetadata(WorkbenchContext::get()->getApiVersion());
  13. } catch (Exception $e) {
  14. displayError($e->getMessage(), false, true);
  15. }
  16. $metadataTypesSelectOptions[""] = "";
  17. foreach ($describeMetadataResult as $resultsKey => $resultsValue) {
  18. if ($resultsKey == 'metadataObjects') {
  19. foreach ($resultsValue as $metadataResultsKey => $metadataResultsValue) {
  20. $metadataTypeMap[$metadataResultsValue->xmlName] = $metadataResultsValue;
  21. $metadataTypesSelectOptions[$metadataResultsValue->xmlName]= $metadataResultsValue->xmlName;
  22. if (isset($metadataResultsValue->childXmlNames)) {
  23. if (!is_array($metadataResultsValue->childXmlNames)) {
  24. $metadataResultsValue->childXmlNames = array($metadataResultsValue->childXmlNames);
  25. }
  26. foreach ($metadataResultsValue->childXmlNames as $childNameKey => $childName) {
  27. $metadataTypesSelectOptions[$childName]= $childName;
  28. $childType = new stdClass();
  29. $childType->parentXmlName = $metadataResultsValue->xmlName .
  30. " <a href='?type=$metadataResultsValue->xmlName' class='miniLink' onClick=\"document.getElementById('loadingMessage').style.visibility='visible';\">[INFO]</a>";
  31. $childType->childXmlName = $childName;
  32. $metadataTypeMap[$childName] = $childType;
  33. $metadataTypeMap[$metadataResultsValue->xmlName]->childXmlNames[$childNameKey] = $childName .
  34. " <a href='?type=$childName' class='miniLink' onClick=\"document.getElementById('loadingMessage').style.visibility='visible';\">[INFO]</a>";
  35. }
  36. }
  37. }
  38. }
  39. }
  40. $metadataTypesSelectOptions = natcaseksort($metadataTypesSelectOptions);
  41. $currentTypeString = isset($_REQUEST['type']) ? htmlspecialchars($_REQUEST['type']) : null;
  42. $previousTypeString = isset($_SESSION['defaultMetadataType']) ? $_SESSION['defaultMetadataType'] : null;
  43. $typeString = $currentTypeString != null ? $currentTypeString : $previousTypeString;
  44. $typeStringChanged = $currentTypeString != null && $previousTypeString != $currentTypeString;
  45. ?>
  46. <p class='instructions'>Choose a metadata type describe and list its
  47. components:</p>
  48. <form id="metadataTypeSelectionForm" name="metadataTypeSelectionForm"
  49. method="GET" action=""><select
  50. id="type" name="type"
  51. onChange="document.getElementById('loadingMessage').style.visibility='visible'; document.metadataTypeSelectionForm.submit();">
  52. <?php printSelectOptions($metadataTypesSelectOptions, $typeString); ?>
  53. </select> <span id='loadingMessage'
  54. style='visibility: hidden; color: #888;'>&nbsp;&nbsp;<img
  55. src='<?php echo getStaticResourcesPath(); ?>/images/wait16trans.gif' align='absmiddle' /> Loading...</span>
  56. </form>
  57. <p />
  58. <?php
  59. if (isset($typeString)) {
  60. if (!isset($metadataTypeMap[$typeString])) {
  61. if (isset($_REQUEST['type']) && $_REQUEST['type']) {
  62. displayError("Invalid metadata type type: $typeString", false, true);
  63. }
  64. exit;
  65. }
  66. $type = $metadataTypeMap[$typeString];
  67. $_SESSION['defaultMetadataType'] = $typeString;
  68. $metadataComponents = listMetadata($type);
  69. $componentsLabel = "Components (" . count($metadataComponents) . ")";
  70. $tree = new ExpandableTree("listMetadataTree", array("Type Description"=>$type, $componentsLabel=>$metadataComponents));
  71. $tree->setForceCollapse($typeStringChanged);
  72. $tree->setContainsIds(true);
  73. $tree->setContainsDates(true);
  74. $tree->printTree();
  75. }
  76. require_once 'footer.php';
  77. function listMetadata($type) {
  78. try {
  79. if (isset($type->childXmlName)) {
  80. return processListMetadataResult(WorkbenchContext::get()->getMetadataConnection()->listMetadata($type->childXmlName, null, WorkbenchContext::get()->getApiVersion()));
  81. }
  82. if (!$type->inFolder) {
  83. return processListMetadataResult(WorkbenchContext::get()->getMetadataConnection()->listMetadata($type->xmlName, null, WorkbenchContext::get()->getApiVersion()));
  84. }
  85. $folderType = $type->xmlName == "EmailTemplate" ? "Email" : $type->xmlName;
  86. $folderQueryResult = WorkbenchContext::get()->getPartnerConnection()->query("SELECT DeveloperName FROM Folder WHERE Type = '" . $folderType . "' AND DeveloperName != null AND NamespacePrefix = null");
  87. if ($folderQueryResult->size == 0) {
  88. return array();
  89. }
  90. foreach ($folderQueryResult->records as $folderRecord) {
  91. $folder = new SObject($folderRecord);
  92. $folderName = $folder->fields->DeveloperName;
  93. $listMetadataResult["$folderName"] = processListMetadataResult(WorkbenchContext::get()->getMetadataConnection()->listMetadata($type->xmlName, $folder->fields->DeveloperName, WorkbenchContext::get()->getApiVersion()));
  94. }
  95. return $listMetadataResult;
  96. } catch (Exception $e) {
  97. displayError($e->getMessage(), false, true);
  98. }
  99. }
  100. function processListMetadataResult($response) {
  101. if (!is_array($response)) {
  102. $response = array($response);
  103. }
  104. $processedResponse = array();
  105. foreach ($response as $responseKey => $responseValue) {
  106. if ($responseValue == null) {
  107. continue;
  108. }
  109. $name = isset($responseValue->fullName) ? $responseValue->fullName : $responseValue->fileName;
  110. if (strrchr($name, "/")) {
  111. $simpleName = substr(strrchr($name, "/"), 1);
  112. $processedResponse[$simpleName] = $responseValue;
  113. } else if (strpos($name, ".")) {
  114. $parentName = substr($name, 0, strpos($name, "."));
  115. $childName = substr($name, strpos($name, ".") + 1);
  116. $processedResponse[$parentName][$childName] = $responseValue;
  117. } else {
  118. $processedResponse[$name] = $responseValue;
  119. }
  120. }
  121. $processedResponse = natcaseksort($processedResponse);
  122. return $processedResponse;
  123. }
  124. ?>