Projet

Général

Profil

Paste
Télécharger (5,59 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / imce_mkdir / imce_mkdir.inc @ 87dbc3bf

1
<?php
2

    
3
/**
4
 * Implements hook_form_formID_alter().
5
 */
6
function _imce_mkdir_form_imce_profile_form_alter(&$form, &$form_state) {
7
  foreach (element_children($form['profile']['directories']) as $key) {
8
    $form['profile']['directories'][$key]['mkdir'] = array(
9
      '#type' => 'checkbox',
10
      '#title' => t('Add subdirectories'),
11
      '#default_value' => isset($form_state['profile']['directories'][$key]['mkdir']) ? $form_state['profile']['directories'][$key]['mkdir'] : 0,
12
    );
13
    $form['profile']['directories'][$key]['rmdir'] = array(
14
      '#type' => 'checkbox',
15
      '#title' => t('Remove subdirectories'),
16
      '#default_value' => isset($form_state['profile']['directories'][$key]['rmdir']) ? $form_state['profile']['directories'][$key]['rmdir'] : 0,
17
    );
18
  }
19
  $form['profile']['mkdirnum'] = array(
20
    '#type' => 'textfield',
21
    '#title' => t('Maximum number of subdirectories'),
22
    '#default_value' => isset($form_state['profile']['mkdirnum']) ? $form_state['profile']['mkdirnum'] : 2,
23
    '#description' => t('This setting is applicable only if you allow subdirectory creation under any of the predefined directories. Define here the maximum number of subdirectories a directory can have. Setting this to 0 removes the limit and also allows infinite subdirectory depth.'),
24
  );
25
}
26

    
27
/**
28
 * Mkdir form.
29
 */
30
function _imce_mkdir_form($form, &$form_state, &$imce) {
31
  $mkdir['html1']['#markup'] = '<div class="container-inline">';
32
  $mkdir['dirname'] = array(
33
    '#type' => 'textfield',
34
    '#title' => t('Subdirectory name'),
35
    '#size' => 12,
36
    '#maxlength' => 255,
37
  );
38
  if (imce_perm_exists($imce, 'mkdir')) {
39
    $mkdir['mkdir'] = array(
40
      '#type' => 'submit',
41
      '#value' => t('Add'),
42
      '#submit' => $imce['perm']['mkdir'] ? array('imce_mkdir_submit') : NULL,
43
    );
44
  }
45
  if (imce_perm_exists($imce, 'rmdir')) {
46
    $mkdir['rmdir'] = array(
47
      '#type' => 'submit',
48
      '#value' => t('Remove'),
49
      '#submit' => $imce['perm']['rmdir'] ? array('imce_mkdir_rmdir_submit') : NULL,
50
    );
51
  }
52
  $mkdir['html2']['#markup'] = '</div>';
53
  $form['fset_mkdir'] = array(
54
    '#type' => 'fieldset',
55
    '#title' => t('Directory'),
56
  ) + $mkdir;
57
  $form['#action'] = $imce['url'];
58
  return $form;
59
}
60

    
61
/**
62
 * Submits mkdir form.
63
 */
64
function imce_mkdir_submit($form, &$form_state) {
65
  $form_state['redirect'] = FALSE;
66
  $imce = &$form_state['build_info']['args'][0]['imce'];
67
  imce_mkdir_batch($imce, array(rawurldecode($form_state['values']['dirname'])));
68
}
69

    
70
/**
71
 * Batch adds directories.
72
 */
73
function imce_mkdir_batch(&$imce, $dirnames = array()) {
74
  if (!isset($imce['diradded'])) {
75
    $imce['diradded'] = array();
76
  }
77
  $parent = imce_dir_uri($imce);
78

    
79
  foreach ($dirnames as $dirname) {
80
    if (!preg_match('/^[A-Za-z0-9_\-]+$/', $dirname)) {
81
      drupal_set_message(t('%dirname is not a valid directory name. It should contain only alphanumeric characters, hyphen and underscore.', array('%dirname' => $dirname)), 'error');
82
      continue;
83
    }
84

    
85
    $dirpath = $parent . $dirname;
86

    
87
    if (file_exists($dirpath)) {
88
      drupal_set_message(t('Subdirectory %dir already exists.', array('%dir' => $dirname)), 'error');
89
      continue;
90
    }
91

    
92
    if (!file_prepare_directory($dirpath, FILE_CREATE_DIRECTORY)) {
93
      drupal_set_message(t('Subdirectory %dir could not be created.', array('%dir' => $dirname)), 'error');
94
      continue;
95
    }
96

    
97
    drupal_set_message(t('Subdirectory %dir has been added.', array('%dir' => $dirname)));
98
    $imce['diradded'][] = $imce['subdirectories'][] = $dirname;
99
  }
100
}
101

    
102
/**
103
 * Submits rmdir form.
104
 */
105
function imce_mkdir_rmdir_submit($form, &$form_state) {
106
  $form_state['redirect'] = FALSE;
107
  $imce = &$form_state['build_info']['args'][0]['imce'];
108
  imce_mkdir_rmdir_batch($imce, array(rawurldecode($form_state['values']['dirname'])));
109
}
110

    
111
/**
112
 * Batch removes directories.
113
 */
114
function imce_mkdir_rmdir_batch(&$imce, $dirnames = array()) {
115
  if (!isset($imce['dirremoved'])) {
116
    $imce['dirremoved'] = array();
117
  }
118
  $parent = imce_dir_uri($imce);
119
  $prefix = ($imce['dir'] == '.' ? '' : $imce['dir'] . '/');
120

    
121
  foreach ($dirnames as $dirname) {
122
    $index = array_search($dirname, $imce['subdirectories']);
123

    
124
    if ($index === FALSE) {
125
      drupal_set_message(t('Subdirectory %dir does not exist.', array('%dir' => $dirname)), 'error');
126
      continue;
127
    }
128

    
129
    if (isset($imce['directories'][$prefix . $dirname])) {
130
      drupal_set_message(t('Subdirectory %dir is a predefined directory and can not be removed.', array('%dir' => $dirname)), 'error');
131
      continue;
132
    }
133

    
134
    $dirpath = $parent . $dirname;
135
    if (!imce_mkdir_rmdir_recursive($dirpath)) {
136
      drupal_set_message(t('Subdirectory %dir could not be removed.', array('%dir' => $dirname)), 'error');
137
      continue;
138
    }
139

    
140
    drupal_set_message(t('Subdirectory %dir has been removed.', array('%dir' => $dirname)));
141
    $imce['dirremoved'] = array_merge($imce['dirremoved'], array_splice($imce['subdirectories'], $index, 1));
142
  }
143
}
144

    
145
/**
146
 * Recursive directory deletion
147
 */
148
function imce_mkdir_rmdir_recursive($path) {
149
  static $dirlen;
150
  if (!isset($dirlen)) {
151
    $dirlen = strlen(file_uri_scheme($path)) + 3;
152
  }
153
  if (is_dir($path) && !is_link($path)) {
154
    if ($handle = @opendir($path)) {
155
      while (($file = readdir($handle)) !== FALSE) {
156
        if ($file == '.' || $file == '..') {
157
          continue;
158
        }
159
        $filepath = $path . '/' . $file;
160
        if (!imce_mkdir_rmdir_recursive($filepath)) {
161
          drupal_set_message(t('%path could not be removed.', array('%path' => substr($filepath, $dirlen))), 'error');
162
          break;
163
        }
164
      }
165
      closedir($handle);
166
    }
167
    return @rmdir($path);
168
  }
169
  return imce_delete_filepath($path);
170
}