fe_date.module 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. /**
  3. * @file
  4. * Features export for date components.
  5. *
  6. * - 'date_format_types': Hooks 'date_format_types_features_revert()' and
  7. * 'date_format_types_features_rebuild()' should be managed by
  8. * 'hook_date_format_types()'.
  9. * - 'locale_date_format': Localized date formats.
  10. * - 'custom_date_formats': These formats can be deleted via gui and can be
  11. * used in all date format types.
  12. */
  13. /**
  14. * Implements hook_features_api().
  15. */
  16. function fe_date_features_api() {
  17. $components['fe_date_date_format_types'] = array(
  18. 'name' => t('Date format types'),
  19. 'feature_source' => TRUE,
  20. 'default_hook' => 'date_format_types',
  21. 'default_file' => FEATURES_DEFAULTS_INCLUDED_COMMON,
  22. );
  23. $components['fe_date_custom_date_formats'] = array(
  24. 'name' => t('Custom date formats'),
  25. 'feature_source' => TRUE,
  26. 'default_hook' => 'fe_date_custom_date_formats',
  27. 'default_file' => FEATURES_DEFAULTS_INCLUDED_COMMON,
  28. );
  29. if (module_exists('locale')) {
  30. $components['fe_date_locale_date_format'] = array(
  31. 'name' => t('Locale date format'),
  32. 'feature_source' => TRUE,
  33. 'default_hook' => 'fe_date_locale_date_format',
  34. 'default_file' => FEATURES_DEFAULTS_INCLUDED_COMMON,
  35. );
  36. }
  37. return $components;
  38. }
  39. /**
  40. * Implements hook_features_export_options().
  41. */
  42. function fe_date_date_format_types_features_export_options() {
  43. $options = array();
  44. $format_types = system_get_date_types();
  45. foreach ($format_types as $type => $format_type) {
  46. $options[$type] = $format_type['title'];
  47. }
  48. return $options;
  49. }
  50. /**
  51. * Implements hook_features_export().
  52. */
  53. function fe_date_date_format_types_features_export($data, &$export, $module_name = '') {
  54. $export['dependencies']['fe_date'] = 'fe_date';
  55. $pipe = array('variable' => array());
  56. $map_variable = features_get_default_map('variable');
  57. $map_custom_formats = features_get_default_map('fe_date_custom_date_formats');
  58. $map_date_format_types = features_get_default_map('fe_date_date_format_types');
  59. foreach ($data as $type) {
  60. // Only add format type to export, if it is available.
  61. if (system_get_date_types($type)) {
  62. $variable = 'date_format_' . $type;
  63. // Add a dependency when the variable already in a feature but not by use.
  64. if (isset($map_variable[$variable]) && $map_variable[$variable] != $module_name) {
  65. $export['dependencies'][$map_variable[$variable]] = $map_variable[$variable];
  66. }
  67. else {
  68. // Add strongarm variable for given default date format for this type.
  69. $pipe['variable'][$variable] = $variable;
  70. }
  71. $default_format = _fe_date_get_default_format($type);
  72. // Add a dependency when the default date format is a custom date format
  73. // and already in a feature but not by use.
  74. if (isset($map_custom_formats[$default_format]) && $map_custom_formats[$default_format] != $module_name) {
  75. $export['dependencies'][$map_custom_formats[$default_format]] = $map_custom_formats[$default_format];
  76. }
  77. else {
  78. // Add custom date format for this type.
  79. $pipe['fe_date_custom_date_formats'][$default_format] = $default_format;
  80. }
  81. // Add a dependency when the date_format_type already in a feature but
  82. // not by use.
  83. if (isset($map_date_format_types[$type]) && $map_date_format_types[$type] != $module_name) {
  84. $export['dependencies'][$map_date_format_types[$type]] = $map_date_format_types[$type];
  85. }
  86. else {
  87. $export['features']['fe_date_date_format_types'][$type] = $type;
  88. }
  89. }
  90. }
  91. return $pipe;
  92. }
  93. /**
  94. * Implements hook_features_export_render().
  95. */
  96. function fe_date_date_format_types_features_export_render($module, $data, $export = NULL) {
  97. $code = array();
  98. $code[] = ' $format_types = array();';
  99. foreach ($data as $type) {
  100. if ($spec = system_get_date_types($type)) {
  101. $format_export = features_var_export($spec['title'], ' ');
  102. $format_identifier = features_var_export($type);
  103. $code[] = " // Exported date format type: $type";
  104. $code[] = " \$format_types[{$format_identifier}] = {$format_export};";
  105. }
  106. }
  107. $code[] = ' return $format_types;';
  108. $code = implode("\n", $code);
  109. return array('date_format_types' => $code);
  110. }
  111. /**
  112. * Implements hook_features_export_options().
  113. */
  114. function fe_date_locale_date_format_features_export_options() {
  115. $options = array();
  116. $format_types = system_get_date_types();
  117. $languages = locale_language_list('native');
  118. foreach ($format_types as $type => $format_type) {
  119. foreach ($languages as $langcode => $language) {
  120. $format = _fe_date_get_locale_date_format($type, $langcode);
  121. if (!empty($format)) {
  122. $options["$type::$langcode"] = "{$format_type['title']} ($language)";
  123. }
  124. }
  125. }
  126. return $options;
  127. }
  128. /**
  129. * Implements hook_features_export().
  130. */
  131. function fe_date_locale_date_format_features_export($data, &$export, $module_name = '') {
  132. $export['dependencies']['features'] = 'features';
  133. $export['dependencies']['fe_date'] = 'fe_date';
  134. $map_language = features_get_default_map('language');
  135. $map_date_format_types = features_get_default_map('fe_date_date_format_types');
  136. $map_locale_date_format = features_get_default_map('fe_date_locale_date_format');
  137. $pipe = array('fe_date_date_format_types' => array(), 'language' => array());
  138. foreach ($data as $name) {
  139. list($type, $langcode) = explode('::', $name . '::', 3);
  140. if ($format = _fe_date_get_locale_date_format($type, $langcode)) {
  141. // Add a dependency when the type already in a feature but not by use
  142. // or system.
  143. if (isset($map_date_format_types[$type]) && !in_array($map_date_format_types[$type], array('system', $module_name))) {
  144. $export['dependencies'][$map_date_format_types[$type]] = $map_date_format_types[$type];
  145. }
  146. else {
  147. // Add date_format_type to pipe.
  148. $pipe['fe_date_date_format_types'][$type] = $type;
  149. }
  150. // Add a dependency when the language is in a feature but not by use.
  151. if (isset($map_language[$langcode]) && $map_language[$langcode] != $module_name) {
  152. $export['dependencies'][$map_language[$langcode]] = $map_language[$langcode];
  153. }
  154. else {
  155. // Add language to pipe.
  156. $pipe['language'][$langcode] = $langcode;
  157. }
  158. if (isset($map_locale_date_format[$name]) && $map_locale_date_format[$name] != $module_name) {
  159. $export['dependencies'][$map_locale_date_format[$name]] = $map_locale_date_format[$name];
  160. }
  161. else {
  162. // Add format to exports.
  163. $export['features']['fe_date_locale_date_format'][$name] = $name;
  164. }
  165. }
  166. }
  167. return $pipe;
  168. }
  169. /**
  170. * Implements hook_features_export_render().
  171. */
  172. function fe_date_locale_date_format_features_export_render($module, $data, $export = NULL) {
  173. $code = array();
  174. $code[] = ' $locale_date_formats = array();';
  175. $code[] = '';
  176. foreach ($data as $name) {
  177. list($type, $langcode) = explode('::', $name . '::', 3);
  178. if ($format = _fe_date_get_locale_date_format($type, $langcode)) {
  179. $var = array(
  180. 'type' => $type,
  181. 'format' => $format,
  182. 'locales' => $langcode ? array($langcode) : array(),
  183. );
  184. $format_export = features_var_export($var, ' ');
  185. $format_identifier = features_var_export($name);
  186. $code[] = " // Exported format: $name";
  187. $code[] = " \$locale_date_formats[{$format_identifier}] = {$format_export};";
  188. }
  189. }
  190. $code[] = ' return $locale_date_formats;';
  191. $code = implode("\n", $code);
  192. return array('fe_date_locale_date_format' => $code);
  193. }
  194. /**
  195. * Implements hook_features_revert().
  196. */
  197. function fe_date_locale_date_format_features_revert($module) {
  198. fe_date_locale_date_format_features_rebuild($module);
  199. }
  200. /**
  201. * Implements hook_features_rebuild().
  202. *
  203. * hook_date_formats() is used to define the necessary formats, but the
  204. * association to the date format type is not set by using that hook. So we have
  205. * to set it in the database by ourselves.
  206. *
  207. * @see hook_date_formats()
  208. */
  209. function fe_date_locale_date_format_features_rebuild($module) {
  210. if ($defaults = features_get_default('fe_date_locale_date_format', $module)) {
  211. foreach ($defaults as $spec) {
  212. foreach ($spec['locales'] as $locale) {
  213. locale_date_format_save($locale, $spec['type'], $spec['format']);
  214. }
  215. }
  216. }
  217. }
  218. /**
  219. * Helper function to get the pure format (locale or from variable).
  220. */
  221. function _fe_date_get_locale_date_format($type, $langcode) {
  222. // Try to get a locale date format.
  223. $format = system_date_format_locale($langcode, $type);
  224. // If no format was found, we get the default value.
  225. if (empty($format)) {
  226. $format = _fe_date_get_default_format($type);
  227. }
  228. return $format ? $format : NULL;
  229. }
  230. /**
  231. * Helper function to get the pure format (from variable).
  232. */
  233. function _fe_date_get_default_format($type) {
  234. // Do not use variable_get() as this may fetch an already localized variable.
  235. $format = db_query('SELECT value FROM {variable} WHERE name = :name', array(':name' => 'date_format_' . $type))->fetchField();
  236. return $format !== FALSE ? unserialize($format) : NULL;
  237. }
  238. /**
  239. * Implements hook_features_export_options().
  240. */
  241. function fe_date_custom_date_formats_features_export_options() {
  242. $formats = system_get_date_formats('custom');
  243. $options = array();
  244. if (!empty($formats)) {
  245. foreach (array_keys($formats) as $format) {
  246. $options[$format] = $format;
  247. }
  248. }
  249. return $options;
  250. }
  251. /**
  252. * Implements hook_features_export().
  253. */
  254. function fe_date_custom_date_formats_features_export($data, &$export, $module_name = '') {
  255. $export['dependencies']['features'] = 'features';
  256. $export['dependencies']['fe_date'] = 'fe_date';
  257. foreach ($data as $format) {
  258. $map = features_get_default_map('fe_date_custom_date_formats');
  259. if (!empty($map[$format]) && $map[$format] != $module_name) {
  260. $export['dependencies'][$map[$format]] = $map[$format];
  261. }
  262. else {
  263. $export['features']['fe_date_custom_date_formats'][$format] = $format;
  264. }
  265. }
  266. return array();
  267. }
  268. /**
  269. * Implements hook_features_export_render().
  270. */
  271. function fe_date_custom_date_formats_features_export_render($module, $data, $export = NULL) {
  272. $formats = system_get_date_formats('custom');
  273. $code = array();
  274. $code[] = ' $custom_date_formats = array();';
  275. foreach ($data as $format) {
  276. if (!empty($formats[$format])) {
  277. $format = features_var_export($format);
  278. $code[] = " \$custom_date_formats[{$format}] = {$format};";
  279. }
  280. }
  281. $code[] = ' return $custom_date_formats;';
  282. $code = implode("\n", $code);
  283. return array('fe_date_custom_date_formats' => $code);
  284. }
  285. /**
  286. * Implements hook_features_rebuild().
  287. */
  288. function fe_date_custom_date_formats_features_rebuild($module) {
  289. fe_date_custom_date_formats_features_revert($module);
  290. }
  291. /**
  292. * Implements hook_features_revert().
  293. */
  294. function fe_date_custom_date_formats_features_revert($module) {
  295. if ($defaults = features_get_default('fe_date_custom_date_formats', $module)) {
  296. $formats = system_get_date_formats('custom');
  297. foreach ($defaults as $format) {
  298. if (empty($formats[$format])) {
  299. $format_data['format'] = trim($format);
  300. $format_data['type'] = 'custom';
  301. $format_data['locked'] = 0;
  302. $format_data['is_new'] = 1;
  303. system_date_format_save($format_data);
  304. }
  305. }
  306. }
  307. }