1: <?php
2: /**
3: * Utility code to make common control management and rendering tasks easier to type.
4: *
5: */
6:
7: // Special Print Functions / Shortcuts
8: // NOTE: These are simply meant to be shortcuts to actual QCubed functional
9: // calls to make your templates a little easier to read. By no means do you have to
10: // use them. Your templates can just as easily make the fully-named method/function calls.
11:
12: /**
13: * Standard Print function. To aid with possible cross-scripting vulnerabilities,
14: * this will automatically perform QApplication::HtmlEntities() unless otherwise specified.
15: *
16: * @param string $strString string value to print
17: * @param boolean $blnHtmlEntities perform HTML escaping on the string first
18: */
19:
20: function _p($strString, $blnHtmlEntities = true) {
21: // Standard Print
22: if ($blnHtmlEntities && (gettype($strString) != 'object'))
23: print(QApplication::HtmlEntities($strString));
24: else
25: print($strString);
26: }
27:
28: /**
29: * Standard Print as Block function. To aid with possible cross-scripting vulnerabilities,
30: * this will automatically perform htmlspecialchars unless otherwise specified.
31: *
32: * Difference between _b() and _p() is that _b() will convert any linebreaks to <br/> tags.
33: * This allows _b() to print any "block" of text that will have linebreaks in standard HTML.
34: *
35: * @param string $strString
36: * @param boolean $blnHtmlEntities
37: */
38: function _b($strString, $blnHtmlEntities = true) {
39: // Text Block Print
40: if ($blnHtmlEntities && (gettype($strString) != 'object'))
41: print(QHtml::RenderString($strString));
42: else
43: print(nl2br($strString));
44: }
45:
46: /**
47: * Standard Print-Translated functions.
48: *
49: * Uses QApplication::Translate() to perform the translation (if applicable)
50: *
51: * @param string $strString string value to print via translation
52: */
53: function _t($strString) {
54: // Print, via Translation (if applicable)
55: print(QApplication::Translate($strString));
56: }
57:
58: /**
59: * Translate
60: * @param $strString
61: * @return string
62: */
63: function _tr($strString) {
64: return QApplication::Translate($strString);
65: }
66:
67: /**
68: * Translate and print encoded for html.
69: *
70: * @param $strString
71: * @param bool $blnHtmlEntities
72: */
73: function _tp($strString, $blnHtmlEntities = true) {
74: _p(_tr($strString), $blnHtmlEntities);
75: }
76:
77: /**
78: * Returns a newline for generating html, only if we are not in MINIMIZE mode. Do NOT use for generating
79: * newlines inside of strings. The primary purpose of this function is to make HTML and other code that will appear
80: * in a browser more readable and easier to debug in development mode.
81: *
82: * If a string is given, it will make sure the string ends with a newline, and return the string with a newline
83: * attached to the end. If a newline is already there, the string will be returned as is. All this provided we
84: * are not in MINIMIZE mode. Otherwise we return the string unchanged.
85: *
86: * @param string|null $strText
87: * @return string
88: */
89: function _nl($strText = null) {
90: if (QApplication::$Minimize) {
91: return $strText;
92: } else {
93: if ($strText === null) return "\n";
94: if (!$strText) return ''; // don't add a shadow newline
95: if (substr($strText, -1) == "\n") {
96: return $strText; // text already ends with a newline
97: } else {
98: return $strText . "\n";
99: }
100: }
101: }
102:
103: /**
104: * Adds an indent to the beginning of every non-empty line in the string, for
105: * the purpose of indenting the text once to the right so it is easier to read when viewing source in a browser and
106: * debugging javascript.
107: *
108: * After some deliberation, since the purpose is mainly for browser source viewing and debugging, the current implementation
109: * uses two spaces as an indent.
110: *
111: * @param string $strText
112: * @param integer $intCount The number of indents to add
113: * @return string
114: */
115: function _indent($strText, $intCount = 1) {
116: if (!defined('__CODE_GENERATING__') && QApplication::$Minimize) {
117: return $strText;
118: } else {
119: if (defined ('__CODE_GENERATING__')) {
120: $strRepeat = ' ';
121: } else {
122: $strRepeat = ' ';
123: }
124: $strTabs = str_repeat($strRepeat, $intCount);
125: $strRet = preg_replace ( '/^/m', $strTabs , $strText);
126: return $strRet;
127: }
128: }
129:
130: /**
131: * Render the control, checking for existence.
132: *
133: * We always return a value, rather than print, because this is designed to primarily be used in a PHP template,
134: * and php now has a shorthand way of printing items by using the opening tag <?= .
135: *
136: * @usage <?= _r($obj, {renderFunc}, ...); ?>
137: * @param QControl $obj
138: * @param null|string $strRenderFunc
139: * @return null|string
140: */
141: function _r($obj, $strRenderFunc = null /*, $overrides */) {
142: $aParams = func_get_args();
143: array_shift($aParams);
144: array_shift($aParams);
145: if ($obj) {
146: if (!$strRenderFunc) {
147: if ($obj->PreferredRenderMethod) {
148: $strRenderFunc = $obj->PreferredRenderMethod;
149: }
150: else {
151: $strRenderFunc = 'Render';
152: }
153: }
154: if (count($aParams) == 1 && is_array($aParams[0])) {
155: return $obj->$strRenderFunc(false, $aParams[0]); // single array of params which is a key->value array
156: }
157: elseif ($aParams) {
158: return call_user_func_array([$obj, $strRenderFunc], $aParams);
159: } else {
160: return $obj->$strRenderFunc(false);
161: }
162: }
163: return null;
164: }
165:
166: /** TODO: Implement the following. */
167: /*
168: function _i($intNumber) {
169: // Not Yet Implemented
170: // Print Integer with Localized Formatting
171: }
172:
173: function _f($intNumber) {
174: // Not Yet Implemented
175: // Print Float with Localized Formatting
176: }
177:
178: function _c($strString) {
179: // Not Yet Implemented
180: // Print Currency with Localized Formatting
181: }*/
182:
183: //////////////////////////////////////
184: