| 1 |
<?php
|
| 2 |
// $Id: mobile_codes.module,v 1.1.2.4 2008/06/12 07:28:39 deciphered Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Mobile Codes core functions
|
| 7 |
*
|
| 8 |
* Mobile Codes core functions to be used by Mobile Codes filter and any future
|
| 9 |
* integration features.
|
| 10 |
*/
|
| 11 |
|
| 12 |
/**
|
| 13 |
* Include additional files
|
| 14 |
*/
|
| 15 |
module_load_include('filter.inc', 'mobile_codes');
|
| 16 |
|
| 17 |
/**
|
| 18 |
* Implementation of hook_theme().
|
| 19 |
*/
|
| 20 |
function mobile_codes_theme() {
|
| 21 |
return array(
|
| 22 |
'mobilecode' => array(
|
| 23 |
'arguments' => array(
|
| 24 |
'data' => NULL,
|
| 25 |
'arguments' => array(),
|
| 26 |
'format' => NULL,
|
| 27 |
),
|
| 28 |
),
|
| 29 |
);
|
| 30 |
}
|
| 31 |
|
| 32 |
/**
|
| 33 |
* Mobile Codes settings form.
|
| 34 |
*/
|
| 35 |
function mobile_codes_settings($format) {
|
| 36 |
$form['mobile_codes_type_'. $format] = array(
|
| 37 |
'#type' => 'select',
|
| 38 |
'#title' => t('Mobile Code type'),
|
| 39 |
'#options' => array(
|
| 40 |
'dm' => t('Datamatrix'),
|
| 41 |
'qr' => t('QR Code'),
|
| 42 |
),
|
| 43 |
'#default_value' => variable_get('mobile_codes_type_'. $format, 'dm'),
|
| 44 |
'#description' => t('Set the default Mobile Code type - <a href="http://en.wikipedia.org/wiki/Datamatrix">Datamatrix</a> or <a href="http://en.wikipedia.org/wiki/QR_Code">QR Code</a>.'),
|
| 45 |
);
|
| 46 |
|
| 47 |
$form['mobile_codes_data_'. $format] = array(
|
| 48 |
'#type' => 'select',
|
| 49 |
'#title' => t('Data type'),
|
| 50 |
'#options' => array(
|
| 51 |
'phone' => t('Phone Number'),
|
| 52 |
'text' => t('Text'),
|
| 53 |
'link' => t('URL'),
|
| 54 |
),
|
| 55 |
'#default_value' => variable_get('mobile_codes_data_'. $format, 'link'),
|
| 56 |
'#description' => t('Set the default Mobile Code data type.'),
|
| 57 |
);
|
| 58 |
|
| 59 |
$form['mobile_codes_size_'. $format] = array(
|
| 60 |
'#type' => 'select',
|
| 61 |
'#title' => t('Mobile Code size'),
|
| 62 |
'#options' => array(
|
| 63 |
's' => t('Small'),
|
| 64 |
'm' => t('Medium'),
|
| 65 |
'l' => t('Large'),
|
| 66 |
),
|
| 67 |
'#default_value' => variable_get('mobile_codes_size_'. $format, 'm'),
|
| 68 |
'#description' => t('Set the default Mobile Code size.'),
|
| 69 |
);
|
| 70 |
|
| 71 |
$form['mobile_codes_tinyurl_'. $format] = array(
|
| 72 |
'#type' => 'checkbox',
|
| 73 |
'#title' => t('Convert URLs to TinyURLs'),
|
| 74 |
'#default_value' => variable_get('mobile_codes_tinyurl_'. $format, 0),
|
| 75 |
'#description' => t('Set the default Mobile Code TinyURL behavior<br />
|
| 76 |
<strong>Note:</strong> URLs over 60 characters will always get converted to TinyURLs.'),
|
| 77 |
);
|
| 78 |
|
| 79 |
return $form;
|
| 80 |
}
|
| 81 |
|
| 82 |
/**
|
| 83 |
* Generate a mobile code
|
| 84 |
*/
|
| 85 |
function mobile_codes_generate($data, $arguments = array(), $format = '') {
|
| 86 |
if (!isset($arguments['data'])) {
|
| 87 |
$arguments['data'] = variable_get('mobile_codes_data_'. $format, 'link');
|
| 88 |
}
|
| 89 |
|
| 90 |
if (!isset($arguments['size'])) {
|
| 91 |
$arguments['size'] = variable_get('mobile_codes_size_'. $format, 'm');
|
| 92 |
}
|
| 93 |
|
| 94 |
if (!isset($arguments['type'])) {
|
| 95 |
$arguments['type'] = variable_get('mobile_codes_type_'. $format, 'dm');
|
| 96 |
}
|
| 97 |
|
| 98 |
$dir = '/mobile_codes/'. $arguments['type'] .'/'. $arguments['data'] .'/'. $arguments['size'];
|
| 99 |
|
| 100 |
$name = '';
|
| 101 |
if (!empty($arguments['name'])) {
|
| 102 |
$name = '-' . $arguments['name'];
|
| 103 |
}
|
| 104 |
else {
|
| 105 |
$arguments['name'] = '';
|
| 106 |
}
|
| 107 |
|
| 108 |
$file = file_directory_path() . $dir .'/'. urlencode($data) . $name .'.png';
|
| 109 |
$image_path = file_directory_path() . $dir .'/'. urlencode(urlencode($data)) . $name .'.png';
|
| 110 |
|
| 111 |
if (!file_exists(''. $file)) {
|
| 112 |
switch ($arguments['data']) {
|
| 113 |
case 'link': case 'text':
|
| 114 |
$mode = 'TEXT';
|
| 115 |
break;
|
| 116 |
|
| 117 |
case 'phone':
|
| 118 |
$mode = 'NUMBER';
|
| 119 |
break;
|
| 120 |
|
| 121 |
default:
|
| 122 |
$arguments['data'] = 'link';
|
| 123 |
$mode = 'TEXT';
|
| 124 |
break;
|
| 125 |
}
|
| 126 |
|
| 127 |
switch (drupal_strtolower($arguments['size'])) {
|
| 128 |
case 's': case 'sml': case 'small':
|
| 129 |
$arguments['size'] = array('dm' => '0.12', 'qr' => '2');
|
| 130 |
break;
|
| 131 |
|
| 132 |
case 'm': case 'med': case 'medium':
|
| 133 |
default:
|
| 134 |
$arguments['size'] = array('dm' => '0.18', 'qr' => '4');
|
| 135 |
break;
|
| 136 |
|
| 137 |
case 'l': case 'lrg': case 'large':
|
| 138 |
$arguments['size'] = array('dm' => '0.24', 'qr' => '6');
|
| 139 |
break;
|
| 140 |
}
|
| 141 |
|
| 142 |
switch ($arguments['type']) {
|
| 143 |
case 'dm':
|
| 144 |
default:
|
| 145 |
$text = array('data' => 'BARCODE', 'size' => 'X', 'margin_encoding' => '');
|
| 146 |
break;
|
| 147 |
|
| 148 |
case 'qr':
|
| 149 |
$text = array('data' => 'DATA', 'size' => 'MODULE_SIZE', 'margin_encoding' => '&MARGIN=2&ENCODING=BYTE');
|
| 150 |
break;
|
| 151 |
}
|
| 152 |
|
| 153 |
if (!isset($arguments['tinyurl'])) {
|
| 154 |
$arguments['tinyurl'] = variable_get('mobile_codes_tinyurl_'. $format, 0);
|
| 155 |
}
|
| 156 |
|
| 157 |
if ($arguments['data'] == 'link' && ($arguments['tinyurl'] == 1 || drupal_strlen($data) > 60)) {
|
| 158 |
$headers = array('Content-type' => 'application/x-www-form-urlencoded');
|
| 159 |
$tinyurl = drupal_http_request('http://tinyurl.com/api-create.php?url='. urlencode($data), $headers, 'POST', NULL);
|
| 160 |
if ($tinyurl->code == 200) {
|
| 161 |
$data = $tinyurl->data;
|
| 162 |
}
|
| 163 |
}
|
| 164 |
|
| 165 |
$url = 'http://mobilecodes.nokia.com/'. $arguments['type'] .'?'. $text['data'] .'='.
|
| 166 |
urlencode($data) .'&'. $text['size'] .'='. $arguments['size'][$arguments['type']]
|
| 167 |
.'&name='. $arguments['name'] . $text['margin_encoding'] .'&type='. $arguments['data']
|
| 168 |
.'&MODE='. $mode .'&a=create';
|
| 169 |
|
| 170 |
mobile_codes_directory_check($dir);
|
| 171 |
$image = drupal_http_request($url);
|
| 172 |
file_save_data($image->data, $file, FILE_EXISTS_REPLACE);
|
| 173 |
}
|
| 174 |
|
| 175 |
return theme('image', $image_path, $arguments['name'], $arguments['name'], array('class' => 'mobile_code'), FALSE);
|
| 176 |
}
|
| 177 |
|
| 178 |
/**
|
| 179 |
* Create directory if it does not exist.
|
| 180 |
*/
|
| 181 |
function mobile_codes_directory_check($directory) {
|
| 182 |
foreach (explode('/', $directory) as $dir) {
|
| 183 |
$dirs[] = $dir;
|
| 184 |
$dir = file_directory_path() .'/'. implode($dirs, '/');
|
| 185 |
if (!file_check_directory($dir, FILE_CREATE_DIRECTORY)) {
|
| 186 |
return FALSE;
|
| 187 |
}
|
| 188 |
}
|
| 189 |
|
| 190 |
return TRUE;
|
| 191 |
}
|
| 192 |
|
| 193 |
/**
|
| 194 |
* Theme function for displaying Mobile Codes
|
| 195 |
*/
|
| 196 |
function theme_mobilecode($data, $arguments = array(), $format = '') {
|
| 197 |
return mobile_codes_generate($data, $arguments, $format);
|
| 198 |
}
|