DateFormatTransformationsPlugin.class.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Abstract class for the date format transformations plugins
  5. *
  6. * @package PhpMyAdmin-Transformations
  7. * @subpackage DateFormat
  8. */
  9. if (! defined('PHPMYADMIN')) {
  10. exit;
  11. }
  12. /* Get the transformations interface */
  13. require_once 'libraries/plugins/TransformationsPlugin.class.php';
  14. require_once 'libraries/js_escape.lib.php';
  15. /**
  16. * Provides common methods for all of the date format transformations plugins.
  17. *
  18. * @package PhpMyAdmin
  19. */
  20. abstract class DateFormatTransformationsPlugin extends TransformationsPlugin
  21. {
  22. /**
  23. * Gets the transformation description of the specific plugin
  24. *
  25. * @return string
  26. */
  27. public static function getInfo()
  28. {
  29. return __(
  30. 'Displays a TIME, TIMESTAMP, DATETIME or numeric unix timestamp'
  31. . ' column as formatted date. The first option is the offset (in'
  32. . ' hours) which will be added to the timestamp (Default: 0). Use'
  33. . ' second option to specify a different date/time format string.'
  34. . ' Third option determines whether you want to see local date or'
  35. . ' UTC one (use "local" or "utc" strings) for that. According to'
  36. . ' that, date format has different value - for "local" see the'
  37. . ' documentation for PHP\'s strftime() function and for "utc" it'
  38. . ' is done using gmdate() function.'
  39. );
  40. }
  41. /**
  42. * Does the actual work of each specific transformations plugin.
  43. *
  44. * @param string $buffer text to be transformed
  45. * @param array $options transformation options
  46. * @param string $meta meta information
  47. *
  48. * @return void
  49. */
  50. public function applyTransformation($buffer, $options = array(), $meta = '')
  51. {
  52. // possibly use a global transform and feed it with special options
  53. // further operations on $buffer using the $options[] array.
  54. if (empty($options[0])) {
  55. $options[0] = 0;
  56. }
  57. if (empty($options[2])) {
  58. $options[2] = 'local';
  59. } else {
  60. $options[2] = strtolower($options[2]);
  61. }
  62. if (empty($options[1])) {
  63. if ($options[2] == 'local') {
  64. $options[1] = __('%B %d, %Y at %I:%M %p');
  65. } else {
  66. $options[1] = 'Y-m-d H:i:s';
  67. }
  68. }
  69. $timestamp = -1;
  70. // INT columns will be treated as UNIX timestamps
  71. // and need to be detected before the verification for
  72. // MySQL TIMESTAMP
  73. if ($meta->type == 'int') {
  74. $timestamp = $buffer;
  75. // Detect TIMESTAMP(6 | 8 | 10 | 12 | 14)
  76. // TIMESTAMP (2 | 4) not supported here.
  77. // (Note: prior to MySQL 4.1, TIMESTAMP has a display size
  78. // for example TIMESTAMP(8) means YYYYMMDD)
  79. } else if (preg_match('/^(\d{2}){3,7}$/', $buffer)) {
  80. if (strlen($buffer) == 14 || strlen($buffer) == 8) {
  81. $offset = 4;
  82. } else {
  83. $offset = 2;
  84. }
  85. $d = array();
  86. $d['year'] = substr($buffer, 0, $offset);
  87. $d['month'] = substr($buffer, $offset, 2);
  88. $d['day'] = substr($buffer, $offset + 2, 2);
  89. $d['hour'] = substr($buffer, $offset + 4, 2);
  90. $d['minute'] = substr($buffer, $offset + 6, 2);
  91. $d['second'] = substr($buffer, $offset + 8, 2);
  92. if (checkdate($d['month'], $d['day'], $d['year'])) {
  93. $timestamp = mktime(
  94. $d['hour'],
  95. $d['minute'],
  96. $d['second'],
  97. $d['month'],
  98. $d['day'],
  99. $d['year']
  100. );
  101. }
  102. // If all fails, assume one of the dozens of valid strtime() syntaxes
  103. // (http://www.gnu.org/manual/tar-1.12/html_chapter/tar_7.html)
  104. } else {
  105. if (preg_match('/^[0-9]\d{1,9}$/', $buffer)) {
  106. $timestamp = (int)$buffer;
  107. } else {
  108. $timestamp = strtotime($buffer);
  109. }
  110. }
  111. // If all above failed, maybe it's a Unix timestamp already?
  112. if ($timestamp < 0 && preg_match('/^[1-9]\d{1,9}$/', $buffer)) {
  113. $timestamp = $buffer;
  114. }
  115. // Reformat a valid timestamp
  116. if ($timestamp >= 0) {
  117. $timestamp -= $options[0] * 60 * 60;
  118. $source = $buffer;
  119. if ($options[2] == 'local') {
  120. $text = PMA_Util::localisedDate(
  121. $timestamp,
  122. $options[1]
  123. );
  124. } elseif ($options[2] == 'utc') {
  125. $text = gmdate($options[1], $timestamp);
  126. } else {
  127. $text = 'INVALID DATE TYPE';
  128. }
  129. return '<dfn onclick="alert(\'' . PMA_jsFormat($source, false) . '\');" title="'
  130. . htmlspecialchars($source) . '">' . htmlspecialchars($text) . '</dfn>';
  131. } else {
  132. return htmlspecialchars($buffer);
  133. }
  134. }
  135. /**
  136. * This method is called when any PluginManager to which the observer
  137. * is attached calls PluginManager::notify()
  138. *
  139. * @param SplSubject $subject The PluginManager notifying the observer
  140. * of an update.
  141. *
  142. * @todo implement
  143. * @return void
  144. */
  145. public function update (SplSubject $subject)
  146. {
  147. ;
  148. }
  149. /* ~~~~~~~~~~~~~~~~~~~~ Getters and Setters ~~~~~~~~~~~~~~~~~~~~ */
  150. /**
  151. * Gets the transformation name of the specific plugin
  152. *
  153. * @return string
  154. */
  155. public static function getName()
  156. {
  157. return "Date Format";
  158. }
  159. }
  160. ?>