diff --git a/includes/features.system.inc b/includes/features.system.inc new file mode 100644 index 0000000..a409f87 --- /dev/null +++ b/includes/features.system.inc @@ -0,0 +1,139 @@ + array( + 'name' => t('Date formats'), + 'default_hook' => 'date_formats', + 'feature_source' => TRUE + ), + ); +} + +/** + * Implements hook_features_export_options(). + */ +function date_formats_features_export_options() { + $options = array(); + $format_types = system_get_date_types(); + $languages = locale_language_list('native'); + + foreach ($format_types as $type => $format_type) { + $options[$type] = $format_type['title']; + foreach($languages as $langcode => $language) { + $options["$type::$langcode"] = "{$format_type['title']} ($language)"; + } + } + return $options; +} + +/** + * Implements hook_features_export(). + */ +function date_formats_features_export($data, &$export, $module_name = '') { + // The filter_default_formats() hook integration is provided by the + // features module so we need to add it as a dependency. + $export['dependencies']['features'] = 'features'; + + foreach ($data as $name) { + list($type, $langcode) = explode('::', $name .'::', 3); + if ($format = _dfe_get_date_format($type, $langcode)) { + // Add format to exports + $export['features']['date_formats'][$name] = $name; + } + } + + // @TODO: maybe add date_format_type to pipe (if it is not locked so far). + $pipe = array(); + return $pipe; +} + +/** + * Implements hook_features_export_render(). + */ +function date_formats_features_export_render($module, $data, $export = NULL) { + $code = array(); + $code[] = ' $formats = array();'; + $code[] = ''; + + foreach ($data as $name) { + list($type, $langcode) = explode('::', $name .'::', 3); + if ($format = _dfe_get_date_format($type, $langcode)) { + $var = array( + 'type' => $type, + 'format' => $format, + 'locales' => $langcode ? array($langcode) : array(), + ); + + $format_export = features_var_export($var, ' '); + $format_identifier = features_var_export($name); + $code[] = " // Exported format: $name"; + $code[] = " \$formats[{$format_identifier}] = {$format_export};"; + } + } + + $code[] = ' return $formats;'; + $code = implode("\n", $code); + return array('date_formats' => $code); +} + +/** + * Implements hook_features_revert(). + */ +function date_formats_features_revert($module) { + return date_formats_features_rebuild($module); +} + +/** + * Implements hook_features_rebuild(). + * + * hook_date_formats() is used to define the necessary formats, but the + * association to the date format type is not set by using that hook. So we have + * to set it in the database by ourselfs. + * + * @see hook_date_formats() + */ +function date_formats_features_rebuild($module) { + if ($defaults = features_get_default('date_formats', $module)) { + foreach ($defaults as $key => $spec) { + if (empty($spec['locales'])) { + variable_set('date_format_'. $spec['type'], $spec['format']); + } + else { + foreach ($spec['locales'] as $locale) { + locale_date_format_save($locale, $spec['type'], $spec['format']); + } + } + } + } +} + +/** + * Helper function to get the pure format (locale or from variable). + */ +function _dfe_get_date_format($type, $language = NULL) { + $format = ''; + if (!empty($language)) { + // Try to get a locale date format + $format = system_date_format_locale($language, $type); + } + + // If no language specific format was fount, or language is not set. + if (empty($format)) { + // DO not use variable_get() as this may fetch an allready localized variable. + $format = unserialize(db_query('SELECT value FROM {variable} WHERE name = :name', array(':name' => "date_format_". $type))->fetchField()); + } + return ($format) ? $format : NULL; +} \ No newline at end of file