Projet

Général

Profil

Paste
Télécharger (22,5 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / sweaver / sweaver.module @ 87dbc3bf

1
<?php
2

    
3
/**
4
 * @file
5
 * Sweaver functions.
6
 */
7

    
8
/**
9
 * Default paths to exclude the frontend editor from.
10
 */
11
define('SWEAVER_PATHS_EXCLUDE', "admin*\nnode/add*\nnode/*/edit\nnode/*/panel*\npanels/ajax*\nuser/*/edit\nbatch*\nwysiwyg_imageupload/*\nmedia/*\ntaxonomy/*\nimce");
12

    
13
/**
14
 * Default selectors to exclude from selecting.
15
 */
16
define('SWEAVER_SELECTORS_EXCLUDE', "#sweaver-frontend\n#admin-menu\n.colorpicker\n#sweaver-messages\n#sweaver-popup\n#follow-link\n#toolbar\n#l10n-client");
17

    
18
/**
19
 * Default classes to exclude from not being themed.
20
 */
21
define('SWEAVER_CLASSES_EXCLUDE', "clear\nclearfix\nsweaver\nsweaver-hovered\nsweaver-clicked\nsweaver-clicked-temp");
22

    
23
/**
24
 * Sweaver default properties that can be used in css extending the default from CTools.
25
 */
26
define('SWEAVER_CTOOLS_ALLOWED_PROPERTIES', "-moz-border-radius\n-webkit-border-radius\nborder-top-right-radius\nborder-bottom-right-radius\nborder-bottom-left-radius\nborder-top-left-radius\nwebkit-border-top-right-radius\nwebkit-border-bottom-right-radius\nwebkit-border-bottom-left-radius\nwebkit-border-top-left-radius\nmoz-border-radius-topright\nmoz-border-radius-bottomright\nmoz-border-radius-bottomleft\nmoz-border-radius-topleft\nposition\ntop\nleft\nbottom\nright\nborder-collapse\nborder-spacing\nlist-style-image\nlist-style-type\nmin-width\nmax-width\nmin-height\nmax-height\nvisibility");
27

    
28
/**
29
 * Default skin.
30
 */
31
define('SWEAVER_SKIN', "default");
32

    
33
/**
34
 * Require sweaver.inc
35
 */
36
require_once('sweaver.inc');
37

    
38
/**
39
 * Implements hook_permission().
40
 */
41
function sweaver_permission() {
42
  return array(
43
    'configure sweaver' => array(
44
      'title' => t('Configure the editor, selectors, properties, types and other plugins.')
45
    ),
46
    'use editor' => array(
47
      'title' => t('Use the front end editor.'),
48
    ),
49
  );
50
}
51

    
52
/**
53
 * Implements hook_menu().
54
 */
55
function sweaver_menu() {
56
  module_load_include('inc', 'sweaver', 'sweaver.registry');
57
  return _sweaver_menu();
58
}
59

    
60
/**
61
 * Implements hook_theme().
62
 */
63
function sweaver_theme() {
64
  module_load_include('inc', 'sweaver', 'sweaver.registry');
65
  return _sweaver_theme();
66
}
67

    
68
/**
69
 * Implements hook_ctools_plugin_api().
70
 */
71
function sweaver_ctools_plugin_api($owner, $api) {
72
  if ($owner == 'sweaver' && ($api == 'sweaver' || $api == 'plugins')) {
73
    return array('version' => 1);
74
  }
75
}
76

    
77
/**
78
 * Implements hook_ctools_plugin_type().
79
 */
80
function sweaver_ctools_plugin_type() {
81
  return array(
82
    'plugins' => array(
83
      'cache' => TRUE,
84
      'use hooks' => TRUE,
85
      'classes' => array('handler'),
86
    ),
87
  );
88
}
89

    
90
/**
91
 * Implements hook_default_sweaver_selector().
92
 */
93
function sweaver_default_sweaver_selector() {
94
  module_load_include('inc', 'sweaver', 'sweaver.registry');
95
  return _sweaver_default_sweaver_selector();
96
}
97

    
98
/**
99
 * Implements hook_default_sweaver_property().
100
 */
101
function sweaver_default_sweaver_property() {
102
  module_load_include('inc', 'sweaver', 'sweaver.registry');
103
  return _sweaver_default_sweaver_property();
104
}
105

    
106
/**
107
 * Implements hook_default_sweaver_type().
108
 */
109
function sweaver_default_sweaver_type() {
110
  module_load_include('inc', 'sweaver', 'sweaver.registry');
111
  return _sweaver_default_sweaver_type();
112
}
113

    
114
/**
115
 * Implements hook_sweaver_plugins().
116
 */
117
function sweaver_sweaver_plugins() {
118
  module_load_include('inc', 'sweaver', 'sweaver.registry');
119
  return _sweaver_sweaver_plugins();
120
}
121

    
122
/**
123
 * CTools selectors list callback for bulk export.
124
 */
125
function sweaver_ctools_selectors_list() {
126
  return sweaver_ctools_object_list('selector');
127
}
128

    
129
/**
130
 * CTools properties list callback for bulk export.
131
 */
132
function sweaver_ctools_properties_list() {
133
  return sweaver_ctools_object_list('property');
134
}
135

    
136
/**
137
 * CTools types list callback for bulk export.
138
 */
139
function sweaver_ctools_types_list() {
140
  return sweaver_ctools_object_list('type');
141
}
142

    
143
/**
144
 * Helper function to return list for CTools.
145
 */
146
function sweaver_ctools_object_list($object_type, $export_type = 2, $parent_check = FALSE) {
147
  $list = array();
148
  $objects = sweaver_object_load(NULL, $object_type, 'enabled');
149
  foreach ($objects as $object) {
150
    if ($object->export_type != $export_type) {
151

    
152
      // Parent check.
153
      if ($parent_check) {
154
        if ($object->property_type == 'parent') {
155
          continue;
156
        }
157
        elseif (!empty($object->property_parent)) {
158
          $object->description = $objects[$object->property_parent]->description .': '. $object->description;
159
        }
160
      }
161
      $list[$object->name] = $object->description;
162
    }
163
  }
164
  return $list;
165
}
166

    
167
/**
168
 * CTools selector export function.
169
 */
170
function sweaver_export_sweaver_selector($object) {
171
  return sweaver_export_sweaver_object($object, 'selector');
172
}
173

    
174
/**
175
 * CTools property export function.
176
 */
177
function sweaver_export_sweaver_property($object) {
178
  return sweaver_export_sweaver_object($object, 'property');
179
}
180

    
181
/**
182
 * CTools property export function.
183
 */
184
function sweaver_export_sweaver_type($object) {
185
  return sweaver_export_sweaver_object($object, 'type');
186
}
187

    
188
/**
189
 * CTools object export function.
190
 */
191
function sweaver_export_sweaver_object($object, $object_type) {
192
  ctools_include('export');
193
  sweaver_export_check_serialized_keys($object);
194
  $output = ctools_export_object('sweaver_'. $object_type, $object, '  ');
195
  return $output;
196
}
197

    
198
/**
199
 * Check if we have a serialized key. If so convert it to an array.
200
 *
201
 * @param $object
202
 *   A sweaver object.
203
 */
204
function sweaver_export_check_serialized_keys($object) {
205
  if (isset($object->table)) {
206
    $object_type = str_replace('sweaver_', '', $object->table);
207
    $key = $object_type .'_options';
208
    if (isset($object->{$key}) && !empty($object->{$key}) && is_string($object->{$key})) {
209
      $object->{$key} = unserialize($object->{$key});
210
    }
211
  }
212
}
213

    
214
/**
215
 * Load one object or all objects.
216
 *
217
 * @param $name
218
 *   The machine name of the object.
219
 * @param $map
220
 *   Can be an array passed on by the load arguments of the menu or a string.
221
 *   In case of array, object_type will be in $map[6]
222
 * @param $status
223
 *   Whether to return all objects or only enabled.
224
 * @return
225
 *   One object or an array of objects.
226
 */
227
function sweaver_object_load($name = NULL, $map = NULL, $status = 'enabled') {
228

    
229
  static $run = FALSE;
230
  static $objects = NULL;
231
  $object_type = is_array($map) ? $map[6] : $map;
232

    
233
  if (!$run) {
234

    
235
    if ($objects_cache = cache_get('sweaver')) {
236
      $objects = $objects_cache->data;
237
    }
238
    else {
239
      ctools_include('export');
240
      $objects = new stdClass;
241
      foreach (array('sweaver_selector', 'sweaver_property', 'sweaver_type') as $object_to_load) {
242
        $loaded_objects = ctools_export_load_object($object_to_load);
243
        foreach ($loaded_objects as $key => $object) {
244
          $object_type_key = str_replace('sweaver_', '', $object->table);
245
          if (!isset($object->disabled)) {
246
            $object->disabled = FALSE;
247
          }
248
          if ($object->disabled == FALSE) {
249
            $objects->{$object_type_key}->enabled[$key] = $object;
250
          }
251
          $objects->{$object_type_key}->all[$key] = $object;
252
        }
253
      }
254

    
255
      // Let sweaver plugins alter objects.
256
      $sweaver = Sweaver::get_instance();
257
      foreach (array_keys($sweaver->get_plugins_registry(TRUE)) as $plugin_name) {
258
        $sweaver_plugin = $sweaver->get_plugin($plugin_name);
259
        $sweaver_plugin->sweaver_objects_alter($objects);
260
      }
261

    
262
      cache_set('sweaver', $objects);
263
    }
264
    $run = TRUE;
265
  }
266

    
267
  if ($name) {
268
    return isset($objects->{$object_type}->all[$name]) ? $objects->{$object_type}->all[$name] : FALSE;
269
  }
270
  else {
271
    return isset($objects->{$object_type}->{$status}) ? $objects->{$object_type}->{$status} : array();
272
  }
273
}
274

    
275
/**
276
 * Load all inclusive selectors defined in the theme .info
277
 * 
278
 * @return
279
 *   An array of selectors
280
 */
281
function sweaver_load_theme_selectors() {
282
  $selectors = array();
283
  if (variable_get('sweaver_selectors_source', false)) {
284
    $theme_key = Sweaver::get_instance()->get_theme_key();
285
    $theme_info = sweaver_get_theme_info($theme_key);
286
    if (isset($theme_info['sweaver']['selectors'])) {
287
      foreach ($theme_info['sweaver']['selectors'] as $selector_selector => $selector_description) {
288
        if (is_array($selector_description)) {
289
          if (!isset($selector_description['type'])) {
290
            $selector_description['type'] = 'add';
291
          }
292
          if ($selector_description['type'] == 'add') {
293
            // in this case this is a selector that needs to be added to others defined in regestry
294
            $selector = new stdClass;
295

    
296
            // Major properties
297
            $selector->api_version = 1;
298
            $selector->disabled = false;
299
            $selector->export_type = 2;
300

    
301
            if (isset($selector_description['name'])) {
302
              $name = $selector_description['name'];
303
            }
304
            else {
305
              $name = str_replace(array(
306
                '',
307
                '.',
308
                '-'), array(''), $selector_selector);
309
            }
310
            $selector->name = $name;
311
            $selector->description = isset($selector_description['description']) ? $selector_description['description'] :
312
              $selector->name;
313
            $selector->selector_selector = $selector_selector;
314

    
315
            if (isset($selector_description['selector_highlight'])) {
316
              if ($selector_description['selector_highlight']) {
317
                $selector->selector_highlight = true;
318
              }
319
              else {
320
                $selector->selector_highlight = false;
321
              }
322
            }
323
            else {
324
              $selector->selector_highlight = false;
325
            }
326

    
327
            if (isset($selector_description['weight'])) {
328
              $selector->weight = $selector_description['weight'];
329
            }
330
            $selectors[$selector->name] = $selector;
331
          }
332
        }
333
      }
334
    }
335
  }
336
  return $selectors;
337
}
338

    
339
/**
340
 * Get all plugins or a plugin handler.
341
 * Deprecated. Only here for backward compatibility.
342
 *
343
 * @param $plugin_name
344
 *   The name of the plugin.
345
 * @param $enabled
346
 *   Whether the plugin is enabled or not.
347
 * @return
348
 *   Either a list of (enabled) plugins or a(n) (enabled) loaded plugin class.
349
 */
350
function sweaver_get_plugin($plugin_name = NULL, $enabled = FALSE) {
351
  $sweaver = Sweaver::get_instance();
352
  if ($plugin_name == NULL) {
353
    return $sweaver->get_plugins_registry($enabled);
354
  }
355
  else {
356
    return $sweaver->get_plugin($plugin_name, $enabled);
357
  }
358
}
359

    
360
/**
361
 * Implements hook_init().
362
 */
363
function sweaver_init() {
364

    
365
  // We use the drupal_static here instead of a session, because
366
  // pressflow doesn't allow us to start a session without breaking
367
  // the page cache.
368
  $load_style = &drupal_static('load_style', TRUE);
369
  if (sweaver_show_editor()) {
370

    
371
    $sweaver = Sweaver::get_instance();
372

    
373
    $load_style = FALSE;
374
    $inline_js_settings = array('sweaver' => array());
375
    $skin = variable_get('sweaver_skin', SWEAVER_SKIN);
376

    
377
    drupal_add_js(drupal_get_path('module', 'sweaver') .'/sweaver_plugin.js');
378
    drupal_add_css(drupal_get_path('module', 'sweaver') .'/skins/'. $skin .'/'. $skin .'.css');
379

    
380
    foreach (array_keys($sweaver->get_plugins_registry(TRUE)) as $plugin_name) {
381
      $sweaver_plugin = $sweaver->get_plugin($plugin_name);
382
      // Fire init.
383
      $sweaver_plugin->sweaver_init();
384
      // CSS and JS.
385
      $sweaver_plugin->sweaver_form_css_js($inline_js_settings);
386
    }
387

    
388
    // JS inline settings.
389
    drupal_add_js($inline_js_settings, 'setting');
390
  }
391
}
392

    
393
/**
394
 * Implements hook_custom_theme().
395
 */
396
function sweaver_custom_theme() {
397
  if (sweaver_show_editor() && $theme_key = sweaver_session(NULL, 'sweaver_theme')) {
398
    return Sweaver::get_instance()->get_theme_key();
399
  }
400
}
401

    
402
/**
403
 * Implements hook_preprocess_page().
404
 */
405
function sweaver_preprocess_page(&$vars) {
406
  // Load css for this theme if available.
407
  if (drupal_static('load_style')) {
408

    
409
    $sweaver = Sweaver::get_instance();
410

    
411
    // Extra check for the theme if we need to load css files.
412
    if (drupal_static('load_style')) {
413
      if (!empty($GLOBALS['theme'])) {
414
        $sweaver->set_current_style($GLOBALS['theme']);
415
      }
416
    }
417

    
418
    $sweaver_style = $sweaver->get_current_style();
419
    if (isset($sweaver_style->css) && drupal_static('load_style')) {
420
      $settings = array(
421
        'weight' => 1000,
422
        'preprocess' => TRUE,
423
        'group' => CSS_THEME,
424
      );
425
      $css_path = 'public://sweaver/sweaver_' . $sweaver->get_theme_key() . '_' . $sweaver_style->style_id . '_' . $sweaver_style->type . '.css';
426
      drupal_add_css($css_path, $settings);
427

    
428
      foreach (array_keys($sweaver->get_plugins_registry(TRUE)) as $plugin_name) {
429
        $sweaver_plugin = $sweaver->get_plugin($plugin_name);
430
        // Fire preprocess.
431
        $sweaver_plugin->sweaver_preprocess_page($vars);
432
      }
433
    }
434
  }
435
}
436

    
437
/**
438
 * Implements hook_page_alter().
439
 */
440
function sweaver_page_alter(&$page) {
441
  // We return the complete form in the page_bottom.
442
  if (sweaver_show_editor()) {
443
    $content = drupal_get_form('sweaver_frontend');
444
    $content['#suffix'] = '<div id="sweaver-messages"><div class="close">x</div><div class="message"></div></div>';
445
    $page['page_bottom']['sweaver'] = array(
446
      '#markup' => drupal_render($content),
447
    );
448
  }
449
}
450

    
451
/**
452
 * Sweaver editor containers.
453
 */
454
function sweaver_containers() {
455
  return array(
456
    'one' => t('Font'),
457
    'two' => t('Background'),
458
    'three' => t('Spacing'),
459
    'four' => t('Empty'),
460
  );
461
}
462

    
463
/**
464
 * Return all enabled themes.
465
 */
466
function sweaver_get_all_themes() {
467
  $themes = &drupal_static('all_themes', array());
468
  if (empty($themes)) {
469
    $themes = array();
470
    $results = db_query("SELECT filename, name, status, info FROM {system} WHERE type = 'theme' AND status = '1' ORDER BY name ASC")->fetchAll();
471
    foreach ($results as $theme) {
472
      if ($theme->status) {
473
        $theme->info = unserialize($theme->info);
474
        $theme->prefix = isset($theme->info['engine']) ? $theme->info['engine'] : NULL;
475
        $themes[$theme->name] = $theme;
476
      }
477
    }
478
  }
479
  return $themes;
480
}
481

    
482
/**
483
 * Get theme info.
484
 *
485
 * @param $theme
486
 *   The machine name for the theme.
487
 * @return $theme_info
488
 *   The info about a theme.
489
 */
490
function sweaver_get_theme_info($theme) {
491
  $themes = sweaver_get_all_themes();
492
  return $themes[$theme]->info;
493
}
494

    
495
/**
496
 * Theme an image. Mimics theme('image') but uses the alias => TRUE option.
497
 *
498
 * @return $image
499
 *   A themed image.
500
 */
501
function sweaver_theme_image($path, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) {
502
  if (!$getsize || (is_file($path) && (list($width, $height, $type, $image_attributes) = @getimagesize($path)))) {
503
    $attributes = drupal_attributes($attributes);
504
    $url = (url($path, array('alias' => TRUE)) == $path) ? $path : (base_path() . $path);
505
    return '<img src="'. check_url($url) .'" alt="'. check_plain($alt) .'" title="'. check_plain($title) .'" '. (isset($image_attributes) ? $image_attributes : '') . $attributes .' />';
506
  }
507
}
508

    
509
/**
510
 * Session handler for sweaver.
511
 *
512
 * @param $value
513
 *   The value hold by the session key.
514
 * @param $session_key
515
 *   Name of the session key.
516
 * @param $action
517
 *   Action to perform. TRUE will remove the session key.
518
 * @param $return_string
519
 *   Whether to return the value of the session key as string
520
 *   or simply return the value.
521
 * @return
522
 *   Either FALSE, a value from the key or nothing.
523
 */
524
function sweaver_session($value = NULL, $session_key = 'sweaver_editor_messages', $remove = FALSE, $return_string = TRUE) {
525
  // Add value to session key.
526
  if (isset($session_key) && isset($value) && !$remove) {
527
    if (!isset($_SESSION[$session_key])) $_SESSION[$session_key] = array();
528
    $_SESSION[$session_key][] = $value;
529
  }
530
  // Remove complete session key.
531
  elseif (isset($_SESSION[$session_key]) && !isset($value) && $remove) {
532
    if (isset($_SESSION[$session_key])) unset($_SESSION[$session_key]);
533
  }
534
  // Return values from session key.
535
  elseif (isset($_SESSION[$session_key])) {
536
    if ($return_string) {
537
      $return = FALSE;
538
      $values = $_SESSION[$session_key];
539
      if (count($values) > 1) {
540
        foreach ($values as $key => $value) {
541
          $return .= $value ."\n";
542
        }
543
      }
544
      elseif (isset($values[0])) {
545
        $return = $values[0];
546
      }
547
      return $return;
548
    }
549
    else {
550
      return $_SESSION[$session_key];
551
    }
552
  }
553
}
554

    
555
/**
556
 * Function to check if we are going to show the editor.
557
 */
558
function sweaver_show_editor($cache = TRUE, $plugins = TRUE) {
559
  static $run = FALSE;
560
  static $return = FALSE;
561

    
562
  $_return = $return;
563

    
564
  if (!$run || !$cache) {
565
    if ($cache) {
566
      $run = TRUE;
567
    }
568

    
569
    // Let's do the check for the editor early.
570
    if (variable_get('sweaver_enabled', TRUE)) {
571

    
572
      // Path visibility.
573
      $path = drupal_get_path_alias($_GET['q']);
574
      $page_match = drupal_match_path($path, variable_get('sweaver_paths', SWEAVER_PATHS_EXCLUDE));
575
      if ($path != $_GET['q']) {
576
        $page_match = $page_match || drupal_match_path($_GET['q'], variable_get('sweaver_paths', SWEAVER_PATHS_EXCLUDE));
577
      }
578
      $page_match = !(0 xor $page_match);
579
      if (user_access('use editor') && $page_match) {
580
        $_return = TRUE;
581
        if ($plugins) {
582
          $sweaver = Sweaver::get_instance();
583
          foreach (array_keys($sweaver->get_plugins_registry(TRUE)) as $plugin_name) {
584
            $sweaver_plugin = $sweaver->get_plugin($plugin_name);
585
            // Allow plugins to set editor to no show
586
            if ($sweaver_plugin->show_editor() === FALSE) {
587
              $_return = FALSE;
588
            }
589
          }
590
        }
591
      }
592
    }
593
  }
594

    
595
  if ($cache) {
596
    $return = $_return;
597
  }
598
  return $_return;
599
}
600

    
601
/**
602
 * Rock 'n' roll: the sweaver editor.
603
 */
604
function sweaver_frontend($form, &$form_state) {
605
  $form = array();
606
  $weight = 1;
607
  $form['#plugins'] = array();
608
  $form['#theme'] = 'sweaver_plugin';
609
  $form['#attributes'] = array('enctype' => 'multipart/form-data');
610
  $plugins_order = variable_get('sweaver_plugins_weight', array());
611

    
612
  $sweaver = Sweaver::get_instance();
613
  foreach ($sweaver->get_plugins_registry(TRUE) as $plugin_name => $plugin) {
614
    $sweaver_plugin = $sweaver->get_plugin($plugin_name);
615

    
616
    // Calculate weight.
617
    $default_weight = isset($plugins_order[$plugin_name]) ? $plugins_order[$plugin_name] : $weight++;
618
    if ($plugin_name == 'sweaver_plugin') $default_weight = -100;
619
    $form['#plugins_order'][$plugin_name] = $default_weight;
620
    $form['#plugins_full'][$plugin_name] = $plugin;
621

    
622
    // Form.
623
    $plugin_form = $sweaver_plugin->sweaver_form();
624
    if (!empty($plugin_form)) {
625
      $form[$plugin['name']]['form'] = $plugin_form;
626
      if (isset($plugin['tab'])) {
627
        $form[$plugin['name']]['#tab_name'] = isset($plugin['tab']) ? $plugin['tab'] : drupal_ucfirst($plugin_name);
628
        $form[$plugin['name']]['#tab_description'] = isset($plugin['tab_description']) ? $plugin['tab_description'] : '';
629
      }
630
    }
631
  }
632

    
633
  // Editor messages.
634
  $messages = sweaver_session();
635
  sweaver_session(NULL, 'sweaver_editor_messages', TRUE);
636
  $form['sweaver-editor-messages'] = array(
637
    '#type' => 'hidden',
638
    '#value' => trim($messages),
639
  );
640

    
641
  $form['#current_theme'] = $sweaver->get_theme_key();
642
  $form['destination'] = array(
643
    '#type' => 'hidden',
644
    '#value' => $_GET['q'],
645
  );
646

    
647
  return $form;
648
}
649

    
650
/**
651
 * Clear cache and page requisites.
652
 */
653
function sweaver_clear_cache() {
654
  cache_clear_all();
655
  drupal_clear_css_cache();
656
}
657

    
658
/**
659
 * Sweaver frontend submit.
660
 */
661
function sweaver_frontend_submit($form, &$form_state) {
662

    
663
  $sweaver = Sweaver::get_instance();
664
  foreach (array_keys($sweaver->get_plugins_registry(TRUE)) as $plugin_name) {
665
    $sweaver_plugin = $sweaver->get_plugin($plugin_name);
666
    // In the first version, we'll let everyone who has submit methods
667
    // simply do their stuff with the submitted values.
668
    $sweaver_plugin->sweaver_form_submit($form, $form_state);
669
  }
670

    
671
  // Redirect to same path. We issue this function so things like
672
  // session, current style and possible others are properly set.
673
  drupal_goto();
674
}
675

    
676
/**
677
 * Sweaver menu callback.
678
 *
679
 * @param $settings
680
 *   A collection of page arguments from the menu callback.
681
 * @return
682
 *   The rendered page - can be form or anything else.
683
 */
684
function sweaver_menu_callback($settings) {
685
  $plugin_name = $settings['plugin'];
686
  $callback_method = (isset($settings['callback_method'])) ? $settings['callback_method']: 'sweaver_menu_callback';
687
  $return_method = (isset($settings['return_method'])) ? $settings['return_method'] : 'drupal_get_form';
688
  $js = (isset($settings['js'])) ? TRUE : FALSE;
689

    
690
  if ($sweaver_plugin = Sweaver::get_instance()->get_plugin($plugin_name, TRUE)) {
691
    $output = $sweaver_plugin->$callback_method();
692
    if (!empty($output)) {
693
      if ($return_method == 'drupal_get_form') {
694
        return drupal_get_form('sweaver_menu_callback_form', $output, $plugin_name, $callback_method);
695
      }
696
      elseif (isset($js)) {
697
        exit(drupal_to_js($output));
698
      }
699
      elseif (!empty($return_method) && function_exists($return_method)) {
700
        return $return_method($output);
701
      }
702
      else {
703
        return $output;
704
      }
705
    }
706
  }
707

    
708
  return t('No page found or you do not have sufficient permissions to access the page.');
709
}
710

    
711
/**
712
 * Sweaver menu access callback.
713
 *
714
 * @param $args
715
 *   A collection of arguments.
716
 */
717
function sweaver_menu_access_callback($args) {
718

    
719
  $plugin_name = array_shift($args);
720
  $callback_access_method = array_shift($args);
721
  $callback_access_method = (!empty($callback_access_method)) ? $callback_access_method: 'sweaver_menu_access_callback';
722

    
723
  if ($sweaver_plugin = Sweaver::get_instance()->get_plugin($plugin_name, TRUE)) {
724
    return $sweaver_plugin->$callback_access_method($args);
725
  }
726

    
727
  return FALSE;
728
}
729

    
730
/**
731
 * Helper function to return the form passed by sweaver_menu_callback.
732
 *
733
 * @param $plugin_name
734
 *   Name of the plugin.
735
 * @param $callback_method
736
 *   Name of the method to call.
737
 * @return $form
738
 *   The rendered form.
739
 */
740
function sweaver_menu_callback_form($dform, $form_state, $form, $plugin_name, $callback_method) {
741
  $form['#plugin_name'] = $plugin_name;
742
  $form['#callback_method'] = $callback_method;
743
  return $form;
744
}
745

    
746
/**
747
 * Sweaver menu callback validate.
748
 */
749
function sweaver_menu_callback_form_validate($form, &$form_state) {
750
  $sweaver_plugin = Sweaver::get_instance()->get_plugin($form['#plugin_name']);
751
  $callback_validate_function = $form['#callback_method'] .'_validate';
752
  if (method_exists($sweaver_plugin, $callback_validate_function)) {
753
    $sweaver_plugin->$callback_validate_function($form, $form_state);
754
  }
755
}
756

    
757
/**
758
 * Sweaver menu callback form submit.
759
 */
760
function sweaver_menu_callback_form_submit($form, &$form_state) {
761
  $sweaver_plugin = Sweaver::get_instance()->get_plugin($form['#plugin_name']);
762
  $callback_submit_function = $form['#callback_method'] .'_submit';
763
  if (method_exists($sweaver_plugin, $callback_submit_function)) {
764
    $sweaver_plugin->$callback_submit_function($form, $form_state);
765
  }
766
}