Project

General

Profile

Download (16.5 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/**
3
 * @file
4
 * Allows to create media URI conversion rules.
5
 *
6
 * @copyright
7
 *   (C) 2007-2012 EDIT
8
 *   European Distributed Institute of Taxonomy
9
 *   http://www.e-taxonomy.eu
10
 *
11
 *   The contents of this module are subject to the Mozilla
12
 *   Public License Version 1.1.
13
 * @see http://www.mozilla.org/MPL/MPL-1.1.html
14
 *
15
 * @author
16
 *   - Andreas Kohlbecker <a.kohlbecker@BGBM.org>
17
 *   - Wouter Addink <w.addink@eti.uva.nl> (migration from Drupal 5 to Drupal7)
18
 */
19

    
20
/**
21
 * Implements hook_menu().
22
 */
23
function cdm_mediauri_menu() {
24
  $items = array();
25

    
26
  $items['admin/config/cdm_dataportal/cdm_mediauri'] = array(
27
    'title' => 'CDM Media URI Conversion',
28
    'description' => 'Media URI Conversion Rules.',
29
    'access arguments' => array('administer cdm_dataportal'),
30
    'page callback' => 'cdm_mediauri_overview',
31
    'type' => MENU_NORMAL_ITEM,
32
    'weight' => 20,
33
  );
34

    
35
  $items['admin/config/cdm_dataportal/cdm_mediauri/list'] = array(
36
    'title' => 'List',
37
    'description' => 'Media URI Conversion Rules',
38
    'access arguments' => array('administer cdm_dataportal'),
39
    'page callback' => 'cdm_mediauri_overview',
40
    'weight' => 1,
41
    'type' => MENU_DEFAULT_LOCAL_TASK,
42
  );
43

    
44
  $items['admin/config/cdm_dataportal/cdm_mediauri/add'] = array(
45
    'title' => 'Add Rule',
46
    'description' => 'Add Media URI Conversion Rule',
47
    'access arguments' => array('administer cdm_dataportal'),
48
    'page callback' => 'drupal_get_form',
49
    'page arguments' => array('cdm_mediauri_rule_add'),
50
    'weight' => 2,
51
    'type' => MENU_LOCAL_TASK,
52
  );
53

    
54
  $items['admin/config/cdm_dataportal/cdm_mediauri/edit'] = array(
55
    'access arguments' => array('administer cdm_dataportal'),
56
    'page callback' => 'drupal_get_form',
57
    'page arguments' => array('cdm_mediauri_rule_edit'),
58
    'type' => MENU_CALLBACK,
59
  );
60

    
61
  $items['admin/config/cdm_dataportal/cdm_mediauri/delete'] = array(
62
    'access arguments' => array('administer cdm_dataportal'),
63
    'page callback' => 'drupal_get_form',
64
    'page arguments' => array('cdm_mediauri_rule_delete'),
65
    'type' => MENU_CALLBACK,
66
  );
67

    
68
  $rules = cdm_mediauri_rules();
69
  foreach ($rules as $rule) {
70
    if ($rule['embed_html'] && $rule['path']) {
71
      $items[$rule['path']] = array(
72
        // TODO restrict access.
73
        'access arguments' => TRUE,
74
        'page callback' => 'cdm_mediauri_view_embed',
75
        'type' => MENU_CALLBACK,
76
      );
77
    }
78
  }
79

    
80
  return $items;
81
}
82

    
83
/**
84
 * Returns an overview list of existing media uri rules
85
 */
86
function cdm_mediauri_overview($rules = FALSE) {
87
  if (!$rules) {
88
    $rules = cdm_mediauri_rules();
89
  }
90

    
91
  $header = array(
92
    t('Pattern'),
93
    t('Replacement'),
94
    t('Operations'),
95
  );
96
  $rid = -1;
97
  if (count($rules)) {
98
    foreach ($rules as $rule) {
99
      $rid++;
100
      $rows[] = array(
101
        $rule['pattern'],
102
        $rule['replace'],
103
        l(t('edit'), 'admin/config/cdm_mediauri/edit/' . $rid) . ' | ' . l(t('delete'), 'admin/config/cdm_mediauri/delete/' . $rid),
104
      );
105
    }
106
  }
107
  else {
108
    $rows[] = array(
109
      array(
110
        'data' => '<em>' . t('There are no existing media uri conversion rules. You may add some rule: <a href="@add">add rule</a>', array(
111
          '@add' => url('admin/config/cdm_mediauri/add'),
112
        )) . '</em>',
113
        'colspan' => 3,
114
      ),
115
    );
116
  }
117

    
118
  return theme('table', array('header' => $header, 'rows' => $rows));
119
}
120

    
121
/**
122
 * Menu callback; add new mediauri rule.
123
 */
124
function cdm_mediauri_rule_add($form, &$form_state) {
125
  $form = cdm_mediauri_rule_edit_form('new');
126
  // Comment @WA: we cannot use system settings form here because we want to
127
  // use our own submit function (cdm_mediauri_rule_add_submit)
128
  // return system_settings_form($form);
129
  return $form;
130
}
131
/**
132
 * @todo Please document this function.
133
 * @see http://drupal.org/node/1354
134
 */
135
function cdm_mediauri_rule_edit($form, &$form_state) {
136
  $rid = arg(4);
137
  $form = cdm_mediauri_rule_edit_form($rid);
138
  // return system_settings_form($form);
139
  return $form;
140
}
141
/**
142
 * @todo Please document this function.
143
 * @see http://drupal.org/node/1354
144
 */
145
function cdm_mediauri_rule_add_submit($form, &$form_state) {
146
  if ($form_state['values']['rid'] == 'new') {
147
    $rules = cdm_mediauri_rules();
148
    $rules[] = $form_state['values']['rule'];
149
    variable_set('cdm_mediauri_rules', $rules);
150
    drupal_set_message(t('Rule added'));
151
  }
152
  else {
153
    drupal_set_message(t('Cannot add rule'), 'error');
154
  }
155
  $form_state['redirect'] = 'admin/config/cdm_mediauri';
156
}
157
/**
158
 * @todo Please document this function.
159
 * @see http://drupal.org/node/1354
160
 */
161
function cdm_mediauri_rule_edit_submit($form, &$form_state) {
162
  if (is_numeric($form_state['values']['rid'])) {
163
    $rules = cdm_mediauri_rules();
164
    // Determine if the menu has changed.
165
    if (isset($rules[$form_state['values']['rid']]['embed']) && $rules[$form_state['values']['rid']]['embed'] != $form_state['values']['rule']['embed'] || $rules[$form_state['values']['rid']]['path'] != $form_state['values']['rule']['path']) {
166
      menu_rebuild();
167
      drupal_set_message(t('Menu Rebuild'));
168
    }
169
    $rules[$form_state['values']['rid']] = $form_state['values']['rule'];
170
    variable_set('cdm_mediauri_rules', $rules);
171
    drupal_set_message(t('Rule saved'));
172
  }
173
  else {
174
    drupal_set_message(t('Cannot save rule (invalid rule id)'), 'error');
175
  }
176
  $form_state['redirect'] = 'admin/config/cdm_mediauri';
177
}
178
/**
179
 * @todo Please document this function.
180
 * @see http://drupal.org/node/1354
181
 */
182
function cdm_mediauri_rule_delete_submit($form, &$form_state) {
183
  if (is_numeric($form_state['values']['rid'])) {
184
    $rules = cdm_mediauri_rules();
185
    unset($rules[$form_state['values']['rid']]);
186
    variable_set('cdm_mediauri_rules', $rules);
187
    drupal_set_message(t('Rule deleted'));
188
  }
189
  else {
190
    drupal_set_message(t('Cannot delete rule (invalid rule id)'), 'error');
191
  }
192
  $form_state['redirect'] = 'admin/config/cdm_mediauri';
193
}
194

    
195
/**
196
 * Menu callback; confirm deletion of mediauri rule.
197
 */
198
function cdm_mediauri_rule_delete($form, &$form_state) {
199
  $rid = arg(4);
200
  $rules = cdm_mediauri_rules();
201
  $form['rid'] = array(
202
    '#type' => 'hidden',
203
    '#value' => $rid,
204
  );
205

    
206
  // Comment @WA: I removed the rule table from the <h2> title to the
207
  // description so it is consistent with the rest of the interface,
208
  // instead of displaying the table enlarged like in D5. I would recommend
209
  // also to hide the Operations column from the table because it may
210
  // be confusing here.
211
  return confirm_form(
212
      $form,
213
      t('Are you sure you want to delete the following rule:'),
214
      'admin/config/cdm_mediauri',
215
      cdm_mediauri_overview(array($rules[$rid])),
216
      t('Delete'),
217
      t('Cancel')
218
    );
219
}
220
/**
221
 * @todo Please document this function.
222
 * @see http://drupal.org/node/1354
223
 */
224
function cdm_mediauri_rule_edit_form($rid = 'new') {
225
  if ($rid == 'new') {
226
    // ---- Little code snippet used to import default rules during development.
227
    // 0 or 1 or FALSE.
228
    $default_rule_id = FALSE;
229

    
230
    if ($default_rule_id !== FALSE) {
231
      $default_rules = cdm_mediauri_rules(TRUE);
232
      $rule = $default_rules[$default_rule_id];
233
    }
234
    /* --- END */
235

    
236
    if (!isset($rule)) {
237
      $rule = array();
238
      $rule['type'] = 'empty';
239
      $rule['pattern'] = '';
240
      $rule['replace'] = '';
241
      $rule['prefix'] = '';
242
      $rule['affix'] = '';
243
      $rule['url_encode'] = 0;
244
      $rule['embed_html'] = 0;
245
      $rule['path'] = '';
246
      $rule['title'] = '';
247
      $rule['size_x'] = '';
248
      $rule['size_y'] = '';
249
    }
250
  }
251
  else {
252
    $rules = cdm_mediauri_rules();
253
    $rule = $rules[$rid];
254
  }
255

    
256
  $form = array();
257

    
258
  /*
259
   * rule ['type']: values(preview, webapp, media) ['size_x'], ['size_y']:
260
   * dimensions of preview image in px ['pattern']: php PCRE regex pattern (see:
261
   * http://www.php.net/manual/reference.pcre.pattern.syntax.php) ['replace']:
262
   * replacement string ['prefix']: string by which the converted url is to be
263
   * prefixed ['affix']: string by which the converted url is to be affixed
264
   * ['url_encode']: values(TRUE, FALSE) if the converted url is to be url
265
   * encoded ['embed_html']: embes the construct of prefix + url + affix into
266
   * the page as plain html
267
   */
268
  $form['rid'] = array(
269
    '#type' => 'hidden',
270
    '#value' => $rid,
271
  );
272

    
273
  $form['rule'] = array(
274
    '#type' => 'fieldset',
275
    '#title' => check_plain($rule['type'] == 'empty' ? t('New Rule') : t('Rule !rule', array('!rule' => $rid))),
276
    '#tree' => TRUE,
277
  );
278

    
279
  $form['rule']['type'] = array(
280
    '#type' => 'select',
281
    '#title' => t('Type') . ':',
282
    '#default_value' => $rule['type'],
283
    '#options' => array(
284
      'preview' => t('Preview Quality'),
285
      'webapp' => t('Web Application'),
286
    ),
287
    '#description' => t('The type media representation.'),
288
  );
289

    
290
  $form['rule']['pattern'] = array(
291
    '#type' => 'textarea',
292
    '#title' => t('Pattern') . ':',
293
    '#description' => check_plain(t('php PCRE regex pattern (see: !php_manual)', array(
294
      '!php_manual' => l(t('php manual'), 'http://www.php.net/manual/reference.pcre.pattern.syntax.php'),
295
    ))),
296
    '#default_value' => $rule['pattern'],
297
    '#rows' => 1,
298
  );
299

    
300
  $form['rule']['replace'] = array(
301
    '#type' => 'textarea',
302
    '#title' => t('Replacement') . ':',
303
    '#default_value' => $rule['replace'],
304
    '#rows' => 1,
305
  );
306

    
307
  // ['embed_html'] = 1;
308
  $form['rule']['embed_html'] = array(
309
    '#type' => 'checkbox',
310
    '#title' => t('Embed'),
311
    '#default_value' => $rule['embed_html'],
312
    '#description' => t('Embed the construct of prefix + url + affix into the page as plain html'),
313
  );
314

    
315
  $form['rule']['url_encode'] = array(
316
    '#type' => 'checkbox',
317
    '#title' => t('Url Encode'),
318
    '#default_value' => $rule['url_encode'],
319
    '#description' => t('Whether the converted url is to be url encoded'),
320
  );
321

    
322
  $form['rule']['path'] = array(
323
    '#type' => 'textfield',
324
    '#title' => t('Drupal Path') . ':',
325
    '#default_value' => $rule['path'],
326
    '#description' => t('Drupal Path under which the media is to be embedded into a page'),
327
  );
328

    
329
  $form['rule']['title'] = array(
330
    '#type' => 'textfield',
331
    '#title' => t('Page Title') . ':',
332
    '#default_value' => $rule['title'],
333
    '#description' => t('Page Title'),
334
  );
335

    
336
  $form['rule']['prefix'] = array(
337
    '#type' => 'textarea',
338
    '#title' => t('Prefix') . ':',
339
    '#default_value' => $rule['prefix'],
340
    '#rows' => 1,
341
  );
342

    
343
  $form['rule']['affix'] = array(
344
    '#type' => 'textarea',
345
    '#title' => t('Affix') . ':',
346
    '#default_value' => $rule['affix'],
347
    '#rows' => 1,
348
  );
349

    
350
  // Comment @WA: I think this is not implemented the right way?
351
  // Should the field not be enabled if 'Preview' is selected as type in
352
  // the form, for instance with ajax?
353
  $form['rule']['size_x'] = array(
354
    '#type' => 'textfield',
355
    '#title' => t('Preview Image Width') . ':',
356
    '#default_value' => $rule['size_x'],
357
    '#disabled' => $rule['type'] != 'preview',
358
    '#description' => check_plain(t('Width of preview image in px')),
359
    '#size' => 4,
360
  );
361

    
362
  $form['rule']['size_y'] = array(
363
    '#type' => 'textfield',
364
    '#title' => t('Preview Image Height') . ':',
365
    '#default_value' => $rule['size_y'],
366
    '#disabled' => $rule['type'] != 'preview',
367
    '#description' => t('Height of preview image in px'),
368
    '#size' => 4,
369
  );
370

    
371
  $form['actions']['submit'] = array(
372
    '#type' => 'submit',
373
    '#value' => t('Save configuration'),
374
  );
375

    
376
  // @WA: D7 form api does not support reset buttons,
377
  // so to mimic the D5 reset button we add one like this.
378
  $form['actions']['reset'] = array(
379
    '#markup' => '<input id="reset" type="reset" class="form-submit" value="' . t('Reset to defaults') . '" />',
380
    '#weight' => 1000,
381
  );
382
  return $form;
383
}
384
/**
385
 * @todo Please document this function.
386
 * @see http://drupal.org/node/1354
387
 */
388
function cdm_mediauri_rules($default = FALSE) {
389
  /*
390
  rules ['type']: values(preview, webapp, media) ['size_x'], ['size_y']:
391
  dimensions of preview image in px ['pattern']: php PCRE regex pattern (see:
392
  http://www.php.net/manual/reference.pcre.pattern.syntax.php) ['replace']:
393
  replacement string ['prefix']: string by which the converted url is to be
394
  prefixed ['affix']: string by which the converted url is to be affixed
395
  ['url_encode']: values(TRUE, FALSE) if the converted url to be url encoded
396
  ['embed_html']: embes the construct of prefix + url + affix into the page
397
  as plain html
398
  */
399
  if ($default) {
400
    // Default rules.
401
    $rule1 = array();
402
    $rule1['type'] = 'preview';
403
    $rule1['pattern'] = '/^(http:\/\/ww2.bgbm.org\/herbarium\/images(?:\/\w+)(?:\/\d+){4})(\/)(.*)$/';
404
    $rule1['replace'] = '$1/thumbs/$3';
405
    $rule1['size_x'] = 125;
406
    $rule1['size_Y'] = 200;
407

    
408
    $rule2 = array();
409
    $rule2['type'] = 'webapp';
410
    $rule2['pattern'] = '/^http:\/\/ww2.bgbm.org\/herbarium\/images\/((?:\w+\/)(?:\d+\/){4}[\w_]+?\.)(.*)$/';
411
    $rule2['replace'] = '$1fpx';
412
    $rule2['prefix'] = '<div style="float: left; width: 10em;">
413
  <p>© BGBM: <br/>
414
  Access is granted under the following conditions:<br/>
415
  The images may not passed on to any third party without our written
416
  consent and due mention of the copyright restrictions, and may not be
417
  used for commercial or non-scientific purposes. Reproduction in
418
  scientific publications is authorized on the condition that the source
419
  and the copyright are fully acknowledged:<br/>
420
  © Botanischer Garten und Botanisches Museum Berlin-Dahlem, FU Berlin.</li>
421
</p>
422
<p><a href="http://www.bgbm.org/bgbm/verantwo_e.htm"
423
    class="leftnavi" title="Contact">Contact</a> | <a href="http://www.bgbm.org/bgbm/imprint.htm"
424
    class="leftnavi" title="Impressum">Imprint</a> | <a href="http://www.bgbm.org/disclaim_e.htm" class="leftnavi"
425
    title="Disclaimer">Disclaimer</a>
426
</p>
427
</div><div><embed width="490" height="700" align="middle" type="application/x-shockwave-flash" 
428
       pluginspage="http://www.macromedia.com/go/getflashplayer" name="FSIViewer" 
429
       bgcolor="black" quality="high" play="true" 
430
       src="http://ww2.bgbm.org/fsi/fsi.swf?cfg=plugin2&FPXSrc=';
431
    $rule2['affix'] = '&FPXWidth=2801&FPXHeight=4478&Effects=qlt%3D85&InitialView=&Measure_ImageWidth=270.0&Measure_Suffix=%20mm"/></div>';
432
    $rule2['url_encode'] = 1;
433
    $rule2['embed_html'] = 1;
434
    $rule2['path'] = 'bgbm.org/herbarium';
435

    
436
    return array(
437
      $rule1,
438
      $rule2,
439
    );
440
  }
441
  else {
442
    return variable_get('cdm_mediauri_rules', array());
443
  }
444
}
445

    
446
/**
447
 * Converts a media uri.
448
 *
449
 * Converts according to all matching conversion rules which are
450
 * currently defined.
451
 *
452
 * Format of a returned array element:
453
 *
454
 * $return_array[{type}] { // values for {type}:(preview, webapp, media,
455
 * original)
456
 * ['size_x'], // dimensions of preview image in px
457
 * ['size_y'], // dimensions of preview image in px
458
 * ['uri'] // the uri possibly prefixed with path
459
 * ['embed_html'] // the uri embedded into some string
460
 * }
461
 *
462
 * @param string $mediaUri
463
 *   The media uri to convert.
464
 *
465
 * @return array
466
 *   Returns an associative array of the original media uri and all conversions
467
 *   whereas the rule type becomes the array key of the array entries.
468
 *   All uris are again packed into associative arrays by means to submit
469
 *   additional information as defined in the rules applied.
470
 */
471
function cdm_mediauri_conversion($mediaUri) {
472
  $rules = cdm_mediauri_rules();
473

    
474
  $muris = array();
475
  $muris['original'] = array(
476
    'uri' => $mediaUri,
477
    'size_x' => NULL,
478
    'size_y' => NULL,
479
  );
480

    
481
  foreach ($rules as $rule) {
482
    $uri_converted = preg_replace($rule['pattern'], $rule['replace'], $mediaUri);
483
    if ($uri_converted != $mediaUri) {
484
      if (isset($rule['url_encode']) && $rule['url_encode']) {
485
        $uri_converted = urlencode($uri_converted);
486
      }
487
      $muris[$rule['type']] = array(
488
        'uri' => $uri_converted,
489
        'size_x' => $rule['size_x'],
490
        'size_y' => $rule['size_y'],
491
      );
492
      if (isset($rule['embed_html']) && $rule['embed_html']) {
493
        if ($rule['path']) {
494
          // Embed later on other page.
495
          $muris[$rule['type']]['uri'] = $rule['path'] . '/' . $uri_converted;
496
        }
497
        else {
498
          // Direct embed.
499
          $muris[$rule['type']]['embed_html'] = $rule['prefix'] . $uri_converted . $rule['affix'];
500
        }
501
      }
502
    }
503
  }
504
  return $muris;
505
}
506
/**
507
 * @todo Please document this function.
508
 * @see http://drupal.org/node/1354
509
 */
510
function cdm_mediauri_view_embed($uri) {
511
  $path = '';
512
  for ($i = 0; ($arg = arg($i)); $i++) {
513
    if ($arg == $uri) {
514
      break;
515
    }
516
    else {
517
      $path .= ($path ? '/' : '') . $arg;
518
    }
519
  }
520

    
521
  $rule = FALSE;
522
  foreach (cdm_mediauri_rules() as $r) {
523
    if ($r['embed_html'] && $r['path'] == $path) {
524
      $rule = $r;
525
      break;
526
    }
527
  }
528

    
529
  if ($rule) {
530
    drupal_set_title($rule['title'], PASS_THROUGH);
531
    return $rule['prefix'] . $uri . $rule['affix'];
532
  }
533

    
534
  return 'ERROR:' . $path . $uri;
535
}
(3-3/4)