diff --git a/uc_attribute/views/uc_attribute_handler_field_combination.inc b/uc_attribute/views/uc_attribute_handler_field_combination.inc index eee5a2e..9375955 100644 --- a/uc_attribute/views/uc_attribute_handler_field_combination.inc +++ b/uc_attribute/views/uc_attribute_handler_field_combination.inc @@ -31,6 +31,7 @@ class uc_attribute_handler_field_combination extends views_handler_field { $values = array(); $values['nid'] = $nid; + $query_select_count = $query_select . " COUNT(*),"; foreach ($result as $prod_attr) { $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 = :nid),"; @@ -43,11 +44,17 @@ class uc_attribute_handler_field_combination extends views_handler_field { // Remove last connecting parts (commas, "AND") $query_select = rtrim($query_select, ','); + $query_select_count = rtrim($query_select_count, ','); $query_from = rtrim($query_from, ','); $query_where = substr($query_where, 0, strlen($query_where) - 4); $query_order = rtrim($query_order, ','); if ($num_prod_attr) { + // Count because this array can get huge, so do not process if over the configured amount + $count = db_query($query_select_count . $query_from . $query_where . $query_order, $values)->fetchField(); + if ($count > $this->options['fetch_limit']) { + return FALSE; + } $result = db_query($query_select . $query_from . $query_where . $query_order, $values); foreach ($result as $row) { $attrs_info = array(); @@ -86,6 +93,7 @@ class uc_attribute_handler_field_combination extends views_handler_field { $options = parent::option_definition(); $options['fetch_if_null'] = array('default' => FALSE); + $options['fetch_limit'] = array('default' => 10); return $options; } @@ -102,7 +110,15 @@ class uc_attribute_handler_field_combination extends views_handler_field { '#description' => t('If checked, and if the combination is not found for a specific SKU/model, get the attributes and options from other tables.'), '#default_value' => $this->options['fetch_if_null'], ); - + $form['fetch_limit'] = array( + '#type' => 'textfield', + '#title' => t('Fetch limit'), + '#description' => t('Attempting to build the full combination of attributes could led to a huge amount of items. To prevent ever-lasting queries and a big output, we encourage to limit the amount of it or not to use this feature.'), + '#default_value' => $this->options['fetch_limit'], + '#dependency' => array( + 'edit-options-fetch-if-null' => array(1) + ), + ); } function pre_render(&$values) { @@ -135,6 +151,9 @@ class uc_attribute_handler_field_combination extends views_handler_field { else if ($this->options['fetch_if_null']) { $nid = $values->nid; $combinations = $this->_uc_stock_extra_combinations($nid); + if ($combinations === FALSE) { + return t('Too many attributes.'); + } if (!empty($combinations)) { foreach ($combinations as $combination => $attr_info) { $attributes = array();