You are here

function uc_product_adjustments_form in Ubercart 5

Same name and namespace in other branches
  1. 6.2 uc_attribute/uc_attribute.admin.inc \uc_product_adjustments_form()
  2. 7.3 uc_attribute/uc_attribute.admin.inc \uc_product_adjustments_form()

Form builder to associate option combinations with mutations of a product's model number.

See also

uc_product_adjustments_form_submit

2 string references to 'uc_product_adjustments_form'
uc_attribute_menu in uc_attribute/uc_attribute.module
Implementation of hook_menu().
uc_importer_import in uc_importer/uc_importer.module
Imports an XML document into the database.

File

uc_attribute/uc_attribute.module, line 1173

Code

function uc_product_adjustments_form($nid) {
  $node = node_load($nid);
  drupal_set_title(check_plain($node->title));

  //Populate table and such.
  $model = $node->model;
  $query_select = "SELECT DISTINCT";
  $query_from = " FROM";
  $query_where = " WHERE";
  $query_order = " ORDER BY";
  $result = db_query("SELECT pa.ordering, a.name, a.ordering, ao.aid, COUNT(po.oid) FROM {uc_product_attributes} AS pa LEFT JOIN {uc_attributes} AS a ON pa.aid = a.aid LEFT JOIN {uc_attribute_options} AS ao ON a.aid = ao.aid LEFT JOIN {uc_product_options} AS po ON ao.oid = po.oid AND po.nid = %d WHERE pa.nid = %d GROUP BY ao.aid, pa.aid, a.name, pa.ordering, a.ordering HAVING count(po.oid) > 0 ORDER BY pa.ordering, a.ordering", $nid, $nid);
  $num_prod_attr = db_num_rows($result);
  $i = 1;
  $attribute_names = '';
  $full_attributes = array();
  $values = array();
  while ($prod_attr = db_fetch_object($result)) {
    $query_select .= " ao{$i}.aid AS aid{$i}, ao{$i}.name AS name{$i}, ao{$i}.oid AS oid{$i}, po{$i}.ordering,";
    $query_from .= " ({uc_product_options} AS po{$i} LEFT JOIN {uc_attribute_options} AS ao{$i} ON po{$i}.oid = ao{$i}.oid AND po{$i}.nid = %d),";
    $values[] = $nid;
    $query_where .= " ao{$i}.aid = " . $prod_attr->aid . " AND";
    $query_order .= " po{$i}.ordering, ao{$i}.name,";
    ++$i;
    $attribute_names .= '<th>' . $prod_attr->name . '</th>';
    $attribute_ids[] = $prod_attr->aid;
  }

  // Remove last connecting parts (commas, "AND")
  $query_select = rtrim($query_select, ',');
  $query_from = rtrim($query_from, ',');
  $query_where = substr($query_where, 0, strlen($query_where) - 4);
  $query_order = rtrim($query_order, ',');
  if ($num_prod_attr) {

    //Get previous values
    $result = db_query("SELECT * FROM {uc_product_adjustments} WHERE nid = %d", $nid);
    $old_vals = array();
    while ($obj = db_fetch_object($result)) {
      $old_vals[] = $obj;
    }
    $result = pager_query($query_select . $query_from . $query_where . $query_order, 20, 0, null, $values);
    $form['original'] = array(
      '#value' => '<div><br /><b>' . t('Default product SKU: @sku', array(
        '@sku' => $model,
      )) . '</b></div>',
    );
    $form['default'] = array(
      '#type' => 'value',
      '#value' => $model,
    );
    $form['table'] = array(
      '#prefix' => '<table class="combinations">',
      '#suffix' => '</table>',
    );
    $form['table']['head'] = array(
      '#prefix' => '<thead><tr>',
      '#suffix' => '</tr></thead>',
      '#value' => $attribute_names . '<th>Alternate SKU</th>',
      '#weight' => 0,
    );
    $form['table']['body'] = array(
      '#prefix' => '<tbody>',
      '#suffix' => '</tbody>',
      '#weight' => 1,
      '#tree' => TRUE,
    );
    $i = 0;
    while ($combo = db_fetch_object($result)) {
      $cells = '';
      $row_title = '';
      $comb_array = array();
      for ($j = 1; $j <= $num_prod_attr; ++$j) {
        $cells .= '<td>' . $combo->{'name' . $j} . '</td>';
        $row_title .= $combo->{'name' . $j} . ', ';
        $comb_array[$combo->{'aid' . $j}] = $combo->{'oid' . $j};
      }
      $row_title = substr($row_title, 0, strlen($row_title) - 2);
      $serial_array = serialize($comb_array);
      $default_model = $model;
      foreach ($old_vals as $ov) {
        if (!count(array_diff_assoc(unserialize($ov->combination), unserialize($serial_array)))) {
          $default_model = $ov->model;
          break;
        }
      }
      $form['table']['body'][$i] = array(
        '#prefix' => '<tr title="' . $row_title . '">',
        '#suffix' => '</tr>',
      );
      $form['table']['body'][$i]['combo'] = array(
        '#value' => $cells,
      );
      $form['table']['body'][$i]['combo_array'] = array(
        '#type' => 'value',
        '#value' => $serial_array,
      );
      $form['table']['body'][$i]['model'] = array(
        '#type' => 'textfield',
        '#default_value' => $default_model,
        '#prefix' => '<td>',
        '#suffix' => '</td>',
      );
      ++$i;
    }
    $form['nid'] = array(
      '#type' => 'hidden',
      '#value' => $nid,
    );
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Submit'),
    );
  }
  else {
    $form['error'] = array(
      '#value' => '<div><br />' . t('This product does not have any attributes.') . '</div>',
    );
  }
  $form['pager'] = array(
    '#value' => theme('pager'),
  );
  return $form;
}