date_api.install 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the date_api module.
  5. */
  6. /**
  7. * Helper function for setting Date variables.
  8. */
  9. function date_api_set_variables() {
  10. // Set absolute minimum and maximum year for dates on this site.
  11. // There is actually no maximum and minimum year in PHP 5, but a date with
  12. // a year less than 0 would result in negative ISO and DATETIME dates,
  13. // like -1250-01-01T00:00:00, which probably won't make sense or work
  14. // correctly anywhere.
  15. // The odd construct of using variable_get() instead of variable_set()
  16. // is so we don't accidentally write over an existing value. If
  17. // no value is set, variable_get() will set it.
  18. variable_get('date_max_year', 4000);
  19. variable_get('date_min_year', 1);
  20. variable_get('date_php_min_year', 1901);
  21. // Set an API version in a way that other modules can test for compatibility.
  22. variable_set('date_api_version', '7.2');
  23. }
  24. /**
  25. * Implements hook_requirements().
  26. */
  27. function date_api_requirements($phase) {
  28. $requirements = array();
  29. if ($phase == 'runtime') {
  30. $t = get_t();
  31. module_load_include('module', 'date_api');
  32. $messages = date_api_status();
  33. $error_messages = !empty($messages['errors']) ? $messages['errors'] : array();
  34. $success_messages = !empty($messages['success']) ? $messages['success'] : array();
  35. if (!empty($error_messages)) {
  36. $requirements['date'] = array(
  37. 'title' => $t('Date API'),
  38. 'value' => $t('Missing system date settings'),
  39. 'description' => implode(' ', array_merge($error_messages, $success_messages)),
  40. 'severity' => REQUIREMENT_ERROR,
  41. );
  42. }
  43. else {
  44. $requirements['date'] = array(
  45. 'title' => $t('Date API'),
  46. 'value' => $t('System date settings'),
  47. 'description' => implode(' ', $success_messages),
  48. );
  49. }
  50. }
  51. return $requirements;
  52. }
  53. /**
  54. * Implements hook_install().
  55. */
  56. function date_api_install() {
  57. // Only set the message if Drupal itself is already installed.
  58. if (variable_get('install_task') == 'done') {
  59. // Ensure translations don't break at install time.
  60. $t = get_t();
  61. // date_api_set_variables can install date_timezone. The
  62. // date_timezone_install() function does a module_enable('date_api'). This
  63. // means that date_api_enable() can be called before date_api_install()
  64. // finishes! So the date_api schema needs to be installed before this line!
  65. date_api_set_variables();
  66. $message = $t('The Date API requires that you set up the <a href="@regional_settings">site timezone and first day of week settings</a> and the <a href="@regional_date_time">date format settings</a> to function correctly.', array('@regional_settings' => url('admin/config/regional/settings'), '@regional_date_time' => url('admin/config/regional/date-time')));
  67. drupal_set_message(filter_xss_admin($message), 'warning');
  68. }
  69. }
  70. /**
  71. * Implements hook_enable().
  72. */
  73. function date_api_enable() {
  74. date_api_set_variables();
  75. }
  76. /**
  77. * Implements hook_uninstall().
  78. */
  79. function date_api_uninstall() {
  80. cache_clear_all('date_timezone_identifiers_list', 'cache');
  81. $variables = array(
  82. 'date_api_version',
  83. 'date_min_year',
  84. 'date_max_year',
  85. 'date_php_min_year',
  86. 'date_db_tz_support',
  87. 'date_api_use_iso8601',
  88. );
  89. foreach ($variables as $variable) {
  90. variable_del($variable);
  91. }
  92. if (db_table_exists('views_display')) {
  93. $displays = array(
  94. 'date_nav',
  95. );
  96. db_query("DELETE FROM {views_display} WHERE display_plugin IN ('" . implode("','", $displays) . "')");
  97. db_query("DELETE FROM {cache_views}");
  98. }
  99. }
  100. /**
  101. * Implements hook_update_last_removed().
  102. */
  103. function date_api_update_last_removed() {
  104. return 6005;
  105. }
  106. /**
  107. * Move old date format to new date format tables,and delete the old tables.
  108. *
  109. * Insert only values that don't already exist in the new tables, in
  110. * case new version of those custom values have already been created.
  111. */
  112. function date_api_update_7000() {
  113. // Move format data from the old 'date_format_types' table to the new
  114. // 'date_format_type' table.
  115. if (db_table_exists('date_format_types')) {
  116. // Find all the custom entries in the D6 table.
  117. $result = db_select('date_format_types', 'old')
  118. ->fields('old', array('type', 'title', 'locked'))
  119. ->condition('locked', 0)
  120. ->execute()
  121. ->fetchAll(PDO::FETCH_ASSOC);
  122. // Iterate over all the old values.
  123. foreach ($result as $row) {
  124. // See if this value already exists in the new table
  125. // (it might have been added manually before this update got run).
  126. $count = db_select('date_format_type', 'new')
  127. ->condition('type', $row['type'])
  128. ->countQuery()
  129. ->execute()
  130. ->fetchField();
  131. // If the value is missing, insert it.
  132. // Do nothing if it already exists, assume the value in the
  133. // new table trumps the old values.
  134. if (empty($count)) {
  135. db_insert('date_format_type')
  136. ->fields(array(
  137. 'type' => $row['type'],
  138. 'title' => $row['title'],
  139. 'locked' => $row['locked'],
  140. ))
  141. ->execute();
  142. }
  143. }
  144. // Drop the old table.
  145. db_drop_table('date_format_types');
  146. }
  147. // Move format data from the old 'date_formats' table (which was renamed to
  148. // 'd6_date_formats') to the new 'date_formats' table.
  149. if (db_table_exists('d6_date_formats')) {
  150. // Find all the custom entries in the D6 table.
  151. $result = db_select('d6_date_formats', 'old')
  152. ->fields('old', array('format', 'type', 'locked'))
  153. ->condition('type', 'custom')
  154. ->execute()
  155. ->fetchAll(PDO::FETCH_ASSOC);
  156. // Iterate over all the old values.
  157. foreach ($result as $row) {
  158. // See if this value already exists in the new table (it might have been
  159. // added manually before this update got run).
  160. $count = db_select('date_formats', 'new')
  161. ->condition('format', $row['format'])
  162. ->condition('type', $row['type'])
  163. ->countQuery()
  164. ->execute()
  165. ->fetchField();
  166. // If the value is missing, insert it. Do nothing if it already exists,
  167. // assume the value in the new table trumps the old values.
  168. if (empty($count)) {
  169. db_insert('date_formats')
  170. ->fields(array(
  171. 'format' => $row['format'],
  172. 'type' => $row['type'],
  173. 'locked' => $row['locked'],
  174. ))
  175. ->execute();
  176. }
  177. }
  178. // Drop the old table.
  179. db_drop_table('d6_date_formats');
  180. }
  181. // Move format data from the old 'date_format_locale' table (which was renamed
  182. // to 'd6_date_format_locale') to the new 'date_format_locale' table.
  183. if (db_table_exists('d6_date_format_locale')) {
  184. // Find all the custom entries in the D6 table.
  185. $result = db_select('d6_date_format_locale', 'old')
  186. ->fields('old', array('format', 'type', 'language'))
  187. ->condition('type', 'custom')
  188. ->execute()
  189. ->fetchAll(PDO::FETCH_ASSOC);
  190. // Iterate over all the old values.
  191. foreach ($result as $row) {
  192. // See if this value already exists in the new table (it might have been
  193. // added manually before this update got run).
  194. $count = db_select('date_format_locale', 'new')
  195. ->condition('format', $row['format'])
  196. ->condition('type', $row['type'])
  197. ->condition('language', $row['language'])
  198. ->countQuery()
  199. ->execute()
  200. ->fetchField();
  201. // If the value is missing, insert it.
  202. // Do nothing if it already exists, assume the value in the
  203. // new table trumps the old values.
  204. if (empty($count)) {
  205. db_insert('date_format_locale')
  206. ->fields(array(
  207. 'format' => $row['format'],
  208. 'type' => $row['type'],
  209. 'language' => $row['language'],
  210. ))
  211. ->execute();
  212. }
  213. }
  214. // Drop the old table.
  215. db_drop_table('d6_date_format_locale');
  216. }
  217. }
  218. /**
  219. * Drop D6 timezone_name field on {users} after upgrading to D7.
  220. */
  221. function date_api_update_7001() {
  222. if (db_field_exists('users', 'timezone_name')) {
  223. db_drop_field('users', 'timezone_name');
  224. }
  225. }