summaryrefslogtreecommitdiff
path: root/kolab.org/www/drupal-7.18/sites/all/modules/checklistapi/checklistapi.pages.inc
blob: dd82ec9ceeb5aca6e2bd0fd9d61081b0923d5562 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php

/**
 * @file
 * Page callbacks for the Checklist API module.
 */

/**
 * Page callback: Form constructor for "Clear saved progress" confirmation form.
 *
 * @param string $id
 *   The checklist ID.
 *
 * @see checklistapi_menu()
 *
 * @ingroup forms
 */
function checklistapi_checklist_clear_confirm($form, &$form_state, $id) {
  $checklist = checklistapi_checklist_load($id);
  $form['#checklist'] = $checklist;
  return confirm_form(
    $form,
    t('Are you sure you want to clear %title saved progress?', array(
      '%title' => $checklist->title,
    )),
    $checklist->path,
    t('All progress details will be erased. This action cannot be undone.'),
    t('Clear'),
    t('Cancel')
  );
}

/**
 * Form submission handler for checklistapi_checklist_clear_confirm().
 */
function checklistapi_checklist_clear_confirm_submit($form, &$form_state) {
  if ($form_state['values']['confirm']) {
    $form['#checklist']->clearSavedProgress();
  }
  $form_state['redirect'] = $form['#checklist']->path;
}

/**
 * Page callback: Form constructor for the checklist form.
 *
 * @param string $id
 *   The checklist ID.
 *
 * @see checklistapi_checklist_form_submit()
 * @see checklistapi_menu()
 *
 * @ingroup forms
 */
function checklistapi_checklist_form($form, &$form_state, $id) {
  $form['#checklist'] = $checklist = checklistapi_checklist_load($id);
  if (checklistapi_compact_mode()) {
    $form['#attributes']['class'] = array('compact-mode');
  }
  $form['compact_mode_link'] = array(
    '#markup' => theme('checklistapi_compact_link'),
  );
  $form['checklistapi'] = array(
    '#attached' => array(
      'css' => array(drupal_get_path('module', 'checklistapi') . '/checklistapi.css'),
      'js' => array(drupal_get_path('module', 'checklistapi') . '/checklistapi.js'),
    ),
    '#tree' => TRUE,
    '#type' => 'vertical_tabs',
  );
  // Loop through groups.
  $num_autochecked_items = 0;
  $groups = $checklist->items;
  foreach (element_children($groups) as $group_key) {
    $group = &$groups[$group_key];
    $form['checklistapi'][$group_key] = array(
      '#title' => filter_xss($group['#title']),
      '#type' => 'fieldset',
    );
    if (!empty($group['#description'])) {
      $form['checklistapi'][$group_key]['#description'] = filter_xss_admin($group['#description']);
    }
    // Loop through items.
    foreach (element_children($group) as $item_key) {
      $item = &$group[$item_key];
      $saved_item = !empty($checklist->savedProgress[$item_key]) ? $checklist->savedProgress[$item_key] : 0;
      // Build title.
      $title = filter_xss($item['#title']);
      if ($saved_item) {
        // Append completion details.
        $user = user_load($saved_item['#uid']);
        $title .= t(
          '<span class="completion-details"> - Completed @time by !user</a>',
          array(
            '@time' => format_date($saved_item['#completed'], 'short'),
            '!user' => theme('username', array('account' => $user)),
          )
        );
      }
      // Set default value.
      $default_value = FALSE;
      if ($saved_item) {
        $default_value = TRUE;
      }
      elseif (!empty($item['#default_value'])) {
        if ($default_value = $item['#default_value']) {
          $num_autochecked_items++;
        }
      }
      // Get description.
      $description = (isset($item['#description'])) ? '<p>' . filter_xss_admin($item['#description']) . '</p>' : '';
      // Append links.
      $links = array();
      foreach (element_children($item) as $link_key) {
        $link = &$item[$link_key];
        $context = (!empty($link['#context'])) ? $link['#context'] : CHECKLISTAPI_LINK_CONTEXT_ANY;
        $show_link = FALSE;
        if ($context == CHECKLISTAPI_LINK_CONTEXT_ANY) {
          $show_link = TRUE;
        }
        elseif ($saved_item && $context == CHECKLISTAPI_LINK_CONTEXT_ITEM_CHECKED) {
          $show_link = TRUE;
        }
        elseif (!$saved_item && $context == CHECKLISTAPI_LINK_CONTEXT_ITEM_UNCHECKED) {
          $show_link = TRUE;
        }
        if ($show_link) {
          $options = (!empty($link['#options']) && is_array($link['#options'])) ? $link['#options'] : array();
          $links[] = l($link['#text'], $link['#path'], $options);
        }
      }
      if (count($links)) {
        $description .= '<div class="links">' . implode(' | ', $links) . '</div>';
      }
      // Compile the list item.
      $form['checklistapi'][$group_key][$item_key] = array(
        '#attributes' => array('class' => array('checklistapi-item')),
        '#default_value' => $default_value,
        '#description' => filter_xss_admin($description),
        '#disabled' => !($user_has_edit_access = $checklist->userHasAccess('edit')),
        '#title' => filter_xss_admin($title),
        '#type' => 'checkbox',
      );
    }
  }
  $form['actions'] = array(
    '#access' => $user_has_edit_access,
    '#type' => 'actions',
    '#weight' => 100,
    'save' => array(
      '#submit' => array('checklistapi_checklist_form_submit'),
      '#type' => 'submit',
      '#value' => t('Save'),
    ),
    'clear' => array(
      '#access' => $checklist->hasSavedProgress(),
      '#href' => $checklist->path . '/clear',
      '#title' => t('Clear saved progress'),
      '#type' => 'link',
    ),
  );

  // Alert the user of autochecked items. Only set the message on GET requests
  // to prevent it from reappearing after saving the form. (Testing the request
  // method may not be the "correct" way to accomplish this.)
  if ($num_autochecked_items && $_SERVER['REQUEST_METHOD'] == 'GET') {
    drupal_set_message(t(
      format_plural(
        $num_autochecked_items,
        '%checklist found 1 unchecked item that was already completed and checked it for you. Save the form to record the change.',
        '%checklist found @num unchecked items that were already completed and checked them for you. Save the form to record the changes.'
      ),
      array(
        '%checklist' => $checklist->title,
        '@num' => $num_autochecked_items,
      )
    ), 'status');
  }

  return $form;
}

/**
 * Form submission handler for checklistapi_checklist_form().
 */
function checklistapi_checklist_form_submit($form, &$form_state) {
  $form['#checklist']->saveProgress($form_state['values']['checklistapi']);
}

/**
 * Determines whether the current user is in compact mode.
 *
 * Compact mode shows checklist forms with less description text.
 *
 * Whether the user is in compact mode is determined by a cookie, which is set
 * for the user by checklistapi_compact_page().
 *
 * If the user does not have the cookie, the default value is given by the
 * system variable 'checklistapi_compact_mode', which itself defaults to FALSE.
 * This does not have a user interface to set it: it is a hidden variable which
 * can be set in the settings.php file.
 *
 * @return bool
 *   TRUE when in compact mode, FALSE when in expanded mode.
 */
function checklistapi_compact_mode() {
  return isset($_COOKIE['Drupal_visitor_checklistapi_compact_mode']) ? $_COOKIE['Drupal_visitor_checklistapi_compact_mode'] : variable_get('checklistapi_compact_mode', FALSE);
}

/**
 * Menu callback: Sets whether the admin menu is in compact mode or not.
 *
 * @param string $mode
 *   Valid values are 'on' and 'off'.
 */
function checklistapi_compact_page($mode = 'off') {
  user_cookie_save(array('checklistapi_compact_mode' => ($mode == 'on')));
  drupal_goto();
}

/**
 * Returns HTML for a link to show or hide inline item descriptions.
 *
 * @ingroup themeable
 */
function theme_checklistapi_compact_link() {
  $output = '<div class="compact-link">';
  if (checklistapi_compact_mode()) {
    $output .= l(
      t('Show descriptions'),
      request_path() . '/compact/off',
      array(
        'attributes' => array(
          'title' => t('Expand layout to include descriptions.'),
        ),
        'query' => drupal_get_destination(),
      )
    );
  }
  else {
    $output .= l(
      t('Hide descriptions'),
      request_path() . '/compact/on',
      array(
        'attributes' => array(
          'title' => t('Compress layout by hiding descriptions.'),
        ),
        'query' => drupal_get_destination(),
      )
    );
  }
  $output .= '</div>';
  return $output;
}