field_timer.module in Field Timer 7
Same filename and directory in other branches
Main module file.
File
field_timer.moduleView source
<?php
/**
* @file
* Main module file.
*/
/**
* Implements hook_libraries_info().
*/
function field_timer_libraries_info() {
return array(
'county' => array(
'name' => t('County'),
'vendor url' => 'https://github.com/brilsergei/county',
'download url' => 'https://github.com/brilsergei/county/archive/master.zip',
'version' => '1.0',
'files' => array(
'js' => array(
'js/county.js',
),
'css' => array(
'css/county.css',
),
),
),
'jquery.countdown' => array(
'name' => t('jQuery Countdown'),
'vendor url' => 'http://keith-wood.name/countdown.html',
'download url' => 'http://keith-wood.name/countdown.html',
'version arguments' => array(
'file' => 'js/jquery.countdown.js',
'pattern' => '/Countdown for jQuery v(2.1.[0-9])/',
'lines' => 2,
),
'files' => array(
'js' => array(
'js/jquery.plugin.min.js',
'js/jquery.countdown.min.js',
),
'css' => array(
'css/jquery.countdown.css',
),
),
),
);
}
/**
* Implements hook_field_info().
*/
function field_timer_field_info() {
return array(
'field_timer' => array(
'label' => t('Timer Field'),
'description' => t('Timer or countdown.'),
'settings' => array(),
'instance_settings' => array(
// Used only for jQuery Countdown plugin.
'jquery-countdown' => array(
'description' => '',
'expiryText' => '',
'expiryUrl' => '',
),
),
'default_widget' => 'field_timer_simple_date',
'default_formatter' => 'field_timer_text',
),
);
}
/**
* Implements hook_field_widget_info().
*/
function field_timer_field_widget_info() {
return array(
'field_timer_simple_date' => array(
'label' => t('Date Popup'),
'field types' => array(
'field_timer',
),
'settings' => array(),
'weight' => -1,
),
);
}
/**
* Implements hook_field_widget_info_alter().
*/
function field_timer_field_widget_info_alter(&$info) {
$info['options_select']['field types'][] = 'field_timer';
$info['options_buttons']['field types'][] = 'field_timer';
}
/**
* Implements hook_options_list().
*/
function field_timer_options_list($field, $instance, $entity_type, $entity) {
return field_timer_entity_properties($entity_type);
}
/**
* Implements hook_field_widget_form().
*/
function field_timer_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
switch ($instance['widget']['type']) {
case 'field_timer_simple_date':
$default_value = NULL;
// Default value from database is timestamp, default value set by admin
// at field configuration page is formatted date string.
if (isset($items[$delta]['timestamp'])) {
if (is_numeric($items[$delta]['timestamp'])) {
$default_value = date('Y-m-d H:i:s', $items[$delta]['timestamp']);
}
else {
$default_value = $items[$delta]['timestamp'];
}
}
$widget = array(
'#type' => 'date_popup',
'#title' => $element['#title'],
'#default_value' => $default_value,
);
$element['timestamp'] = $widget;
break;
}
return $element;
}
/**
* Implements hook_field_formatter_info().
*/
function field_timer_field_formatter_info() {
$formatters = array(
'field_timer_text' => array(
'label' => t('Text'),
'description' => t('Simple text timer and countdown.'),
'field types' => array(
'field_timer',
),
'settings' => array(
'type' => 'auto',
'granularity' => 2,
),
),
);
$library = libraries_detect('county');
if ($library && !empty($library['installed'])) {
$formatters['field_timer_county'] = array(
'label' => t('County'),
'description' => t('Use County jQuery plugin to display countdown.'),
'field types' => array(
'field_timer',
),
'settings' => array(
'animation' => 'fade',
'speed' => 500,
'county_theme' => 'blue',
'background' => '',
'reflection' => 1,
'reflectionOpacity' => 0.2,
),
);
}
$library = libraries_detect('jquery.countdown');
if ($library && !empty($library['installed'])) {
$formatters['field_timer_jquery_countdown'] = array(
'label' => t('jQuery Countdown'),
'description' => t('Use jQuery Countdown plugin to display countdown and timer.'),
'field types' => array(
'field_timer',
),
'settings' => array(
'type' => 'auto',
'regional' => 'en',
'format' => 'dHMS',
'layout' => '',
'compact' => 0,
'significant' => 0,
'timeSeparator' => ':',
'padZeroes' => 0,
),
);
$formatters['field_timer_jquery_countdown_led'] = array(
'label' => 'jQuery Countdown LED',
'description' => t('Display timer and countdown as a LED.'),
'field types' => array(
'field_timer',
),
'settings' => array(
'type' => 'auto',
'countdown_theme' => 'green',
'max_count_of_days' => 2,
'display_days' => 1,
'display_hours' => 1,
'display_minutes' => 1,
'display_seconds' => 1,
),
);
}
return $formatters;
}
/**
* Implements hook_field_formatter_view().
*/
function field_timer_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
module_load_include('inc', 'field_timer', 'includes/field_timer');
$settings = $display['settings'];
switch ($display['type']) {
case 'field_timer_text':
return _field_timer_text_formatter($items, $settings);
case 'field_timer_county':
return _field_timer_county_formatter($items, $settings, $entity_type, $entity, $instance);
case 'field_timer_jquery_countdown':
return _field_timer_jquery_countdown_formatter($items, $settings, $entity_type, $entity, $instance);
case 'field_timer_jquery_countdown_led':
return _field_timer_jquery_countdown_led_formatter($items, $settings, $entity_type, $entity, $instance);
}
}
/**
* Implements hook_field_is_empty().
*/
function field_timer_field_is_empty($item, $field) {
return empty($item['timestamp']) && empty($item['entity_property_name']);
}
/**
* Implements hook_field_presave().
*/
function field_timer_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
// Date popup widget return date as formatted string. We need to convert it to
// timestamp.
if ($field['type'] == 'field_timer') {
foreach ($items as $delta => $item) {
if (!empty($item['timestamp'])) {
$timestamp = strtotime($item['timestamp']);
if ($timestamp) {
$items[$delta]['timestamp'] = $timestamp;
}
}
}
}
}
/**
* Implements hook_field_load().
*/
function field_timer_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
// Set timestamp for field item if an entity property used as target/end date.
foreach ($entities as $id => $entity) {
foreach ($items[$id] as $delta => $item) {
if (empty($item['timestamp']) && !empty($item['entity_property_name']) && isset($entity->{$item['entity_property_name']})) {
$items[$id][$delta]['timestamp'] = $entity->{$item['entity_property_name']};
}
}
}
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function field_timer_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
module_load_include('inc', 'field_timer', 'includes/field_timer');
switch ($display['type']) {
case 'field_timer_text':
return _field_timer_text_summary($settings);
case 'field_timer_county':
return _field_timer_county_summary($settings);
case 'field_timer_jquery_countdown':
return _field_timer_jquery_countdown_summary($settings);
case 'field_timer_jquery_countdown_led':
return _field_timer_jquery_countdown_led_summary($settings);
}
}
/**
* Implements hook_field_formatter_settings_form().
*/
function field_timer_field_formatter_settings_form($field, $instance, $view_mode, $form, $form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
module_load_include('inc', 'field_timer', 'includes/field_timer');
switch ($display['type']) {
case 'field_timer_text':
return _field_timer_text_settings_form($settings);
case 'field_timer_county':
return _field_timer_county_settings_form($settings);
case 'field_timer_jquery_countdown':
return _field_timer_jquery_countdown_settings_form($settings);
case 'field_timer_jquery_countdown_led':
return _field_timer_jquery_countdown_led_settings_form($settings);
}
}
/**
* Implements hook_field_instance_settings_form().
*/
function field_timer_field_instance_settings_form($field, $instance) {
$settings_form = array();
$settings = $instance['settings'];
foreach ($instance['display'] as $display) {
if ($display['type'] == 'field_timer_jquery_countdown' || $display['type'] == 'field_timer_jquery_countdown_led') {
$settings_form['jquery-countdown'] = array(
'#type' => 'fieldset',
'#title' => t('jQuery Countdown options'),
'#description' => t('This settings effect only on jQuery Countdown and jQuery Countdown LED formatters.'),
'#collapsed' => TRUE,
'#collapsible' => TRUE,
);
$token_description = module_exists('token') ? '<br />' . t('This field supports tokens.') : '';
$settings_form['jquery-countdown']['expiryUrl'] = array(
'#type' => 'textfield',
'#title' => t('Expiry URL'),
'#default_value' => $settings['jquery-countdown']['expiryUrl'],
'#description' => t('A URL to load when the countdown reaches zero.!brSee !link for this parameter.!token_description', array(
'!token_description' => $token_description,
'!br' => '<br />',
'!link' => l(t('documentation'), 'http://keith-wood.name/countdownRef.html', array(
'fragment' => 'expiryUrl',
)),
)),
);
$settings_form['jquery-countdown']['expiryText'] = array(
'#type' => 'textarea',
'#title' => t('Expiry text'),
'#default_value' => $settings['jquery-countdown']['expiryText'],
'#description' => t('A message to display when the countdown reaches zero.!brSee !link for this parameter.!token_description', array(
'!token_description' => $token_description,
'!br' => '<br />',
'!link' => l(t('documentation'), 'http://keith-wood.name/countdownRef.html', array(
'fragment' => 'expiryText',
)),
)),
);
$settings_form['jquery-countdown']['description'] = array(
'#type' => 'textarea',
'#title' => t('Description'),
'#default_value' => $settings['jquery-countdown']['description'],
'#description' => t('The descriptive text shown below the countdown timer.!brSee !link for this parameter.!token_description', array(
'!token_description' => $token_description,
'!br' => '<br />',
'!link' => l(t('documentation'), 'http://keith-wood.name/countdownRef.html', array(
'fragment' => 'description',
)),
)),
);
if (module_exists('token')) {
$settings_form['jquery-countdown']['token'] = array(
'#theme' => 'token_tree',
'#token_types' => array(
$instance['entity_type'],
),
'#global_types' => TRUE,
'#click_insert' => FALSE,
);
}
break;
}
}
return $settings_form;
}
/**
* Implements hook_field_timer_entity_properties().
*/
function field_timer_field_timer_entity_properties() {
$properties = array(
'node' => array(
'created' => t('Node create date'),
'changed' => t('Node last change date'),
),
'comment' => array(
'created' => t('Comment create date'),
'changed' => t('Comment last change date'),
),
'file' => array(
'timestamp' => t('File upload date'),
),
'taxonomy_term' => array(),
'taxonomy_vocabulary' => array(),
'user' => array(
'created' => t('User create date'),
'access' => t('User last access date'),
'login' => t('User last login date'),
),
);
return $properties;
}
/**
* Loads entity properties which represent a date.
*
* @param string $entity_type
* Entity type machine name or empty string.
*
* @return array
* Returns an array of entity's properties which represent a date keyed by
* entity types if $entity_type is empty, array of properties of given entity
* type otherwise.
*/
function field_timer_entity_properties($entity_type = '') {
$properties = module_invoke_all('field_timer_entity_properties');
drupal_alter('field_timer_entity_properties', $properties);
if (!empty($entity_type)) {
return $properties[$entity_type];
}
return $properties;
}
/**
* Help function.
*
* Get all available regions for jquery.countdown.
*/
function _field_timer_jquery_countdown_regions() {
return array(
'sq' => t('Albanian'),
'ar' => t('Arabic'),
'hy' => t('Armenian'),
'bn' => t('Bengali/Bangla'),
'bs' => t('Bosnian'),
'bg' => t('Bulgarian'),
'my' => t('Burmese'),
'ca' => t('Catalan'),
'zh-CN' => t('Chinese/Simplified'),
'zh-TW' => t('Chinese/Traditional'),
'hr' => t('Croatian'),
'cs' => t('Czech'),
'da' => t('Danish'),
'nl' => t('Dutch'),
'et' => t('Estonian'),
'en' => t('English'),
'fa' => t('Farsi/Persian'),
'fi' => t('Finnish'),
'fo' => t('Faroese'),
'fr' => t('French'),
'gl' => t('Galician'),
'de' => t('German'),
'el' => t('Greek'),
'gu' => t('Gujarati'),
'he' => t('Hebrew'),
'hu' => t('Hungarian'),
'id' => t('Indonesian'),
'is' => t('Icelandic'),
'it' => t('Italian'),
'ja' => t('Japanese'),
'kn' => t('Kannada'),
'ko' => t('Korean'),
'lv' => t('Latvian'),
'lt' => t('Lithuanian'),
'mk' => t('Macedonian'),
'ml' => t('Malayalam'),
'ms' => t('Malaysian'),
'nb' => t('Norvegian'),
'pl' => t('Polish'),
'pt-BR' => t('Portuguese/Brazilian'),
'ro' => t('Romanian'),
'ru' => t('Russian'),
'sr' => t('Serbian'),
'sk' => t('Slovak'),
'sl' => t('Slovenian'),
'es' => t('Spanish'),
'sv' => t('Swedish'),
'th' => t('Thai'),
'tr' => t('Turkish'),
'uk' => t('Ukrainian'),
'ur' => t('Urdu'),
'uz' => t('Uzbek'),
'vi' => t('Vietnamese'),
'cy' => t('Welsh'),
);
}