You are here

function uc_product_class_form_submit in Ubercart 5

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

Submit handler for uc_product_class_form.

File

uc_product/uc_product.module, line 2184
The product module for Ubercart.

Code

function uc_product_class_form_submit($form_id, $form_values) {
  if (uc_product_class_load($form_values['pcid'])) {
    $is_new = false;
    $pcid = $form_values['pcid'];
    db_query("UPDATE {uc_product_classes} SET name = '%s', description = '%s' WHERE pcid = '%s'", $form_values['name'], $form_values['description'], $pcid);
    uc_product_node_info(true);
    module_invoke_all('product_class', $pcid, 'update');
  }
  else {
    $is_new = true;

    // Convert whitespace to underscores, and remove other non-alphanumeric characters.
    $pcid = preg_replace(array(
      '/\\s+/',
      '/\\W/',
    ), array(
      '_',
      '',
    ), strtolower($form_values['pcid']));

    // Search for class ids of the form $pcid_# where # is any character.
    $similar = db_num_rows(db_query("SELECT pcid FROM {uc_product_classes} WHERE pcid LIKE '%s\\__' OR pcid LIKE '%s' UNION SELECT type FROM {node_type} WHERE type = '%s' AND custom = 0", $pcid, $pcid, $pcid));
    if ($similar) {
      $pcid = $pcid . '_' . $similar;
    }
    db_query("INSERT INTO {uc_product_classes} (pcid, name, description) VALUES ('%s', '%s', '%s')", $pcid, $form_values['name'], $form_values['description']);
    uc_product_node_info(true);
    variable_set('node_options_' . $pcid, variable_get('node_options_product', array(
      'status',
      'promote',
    )));
    if (module_exists('comment')) {
      variable_set('comment_' . $pcid, variable_get('comment_product', COMMENT_NODE_READ_WRITE));
    }
    module_invoke_all('product_class', $pcid, 'insert');
  }
  node_types_rebuild();
  menu_rebuild();
  if ($is_new && module_exists('imagefield')) {
    $info = content_types($pcid);
    if (!isset($info['fields']['field_image_cache'])) {
      $result = db_query("SELECT field_name FROM {node_field} WHERE field_name = 'field_image_cache' AND type = 'image'");
      if (db_num_rows($result)) {
        drupal_execute('_content_admin_field_add_existing', array(
          'type_name' => $pcid,
          'field_name' => 'field_image_cache',
        ), $pcid);
      }
    }
  }
  return 'admin/store/products/classes';
}