Fixing coding standards
[project/flashgame.git] / flashgame.module
1 <?php
2 /**
3 * @file
4 * A module for flash game websites.
5 */
6
7 /**
8 * Implements hook_menu().
9 */
10 function flashgame_menu() {
11 $items = array();
12 $items['admin/config/media/flashgame'] = array(
13 'title' => 'Flash Games setting',
14 'description' => 'Configuration for Flashgames',
15 'page callback' => 'drupal_get_form',
16 'page arguments' => array('flashgame_admin_settings'),
17 'access arguments' => array('administer site configuration'),
18 );
19 return $items;
20 }
21
22 /**
23 * Form generation function for flashgame config page.
24 */
25 function flashgame_admin_settings($form, &$form_state) {
26 $form = array();
27 $types = node_type_get_names();
28 $form['flashgame_nodetypes'] = array(
29 '#type' => 'select',
30 '#title' => t('Content types'),
31 '#description' => t('Select content types for miniclip games'),
32 '#options' => $types,
33 '#multiple' => TRUE,
34 '#default_value' => variable_get('flashgame_nodetypes'),
35 );
36 return system_settings_form($form);
37 }
38
39 /**
40 * Implements hook_preprocess_node().
41 */
42 function flashgame_preprocess_node(&$variables) {
43 $types = variable_get('flashgame_nodetypes');
44 $cur_type = $variables['type'];
45 if ($types) {
46 foreach ($types as $type) {
47 if ($cur_type == $type) {
48 drupal_add_js('http://www.miniclip.com/js/game-embed.js', 'external');
49 }
50 }
51 }
52 }
53
54 /**
55 * Implements hook_field_formatter_info().
56 */
57 function flashgame_field_formatter_info() {
58 return array(
59 'miniclip' => array(
60 'label' => t('Miniclip game'),
61 'field types' => array('text'),
62 ),
63 'flash' => array(
64 'label' => t('Flash'),
65 'description' => t('For show as a flash file'),
66 'field types' => array('file'),
67 'settings' => array(
68 'width' => 640,
69 'height' => 480,
70 ),
71 ),
72 );
73 }
74
75 /**
76 * Implements hook_field_formatter_settings_form().
77 */
78 function flashgame_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
79 $display = $instance['display'][$view_mode];
80 $settings = $display['settings'];
81 $element = array(
82 'width' => array(
83 '#title' => t('width'),
84 '#type' => 'textfield',
85 '#size' => 20,
86 '#default_value' => 640,
87 ),
88 'height' => array(
89 '#title' => t('height'),
90 '#type' => 'textfield',
91 '#size' => 20,
92 '#default_value' => 480,
93 ),
94 );
95 return $element;
96 }
97
98 /**
99 * Implements hook_field_formatter_settings_summary().
100 */
101 function flashgame_field_formatter_settings_summary($field, $instance, $view_mode) {
102 $display = $instance['display'][$view_mode];
103 $settings = $display['settings'];
104 $summary = t('Height:@height | Width:@width', array(
105 '@height' => $settings['height'],
106 '@width' => $settings['width'],
107 )
108 );
109 return $summary;
110 }
111
112 /**
113 * Implements hook_field_formatter_view().
114 */
115 function flashgame_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
116 $element = array();
117 $settings = $display['settings'];
118 switch ($display['type']) {
119 case 'miniclip':
120 foreach ($items as $delta => $item) {
121 $game = array();
122 $game = _flashgame_split('/', $item['value']);
123 $index = count($game) - 2;
124 $embed = '<div id="miniclip-game-embed" data-game-name=' . $game[$index] . ' data-theme="1" data-width='
125 . $settings['width'] . ' data-height=' . $settings['height'] . '></div>';
126 $element[$delta] = array('#markup' => $embed);
127 }
128 break;
129
130 case 'flash':
131 foreach ($items as $delta => $item) {
132 $url = empty($item['uri']) ? '' : file_create_url($item['uri']);
133 $embed = '<embed src = "' . $url . '" width = "' . $settings['width'] . '" height = "' . $settings['height'] . '" />';
134 $element[$delta] = array('#markup' => $embed);
135 }
136 break;
137
138 }
139 return $element;
140 }
141
142 /**
143 * A helper function for split strings by a given character.
144 */
145 function _flashgame_split($s, $str) {
146 $ret = array();
147 $tmp = '';
148 for ($i = 0; $i < drupal_strlen($str); $i++) {
149 $char = drupal_substr($str, $i, 1);
150 if ($char == $s) {
151 $ret[] = $tmp;
152 $tmp = '';
153 }
154 else {
155 $tmp .= $char;
156 }
157 }
158 if ($tmp) {
159 $ret[] = $tmp;
160 }
161 return $ret;
162 }