Index: uc_stock/uc_stock.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/ubercart/uc_stock/uc_stock.module,v retrieving revision 1.8 diff -u -u -r1.8 uc_stock.module --- uc_stock/uc_stock.module 31 Mar 2008 20:45:13 -0000 1.8 +++ uc_stock/uc_stock.module 17 Apr 2008 23:56:12 -0000 @@ -394,3 +394,125 @@ return drupal_mail('uc_stock', $to, $subject, $body, $from); } +/** + * Implementation of hook_views_tables() + */ + +function uc_stock_views_tables() { + $tables['uc_product_stock'] = array( + 'name' => 'uc_product_stock', + 'join' => array( + 'left' => array( + 'table' => 'node', + 'field' => 'nid' + ), + 'right' => array( + 'field' => 'nid', + ), + //@todo: should I join uc_stock.sku to uc_product.model? + ), + 'fields' => array( + 'sku' => array( + 'name' => t('Stock: SKU'), + 'help' => t('The model or SKU of the stock level'), + 'sortable' => TRUE, + ), + 'active' => array( + 'name' => t('Stock: Active'), + 'help' => t('@todo: copy goes here'), + 'handler' => 'uc_stock_views_handler_active', + 'sortable' => TRUE, + ), + 'stock' => array( + 'name' => t('Stock: Stock Level'), + 'help' => t('The current stock level'), + //@todo: add handler for formatting non-whole numbers. with options... + 'sortable' => TRUE, + ), + 'threshold' => array( + 'name' => t('Stock: threshold'), + 'help' => t('The threshold or warning limit of the stock level'), + 'sortable' => TRUE, + ), + ), + 'sorts' => array( + 'sku' => array( + 'name' => t('Stock: SKU'), + ), + 'active' => array( + 'name' => t('Stock: Active'), + 'help' => t('@todo: copy goes here'), + ), + 'stock' => array( + 'name' => t('Stock: Stock Level'), + 'help' => t('The current stock level'), + ), + 'threshold' => array( + 'name' => t('Stock: threshold'), + 'help' => t('The threshold or warning limit of the stock level'), + ), + ), + 'filters' => array( + 'sku' => array( + 'name' => 'Stock: SKU', + 'operator' => 'views_handler_operator_eqneq', + 'help' => t('Filter the node based on stock SKU.'), + ), + 'stock' => array( + 'name' => 'Stock: Stock Level', + 'operator' => 'views_handler_operator_gtlt', + 'help' => t('Filter the node based on stock level.'), + ), + 'threshold' => array( + 'name' => 'Stock: Threshold', + 'operator' => 'views_handler_operator_gtlt', + 'help' => t('Filter the node based on threshold level.'), + ), + 'is_active' => array( + 'name' => 'Stock: Is Active', + 'operator' => 'views_handler_operator_yesno', + 'handler' => 'uc_stock_views_handler_filter_is_active', + 'help' => t('Filter the node based on whether or not it is derived from content type Product.'), + ), + 'below_threshold' => array( + 'name' => 'Stock: Is Below Threshold', + 'operator' => 'views_handler_operator_yesno', + 'handler' => 'uc_stock_views_handler_filter_below_threshold', + 'help' => t('Filter the node based on whether it stock level is below te threshold.'), + ), + + ), + ); + return $tables; +} + +function uc_stock_views_handler_active($fieldinfo, $fielddata, $value, $data) { + return ($value) ? t('Active') : t('Inactive'); +} + +/** + * Filter out nodes that are not products. + */ +function uc_stock_views_handler_filter_below_threshold($op, $filter, $filterinfo, &$query) { + $types = array_keys(uc_product_node_info()); + + switch ($op){ + case 'handler': + switch ($filter['operator']) { + case '0': + $query->add_where("uc_product_stock.stock >= uc_product_stock.threshold"); + break; + case '1': + $query->add_where("uc_product_stock.stock < uc_product_stock.threshold"); + break; + } + break; + } +} +function uc_stock_views_handler_filter_is_active($op, $filter, $filterinfo, &$query) { + switch ($op){ + case 'handler': + $query->ensure_table("uc_product_stock"); + $query->add_where("uc_product_stock.active = ". $filter['operator']); + } +}