summaryrefslogtreecommitdiffstats
path: root/scoutle.module
blob: d9203984db1ea3c1001f8a6003f89fd9f56f48d3 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php

/**
 * @file
 * Module for supporting the Scoutle social networking service.
 */

/**
 *  Copyright (C) 2009  Yoo Chung <http://chungyc.org/>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/** 
 * Default stage type for Scoutle.
 */
define('SCOUTLE_STAGE', 'dynamic180');

/**
 * The type of host system reported to Scoutle.
 */
define('SCOUTLE_HOST', 'drupal');

/**
 * Help text for the module.
 * @return HTML help text for the scoutle module.
 */
function scoutle_help($path, $arg) {
  switch ($path) {
    case 'admin/help#scoutle':
      return '<p>' . t('Support for the <a href="http://www.scoutle.com/">Scoutle</a> social networking service. It enables the use of Scoutle stages that can be embedded in websites which connect blogs to other related blogs.  In order to embed a stage, the website has to be registered with a scout on <a href="http://www.scoutle.com/">Scoutle</a>, which will give you the scout hash necessary for configuring the stage.') . '</p>';
    case 'admin/settings/scoutle':
      return '<p>' . t('Configure the stage for Scoutle.  The scout hash should be available at <a href="http://www.scoutle.com/">Scoutle</a> if this website has been registered.') . '</p>';
  }
}

/**
 * Valid permissions for the module.
 * @return An array of valid permissions for the scoutle module.
 */
function scoutle_permission() {
  return array(
    'administer scoutle' => array(
      'title' => t('Administer Scoutle'),
      'description' => t('Administer the settings for the Scoutle stage.'),
    ),
    'access scoutle stage' => array(
      'title' => t('Access Scoutle stage'),
      'description' => t('View the Scoutle stage.'),
    ),
  );
}

/**
 * Return the blocks available from the scoutle module.
 * @return available blocks
 */
function scoutle_block_info() {
  $blocks['stage'] = array(
    'info' => t('Scoutle stage'),
    'cache' => DRUPAL_CACHE_GLOBAL,
  );

  return $blocks;
}

/**
 * Generate HTML for a Scoutle block.
 * @param delta the block to return
 * @return a Scoutle block
 */
function scoutle_block_view($delta = '') {
  if ($delta != 'stage' || !user_access('access scoutle stage')) {
    return NULL;
  }

  $hash = urlencode(variable_get('scoutle_hash', ''));
  $stage = urlencode(variable_get('scoutle_stage', SCOUTLE_STAGE));
  $host = SCOUTLE_HOST; // identifies type of host to Scoutle
  $message = t('Connect with me at Scoutle.com'); // for missing JavaScript

  if ($hash == '') {
    return NULL; // do not output widget
  }

  $block['subject'] = '';
  if ($stage == 'static') {
    $block['content'] = <<<EOT
<a href="http://www.scoutle.com/incoming/index.php?stash={$hash}"><img src="http://www.scoutle.com/stageload/loadv2.php?stash={$hash}&amp;st={$stage}&amp;host={$host}" border="0" title="{$message}" alt="{$message}" /></a>
EOT;
  } else {
    $block['content'] = <<<EOT
<script type="text/javascript" src="http://www.scoutle.com/stageload/loadv2.php?stash={$hash}&amp;st={$stage}&amp;host={$host}"></script><noscript><div><a href="http://www.scoutle.com/incoming/index.php?stash={$hash}">{$message}</a></div></noscript>
EOT;
  }

  return $block;
}

/**
 * Form for configuring the scoutle module.
 * @return Form for configuring the settings.
 */
function scoutle_admin() {
  $stage_options = array(
    'dynamic180' => t('Dynamic stage (180x240)'),
    'dynamic125' => t('Dynamic stage (125x220)'),
    'classic' => t('Classic stage'),
    'mini' => t('Mini stage'),
    'static' => t('Static stage (not recommended)'),
  );

  $form['scoutle_hash'] = array(
    '#type' => 'textfield',
    '#title' => t('Scout Hash'),
    '#default_value' => variable_get('scoutle_hash', ''),
    '#size' => 32,
    '#maxlength' => 32,
    '#description' => t('The hash associated with the Scoutle scout.'),
    '#required' => TRUE,
  );

  $form['scoutle_stage'] = array(
    '#type' => 'radios',
    '#title' => t('Type of stage'),
    '#options' => $stage_options,
    '#default_value' => variable_get('scoutle_stage', SCOUTLE_STAGE),
    '#description' => t('The type of stage to embed within this website.'),
    '#required' => TRUE,
  );

  return system_settings_form($form);
}

/**
 * Validate form input when configuring settings for the scoutle module.
 * @see scoutle_admin()
 */
function scoutle_admin_validate($form, &$form_state) {
  $hash = $form_state['values']['scoutle_hash'];
  $stage = $form_state['values']['scoutle_stage'];

  if (strlen($hash) != 32 || !preg_match('/^([a-f]|[0-9])*$/', $hash)) {
    form_set_error('scoutle_hash', t('You must select a valid hash associated with a scout.'));
  }

  if (!in_array($stage, array('dynamic180', 'dynamic125', 'classic', 'mini', 'static'))) {
    form_set_error('scoutle_stage', t('You must set a valid type for the stage.'));
  }
}

/**
 * Defines the menu settings for the scoutle module.
 * @return the menu settings.
 */
function scoutle_menu() {
  $items = array();

  $items['admin/config/services/scoutle'] = array(
    'title' => 'Scoutle',
    'description' => 'Configure the settings for the Scoutle stage.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('scoutle_admin'),
    'access arguments' => array('administer scoutle'),
    'type' => MENU_NORMAL_ITEM,
  );

  return $items;
}