sharer_toolbar.forms.inc 3.92 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
<?php

/**
 * @file sharer_toolbar.forms.inc
 * Forms configuration
 */

/**
 * Sharer toolbar configuration form.
 */
function sharer_toolbar_config_form($form, &$form_state) {
  module_load_include('inc', 'sharer_toolbar');

14
15
16
17
18
19
20
21
22
23
  // Sharer toolbar title
  $form['sharer_toolbar_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Sharer toolbar title'),
    '#description' => t('Set this value in english, and translate it using !link.', array('!link' => l(t('translatable strings'), 'admin/config/regional/translate/translate'))),
    '#default_value' => variable_get('sharer_toolbar_title', 'Share'),
  );

  // Make sharers sortable
  $form['sharers']['#tree'] = TRUE;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

  // List all available sharers
  $sharers = _sharer_toolbar_get_ordered_available_sharers();
  $sharers_configuration = variable_get('sharer_toolbar_sharers_configuration', array());
  foreach ($sharers as $delta => $sharer) {
    $sid = $sharer['sid'];
    $sharer_conf = (isset($sharers_configuration[$sid])) ? $sharers_configuration[$sid] : array();

    // Checkbox to activate or not sharer.
    $form['sharers'][$sid]['status'] = array(
      '#type' => 'checkbox',
      '#default_value' => (isset($sharer_conf['status'])) ? $sharer_conf['status'] : 0,
    );

    // Textfield to hold sharer name.
    $form['sharers'][$sid]['name'] = array(
      '#type' => 'textfield',
      '#default_value' => (isset($sharer_conf['name'])) ? $sharer_conf['name'] : $sharer['name'],
    );

    // Textfield to hold sharer icon class.
45
    $icon_value = (isset($sharer_conf['icon_class'])) ? $sharer_conf['icon_class'] : $sharer['icon_class'];
46
47
    $form['sharers'][$sid]['icon_class'] = array(
      '#type' => 'textfield',
48
49
      '#default_value' => $icon_value,
      '#field_prefix' => '<i class="' . $icon_value . '"></i>',
50
51
    );

52
53
54
55
56
57
    // Textfield to hold sharer label.
    $form['sharers'][$sid]['label'] = array(
      '#type' => 'textfield',
      '#default_value' => (isset($sharer_conf['label'])) ? $sharer_conf['label'] : $sharer['label'],
    );

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
    // Invisible field, but contains sort info (weights).
    $form['sharers'][$sid]['weight'] = array(
      '#type' => 'weight',
      '#title' => t('Weight'),
      '#title_display' => 'invisible',
      '#default_value' => $delta,
    );
  }

  // Submit button
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save configuration'),
  );

  return $form;
}


/**
 * Sharer toolbar configuration form submit.
 */
function sharer_toolbar_config_form_submit($form, &$form_state) {
  $sharers_order = array();
  $sharers_configuration = array();

84
85
86
  // Save sharer toolbar title
  variable_set('sharer_toolbar_title', $form_state['values']['sharer_toolbar_title']);

87
88
89
90
  // Get available sharers and submitted values to compare them
  $available_sharers = _sharer_toolbar_get_available_sharers();
  $submitted_sharers = $form_state['values']['sharers'];

91
  // Iterate on orders to store informations
92
  foreach ($submitted_sharers as $sid => $submitted_sharer) {
93
94

    // Store weight
95
    $sharers_order[$submitted_sharer['weight']] = $sid;
96

97
    // Store status
98
    $sharers_configuration[$sid] = array(
99
      'status' => $submitted_sharer['status'],
100
    );
101
102
103
104
105
106
107
108
109
110
111

    // And store configuration if different from default
    if ($submitted_sharer['name'] != $available_sharers[$sid]['name']) {
      $sharers_configuration[$sid]['name'] = $submitted_sharer['name'];
    }
    if ($submitted_sharer['icon_class'] != $available_sharers[$sid]['icon_class']) {
      $sharers_configuration[$sid]['icon_class'] = $submitted_sharer['icon_class'];
    }
    if ($submitted_sharer['label'] != $available_sharers[$sid]['label']) {
      $sharers_configuration[$sid]['label'] = $submitted_sharer['label'];
    }
112
113
114
115
116
117
118
119
120
  }

  // Save values
  variable_set('sharer_toolbar_sharers_order', $sharers_order);
  variable_set('sharer_toolbar_sharers_configuration', $sharers_configuration);

  // Confirmation
  drupal_set_message(t('Sharer toolbar configuration has been saved.'));
}