export.inc 39 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
<?php

/**
 * @file
 * Contains code to make it easier to have exportable objects.
 *
 * Documentation for exportable objects is contained in help/export.html
 */

/**
 * A bit flag used to let us know if an object is in the database.
 */
define('EXPORT_IN_DATABASE', 0x01);

/**
 * A bit flag used to let us know if an object is a 'default' in code.
 */
define('EXPORT_IN_CODE', 0x02);

/**
 * @defgroup export_crud CRUD functions for export.
 * @{
 * export.inc supports a small number of CRUD functions that should always
 * work for every exportable object, no matter how complicated. These
 * functions allow complex objects to provide their own callbacks, but
 * in most cases, the default callbacks will be used.
 *
 * Note that defaults are NOT set in the $schema because it is presumed
 * that a module's personalized CRUD functions will already know which
 * $table to use and not want to clutter up the arguments with it.
 */

/**
 * Create a new object for the given $table.
 *
 * @param $table
 *   The name of the table to use to retrieve $schema values. This table
 *   must have an 'export' section containing data or this function
 *   will fail.
 * @param $set_defaults
 *   If TRUE, which is the default, then default values will be retrieved
 *   from schema fields and set on the object.
 *
 * @return
 *   The loaded object.
 */
function ctools_export_crud_new($table, $set_defaults = TRUE) {
  $schema = ctools_export_get_schema($table);
  $export = $schema['export'];

  if (!empty($export['create callback']) && function_exists($export['create callback'])) {
    return $export['create callback']($set_defaults);
  }
  else {
    return ctools_export_new_object($table, $set_defaults);
  }
}

/**
 * Load a single exportable object.
 *
 * @param $table
 *   The name of the table to use to retrieve $schema values. This table
 *   must have an 'export' section containing data or this function
 *   will fail.
 * @param $name
 *   The unique ID to load. The field for this ID will be specified by
 *   the export key, which normally defaults to 'name'.
 *
 * @return
 *   The loaded object.
 */
function ctools_export_crud_load($table, $name) {
  $schema = ctools_export_get_schema($table);
  $export = $schema['export'];

  if (!empty($export['load callback']) && function_exists($export['load callback'])) {
    return $export['load callback']($name);
  }
  else {
    $result = ctools_export_load_object($table, 'names', array($name));
    if (isset($result[$name])) {
      return $result[$name];
    }
  }
}

/**
 * Load multiple exportable objects.
 *
 * @param $table
 *   The name of the table to use to retrieve $schema values. This table
 *   must have an 'export' section containing data or this function
 *   will fail.
 * @param $names
 *   An array of unique IDs to load. The field for these IDs will be specified
 *   by the export key, which normally defaults to 'name'.
 *
 * @return
 *   An array of the loaded objects.
 */
function ctools_export_crud_load_multiple($table, array $names) {
  $schema = ctools_export_get_schema($table);
  $export = $schema['export'];

  $results = array();
  if (!empty($export['load multiple callback']) && function_exists($export['load multiple callback'])) {
    $results = $export['load multiple callback']($names);
  }
  else {
    $results = ctools_export_load_object($table, 'names', $names);
  }

  // Ensure no empty results are returned.
  return array_filter($results);
}

/**
 * Load all exportable objects of a given type.
 *
 * @param $table
 *   The name of the table to use to retrieve $schema values. This table
 *   must have an 'export' section containing data or this function
 *   will fail.
 * @param $reset
 *   If TRUE, the static cache of all objects will be flushed prior to
 *   loading all. This can be important on listing pages where items
 *   might have changed on the page load.
 * @return
 *   An array of all loaded objects, keyed by the unique IDs of the export key.
 */
function ctools_export_crud_load_all($table, $reset = FALSE) {
  $schema = ctools_export_get_schema($table);
  if (empty($schema['export'])) {
    return array();
  }

  $export = $schema['export'];

  if ($reset) {
    ctools_export_load_object_reset($table);
  }

  if (!empty($export['load all callback']) && function_exists($export['load all callback'])) {
    return $export['load all callback']($reset);
  }
  else {
    return ctools_export_load_object($table, 'all');
  }
}

/**
 * Save a single exportable object.
 *
 * @param $table
 *   The name of the table to use to retrieve $schema values. This table
 *   must have an 'export' section containing data or this function
 *   will fail.
 * @param $object
 *   The fully populated object to save.
 *
 * @return
 *   Failure to write a record will return FALSE. Otherwise SAVED_NEW or
 *   SAVED_UPDATED is returned depending on the operation performed. The
 *   $object parameter contains values for any serial fields defined by the $table
 */
function ctools_export_crud_save($table, &$object) {
  $schema = ctools_export_get_schema($table);
  $export = $schema['export'];

  if (!empty($export['save callback']) && function_exists($export['save callback'])) {
    return $export['save callback']($object);
  }
  else {
    // Objects should have a serial primary key. If not, simply fail to write.
    if (empty($export['primary key'])) {
      return FALSE;
    }

    $key = $export['primary key'];
    if ($object->export_type & EXPORT_IN_DATABASE) {
      // Existing record.
      $update = array($key);
    }
    else {
      // New record.
      $update = array();
      $object->export_type = EXPORT_IN_DATABASE;
    }
    return drupal_write_record($table, $object, $update);
  }
}

/**
 * Delete a single exportable object.
 *
 * This only deletes from the database, which means that if an item is in
 * code, then this is actually a revert.
 *
 * @param $table
 *   The name of the table to use to retrieve $schema values. This table
 *   must have an 'export' section containing data or this function
 *   will fail.
 * @param $object
 *   The fully populated object to delete, or the export key.
 */
function ctools_export_crud_delete($table, $object) {
  $schema = ctools_export_get_schema($table);
  $export = $schema['export'];

  if (!empty($export['delete callback']) && function_exists($export['delete callback'])) {
    return $export['delete callback']($object);
  }
  else {
    // If we were sent an object, get the export key from it. Otherwise
    // assume we were sent the export key.
    $value = is_object($object) ? $object->{$export['key']} : $object;
    db_delete($table)
      ->condition($export['key'], $value)
      ->execute();
  }
}

/**
 * Get the exported code of a single exportable object.
 *
 * @param $table
 *   The name of the table to use to retrieve $schema values. This table
 *   must have an 'export' section containing data or this function
 *   will fail.
 * @param $object
 *   The fully populated object to delete, or the export key.
 * @param $indent
 *   Any indentation to apply to the code, in case this object is embedded
 *   into another, for example.
 *
 * @return
 *   A string containing the executable export of the object.
 */
function ctools_export_crud_export($table, $object, $indent = '') {
  $schema = ctools_export_get_schema($table);
  $export = $schema['export'];

  if (!empty($export['export callback']) && function_exists($export['export callback'])) {
    return $export['export callback']($object, $indent);
  }
  else {
    return ctools_export_object($table, $object, $indent);
  }
}

/**
 * Turn exported code into an object.
 *
 * Note: If the code is poorly formed, this could crash and there is no
 * way to prevent this.
 *
 * @param $table
 *   The name of the table to use to retrieve $schema values. This table
 *   must have an 'export' section containing data or this function
 *   will fail.
 * @param $code
 *   The code to eval to create the object.
 *
 * @return
 *   An object created from the export. This object will NOT have been saved
 *   to the database. In the case of failure, a string containing all errors
 *   that the system was able to determine.
 */
function ctools_export_crud_import($table, $code) {
  $schema = ctools_export_get_schema($table);
  $export = $schema['export'];

  if (!empty($export['import callback']) && function_exists($export['import callback'])) {
    return $export['import callback']($code);
  }
  else {
    ob_start();
    eval($code);
    ob_end_clean();

    if (empty(${$export['identifier']})) {
      $errors = ob_get_contents();
      if (empty($errors)) {
        $errors = t('No item found.');
      }
      return $errors;
    }

    $item = ${$export['identifier']};

    // Set these defaults just the same way that ctools_export_new_object sets
    // them.
    $item->export_type = NULL;
    $item->{$export['export type string']} = t('Local');

    return $item;
  }
}

/**
 * Change the status of a certain object.
 *
 * @param $table
 *   The name of the table to use to enable a certain object. This table
 *   must have an 'export' section containing data or this function
 *   will fail.
 * @param $object
 *   The fully populated object to enable, or the machine readable name.
 * @param $status
 *   The status, in this case, is whether or not it is 'disabled'.
 */
function ctools_export_crud_set_status($table, $object, $status) {
  $schema = ctools_export_get_schema($table);
  $export = $schema['export'];

  if (!empty($export['status callback']) && function_exists($export['status callback'])) {
    $export['status callback']($object, $status);
  }
  else {
    if (is_object($object)) {
      ctools_export_set_object_status($object, $status);
    }
    else {
      ctools_export_set_status($table, $object, $status);
    }
  }

}


/**
 * Enable a certain object.
 *
 * @param $table
 *   The name of the table to use to enable a certain object. This table
 *   must have an 'export' section containing data or this function
 *   will fail.
 * @param $object
 *   The fully populated object to enable, or the machine readable name.
 */
function ctools_export_crud_enable($table, $object) {
  return ctools_export_crud_set_status($table, $object, FALSE);
}

/**
 * Disable a certain object.
 *
 * @param $table
 *   The name of the table to use to disable a certain object. This table
 *   must have an 'export' section containing data or this function
 *   will fail.
 * @param $object
 *   The fully populated object to disable, or the machine readable name.
 */
function ctools_export_crud_disable($table, $object) {
  return ctools_export_crud_set_status($table, $object, TRUE);
}

/**
 * @}
 */

/**
 * Load some number of exportable objects.
 *
 * This function will cache the objects, load subsidiary objects if necessary,
 * check default objects in code and properly set them up. It will cache
 * the results so that multiple calls to load the same objects
 * will not cause problems.
 *
 * It attempts to reduce, as much as possible, the number of queries
 * involved.
 *
 * @param $table
 *   The name of the table to be loaded from. Data is expected to be in the
 *   schema to make all this work.
 * @param $type
 *   A string to notify the loader what the argument is
 *   - all: load all items. This is the default. $args is unused.
 *   - names: $args will be an array of specific named objects to load.
 *   - conditions: $args will be a keyed array of conditions. The conditions
 *       must be in the schema for this table or errors will result.
 * @param $args
 *   An array of arguments whose actual use is defined by the $type argument.
 */
function ctools_export_load_object($table, $type = 'all', $args = array()) {
  $cache = &drupal_static(__FUNCTION__);
  $cache_table_exists = &drupal_static(__FUNCTION__ . '_table_exists', array());
  $cached_database = &drupal_static('ctools_export_load_object_all');

  if (!array_key_exists($table, $cache_table_exists)) {
    $cache_table_exists[$table] = db_table_exists($table);
  }

  $schema = ctools_export_get_schema($table);
  if (empty($schema) || !$cache_table_exists[$table]) {
    return array();
  }

  $export = $schema['export'];

  if (!isset($cache[$table])) {
    $cache[$table] = array();
  }

  // If fetching all and cached all, we've done so and we are finished.
  if ($type == 'all' && !empty($cached_database[$table])) {
    return $cache[$table];
  }

  $return = array();

  // Don't load anything we've already cached.
  if ($type == 'names' && !empty($args)) {
    foreach ($args as $id => $name) {
      if (isset($cache[$table][$name])) {
        $return[$name] = $cache[$table][$name];
        unset($args[$id]);
      }
    }

    // If nothing left to load, return the result.
    if (empty($args)) {
      return $return;
    }
  }

  // Build the query
  $query = db_select($table, 't__0')->fields('t__0');
  $alias_count = 1;
  if (!empty($schema['join'])) {
    foreach ($schema['join'] as $join_key => $join) {
      if ($join_schema = drupal_get_schema($join['table'])) {
        $query->join($join['table'], 't__' . $alias_count, 't__0.' . $join['left_key'] . ' = ' . 't__' . $alias_count . '.' . $join['right_key']);
        $query->fields('t__' . $alias_count);
        $alias_count++;

        // Allow joining tables to alter the query through a callback.
        if (isset($join['callback']) && function_exists($join['callback'])) {
          $join['callback']($query, $schema, $join_schema);
        }
      }
    }
  }

  $conditions = array();
  $query_args = array();

  // If they passed in names, add them to the query.
  if ($type == 'names') {
    $query->condition($export['key'], $args, 'IN');
  }
  else if ($type == 'conditions') {
    foreach ($args as $key => $value) {
      if (isset($schema['fields'][$key])) {
        $query->condition($key, $value);
      }
    }
  }

  $result = $query->execute();

  $status = variable_get($export['status'], array());
  // Unpack the results of the query onto objects and cache them.
  foreach ($result as $data) {
    if (isset($schema['export']['object factory']) && function_exists($schema['export']['object factory'])) {
      $object = $schema['export']['object factory']($schema, $data);
    }
    else {
      $object = _ctools_export_unpack_object($schema, $data, $export['object']);
    }
    $object->table = $table;
    $object->{$export['export type string']} = t('Normal');
    $object->export_type = EXPORT_IN_DATABASE;
    // Determine if default object is enabled or disabled.
    if (isset($status[$object->{$export['key']}])) {
      $object->disabled = $status[$object->{$export['key']}];
    }

    $cache[$table][$object->{$export['key']}] = $object;
    if ($type == 'conditions') {
      $return[$object->{$export['key']}] = $object;
    }
  }

  // Load subrecords.
  if (isset($export['subrecords callback']) && function_exists($export['subrecords callback'])) {
    $export['subrecords callback']($cache[$table]);
  }

  if ($type == 'names' && !empty($args) && !empty($export['cache defaults'])) {
    $defaults = _ctools_export_get_some_defaults($table, $export, $args);
  }
  else {
    $defaults = _ctools_export_get_defaults($table, $export);
  }

  if ($defaults) {
    foreach ($defaults as $object) {
      if ($type == 'conditions') {
        // if this does not match all of our conditions, skip it.
        foreach ($args as $key => $value) {
          if (!isset($object->$key)) {
            continue 2;
          }
          if (is_array($value)) {
            if (!in_array($object->$key, $value)) {
              continue 2;
            }
          }
          else if ($object->$key != $value) {
            continue 2;
          }
        }
      }
      else if ($type == 'names') {
        if (!in_array($object->{$export['key']}, $args)) {
          continue;
        }
      }

      // Determine if default object is enabled or disabled.
      if (isset($status[$object->{$export['key']}])) {
        $object->disabled = $status[$object->{$export['key']}];
      }

      if (!empty($cache[$table][$object->{$export['key']}])) {
        $cache[$table][$object->{$export['key']}]->{$export['export type string']} = t('Overridden');
        $cache[$table][$object->{$export['key']}]->export_type |= EXPORT_IN_CODE;
        $cache[$table][$object->{$export['key']}]->export_module = isset($object->export_module) ? $object->export_module : NULL;
        if ($type == 'conditions') {
          $return[$object->{$export['key']}] = $cache[$table][$object->{$export['key']}];
        }
      }
      else {
        $object->{$export['export type string']} = t('Default');
        $object->export_type = EXPORT_IN_CODE;
        $object->in_code_only = TRUE;
        $object->table = $table;

        $cache[$table][$object->{$export['key']}] = $object;
        if ($type == 'conditions') {
          $return[$object->{$export['key']}] = $object;
        }
      }
    }
  }

  // If fetching all, we've done so and we are finished.
  if ($type == 'all') {
    $cached_database[$table] = TRUE;
    return $cache[$table];
  }

  if ($type == 'names') {
    foreach ($args as $name) {
      if (isset($cache[$table][$name])) {
        $return[$name] = $cache[$table][$name];
      }
    }
  }

  // For conditions,
  return $return;
}

/**
 * Reset all static caches in ctools_export_load_object() or static caches for
 * a given table in ctools_export_load_object().
 *
 * @param $table
 *   String that is the name of a table. If not defined, all static caches in
 *   ctools_export_load_object() will be reset.
 */
function ctools_export_load_object_reset($table = NULL) {
  // Reset plugin cache to make sure new include files are picked up.
  ctools_include('plugins');
  ctools_get_plugins_reset();
  if (empty($table)) {
    drupal_static_reset('ctools_export_load_object');
    drupal_static_reset('ctools_export_load_object_all');
    drupal_static_reset('_ctools_export_get_defaults');
  }
  else {
    $cache = &drupal_static('ctools_export_load_object');
    $cached_database = &drupal_static('ctools_export_load_object_all');
    $cached_defaults = &drupal_static('_ctools_export_get_defaults');
    unset($cache[$table]);
    unset($cached_database[$table]);
    unset($cached_defaults[$table]);
  }
}

/**
 * Get the default version of an object, if it exists.
 *
 * This function doesn't care if an object is in the database or not and
 * does not check. This means that export_type could appear to be incorrect,
 * because a version could exist in the database. However, it's not
 * incorrect for this function as it is *only* used for the default
 * in code version.
 */
function ctools_get_default_object($table, $name) {
  $schema = ctools_export_get_schema($table);
  $export = $schema['export'];

  if (!$export['default hook']) {
    return;
  }

  // Try to load individually from cache if this cache is enabled.
  if (!empty($export['cache defaults'])) {
    $defaults = _ctools_export_get_some_defaults($table, $export, array($name));
  }
  else {
    $defaults = _ctools_export_get_defaults($table, $export);
  }

  $status = variable_get($export['status'], array());

  if (!isset($defaults[$name])) {
    return;
  }

  $object = $defaults[$name];

  // Determine if default object is enabled or disabled.
  if (isset($status[$object->{$export['key']}])) {
    $object->disabled = $status[$object->{$export['key']}];
  }

  $object->{$export['export type string']} = t('Default');
  $object->export_type = EXPORT_IN_CODE;
  $object->in_code_only = TRUE;

  return $object;
}

/**
 * Call the hook to get all default objects of the given type from the
 * export. If configured properly, this could include loading up an API
 * to get default objects.
 */
function _ctools_export_get_defaults($table, $export) {
  $cache = &drupal_static(__FUNCTION__, array());

  // If defaults may be cached, first see if we can load from cache.
  if (!isset($cache[$table]) && !empty($export['cache defaults'])) {
    $cache[$table] = _ctools_export_get_defaults_from_cache($table, $export);
  }

  if (!isset($cache[$table])) {
    // If we're caching, attempt to get a lock. We will wait a short time
    // on the lock, but not too long, because it's better to just rebuild
    // and throw away results than wait too long on a lock.
    if (!empty($export['cache defaults'])) {
      for ($counter = 0; !($lock = lock_acquire('ctools_export:' . $table)) && $counter > 5; $counter++) {
        lock_wait('ctools_export:' . $table, 1);
        ++$counter;
      }
    }

    $cache[$table] = array();

    if ($export['default hook']) {
      if (!empty($export['api'])) {
        ctools_include('plugins');
        $info = ctools_plugin_api_include($export['api']['owner'], $export['api']['api'],
          $export['api']['minimum_version'], $export['api']['current_version']);
        $modules = array_keys($info);
      }
      else {
        $modules = module_implements($export['default hook']);
      }

      foreach ($modules as $module) {
        $function = $module . '_' . $export['default hook'];
        if (function_exists($function)) {
          foreach ((array) $function($export) as $name => $object) {
            // Record the module that provides this exportable.
            $object->export_module = $module;

            if (empty($export['api'])) {
              $cache[$table][$name] = $object;
            }
            else {
              // If version checking is enabled, ensure that the object can be used.
              if (isset($object->api_version) &&
                version_compare($object->api_version, $export['api']['minimum_version']) >= 0 &&
                version_compare($object->api_version, $export['api']['current_version']) <= 0) {
                $cache[$table][$name] = $object;
              }
            }
          }
        }
      }

      drupal_alter($export['default hook'], $cache[$table]);

      // If we acquired a lock earlier, cache the results and release the
      // lock.
      if (!empty($lock)) {
        // Cache the index.
        $index = array_keys($cache[$table]);
        cache_set('ctools_export_index:' . $table, $index, $export['default cache bin']);

        // Cache each object.
        foreach ($cache[$table] as $name => $object) {
          cache_set('ctools_export:' . $table . ':' . $name, $object, $export['default cache bin']);
        }
        lock_release('ctools_export:' . $table);
      }
    }
  }

  return $cache[$table];
}

/**
 * Attempt to load default objects from cache.
 *
 * We can be instructed to cache default objects by the schema. If so
 * we cache them as an index which is a list of all default objects, and
 * then each default object is cached individually.
 *
 * @return Either an array of cached objects, or NULL indicating a cache
 *   rebuild is necessary.
 */
function _ctools_export_get_defaults_from_cache($table, $export) {
  $data = cache_get('ctools_export_index:' . $table, $export['default cache bin']);
  if (!$data || !is_array($data->data)) {
    return;
  }

  // This is the perfectly valid case where there are no default objects,
  // and we have cached this state.
  if (empty($data->data)) {
    return array();
  }

  $keys = array();
  foreach ($data->data as $name) {
    $keys[] = 'ctools_export:' . $table . ':' . $name;
  }

  $data = cache_get_multiple($keys, $export['default cache bin']);

  // If any of our indexed keys missed, then we have a fail and we need to
  // rebuild.
  if (!empty($keys)) {
    return;
  }

  // Now, translate the returned cache objects to actual objects.
  $cache = array();
  foreach ($data as $cached_object) {
    $cache[$cached_object->data->{$export['key']}] = $cached_object->data;
  }

  return $cache;
}

/**
 * Get a limited number of default objects.
 *
 * This attempts to load the objects directly from cache. If it cannot,
 * the cache is rebuilt. This does not disturb the general get defaults
 * from cache object.
 *
 * This function should ONLY be called if default caching is enabled.
 * It does not check, it is assumed the caller has already done so.
 */
function _ctools_export_get_some_defaults($table, $export, $names) {
  foreach ($names as $name) {
    $keys[] = 'ctools_export:' . $table . ':' . $name;
  }

  $data = cache_get_multiple($keys, $export['default cache bin']);

  // Cache hits remove the $key from $keys by reference. Cache
  // misses do not. A cache miss indicates we may have to rebuild.
  if (!empty($keys)) {
    return _ctools_export_get_defaults($table, $export);
  }

  // Now, translate the returned cache objects to actual objects.
  $cache = array();
  foreach ($data as $cached_object) {
    $cache[$cached_object->data->{$export['key']}] = $cached_object->data;
  }

  return $cache;
}

/**
 * Unpack data loaded from the database onto an object.
 *
 * @param $schema
 *   The schema from drupal_get_schema().
 * @param $data
 *   The data as loaded from the database.
 * @param $object
 *   If an object, data will be unpacked onto it. If a string
 *   an object of that type will be created.
 */
function _ctools_export_unpack_object($schema, $data, $object = 'stdClass') {
  if (is_string($object)) {
    if (class_exists($object)) {
      $object = new $object;
    }
    else {
      $object = new stdClass;
    }
  }

  // Go through our schema and build correlations.
  foreach ($schema['fields'] as $field => $info) {
    if (isset($data->$field)) {
      $object->$field = empty($info['serialize']) ? $data->$field : unserialize($data->$field);
    }
    else {
      $object->$field = NULL;
    }
  }

  if (isset($schema['join'])) {
    foreach ($schema['join'] as $join_key => $join) {
      $join_schema = ctools_export_get_schema($join['table']);
      if (!empty($join['load'])) {
        foreach ($join['load'] as $field) {
          $info = $join_schema['fields'][$field];
          $object->$field = empty($info['serialize']) ? $data->$field : unserialize($data->$field);
        }
      }
    }
  }

  return $object;
}

/**
 * Unpack data loaded from the database onto an object.
 *
 * @param $table
 *   The name of the table this object represents.
 * @param $data
 *   The data as loaded from the database.
 */
function ctools_export_unpack_object($table, $data) {
  $schema = ctools_export_get_schema($table);
  return _ctools_export_unpack_object($schema, $data, $schema['export']['object']);
}

/**
 * Export a field.
 *
 * This is a replacement for var_export(), allowing us to more nicely
 * format exports. It will recurse down into arrays and will try to
 * properly export bools when it can, though PHP has a hard time with
 * this since they often end up as strings or ints.
 */
function ctools_var_export($var, $prefix = '') {
  if (is_array($var)) {
    if (empty($var)) {
      $output = 'array()';
    }
    else {
      $output = "array(\n";
      foreach ($var as $key => $value) {
        $output .= $prefix . "  " . ctools_var_export($key) . " => " . ctools_var_export($value, $prefix . '  ') . ",\n";
      }
      $output .= $prefix . ')';
    }
  }
  else if (is_object($var) && get_class($var) === 'stdClass') {
    // var_export() will export stdClass objects using an undefined
    // magic method __set_state() leaving the export broken. This
    // workaround avoids this by casting the object as an array for
    // export and casting it back to an object when evaluated.
    $output = '(object) ' . ctools_var_export((array) $var, $prefix);
  }
  else if (is_bool($var)) {
    $output = $var ? 'TRUE' : 'FALSE';
  }
  else {
    $output = var_export($var, TRUE);
  }

  return $output;
}

/**
 * Export an object into code.
 */
function ctools_export_object($table, $object, $indent = '', $identifier = NULL, $additions = array(), $additions2 = array()) {
  $schema = ctools_export_get_schema($table);
  if (!isset($identifier)) {
    $identifier = $schema['export']['identifier'];
  }

  $output = $indent . '$' . $identifier . ' = new ' . get_class($object) . "();\n";

  if ($schema['export']['can disable']) {
    $output .= $indent . '$' . $identifier . '->disabled = FALSE; /* Edit this to true to make a default ' . $identifier . ' disabled initially */' . "\n";
  }
  if (!empty($schema['export']['api']['current_version'])) {
    $output .= $indent . '$' . $identifier . '->api_version = ' . $schema['export']['api']['current_version'] . ";\n";
  }

  // Put top additions here:
  foreach ($additions as $field => $value) {
    $output .= $indent . '$' . $identifier . '->' . $field . ' = ' . ctools_var_export($value, $indent) . ";\n";
  }

  $fields = $schema['fields'];
  if (!empty($schema['join'])) {
    foreach ($schema['join'] as $join) {
      if (!empty($join['load'])) {
        foreach ($join['load'] as $join_field) {
          $fields[$join_field] = $join['fields'][$join_field];
        }
      }
    }
  }

  // Go through our schema and joined tables and build correlations.
  foreach ($fields as $field => $info) {
    if (!empty($info['no export'])) {
      continue;
    }
    if (!isset($object->$field)) {
      if (isset($info['default'])) {
        $object->$field = $info['default'];
      }
      else {
        $object->$field = '';
      }
    }

    // Note: This is the *field* export callback, not the table one!
    if (!empty($info['export callback']) && function_exists($info['export callback'])) {
      $output .= $indent . '$' . $identifier . '->' . $field . ' = ' . $info['export callback']($object, $field, $object->$field, $indent) . ";\n";
    }
    else {
      $value = $object->$field;
      if ($info['type'] == 'int') {
        if (isset($info['size']) && $info['size'] == 'tiny') {
          $info['boolean'] = (!isset($info['boolean'])) ? $schema['export']['boolean'] : $info['boolean'];
          $value = ($info['boolean']) ? (bool) $value : (int) $value;
        }
        else {
          $value = (int) $value;
        }
      }

      $output .= $indent . '$' . $identifier . '->' . $field . ' = ' . ctools_var_export($value, $indent) . ";\n";
    }
  }

  // And bottom additions here
  foreach ($additions2 as $field => $value) {
    $output .= $indent . '$' . $identifier . '->' . $field . ' = ' . ctools_var_export($value, $indent) . ";\n";
  }

  return $output;
}

/**
 * Get the schema for a given table.
 *
 * This looks for data the export subsystem needs and applies defaults so
 * that it's easily available.
 */
function ctools_export_get_schema($table) {
  static $drupal_static_fast;
  if (!isset($drupal_static_fast)) {
    $drupal_static_fast['cache'] = &drupal_static(__FUNCTION__);
  }
  $cache = &$drupal_static_fast['cache'];

  if (empty($cache[$table])) {
    $schema = drupal_get_schema($table);

    // If our schema isn't loaded, it's possible we're in a state where it
    // simply hasn't been cached. If we've been asked, let's force the
    // issue.
    if (!$schema || empty($schema['export'])) {
      // force a schema reset:
      $schema = drupal_get_schema($table, TRUE);
    }

    if (!isset($schema['export'])) {
      return array();
    }

    if (empty($schema['module'])) {
      return array();
    }

    // Add some defaults
    $schema['export'] += array(
      'key' => 'name',
      'key name' => 'Name',
      'object' => 'stdClass',
      'status' => 'default_' . $table,
      'default hook' => 'default_' . $table,
      'can disable' => TRUE,
      'identifier' => $table,
      'primary key' => !empty($schema['primary key']) ? $schema['primary key'][0] : '',
      'bulk export' => TRUE,
      'list callback' => "$schema[module]_{$table}_list",
      'to hook code callback' => "$schema[module]_{$table}_to_hook_code",
      'cache defaults' => FALSE,
      'default cache bin' => 'cache',
      'export type string' => 'type',
      'boolean' => TRUE,
    );

    // If the export definition doesn't have the "primary key" then the CRUD
    // save callback won't work.
    if (empty($schema['export']['primary key']) && user_access('administer site configuration')) {
      drupal_set_message(t('The export definition of @table is missing the "primary key" property.', array('@table' => $table)), 'error');
    }

    // Notes:
    // The following callbacks may be defined to override default behavior
    // when using CRUD functions:
    //
    // create callback
    // load callback
    // load multiple callback
    // load all callback
    // save callback
    // delete callback
    // export callback
    // import callback
    //
    // See the appropriate ctools_export_crud function for details on what
    // arguments these callbacks should accept. Please do not call these
    // directly, always use the ctools_export_crud_* wrappers to ensure
    // that default implementations are honored.
    $cache[$table] = $schema;
  }

  return $cache[$table];
}

/**
 * Gets the schemas for all tables with ctools object metadata.
 */
function ctools_export_get_schemas($for_export = FALSE) {
  $export_tables = &drupal_static(__FUNCTION__);
  if (is_null($export_tables)) {
    $export_tables = array();
    $schemas = drupal_get_schema();
    foreach ($schemas as $table => $schema) {
      if (!isset($schema['export'])) {
        unset($schemas[$table]);
        continue;
      }
      $export_tables[$table] = ctools_export_get_schema($table);
    }
  }
  return $for_export ? array_filter($export_tables, '_ctools_export_filter_export_tables') : $export_tables;
}

function _ctools_export_filter_export_tables($schema) {
  return !empty($schema['export']['bulk export']);
}

function ctools_export_get_schemas_by_module($modules = array(), $for_export = FALSE) {
  $export_tables = array();
  $list = ctools_export_get_schemas($for_export);
  foreach ($list as $table => $schema) {
    $export_tables[$schema['module']][$table] = $schema;
  }
  return empty($modules) ? $export_tables : array_keys($export_tables, $modules);
}

/**
 * Set the status of a default $object as a variable.
 *
 * The status, in this case, is whether or not it is 'disabled'.
 * This function does not check to make sure $object actually
 * exists.
 */
function ctools_export_set_status($table, $name, $new_status = TRUE) {
  $schema = ctools_export_get_schema($table);
  $status = variable_get($schema['export']['status'], array());

  $status[$name] = $new_status;
  variable_set($schema['export']['status'], $status);
}

/**
 * Set the status of a default $object as a variable.
 *
 * This is more efficient than ctools_export_set_status because it
 * will actually unset the variable entirely if it's not necessary,
 * this saving a bit of space.
 */
function ctools_export_set_object_status($object, $new_status = TRUE) {
  $table = $object->table;
  $schema = ctools_export_get_schema($table);
  $export = $schema['export'];
  $status = variable_get($export['status'], array());

  // Compare
  if (!$new_status && $object->export_type & EXPORT_IN_DATABASE) {
    unset($status[$object->{$export['key']}]);
  }
  else {
    $status[$object->{$export['key']}] = $new_status;
  }

  variable_set($export['status'], $status);
}

/**
 * Provide a form for displaying an export.
 *
 * This is a simple form that should be invoked like this:
 * @code
 *   $output = drupal_get_form('ctools_export_form', $code, $object_title);
 * @endcode
 */
function ctools_export_form($form, &$form_state, $code, $title = '') {
  $lines = substr_count($code, "\n");
  $form['code'] = array(
    '#type' => 'textarea',
    '#title' => $title,
    '#default_value' => $code,
    '#rows' => $lines,
  );

  return $form;
}

/**
 * Create a new object based upon schema values.
 *
 * Because 'default' has ambiguous meaning on some fields, we will actually
 * use 'object default' to fill in default values if default is not set
 * That's a little safer to use as it won't cause weird database default
 * situations.
 */
function ctools_export_new_object($table, $set_defaults = TRUE) {
  $schema = ctools_export_get_schema($table);
  $export = $schema['export'];

  $object = new $export['object'];
  foreach ($schema['fields'] as $field => $info) {
    if (isset($info['object default'])) {
      $object->$field = $info['object default'];
    }
    else if (isset($info['default'])) {
      $object->$field = $info['default'];
    }
    else {
      $object->$field = NULL;
    }
  }

  if ($set_defaults) {
    // Set some defaults so this data always exists.
    // We don't set the export_type property here, as this object is not saved
    // yet. We do give it NULL so we don't generate notices trying to read it.
    $object->export_type = NULL;
    $object->{$export['export type string']} = t('Local');
  }
  return $object;
}

/**
 * Convert a group of objects to code based upon input and return this as a larger
 * export.
 */
function ctools_export_to_hook_code(&$code, $table, $names = array(), $name = 'foo') {
  $schema = ctools_export_get_schema($table);
  $export = $schema['export'];
  // Use the schema-specified function for generating hook code, if one exists
  if (function_exists($export['to hook code callback'])) {
    $output = $export['to hook code callback']($names, $name);
  }
  // Otherwise, the following code generates basic hook code
  else {
    $output = ctools_export_default_to_hook_code($schema, $table, $names, $name);
  }

  if (!empty($output)) {
    if (isset($export['api'])) {
      if (isset($code[$export['api']['owner']][$export['api']['api']]['version'])) {
        $code[$export['api']['owner']][$export['api']['api']]['version'] = max($code[$export['api']['owner']][$export['api']['api']]['version'], $export['api']['minimum_version']);
      }
      else {
        $code[$export['api']['owner']][$export['api']['api']]['version'] = $export['api']['minimum_version'];
        $code[$export['api']['owner']][$export['api']['api']]['code'] = '';
      }
      $code[$export['api']['owner']][$export['api']['api']]['code'] .= $output;
    }
    else {
      if (empty($code['general'])) {
        $code['general'] = '';
      }
      $code['general'] .= $output;
    }
  }
}

/**
 * Default function to export objects to code.
 *
 * Note that if your module provides a 'to hook code callback' then it will
 * receive only $names and $name as arguments. Your module is presumed to
 * already know the rest.
 */
function ctools_export_default_to_hook_code($schema, $table, $names, $name) {
  $export = $schema['export'];
  $output = '';
  $objects = ctools_export_crud_load_multiple($table, $names);
  if ($objects) {
    $output = "/**\n";
    $output .= " * Implements hook_{$export['default hook']}().\n";
    $output .= " */\n";
    $output .= "function " . $name . "_{$export['default hook']}() {\n";
    $output .= "  \${$export['identifier']}s = array();\n\n";
    foreach ($objects as $object) {
      $output .= ctools_export_crud_export($table, $object, '  ');
      $output .= "  \${$export['identifier']}s['" . check_plain($object->$export['key']) . "'] = \${$export['identifier']};\n\n";
    }
    $output .= "  return \${$export['identifier']}s;\n";
    $output .= "}\n";
  }

  return $output;
}
/**
 * Default function for listing bulk exportable objects.
 */
function ctools_export_default_list($table, $schema) {
  $list = array();

  $items = ctools_export_crud_load_all($table);
  $export_key = $schema['export']['key'];
  foreach ($items as $item) {
    // Try a couple of possible obvious title keys:
    $keys = array('admin_title', 'title');
    if (isset($schema['export']['admin_title'])) {
      array_unshift($keys, $schema['export']['admin_title']);
    }

    $string = '';
    foreach ($keys as $key) {
      if (!empty($item->$key)) {
        $string = $item->$key . " (" . $item->$export_key . ")";
        break;
      }
    }

    if (empty($string)) {
      $string = $item->$export_key;
    }
    $list[$item->$export_key] = check_plain($string);
  }
  return $list;
}