'Conditions', 'description' => 'Configure conditions other modules use to trigger actions.', 'page callback' => 'drupal_get_form', 'page arguments' => array('condition_list_form'), 'access arguments' => array('administer conditions'), 'file' => 'condition.admin.inc', ); $items['admin/settings/condition/list'] = array( 'title' => t('List'), 'weight' => -10, 'type' => MENU_DEFAULT_LOCAL_TASK, ); $items['admin/settings/condition/add'] = array( 'title' => t('Add'), 'page callback' => 'drupal_get_form', 'page arguments' => array('condition_edit_form'), 'type' => MENU_LOCAL_TASK, 'file' => 'condition.admin.inc', ); $items['admin/settings/condition/%condition/edit'] = array( 'title' => t('Edit'), 'page callback' => 'drupal_get_form', 'page arguments' => array('condition_edit_form', 3), 'type' => MENU_CALLBACK, 'file' => 'condition.admin.inc', ); $items['admin/settings/condition/%condition/delete'] = array( 'title' => t('Delete'), 'page callback' => 'drupal_get_form', 'page arguments' => array('condition_delete_form', 3), 'type' => MENU_CALLBACK, 'file' => 'condition.admin.inc', ); return $items; } /** * Implementation of hook_perm(). */ function condition_perm() { return array('administer conditions'); } /** * Implementation of hook_theme(). */ function condition_theme() { return array( 'condition_edit_form' => array('arguments' => array('form' => NULL)), 'condition_list_form' => array('arguments' => array('form' => NULL)), ); } /** * Implementation of hook_help(). */ function condition_help($path, $arg) { switch ($path) { case 'admin/settings/condition': return '

'.t('Conditions are sets of requirements that make the condition met (TRUE) or not (FALSE). Other modules can provide requirements (e.g. requested path is admin/*) or use the Condition API to trigger some kind of action (e.g. switch theme to Garland) in case a condition is met. Start by !adding a condition.', array('!adding' => l(t('adding'), 'admin/settings/condition/add'))).'

'; } } /** * Loading one, more or all conditions. */ function condition_load($cid = NULL) { static $conditions; if (!is_array($conditions)) { if (is_numeric($cid)) { $condition = db_fetch_object(db_query("SELECT * FROM {conditions} WHERE cid = %d", $cid)); $condition->parameters = (array) unserialize($condition->parameters); return $condition; } else { $result = db_query("SELECT * FROM {conditions} ORDER BY weight ASC"); $conditions = array(); while ($condition = db_fetch_object($result)) { $condition->parameters = (array) unserialize($condition->parameters); $conditions[$condition->cid] = $condition; } } } if (is_array($conditions)) { if (is_numeric($cid)) { return $conditions[$cid]; } elseif (is_array($cid)) { return array_intersect_key($conditions, array_flip($cid)); } else { return $conditions; } } } /** * Saving one or more conditions. */ function condition_save($condition) { if (is_array($condition)) { foreach (array_keys($condition) as $key) { $condition[$key]->saved = condition_save($condition[$key]); } return $condition; } if ($condition->parameters) { $condition->parameters = serialize((array) $condition->parameters); } return drupal_write_record('conditions', $condition, $condition->cid ? 'cid' : array()); } /** * Deleting one or more conditions. */ function condition_delete($condition) { if (is_array($condition)) { foreach (array_keys($condition) as $key) { $condition[$key]->deleted = condition_delete($condition[$key]); } return $condition; } if (!is_object($condition)) { $condition = condition_load($condition); } return db_query("DELETE FROM {conditions} WHERE cid = %d", $condition->cid); } /** * Validating a condition. */ function condition_validate($condition) { static $validations; if (is_array($condition)) { foreach (array_keys($condition) as $key) { $condition[$key]->valid = condition_validate($condition[$key]); } return $condition; } if (is_array($validations) && isset($validations[$condition->cid])) { $condition->valid = $validations[$condition->cid]; return $condition; } if (!$condition->status) { return $validations[$condition->cid] = FALSE; } $requirements = module_invoke_all('requirement_info'); $valid = TRUE; if (is_array($requirements) && count($requirements)) { foreach ($requirements as $requirement => $info) { if ($condition->parameters[$requirement]) { if ($requirement($condition, $condition->parameters[$requirement]) === FALSE) { $valid = FALSE; break; } } } } return $validations[$condition->cid] = $valid; } /* * Selecting conditions for some other module. */ function condition_selection_form(&$form_state) { $conditions = condition_load(); // We might get just the values instead of the form_state. if ($form_state['conditions']) { $form_state['values'] = $form_state; } if (count($conditions)) { $form['conditions'] = array( '#type' => 'fieldset', '#title' => t('Conditions'), '#description' => t('Restrict to situations where any, all or none of the following conditions are met.'), '#collapsible' => TRUE, '#collapsed' => FALSE, '#tree' => TRUE, ); $form['conditions']['operator'] = array( '#type' => 'select', '#title' => 'Situation', '#options' => array( '' => t("(skip conditions)"), CONDITION_ANY => t('Any condition'), CONDITION_ALL => t('All conditions'), CONDITION_NONE => t('No condition'), ), '#default_value' => $form_state['values']['conditions']['operator'], ); $form['conditions']['selection'] = array( '#type' => 'checkboxes', '#title' => t('Conditions'), '#options' => array(), '#default_value' => (array) $form_state['values']['conditions']['selection'], ); foreach ($conditions as $condition) { $form['conditions']['selection']['#options'][$condition->cid] = $condition->name.' ('.l(t('edit'), 'admin/settings/condition/'.$condition->cid.'/edit').')'; } } return $form; } /* * Submit: Selecting conditions for some other module. */ function condition_selection_form_submit($form, $form_state) { return array('conditions' => $form_state['values']['conditions']); } /* * Validating a selection of conditions for some other module. */ function condition_selection_validate($parameters) { // We might get all form values instead of just ours. if ($parameters['conditions']) { $parameters = $parameters['conditions']; } // No operator means we don't use conditions, so that would be a TRUE in order to keep things going. if (!$parameters['operator']) { return TRUE; } $values = array_values($parameters['selection']); sort($values); // We might get the condition ids as values or as keys with the value telling if they are selected. if (array_pop($values) > 1 || array_key_exists(0, $parameters['selection'])) { $parameters['selection'] = array_flip(array_filter($parameters['selection'])); } $conditions = condition_load($parameters['selection']); // If no (existing) conditions were selected our reponse is only TRUE if the operator is NONE. if (!is_array($conditions) || count($conditions) == 0) { return ($parameters['operator'] == CONDITION_NONE); } $valid = 0; foreach ($conditions as $condition) { if (condition_validate($condition)) { $valid++; } } // If we need all to be valid we check if all existing (not selected, some might be deleted) conditions are valid. if ($parameters['operator'] == CONDITION_ALL) { return (count($conditions) == $valid); // If we need just one, that's what we will check here. } else { return ($valid > 0); } } ?>