summaryrefslogtreecommitdiffstats
path: root/flashgame.module
blob: fa47c5d1502f9a9d3865e048cbc8b0e7dde2eaa0 (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
<?php
/**
 * @file
 * A module for flash game websites.
 */

/**
 * Implements hook_menu().
 */
function flashgame_menu() {
  $items = array();
  $items['admin/config/media/flashgame'] = array(
    'title' => 'Flash Games setting',
    'description' => 'Configuration for Flashgames',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('flashgame_admin_settings'),
    'access arguments' => array('administer site configuration'),
  );
  return $items;
}

/**
 * Form generation function for flashgame config page.
 */
function flashgame_admin_settings($form, &$form_state) {
  $form = array();
  $types = node_type_get_names();
  $form['flashgame_nodetypes'] = array(
    '#type' => 'select',
    '#title' => t('Content types'),
    '#description' => t('Select content types for miniclip games'),
    '#options' => $types,
    '#multiple' => TRUE,
    '#default_value' => variable_get('flashgame_nodetypes'),
  );
  return system_settings_form($form);
}

/**
 * Implements hook_preprocess_node().
 */
function flashgame_preprocess_node(&$variables) {
  $types = variable_get('flashgame_nodetypes');
  $cur_type = $variables['type'];
  if ($types) {
    foreach ($types as $type) {
      if ($cur_type == $type) {
        drupal_add_js('http://www.miniclip.com/js/game-embed.js', 'external');
      }
    }
  }
}

/**
 * Implements hook_field_formatter_info().
 */
function flashgame_field_formatter_info() {
  return array(
    'miniclip' => array(
      'label' => t('Miniclip game'),
      'field types' => array('text'),
    ),
    'flash' => array(
      'label' => t('Flash'),
      'description' => t('For show as a flash file'),
      'field types' => array('file'),
      'settings' => array(
        'width' => 640,
        'height' => 480,
      ),
    ),
  );	
}

/**
 * Implements hook_field_formatter_settings_form().
 */
function flashgame_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $element = array(
    'width' => array(
      '#title' => t('width'),
      '#type' => 'textfield',
      '#size' => 20,
      '#default_value' => 640,
    ),
    'height' => array(
      '#title' => t('height'),
      '#type' => 'textfield',
      '#size' => 20,
      '#default_value' => 480,
    ),
  );
  return $element;
}

/**
 * Implements hook_field_formatter_settings_summary().
 */
function flashgame_field_formatter_settings_summary($field, $instance, $view_mode) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $summary = t('Height:@height | Width:@width', array(
    '@height'	=> $settings['height'],
    '@width'	=> $settings['width'],
    )
  );
  return $summary;
}

/**
 * Implements hook_field_formatter_view().
 */
function flashgame_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $settings = $display['settings'];
  switch ($display['type']) {
    case 'miniclip':
      foreach ($items as $delta => $item) {
        $game = array();
        $game = _flashgame_split('/', $item['value']);
        $index = count($game) - 2;
        $embed = '<div id="miniclip-game-embed" data-game-name=' . $game[$index] . ' data-theme="1" data-width='
        . $settings['width'] . ' data-height=' . $settings['height'] . '></div>';
        $element[$delta] = array('#markup' => $embed);
      }
      break;

    case 'flash':
      foreach ($items as $delta => $item) {
        $url = empty($item['uri']) ? '' : file_create_url($item['uri']);
        $embed = '<embed src = "' . $url . '" width = "' . $settings['width'] . '" height = "' . $settings['height'] . '" />';
        $element[$delta] = array('#markup' => $embed);
      }
      break;

  }
  return $element;
}

/**
 * A helper function for split strings by a given character.
 */
function _flashgame_split($s, $str) {
  $ret = array();
  $tmp = '';
  for ($i = 0; $i < drupal_strlen($str); $i++) {
    $char = drupal_substr($str, $i, 1);
    if ($char == $s) {
      $ret[] = $tmp;
      $tmp = '';
    }
    else {
      $tmp .= $char;
    }
  }
  if ($tmp) {
    $ret[] = $tmp;
  }
  return $ret;
}