voxpelli / drupal-oembed

An oEmbed client and provider for Drupal

This URL has Read+Write access

drupal-oembed / oembedcore.admin.inc
100644 349 lines (313 sloc) 10.565 kb
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
// $Id$
 
/**
 * @file
 * Administrative functions for the oembed module.
 *
 * This provides the UI to list, create, edit and delete presets.
 */
 
/**
 * Output a list of presets.
 */
function oembedcore_list_preset($js = NULL) {
  $header = array(
    array('data' => t('Name'), 'class' => 'oembedcore-preset-name'),
    array('data' => t('Width'), 'class' => 'oembedcore-preset-width'),
    array('data' => t('Height'), 'class' => 'oembedcore-preset-height'),
    array('data' => t('Storage'), 'class' => 'oembedcore-preset-storage'),
  );
 
  $header[] = array('data' => t('Operations'), 'class' => 'oembedcore-preset-operations');
 
  $presets = oembedcore_preset_load_all();
  $rows = array();
 
  foreach ($presets as $preset) {
    $operations = array();
 
    if (empty($preset->disabled)) {
      $operations[] = array(
        'title' => t('Edit'),
        'href' => 'admin/build/oembed/' . $preset->name . '/edit',
      );
      $operations[] = array(
        'title' => t('Export'),
        'href' => 'admin/build/oembed/' . $preset->name . '/export',
      );
    }
 
    if ($preset->export_type == (EXPORT_IN_CODE | EXPORT_IN_DATABASE)) {
      $operations[] = array(
        'title' => t('Revert'),
        'href' => 'admin/build/oembed/' . $preset->name . '/delete',
      );
    }
    elseif ($preset->export_type != EXPORT_IN_CODE) {
      $operations[] = array(
        'title' => t('Delete'),
        'href' => 'admin/build/oembed/' . $preset->name . '/delete',
      );
    }
    elseif (empty($preset->disabled)) {
      $operations[] = array(
        'title' => t('Disable'),
        'href' => 'admin/build/oembed/' . $preset->name . '/disable',
        'query' => drupal_get_destination(),
      );
    }
    else {
      $operations[] = array(
        'title' => t('Enable'),
        'href' => 'admin/build/oembed/' . $preset->name . '/enable',
        'query' => drupal_get_destination(),
      );
    }
 
    $rows[$preset->name] = array(
      'data' => array(
        'name' => array(
          'data' => $preset->name,
          'class' => 'oembedcore-preset-name',
        ),
        'width' => array(
          'data' => $preset->width,
          'class' => 'oembedcore-preset-width',
        ),
        'height' => array(
          'data' => $preset->height,
          'class' => 'oembedcore-preset-height',
        ),
        'storage' => array(
          'data' => ($preset->export_type == EXPORT_IN_CODE) ? t('In code') : t('In database'),
          'class' => 'oembedcore-preset-storage',
        ),
        'operations' => array(
          'data' => theme('links', $operations),
          'class' => 'oembedcore-preset-operations',
        ),
      ),
      'class' => 'oembedcore-preset-' . $preset->name . (!empty($preset->disabled) ? ' oembedcore-preset-disabled' : ''),
      //'title => ??
    );
  }
 
  $table = theme('table', $header, $rows, array('id' => 'oembedcore-list-preset'));
 
// $operations = '<div id="oembedcore-links" class="links">' . theme('links', $pages['operations']) . '</div>';
 
  drupal_add_css(drupal_get_path('module', 'oembedcore') . '/oembedcore.admin.css');
 
  return $table;
}
 
/**
 * Handle the add preset page.
 */
function oembedcore_add_preset() {
  $preset = oembedcore_preset_new();
  drupal_set_title(t('Add preset')); //TODO: Isn't this redundant?
  return oembedcore_edit_preset($preset);
}
 
/**
 * Edit a preset.
 *
 * Called from both the add and edit points to provide for common flow.
 */
function oembedcore_edit_preset($preset) {
  if (!is_object($preset)) {
    $preset = oembedcore_preset_load($preset);
  }
  if ($preset) {
    drupal_set_title(check_plain($preset->name));
  }
 
  return drupal_get_form('oembedcore_edit_form_preset', $preset);
}
 
/**
 * Form to edit the settings of a preset.
 */
function oembedcore_edit_form_preset(&$form_state, $preset) {
  $form = array();
 
  $form['pid'] = array(
    '#type' => 'value',
    '#value' => isset($preset->pid) ? $preset->pid : '',
  );
  $form['preset'] = array(
    '#type' => 'value',
    '#value' => $preset,
  );
 
  $form['name'] = array(
    '#type' => 'textfield',
    '#size' => 24,
    '#default_value' => $preset->name,
    '#title' => t('Preset name'),
    '#description' => t('A unique name used to identify this preset internally. It must be only be alpha characters and underscores. No spaces, numbers or uppercase characters.'),
  );
 
  $form['width'] = array(
    '#type' => 'textfield',
    '#size' => 6,
    '#default_value' => $preset->width,
    '#title' => t('Max width'),
    '#description' => t('A maximum width in pixels of the embed or 0 for no maximum.'),
  );
 
  $form['height'] = array(
    '#type' => 'textfield',
    '#size' => 6,
    '#default_value' => $preset->height,
    '#title' => t('Max height'),
    '#description' => t('A maximum height in pixels of the embed or 0 for no maximum.'),
  );
 
  $form['disable_title'] = array(
    '#type' => 'checkbox',
    '#default_value' => !empty($preset->conf['disable_title']),
    '#title' => t('Disable title when other embeddable data is available'),
  );
 
  $label = empty($preset->pid) ? t('Save and proceed') : t('Save');
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => $label,
  );
 
  return $form;
}
 
/**
 * Validate submission of the preset edit form.
 */
function oembedcore_edit_form_preset_validate($form, &$form_state) {
  // Test uniqueness of name:
  if (!$form_state['values']['name']) {
    form_error($form['name'], t('Preset name is required.'));
  }
  elseif (preg_match("/[^A-Za-z0-9_]/", $form_state['values']['name'])) {
    form_error($form['name'], t('Preset name must be alphanumeric or underscores only.'));
  }
  else {
    $query = "SELECT pid FROM {oembedcore_preset} WHERE name = '%s'";
    if (!empty($form_state['values']['pid']) && is_numeric($form_state['values']['pid'])) {
      $query .= ' AND pid != ' . $form_state['values']['pid'];
    }
    if (db_result(db_query($query, $form_state['values']['name']))) {
      form_error($form['name'], t('Preset name must be unique.'));
    }
  }
 
  if (!is_numeric($form_state['values']['width'])) {
    if (empty($form_state['values']['width'])) {
      form_set_value($form['width'], '0', $form_state);
    }
    else {
      form_error($form['width'], t('Preset width must be number.'));
    }
  }
  elseif ($form_state['values']['width'] < 0) {
    form_error($form['width'], t('Preset width must be a non-negative number.'));
  }
  if (!is_numeric($form_state['values']['height'])) {
    if (empty($form_state['values']['height'])) {
      form_set_value($form['height'], '0', $form_state);
    }
    else {
      form_error($form['height'], t('Preset height must be number.'));
    }
  }
  elseif ($form_state['values']['height'] < 0) {
    form_error($form['height'], t('Preset height must be a non-negative number.'));
  }
}
 
/**
 * Process submission of the mini panel edit form.
 */
function oembedcore_edit_form_preset_submit($form, &$form_state) {
  $preset = $form_state['values']['preset'];
 
  $preset->title = $form_state['values']['title'];
  $preset->name = $form_state['values']['name'];
  $preset->width = (int)$form_state['values']['width'];
  $preset->height = (int)$form_state['values']['height'];
 
  $preset->conf = array(
    'disable_title' => !empty($form_state['values']['disable_title']),
  );
 
  if (empty($preset->pid)) {
    drupal_set_message(t('Your new preset %name has been saved.', array('%name' => $preset->name)));
    oembedcore_preset_save($preset);
    $form_state['values']['pid'] = $preset->pid;
    //TODO: Some redirect?
// $form_state['redirect'] = "admin/build/panel-mini/$panel_mini->name/edit-context/next";
  }
  else {
    drupal_set_message(t('Your changes have been saved.'));
    oembedcore_preset_save($preset);
  }
 
  $form_state['redirect'] = 'admin/build/oembed';
}
 
/**
 * Page callback to export a preset to PHP code.
 */
function oembedcore_export_preset(&$form_state, $preset) {
  if (!is_object($preset)) {
    $preset = oembedcore_preset_load($preset);
  }
  drupal_set_title(check_plain($preset->name));
  $code = oembedcore_preset_export($preset);
 
  $lines = substr_count($code, "\n");
  $form['code'] = array(
    '#type' => 'textarea',
    '#title' => $preset->name,
    '#default_value' => $code,
    '#rows' => $lines,
  );
  return $form;
}
 
/**
 * Provide a form to confirm deletion of a preset.
 */
function oembedcore_delete_confirm_preset(&$form_state, $preset) {
  if (!is_object($preset)) {
    $preset = oembedcore_preset_load($preset);
  }
  if ($preset->export_type == (EXPORT_IN_CODE | EXPORT_IN_DATABASE)) {
    $submit = t('Revert');
  }
  elseif ($preset->export_type != EXPORT_IN_CODE) {
    $submit = t('Delete');
  }
  else {
    drupal_not_found();
    die;
  }
  $form['name'] = array('#type' => 'value', '#value' => $preset->name);
  $form['pid'] = array('#type' => 'value', '#value' => $preset->pid);
  return confirm_form($form,
    t('Are you sure you want to delete the preset "@name"?', array('@name' => $preset->name)),
    !empty($_GET['destination']) ? $_GET['destination'] : 'admin/build/oembed',
    t('This action cannot be undone.'),
    $submit, t('Cancel')
  );
}
 
/**
 * Handle the submit button to delete a mini panel.
 */
function oembedcore_delete_confirm_preset_submit($form, &$form_state) {
  $preset = oembedcore_preset_load($form_state['values']['name']);
  if ($preset->pid == $form_state['values']['pid']) {
    oembedcore_preset_delete($preset);
    $form_state['redirect'] = 'admin/build/oembed';
  }
}
 
/**
 * Enable a default preset.
 */
function oembedcore_enable_preset($preset) {
  if (!is_object($preset)) {
    $preset = oembedcore_preset_load($preset);
  }
  ctools_include('export');
  ctools_export_set_status('oembedcore_preset', $preset->name, FALSE);
  //TODO: Extract this and move to oembedfield
  if (module_exists('content')) {
    content_clear_type_cache();
    drupal_rebuild_theme_registry();
  }
  drupal_goto();
}
 
/**
 * Disable a default preset.
 */
function oembedcore_disable_preset($preset) {
  if (!is_object($preset)) {
    $preset = oembedcore_preset_load($preset);
  }
  ctools_include('export');
  ctools_export_set_status('oembedcore_preset', $preset->name, TRUE);
  //TODO: Extract this and move to oembedfield
  if (module_exists('content')) {
    content_clear_type_cache();
    drupal_rebuild_theme_registry();
  }
  drupal_goto();
}