diff --git a/uc_views_attribute/uc_views_attribute.info b/uc_views_attribute/uc_views_attribute.info index d4be5e9..fd74833 100644 --- a/uc_views_attribute/uc_views_attribute.info +++ b/uc_views_attribute/uc_views_attribute.info @@ -1,3 +1,4 @@ +; $Id: uc_views_attribute.info,v 1.1.2.3 2010/01/19 18:08:58 hanoii Exp $ name = Ubercart Views Attribute description = Create views on Ubercart Attribute data. dependencies[] = uc_stock @@ -5,6 +6,13 @@ dependencies[] = uc_product dependencies[] = uc_order dependencies[] = uc_attribute dependencies[] = views -dependencies[] = uc_views +; dependencies[] = uc_views package = "Ubercart - views" -core = 6.x +core = 7.x + + + +files[] = views/uc_views_attribute_handler_field_order_product_attribute.inc +files[] = views/uc_views_attribute_handler_field_combination.inc +files[] = views/uc_views_attribute_handler_field_combination_price.inc +files[] = views/uc_views_attribute_handler_filter_attr.inc \ No newline at end of file diff --git a/uc_views_attribute/uc_views_attribute.install b/uc_views_attribute/uc_views_attribute.install index 268d5fc..0068a1c 100644 --- a/uc_views_attribute/uc_views_attribute.install +++ b/uc_views_attribute/uc_views_attribute.install @@ -1,13 +1,18 @@ '2.0', - 'path' => drupal_get_path('module', 'uc_views_attribute') .'/views', + 'path' => drupal_get_path('module', 'uc_views_attribute') . '/views', ); } diff --git a/uc_views_attribute/views/uc_views_attribute.views.inc b/uc_views_attribute/views/uc_views_attribute.views.inc index 46e8c5c..22d8217 100644 --- a/uc_views_attribute/views/uc_views_attribute.views.inc +++ b/uc_views_attribute/views/uc_views_attribute.views.inc @@ -1,21 +1,21 @@ aid] = array( - 'title' => 'Attribute: '.$row->name, - 'help' => 'Attribute desc: '.$row->description, + foreach($result as $row) { + $data['uc_order_products']['attr_' . $row->aid] = array( + 'title' => 'Attribute: ' . $row->name, + 'help' => 'Attribute desc: ' . $row->description, 'filter' => array( 'handler' => 'uc_views_attribute_handler_filter_attr', ), @@ -29,8 +29,8 @@ function uc_views_attribute_views_data() { $uc_product_adjustments = drupal_get_schema('uc_product_adjustments'); $data['uc_product_adjustments']['table']['join']['node'] = array( - 'left_field' => 'nid', - 'field' => 'nid', + 'left_field' => 'nid', + 'field' => 'nid', ); $data['uc_product_adjustments']['model'] = array( @@ -45,7 +45,7 @@ function uc_views_attribute_views_data() { 'relationship field' => 'model', 'handler' => 'views_handler_relationship', 'label' => t('Model'), - ) + ), ); $data['uc_product_adjustments']['combination'] = array( @@ -90,7 +90,6 @@ function uc_views_attribute_views_data() { 'handler' => 'views_handler_relationship', 'label' => t('SKU'), ); - // Add viewhandler for uc_order_product attributes $data['uc_order_products']['attributes'] = array( 'title' => t('Product attributes'), @@ -107,7 +106,7 @@ function uc_views_attribute_views_data() { } /** - * Implementation of hook_views_handlers(). + * Implements hook_views_handlers(). */ function uc_views_attribute_views_handlers() { return array( @@ -120,7 +119,7 @@ function uc_views_attribute_views_handlers() { 'uc_views_attribute_handler_field_order_product_attribute' => array('parent' => 'views_handler_field'), 'uc_views_attribute_handler_field_combination_price' => array('parent' => 'uc_views_handler_field_money_amount'), //fiters - 'uc_views_attribute_handler_filter_attr' => array('parent' => 'views_handler_filter_in_operator',), + 'uc_views_attribute_handler_filter_attr' => array('parent' => 'views_handler_filter_in_operator'), ), ); } @@ -128,8 +127,7 @@ function uc_views_attribute_views_handlers() { /** * Conditionally add editablefields support. */ -function uc_views_attribute_views_tables_alter(&$tables) { -} +function uc_views_attribute_views_tables_alter(&$tables) { } /** * Load all attributes. @@ -137,7 +135,7 @@ function uc_views_attribute_views_tables_alter(&$tables) { function uc_get_attributes() { $result = db_query("SELECT aid FROM {uc_attributes} ORDER BY ordering"); $chosen_attr = array(); - while ($attr = db_fetch_object($result)){ + while ($attr = db_fetch_object($result)) { $chosen_attr[$attr->aid] = uc_attribute_load($attr->aid); } return $chosen_attr; diff --git a/uc_views_attribute/views/uc_views_attribute_handler_field_combination.inc b/uc_views_attribute/views/uc_views_attribute_handler_field_combination.inc index 2b52703..a58e3de 100644 --- a/uc_views_attribute/views/uc_views_attribute_handler_field_combination.inc +++ b/uc_views_attribute/views/uc_views_attribute_handler_field_combination.inc @@ -1,4 +1,5 @@ 0 ORDER BY pa.ordering, a.ordering", $nid, $nid); + $result = db_query("SELECT pa.nid, pa.aid, 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 = :pa.nid GROUP BY ao.aid, pa.aid, a.name, pa.ordering, a.ordering, pa.nid HAVING count(po.oid) > :count(po.oid) ORDER BY pa.ordering, a.ordering", array(':pa.nid' => $nid, ':count(po.oid)' => 0, '' => $nid)); $i = 1; $values = array(); @@ -34,7 +35,7 @@ class uc_views_attribute_handler_field_combination extends views_handler_field { $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_where .= " ao$i.aid = " . $prod_attr->aid . " AND"; $query_order .= " po$i.ordering, ao$i.name,"; ++$i; $attribute_ids[] = $prod_attr->aid; @@ -48,16 +49,17 @@ class uc_views_attribute_handler_field_combination extends views_handler_field { $query_order = rtrim($query_order, ','); if ($num_prod_attr) { + // TODO Please convert this statement to the D7 database API syntax. $result = db_query($query_select . $query_from . $query_where . $query_order, $values); while ($row = db_fetch_object($result)) { $attrs_info = array(); $attrs = array(); - for ($i = 1 ; $i <= $num_prod_attr ; $i++) { + for ($i = 1; $i <= $num_prod_attr; $i++) { $aid_key = "aid$i"; $oid_key = "oid$i"; $name_key = "name$i"; $aid = $row->$aid_key; - $attr = db_result(db_query("SELECT name FROM {uc_attributes} WHERE aid = %d", $aid)); + $attr = db_query("SELECT name FROM {uc_attributes} WHERE aid = :aid", array(':aid' => $aid))->fetchField(); $attrs_info[$attr] = $row->$name_key; $attrs[$aid] = $row->$oid_key; } @@ -67,7 +69,7 @@ class uc_views_attribute_handler_field_combination extends views_handler_field { } // Exclude configured combinations - $result = db_query("SELECT * FROM {uc_product_adjustments} WHERE nid = %d", $nid); + $result = db_query("SELECT * FROM {uc_product_adjustments} WHERE nid = :nid", array(':nid' => $nid)); while ($row = db_fetch_object($result)) { // TODO: Remove unserialize/asort/serialize because rc6+ already sorts it $c = unserialize($row->combination); @@ -125,13 +127,13 @@ class uc_views_attribute_handler_field_combination extends views_handler_field { if (is_string($combination)) { $combination = unserialize($combination); $attributes = array(); - foreach($combination as $aid => $oid) { + foreach ($combination as $aid => $oid) { $attribute = uc_attribute_load($aid); $option = uc_attribute_option_load($oid); $attributes[] = "$attribute->name: $option->name"; } $items[] = implode($attributes, ', '); - return theme('item_list', $items); + return theme('item_list', array('items' => $items)); } // combination information is not found, render other node's attribute and options else if ($this->options['fetch_if_null']) { @@ -145,7 +147,7 @@ class uc_views_attribute_handler_field_combination extends views_handler_field { } $items[] = implode($attributes, ', '); } - return theme('item_list', $items); + return theme('item_list', array('items' => $items)); } } return parent::render($values); diff --git a/uc_views_attribute/views/uc_views_attribute_handler_field_combination_price.inc b/uc_views_attribute/views/uc_views_attribute_handler_field_combination_price.inc index 9c07995..57625d9 100644 --- a/uc_views_attribute/views/uc_views_attribute_handler_field_combination_price.inc +++ b/uc_views_attribute/views/uc_views_attribute_handler_field_combination_price.inc @@ -1,4 +1,5 @@ ensure_my_table(); $this->add_additional_fields(); // Is this the proper way to access vid? $this->aliases['vid'] = $this->query->add_field('node', 'vid'); } - + function render($values) { switch ($this->definition['price']) { case 'sell_price': @@ -37,14 +38,16 @@ class uc_views_attribute_handler_field_combination_price extends uc_views_handle // I did try to add it through // additional_fields => array('product_price' => array('table' => 'uc_products', 'field' => 'sell_price')) // but it didn't work. - $price = db_result(db_query("SELECT $uc_products_field FROM {uc_products} WHERE nid = %d AND vid = %d", $values->nid, $values->{$this->aliases['vid']})); + // TODO Please convert this statement to the D7 database API syntax. + $price = db_query("SELECT $uc_products_field FROM {uc_products} WHERE nid = %d AND vid = %d", $values->nid, $values->{$this->aliases['vid']})->fetchField(); $offset_total = 0; - - $combination =$values->{$this->aliases['combination']}; + + $combination = $values->{$this->aliases['combination']}; if ($combination) { $combination = unserialize($combination); foreach ($combination as $aid => $oid) { - $offset = db_result(db_query("SELECT $uc_product_options_field FROM {uc_product_options} WHERE nid = %d AND oid = %d", $values->nid, $oid)); + // TODO Please convert this statement to the D7 database API syntax. + $offset = db_query("SELECT $uc_product_options_field FROM {uc_product_options} WHERE nid = %d AND oid = %d", $values->nid, $oid)->fetchField(); $offset_total += $offset; } } diff --git a/uc_views_attribute/views/uc_views_attribute_handler_field_order_product_attribute.inc b/uc_views_attribute/views/uc_views_attribute_handler_field_order_product_attribute.inc index 7b26e4f..b4384d7 100644 --- a/uc_views_attribute/views/uc_views_attribute_handler_field_order_product_attribute.inc +++ b/uc_views_attribute/views/uc_views_attribute_handler_field_order_product_attribute.inc @@ -1,10 +1,11 @@ $option) { - $rows[] = t('@attribute: @option', array('@attribute' => $attribute, '@option' => implode(', ', (array)$option))); + $rows[] = t('@attribute: @option', array('@attribute' => $attribute, '@option' => implode(', ', (array) $option))); if (count($rows)) { - $result = theme('item_list', $rows, NULL, 'ul', array('class' => 'product-description')); + $result = theme('item_list', array('items' => $rows, 'title' => NULL, 'type' => 'ul', 'attributes' => array('class' => 'product-description'))); } } } diff --git a/uc_views_attribute/views/uc_views_attribute_handler_filter_attr.inc b/uc_views_attribute/views/uc_views_attribute_handler_filter_attr.inc index 9c2cef3..171dd53 100644 --- a/uc_views_attribute/views/uc_views_attribute_handler_filter_attr.inc +++ b/uc_views_attribute/views/uc_views_attribute_handler_filter_attr.inc @@ -1,10 +1,13 @@ value_options)) { - $aid = explode('_',$this->field); + $aid = explode('_', $this->field); $aid = $aid[1]; - + $this->value_title = t('Options'); - $result = db_query("SELECT name, oid FROM {uc_attribute_options} WHERE aid = %d ORDER BY ordering", $aid); + $result = db_query("SELECT name, oid FROM {uc_attribute_options} WHERE aid = :aid ORDER BY ordering", array(':aid' => $aid)); while ($row = db_fetch_object($result)) { $options[$row->oid] = $row->name; @@ -30,66 +33,69 @@ class uc_views_attribute_handler_filter_attr extends views_handler_filter_in_ope $this->value_options = $options; } } - + function extra_options_form(&$form, &$form_state) { $form['type'] = array( '#type' => 'radios', '#title' => t('Selection type'), - '#options' => array('select' => t('Checkboxes/Dropdown'), 'textfield' => t('Textfield')), + '#options' => array( + 'select' => t('Checkboxes/Dropdown'), + 'textfield' => t('Textfield'), + ), '#default_value' => $this->options['type'], ); } - + function value_form(&$form, &$form_state) { parent::value_form($form, $form_state); if ($this->options['type'] == 'textfield') { - $form['value'] = array ( + $form['value'] = array( '#type' => 'textfield', '#title' => 'Text search', ); } } - + function query() { - $aid = explode('_',$this->field); + $aid = explode('_', $this->field); $aid = $aid[1]; - - $key = db_result(db_query('SELECT name FROM {uc_attributes} WHERE aid = %d', $aid)); - + + $key = db_query('SELECT name FROM {uc_attributes} WHERE aid = :aid', array(':aid' => $aid))->fetchField(); + $this->ensure_my_table(); $this->real_field = 'data'; $field = "$this->table_alias.$this->real_field"; $upper = ''; - if($this->operator == 'not in') { + if ($this->operator == 'not in') { $not = 'NOT'; } - if(!is_array($this->value)) { + if (!is_array($this->value)) { $this->value = array($this->value); } foreach ($this->value as &$value) { - if($this->options['type'] == 'textfield') { - $optval = $value; - } - else { - $optval = db_result(db_query('SELECT name FROM {uc_attribute_options} WHERE oid = %d', $value)); - } - if(!$optval) { - //If we let the query get added, only products with the attribute enabled & with the value of that attribute empty - //will return. By default, I think it makes more sense to allow any product to return despite whether or not it - //has the attribute if the user put nothing in to search for. - //Adding a new type of filter that merely captures whether or not the attribute exists at all for the product may - //be the best way to capture the alternative. Or, perhaps a config option could be added to this filter : - //"Show products only if they have this attribute enabled". If this option is unchecked, a blank value returns all - //products. If checked, a blank value at least filters out products that dont have this attribute available. - continue; - } - $var = array($key => array(0 => $optval)); - $servar = serialize($var); - $l = strpos($servar,'{')+1; - $r = strpos($servar,'}')-$l; - $value = substr($servar,$l,$r); - //@todo - this method of building the query does allow for partial text matching (i.e. 'foo' matches in 'foobar'). - $this->query->add_where($this->options['group'], "$upper(%s) $not LIKE $upper('%%%s%%')", $field, $value); + if ($this->options['type'] == 'textfield') { + $optval = $value; + } + else { + $optval = db_query('SELECT name FROM {uc_attribute_options} WHERE oid = :oid', array(':oid' => $value))->fetchField(); + } + if (!$optval) { + //If we let the query get added, only products with the attribute enabled & with the value of that attribute empty + //will return. By default, I think it makes more sense to allow any product to return despite whether or not it + //has the attribute if the user put nothing in to search for. + //Adding a new type of filter that merely captures whether or not the attribute exists at all for the product may + //be the best way to capture the alternative. Or, perhaps a config option could be added to this filter : + //"Show products only if they have this attribute enabled". If this option is unchecked, a blank value returns all + //products. If checked, a blank value at least filters out products that dont have this attribute available. + continue; + } + $var = array($key => array(0 => $optval)); + $servar = serialize($var); + $l = strpos($servar, '{') + 1; + $r = strpos($servar, '}') -$l; + $value = substr($servar, $l, $r); + //@todo - this method of building the query does allow for partial text matching (i.e. 'foo' matches in 'foobar'). + $this->query->add_where($this->options['group'], "$upper(%s) $not LIKE $upper('%%%s%%')", $field, $value); } } }