aid"] = array( 'name' => 'uc_product_attributes', 'join' => array( 'left' => array( 'table' => 'node', 'field' => 'nid', ), 'right' => array( 'field' => 'nid', ), ), 'fields' => array( 'options' => array( 'name' => t('Product: Options for @attr-name', array('@attr-name' => $attr->name)), 'sortable' => false, 'help' => t('This will display all product options for %attr-name. Note that this causes one extra query per row displayed, and might have a minor performance impact.', array('%attr-name' => $attr->name)), 'handler' => 'uc_attribute_views_handler_nodeoptions', 'attribute' => $attr->aid, 'notafield' => true, ), ), ); } $tables["uc_product_adjustments"] = array( 'name' => 'uc_product_adjustments', 'join' => array( 'left' => array( 'table' => 'node', 'field' => 'nid', ), 'right' => array( 'field' => 'nid', ), ), 'fields' => array( 'combination' => array( 'name' => t('Product: Option Combination (All)'), 'help' => t('This will display all product option combinations regardless of whether or not those combinations are in stock.'), 'sortable' => FALSE, 'handler' => 'uc_attribute_views_handler_option_combination', 'notafield' => true, ), ), 'filters' => array( 'combination' => array( 'name' => t('Product: Option Combination (All)'), 'help' => t('This filters by product option combinations regardless of whether or not those combinations are in stock.'), 'value' => views_uc_attibute_form(), 'value-type' => 'array', 'operator' => 'views_handler_operator_andor', 'handler' => 'views_handler_filter_combination' ), ), ); $tables["uc_product_stock"] = array( 'name' => 'uc_product_stock', 'join' => array( 'left' => array( 'table' => 'uc_product_adjustments', 'field' => 'model', ), 'right' => array( 'field' => 'sku', ), ), 'fields' => array( 'sku' => array( 'name' => t('Product: Option Combination (In stock)'), 'help' => t('This will display all product option combinations that are in stock.'), 'sortable' => FALSE, 'handler' => 'uc_attribute_views_handler_option_combination', 'notafield' => true, ), ), 'filters' => array( 'sku' => array( 'name' => t('Product: Option Combination (In Stock)'), 'help' => t('This filters by product option combinations that are in stock.'), 'value' => views_uc_attibute_form(), 'value-type' => 'array', 'operator' => 'views_handler_operator_andor', 'handler' => 'views_handler_filter_combination', 'instock' => true ), ), ); return $tables; } } /** * Turn the Attribute Combination into a meaningful text value */ function uc_attribute_views_handler_option_combination($fieldinfo, $fielddata, $value, $data) { $array = unserialize($value); if ($array) { foreach ($array as $option) { $result = db_fetch_object(db_query("SELECT * FROM {uc_attribute_options} WHERE oid = %d",$option)); $option_string[] = check_plain($result->name); } } $option_string = !empty($option_string) ? implode(' > ', $option_string) : ''; return $option_string; } /** * Display all the options for a given attribute in a given node */ function uc_attribute_views_handler_nodeoptions($fieldinfo, $fielddata, $value, $data) { $attr = uc_attribute_load($fieldinfo['attribute'], $data->nid, 'product'); foreach ($attr->options as $option) { $links[] = check_plain($option->name); } $links = !empty($links) ? implode(' | ', $links) : ''; return $links; } /** * Display all the options for a given attribute */ function uc_attribute_views_handler_alloptions($fieldinfo, $fielddata, $value, $data) { $attr = uc_attribute_load($fieldinfo['attribute']); foreach ($attr->options as $option) { $links[] = check_plain($option->name); } $links = !empty($links) ? implode(' | ', $links) : ''; return $links; } /* * Create a list of attribute names and IDs. */ function views_handler_filter_aid() { $aids = array(); $result = db_query("SELECT * FROM {uc_attributes} attr ORDER BY attr.ordering, attr.name"); while ($obj = db_fetch_object($result)) { $aids[$obj->aid] = $obj->name; } return $aids; } /* * Create a form that lists options hierarchically for each attribute. */ function views_uc_attibute_form() { $attributes = uc_get_attributes(); $options = array(); $options[''] = t('- None selected -'); $depth = 0; $this_option = array(); $options = _uc_attribute_build_form($options, $attributes, $this_option, $depth); $form = array('#type' => 'select', '#title' => t('Attribute Options'), '#default_value' => $options[0], '#options' => $options, '#description' => t('Attribute Options'), '#multiple' => TRUE, '#size' => $multiple ? min(9, count($options)) : 0, '#weight' => -15, '#theme' => 'attribute_option_select', ); return $form; } function views_handler_filter_combination($op, $filter, $filterinfo, &$query) { foreach ($filter['value'] as $value) { $array = explode(',', $value); if ($array) { foreach ($array as $option) { if ($filterinfo['instock']) { $query->add_where("(combination LIKE '%%\"%d\";%%' AND active = 1 AND stock > threshold )" , $option); } else { $query->add_where("(combination LIKE '%%\"%d\";%%' )" , $option); } } } } } /** * Load all attributes. */ function uc_get_attributes() { $result = db_query("SELECT aid FROM {uc_attributes} ORDER BY ordering"); $chosen_attr = array(); while ($attr = db_fetch_object($result)){ $chosen_attr[$attr->aid] = uc_attribute_load($attr->aid); } uasort($chosen_attr, '_uc_attribute_sort'); return $chosen_attr; } /** * We use the default selection field for choosing terms. */ function theme_attribute_option_select($element) { return theme('select', $element); } /** * This function should be called recursively to build the options form. */ function _uc_attribute_build_form($options, $attributes, $prior_option = array(), $depth = 0) { $depth = $depth + 1; $remaining_attributes = array_slice($attributes, 1); reset($attributes); $attr = current($attributes); foreach ($attr->options as $option) { $this_option = $prior_option; $this_option[] = $option->oid; $choice = new stdClass(); $choice->option = array(implode(',', $this_option) => str_repeat('-', $depth - 1) . ' ' . $option->name); $options[] = $choice; if ($remaining_attributes) { $options = _uc_attribute_build_form($options, $remaining_attributes, $this_option, $depth); } } return $options; } ?>