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: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474: 475: 476: 477: 478: 479: 480: 481: 482: 483: 484: 485: 486: 487: 488: 489: 490: 491: 492: 493: 494: 495: 496: 497: 498: 499: 500: 501: 502: 503: 504: 505: 506: 507: 508: 509: 510: 511: 512: 513: 514: 515: 516: 517: 518: 519: 520: 521: 522: 523: 524: 525: 526: 527: 528: 529: 530: 531: 532: 533: 534: 535: 536: 537: 538: 539: 540:
<?php
namespace Nethgui\Widget;
abstract class XhtmlWidget extends AbstractWidget implements \Nethgui\View\CommandReceiverInterface
{
protected function getRenderer()
{
return $this->view;
}
private function label($text, $id)
{
$labelWidget = new Xhtml\TextLabel($this->view);
call_user_func($this->di, $labelWidget);
$labelWidget
->setAttribute('template', $text)
->setAttribute('tag', 'label')
->setAttribute('class', $this->getAttribute('helpId', $this->view->getUniqueId($this->getAttribute('name', FALSE))))
->setAttribute('htmlAttributes', array('for' => $id))
;
if ($this->hasAttribute('labelSource')) {
$labelWidget->setAttribute('name', $this->getAttribute('labelSource'));
}
if ($this->hasAttribute('flags')) {
$labelWidget->setAttribute('flags', $this->getAttribute('flags'));
}
return $labelWidget->render();
}
protected function labeledControlTag($label, $tag, $name, $flags, $cssClass = '', $attributes = array(), $tagContent = '')
{
if (isset($attributes['id'])) {
$controlId = $attributes['id'];
} elseif ($this->hasAttribute('receiver')) {
$controlId = $this->view->getUniqueId($this->getAttribute('receiver'));
$attributes['id'] = $controlId;
} else {
$controlId = $this->view->getUniqueId($name);
$attributes['id'] = $controlId;
}
$wrapperClass = 'labeled-control';
if ($this->hasAttribute('labelWrapClass')) {
$wrapperClass = $this->getAttribute('labelWrapClass');
}
$content = '';
if ($flags & \Nethgui\Renderer\WidgetFactoryInterface::LABEL_NONE) {
$content .= $this->controlTag($tag, $name, $flags, $cssClass, $attributes, $tagContent);
} else {
if ($flags & \Nethgui\Renderer\WidgetFactoryInterface::LABEL_RIGHT) {
$wrapperClass .= ' label-right';
$content .= $this->controlTag($tag, $name, $flags, $cssClass, $attributes, $tagContent);
$content .= $this->label($label, $controlId);
} else {
if ($flags & \Nethgui\Renderer\WidgetFactoryInterface::LABEL_ABOVE) {
$wrapperClass .= ' label-above';
} elseif ($flags & \Nethgui\Renderer\WidgetFactoryInterface::LABEL_LEFT) {
$wrapperClass .= ' label-left';
}
$content .= $this->label($label, $controlId);
$content .= $this->controlTag($tag, $name, $flags, $cssClass, $attributes, $tagContent);
}
$wrapTag = $this->getAttribute('labelWrapTag', 'div');
if ($wrapTag) {
$content = $this->openTag($wrapTag, array('class' => $wrapperClass)) . $content . $this->closeTag($wrapTag);
}
}
return $content;
}
protected function controlTag($tag, $name, $flags, $cssClass = '', $attributes = array(), $tagContent = '')
{
$flags |= intval($this->getAttribute('flags'));
$tag = strtolower($tag);
if ( ! isset($attributes['id'])) {
$attributes['id'] = $this->view->getUniqueId($name);
}
if ( ! isset($attributes['name'])) {
$attributes['name'] = $this->getControlName($name);
}
$isCheckable = ($tag == 'input') && isset($attributes['type']) && ($attributes['type'] == 'checkbox' || $attributes['type'] == 'radio');
if ($flags & \Nethgui\Renderer\WidgetFactoryInterface::STATE_CHECKED && $isCheckable) {
$attributes['checked'] = 'checked';
}
if ($flags & \Nethgui\Renderer\WidgetFactoryInterface::STATE_READONLY) {
if ($isCheckable) {
$attributes['disabled'] = 'disabled';
} else {
$attributes['readonly'] = 'readonly';
}
}
if ($tag == 'button') {
if (empty($tagContent)) {
$tagContent = $attributes['value'];
}
}
if (in_array($tag, array('input', 'button', 'textarea', 'select', 'optgroup', 'option'))) {
if ($flags & \Nethgui\Renderer\WidgetFactoryInterface::STATE_DISABLED) {
$attributes['disabled'] = 'disabled';
$cssClass .= ' keepdisabled';
}
if ($flags & \Nethgui\Renderer\WidgetFactoryInterface::STATE_VALIDATION_ERROR) {
$cssClass .= ' validation-error ui-state-error';
}
}
if ( ! isset($attributes['class'])) {
$attributes['class'] = trim($cssClass . ' ' . $this->getClientEventTarget());
}
$content = '';
if ($tagContent == '' && $tag !== 'textarea') {
$content .= $this->selfClosingTag($tag, $attributes);
} else {
$content .= $this->openTag($tag, $attributes);
$content .= $tagContent;
$content .= $this->closeTag($tag);
}
return $content;
}
protected function applyDefaultLabelAlignment($flags, $default)
{
return (\Nethgui\Renderer\WidgetFactoryInterface::LABEL_NONE | \Nethgui\Renderer\WidgetFactoryInterface::LABEL_ABOVE | \Nethgui\Renderer\WidgetFactoryInterface::LABEL_LEFT | \Nethgui\Renderer\WidgetFactoryInterface::LABEL_RIGHT) & $flags ? $flags : $flags | $default;
}
protected function openTag($tag, $attributes = array())
{
$tag = strtolower($tag);
$attributeString = $this->prepareXhtmlAttributes($attributes);
return sprintf('<%s%s>', $tag, $attributeString);
}
protected function selfClosingTag($tag, $attributes)
{
$tag = strtolower($tag);
return sprintf('<%s%s />', $tag, $this->prepareXhtmlAttributes($attributes));
}
protected function closeTag($tag)
{
return sprintf('</%s>', strtolower($tag));
}
private function prepareXhtmlAttributes($attributes)
{
$content = '';
foreach ($attributes as $attribute => $value) {
if ($value === FALSE) {
continue;
}
$content .= $attribute . '=\'' . htmlspecialchars($value, ENT_QUOTES) . '\' ';
}
return ' ' . trim($content);
}
protected function getControlName($parts = '')
{
$nameSegments = $this->view->resolvePath($parts);
$prefix = array_shift($nameSegments);
return $prefix . '[' . implode('][', $nameSegments) . ']';
}
protected function getJsWidgetTypes()
{
return array(
'Nethgui:' . strtolower(\Nethgui\array_end(explode('\\', get_class($this))))
);
}
public function render()
{
$this->invokeCommands();
$content = parent::render();
if ($this->canEscapeUnobstrusive($this->getAttribute('flags', 0))) {
$content = $this->escapeUnobstrusive($content);
}
if (NETHGUI_ENABLE_INCLUDE_WIDGET) {
$types = array_merge(array('Nethgui:base'), $this->getJsWidgetTypes());
foreach ($types as $type) {
$typeParts = explode(':', $type);
$this->getRenderer()->includeFile(sprintf('%s/Js/jquery.nethgui.%s.js', $typeParts[0], $typeParts[1]));
}
}
return $content;
}
private function invokeCommands()
{
if ( ! $this->hasAttribute('receiver')) {
return;
}
$targetName = $this->getAttribute('receiver');
if ( ! $this->view->hasCommandList($targetName)) {
return;
}
$command = $this->view->getCommandList($targetName);
if ( ! $command->isExecuted()) {
$command->setReceiver($this)->execute();
}
}
public function executeCommand(\Nethgui\View\ViewInterface $origin, $selector, $name, $arguments)
{
$this->getLog()->deprecated(sprintf("%%s %%s: %s() command is DEPRECATED on Xhtml widget!", __CLASS__, $name));
if ($name === 'requireFlag') {
$flags = intval($arguments[0]);
$this->setAttribute('flags', $flags | $this->getAttribute('flags', 0));
return;
} elseif ($name === 'rejectFlag') {
$flags = ~intval($arguments[0]);
$this->setAttribute('flags', $flags & $this->getAttribute('flags', 0));
} elseif ($name === 'enable') {
$this->executeCommand($origin, $selector, 'rejectFlag', array(\Nethgui\Renderer\WidgetFactoryInterface::STATE_DISABLED));
} elseif ($name === 'disable') {
$this->executeCommand($origin, $selector, 'requireFlag', array(\Nethgui\Renderer\WidgetFactoryInterface::STATE_DISABLED));
}
}
protected function getTranslateClosure($message, $args = array())
{
$view = $this->view;
$f = function($attributeName) use ($view, $message, $args) {
return $view->translate($message, $args);
};
return $f;
}
protected function escapeUnobstrusive($content)
{
return '<script class="unobstrusive">document.write(' . json_encode(strval($content)) . ');</script>';
}
protected function canEscapeUnobstrusive($flags)
{
$unobtrusiveRequired = ($flags & \Nethgui\Renderer\WidgetFactoryInterface::STATE_UNOBTRUSIVE);
$unobtrusiveApplying = ($this->getRenderer()->getDefaultFlags() & \Nethgui\Renderer\WidgetFactoryInterface::STATE_UNOBTRUSIVE);
return $unobtrusiveRequired && ! $unobtrusiveApplying;
}
public function insertPlugins($name = 'Plugin')
{
$pluginList = array();
if (empty($this->view[$name])) {
return $this;
}
foreach ($this->view[$name] as $pluginView) {
if ($pluginView instanceof \Nethgui\View\ViewInterface) {
$pluginModule = $pluginView->getModule();
$cat = $pluginModule->getAttributesProvider()->getCategory();
if ( ! isset($pluginList[$cat])) {
$pluginList[$cat] = $this->view->panel()
->setAttribute('name', $cat)
->setAttribute('title', $pluginView->translate($cat . '_Title'))
->setAttribute('isPluginPlaceholder', TRUE)
;
}
$pluginLiteral = $this->view->literal($pluginView);
$pluginLiteral->setAttribute('isPlugin', TRUE);
$pluginList[$cat]->insert($pluginLiteral);
} else {
$this->insert($this->view->literal($pluginView));
}
}
ksort($pluginList);
foreach ($pluginList as $plugin) {
$this->insert($plugin);
}
return $this;
}
protected function optGroups($value, $choices)
{
$tagContent = '';
if ( ! is_array($choices)) {
throw new \InvalidArgumentException(sprintf('%s: invalid choices type (%s). Must be an array', __CLASS__, gettype($choices)), 1340631080);
}
foreach (array_values($choices) as $choice) {
$labelText = ! empty($choice[1]) ? $choice[1] : htmlspecialchars(strval($choice[0]));
if (is_array($choice[0])) {
$tagContent .= $this->openTag('optgroup', array('label' => $labelText));
$tagContent .= $this->optGroups($value, $choice[0]);
$tagContent .= $this->closeTag('optgroup');
} else {
$tagContent .= $this->openTag('option', array('value' => $choice[0], 'selected' => ($value == $choice[0] ? 'selected' : FALSE)));
$tagContent .= $labelText;
$tagContent .= $this->closeTag('option');
}
}
return $tagContent;
}
public static function hashToDatasource($H, $sort = FALSE)
{
$D = array();
if ( ! is_array($H) && ! $H instanceof \Traversable) {
return $D;
}
foreach ($H as $k => $v) {
if (is_array($v)) {
$D[] = array(self::hashToDatasource($v, $sort), $k);
} elseif (is_string($v)) {
$D[] = array($k, $v);
}
}
if ($sort === TRUE) {
usort($D, function($a, $b) {
return strcasecmp($a[1], $b[1]);
});
}
return $D;
}
protected function getChoices($name, &$dataSourceName)
{
$choices = $this->getAttribute('choices', $name . 'Datasource');
if (is_string($choices)) {
$dataSourceName = $choices;
if (isset($this->view[$dataSourceName])) {
$choices = $this->view[$dataSourceName];
}
} else {
$dataSourceName = $name . 'Datasource';
}
if ($choices instanceof \Traversable) {
$choices = iterator_to_array($choices);
} elseif ( ! is_array($choices)) {
$choices = array();
}
return $choices;
}
}