type, module_invoke_all('product_types'))) { if(!empty($node->model)) { $document->ss_uc_sku = $node->model; } if (!empty($node->list_price)) { $document->fs_uc_list_price = uc_price($node->list_price, array('revision' => 'altered', 'type' => $node->type, 'subject' => array('node' => $node))); } if (!empty($node->sell_price)) { $document->fs_uc_sell_price = uc_price($node->sell_price, array('revision' => 'altered', 'type' => $node->type, 'subject' => array('node' => $node))); } if(!empty($node->weight)) { $document->fs_uc_weight = $node->weight; } if(!empty($node->length)) { $document->fs_uc_length = $node->length; } if(!empty($node->width)) { $document->fs_uc_width = $node->width; } if(!empty($node->height)) { $document->fs_uc_height = $node->height; } if(!empty($node->ordening)) { $document->fs_uc_position = $node->ordering; } //index also how many times a product has been sold $stat = db_result(db_query('SELECT SUM(qty) FROM {uc_order_products} WHERE nid = %d', $node->nid)); //to prevent Apache Solr Errors while indexing empty values if(empty($stat)) { $stat = 0; } $document->fs_common_num_sold = $stat; //attributes if(!empty($node->attributes)) { foreach($node->attributes as $attribute) { $solr_index_name ="sm_uc_attribute_".$attribute->aid; foreach($attribute->options as $options) { //we could choose to use oid or to use string //for max maintainabilityand "hookability" we go for oid //we will use theming functions to output decent names $document->setMultiValue($solr_index_name, $options->oid); } } } } } /** * Implementation of hook_apachesolr_facets(). */ function apachesolr_ubercart_apachesolr_facets() { $facets = array(); //ubercart list prices $facets['fs_uc_sell_price'] = array( 'info' => t('Apache Solr Search: Filter by Price (Selling price)'), 'facet_field' => 'fs_uc_sell_price', 'dividable' => TRUE, ); $facets['fs_uc_list_price'] = array( 'info' => t('Apache Solr Search: Filter by Price (Listed price)'), 'facet_field' => 'fs_uc_list_price', 'dividable' => TRUE, ); $facets['ss_uc_sku'] = array( 'info' => t('Apache Solr Search: Filter by Model)'), 'facet_field' => 'ss_uc_sku', ); $facets['fs_uc_weight'] = array( 'info' => t('Apache Solr Search: Filter by weight'), 'facet_field' => 'fs_uc_weight', 'dividable' => TRUE, ); $facets['fs_uc_length'] = array( 'info' => t('Apache Solr Search: Filter by length'), 'facet_field' => 'fs_uc_length', 'dividable' => TRUE, ); $facets['fs_uc_width'] = array( 'info' => t('Apache Solr Search: Filter by width'), 'facet_field' => 'fs_uc_width', 'dividable' => TRUE, ); $facets['fs_uc_height'] = array( 'info' => t('Apache Solr Search: Filter by height'), 'facet_field' => 'fs_uc_height', 'dividable' => TRUE, ); $types = uc_product_types(); foreach($types as $type_id => $type) { $attributes = apachesolr_ubercart_uc_get_attributes(); foreach($attributes as $attribute) { $solr_index_name ="sm_uc_attribute_".$attribute->aid; $facets[$solr_index_name] = array( 'info' => t('Apache Solr Search: Filter by attribute @attribute', array('@attribute' => $attribute->name)), 'facet_field' => $solr_index_name, ); } } return $facets; } /** * Implementation of hook_block(). */ function apachesolr_ubercart_block($op = 'list', $delta = 0, $edit = array()) { switch ($op) { case 'list': $enabled_facets = apachesolr_get_enabled_facets('apachesolr_ubercart'); $facets = apachesolr_ubercart_apachesolr_facets(); // Add the blocks $blocks = array(); foreach ($enabled_facets as $delta => $facet_field) { if (isset($facets[$delta])) { $blocks[$delta] = $facets[$delta] + array('cache' => BLOCK_CACHE_PER_PAGE,); } } return $blocks; case 'configure': $facets = apachesolr_ubercart_apachesolr_facets(); $form = array(); if (array_key_exists($delta, $facets)) { if ($facets[$delta]['dividable']) { $form['apachesolr_ubercart_division'] = array( '#type' => 'textfield', '#title' => t('The division of the price block (split all our prices by 20 for example'), '#default_value' => variable_get('apachesolr_ubercart_division_'.$delta,20), ); $form['apachesolr_ubercart_automatic_division'] = array( '#type' => 'checkbox', '#title' => t('You can also choose to let Apachesolr Ubercart divide the prices for you. This is enabled by default'), '#default_value' => variable_get('apachesolr_ubercart_automatic_division_'.$delta,TRUE), ); } } return $form; case 'save': $facets = apachesolr_ubercart_apachesolr_facets(); if (array_key_exists($delta,$facets)) { if ($facets[$delta]['dividable']) { variable_set('apachesolr_ubercart_division_'.$delta, (int) $edit['apachesolr_ubercart_division']); variable_set('apachesolr_ubercart_automatic_division_'.$delta, (int) $edit['apachesolr_ubercart_automatic_division']); } } break; case 'view': if (apachesolr_has_searched()) { // Get the query and response. Without these no blocks make sense. $response = apachesolr_static_response_cache(); if (empty($response)) { return; } $query = apachesolr_current_query(); $facets = apachesolr_get_enabled_facets('apachesolr_ubercart'); if (empty($facets[$delta]) && ($delta != 'currentsearch')) { return; } // Get information needed by the taxonomy blocks about limits. $params = array(); $params['initial_limits'] = variable_get('apachesolr_facet_query_initial_limits', array()); $params['division'] = variable_get('apachesolr_ubercart_division_' . $delta, 20); $params['automatic_division'] = variable_get('apachesolr_ubercart_automatic_division_' . $delta, TRUE); $params['limit_default'] = variable_get('apachesolr_facet_query_initial_limit_default', 10); switch ($delta) { case 'fs_uc_sell_price': return apachesolr_ubercart_price_facet_block($response, $query, 'apachesolr_ubercart', $delta, $delta, t('Filter by Sell Price'),$params); case 'fs_uc_list_price': return apachesolr_ubercart_price_facet_block($response, $query, 'apachesolr_ubercart', $delta, $delta, t('Filter by List Price'),$params); case 'ss_uc_sku': return apachesolr_facet_block($response, $query, 'apachesolr_ubercart', $delta, $delta, t('Filter by product ID')); case 'fs_uc_weight': return apachesolr_ubercart_price_facet_block($response, $query, 'apachesolr_ubercart', $delta, $delta, t('Filter by weight'),$params); case 'fs_uc_length': return apachesolr_ubercart_price_facet_block($response, $query, 'apachesolr_ubercart', $delta, $delta, t('Filter by length'),$params); case 'fs_uc_width': return apachesolr_ubercart_price_facet_block($response, $query, 'apachesolr_ubercart', $delta, $delta, t('Filter by width'),$params); case 'fs_uc_height': return apachesolr_ubercart_price_facet_block($response, $query, 'apachesolr_ubercart', $delta, $delta, t('Filter by height'),$params); } $types = uc_product_types(); foreach($types as $type_id => $type) { $attributes = apachesolr_ubercart_uc_get_attributes(); foreach($attributes as $attribute) { $solr_index_name ="sm_uc_attribute_".$attribute->aid; if($delta == $solr_index_name) { return apachesolr_facet_block($response, $query, 'apachesolr_ubercart', $delta, $delta, t('Filter by attribute @attribute', array('@attribute' => $attribute->name)), 'apachesolr_ubercart_attribute_to_name'); } } } } break; } } /** * Helper function for displaying a facet block based on prices. * * @todo: This is a copy of apachesolr_facet_block() with some modifications. * We should reuse more code, that means apachesolr_facet_block() should be * properly abstracted. */ function apachesolr_ubercart_price_facet_block($response, $query, $module, $delta, $facet_field, $filter_by, $params, $facet_callback = FALSE) { if (!empty($response->facet_counts->facet_fields->$facet_field)) { $facet_query_sorts = variable_get('apachesolr_facet_query_sorts', array()); $contains_active = FALSE; $items = array(); // Construct our facet link amounts. $links = array(); $min_price = 9999999999999999; $max_price = 0; //loop through our results once to find out the min and max price foreach ($response->facet_counts->facet_fields->$delta as $price => $count){ //calculate dynamic division if($price > $max_price) { $max_price = $price; } if($price < $min_price) { $min_price = $price; } } //checking for automatic_division if (isset($params['automatic_division']) && ($params['automatic_division'] == TRUE) && ($max_price != 0)) { //We want no more then 5 facets available in our block $division = intval(($max_price-$min_price)/5); if($division == 0) { $division = $max_price/2; } } else { $division = $params['division']; } //loop again through the prices with the division in mind foreach ($response->facet_counts->facet_fields->$delta as $price => $count){ $options = array('delta' => $delta); $exclude = FALSE; // Solr sends this back if it's empty. if ($facet == '_empty_') { $exclude = TRUE; $facet = '[* TO *]'; $options['html'] = TRUE; } // we use Intval to filter on our divisions. $divide = intval($price/$division); $links[$divide] += $count ; } // Iterate over the available facet links. foreach ($links as $price_range => $count) { $sortpre = 1000000 - $count; $options = array(); $exclude = FALSE; $from_price = ($price_range * $division); $output_from_price = uc_currency_format($from_price, $sign = TRUE, $thou = TRUE, $dec = NULL); //patched from issue #845152 making double facet items impossible $to_price = (($price_range * $division) + $division) - 0.01; $output_to_price = uc_currency_format($to_price, $sign = TRUE, $thou = TRUE, $dec = NULL); $facet_text = t('from @price_from to @price_to', array( '@price_from'=> $output_from_price, '@price_to'=>$output_to_price ) ); //we do not put this in a t function since this is a solr parameter' $facet = '['.$from_price.' TO '.$to_price.']'; $unclick_link = ''; $active = FALSE; $new_query = clone $query; $active = $query->has_filter($facet_field, $facet); if ($query->has_filter($facet_field, $facet)) { $contains_active = TRUE; // '*' sorts before all numbers. $sortpre = '*'; $new_query->remove_filter($facet_field, $facet); $options['query'] = $new_query->get_url_queryvalues(); $link = theme('apachesolr_unclick_link', $facet_text, $new_query->get_path(), $options); } else { $new_query->add_filter($facet_field, $facet, $exclude); $options['query'] = $new_query->get_url_queryvalues(); $link = theme('apachesolr_facet_link', $facet_text, $new_query->get_path(), $options, $count, $active, $response->response->numFound); } if ($count || $active) { $items[$sortpre . '*' . $facet_text] = $link; } } // Unless a facet is active only display 2 or more. if ($items && ($response->response->numFound > 1 || $contains_active)) { ksort($items, SORT_STRING); // Get information needed by the rest of the blocks about limits. $initial_limits = variable_get('apachesolr_facet_query_initial_limits', array()); $limit = isset($initial_limits[$module][$delta]) ? $initial_limits[$module][$delta] : variable_get('apachesolr_facet_query_initial_limit_default', 10); $output = theme('apachesolr_facet_list', $items, $limit); return array('subject' => $filter_by, 'content' => $output); } } return NULL; } /** * Implementation of hook_apachesolr_prepare_query(); */ function apachesolr_ubercart_apachesolr_prepare_query(&$query) { $query->set_available_sort('fs_uc_sell_price', array( 'title' => t('Sell Price'), 'default' => 'asc', )); $query->set_available_sort('fs_uc_list_price', array( 'title' => t('List Price'), 'default' => 'asc', )); $query->set_available_sort('fs_common_num_sold', array( 'title' => t('Sold times'), 'default' => 'asc', )); $query->set_available_sort('fs_uc_weight', array( 'title' => t('Weight'), 'default' => 'asc', )); $query->set_available_sort('fs_uc_width', array( 'title' => t('Width'), 'default' => 'asc', )); $query->set_available_sort('fs_uc_height', array( 'title' => t('Height'), 'default' => 'asc', )); $query->set_available_sort('fs_uc_position', array( 'title' => t('Position'), 'default' => 'asc', )); } /** * Implementation of hook_ca_action(). * * Demonstrates defining an action for predicates to use; primarily specifies a * callback function to perform the action and an array that specifies arguments * and their data types. */ function apachesolr_ubercart_ca_action() { $actions['apachesolr_ubercart_set_stats_in_node'] = array( '#title' => t('Update the statistics in the node field'), '#category' => t('Statistics'), '#callback' => 'apachesolr_ubercart_update_stats', '#arguments' => array( 'order' => array('#entity' => 'uc_order', '#title' => t('Order')), ), ); return $actions; } /** * Perform a custom PHP action. * * @see ca_action_custom_php_form() */ function apachesolr_ubercart_update_stats(&$order, $settings = array()) { //check if we have products if (is_array($order->products)) { foreach ($order->products as $product) { //load node $node = node_load($product->nid); //change the updated date to the current time so it can be reindexed when the next solr cron runs $node->changed = time(); //save the node node_save($node); } } } /** * Implementation of hook_ca_predicate(). * Definition of our statistics updater */ function apachesolr_ubercart_ca_predicate() { $predicates = array(); $predicates['apachesolr_ubercart_update_stats'] = array( '#title' => t('Update statistics'), '#description' => t('Update the statistics for the most sold in Solr by changing the products date.'), '#class' => 'apachesolr_ubercart', '#status' => 1, '#trigger' => 'uc_checkout_complete', '#actions' => array( array( '#name' => 'apachesolr_ubercart_set_stats_in_node', '#title' => t('Update the changed date in the node'), '#argument_map' => array( 'order' => 'order', ), ), ), ); return $predicates; } function apachesolr_ubercart_uc_get_attributes() { $attributes = array(); $result = db_query("SELECT ua.aid FROM {uc_attributes} AS ua ORDER BY ua.ordering, ua.name"); while ($attribute = db_fetch_object($result)) { $attributes[$attribute->aid] = uc_attribute_load($attribute->aid, $pcid, 'class'); } return $attributes; } function apachesolr_ubercart_attribute_to_name($facet, $options) { $option = uc_attribute_option_load($facet); return $option->name; }