background_audio_block.module 9.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php
/**
 * @file
 * Main file for the Background Audio Block module.
 */

/**
 * Implements hook_menu().
 */
function background_audio_block_menu() {
  $items = array();
  $items['admin/structure/block/manage/background_audio/background_audio/badelete/%file'] = array(
    'title' => 'Delete file from playlist',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('background_audio_block_file_badelete', 7),
    'access arguments' => array('administer blocks'),
17
    'file' => 'background_audio_block.admin.inc',
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/**
 * Implements hook_block_info().
 */
function background_audio_block_block_info() {
  $blocks['background_audio'] = array(
    'info' => t('Background audio'),
    'cache' => DRUPAL_CACHE_GLOBAL,
  );

  return $blocks;
}

/**
 * Implements hook_block_configure().
 */
function background_audio_block_block_configure($delta = '') {
  if ($delta == 'background_audio') {
    $form = array();

    $form['vtabs'] = array(
      '#type' => 'vertical_tabs',
    );

    $form['vtabs']['files'] = array(
      '#type' => 'fieldset',
49
      '#title' => t('Manual playlist'),
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
      '#theme' => 'background_audio_block_files_table',
      '#tree' => TRUE,
    );
    $files = variable_get('background_audio_files', array());
    $enabled_files = array_filter($files);
    if (!empty($files)) {
      $weight = -100;
      foreach ($files as $fid => $value) {
        $file = file_load($fid);
        $form['vtabs']['files'][$fid][$fid . '_enabled'] = array(
          '#type' => 'checkbox',
          '#title' => $file->filename,
          '#default_value' => array_key_exists($fid, $enabled_files),
        );
        $form['vtabs']['files'][$fid][$fid . '_uri'] = array(
          '#markup' => $file->uri,
        );
        $form['vtabs']['files'][$fid][$fid . '_weight'] = array(
          '#type' => 'weight',
          '#title' => t('Weight'),
          '#title_display' => 'invisible',
          '#delta' => 100,
          '#default_value' => $weight,
          '#attributes' => array('class' => array('background-audio-file-weight')),
        );
        $weight++;
        $form['vtabs']['files'][$fid][$fid . '_delete'] = array(
          '#markup' => l(t('Delete'), 'admin/structure/block/manage/background_audio/background_audio/badelete/' . $fid),
        );
      }
    }
    $form['vtabs']['files']['background_audio_file'] = array(
      '#type' => 'managed_file',
      '#title' => t('Add file to playlist'),
      '#description' => t('Only *.mp3 files are allowed.'),
      '#upload_location' => file_default_scheme() . '://background_audio/',
      '#upload_validators' => array(
        'file_validate_extensions' => array('mp3'),
      ),
    );
    if (module_exists('plupload')) {
      $form['vtabs']['files']['background_audio_file']['#type'] = 'plupload';
      unset($form['vtabs']['files']['background_audio_file']['#description']);
    }

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
    if (module_exists('views')) {
      $form['vtabs']['views'] = array(
        '#type' => 'fieldset',
        '#title' => t('View reference'),
        '#description' => t('View reference takes precedence over the manual playlist.'),
        '#tree' => TRUE,
      );

      $options = array('' => t('None')) + views_get_views_as_options(FALSE, 'all', NULL, TRUE);
      $form['vtabs']['views']['view'] = array(
        '#type' => 'select',
        '#title' => t('Select a view'),
        '#options' => $options,
        '#default_value' => variable_get('background_audio_block_view', ''),
      );
    }
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

    return $form;
  }
}

/**
 * Implements hook_block_save().
 */
function background_audio_block_block_save($delta = '', $edit = array()) {
  global $user;
  if ($delta == 'background_audio') {
    $files = variable_get('background_audio_files', array());

    // Process playlist files reorder and enable/disable flag.
    if (!empty($files)) {
      $file_weights = array();
      foreach ($files as $fid => $value) {
        $file_weights[$fid] = $edit['files'][$fid][$fid . '_weight'];
      }
      asort($file_weights);
      $files = array();
      foreach ($file_weights as $fid => $weight) {
        $files[$fid] = $edit['files'][$fid][$fid . '_enabled'];
      }
    }

    // Add new file/files to playlist.
    if (module_exists('plupload') && !empty($edit['files']['background_audio_file'])) {
      $directory = file_default_scheme() . '://background_audio/';
      if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
141
        watchdog('background_audio_block', 'Directory %directory could not be created.', array('%directory' => $directory), WATCHDOG_WARNING);
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
      }
      else {
        foreach ($edit['files']['background_audio_file'] as $tmp_file) {
          if ($tmp_file['status'] == 'done') {
            $uri = file_unmanaged_move($tmp_file['tmppath'], $directory . $tmp_file['name']);
            $file = new StdClass();
            $file->uid = $user->uid;
            $file->filename = basename($uri);
            $file->uri = $uri;
            $file->filemime = file_get_mimetype($uri);
            $file->status = FILE_STATUS_PERMANENT;
            file_save($file);
            file_usage_add($file, 'background_audio', 'block', 0);
            $files[$file->fid] = $file->fid;
          }
        }
      }
    }
    else {
      $new_fid = $edit['files']['background_audio_file'];
      if ($new_fid) {
        $file = file_load($new_fid);
        $file->status = FILE_STATUS_PERMANENT;
        file_save($file);
        file_usage_add($file, 'background_audio', 'block', 0);
        $files[$file->fid] = $file->fid;
      }
    }

    // Save modified playlist files.
    variable_set('background_audio_files', $files);

174 175 176 177
    // Save the chosen view.
    if (module_exists('views')) {
      variable_set('background_audio_block_view', $edit['views']['view']);
    }
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
  }
}

/**
 * Implements hook_block_view().
 */
/*function background_audio_block_block_view($delta = '') {
  if ($delta == 'background_audio') {
    $block = array();

    return $block;
  }
}*/

/**
 * Implements hook_theme().
 */
function background_audio_block_theme($existing, $type, $theme, $path) {
  return array(
    'background_audio_block_files_table' => array(
      'render element' => 'form',
      'file' => 'background_audio_block.theme.inc',
    ),
  );
}

/**
205
 * Implements hook_background_audio_items().
206 207 208 209
 */
function background_audio_block_background_audio_items() {
  // If background_audio block is present on the page - then return the required information.
  if (_background_audio_block_info()) {
210
    $enabled_files = array();
211
    if (module_exists('views') && $view = variable_get('background_audio_block_view', '')) {
212 213
      list($name, $display_id) = explode(':', $view);
      if ($result = views_get_view_result($name, $display_id)) {
214 215 216 217 218 219 220 221 222 223 224 225 226
        // A kind of ugly way to compose 'file_managed_field_data_field_file_fid'.
        $view = views_get_view($name);
        $display = $view->display[$display_id];
        $fields = $display->display_options['fields'];
        $field_names = array();
        foreach ($fields as $field) {
          if ($field['table'] == 'file_managed' && $field['field'] == 'fid') {
            if ($field['relationship'] != 'none') {
              $field_names[] = 'file_managed_field_data_' . $field['relationship'];
            }
            else {
              $field_names[] = 'fid';
            }
227
          }
228 229 230 231 232 233 234
        }

        foreach ($result as $item) {
          foreach ($field_names as $field_name) {
            if (isset($item->$field_name)) {
              $enabled_files[$item->$field_name] = $item->$field_name;
            }
235
          }
236 237 238 239 240 241 242 243
        }
      }
    }

    if (empty($enabled_files)) {
      $enabled_files = array_filter(variable_get('background_audio_files', array()));
    }

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
    if (!empty($enabled_files)) {
      $files = array();
      foreach ($enabled_files as $fid => $value) {
        $file = file_load($fid);
        $files[] = array(
          'src' => file_create_url($file->uri),
          'title' => drupal_substr($file->filename, 0, -4),
          'type' => 'audio/mpeg',
        );
      }
      return $files;
    }
  }

  return NULL;
}

/**
262
 * Implements hook_background_audio_settings_alter().
263
 */
264
function background_audio_block_background_audio_settings_alter(&$settings) {
265
  if ($block = _background_audio_block_info()) {
266
    $settings['region'] = $block->region;
267 268 269 270 271 272 273 274 275 276 277 278 279
  }
}

/**
 * Helper function to determine if block is shown on the page.
 */
function _background_audio_block_info() {
  static $block;
  if (isset($block)) {
    return $block;
  }

  global $theme_key;
duozersk's avatar
duozersk committed
280
  $debug = variable_get('background_audio_debug', 0);
281
  // block_load() function doesn't do what we need.
282 283
  //$block = block_load('background_audio', 'background_audio');
  $block = db_query('SELECT * FROM {block} WHERE module = :module AND delta = :delta AND theme = :theme AND status = 1',
284
    array(':module' => 'background_audio_block', ':delta' => 'background_audio', ':theme' => $theme_key))->fetchObject();
285
  if (!$block) {
duozersk's avatar
duozersk committed
286
    if ($debug) {
287
      watchdog('background_audio_block', 'Background Audio Block is not enabled in theme !theme.', array('!theme' => $theme_key), WATCHDOG_DEBUG);
duozersk's avatar
duozersk committed
288
    }
289 290
    return $block;
  }
duozersk's avatar
duozersk committed
291
  if ($debug) {
292
    watchdog('background_audio_block', 'Background Audio Block is enabled in theme !theme, block !block.', array('!theme' => $theme_key, '!block' => print_r($block, 1)), WATCHDOG_DEBUG);
duozersk's avatar
duozersk committed
293
  }
294 295 296 297 298 299 300 301

  $block_info = array(
    $block->bid => $block,
  );
  // Allow modules to modify the block list (visibility settings, etc.).
  drupal_alter('block_list', $block_info);
  if (isset($block_info[$block->bid])) {
    $block = $block_info[$block->bid];
duozersk's avatar
duozersk committed
302
    if ($debug) {
303
      watchdog('background_audio_block', 'Background Audio Block was not removed by hook_block_list_alter().', array(), WATCHDOG_DEBUG);
duozersk's avatar
duozersk committed
304
    }
305 306 307
  }
  else {
    $block = FALSE;
duozersk's avatar
duozersk committed
308
    if ($debug) {
309
      watchdog('background_audio_block', 'Background Audio Block was removed by hook_block_list_alter().', array(), WATCHDOG_DEBUG);
duozersk's avatar
duozersk committed
310
    }
311 312 313 314
  }

  return $block;
}