function sarnia_schema_rule_form in Sarnia 7
Administration form for rules.
Administrators can use this form to add, delete, reorder, and update the properties of rules.
1 string reference to 'sarnia_schema_rule_form'
- sarnia_schema_page in ./
sarnia.rules.inc - Faux menu router, since this admin page is nested too deeply in the menu.
File
- ./
sarnia.rules.inc, line 65
Code
function sarnia_schema_rule_form($form, &$form_state, $search_api_server, $behavior) {
drupal_set_title(t('!behavior Rules', array(
'!behavior' => ucfirst(_sarnia_get_behavior_label($behavior)),
)));
$form['search_api_server'] = array(
'#type' => 'value',
'#value' => $search_api_server->machine_name,
);
$form['rules'] = array(
'#tree' => TRUE,
);
// Get a list of existing rules.
$rules = sarnia_sarnia_solr_service_schema(array(
'search_api_server' => array(
'',
$search_api_server->machine_name,
),
'behavior' => $behavior,
'enabled' => array(
TRUE,
FALSE,
),
));
// Add a blank rule to the end of the list.
$rules[] = (object) array(
'id' => NULL,
'search_api_server' => NULL,
'behavior' => $behavior,
'match_type' => NULL,
'match_value' => NULL,
'effect' => NULL,
'replacement' => NULL,
'enabled' => TRUE,
'delete' => NULL,
);
// Built a template set of form elements for configuring one rule.
$rule_form = array();
$rule_form['rule'] = array(
'#type' => 'value',
'#value' => NULL,
);
$rule_form['id'] = array(
'#type' => 'value',
'#value' => NULL,
);
$rule_form['search_api_server'] = array(
'#type' => 'checkbox',
'#title' => t('This server only'),
'#title_display' => 'invisible',
'#return_value' => $search_api_server->machine_name,
);
$rule_form['behavior'] = array(
'#type' => 'value',
'#value' => $behavior,
);
$rule_form['match_type'] = array(
'#type' => 'select',
'#title' => t('Match Solr fields by...'),
'#title_display' => 'invisible',
'#options' => drupal_map_assoc(array(
'name',
'dynamicBase',
'type',
)),
);
$rule_form['match_value'] = array(
'#type' => 'textfield',
'#size' => 30,
'#title' => t('Match value'),
'#title_display' => 'invisible',
);
$rule_form['effect'] = array(
'#type' => 'select',
'#title' => t('Rule effect'),
'#title_display' => 'invisible',
'#options' => drupal_map_assoc(array(
'disable',
'replace',
)),
);
$rule_form['replacement'] = array(
'#type' => 'textfield',
'#size' => 30,
'#title' => t('Replacement'),
'#title_display' => 'invisible',
);
$rule_form['enabled'] = array(
'#type' => 'checkbox',
'#title' => t('Enabled'),
'#title_display' => 'invisible',
);
$rule_form['delete'] = array(
'#type' => 'checkbox',
'#title' => t('Delete'),
'#title_display' => 'invisible',
);
// Generate a set of form elements for each rule.
$colors = array();
foreach ($rules as $index => $rule) {
$form['rules'][$index] = $rule_form;
// Set default values for this rule.
$form['rules'][$index]['rule']['#value'] = $rule;
$form['rules'][$index]['id']['#value'] = $rule->id;
$form['rules'][$index]['search_api_server']['#default_value'] = $rule->search_api_server == $search_api_server->machine_name;
$form['rules'][$index]['match_type']['#default_value'] = $rule->match_type;
$form['rules'][$index]['match_value']['#default_value'] = $rule->match_value;
$form['rules'][$index]['effect']['#default_value'] = $rule->effect;
$form['rules'][$index]['replacement']['#default_value'] = $rule->replacement;
$form['rules'][$index]['replacement']['#states'] = array(
'visible' => array(
":input[name='rules[{$index}][effect]']" => array(
'value' => 'replace',
),
),
);
$form['rules'][$index]['enabled']['#default_value'] = $rule->enabled;
$form['rules'][$index]['delete']['#default_value'] = FALSE;
// Display the form element titles for the new rule row.
if (!$rule->id) {
foreach ($form['rules'][$index] as &$element) {
if (isset($element['#title_display'])) {
unset($element['#title_display']);
}
}
$form['rules'][$index]['delete']['#access'] = FALSE;
$form['rules'][$index]['match_type']['#options'] = array(
'' => '',
) + $form['rules'][$index]['match_type']['#options'];
$form['rules'][$index]['effect']['#options'] = array(
'' => '',
) + $form['rules'][$index]['effect']['#options'];
$form['rules'][$index]['enabled'] = array(
'#type' => 'value',
'#value' => TRUE,
);
}
elseif ($rule->enabled) {
// Putting an inline CSS style here is gross, but it allows us to generate
// a range of colors and associate them over the two tables.
$colors[$rule->id] = 'background: hsl(' . count($colors) * 80 % 360 . ', 100%, 90%);';
$form['rules'][$index]['#sarnia_rule_color'] = $colors[$rule->id];
}
}
// Add a save button directly below the rule configuration form.
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
// Add a table of Solr properties that these rules may be applied to.
$form['property_table_info'] = array(
'#type' => 'container',
);
$form['property_table_info'] = array(
'#markup' => '<p>' . 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),
)) . '</p>',
);
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'][] = "<span style='{$color}' title='Rule ID {$rule->id}'>{$rule->effect}</span>";
$form['property_table']['#rows'][$name]['class'][] = 'sarnia-has-schema-rule';
}
else {
$form['property_table']['#rows'][$name]['data'][] = '--';
}
}
return $form;
}