Project

General

Profile

Paste
Download (9.08 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / module_filter / module_filter.module @ 7d7b5830

1
<?php
2

    
3
/**
4
 * @file
5
 * This is the file description for Module Filter module.
6
 *
7
 * In this more verbose, multi-line description, you can specify what this
8
 * file does exactly. Make sure to wrap your documentation in column 78 so
9
 * that the file can be displayed nicely in default-sized consoles.
10
 *
11
 * @author greenSkin
12
 */
13

    
14
/**
15
 * Implements hook_perm().
16
 */
17
function module_filter_permission() {
18
  return array(
19
    'administer module filter' => array(
20
      'title' => t('Administer Module Filter'),
21
      'description' => t('Configure how Module Filter performs.')
22
    )
23
  );
24
}
25

    
26
/**
27
 * Implements hook_menu().
28
 */
29
function module_filter_menu() {
30
  $items['admin/config/user-interface/modulefilter'] = array(
31
    'title' => 'Module filter',
32
    'description' => 'Configure how the modules page looks and acts.',
33
    'access arguments' => array('administer module filter'),
34
    'page callback' => 'drupal_get_form',
35
    'page arguments' => array('module_filter_settings'),
36
    'file' => 'module_filter.admin.inc'
37
  );
38
  return $items;
39
}
40

    
41
/**
42
 * Implements hook_menu_alter().
43
 */
44
function module_filter_menu_alter(&$items) {
45
  if (isset($items['admin/reports/updates'])) {
46
    // We route the updates report page through us.
47
    $items['admin/reports/updates']['page callback'] = 'module_filter_update_status';
48
    $items['admin/reports/updates']['file'] = 'module_filter.pages.inc';
49
    $items['admin/reports/updates']['module'] = 'module_filter';
50
  }
51
}
52

    
53
/**
54
 * Implements hook_form_FORM_ID_alter().
55
 */
56
function module_filter_form_system_modules_alter(&$form, &$form_state, $form_id) {
57
  // Don't alter the form when confirming.
58
  if (isset($form['confirm'])) {
59
    return;
60
  }
61

    
62
  $form['module_filter'] = array(
63
    '#type' => 'module_filter',
64
    '#attached' => array(
65
      'js' => array(
66
        drupal_get_path('module', 'module_filter') . '/js/modules.js'
67
      )
68
    )
69
  );
70
  $checkbox_defaults = array(
71
    ((isset($_GET['enabled'])) ? $_GET['enabled'] : 1) ? 'enabled' : '',
72
    ((isset($_GET['disabled'])) ? $_GET['disabled'] : 1) ? 'disabled' : '',
73
    ((isset($_GET['required'])) ? $_GET['required'] : 1) ? 'required' : '',
74
    ((isset($_GET['unavailable'])) ? $_GET['unavailable'] : 1) ? 'unavailable' : ''
75
  );
76
  $form['module_filter']['show'] = array(
77
    '#type' => 'checkboxes',
78
    '#default_value' => array_filter($checkbox_defaults),
79
    '#options' => array('enabled' => t('Enabled'), 'disabled' => t('Disabled'), 'required' => t('Required'), 'unavailable' => t('Unavailable')),
80
    '#prefix' => '<div id="module-filter-show-wrapper">',
81
    '#suffix' => '</div>'
82
  );
83

    
84
  if (variable_get('module_filter_tabs', 1)) {
85
    $form['module_filter']['#attached']['css'][] = drupal_get_path('module', 'module_filter') .'/css/module_filter_tab.css';
86
    $form['module_filter']['#attached']['library'][] = array('system', 'jquery.bbq');
87
    $form['module_filter']['#attached']['js'][] = drupal_get_path('module', 'module_filter') .'/js/module_filter_tab.js';
88

    
89
    if (!module_exists('page_actions') && variable_get('module_filter_dynamic_save_position', 1)) {
90
      $form['module_filter']['#attached']['css'][] = drupal_get_path('module', 'module_filter') .'/css/dynamic_position.css';
91
      $form['module_filter']['#attached']['js'][] = drupal_get_path('module', 'module_filter') .'/js/dynamic_position.js';
92
    }
93

    
94
    $form['#attached']['css'][] = drupal_get_path('module', 'module_filter') . '/css/modules.css';
95

    
96
    $form['#theme'] = 'module_filter_system_modules_tabs';
97
  }
98

    
99
  $form['#submit'][] = 'module_filter_system_modules_submit_redirect';
100

    
101
  if (variable_get('module_filter_track_recent_modules', 1)) {
102
    $form['#submit'][] = 'module_filter_system_modules_submit_recent';
103
  }
104
}
105

    
106
/**
107
 * Implements hook_form_FORM_ID_alter().
108
 */
109
function module_filter_form_user_admin_permissions_alter(&$form, &$form_state) {
110
  $form['module_filter'] = array(
111
    '#type' => 'module_filter',
112
    '#description' => t('Filter list by module. Use the query operator "perm" to filter by permission, e.g., perm:access.'),
113
    '#attached' => array(
114
      'js' => array(
115
        drupal_get_path('module', 'module_filter') . '/js/permissions.js',
116
      ),
117
    ),
118
    '#weight' => -100,
119
  );
120
}
121

    
122
/**
123
 * Implements hook_element_info().
124
 */
125
function module_filter_element_info() {
126
  $types['module_filter'] = array(
127
    '#input' => TRUE,
128
    '#process' => array('form_process_module_filter', 'ajax_process_form'),
129
    '#weight' => -1,
130
    '#tree' => TRUE,
131
    '#theme' => 'module_filter'
132
  );
133
  return $types;
134
}
135

    
136
/**
137
 * Implements hook_theme().
138
 */
139
function module_filter_theme() {
140
  return array(
141
    'module_filter' => array(
142
      'render element' => 'element',
143
      'file' => 'module_filter.theme.inc',
144
    ),
145
    'module_filter_system_modules_tabs' => array(
146
      'render element' => 'form',
147
      'file' => 'module_filter.theme.inc',
148
    ),
149
    'module_filter_operations' => array(
150
      'variables' => array('links' => array(), 'dropbutton' => FALSE),
151
      'file' => 'module_filter.theme.inc',
152
    ),
153
  );
154
}
155

    
156
function form_process_module_filter($element, &$form_state) {
157
  $element['name'] = array(
158
    '#type' => 'textfield',
159
    '#title' => (isset($element['#title'])) ? $element['#title'] : t('Filter list'),
160
    '#default_value' => (isset($element['#default_value'])) ? $element['#default_value'] : ((isset($_GET['filter'])) ? $_GET['filter'] : ''),
161
    '#size' => (isset($element['#size'])) ? $element['#size'] : 45,
162
    '#weight' => (isset($element['#weight'])) ? $element['#weight'] : -10,
163
    '#attributes' => ((isset($element['#attributes'])) ? $element['#attributes'] : array()) + array('autocomplete' => 'off'),
164
    '#attached' => array(
165
      'css' => array(
166
        drupal_get_path('module', 'module_filter') . '/css/module_filter.css'
167
      ),
168
      'js' => array(
169
        'misc/jquery.cookie.js',
170
        drupal_get_path('module', 'module_filter') . '/js/module_filter.js',
171
        array(
172
          'data' => array(
173
            'moduleFilter' => array(
174
              'setFocus' => variable_get('module_filter_set_focus', 1),
175
              'tabs' => variable_get('module_filter_tabs', 1),
176
              'countEnabled' => variable_get('module_filter_count_enabled', 1),
177
              'visualAid' => variable_get('module_filter_visual_aid', 1),
178
              'hideEmptyTabs' => variable_get('module_filter_hide_empty_tabs', 0),
179
              'dynamicPosition' => (!module_exists('page_actions')) ? variable_get('module_filter_dynamic_save_position', 1) : FALSE,
180
              'useURLFragment' => variable_get('module_filter_use_url_fragment', 1),
181
              'useSwitch' => variable_get('module_filter_use_switch', 1),
182
              'trackRecent' => variable_get('module_filter_track_recent_modules', 1),
183
              'rememberActiveTab' => variable_get('module_filter_remember_active_tab', 1),
184
              'rememberUpdateState' => variable_get('module_filter_remember_update_state', 0),
185
            )
186
          ),
187
          'type' => 'setting'
188
        )
189
      )
190
    )
191
  );
192
  if (isset($element['#description'])) {
193
    $element['name']['#description'] = $element['#description'];
194
  }
195
  return $element;
196
}
197

    
198
function module_filter_system_modules_submit_redirect($form, &$form_state) {
199
  $query = array();
200
  if (!empty($form_state['values']['module_filter']['name'])) {
201
    $query['filter'] = $form_state['values']['module_filter']['name'];
202
  }
203
  $query['enabled'] = (int)(!empty($form_state['values']['module_filter']['show']['enabled']));
204
  $query['disabled'] = (int)(!empty($form_state['values']['module_filter']['show']['disabled']));
205
  $query['required'] = (int)(!empty($form_state['values']['module_filter']['show']['required']));
206
  $query['unavailable'] = (int)(!empty($form_state['values']['module_filter']['show']['unavailable']));
207

    
208
  $form_state['redirect'] = array(
209
    'admin/modules',
210
    array('query' => $query),
211
  );
212
}
213

    
214
function module_filter_system_modules_submit_recent($form, &$form_state) {
215
  $recent_modules = variable_get('module_filter_recent_modules', array());
216

    
217
  foreach ($form_state['values']['modules'] as $package => $modules) {
218
    foreach ($modules as $key => $module) {
219
      if ($form['modules'][$package][$key]['enable']['#default_value'] != $module['enable']) {
220
        $recent_modules[$key] = REQUEST_TIME;
221
      }
222
    }
223
  }
224

    
225
  variable_set('module_filter_recent_modules', $recent_modules);
226
}
227

    
228
function module_filter_new_modules() {
229
  // Get current list of modules.
230
  $files = system_rebuild_module_data();
231

    
232
  // Remove hidden modules from display list.
233
  $visible_files = $files;
234
  foreach ($visible_files as $filename => $file) {
235
    if (!empty($file->info['hidden'])) {
236
      unset($visible_files[$filename]);
237
    }
238
  }
239

    
240
  uasort($visible_files, 'system_sort_modules_by_info_name');
241

    
242
  $new_modules = array();
243
  foreach ($visible_files as $filename => $module) {
244
    $ctime = filectime(dirname($module->uri) . '/' . $module->name . '.info');
245
    if (($ctime - strtotime('-1 week')) > 0) {
246
      $new_modules[$filename] = module_filter_get_id($filename);
247
    }
248
  }
249
  return $new_modules;
250
}
251

    
252
function module_filter_get_id($text) {
253
  $id = strtolower($text);
254
  $id = preg_replace('/([^a-z0-9]+)/', '-', $id);
255
  return trim($id, '-');
256
}
257

    
258
function module_filter_recent_filter($var) {
259
  return (!($var < REQUEST_TIME - 60*60*24*7));
260
}