'markup', '#markup' => '
' . t("This section can help configure the behavior of your Solr properties. Because the behavior of Solr properties is determined by a particular Solr core's schema.xml
file, a particular Solr setup may customize property names, dynamic bases for aggregation, or property type names.") . '
' . t("Configuration on this page is not necessary; Sarnia can determine which properties are technically available for display, filter search, fulltext search, or sort. However, if you find that some properties are available to Sarnia Views handlers in the Views UI where they do not make sense, configuring the schema rules can help refine properties' behavior.") . '
' . '' . t("Properties may be disabled, or may be replaced with another property. Disabling a property is useful for things like preventing sorting on fulltext search fields (which yield unpredicatble sort orders) or preventing the display of aggregated fields; replacing a property allows you to associate a sortable property with a displayable property, which makes click-sorting available on Views fields. These adjustments are not relevant to the end user, but they may be useful for sitebuilders working with Views who are not intimately familiar with the particulars of Solr.") . '
' . '' . t("Sarnia comes with a basic set of rules which support some schema.xml
conventions. These may be removed or overridden for a particular Search API server.") . '
' . t("The following reference table shows a list of Solr properties eligible for @behavior. If a rule matches a particular property, it's effect is listed in the far-right column color-coded to match the table of rules above. Only one rule may match a property, and rules match in the order listed (first by name, then by dynamicBase, and last by type).", array('@behavior' => _sarnia_get_behavior_label($behavior))) . '
', ); module_load_include('inc', 'sarnia', 'sarnia.entities'); $fields = $search_api_server->_getFilteredFields($behavior); $form['property_table'] = sarnia_entity_properties_table($search_api_server, $fields); array_pop($form['property_table']['#header']); array_pop($form['property_table']['#header']); $form['property_table']['#header'][] = 'Rule effect'; foreach ($form['property_table']['#rows'] as $name => $row) { $rule = $search_api_server->schemaGetRule($fields[$name], $behavior); array_pop($form['property_table']['#rows'][$name]['data']); array_pop($form['property_table']['#rows'][$name]['data']); if ($rule) { $color = $colors[$rule->id]; $form['property_table']['#rows'][$name]['data'][] = "{$rule->effect}"; $form['property_table']['#rows'][$name]['class'][] = 'sarnia-has-schema-rule'; } else { $form['property_table']['#rows'][$name]['data'][] = '--'; } } return $form; } /** * Transforms the rules administration form into a table. */ function theme_sarnia_schema_rule_form($variables) { $form = $variables['form']; $columns = array( 'enabled', 'search_api_server', 'match_type', 'match_value', 'effect', 'replacement', 'delete', ); $header = array(); $rows = array(); $stripe = TRUE; foreach (element_children($form['rules']) as $i) { $element = &$form['rules'][$i]; $stripe = !$stripe; // New rule title. if (!$element['rule']['#value']->id) { $row = array( 'data' => array(), 'no_striping' => TRUE, 'class' => array(($stripe ? 'odd' : 'even')), ); $row['data'][] = array( 'data' => '' . t('Add a new rule:') . '', 'colspan' => count($columns), ); $rows[] = $row; } // Rule form as row. $row = array( 'data' => array(), 'no_striping' => TRUE, 'class' => array(($stripe ? 'odd' : 'even')), ); foreach ($columns as $col) { $row['data'][$col] = drupal_render($element[$col]); if (empty($header[$col])) { $header[$col] = isset($element[$col]['#title']) ? $element[$col]['#title'] : ''; } } $row['data']['search_api_server'] = array( 'data' => $row['data']['search_api_server'], 'width' => '20px', ); $rows[] = $row; // Rule description text. if ($element['rule']['#value']->id) { $row = array( 'data' => array('', ''), 'no_striping' => TRUE, 'class' => array(($stripe ? 'odd' : 'even')), ); $color = isset($element['#sarnia_rule_color']) ? $element['#sarnia_rule_color'] : ''; $row['data'][] = array( 'data' => "id . "'>" . _sarnia_get_rule_text($element['rule']['#value']) . '', 'colspan' => count($columns) - 1, ); $rows[] = $row; } } $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'sarnia-schema-rules'))); $output .= drupal_render_children($form); return $output; } function sarnia_schema_rule_form_validate($form, &$form_state) { $non_empty = array( 'match_type' => t('Match type'), 'match_value' => t('Match value'), 'effect' => t('Rule effect'), ); foreach ($form_state['values']['rules'] as $i => $values) { if (_sarnia_schema_rule_is_changed($values['rule'], $values)) { // Don't allow empty values in rules. foreach ($non_empty as $key => $label) { if (empty($values[$key])) { form_set_error("rules][$i][$key", t('!label cannot be empty.', array('!label' => $label))); } } // Disallow 'effect' == 'replace' when 'match_type' == 'type' if ($values['match_type'] == 'type' && $values['effect'] = 'replace') { form_set_error("rules][$i][effect", t("The 'replace' effect can only be used when matching on 'name' and 'dynamicBase'.")); } // Require a 'replace' value when 'effect' == 'replace' if ($values['effect'] == 'replace' && empty($values['replacement'])) { form_set_error("rules][$i][replacement", t("When using a 'replace' rule, a value must be provided in the 'replacement' field.")); } } } } /** * Form submit handler for sarnia schema rule administration. */ function sarnia_schema_rule_form_submit($form, &$form_state) { $changes = FALSE; foreach ($form_state['values']['rules'] as $values) { // The "this server only" checkbox will return either a server name or 0. if (!$values['search_api_server']) { $values['search_api_server'] = ''; } // If the "delete" checkbox was checked, delete the rule. if (!empty($values['delete'])) { // delete rule db_delete('sarnia_solr_service_schema') ->condition('id', $values['id']) ->execute(); drupal_set_message(t('Rule %rule has been deleted.', array('%rule' => $values['id']))); } // Otherwise, check whether the rule changed and create or update the record. elseif (_sarnia_schema_rule_is_changed($values['rule'], $values)) { $changes = TRUE; if (!empty($values['id'])) { drupal_write_record('sarnia_solr_service_schema', $values, 'id'); } else { drupal_write_record('sarnia_solr_service_schema', $values); } } } if ($changes) { drupal_set_message(t('Your changes have been saved.')); } } function _sarnia_schema_rule_is_changed($rule, $form_values) { foreach ($form_values as $key => $value) { if (property_exists($rule, $key) && $rule->{$key} != $value) { return TRUE; } } return FALSE; } function _sarnia_get_rule_text($rule) { $replacements = array( '@effect' => $rule->effect, '@behavior' => _sarnia_get_behavior_label($rule->behavior), '@match_type' => $rule->match_type, '@match_value' => $rule->match_value, '@replacement' => $rule->replacement, ); if ($rule->effect == 'disable') { return t("@effect @behavior when property @match_type = @match_value", $replacements); } elseif ($rule->effect == 'replace') { return t("use properties where @match_type = @match_value to @behavior properties where @match_type = @replacement", $replacements); } } function _sarnia_get_behavior_label($behavior) { $behavior_labels = array( 'display' => t('display'), 'filter' => t('filter search'), 'fulltext' => t('fulltext search'), 'sort' => t('sort'), ); return isset($behavior_labels[$behavior]) ? $behavior_labels[$behavior] : NULL; }