You are here

function featured_content_block_save in Featured Content 7.2

Same name and namespace in other branches
  1. 7 featured_content.module \featured_content_block_save()

Implements hook_block_save().

1 call to featured_content_block_save()
featured_content_add_block_form_submit in ./featured_content.admin.inc
Save new block.

File

./featured_content.module, line 1266
Featured Content module for created related & featured content blocks.

Code

function featured_content_block_save($delta, $edit) {
  $featured_blocks = variable_get('featured_content_blocks', array());
  $vocabularies = taxonomy_get_vocabularies();

  // General settings.
  if (!isset($featured_blocks[$delta])) {
    $featured_blocks[$delta] = array();
  }
  if (!isset($featured_blocks[$delta]['more']) || !is_array($featured_blocks[$delta]['more'])) {
    $featured_blocks[$delta]['more'] = array();
  }
  if (!isset($featured_blocks[$delta]['rss']) || !is_array($featured_blocks[$delta]['rss'])) {
    $featured_blocks[$delta]['rss'] = array();
  }
  $featured_blocks[$delta]['name'] = $edit['featured_content_block_name'];
  $featured_blocks[$delta]['header'] = $edit['featured_content_block_header'];
  $featured_blocks[$delta]['footer'] = $edit['featured_content_block_footer'];
  $featured_blocks[$delta]['empty'] = $edit['featured_content_block_empty'];
  $featured_blocks[$delta]['type'] = $edit['featured_content_block_type'];
  $featured_blocks[$delta]['sort'] = $edit['featured_content_block_sort'];
  $featured_blocks[$delta]['display'] = $edit['featured_content_block_display'];
  $featured_blocks[$delta]['style'] = $edit['featured_content_block_style'];
  $featured_blocks[$delta]['more']['text'] = $edit['featured_content_block_more_text'];
  $featured_blocks[$delta]['more']['num'] = $edit['featured_content_block_more_num'];
  $featured_blocks[$delta]['more']['display'] = $edit['featured_content_block_more_display'];
  $featured_blocks[$delta]['more']['style'] = $edit['featured_content_block_more_style'];
  $featured_blocks[$delta]['more']['title'] = $edit['featured_content_block_more_title'];
  $featured_blocks[$delta]['more']['header'] = $edit['featured_content_block_more_header'];
  $featured_blocks[$delta]['more']['footer'] = $edit['featured_content_block_more_footer'];
  $featured_blocks[$delta]['more']['url'] = $edit['featured_content_block_more_url'];
  $featured_blocks[$delta]['rss']['display'] = $edit['featured_content_block_rss_display'];
  $featured_blocks[$delta]['rss']['title'] = $edit['featured_content_block_rss_title'];

  // If sort is set to popularity, print warning.
  if ($edit['featured_content_block_sort'] == 'popular_asc' || $edit['featured_content_block_sort'] == 'popular_desc') {
    if (featured_content_node_statistics_enabled()) {
      drupal_set_message(t('You have chosen to sort by popularity. Since the
          Statistics module is turned on and Count Content Views is enabled,
          this will work as expected. If you disable either of these in the
          future, the content items will no longer be sorted by popularity.'));
    }
    else {
      if (!module_exists('statistics')) {
        drupal_set_message(t('You have chosen to sort by popularity, but the
            Statistics module is currently turned off. Turn on the Statistics
            module (!admin_modules_page) and enable Count Content Views
            (!statistics_settings_page) so the content items can be sorted
            by popularity.', array(
          '!admin_modules_page' => l(t('admin/modules'), 'admin/modules'),
          '!statistics_settings_page' => l(t('admin/config/system/statistics'), 'admin/config/system/statistics'),
        )));
      }
      elseif (!variable_get('statistics_count_content_views', 0)) {
        drupal_set_message(t('You have chosen to sort by popularity. Although
            the Statistics module is currently turned on, Count Content Views
            is currently disabled. Enable Count Content Views
            (!statistics_settings_page) so the content items can be sorted
            by popularity.', array(
          '!statistics_settings_page' => l(t('admin/config/system/statistics'), 'admin/config/system/statistics'),
        )));
      }
    }
  }
  $num_show = $edit['featured_content_block_num_show'];
  if (!is_numeric($num_show) || $num_show <= 0 || $num_show > variable_get('featured_content_max_node_show', 20)) {
    $num_show = variable_get('featured_content_max_node_show', 20);
    if (!is_numeric($num_show) || $num_show <= 0) {
      $num_show = 20;
    }
  }
  $featured_blocks[$delta]['num_show'] = $num_show;

  // Manual settings.
  $nids = array();
  $featured_blocks[$delta]['manual']['paths'] = $edit['manual_paths'];
  $tmp = preg_replace('/(\\r\\n?|\\n)/', '|||', $edit['manual_paths']);
  $paths = explode('|||', $tmp);
  foreach ($paths as $path) {
    $path = trim($path);
    if (!empty($path)) {
      if (!valid_url($path, TRUE)) {

        // Check for external URLs.
        // Assume it's an internal URL.
        $path = drupal_get_normal_path($path);
      }
      $matches = array();
      if (preg_match("/^node\\/(.*)/", $path, $matches)) {
        $nids[] = $matches[1];
      }
      else {
        if (preg_match("/\\*/", $path, $matches)) {
          $path_sql = str_replace('*', '%', $path);
          $path_sql = str_replace('/', '\\/', $path_sql);
          $query = db_select('url_alias', 'ua');
          $query
            ->fields('ua', array(
            'source',
          ));
          $query
            ->condition('alias', $path_sql, 'LIKE');
          $results = $query
            ->execute();
          foreach ($results as $row) {
            if (preg_match("/^node\\/(.*)/", $row->source, $matches)) {
              $nids[] = $matches[1];
            }
          }
        }
      }
    }
  }
  $featured_blocks[$delta]['manual']['nids'] = $nids;

  // CCK/Field settings.
  $cck_fields = field_info_fields();
  if (!empty($cck_fields)) {
    foreach ($cck_fields as $field_name => $field_data) {
      $featured_blocks[$delta]['cck']['fields'][$field_name] = isset($edit['cck_fields_' . $field_name]) ? $edit['cck_fields_' . $field_name] : NULL;
    }
  }

  // Filter settings.
  $featured_blocks[$delta]['filter']['include_node'] = $edit['filter_include_node'];
  $featured_blocks[$delta]['filter']['paths']['match'][0] = $edit['filter_paths_match_0'];
  $featured_blocks[$delta]['filter']['paths']['match'][1] = $edit['filter_paths_match_1'];
  $featured_blocks[$delta]['filter']['paths']['match'][2] = $edit['filter_paths_match_2'];
  $featured_blocks[$delta]['filter']['paths']['match'][3] = $edit['filter_paths_match_3'];
  $languages = featured_content_get_languages();
  foreach ($languages as $language_code => $language_label) {
    $featured_blocks[$delta]['filter']['languages'][$language_code] = $edit['filter_languages_' . $language_code];
  }
  $content_types = featured_content_get_content_types();
  foreach ($content_types as $content_type_name => $content_type_label) {
    $featured_blocks[$delta]['filter']['content_types']['type'][$content_type_name] = $edit['filter_content_types_type_' . $content_type_name];
  }
  $users = featured_content_get_users();
  foreach ($users as $user_uid => $user_label) {
    $featured_blocks[$delta]['filter']['users']['user'][$user_uid] = $edit['filter_users_user_' . $user_uid];
  }
  foreach ($vocabularies as $vocabulary) {
    $vid = $vocabulary->vid;
    $terms = featured_content_get_vocabulary_terms($vocabulary);
    foreach ($terms as $term_tid => $term_name) {
      $featured_blocks[$delta]['filter']['vocab'][$vid]['term'][$term_tid] = isset($edit['filter_vocab_term_' . $term_tid]) ? $edit['filter_vocab_term_' . $term_tid] : NULL;
    }
  }
  $featured_blocks[$delta]['filter']['primary_term']['node'] = isset($edit['filter_primary_term_node']) ? $edit['filter_primary_term_node'] : NULL;
  if (module_exists('views') && function_exists('views_get_all_views')) {
    $views = views_get_all_views();
    if (!empty($views)) {
      foreach ($views as $view_name => $view_data) {
        if (!empty($view_data->display)) {
          foreach ($view_data->display as $display_name => $display_data) {
            $featured_blocks[$delta]['filter']['views']['view'][$view_name][$display_name] = $edit['filter_views_view_' . $view_name . '_' . $display_name];
          }
        }
      }
    }
  }
  if (module_exists('search')) {
    $featured_blocks[$delta]['filter']['keyword'] = array();
    $featured_blocks[$delta]['filter']['keyword']['filter_by_keyword'] = $edit['filter_by_keyword'];
    $featured_blocks[$delta]['filter']['keyword']['num_words_in_title_keyword'] = $edit['num_words_in_title_keyword'];
    $featured_blocks[$delta]['search']['include_node'] = $edit['search_include_node'];
    $featured_blocks[$delta]['search']['num_words_in_title'] = $edit['num_words_in_title'];
    $featured_blocks[$delta]['search']['search_restrict_type'] = $edit['search_restrict_type'];
  }

  // Visibility settings.
  $featured_blocks[$delta]['visibility']['content_types']['selected'] = $edit['visibility_content_types_selected'];
  $featured_blocks[$delta]['visibility']['users']['selected'] = $edit['visibility_users_selected'];
  foreach ($vocabularies as $vocabulary) {
    $vid = $vocabulary->vid;
    $featured_blocks[$delta]['visibility']['vocab'][$vid]['selected'] = isset($edit['visibility_vocab_' . $vid . '_selected']) ? $edit['visibility_vocab_' . $vid . '_selected'] : NULL;
  }
  variable_set('featured_content_blocks', $featured_blocks);
}