Project

General

Profile

« Previous | Next » 

Revision d84a8fff

Added by Andreas Kohlbecker over 14 years ago

initial commit of cdm_mediauri module

View differences:

.gitattributes
39 39
modules/cdm_dataportal/cdm_dataportal.theme.php -text
40 40
modules/cdm_dataportal/cdm_dataportal_print.css -text
41 41
modules/cdm_dataportal/cdm_dataportal_screen.css -text
42
modules/cdm_dataportal/cdm_mediauri/cdm_mediauri.info -text
43
modules/cdm_dataportal/cdm_mediauri/cdm_mediauri.module -text
42 44
modules/cdm_dataportal/cdm_taxontree/cdm_taxontree.css -text
43 45
modules/cdm_dataportal/cdm_taxontree/cdm_taxontree.info -text
44 46
modules/cdm_dataportal/cdm_taxontree/cdm_taxontree.js -text
modules/cdm_dataportal/cdm_mediauri/cdm_mediauri.info
1
; $Id$
2
name = "CDM Media-URI Conversion"
3
description = "Allows to create conversion rules for media uris"
4
version = 5.x-0.1
5
package = "CDM"
6
; dependencies = 
modules/cdm_dataportal/cdm_mediauri/cdm_mediauri.module
1
<?php
2
// $Id$
3

  
4
/*
5
 * @file
6
 * cdm_mediauri.module
7
 *
8
 * Copyright (C) 2007 EDIT
9
 * European Distributed Institute of Taxonomy
10
 * http://www.e-taxonomy.eu
11
 *
12
 * The contents of this file are subject to the Mozilla Public License Version 1.1
13
 * See LICENSE.TXT at the top of this package for the full license terms.
14
 */
15

  
16

  
17
/**
18
 * Implementation of hook_menu()
19
 */
20
function cdm_mediauri_menu($may_cache) {
21
  
22
  $items = array();
23
  if ($may_cache) {
24
    
25
   $items[] = array(
26
      'path' => 'admin/settings/cdm_mediauri',
27
      'title' => t('CDM Media URI Conversion'),
28
      'description' => t('Media URI Conversion Rules'),
29
      'access' => user_access('administer cdm_dataportal'),
30
      'callback' => 'cdm_mediauri_overview',
31
      'type' => MENU_NORMAL_ITEM,
32
    );
33

  
34
  $items[] = array(
35
      'path' => 'admin/settings/cdm_mediauri/list',
36
      'title' => t('List'),
37
      'description' => t('Media URI Conversion Rules'),
38
      'access' => user_access('administer cdm_dataportal'),
39
      'callback' => 'cdm_mediauri_overview',
40
      'weight' => 1,
41
      'type' => MENU_DEFAULT_LOCAL_TASK,
42
    );
43
    
44
    $items[] = array(
45
      'path' => 'admin/settings/cdm_mediauri/add',
46
      'title' => t('Add Rule'),
47
      'description' => t('Add Media URI Conversion Rule'),
48
      'access' => user_access('administer cdm_dataportal'),
49
      'callback' => 'drupal_get_form',
50
      'callback arguments' => array('cdm_mediauri_rule_add'),
51
      'weight' => 2,
52
      'type' => MENU_LOCAL_TASK,
53
    );
54
    
55
     $items[] = array(
56
      'path' => 'admin/settings/cdm_mediauri/edit',
57
      'access' => user_access('administer cdm_dataportal'),
58
      'callback' => 'drupal_get_form',
59
      'callback arguments' => array('cdm_mediauri_rule_edit'),
60
      'type' => MENU_CALLBACK,
61
    );
62
    
63
     $items[] = array(
64
      'path' => 'admin/settings/cdm_mediauri/delete',
65
      'access' => user_access('administer cdm_dataportal'),
66
      'callback' => 'drupal_get_form',
67
      'callback arguments' => array('cdm_mediauri_rule_delete'),
68
      'type' => MENU_CALLBACK,
69
    );
70

  
71
  }
72
  return $items;
73
}
74

  
75

  
76
/**
77
 * Returns an overview list of existing media uri rules
78
 */
79
function cdm_mediauri_overview($rules = false) {
80
  
81

  
82
  if(!$rules){
83
    $rules = cdm_mediauri_rules();
84
  }
85

  
86
  $header = array(t('Pattern'), t('Replacement'), t('Operations'));
87
  $rid = -1;
88
  if(count($rules)){
89
  foreach ($rules as $rule) {
90
      $rid++;
91
      $rows[] = array($rule['pattern'], $rule['replace'], l('edit', 'admin/settings/cdm_mediauri/edit/'. $rid).' | '.l('delete', 'admin/settings/cdm_mediauri/delete/'. $rid));
92
    }
93
  }
94
  else {
95
     $rows[] = array(array('data' => '<em>' 
96
     . t('There are no existing media uri conversion rules. You may add some rule: <a href="@add">add rule</a>', array('@add' => url('admin/settings/cdm_mediauri/add'))) . '</em>', 'colspan' => 3) );
97
  }
98
  return theme('table', $header, $rows);
99
}
100

  
101

  
102
/**
103
 * Menu callback; add new mediauri rule.
104
 */
105
function cdm_mediauri_rule_add() {
106
  
107
 
108
  $form = cdm_mediauri_rule_edit_form('new');
109
  return system_settings_form($form);
110
}
111

  
112
function cdm_mediauri_rule_edit($rid) {
113
  
114
  $form = cdm_mediauri_rule_edit_form($rid);
115
  return system_settings_form($form);
116
}
117

  
118

  
119
function cdm_mediauri_rule_add_submit($form_id, $form_values) {
120
  
121
  if($form_values['rid'] == 'new'){
122
    $rules = cdm_mediauri_rules();
123
    $rules[] = $form_values['rule'];
124
    variable_set('cdm_mediauri_rules', $rules);
125
       drupal_set_message('Rule added');
126
  }else {
127
    drupal_set_message('Cannot add rule', 'error');
128
  }
129
  return 'admin/settings/cdm_mediauri';
130
}
131

  
132
function cdm_mediauri_rule_edit_submit($form_id, $form_values) {
133
  
134
  if(is_numeric($form_values['rid'])){
135
    $rules = cdm_mediauri_rules();
136
    $rules[$form_values['rid']] = $form_values['rule'];
137
    variable_set('cdm_mediauri_rules', $rules);
138
     drupal_set_message('Rule saved');
139
  }else {
140
    drupal_set_message('Cannot save rule (invalid rule id)', 'error');
141
  }
142
  return 'admin/settings/cdm_mediauri';
143
}
144

  
145
function cdm_mediauri_rule_delete_submit($form_id, $form_values) {
146
  
147
  if(is_numeric($form_values['rid'])){
148
    $rules = cdm_mediauri_rules();
149
    unset($rules[$form_values['rid']]);
150
    variable_set('cdm_mediauri_rules', $rules);
151
    drupal_set_message('Rule deleted');
152
  } else {
153
    drupal_set_message('Cannot delete rule (invalid rule id)', 'error');
154
  }
155
  return 'admin/settings/cdm_mediauri';
156
}
157

  
158
/**
159
 * Menu callback; confirm deletion of mediauri rule.
160
 */
161
function cdm_mediauri_rule_delete($rid = 0) {
162
  $rules = cdm_mediauri_rules();
163
  $form['rid'] = array('#type' => 'hidden', '#value' => $rid);
164

  
165
  return confirm_form($form, t('Are you sure you want to delete the following rule:'.cdm_mediauri_overview(array($rules[$rid]))), 'admin/settings/cdm_mediauri', '', t('Delete'), t('Cancel'));
166
}
167

  
168

  
169

  
170
function cdm_mediauri_rule_edit_form($rid = 'new'){
171

  
172
  if($rid == 'new'){
173
    // ---- little code snipped used to import default rules during development 
174
    $default_rule_id = FALSE; // 0 or 1 or FALSE
175
    
176
    if($default_rule_id !== false){
177
      $default_rules = cdm_mediauri_rules(true);
178
      $rule = $default_rules[$default_rule_id];
179
    }
180
    // --- END
181
  
182
    if(!$rule) {
183
      $rule = array();
184
      $rule['type'] = 'empty';
185
      $rule['pattern'] = '';
186
      $rule['replace'] = '';
187
      $rule['prefix'] = ''; 
188
      $rule['affix'] = '';
189
      $rule['url_encode'] = 0; 
190
      $rule['embed_html'] = 0;
191
    }
192
  } else {
193
    $rules = cdm_mediauri_rules(); 
194
    $rule = $rules[$rid];
195
  }
196
  
197
  $form = array();
198

  
199
  /*
200
   * rule
201
   * 
202
   *  ['type']: values(preview, webapp, media)
203
   *  ['size_x'], ['size_y']: dimensions of preview image in px
204
   *  ['pattern']: php PCRE regex pattern (see: http://www.php.net/manual/reference.pcre.pattern.syntax.php)
205
   *  ['replace']: replacement string 
206
   *  ['prefix']: string by which the converted url is to be prefixed
207
   *  ['affix']: string by which the converted url is to be affixed
208
   *  ['url_encode']: values(TRUE, FALSE) if the converted url is to be url encoded
209
   *  ['embed_html']: embes the construct of prefix + url + affix into the page as plain html
210
   * 
211
   */
212
  $form['rid'] = array(
213
    '#type' => 'hidden',
214
    '#value' => $rid,
215
   );
216
  
217
  $form['rule'] = array(
218
    '#type' => 'fieldset',
219
    '#title' => ($rule['type'] == 'empty' ? t('New Rule') : t('Rule '.$rid)),
220
    '#tree' => TRUE,
221
  );
222
  
223
  $form['rule']['type'] =  array(
224
    '#type'  => 'select',
225
    '#title' => t('Type'),
226
    '#default_value' => $rule['type'],
227
    '#options' => array(
228
        'preview' => t('Preview Quality'),
229
        'webapp' => t('Web Application'),
230
        ),
231
    '#description'   => t('The type media representation.'),
232
  );
233
  
234
   $form['rule']['pattern'] =  array(
235
    '#type' => 'textarea',
236
    '#title'         => t('Pattern'),
237
    '#description'   => t('php PCRE regex pattern (see: ').l('php manual', 'http://www.php.net/manual/reference.pcre.pattern.syntax.php').')',
238
    '#default_value' => $rule['pattern'],
239
    '#rows' => 1
240
  );
241
  
242
  $form['rule']['replace'] =  array(
243
    '#type' => 'textarea',
244
    '#title'         => t('Replacement'),
245
    '#default_value' => $rule['replace'],
246
    '#rows' => 1,
247
   );
248
  
249
  $form['rule']['url_encode'] =  array(
250
    '#type' => 'checkbox',
251
    '#title'         => t('Url Encode'),
252
    '#default_value' => $rule['url_encode'],
253
    '#description'   => t('Whether the converted url is to be url encoded')
254
   );
255
  
256
  //  ['embed_html'] = 1;
257
  
258
  $form['rule']['embed_html'] =  array(
259
    '#type' => 'checkbox',
260
    '#title'         => t('Embed'),
261
    '#default_value' => $rule['embed_html'],
262
    '#description'   => t('Embed the construct of prefix + url + affix into the page as plain html')
263
   );
264
  
265
   
266
  $form['rule']['prefix'] =  array(
267
    '#type' => 'textarea',
268
    '#title'   => t('Prefix'),
269
    '#default_value' => $rule['prefix'],
270
    '#rows' => 1,
271
  );
272
  
273
    $form['rule']['affix'] =  array(
274
    '#type' => 'textarea',
275
    '#title'   => t('Affix'),
276
    '#default_value' => $rule['affix'],
277
    '#rows' => 1,
278
  );
279
  
280
   $form['rule']['size_x'] =  array(
281
    '#type' => 'textfield',
282
    '#title'         => t('Preview Image Width'),
283
    '#default_value' => $rule['size_x'],
284
    '#disabled' => $rule['type'] != 'preview',
285
    '#description'   => t('Width of preview image in px'),
286
    '#size' => 4, 
287
  );
288
  
289
  $form['rule']['size_y'] =  array(
290
    '#type' => 'textfield',
291
    '#title'         => t('Preview Image Height'),
292
    '#default_value' => $rule['size_y'],
293
    '#disabled' => $rule['type'] != 'preview',
294
    '#description'   => t('Height of preview image in px'),
295
    '#size' => 4, 
296
  );
297
    
298
  return $form;
299
}
300

  
301

  
302

  
303
function cdm_mediauri_rules($default = false){
304
  /*
305
   * rules
306
   * 
307
   *  ['type']: values(preview, webapp, media)
308
   *  ['size_x'], ['size_y']: dimensions of preview image in px
309
   *  ['pattern']: php PCRE regex pattern (see: http://www.php.net/manual/reference.pcre.pattern.syntax.php)
310
   *  ['replace']: replacement string 
311
   *  ['prefix']: string by which the converted url is to be prefixed
312
   *  ['affix']: string by which the converted url is to be affixed
313
   *  ['url_encode']: values(TRUE, FALSE) if the converted url to be url encoded
314
   *  ['embed_html']: embes the construct of prefix + url + affix into the page as plain html
315
   * 
316
   */
317
  if($default){
318
  // default rules
319
  $rule1 = array();
320
  $rule1['type'] = 'preview';
321
  $rule1['pattern'] = '/^(http:\/\/ww2.bgbm.org\/herbarium\/images(?:\/\w+)(?:\/\d+){4})(\/)(.*)$/';
322
  $rule1['replace'] = '$1/thumbs/$3';
323
  $rule1['size_x'] = 125;
324
  $rule1['size_Y'] = 200;
325
  
326
  $rule2 = array();
327
  $rule2['type'] = 'webapp';
328
  $rule2['pattern'] = '/^http:\/\/ww2.bgbm.org\/herbarium\/images\/((?:\w+\/)(?:\d+\/){4}[\w_]+?\.)(.*)$/';
329
  $rule2['replace'] = '$1fpx';
330
  $rule2['prefix'] = '<embed width="490" height="700" align="middle" type="application/x-shockwave-flash" 
331
       pluginspage="http://www.macromedia.com/go/getflashplayer" name="FSIViewer" 
332
       bgcolor="black" quality="high" play="true" 
333
       src="http://ww2.bgbm.org/fsi/fsi.swf?cfg=plugin2&FPXSrc='; 
334
  $rule2['affix'] = '&FPXWidth=2801&FPXHeight=4478&Effects=qlt%3D85&InitialView=&Measure_ImageWidth=270.0&Measure_Suffix=%20mm"/>';
335
  $rule2['url_encode'] = 1; 
336
  $rule2['embed_html'] = 1;
337
    return array($rule1, $rule2);
338
  
339
  } else {
340
    return variable_get('cdm_mediauri_rules', array());
341
  }
342
  
343
}
344

  
345
/**
346
 * Converts a media uri according to all matching conversion rules which are currently defined.
347
 * 
348
 * Format of a returned array element:
349
 * 
350
 *  $return_array[{type}] { // values for {type}:(preview, webapp, media, original)
351
 *      ['size_x'],         // dimensions of preview image in px
352
 *      ['size_y'],         // dimensions of preview image in px
353
 *      ['uri']             //the uri 
354
 *  }
355
 * 
356
 * @return returns an associative array of the original media uri and all convered 
357
 *    whereas the rule type becomes the array key of the array entries.
358
 *    All uris are again packed into associative arrays by means to submit 
359
 *    additional information as defined in the rules applied. 
360
 */
361
function cdm_mediauri_conversion($mediaUri){
362
  
363
  $rules = cdm_mediauri_rules();
364
  
365
  $muris = array();
366
  $muris['original'] = array('uri' => $mediaUri, 'size_x'=>null, 'size_y'=>null);
367
  
368
  foreach($rules as $rule){
369
      $uri_converted = preg_replace($rule['pattern'], $rule['replace'], $mediaUri);
370
      if($uri_converted != $mediaUri){
371
        if(isset($rule['url_encode']) && $rule['url_encode']){
372
          $uri_converted = urlencode($uri_converted);
373
        }
374
        $muris[$rule['type']] = array('uri'=>$uri_converted, 'size_x'=>$rule['size_x'], 'size_y'=>$rule['size_y']);
375
      }
376
  }
377
  return $muris;
378
}

Also available in: Unified diff