You are here

function skinr_ui_admin_library_form_submit in Skinr 7.2

Form submission handler for skinr_ui_admin_library_form().

File

./skinr_ui.admin.inc, line 799
Admin page callbacks for the Skinr UI module.

Code

function skinr_ui_admin_library_form_submit($form, &$form_state) {
  $skins = skinr_get_skin_info();
  $theme = $form_state['values']['edited_theme'];
  $theme_info = system_get_info('theme', $theme);
  $reset = $form_state['clicked_button']['#id'] == 'edit-reset' ? TRUE : FALSE;
  if ($reset) {

    // Reset all values to their default.
    foreach ($form_state['values']['skins'] as $category => $data) {
      foreach ($data as $skin => $enabled) {
        $default_status = isset($skins[$skin]['status'][$theme]) ? $skins[$skin]['status'][$theme] : $skins[$skin]['default status'];
        $form_state['values']['skins'][$category][$skin]['enable'] = $default_status;
      }
    }
  }
  if ($form_state['clicked_button']['#id'] == 'edit-submit' || $reset) {

    // Make sure we don't disable skins for which configuration exists. Ask to
    // disable all related skin configurations so we can disable the skin.
    $affected_skins = array();
    $disable_sids = array();
    $rebuild = FALSE;
    foreach ($form_state['values']['skins'] as $category => $data) {
      foreach ($data as $skin => $enabled) {
        $enabled = $enabled['enable'];
        $status = skinr_skin_info_status_get($skins[$skin]);
        if (!empty($status[$theme]) && !$enabled) {

          // This skin is being disabled.
          $affected_skins[] = $skin;

          // Find all enabled configurations for this skin.
          $params = array(
            'theme' => $theme,
            'skin' => $skin,
            'status' => 1,
          );
          $sids = skinr_skin_get_sids($params);
          if (count($sids)) {
            $disable_sids += $sids;
            $rebuild = TRUE;
          }
        }
      }
    }
    if ($rebuild) {
      $form_state['storage']['status'] = $form_state['values']['skins'];
      $form_state['storage']['skins'] = $affected_skins;
      $form_state['storage']['sids'] = $disable_sids;
      $form_state['storage']['reset'] = $reset;
      $form_state['rebuild'] = TRUE;
      return;
    }
  }
  if (!empty($form_state['storage']['sids'])) {

    // Disable any configurations for skins that are being disabled.
    db_update('skinr_skins')
      ->fields(array(
      'status' => 0,
    ))
      ->condition('sid', $form_state['storage']['sids'])
      ->execute();

    // Clear skinr_skin_load_multiple cache.
    drupal_static_reset('skinr_skin_load_multiple');
    foreach ($form_state['storage']['skins'] as $skin) {
      drupal_set_message(t('Disabled all skin configurations for skin %skin and theme %theme.', array(
        '%skin' => $skin,
        '%theme' => $theme_info['name'],
      )));
    }
    $changed_status = $form_state['storage']['status'];
    $reset = $form_state['storage']['reset'];
  }
  else {
    $changed_status = $form_state['values']['skins'];
  }

  // Save new status.
  foreach ($changed_status as $category => $data) {
    foreach ($data as $skin => $enabled) {
      $enabled = $enabled['enable'];
      $status = skinr_skin_info_status_get($skins[$skin]);
      if (!isset($status[$theme]) || $status[$theme] != $enabled) {

        // Update status.
        $status[$theme] = $enabled;
        skinr_skin_info_status_set($skins[$skin], $status);
      }
    }
  }
  if ($reset) {
    drupal_set_message(t("Statuses for %theme's skins have been reset to their defaults.", array(
      '%theme' => $theme_info['name'],
    )));
  }
  else {
    drupal_set_message(t("Statuses for %theme's skins have been updated.", array(
      '%theme' => $theme_info['name'],
    )));
  }
  $form_state['redirect'] = 'admin/structure/skinr/library';
}