/mah_apstylee/pi.mah_apstylee.php

https://github.com/mhulse/mah_apstylee · PHP · 427 lines · 154 code · 122 blank · 151 comment · 26 complexity · ba97f91c7bb091bdc7f2a2fe7089431c MD5 · raw file

  1. <?php
  2. if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  3. // --------------------------------------------------------------------
  4. $plugin_info = array(
  5. 'pi_name' => 'APStylee',
  6. 'pi_version' => '1.1',
  7. 'pi_author' => 'Micky Hulse',
  8. 'pi_author_url' => 'https://github.com/mhulse/mah_apstylee',
  9. 'pi_description' => '[Expression Engine 2.0] APStylee: Formats date/time according to AP style standards.',
  10. 'pi_usage' => Mah_apstylee::usage()
  11. );
  12. // --------------------------------------------------------------------
  13. /**
  14. * Mah_apstylee Class
  15. *
  16. * @package ExpressionEngine
  17. * @category Plugin
  18. * @author Micky Hulse
  19. * @copyright Copyright (c) 2011, Micky Hulse
  20. * @link http://hulse.me/
  21. */
  22. class Mah_apstylee {
  23. // ----------------------------------
  24. // Public class variables:
  25. // ----------------------------------
  26. public $return_data = '';
  27. // ----------------------------------
  28. // Private class variables:
  29. // ----------------------------------
  30. private $timestamp = FALSE;
  31. private $ap_noon = FALSE;
  32. private $ap_midnight = FALSE;
  33. private $ap_today = FALSE;
  34. private $ap_not_today = TRUE;
  35. private $ap_year_now = FALSE;
  36. private $ap_not_year_now = TRUE;
  37. private $ap_month_now = FALSE;
  38. private $ap_not_month_now = TRUE;
  39. private $ap_time = '';
  40. private $ap_meridiem = '';
  41. private $ap_day = '';
  42. private $ap_month = '';
  43. private $ap_year = '';
  44. /**
  45. * PHP 4 constructor
  46. *
  47. * @see __construct
  48. */
  49. public function Mah_apstylee()
  50. {
  51. $this->__construct();
  52. }
  53. /**
  54. * Constructor
  55. *
  56. * @access public
  57. * @return mixed
  58. */
  59. function __construct()
  60. {
  61. # Performance Guidelines:
  62. # http://expressionengine.com/public_beta/docs/development/guidelines/performance.html
  63. # General Style and Syntax:
  64. # http://expressionengine.com/public_beta/docs/development/guidelines/general.html
  65. // ----------------------------------
  66. // Call super object:
  67. // ----------------------------------
  68. $this->EE =& get_instance();
  69. // ----------------------------------
  70. // Fetch plugin parameters:
  71. // ----------------------------------
  72. $this->timestamp = $this->EE->TMPL->fetch_param('timestamp');
  73. // ----------------------------------
  74. // Check required:
  75. // ----------------------------------
  76. if ($this->timestamp !== FALSE) {
  77. // ----------------------------------
  78. // Validate timestamp:
  79. // ----------------------------------
  80. $this->timestamp = (is_numeric($this->timestamp) && strtotime($this->timestamp) === FALSE) ? $this->timestamp : strtotime($this->timestamp .' UTC');
  81. // ----------------------------------
  82. // Populate class variables:
  83. // ----------------------------------
  84. $this->ap_meridiem = $this->_ap_meridiem();
  85. $this->ap_time = $this->_ap_time();
  86. $this->ap_day = $this->_ap_day();
  87. $this->ap_month = $this->_ap_month();
  88. $this->ap_year = $this->_ap_year();
  89. // ----------------------------------
  90. // Obtain tag data:
  91. // ----------------------------------
  92. $tagdata = $this->EE->TMPL->tagdata;
  93. // ----------------------------------
  94. // Single or wrapping tag?
  95. // ----------------------------------
  96. if (trim($tagdata) !== ''):
  97. //--------------------------------------------------------------------------
  98. //
  99. // "Wrapping" tag:
  100. //
  101. //--------------------------------------------------------------------------
  102. // ----------------------------------
  103. // Parse variables:
  104. // ----------------------------------
  105. $variables[] = array(
  106. 'ap_noon' => $this->ap_noon,
  107. 'ap_midnight' => $this->ap_midnight,
  108. 'ap_today' => $this->ap_today,
  109. 'ap_not_today' => $this->ap_not_today,
  110. 'ap_year_now' => $this->ap_year_now,
  111. 'ap_not_year_now' => $this->ap_not_year_now,
  112. 'ap_month_now' => $this->ap_month_now,
  113. 'ap_not_month_now' => $this->ap_not_month_now,
  114. 'ap_time' => $this->ap_time,
  115. 'ap_meridiem' => $this->ap_meridiem,
  116. 'ap_day' => $this->ap_day,
  117. 'ap_month' => $this->ap_month,
  118. 'ap_year' => $this->ap_year
  119. );
  120. // ----------------------------------
  121. // Return:
  122. // ----------------------------------
  123. $this->return_data = $this->EE->TMPL->parse_variables($tagdata, $variables); // Exit point 1 of 2.
  124. else:
  125. //--------------------------------------------------------------------------
  126. //
  127. // "Single" tag:
  128. //
  129. //--------------------------------------------------------------------------
  130. // ----------------------------------
  131. // Fetch parameters:
  132. // ----------------------------------
  133. $get_year = $this->EE->TMPL->fetch_param('year'); // Note: values of 'y', 'on' and 'yes' will all return 'yes', while 'n', 'off' and 'no' all return 'no'.
  134. $get_today = $this->EE->TMPL->fetch_param('today');
  135. $get_noon = $this->EE->TMPL->fetch_param('noon');
  136. $get_midnight = $this->EE->TMPL->fetch_param('midnight');
  137. // ----------------------------------
  138. // Determine return value:
  139. // ----------------------------------
  140. $return = '';
  141. switch ($this->EE->TMPL->fetch_param('return')) {
  142. case 'timeonly':
  143. $return .= ((($get_noon !== FALSE) && ($this->ap_noon === TRUE)) ? $get_noon : ((($get_midnight !== FALSE) && ($this->ap_midnight === TRUE)) ? $get_midnight : $this->ap_time)) . ((($get_noon !== FALSE & $this->ap_noon === TRUE) || ($get_midnight !== FALSE & $this->ap_midnight === TRUE)) ? '' : ' ' . $this->ap_meridiem);
  144. break;
  145. case 'dateonly':
  146. $return .= (($get_today !== FALSE) && ($this->ap_today === TRUE)) ? $get_today : $this->ap_month . ' ' . $this->ap_day . (($get_year === 'yes') ? ', ' . $this->ap_year : (($this->ap_year_now !== TRUE) ? ', ' . $this->ap_year : ''));
  147. break;
  148. case 'time':
  149. $return .= $this->ap_time;
  150. break;
  151. case 'meridiem':
  152. $return .= $this->ap_meridiem;
  153. break;
  154. case 'day':
  155. $return .= (($get_today !== FALSE) && ($this->ap_today === TRUE)) ? $get_today : $this->ap_day;
  156. break;
  157. case 'month':
  158. $return .= $this->ap_month;
  159. break;
  160. case 'year':
  161. $return .= $this->ap_year;
  162. break;
  163. default:
  164. $return .= ((($get_noon !== FALSE) && ($this->ap_noon === TRUE)) ? $get_noon : ((($get_midnight !== FALSE) && ($this->ap_midnight === TRUE)) ? $get_midnight : $this->ap_time)) . ((($get_noon !== FALSE & $this->ap_noon === TRUE) || ($get_midnight !== FALSE & $this->ap_midnight === TRUE)) ? '' : ' ' . $this->ap_meridiem); // timeonly
  165. $return .= ', '; // Add param for date/time separator?
  166. $return .= (($get_today !== FALSE) && ($this->ap_today === TRUE)) ? $get_today : $this->ap_month . ' ' . $this->ap_day . (($get_year === 'yes') ? ', ' . $this->ap_year : (($this->ap_year_now !== TRUE) ? ', ' . $this->ap_year : '')); // dateonly
  167. }
  168. // ----------------------------------
  169. // Return:
  170. // ----------------------------------
  171. $this->return_data = $return; // Exit point 2 of 2.
  172. endif;
  173. }
  174. }
  175. //--------------------------------------------------------------------------
  176. //
  177. // Public methods:
  178. //
  179. //--------------------------------------------------------------------------
  180. /**
  181. * Plugin "third segment" method
  182. *
  183. * @access private
  184. * @return string
  185. */
  186. public function single()
  187. {
  188. /*
  189. **
  190. ** Using a "third segment" class method bypasses the "random" bug.
  191. ** http://emarketsouth.com/add-ons/string-plugin/template-tags/#random-parameter
  192. **
  193. */
  194. return $this->return_data;
  195. }
  196. //--------------------------------------------------------------------------
  197. //
  198. // Private methods:
  199. //
  200. //--------------------------------------------------------------------------
  201. /**
  202. * Gets meridiem
  203. *
  204. * @access private
  205. * @return string
  206. */
  207. private function _ap_meridiem()
  208. {
  209. return ($this->EE->localize->decode_date('%a', $this->timestamp) == 'am') ? 'a.m.' : 'p.m.';
  210. }
  211. // --------------------------------------------------------------------
  212. /**
  213. * Gets time
  214. *
  215. * @access private
  216. * @return string
  217. */
  218. private function _ap_time()
  219. {
  220. // Noon or midnight?
  221. $t = $this->EE->localize->decode_date('%H:%i', $this->timestamp);
  222. if ($t == '00:00'): $this->ap_midnight = TRUE;
  223. elseif ($t == '12:00'): $this->ap_noon = TRUE;
  224. endif;
  225. // Leading zeros:
  226. if ($this->EE->localize->decode_date('%i', $this->timestamp) == '00'): $return = $this->EE->localize->decode_date('%g', $this->timestamp);
  227. else: $return = $this->EE->localize->decode_date('%g:%i', $this->timestamp);
  228. endif;
  229. return $return;
  230. }
  231. // --------------------------------------------------------------------
  232. /**
  233. * Gets day
  234. *
  235. * @access private
  236. * @return string
  237. */
  238. private function _ap_day()
  239. {
  240. if ($this->EE->localize->decode_date('%F %j %Y', $this->timestamp) == $this->EE->localize->decode_date('%F %j %Y', $this->EE->localize->now)):
  241. $this->ap_today = TRUE;
  242. $this->ap_not_today = FALSE;
  243. endif;
  244. return $this->EE->localize->decode_date('%j', $this->timestamp);
  245. }
  246. // --------------------------------------------------------------------
  247. /**
  248. * Gets month
  249. *
  250. * @access private
  251. * @return string
  252. */
  253. private function _ap_month()
  254. {
  255. $m = $this->EE->localize->decode_date('%m', $this->timestamp);
  256. if ($m == $this->EE->localize->decode_date('%m', $this->EE->localize->now)):
  257. $this->ap_month_now = TRUE;
  258. $this->ap_not_month_now = FALSE;
  259. endif;
  260. $ap_months_array = array('01' => 'Jan.', '02' => 'Feb.', '08' => 'Aug.', '09' => 'Sept.', '10' => 'Oct.', '11' => 'Nov.', '12' => 'Dec.');
  261. // All months with five letters or less are never abbreviated:
  262. return (isset($ap_months_array[$m])) ? $ap_months_array[$m] : $this->EE->localize->decode_date('%F', $this->timestamp);
  263. }
  264. // --------------------------------------------------------------------
  265. /**
  266. * Gets year
  267. *
  268. * @access private
  269. * @return string
  270. */
  271. private function _ap_year()
  272. {
  273. $y = $this->EE->localize->decode_date('%Y', $this->timestamp);
  274. if ($y == $this->EE->localize->decode_date('%Y', $this->EE->localize->now)):
  275. $this->ap_year_now = TRUE;
  276. $this->ap_not_year_now = FALSE;
  277. endif;
  278. return $y;
  279. }
  280. //--------------------------------------------------------------------------
  281. //
  282. // Usage:
  283. //
  284. //--------------------------------------------------------------------------
  285. /**
  286. * Plugin Usage
  287. *
  288. * @access public
  289. * @return string
  290. */
  291. public function usage()
  292. {
  293. ob_start();
  294. ?>
  295. More information & documentation:
  296. https://github.com/mhulse/mah_apstylee
  297. <?php
  298. $buffer = ob_get_contents();
  299. ob_end_clean();
  300. return $buffer;
  301. }
  302. // --------------------------------------------------------------------
  303. }
  304. /* End of file pi.Mah_apstylee.php */
  305. /* Location: ./system/expressionengine/third_party/mah_eencode/pi.mah_apstylee.php */