You are here

function commerce_pdm_product_display_manager_form in Commerce (Product Display Manager) 7

Creates the display manager form.

@todo Typically, node entities are used as product displays. But there are no restrictions to turn any other entity type like terms, users, files, media,... into product displays too. PDM currently assumes all product displays are nodes. See: http://drupal.org/node/1376938

1 string reference to 'commerce_pdm_product_display_manager_form'
commerce_pdm_product_display_manager_page in ./commerce_pdm.admin.inc
Display manager page.

File

./commerce_pdm.admin.inc, line 225

Code

function commerce_pdm_product_display_manager_form($form, &$form_state) {
  $form = array(
    '#tree' => TRUE,
  );
  $session = isset($_SESSION['products_overview_filter']) ? $_SESSION['products_overview_filter'] : array();
  $current_path = $_GET['q'];
  $product_display_types = _commerce_pdm_get_product_display_types(FALSE);

  // Skip the rest if no product displays where found.
  if (empty($product_display_types)) {
    return array(
      'no_product_displays' => array(
        '#markup' => '<div style="margin-bottom:20px;">' . t('The store does not yet contain any product display nodes.') . '</div>',
      ),
    );
  }
  $product_fields = _commerce_pdm_get_product_reference_fields();

  // Fetch the products & display nodes as a UNION query.
  $union = FALSE;
  $base_query = NULL;
  foreach ($product_fields as $product_field) {
    $nodes_query = db_select('node', 'n');
    $nodes_query
      ->leftJoin('field_data_' . $product_field, 'pr', 'pr.entity_id = n.nid');
    $nodes_query
      ->leftJoin('commerce_product', 'cp', 'cp.product_id = pr.' . $product_field . '_product_id');
    $nodes_query
      ->fields('cp', array(
      'type',
      'product_id',
      'sku',
      'title',
      'language',
    ));
    $nodes_query
      ->fields('n', array(
      'nid',
      'title',
      'language',
      'type',
      'status',
      'promote',
      'sticky',
      'translate',
    ));
    $nodes_query
      ->fields('pr', array(
      'delta',
    ));
    $display_types = array();
    foreach ($product_display_types as $product_display_type) {
      $display_types[] = $product_display_type['machine_name'];
    }
    $nodes_query
      ->condition('n.type', $display_types, 'IN');
    if ($union) {
      $base_query
        ->union($nodes_query, 'ALL');
    }
    else {
      $base_query = $nodes_query;
      $union = TRUE;
    }
  }

  // Add filter conditions to the union query.
  $union = db_select($base_query, 'u');
  $union
    ->fields('u');
  foreach ($session as $filter) {
    list($key, $value) = $filter;
    switch ($key) {
      case 'status':

        // Note: no exploitable hole as $key/$value
        // have already been checked when submitted.
        list($key, $value) = explode('-', $value, 2);
        $union
          ->condition('u.' . $key, $value);
        break;
      case 'display_node_type':
        $union
          ->condition('u.n_type', $value);
        break;
      case 'product_type':
        $union
          ->condition('u.type', $value);
        break;
      case 'language':
        $union
          ->condition('u.n_language', $value);
        break;
    }
  }
  $nodes_query_result = $union
    ->extend('PagerDefault')
    ->limit(25)
    ->execute();
  $product_list = array();
  foreach ($nodes_query_result as $record) {

    // Create the Product Display when first time.
    if (!isset($product_list[$record->nid])) {
      $object = new stdClass();
      $object->nid = $record->nid;
      $object->title = $record->n_title;
      $object->type = $record->n_type;
      $object->product_type = $record->type;
      $object->language = $record->n_language;
      $object->children = array();
      $product_list[$record->nid] = $object;
    }
    if (!is_null($record->product_id)) {

      // Add the product to the list.
      $product_object = new stdClass();
      $product_object->product_id = $record->product_id;
      $product_object->sku = $record->sku;
      $product_object->type = $record->type;
      $product_object->title = $record->title;
      $product_object->language = $record->language;
      $product_list[$record->nid]->children[$record->delta] = $product_object;
    }
  }
  if (count($product_list) > 0) {

    // Add all display nodes and their referenced products to the form.
    foreach ($product_list as $node_obj) {
      $form[] = array(
        'title' => array(
          '#type' => 'item',
          '#markup' => $node_obj->title,
        ),
        'language' => array(
          '#type' => 'item',
          '#markup' => $node_obj->language,
        ),
        'edit_link' => array(
          '#type' => 'link',
          '#title' => t('Edit'),
          '#href' => 'node/' . $node_obj->nid . '/edit',
          '#options' => array(
            'query' => array(
              'destination' => $current_path,
            ),
          ),
        ),
        'delete_link' => array(
          '#type' => 'link',
          '#title' => t('Delete'),
          '#href' => 'node/' . $node_obj->nid . '/delete',
          '#options' => array(
            'query' => array(
              'destination' => $current_path,
            ),
          ),
        ),
        'nid' => array(
          '#type' => 'hidden',
          '#value' => $node_obj->nid,
        ),
        '#type' => $node_obj->product_type,
      );
      if (!empty($node_obj->children)) {
        foreach ($node_obj->children as $delta => $related_product) {
          $form[] = array(
            'title' => array(
              '#type' => 'item',
              '#markup' => $related_product->title,
            ),
            'language' => array(
              '#type' => 'item',
              '#markup' => $related_product->language,
            ),
            'edit_link' => array(
              '#type' => 'link',
              '#title' => t('Edit'),
              '#href' => 'admin/commerce/products/' . $related_product->product_id . '/edit',
              '#options' => array(
                'query' => array(
                  'destination' => $current_path,
                ),
              ),
            ),
            'clone_link' => array(
              '#type' => 'link',
              '#title' => t('Clone'),
              '#href' => 'admin/commerce/products/clone/' . str_replace("_", "-", $related_product->type) . '/' . $related_product->product_id,
              '#options' => array(
                'query' => array(
                  'pdid' => $node_obj->nid,
                  'destination' => $current_path,
                ),
              ),
            ),
            'delete_link' => array(
              '#type' => 'link',
              '#title' => t('Delete'),
              '#href' => 'admin/commerce/products/' . $related_product->product_id . '/delete',
              '#options' => array(
                'query' => array(
                  'destination' => $current_path,
                ),
              ),
            ),
            'pid' => array(
              '#type' => 'hidden',
              '#default_value' => $related_product->product_id,
            ),
            'delta' => array(
              '#type' => 'weight',
              '#title' => t('Delta'),
              '#title_display' => 'invisible',
              '#default_value' => $delta,
            ),
          );
        }
      }
    }

    // Load all products currently not referenced.
    $orphans_query = db_select('commerce_product', 'cp');
    $orphans_query
      ->fields('cp', array(
      'product_id',
      'sku',
      'title',
      'language',
      'type',
    ));
    foreach ($product_fields as $field_name) {
      $orphans_query
        ->leftJoin('field_data_' . $field_name, 'fd_' . $field_name, 'cp.product_id = fd_' . $field_name . '.' . $field_name . '_product_id');
      $orphans_query
        ->condition('fd_' . $field_name . '.entity_id', NULL);
    }

    // Add only the product type filter here.
    foreach ($session as $index => $filter) {
      list($key, $value) = $filter;
      switch ($key) {
        case 'product_type':
          $orphans_query
            ->condition('cp.type', $value);
          break;
      }
    }
    $orphans_result = $orphans_query
      ->execute();
    $form[] = array(
      'title' => array(
        '#type' => 'item',
        '#markup' => COMMERCE_PDM_ORPHANS_ROW_TITLE,
      ),
      'nid' => array(
        '#type' => 'hidden',
        '#value' => 0,
      ),
      'language' => array(
        '#type' => 'item',
        '#markup' => '',
      ),
    );

    // Add all orphan products to the table.
    if ($orphans_result
      ->rowCount() > 0) {
      foreach ($orphans_result as $orphan_product) {
        $form[] = array(
          'title' => array(
            '#type' => 'item',
            '#markup' => $orphan_product->title,
          ),
          'language' => array(
            '#type' => 'item',
            '#markup' => $orphan_product->language,
          ),
          'edit_link' => array(
            '#type' => 'link',
            '#title' => t('Edit'),
            '#href' => 'admin/commerce/products/' . $orphan_product->product_id . '/edit',
            '#options' => array(
              'query' => array(
                'destination' => $current_path,
              ),
            ),
          ),
          'delete_link' => array(
            '#type' => 'link',
            '#title' => t('Delete'),
            '#href' => 'admin/commerce/products/' . $orphan_product->product_id . '/delete',
            '#options' => array(
              'query' => array(
                'destination' => $current_path,
              ),
            ),
          ),
          'pid' => array(
            '#type' => 'hidden',
            '#default_value' => $orphan_product->product_id,
          ),
          'orphan' => array(
            '#type' => 'hidden',
            '#default_value' => TRUE,
          ),
          'delta' => array(
            '#type' => 'weight',
            '#title' => t('Delta'),
            '#title_display' => 'invisible',
          ),
        );
      }
    }
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );

  // Adds JavaScript and CSS used by form.
  $form['#after_build'][] = '_commerce_pdm_product_display_manager_form_attach';

  // Turns it into a table.
  $form['#languages'] = module_invoke('locale', 'language_list');
  $form['#theme'] = 'commerce_pdm_product_display_manager_table';
  $form['pager'] = array(
    '#theme' => 'pager',
  );
  return $form;
}