date_api.install 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 data to new date format tables, and delete the old
  108. * tables. Insert only values that don't already exist in the new tables, in
  109. * case new version of those custom values have already been created.
  110. */
  111. function date_api_update_7000() {
  112. // Move format data from the old 'date_format_types' table to the new
  113. // 'date_format_type' table.
  114. if (db_table_exists('date_format_types')) {
  115. // Find all the custom entries in the D6 table.
  116. $result = db_select('date_format_types', 'old')
  117. ->fields('old', array('type', 'title', 'locked'))
  118. ->condition('locked', 0)
  119. ->execute()
  120. ->fetchAll(PDO::FETCH_ASSOC);
  121. // Iterate over all the old values.
  122. foreach ($result as $row) {
  123. // See if this value already exists in the new table
  124. // (it might have been added manually before this update got run).
  125. $count = db_select('date_format_type', 'new')
  126. ->condition('type', $row['type'])
  127. ->countQuery()
  128. ->execute()
  129. ->fetchField();
  130. // If the value is missing, insert it.
  131. // Do nothing if it already exists, assume the value in the
  132. // new table trumps the old values.
  133. if (empty($count)) {
  134. db_insert('date_format_type')
  135. ->fields(array(
  136. 'type' => $row['type'],
  137. 'title' => $row['title'],
  138. 'locked' => $row['locked'],
  139. ))
  140. ->execute();
  141. }
  142. }
  143. // Drop the old table.
  144. db_drop_table('date_format_types');
  145. }
  146. // Move format data from the old 'date_formats' table (which was renamed to
  147. // 'd6_date_formats') to the new 'date_formats' table.
  148. if (db_table_exists('d6_date_formats')) {
  149. // Find all the custom entries in the D6 table.
  150. $result = db_select('d6_date_formats', 'old')
  151. ->fields('old', array('format', 'type', 'locked'))
  152. ->condition('type', 'custom')
  153. ->execute()
  154. ->fetchAll(PDO::FETCH_ASSOC);
  155. // Iterate over all the old values.
  156. foreach ($result as $row) {
  157. // See if this value already exists in the new table (it might have been
  158. // added manually before this update got run).
  159. $count = db_select('date_formats', 'new')
  160. ->condition('format', $row['format'])
  161. ->condition('type', $row['type'])
  162. ->countQuery()
  163. ->execute()
  164. ->fetchField();
  165. // If the value is missing, insert it. Do nothing if it already exists,
  166. // assume the value in the new table trumps the old values.
  167. if (empty($count)) {
  168. db_insert('date_formats')
  169. ->fields(array(
  170. 'format' => $row['format'],
  171. 'type' => $row['type'],
  172. 'locked' => $row['locked'],
  173. ))
  174. ->execute();
  175. }
  176. }
  177. // Drop the old table.
  178. db_drop_table('d6_date_formats');
  179. }
  180. // Move format data from the old 'date_format_locale' table (which was renamed
  181. // to 'd6_date_format_locale') to the new 'date_format_locale' table.
  182. if (db_table_exists('d6_date_format_locale')) {
  183. // Find all the custom entries in the D6 table.
  184. $result = db_select('d6_date_format_locale', 'old')
  185. ->fields('old', array('format', 'type', 'language'))
  186. ->condition('type', 'custom')
  187. ->execute()
  188. ->fetchAll(PDO::FETCH_ASSOC);
  189. // Iterate over all the old values.
  190. foreach ($result as $row) {
  191. // See if this value already exists in the new table (it might have been
  192. // added manually before this update got run).
  193. $count = db_select('date_format_locale', 'new')
  194. ->condition('format', $row['format'])
  195. ->condition('type', $row['type'])
  196. ->condition('language', $row['language'])
  197. ->countQuery()
  198. ->execute()
  199. ->fetchField();
  200. // If the value is missing, insert it.
  201. // Do nothing if it already exists, assume the value in the
  202. // new table trumps the old values.
  203. if (empty($count)) {
  204. db_insert('date_format_locale')
  205. ->fields(array(
  206. 'format' => $row['format'],
  207. 'type' => $row['type'],
  208. 'language' => $row['language'],
  209. ))
  210. ->execute();
  211. }
  212. }
  213. // Drop the old table.
  214. db_drop_table('d6_date_format_locale');
  215. }
  216. }
  217. /**
  218. * Drop D6 timezone_name field on {users} after upgrading to D7.
  219. */
  220. function date_api_update_7001() {
  221. if (db_field_exists('users', 'timezone_name')) {
  222. db_drop_field('users', 'timezone_name');
  223. }
  224. }