You are here

function _uc_discounts_product_filter_form in Ubercart Discounts (Alternative) 7.2

Same name and namespace in other branches
  1. 6.2 uc_discounts/uc_discounts.admin.inc \_uc_discounts_product_filter_form()

Helper function that creates a series of dropdowns for selecting a product via product_id, sku, class, term, or author.

1 call to _uc_discounts_product_filter_form()
uc_discounts_admin_discount_edit_form in uc_discounts/uc_discounts.admin.inc
Generates admin form to create or edit a discount.

File

uc_discounts/uc_discounts.admin.inc, line 546
Admin forms and functions for uc_discounts module.

Code

function _uc_discounts_product_filter_form(&$form, &$form_state, $grouping, $discount_id) {
  $fieldset_name = $grouping == UC_DISCOUNTS_GROUPING_APPLICATION ? 'application' : 'qualifications';
  $form[$fieldset_name]['filter'] = array(
    '#type' => 'fieldset',
    '#title' => $grouping == UC_DISCOUNTS_GROUPING_APPLICATION ? t('Discounted products') : t('Required products'),
    '#description' => $grouping == UC_DISCOUNTS_GROUPING_APPLICATION ? t('Select the products which will be discounted.') : t('If products are selected, at least one of them will be required.'),
  );
  $options = array();
  if ($grouping == UC_DISCOUNTS_GROUPING_QUALIFICATION) {
    $options[UC_DISCOUNTS_FILTER_TYPE_NONE] = t('No specific product');
    $filter_type = isset($form_state['values']['required_product_type']) ? $form_state['values']['required_product_type'] : UC_DISCOUNTS_FILTER_TYPE_NONE;
    $name = 'required_product_type';
  }
  else {
    $filter_type = isset($form_state['values']['filter_type']) ? $form_state['values']['filter_type'] : UC_DISCOUNTS_FILTER_TYPE_PRODUCTS;
    $name = 'filter_type';
  }
  $filter_name = $name;
  $options[UC_DISCOUNTS_FILTER_TYPE_PRODUCTS] = t('Filter By Products');
  $options[UC_DISCOUNTS_FILTER_TYPE_TERMS] = t('Filter By Terms');
  $options[UC_DISCOUNTS_FILTER_TYPE_SKUS] = t('Filter By SKUs');
  $options[UC_DISCOUNTS_FILTER_TYPE_CLASS] = t('Filter by Class');
  $options[UC_DISCOUNTS_FILTER_TYPE_AUTHORS] = t('Filter by Authors');
  $form[$fieldset_name]['filter'][$name] = array(
    '#type' => 'select',
    '#title' => t('Filter Type'),
    '#description' => t('Determine whether to filter by products, terms, SKUs, class, or node authors. Discount is applied to only these products.'),
    '#options' => $options,
    '#default_value' => $filter_type,
    '#required' => TRUE,
    '#attributes' => array(
      'class' => array(
        'filter-type',
      ),
    ),
  );
  $name = $grouping . '-filter-select-' . UC_DISCOUNTS_FILTER_TYPE_PRODUCTS;
  $product_ids = isset($form_state['values'][$name]) ? $form_state['values'][$name] : uc_discounts_get_product_ids_for_discount_id($discount_id, $grouping, FALSE);
  $options = array();
  $result = db_query('SELECT n.nid as nid, n.title as title, p.model as model
                     FROM {uc_products} p
                     INNER JOIN {node} n ON p.nid=n.nid
                     ORDER BY title');
  if ($grouping == UC_DISCOUNTS_GROUPING_APPLICATION) {
    $options[UC_DISCOUNTS_OPTION_ALL_PRODUCTS] = t('<All Products>');
  }
  foreach ($result as $row) {
    $options[$row->nid] = $row->title . ' (' . $row->model . ')';
  }
  $form[$fieldset_name]['filter'][$name] = array(
    '#type' => 'select',
    '#title' => t('Products'),
    '#description' => t('Select all products this discount applies to or &lt;All Products&gt; to apply to all products.'),
    '#options' => $options,
    '#default_value' => $product_ids,
    '#multiple' => TRUE,
    '#states' => array(
      'visible' => array(
        ':input[name="' . $filter_name . '"]' => array(
          'value' => UC_DISCOUNTS_FILTER_TYPE_PRODUCTS,
        ),
      ),
      'required' => array(
        ':input[name="' . $filter_name . '"]' => array(
          'value' => UC_DISCOUNTS_FILTER_TYPE_PRODUCTS,
        ),
      ),
    ),
  );
  $name = $grouping . '-filter-select-' . UC_DISCOUNTS_FILTER_TYPE_TERMS;
  $term_ids = isset($form_state['values'][$name]) ? $form_state['values'][$name] : uc_discounts_get_term_ids_for_discount_id($discount_id, $grouping, FALSE);
  $options = array();
  $result = db_query('SELECT tid, name FROM {taxonomy_term_data} ORDER BY weight');
  $options[UC_DISCOUNTS_OPTION_ALL_TERMS] = t('<All Terms>');
  foreach ($result as $row) {
    $options[$row->tid] = $row->name;
  }
  $form[$fieldset_name]['filter'][$name] = array(
    '#type' => 'select',
    '#title' => t('Terms'),
    '#description' => t('Select all terms this discount applies to or &lt;All Terms&gt; to apply to all terms.  Selections are <b>not</b> recursive.'),
    '#options' => $options,
    '#default_value' => $term_ids,
    '#multiple' => TRUE,
    '#states' => array(
      'visible' => array(
        ':input[name="' . $filter_name . '"]' => array(
          'value' => UC_DISCOUNTS_FILTER_TYPE_TERMS,
        ),
      ),
      'required' => array(
        ':input[name="' . $filter_name . '"]' => array(
          'value' => UC_DISCOUNTS_FILTER_TYPE_TERMS,
        ),
      ),
    ),
  );
  $name = $grouping . '-filter-select-' . UC_DISCOUNTS_FILTER_TYPE_SKUS;
  $skus = isset($form_state['values'][$name]) ? $form_state['values'][$name] : uc_discounts_get_skus_for_discount_id($discount_id, $grouping, FALSE);
  $options = array();
  $result = db_query('SELECT p.model, n.title
                     FROM {uc_products} p, {node} n
                     WHERE p.nid = n.nid
                     ORDER BY p.model');
  if ($grouping == UC_DISCOUNTS_GROUPING_APPLICATION) {
    $options[UC_DISCOUNTS_OPTION_ALL_SKUS] = t('<All SKUs>');
  }
  foreach ($result as $row) {
    $options[$row->model] = $row->model . ' (' . $row->title . ')';
  }
  $form[$fieldset_name]['filter'][$name] = array(
    '#type' => 'select',
    '#title' => t('SKUs'),
    '#description' => t('Select all SKUs this discount applies to or &lt;All SKUs&gt; to apply to all SKUs.  Selections are <b>not</b> recursive.'),
    '#options' => $options,
    '#default_value' => $skus,
    '#multiple' => TRUE,
    '#states' => array(
      'visible' => array(
        ':input[name="' . $filter_name . '"]' => array(
          'value' => UC_DISCOUNTS_FILTER_TYPE_SKUS,
        ),
      ),
      'required' => array(
        ':input[name="' . $filter_name . '"]' => array(
          'value' => UC_DISCOUNTS_FILTER_TYPE_SKUS,
        ),
      ),
    ),
  );
  $name = $grouping . '-filter-select-' . UC_DISCOUNTS_FILTER_TYPE_CLASS;
  $classes = isset($form_state['values'][$name]) ? $form_state['values'][$name] : uc_discounts_get_classes_for_discount_id($discount_id, $grouping, FALSE);
  $options = array();
  $result = uc_product_types();
  if ($grouping == UC_DISCOUNTS_GROUPING_APPLICATION) {
    $options[UC_DISCOUNTS_OPTION_ALL_CLASSES] = t('<All Product Classes>');
  }
  foreach ($result as $type) {
    $options[$type] = $type;
  }
  $form[$fieldset_name]['filter'][$name] = array(
    '#type' => 'select',
    '#title' => t('Classes'),
    '#description' => t('Select all product classes that this discount applies to or &lt;All Classes&gt; to apply to all classes.  Selections are <b>not</b> recursive.'),
    '#options' => $options,
    '#default_value' => $classes,
    '#multiple' => TRUE,
    '#states' => array(
      'visible' => array(
        ':input[name="' . $filter_name . '"]' => array(
          'value' => UC_DISCOUNTS_FILTER_TYPE_CLASS,
        ),
      ),
      'required' => array(
        ':input[name="' . $filter_name . '"]' => array(
          'value' => UC_DISCOUNTS_FILTER_TYPE_CLASS,
        ),
      ),
    ),
  );
  $name = $grouping . '-filter-select-' . UC_DISCOUNTS_FILTER_TYPE_AUTHORS;
  $author_ids = isset($form_state['values'][$name]) ? $form_state['values'][$name] : uc_discounts_get_author_ids_for_discount_id($discount_id, $grouping, FALSE);
  $options = array();

  // TODO Please convert this statement to the D7 database API syntax.
  $result = db_query('SELECT uid, name
                     FROM {users} u
                     INNER JOIN {node} n USING(uid)
                     WHERE u.uid > 0
                     AND n.type IN (:uc_product_types)
                     GROUP BY u.uid
                     ORDER BY name', array(
    ':uc_product_types' => uc_product_types(),
  ));
  if ($grouping == UC_DISCOUNTS_GROUPING_APPLICATION) {
    $options[UC_DISCOUNTS_OPTION_ALL_AUTHORS] = t('<All Authors>');
  }
  foreach ($result as $row) {
    $options[$row->uid] = $row->name;
  }
  $form[$fieldset_name]['filter'][$name] = array(
    '#type' => 'select',
    '#title' => t('Authors'),
    '#description' => t('Select all authors this discount applies to or &lt;All Authors&gt; to apply to all authors.'),
    '#options' => $options,
    '#default_value' => $author_ids,
    '#multiple' => TRUE,
    '#states' => array(
      'visible' => array(
        ':input[name="' . $filter_name . '"]' => array(
          'value' => UC_DISCOUNTS_FILTER_TYPE_AUTHORS,
        ),
      ),
      'required' => array(
        ':input[name="' . $filter_name . '"]' => array(
          'value' => UC_DISCOUNTS_FILTER_TYPE_AUTHORS,
        ),
      ),
    ),
  );
}