1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
<?php
namespace oroboros\core\traits\utilities\html;
trait HtmlGeneratorTrait {
protected $_content_methods = array(
'row',
'column',
'form',
'table',
'formtable',
'widget',
'navigation',
);
private static $_tabsets = 0;
private static $_datatables = 0;
final private function _checkRequestMethod($method, $checkall = FALSE) {
if (!is_string($method) && ($checkall && !(is_array($method) || is_object($method))))
throw new \oroboros\lib\exception\LibraryException('Invalid parameters passed at ' . __LINE__ . ' of ' . __METHOD__, self::EXCEPTION_CODE_BAD_PARAMETERS);
if (!$checkall) {
if (is_int($method))
return FALSE;
else
return in_array($method, $this->_content_methods);
} elseif ($checkall && is_array($method)) {
$check = TRUE;
foreach ($method as $key => $value) {
if (!$this->_checkRequestMethod($key, 0))
$check = FALSE;
}
return $check;
} elseif ($checkall && is_object($method)) {
return $this->_checkRequestMethod(get_object_vars($method), 1);
}
return FALSE;
}
final private function _evaluateContentArray($array) {
if (!is_array($array)) {
throw new \oroboros\lib\exception\LibraryException('Invalid parameters passed at ' . __LINE__ . ' of ' . __METHOD__, self::EXCEPTION_CODE_BAD_PARAMETERS);
}
if (!array_numeric($array) && !empty($array) && !$this->_checkRequestMethod($array, 1)) {
return $this->_outputCallback($array);
} elseif (array_key_exists('template', $array)) {
throw new \oroboros\lib\exception\LibraryException('Unimplemented request methodology at ' . __LINE__ . ' of ' . __METHOD__, self::_EXCEPTION_CODE_SYSTEM_UNIMPLEMENTED);
} elseif (!empty($array) && $this->_checkRequestMethod($array, 1)) {
$output = array();
foreach ($array as $key => $value) {
if (!is_int($key) && method_exists($this, '_' . $key))
$output[] = array($this->${'_' . $key}($value));
}
return $output;
} else {
$output = array();
foreach ($array as $key => $value) {
$output[] = $this->_evaluateData($value);
}
return $output;
}
}
final private function _evaluateContentObject($object) {
if (!is_object($object))
throw new \oroboros\lib\exception\LibraryException('Invalid parameters passed at ' . __LINE__ . ' of ' . __METHOD__, self::EXCEPTION_CODE_BAD_PARAMETERS);
}
final private function _evaluateData($data) {
return ((is_string($data)) ? $data : ((!isset($data)) ? NULL : ((is_object($data)) ? $this->_evaluateContentObject($data) : $this->_evaluateContentArray($data)) ) );
}
final private function _evaluateObjectMeta($meta, &$data) {
if (!((is_array($meta) && !array_numeric($meta)) || (is_object($meta && !array_numeric(get_object_vars($meta))))))
throw new \oroboros\lib\exception\LibraryException('Invalid parameters passed at ' . __LINE__ . ' of ' . __METHOD__, self::EXCEPTION_CODE_BAD_PARAMETERS);
foreach (((is_object($meta)) ? get_object_vars($meta) : $meta) as $key => $value) {
switch ($key) {
case 'template':
if (isset($this->_template) && is_object($this->_template) && method_exists($this->_template, 'objectMetaParameters')) {
foreach (((is_object($value)) ? get_object_vars($value) : $value) as $name => $args) {
$this->_template->objectMetaParameters($name, $data, $args);
}
} else {
throw new \oroboros\lib\exception\LibraryException('Missing instance of template handler at ' . __LINE__ . ' of ' . __METHOD__, self::_EXCEPTION_CODE_GENERAL_NOT_INITIALIZED);
foreach (((is_object($value)) ? get_object_vars($value) : $value) as $name => $args) {
}
}
break;
case 'theme':
if (isset($this->_theme) && is_object($this->_theme) && method_exists($this->_theme, 'objectMetaParameters')) {
} else {
throw new \oroboros\lib\exception\LibraryException('Missing instance of theme handler at ' . __LINE__ . ' of ' . __METHOD__, self::_EXCEPTION_CODE_GENERAL_NOT_INITIALIZED);
}
break;
case 'module':
throw new \oroboros\lib\exception\LibraryException('Module meta parameters are unimplemented at ' . __LINE__ . ' of ' . __METHOD__, self::_EXCEPTION_CODE_SYSTEM_UNIMPLEMENTED);
break;
case 'component':
throw new \oroboros\lib\exception\LibraryException('Component meta parameters are unimplemented at ' . __LINE__ . ' of ' . __METHOD__, self::_EXCEPTION_CODE_SYSTEM_UNIMPLEMENTED);
break;
default:
throw new \oroboros\lib\exception\LibraryException('Invalid parameters passed at ' . __LINE__ . ' of ' . __METHOD__, self::EXCEPTION_CODE_BAD_PARAMETERS);
break;
}
}
}
protected function _tag($content = NULL, $tag = 'p', $params = NULL, $short = FALSE, $inline = TRUE) {
if (!isset($content)) {
return;
}
return $this->_generateOutput($content, $tag, $params, $short);
}
protected function _generateOutput($content, $tag = 'p', $params = NULL, $short = FALSE, $inline = TRUE) {
$output = array(
'tag' => $tag,
'params' => $params,
'short' => $short,
'inline' => $inline
);
if (is_array($content)) {
if (!array_numeric($content) && !empty($content)) {
$output['content'] = array($this->_generateOutput(((isset($content['content']) ? $content['content'] : NULL)), $content['tag'], ((isset($content['params'])) ? $content['params'] : NULL)));
} elseif (!array_numeric($content) && (!isset($content['content']) || (!array_numeric($content) && isset($content['content']) && empty($content['content'])))) {
$output['content'] = NULL;
} elseif (array_numeric($content)) {
$output['content'] = $content;
} else {
$output['content'] = array_map(array($this, '_generateOutput'), $content);
}
} elseif (is_string($content)) {
$output['content'] = $content;
} elseif (is_object($content)) {
$data_obj = self::_OROBOROS_SYSTEM_DATA_OBJECT;
if (!($content instanceof \stdClass || $content instanceof $data_obj)) {
throw new \OroborosModelException('Invalid object passed at ' . __LINE__ . ' of ' . __METHOD__, self::_EXCEPTION_CODE_GENERAL_INVALID_OBJECT);
}
$output['content'] = $this->_outputCallback(get_object_vars($content));
}
return $this->_generateHtmlTag($output['tag'], $output['content'], $output['params'], $output['short'], $output['inline']);
}
final private function _outputCallback($params) {
$output = array(
'tag' => ((isset($params['tag'])) ? $params['tag'] : 'p'),
'content' => ((isset($params['content'])) ? (is_array($params['content']) && !array_numeric($params['content']) ? array($params['content']) : $params['content']) : NULL),
'params' => ((isset($params['params'])) ? $params['params'] : NULL),
'short' => ((isset($params['short'])) ? $params['short'] : FALSE),
'meta' => ((isset($params['meta'])) ? $params['meta'] : NULL)
);
return $this->_generateOutput($output['content'], $output['tag'], $output['params'], $output['short']);
}
final private function _filterMetaParameters($meta, &$content) {
if (isset($meta)) {
if (is_string($meta) || is_int($meta) || (is_array($meta) && array_numeric($meta)))
throw new \oroboros\lib\exception\LibraryException('Invalid parameters passed at ' . __LINE__ . ' of ' . __METHOD__, self::EXCEPTION_CODE_BAD_PARAMETERS);
$meta = ((is_object($meta)) ? get_object_vars($meta) : $meta);
foreach ($meta as $key => $value) {
switch ($key) {
case 'css':
foreach ($args['css'] as $directive => $params) {
if (method_exists($this, '_queueStylesheet')) {
$this->_queueStylesheet($directive, $params['source'], ((isset($params['inline'])) ? $params['inline'] : FALSE), ( (isset($params['local']) ) ? $params['local'] : FALSE));
}
}
break;
case 'scripts':
foreach ($args['scripts'] as $directive => $params) {
if (method_exists($this, '_queueStylesheet')) {
$this->_queueScript($directive, $params['source'], ((isset($params['inline'])) ? $params['inline'] : FALSE), ( (isset($params['local']) ) ? $params['local'] : FALSE));
}
}
break;
}
}
}
}
protected function _generateHtmlTag($tag = NULL, $content = NULL, $params = NULL, $short = FALSE, $inline = FALSE) {
if (!is_string($tag))
throw new \oroboros\lib\exception\LibraryException('Invalid tag parameter at ' . __METHOD__, self::_EXCEPTION_CODE_GENERAL_BAD_PARAMETERS);
if (isset($content) && (!is_string($content) && !is_array($content)))
throw new \oroboros\lib\exception\LibraryException('Invalid content parameter at ' . __METHOD__, self::_EXCEPTION_CODE_GENERAL_BAD_PARAMETERS);
if (isset($params) && !is_array($params))
throw new \oroboros\lib\exception\LibraryException('Invalid tag arguments params at ' . __METHOD__, self::_EXCEPTION_CODE_GENERAL_BAD_PARAMETERS);
if ($short && $short != TRUE)
throw new \oroboros\lib\exception\LibraryException('Invalid short tag boolean parameter at ' . __METHOD__, self::_EXCEPTION_CODE_GENERAL_BAD_PARAMETERS);
$output = '<' . rtrim(strtolower($tag) . (isset($params) ? ' ' . $this->_packageTagParams($params) . ' ' : NULL), ' ') . (($short) ? ((in_array(strtolower($tag), array('br', 'hr', 'meta', 'link', 'img'))) ? '>' : ' />') : '>') . (($inline) ? NULL : PHP_EOL);
if (!isset($content) && $short)
return $output . (($inline) ? NULL : PHP_EOL);
$output .= ((is_string($content) ? $content : NULL )) . (($inline) ? NULL : PHP_EOL);
if (is_array($content)) {
if (isset($content['meta'])) {
$meta = $this->_handleElementMeta($content['meta']);
} else
$meta = NULL;
foreach ($content as $item) {
if (is_string($item)) {
$output .= $item . (($inline) ? NULL : PHP_EOL);
} else {
if (!isset($item['tag']))
throw new \oroboros\lib\exception\LibraryException('Registered html content does not have a tag value at ' . __METHOD__, self::_EXCEPTION_CODE_GENERAL_MISSING_PARAMETER);
$output .= $this->_generateHtmlTag($item['tag'], (isset($item['content']) ? $item['content'] : NULL), (isset($item['params']) ? $item['params'] : NULL), (isset($item['short']) ? $item['short'] : FALSE), ((isset($item['inline'])) ? $item['inline'] : FALSE)) . (($inline) ? NULL : PHP_EOL);
}
}
}
return $output . (($short) ? null : '</' . strtolower($tag) . '>' . (($inline) ? NULL : PHP_EOL));
}
private function _packageTagParams($params) {
if (!is_array($params))
throw new \OroborosLibraryException('HTML tag params must be an associative array', self::_EXCEPTION_CODE_GENERAL_BAD_PARAMETERS);
$output = null;
foreach ($params as $key => $value) {
$output .= $key . '="' . $value . '" ';
}
return rtrim($output, ' ');
}
}