| 1 |
<?php
|
| 2 |
// $Id: hosting.features.inc,v 1.6 2009/09/14 11:44:33 anarcat Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file Hosting features
|
| 6 |
*
|
| 7 |
* Contains the logic used to generate the features list
|
| 8 |
*/
|
| 9 |
define('HOSTING_FEATURE_DISABLED', 0);
|
| 10 |
define('HOSTING_FEATURE_ENABLED', 1);
|
| 11 |
define('HOSTING_FEATURE_REQUIRED', 2);
|
| 12 |
|
| 13 |
function hosting_feature($feature) {
|
| 14 |
static $features = array();
|
| 15 |
|
| 16 |
if (!sizeof($features)) {
|
| 17 |
$features = hosting_get_features();
|
| 18 |
}
|
| 19 |
if ($features[$feature]['module']) {
|
| 20 |
$return = (module_exists($features[$feature]['module'])) ? HOSTING_FEATURE_ENABLED : HOSTING_FEATURE_DISABLED;
|
| 21 |
}
|
| 22 |
else {
|
| 23 |
$return = variable_get('hosting_feature_' . $feature, $features[$feature]['status']);
|
| 24 |
}
|
| 25 |
return $return;
|
| 26 |
}
|
| 27 |
|
| 28 |
function hosting_features_form() {
|
| 29 |
$form['features'] = array(
|
| 30 |
'#type' => 'item',
|
| 31 |
'#title' => t('Optional system features'),
|
| 32 |
'#value' => t('You may choose any of the additional system features from the list below.'),
|
| 33 |
);
|
| 34 |
|
| 35 |
$experimental = array(
|
| 36 |
'#type' => 'fieldset',
|
| 37 |
'#title' => t('Experimental'),
|
| 38 |
'#collapsed' => TRUE,
|
| 39 |
'#collapsible' => TRUE,
|
| 40 |
'#description' => t('Features marked experimental have not been completed to a satisfactory level to be considered production ready, so use at your own risk.'),
|
| 41 |
);
|
| 42 |
$features = hosting_get_features(TRUE);
|
| 43 |
foreach ($features as $feature => $info) {
|
| 44 |
$element = array(
|
| 45 |
'#type' => 'checkbox',
|
| 46 |
'#title' => $info['title'],
|
| 47 |
'#description' => $info['description'],
|
| 48 |
'#default_value' => hosting_feature($feature),
|
| 49 |
'#required' => hosting_feature($feature) == HOSTING_FEATURE_REQUIRED,
|
| 50 |
);
|
| 51 |
if ($info['group'] == 'experimental') {
|
| 52 |
$experimental[$feature] = $element;
|
| 53 |
}
|
| 54 |
else {
|
| 55 |
$form[$feature] = $element;
|
| 56 |
}
|
| 57 |
|
| 58 |
}
|
| 59 |
$form['experimental'] = $experimental;
|
| 60 |
$form['#submit'][] = 'hosting_features_form_submit';
|
| 61 |
return system_settings_form($form);
|
| 62 |
}
|
| 63 |
|
| 64 |
function hosting_features_form_submit($form, &$form_state) {
|
| 65 |
$values = $form_state['values'];
|
| 66 |
$features = hosting_get_features(TRUE);
|
| 67 |
foreach ($features as $feature => $info) {
|
| 68 |
$value = $values[$feature];
|
| 69 |
$current = hosting_feature($feature);
|
| 70 |
variable_set('hosting_feature_' . $feature, $value);
|
| 71 |
if ((!$current) && $value) {
|
| 72 |
if ($module = $features[$feature]['module']) {
|
| 73 |
include_once('includes/install.inc');
|
| 74 |
|
| 75 |
$modules = array($module);
|
| 76 |
$files = module_rebuild_cache();
|
| 77 |
$file = $files[$module];
|
| 78 |
if (isset($file->info['dependencies']) && is_array($file->info['dependencies'])) {
|
| 79 |
foreach ($file->info['dependencies'] as $dependency) {
|
| 80 |
$modules[] = $dependency;
|
| 81 |
}
|
| 82 |
}
|
| 83 |
|
| 84 |
// turn on module
|
| 85 |
drupal_set_message(t("Enabling %module module", array('%module' => implode(",", $modules))));
|
| 86 |
drupal_install_modules($modules);
|
| 87 |
module_enable($modules);
|
| 88 |
}
|
| 89 |
if (function_exists($callback = $features[$feature]['enable'])) {
|
| 90 |
$callback();
|
| 91 |
}
|
| 92 |
}
|
| 93 |
if ($current && !$value) {
|
| 94 |
$dependencies = array();
|
| 95 |
if ($module = $features[$feature]['module']) {
|
| 96 |
$modules = array($module);
|
| 97 |
$files = module_rebuild_cache();
|
| 98 |
foreach ($files as $dependency => $file) {
|
| 99 |
if (isset($file->info['dependencies']) && is_array($file->info['dependencies'])) {
|
| 100 |
if (in_array($module, $file->info['dependencies']) && $file->status) {
|
| 101 |
$dependencies[] = $dependency;
|
| 102 |
}
|
| 103 |
}
|
| 104 |
}
|
| 105 |
|
| 106 |
if ($dependencies) {
|
| 107 |
form_set_error('', t("You cannot disable %module because %dep depends on it", array('%module' => $module, '%dep' => implode(',', $dependencies))));
|
| 108 |
}
|
| 109 |
else {
|
| 110 |
drupal_set_message(t("Disabling %module module", array('%module' => implode(",", $modules))));
|
| 111 |
// turn off module
|
| 112 |
module_disable($modules);
|
| 113 |
if (function_exists($callback = $features[$feature]['disable'])) {
|
| 114 |
$callback();
|
| 115 |
}
|
| 116 |
}
|
| 117 |
}
|
| 118 |
}
|
| 119 |
#print("$feature $current $value $module <br />");
|
| 120 |
}
|
| 121 |
menu_rebuild();
|
| 122 |
}
|
| 123 |
|
| 124 |
function hosting_get_features($refresh = FALSE) {
|
| 125 |
$cache = cache_get('hosting_features');
|
| 126 |
|
| 127 |
if (!$cache->data || $refresh) {
|
| 128 |
## include any optional hosting.feature.*.inc files
|
| 129 |
$files = drupal_system_listing("hosting\.feature\.[a-zA-Z_]*\.inc$", "modules");
|
| 130 |
if (sizeof($files)) {
|
| 131 |
foreach ($files as $name => $info) {
|
| 132 |
include_once($info->filename);
|
| 133 |
}
|
| 134 |
}
|
| 135 |
$functions = get_defined_functions();
|
| 136 |
foreach ($functions['user'] as $function) {
|
| 137 |
if (preg_match('/_hosting_feature$/', $function)) {
|
| 138 |
$hooks[] = $function;
|
| 139 |
}
|
| 140 |
}
|
| 141 |
$features = array();
|
| 142 |
foreach ($hooks as $func) {
|
| 143 |
$features = array_merge($features, $func());
|
| 144 |
}
|
| 145 |
cache_set('hosting_features', $features);
|
| 146 |
return $features;
|
| 147 |
}
|
| 148 |
else {
|
| 149 |
return $cache->data;
|
| 150 |
}
|
| 151 |
}
|
| 152 |
|
| 153 |
function hosting_feature_node_types($refresh = FALSE) {
|
| 154 |
static $types;
|
| 155 |
|
| 156 |
if (!is_array($types) || $refresh) {
|
| 157 |
$features = hosting_get_features($refresh);
|
| 158 |
foreach ($features as $feature => $info) {
|
| 159 |
if ($info['node']) {
|
| 160 |
$types[$feature] = $info['node'];
|
| 161 |
}
|
| 162 |
}
|
| 163 |
}
|
| 164 |
return $types;
|
| 165 |
}
|