You are here

function fillpdf_load_entities in FillPDF 7

Load entities needed to fill PDFs.

Parameters

$fillpdf_info:

$nids:

$webform_array:

$uc_order_ids:

$uc_order_product_ids:

$user:

$entity_ids:

Return value

array An array of entities.

2 calls to fillpdf_load_entities()
fillpdf_file_download in ./fillpdf.module
Implements hook_file_download().
fillpdf_merge_pdf in ./fillpdf.module
Constructs a page and sends it to the browser or saves it.

File

./fillpdf.module, line 820

Code

function fillpdf_load_entities($fillpdf_info, $nids, $webform_array, $uc_order_ids, $uc_order_product_ids, $user, $entity_ids) {
  $context = array(
    'nodes' => array(),
    'webforms' => array(),
    'uc_orders' => array(),
    'uc_order_products' => array(),
    'entities' => array(),
  );

  // If $webform_array contains entries with an sid, but not an nid, set the nid
  // to the default.
  if (!empty($fillpdf_info->default_nid) && is_array($webform_array)) {
    foreach (array_keys($webform_array) as $key) {
      if (empty($webform_array[$key]['nid'])) {
        $webform_array[$key]['nid'] = $fillpdf_info->default_nid;
      }
    }
  }
  $entity_mode = module_exists('entity_token');

  // If no nid is given, and Entity API is disabled, use the default nid as a
  // classic node. (If Entity API is enabled, we'll handle it as an entity later
  // on in this function.)
  if (empty($fillpdf_info->default_entity_type) && !empty($fillpdf_info->default_nid) && empty($nids) && empty($webform_array)) {
    $default_node = node_load($fillpdf_info->default_nid);
    if ($default_node) {

      // Default node is a non-webform node.
      if (empty($default_node->webform)) {
        if (!$entity_mode) {
          $context['nodes'][] = $default_node;
        }
      }
      else {
        $webform_array = array(
          array(
            'nid' => $fillpdf_info->default_nid,
            'node' => $default_node,
          ),
        );

        // Since this is handled here, prevent it from being processed as an entity later.
        $entity_mode = FALSE;
      }
    }
  }

  // Nodes.
  if (is_array($nids)) {
    foreach ($nids as $nid) {
      $context['nodes'][] = node_load($nid);
    }
  }

  // Webforms.
  if (module_exists('webform') && is_array($webform_array)) {

    // Load the proper submission helper file and account for the different
    // versions of Webform.
    $included = module_load_include('inc', 'webform', 'includes/webform.submissions');
    if ($included === FALSE) {
      module_load_include('inc', 'webform', 'webform_submissions');
    }
    foreach ($webform_array as $this_webform) {
      if (!empty($this_webform['nid'])) {

        // User did not specify submission ID, meaning they want most recent.
        if (empty($this_webform['sid'])) {
          $this_webform['sid'] = db_query('SELECT sid FROM {webform_submissions}
            WHERE nid = :nid AND uid = :uid ORDER BY submitted DESC', array(
            ':nid' => $this_webform['nid'],
            ':uid' => $user->uid,
          ))
            ->fetchField();
        }
        if ($this_webform['sid'] !== FALSE) {
          $context['webforms'][] = array(
            'webform' => empty($this_webform['node']) ? node_load($this_webform['nid']) : $this_webform['node'],
            'submission' => webform_get_submission($this_webform['nid'], $this_webform['sid']),
          );
        }
      }
    }
  }

  // Ubercart Orders.
  if (module_exists('uc_order') && is_array($uc_order_ids)) {
    foreach ($uc_order_ids as $uc_order_id) {
      $context['uc_orders'][] = uc_order_load($uc_order_id);
    }
  }

  // Ubercart Ordered Products.
  if (module_exists('uc_order') && is_array($uc_order_product_ids)) {
    foreach ($uc_order_product_ids as $uc_order_product_id) {
      $context['uc_order_products'][] = uc_order_product_load($uc_order_product_id);
    }
    return $context;
  }

  // Entities.
  $entities_by_type = array();
  if ($entity_mode) {

    // If no entity IDs are specified but we have a default NID, prime a plain
    // entity ID here. The default entity type will be added just below.
    // Note that we don't need a default entity if we already have a Node
    // or Webform in context.
    if (empty($nids) && empty($webform_array) && empty($entity_ids) && !empty($fillpdf_info->default_nid)) {
      $entity_ids = array(
        $fillpdf_info->default_nid,
      );
    }
    if (!empty($entity_ids)) {
      foreach ($entity_ids as $entity_id) {
        list($type, $id) = strpos($entity_id, ':') ? explode(':', $entity_id) : array(
          $entity_id,
          NULL,
        );

        // Type might be missing, in which case we default to either the default
        // entity type (if exists) or 'node' if none is set.
        if (empty($id)) {
          $id = $type;
          $type = !empty($fillpdf_info->default_entity_type) ? $fillpdf_info->default_entity_type : 'node';
        }
        $entities_by_type += array(
          $type => array(),
        );
        $entities_by_type[$type][] = entity_load_single($type, $id);
      }
      $context['entities'] = $entities_by_type;
    }
  }
  return $context;
}