You are here

function skinr_context_ui_form_submit in Skinr 8.2

Same name and namespace in other branches
  1. 7.2 skinr_context/skinr_context_ui.module \skinr_context_ui_form_submit()

Form submission handler for skinr_context_form_alter().

1 string reference to 'skinr_context_ui_form_submit'
skinr_context_ui_form_skinr_ui_form_alter in skinr_context/skinr_context_ui.module
Implements hook_form_alter().

File

skinr_context/skinr_context_ui.module, line 323
Administrative interface for Skinr Context. Without this module, you cannot edit your skins with context.

Code

function skinr_context_ui_form_submit($form, &$form_state) {
  $current_theme = skinr_current_theme(TRUE);
  $module = $form_state['values']['module'];
  $element = $form_state['values']['element'];
  $gid = $form_state['values']['gid'];

  // Save group settings.
  // Load an uncached version of the skin settings group object.
  $group = skinr_context_group_load_unchanged($gid);

  // Let's save some time in skinr_context_group_save() by setting $group->original here.
  $group->original = clone $group;

  // Update title and description.
  $group->title = $form_state['values']['skinr_group']['title'];
  $group->description = $form_state['values']['skinr_group']['description'];

  // Update context.
  if (!empty($form['conditions'])) {
    $enabled = explode(',', $form_state['values']['conditions']['state']);
    foreach ($form_state['values']['conditions']['plugins'] as $condition => $values) {
      if (in_array($condition, $enabled, TRUE) && ($plugin = context_get_plugin('condition', $condition))) {
        if (isset($values['values'])) {
          $group->conditions[$condition]['values'] = $plugin
            ->condition_form_submit($values['values']);
        }
        if (isset($values['options'])) {
          $group->conditions[$condition]['options'] = $plugin
            ->options_form_submit($values['options']);
        }
        if (context_empty($group->conditions[$condition]['values'])) {
          unset($group->conditions[$condition]);
        }
      }
      else {
        unset($group->conditions[$condition]);
      }
    }
  }
  $group->condition_mode = $form_state['values']['condition_mode'];

  // Save group.
  skinr_context_group_save($group);

  // Save skin settings.
  if (!empty($form_state['values']['skinr_settings'])) {
    foreach ($form_state['values']['skinr_settings'] as $theme_name => $theme) {

      // Process widgets.
      if (!empty($theme) && is_array($theme)) {
        foreach ($theme as $skin_name => $options) {
          if ($skin_name == '_additional' && !user_access('edit advanced skin settings')) {

            // This user doesn't have access to alter these options.
            continue;
          }

          // Ensure options is an array.
          if (!is_array($options)) {
            $options = $skin_name == '_additional' ? explode(' ', $options) : array(
              $options,
            );
          }

          // Sanitize options.
          $options = _skinr_array_strip_empty($options);

          // Find existing skin.
          $params = array(
            'theme' => $theme_name,
            'module' => $module,
            'element' => $element,
            'skin' => $skin_name,
            'gid' => $gid,
          );
          $sids = skinr_context_group_get_sids($params);
          unset($skin);
          if (!empty($sids)) {
            $sid = reset($sids);
            $skin = entity_load('skin', $sid);
          }
          if (empty($options)) {
            if (!empty($skin)) {

              // Delete this skin setting.
              $skin
                ->delete();
            }
            continue;
          }
          if (empty($skin)) {

            // It doesn't exist, so create a new skin.
            $skin = new stdClass();
            $skin->theme = $theme_name;
            $skin->module = $module;
            $skin->element = $element;
            $skin->skin = $skin_name;
            $skin->gid = $gid;
          }
          $skin->options = $options;
          $skin->status = 1;

          // Save skin.
          if (!skinr_skin_save($skin)) {
            drupal_set_message(t("Skinr settings for %skin weren't saved due to an error.", array(
              '%skin' => $skin_name,
            )), 'error');
          }
        }
      }
    }
  }
}