diff --git a/uc_attribute/uc_attribute.info b/uc_attribute/uc_attribute.info index 8f55108..26fb9ec 100644 --- a/uc_attribute/uc_attribute.info +++ b/uc_attribute/uc_attribute.info @@ -7,3 +7,6 @@ core = 7.x ; Test cases files[] = tests/uc_attribute.test files[] = tests/uc_attribute_checkout.test + +; Views handlers +files[] = views/uc_attribute_handler_filter_attribute.inc diff --git a/uc_attribute/uc_attribute.module b/uc_attribute/uc_attribute.module index 94e8311..3f433ce 100644 --- a/uc_attribute/uc_attribute.module +++ b/uc_attribute/uc_attribute.module @@ -1539,3 +1539,13 @@ function uc_attribute_translate(&$attribute) { } } } + +/** + * Implements hook_views_api(). + */ +function uc_attribute_views_api() { + return array( + 'api' => '2.0', + 'path' => drupal_get_path('module', 'uc_attribute') . '/views', + ); +} \ No newline at end of file diff --git a/uc_attribute/views/uc_attribute.views.inc b/uc_attribute/views/uc_attribute.views.inc new file mode 100644 index 0000000..6a981a7 --- /dev/null +++ b/uc_attribute/views/uc_attribute.views.inc @@ -0,0 +1,44 @@ + array( + 'left_field' => 'nid', + 'field' => 'nid', + ), + ); + + $result = db_query("SELECT aid, name, description FROM {uc_attributes}"); + foreach ($result as $row) { + $data['uc_product_adjustments']['attr_' . $row->aid] = array( + 'title' => 'Attribute: ' . $row->name, + 'help' => 'Attribute desc: ' . $row->description, + 'real field' => 'combination', + 'group' => t('Product'), + 'field' => array( + 'handler' => 'views_handler_field', + 'click sortable' => TRUE, + 'group' => t('Product'), + ), + 'filter' => array( + 'handler' => 'uc_attribute_handler_filter_attribute', + 'label' => 'Attribute', + ), + 'argument' => array( + 'handler' => 'views_handler_argument_string', + ), + ); + } + + return $data; +} diff --git a/uc_attribute/views/uc_attribute_handler_filter_attribute.inc b/uc_attribute/views/uc_attribute_handler_filter_attribute.inc new file mode 100644 index 0000000..347f582 --- /dev/null +++ b/uc_attribute/views/uc_attribute_handler_filter_attribute.inc @@ -0,0 +1,43 @@ +value_options)) { + return; + } + $this->value_options = array(); + $aid = explode('_', $this->field); + $aid = $aid[1]; + $result = db_query("SELECT name, oid FROM {uc_attribute_options} WHERE aid = :aid ORDER BY ordering", array(':aid' => $aid)); + while ($row = $result->fetchObject()) { + $this->value_options[$row->oid] = $row->name; + } + } + + /** + * Overrides views_handler_field::query(). + */ + function query() { + $this->ensure_my_table(); + $aid = explode('_', $this->field); + $aid = $aid[1]; + $field = "{$this->table_alias}.{$this->real_field}"; + foreach ($this->value as &$value) { + // Match a part of serialized attributes string. + $value = sprintf('%%i:%d;s:%d:"%d"%%', $aid, strlen($value), $value); + $this->query->add_where($this->options['group'], $field, $value, $this->operator == 'not in' ? 'NOT LIKE' : 'LIKE'); + } + } +}