You are here

function rate_ui_widget_save_buttons in Rate 7.2

Save widget buttons from storage array into database.

Parameters

int $wid:

array $storage:

1 call to rate_ui_widget_save_buttons()
rate_ui_widget_form_submit in ui/rate_ui.form.inc
Implements hook_form_submit().

File

ui/rate_ui.form.inc, line 741
This file contains the differtent Rate UI forms.

Code

function rate_ui_widget_save_buttons($wid, $storage) {
  $images = $storage['images'];
  $sprites = $storage['sprites'];
  $separate = $storage['separate_images'];
  $imagesource = $storage['imagesource'];

  // Delete old configuration.
  db_delete('rate_widget_button')
    ->condition('wid', $wid)
    ->execute();

  // Start with the revoke button?
  $start = empty($storage['button0_label']) ? 1 : 0;
  $end = $storage['buttoncount'];
  for ($i = $start; $i <= $end; ++$i) {
    if (empty($storage["button{$i}_label"])) {
      continue;
    }
    $button = new stdClass();
    $button->wid = $wid;
    $button->num = $i;
    $button->label = $storage["button{$i}_label"];
    $button->value = $storage["button{$i}_value"];
    $button->description = $storage["button{$i}_description"];
    foreach (array(
      'default',
      'highlighted',
      'default_voted',
      'highlighted_voted',
      'disabled',
      'disabled_voted',
    ) as $kind) {

      // If we do not have separate buttons, the fields for all buttons have
      // "button1" in the name, except for the revoke button.
      $num = $separate && $i > 1 ? $i : 1;
      if ($imagesource == 'spritegenerator') {
        $button->{"img_{$kind}"} = '';
      }
      elseif ($images && $sprites) {
        $x = $storage["button{$num}_{$kind}_x"];
        $y = $storage["button{$num}_{$kind}_y"];
        $width = $storage["button{$num}_{$kind}_width"];
        $height = $storage["button{$num}_{$kind}_height"];
        if ($imagesource == 'upload') {
          $source = $storage['sprite_file'];
          db_update('file_managed')
            ->fields(array(
            'status' => FILE_STATUS_PERMANENT,
          ))
            ->condition('fid', $source)
            ->execute();
          $file = new stdClass();
          $file->fid = $source;
          file_usage_add($file, 'rate', 'rate_widget', $wid);
        }
        else {
          $source = $storage['sprite_filepath'];
        }
        $button->{"img_{$kind}"} = "{$source}@{$x},{$y}:{$width},{$height}";
      }
      elseif ($images) {
        if ($imagesource == 'upload') {
          $source = $storage["button{$num}_{$kind}_file"];
          db_update('file_managed')
            ->fields(array(
            'status' => FILE_STATUS_PERMANENT,
          ))
            ->condition('fid', $source)
            ->execute();
          $file = new stdClass();
          $file->fid = $source;
          file_usage_add($file, 'rate', 'rate_widget', $wid);
        }
        else {
          $source = $storage["button{$num}_{$kind}_filepath"];
        }
        $button->{"img_{$kind}"} = $source;
      }
    }
    drupal_write_record('rate_widget_button', $button);
  }
}