mobile_codes.module in Mobile Codes 5
Same filename and directory in other branches
Mobile Codes core functions
Mobile Codes core functions to be used by Mobile Codes filter and any future integration features.
File
mobile_codes.moduleView source
<?php
/**
* @file
* Mobile Codes core functions
*
* Mobile Codes core functions to be used by Mobile Codes filter and any future
* integration features.
*/
/**
* Include additional files
*/
include_once drupal_get_path('module', 'mobile_codes') . '/mobile_codes.filter.inc';
include_once drupal_get_path('module', 'mobile_codes') . '/mobile_codes.formatter.inc';
/**
* Implementation of hook_menu().
*/
function mobile_codes_menu($may_cache) {
$items = array();
if (!$may_cache) {
// Only include administrative callbacks if we are viewing an admin page.
if (arg(0) == 'admin') {
include_once drupal_get_path('module', 'mobile_codes') . '/mobile_codes.admin.inc';
}
}
if ($may_cache) {
$items[] = array(
'path' => 'admin/settings/mobile_codes',
'title' => 'Mobile Codes',
'callback' => 'mobile_codes_overview',
'access' => user_access('administer mobile codes'),
'file' => 'mobile_codes.admin.inc',
);
$items[] = array(
'path' => 'admin/settings/mobile_codes/presets',
'title' => 'Presets',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items[] = array(
'path' => 'admin/settings/mobile_codes/presets/list',
'title' => 'List',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items[] = array(
'path' => 'admin/settings/mobile_codes/presets/add',
'title' => 'Add preset',
'callback' => 'drupal_get_form',
'callback arguments' => array(
'mobile_codes_presets_form',
),
'access' => user_access('administer mobile codes'),
'file' => 'mobile_codes.admin.inc',
'type' => MENU_LOCAL_TASK,
);
$items[] = array(
'path' => 'admin/settings/mobile_codes/settings',
'title' => 'Settings',
'callback' => 'drupal_get_form',
'callback arguments' => array(
'mobile_codes_settings_form',
),
'access' => user_access('administer mobile codes'),
'file' => 'mobile_codes.admin.inc',
'type' => MENU_LOCAL_TASK,
);
}
else {
$items[] = array(
'path' => 'admin/settings/mobile_codes/presets/' . arg(4) . '/delete',
'title' => 'Delete preset',
'callback' => 'drupal_get_form',
'callback arguments' => array(
'mobile_codes_presets_delete',
arg(4),
),
'access' => user_access('administer mobile codes'),
'file' => 'mobile_codes.admin.inc',
);
$items[] = array(
'path' => 'admin/settings/mobile_codes/presets/' . arg(4) . '/edit',
'title' => 'Edit preset',
'callback' => 'drupal_get_form',
'callback arguments' => array(
'mobile_codes_presets_form_edit',
arg(4),
),
'access' => user_access('administer mobile codes'),
'file' => 'mobile_codes.admin.inc',
);
}
return $items;
}
function mobile_codes_load_preset($pid) {
if (is_numeric($pid)) {
$query = 'pid = %d';
}
else {
$query = 'name = "%s"';
}
$result = db_query('SELECT * FROM {mobile_codes_presets} WHERE ' . $query, $pid);
if ($preset = db_fetch_object($result)) {
$preset->data = unserialize($preset->data);
return $preset;
}
else {
return FALSE;
}
}
/**
* Generate a mobile code
*/
function mobile_codes_generate($data, &$arguments = array()) {
$values = mobile_codes_process_arguments($arguments);
if (($file = mobile_codes_load_mobile_code($data, $arguments)) === FALSE) {
$dir = '/mobile_codes/' . $arguments['preset'];
$file = file_directory_path() . $dir . '/' . time() . rand(1000, 9999) . '.png';
if ($arguments['data'] == 'link' && ($arguments['tinyurl'] == 1 || drupal_strlen($data) > 60)) {
$headers = array(
'Content-type' => 'application/x-www-form-urlencoded',
);
$tinyurl = drupal_http_request('http://tinyurl.com/api-create.php?url=' . urlencode($data), $headers, 'POST', NULL);
if ($tinyurl->code == 200) {
$data = $tinyurl->data;
}
}
$url = 'http://mobilecodes.nokia.com/' . $arguments['type'] . '?' . $values['text']['data'] . '=' . urlencode($data) . '&' . $values['text']['size'] . '=' . $values['size'][$arguments['type']] . '&name=' . $arguments['name'] . $values['text']['margin_encoding'] . '&type=' . $arguments['data'] . '&MODE=' . $values['mode'] . '&a=create';
mobile_codes_directory_check($dir);
$image = drupal_http_request($url);
file_save_data($image->data, $file, FILE_EXISTS_REPLACE);
mobile_codes_save_mobile_code($data, $arguments, $file);
}
return $file;
}
/**
* Load Mobile Code
*/
function mobile_codes_load_mobile_code($data, $arguments) {
ksort($arguments);
$data = serialize(array(
'data' => $data,
'arguments' => $arguments,
));
$result = db_query("SELECT * FROM {mobile_codes} WHERE data = '%s'", $data);
if ($mobile_code = db_fetch_object($result)) {
return $mobile_code->file;
}
else {
return FALSE;
}
}
/**
* Save Mobile Code
*/
function mobile_codes_save_mobile_code($data, $arguments, $image_path) {
ksort($arguments);
$data = serialize(array(
'data' => $data,
'arguments' => $arguments,
));
db_query("INSERT {mobile_codes} (pid, data, file) VALUES (%d, '%s', '%s')", $arguments['preset'], $data, $image_path);
}
function mobile_codes_process_arguments(&$arguments) {
$values = array();
// Load preset
if (!isset($arguments['preset'])) {
$arguments['preset'] = 1;
}
if ($preset = mobile_codes_load_preset($arguments['preset'])) {
$arguments['preset'] = $preset->pid;
$arguments += $preset->data;
}
else {
drupal_set_message(t('Mobile Codes Preset "%preset" does not exist.', array(
'%preset' => $arguments['preset'],
)), 'warning');
return FALSE;
}
// Set Name
if (!isset($arguments['name'])) {
$arguments['name'] = '';
}
// Set Data type
switch ($arguments['data']) {
case 'link':
case 'text':
$values['mode'] = 'TEXT';
break;
case 'phone':
$values['mode'] = 'NUMBER';
break;
default:
$arguments['data'] = 'link';
$values['mode'] = 'TEXT';
break;
}
// Set Size
switch (drupal_strtolower($arguments['size'])) {
case 's':
case 'sml':
case 'small':
$values['size'] = array(
'dm' => '0.12',
'qr' => '2',
);
break;
case 'm':
case 'med':
case 'medium':
default:
$values['size'] = array(
'dm' => '0.18',
'qr' => '4',
);
break;
case 'l':
case 'lrg':
case 'large':
$values['size'] = array(
'dm' => '0.24',
'qr' => '6',
);
break;
}
// Set Code type
switch ($arguments['type']) {
case 'dm':
$values['text'] = array(
'data' => 'BARCODE',
'size' => 'X',
'margin_encoding' => '',
);
break;
case 'qr':
default:
$values['text'] = array(
'data' => 'DATA',
'size' => 'MODULE_SIZE',
'margin_encoding' => '&MARGIN=2&ENCODING=BYTE',
);
break;
}
return $values;
}
/**
* Create directory if it does not exist.
*/
function mobile_codes_directory_check($directory) {
foreach (explode('/', $directory) as $dir) {
$dirs[] = $dir;
$dir = file_directory_path() . implode($dirs, '/');
if (!file_check_directory($dir, FILE_CREATE_DIRECTORY)) {
return FALSE;
}
}
return TRUE;
}
/**
* Theme function for displaying Mobile Codes
*/
function theme_mobilecode($data, $arguments = array()) {
$file = mobile_codes_generate($data, $arguments);
return theme('image', $file, $arguments['name'], $arguments['name'], NULL, FALSE);
}
Functions
Name | Description |
---|---|
mobile_codes_directory_check | Create directory if it does not exist. |
mobile_codes_generate | Generate a mobile code |
mobile_codes_load_mobile_code | Load Mobile Code |
mobile_codes_load_preset | |
mobile_codes_menu | Implementation of hook_menu(). |
mobile_codes_process_arguments | |
mobile_codes_save_mobile_code | Save Mobile Code |
theme_mobilecode | Theme function for displaying Mobile Codes |