Skip to content
This repository

Cleanup of JHtmlBehavior::_calendartranslation() #1414

Closed
wants to merge 2 commits into from

3 participants

Elijah Madden Rouven Weßling elinw
Elijah Madden

Cleanup of JHtmlBehavior::_calendartranslation() for much better readability

Rouven Weßling
Collaborator

Good idea!

You got a couple of style issues thou: http://developer.joomla.org/pulls/pulls/1414.html

Elijah Madden okonomiyaki3000 closed this July 31, 2012
Elijah Madden okonomiyaki3000 reopened this July 31, 2012
Elijah Madden

Are we OK now?

Elijah Madden okonomiyaki3000 closed this October 04, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 75 additions and 52 deletions. Show diff stats Hide diff stats

  1. 127  libraries/joomla/html/html/behavior.php
127  libraries/joomla/html/html/behavior.php
@@ -881,60 +881,83 @@ protected static function _calendartranslation()
881 881
 	{
882 882
 		static $jsscript = 0;
883 883
 
884  
-		if ($jsscript == 0)
885  
-		{
886  
-			$return = 'Calendar._DN = new Array ("' . JText::_('SUNDAY', true) . '", "' . JText::_('MONDAY', true) . '", "'
887  
-				. JText::_('TUESDAY', true) . '", "' . JText::_('WEDNESDAY', true) . '", "' . JText::_('THURSDAY', true) . '", "'
888  
-				. JText::_('FRIDAY', true) . '", "' . JText::_('SATURDAY', true) . '", "' . JText::_('SUNDAY', true) . '");'
889  
-				. ' Calendar._SDN = new Array ("' . JText::_('SUN', true) . '", "' . JText::_('MON', true) . '", "' . JText::_('TUE', true) . '", "'
890  
-				. JText::_('WED', true) . '", "' . JText::_('THU', true) . '", "' . JText::_('FRI', true) . '", "' . JText::_('SAT', true) . '", "'
891  
-				. JText::_('SUN', true) . '");' . ' Calendar._FD = 0;' . ' Calendar._MN = new Array ("' . JText::_('JANUARY', true) . '", "'
892  
-				. JText::_('FEBRUARY', true) . '", "' . JText::_('MARCH', true) . '", "' . JText::_('APRIL', true) . '", "' . JText::_('MAY', true)
893  
-				. '", "' . JText::_('JUNE', true) . '", "' . JText::_('JULY', true) . '", "' . JText::_('AUGUST', true) . '", "'
894  
-				. JText::_('SEPTEMBER', true) . '", "' . JText::_('OCTOBER', true) . '", "' . JText::_('NOVEMBER', true) . '", "'
895  
-				. JText::_('DECEMBER', true) . '");' . ' Calendar._SMN = new Array ("' . JText::_('JANUARY_SHORT', true) . '", "'
896  
-				. JText::_('FEBRUARY_SHORT', true) . '", "' . JText::_('MARCH_SHORT', true) . '", "' . JText::_('APRIL_SHORT', true) . '", "'
897  
-				. JText::_('MAY_SHORT', true) . '", "' . JText::_('JUNE_SHORT', true) . '", "' . JText::_('JULY_SHORT', true) . '", "'
898  
-				. JText::_('AUGUST_SHORT', true) . '", "' . JText::_('SEPTEMBER_SHORT', true) . '", "' . JText::_('OCTOBER_SHORT', true) . '", "'
899  
-				. JText::_('NOVEMBER_SHORT', true) . '", "' . JText::_('DECEMBER_SHORT', true) . '");'
900  
-				. ' Calendar._TT = {};Calendar._TT["INFO"] = "' . JText::_('JLIB_HTML_BEHAVIOR_ABOUT_THE_CALENDAR', true) . '";'
901  
-				. ' Calendar._TT["ABOUT"] =
902  
- "DHTML Date/Time Selector\n" +
903  
- "(c) dynarch.com 2002-2005 / Author: Mihai Bazon\n" +
904  
-"For latest version visit: http://www.dynarch.com/projects/calendar/\n" +
905  
-"Distributed under GNU LGPL.  See http://gnu.org/licenses/lgpl.html for details." +
906  
-"\n\n" +
907  
-"' . JText::_('JLIB_HTML_BEHAVIOR_DATE_SELECTION', false, false) . '" +
908  
-"' . JText::_('JLIB_HTML_BEHAVIOR_YEAR_SELECT', false, false) . '" +
909  
-"' . JText::_('JLIB_HTML_BEHAVIOR_MONTH_SELECT', false, false) . '" +
910  
-"' . JText::_('JLIB_HTML_BEHAVIOR_HOLD_MOUSE', false, false)
911  
-				. '";
912  
-Calendar._TT["ABOUT_TIME"] = "\n\n" +
913  
-"Time selection:\n" +
914  
-"- Click on any of the time parts to increase it\n" +
915  
-"- or Shift-click to decrease it\n" +
916  
-"- or click and drag for faster selection.";
917  
-
918  
-		Calendar._TT["PREV_YEAR"] = "' . JText::_('JLIB_HTML_BEHAVIOR_PREV_YEAR_HOLD_FOR_MENU', true) . '";' . ' Calendar._TT["PREV_MONTH"] = "'
919  
-				. JText::_('JLIB_HTML_BEHAVIOR_PREV_MONTH_HOLD_FOR_MENU', true) . '";' . ' Calendar._TT["GO_TODAY"] = "'
920  
-				. JText::_('JLIB_HTML_BEHAVIOR_GO_TODAY', true) . '";' . ' Calendar._TT["NEXT_MONTH"] = "'
921  
-				. JText::_('JLIB_HTML_BEHAVIOR_NEXT_MONTH_HOLD_FOR_MENU', true) . '";' . ' Calendar._TT["NEXT_YEAR"] = "'
922  
-				. JText::_('JLIB_HTML_BEHAVIOR_NEXT_YEAR_HOLD_FOR_MENU', true) . '";' . ' Calendar._TT["SEL_DATE"] = "'
923  
-				. JText::_('JLIB_HTML_BEHAVIOR_SELECT_DATE', true) . '";' . ' Calendar._TT["DRAG_TO_MOVE"] = "'
924  
-				. JText::_('JLIB_HTML_BEHAVIOR_DRAG_TO_MOVE', true) . '";' . ' Calendar._TT["PART_TODAY"] = "'
925  
-				. JText::_('JLIB_HTML_BEHAVIOR_TODAY', true) . '";' . ' Calendar._TT["DAY_FIRST"] = "'
926  
-				. JText::_('JLIB_HTML_BEHAVIOR_DISPLAY_S_FIRST', true) . '";' . ' Calendar._TT["WEEKEND"] = "0,6";' . ' Calendar._TT["CLOSE"] = "'
927  
-				. JText::_('JLIB_HTML_BEHAVIOR_CLOSE', true) . '";' . ' Calendar._TT["TODAY"] = "' . JText::_('JLIB_HTML_BEHAVIOR_TODAY', true)
928  
-				. '";' . ' Calendar._TT["TIME_PART"] = "' . JText::_('JLIB_HTML_BEHAVIOR_SHIFT_CLICK_OR_DRAG_TO_CHANGE_VALUE', true) . '";'
929  
-				. ' Calendar._TT["DEF_DATE_FORMAT"] = "%Y-%m-%d";' . ' Calendar._TT["TT_DATE_FORMAT"] = "'
930  
-				. JText::_('JLIB_HTML_BEHAVIOR_TT_DATE_FORMAT', true) . '";' . ' Calendar._TT["WK"] = "' . JText::_('JLIB_HTML_BEHAVIOR_WK', true) . '";'
931  
-				. ' Calendar._TT["TIME"] = "' . JText::_('JLIB_HTML_BEHAVIOR_TIME', true) . '";';
932  
-			$jsscript = 1;
933  
-			return $return;
934  
-		}
935  
-		else
  884
+		// Guard clause, avoids unnecessary nesting
  885
+		if ($jsscript)
936 886
 		{
937 887
 			return false;
938 888
 		}
  889
+
  890
+		// To keep the code simple here, run strings through JText::_() using array_map()
  891
+		$callback = array('JText','_');
  892
+		$weekdays_full = array_map(
  893
+			$callback, array(
  894
+				'SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY'
  895
+			)
  896
+		);
  897
+		$weekdays_short = array_map(
  898
+			$callback,
  899
+			array(
  900
+				'SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN'
  901
+			)
  902
+		);
  903
+		$months_long = array_map(
  904
+			$callback, array(
  905
+				'JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE',
  906
+				'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER'
  907
+			)
  908
+		);
  909
+		$months_short = array_map(
  910
+			$callback, array(
  911
+				'JANUARY_SHORT', 'FEBRUARY_SHORT', 'MARCH_SHORT', 'APRIL_SHORT', 'MAY_SHORT', 'JUNE_SHORT',
  912
+				'JULY_SHORT', 'AUGUST_SHORT', 'SEPTEMBER_SHORT', 'OCTOBER_SHORT', 'NOVEMBER_SHORT', 'DECEMBER_SHORT'
  913
+			)
  914
+		);
  915
+
  916
+		// This will become an object in Javascript but define it first in PHP for readability
  917
+		$text = array(
  918
+			'INFO'			=> JText::_('JLIB_HTML_BEHAVIOR_ABOUT_THE_CALENDAR'),
  919
+
  920
+			'ABOUT'			=> "DHTML Date/Time Selector\n"
  921
+				. "(c) dynarch.com 2002-2005 / Author: Mihai Bazon\n"
  922
+				. "For latest version visit: http://www.dynarch.com/projects/calendar/\n"
  923
+				. "Distributed under GNU LGPL.  See http://gnu.org/licenses/lgpl.html for details."
  924
+				. "\n\n"
  925
+				. JText::_('JLIB_HTML_BEHAVIOR_DATE_SELECTION')
  926
+				. JText::_('JLIB_HTML_BEHAVIOR_YEAR_SELECT')
  927
+				. JText::_('JLIB_HTML_BEHAVIOR_MONTH_SELECT')
  928
+				. JText::_('JLIB_HTML_BEHAVIOR_HOLD_MOUSE'),
  929
+
  930
+			'ABOUT_TIME'	=> "\n\n"
  931
+				. "Time selection:\n"
  932
+				. "- Click on any of the time parts to increase it\n"
  933
+				. "- or Shift-click to decrease it\n"
  934
+				. "- or click and drag for faster selection.",
  935
+
  936
+			'PREV_YEAR'		=> JText::_('JLIB_HTML_BEHAVIOR_PREV_YEAR_HOLD_FOR_MENU'),
  937
+			'PREV_MONTH'	=> JText::_('JLIB_HTML_BEHAVIOR_PREV_MONTH_HOLD_FOR_MENU'),
  938
+			'GO_TODAY'		=> JText::_('JLIB_HTML_BEHAVIOR_GO_TODAY'),
  939
+			'NEXT_MONTH'	=> JText::_('JLIB_HTML_BEHAVIOR_NEXT_MONTH_HOLD_FOR_MENU'),
  940
+			'SEL_DATE'		=> JText::_('JLIB_HTML_BEHAVIOR_SELECT_DATE'),
  941
+			'DRAG_TO_MOVE'	=> JText::_('JLIB_HTML_BEHAVIOR_DRAG_TO_MOVE'),
  942
+			'PART_TODAY'	=> JText::_('JLIB_HTML_BEHAVIOR_TODAY'),
  943
+			'DAY_FIRST'		=> JText::_('JLIB_HTML_BEHAVIOR_DISPLAY_S_FIRST'),
  944
+			'WEEKEND'		=> "0,6",
  945
+			'CLOSE'			=> JText::_('JLIB_HTML_BEHAVIOR_CLOSE'),
  946
+			'TODAY'			=> JText::_('JLIB_HTML_BEHAVIOR_TODAY'),
  947
+			'TIME_PART'		=> JText::_('JLIB_HTML_BEHAVIOR_SHIFT_CLICK_OR_DRAG_TO_CHANGE_VALUE'),
  948
+			'DEF_DATE_FORMAT'	=> "%Y-%m-%d",
  949
+			'TT_DATE_FORMAT'	=> JText::_('JLIB_HTML_BEHAVIOR_TT_DATE_FORMAT'),
  950
+			'WK'			=> JText::_('JLIB_HTML_BEHAVIOR_WK'),
  951
+			'TIME'			=> JText::_('JLIB_HTML_BEHAVIOR_TIME')
  952
+		);
  953
+
  954
+		$jsscript = 1;
  955
+
  956
+		return 'Calendar._DN = ' . json_encode($weekdays_full) . ';'
  957
+			. ' Calendar._SDN = ' . json_encode($weekdays_short) . ';'
  958
+			. ' Calendar._FD = 0;'
  959
+			. ' Calendar._MN = ' . json_encode($months_long) . ';'
  960
+			. ' Calendar._SMN = ' . json_encode($months_short) . ';'
  961
+			. ' Calendar._TT = ' . json_encode($text) . ';';
939 962
 	}
940 963
 }
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.