<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */




abstract class HtmlTag
{
  
  private $_id = '';

  
  private $_classAry = array();

  
  private $_styleAry = array();

  
  private $_title = '';

  
  private $_inhalt = '';

  
  abstract protected function _getTagName();

  protected function _getAttributeList()
  {
    $attributeNameAry = $this->_getAttributeNameAry();
    $attributeString = '';

    foreach ($attributeNameAry as $attributeName) {
      $attributeString .= $this->_getAttributeByName($attributeName) . ' ';
    }

    return $attributeString;
  }

  
  protected function _getAttributeByName($pAttributeName)
  {
    switch ($pAttributeName) {
      default:
        return '';
      case 'id':
        return $this->getIdAttribute();
      case 'class':
        return $this->getClassesAttribute();
      case 'style':
        return $this->getStylesAttribute();
      case 'title':
        return $this->getTitleAttribute();
    }
  }

  
  protected function _getAttributeNameAry()
  {
    return array(
                  'id',
                  'class',
                  'style',
                  'title'
                );
  }

  
  public function getOpenTag()
  {
    return '<' . $this->_getTagName() . ' ' . $this->_getAttributeList() . '>';
  }

  
  public function getCloseTag()
  {
    return '</' . $this->_getTagName() . '>';
  }

  
  public function getTag()
  {
    return $this->getOpenTag() . $this->getInhalt() . $this->getCloseTag();
  }

  
  public function setId($pId)
  {
    $this->_id = $pId;

    return $this;
  }

  
  public function getId()
  {
    return $this->_id;
  }

  
  public function getIdAttribute()
  {
    $id = trim($this->getId());
    if ($id != '') {
      return 'id="' . CleanOperations::cleanStringForHtml($id) . '"';
    } else {
      return '';
    }
  }

  
  public function addClass($pClass)
  {
    if (! is_array($pClass)) {
      $pClass = explode(' ', $pClass);
    }
    $this->_classAry = array_merge($this->_classAry, $pClass);
    $this->_classAry = array_unique($this->_classAry);
  }

  
  public function getClasses()
  {
    return $this->_classAry;
  }

  
  public function getClassesAttribute()
  {
    $classesAry = $this->getClasses();
    if (count($classesAry) > 0) {
      return 'class="' . CleanOperations::cleanStringForHtml(implode(' ', $classesAry)) . '"';
    } else {
      return '';
    }
  }

  
  public function addStyle($pStyle)
  {
    if (! is_array($pStyle)) {
      $pStyle = explode(';', $pStyle);
    }
    $this->_styleAry = array_merge($this->_styleAry, $pStyle);
    $this->_styleAry = array_unique($this->_styleAry);
  }

  
  public function getStyles()
  {
    return $this->_styleAry;
  }

  
  public function getStylesAttribute()
  {
    $stylesAry = $this->getStyles();
    if (count($stylesAry) > 0) {
      return 'style="' . CleanOperations::cleanStringForHtml(implode(' ', $stylesAry)) . '"';
    } else {
      return '';
    }
  }

  
  public function setTitle($pTitle)
  {
    $this->_title = $pTitle;

    return $this;
  }

  
  public function getTitle()
  {
    return $this->_title;
  }

  
  public function getTitleAttribute()
  {
    $title = trim($this->getTitle());
    if ($title != '') {
      return 'title="' . CleanOperations::cleanStringForHtml($title) . '"';
    } else {
      return '';
    }
  }

  
  public function setInhalt($pInhalt)
  {
    $this->_inhalt = $pInhalt;

    return $this;
  }

  
  public function getInhalt()
  {
    return $this->_inhalt;
  }
}

class TextArea extends HtmlTag
{
  
  private $_cols = '';

  
  private $_rows = '';

  
  private $_name = '';

  
  protected function _getTagName()
  {
    return 'textarea';
  }

  
  protected function _getAttributeNameAry()
  {
    return array_merge(parent::_getAttributeNameAry(), array(
             'cols',
             'rows',
             'name'
             ));
  }

  
  protected function _getAttributeByName($pAttributeName)
  {
    switch ($pAttributeName) {
      default:
        return parent::_getAttributeByName($pAttributeName);
      case 'cols':
        return $this->getColsAttribute();
      case 'rows':
        return $this->getRowsAttribute();
      case 'name':
        return $this->getNameAttribute();
    }
  }

  
  public function setCols($pCols)
  {
    $this->_cols = $pCols;

    return $this;
  }

  
  public function getCols()
  {
    return $this->_cols;
  }

  
  public function getColsAttribute()
  {
    $cols = trim($this->getCols());
    if ($cols != '') {
      return 'cols="' . CleanOperations::cleanStringForHtml($cols) . '"';
    } else {
      return '';
    }
  }

  
  public function setRows($pRows)
  {
    $this->_rows = $pRows;

    return $this;
  }

  
  public function getRows()
  {
    return $this->_rows;
  }

  
  public function getRowsAttribute()
  {
    $rows = trim($this->getRows());
    if ($rows != '') {
      return 'rows="' . CleanOperations::cleanStringForHtml($rows) . '"';
    } else {
      return '';
    }
  }

  
  public function setName($pName)
  {
    $this->_name = $pName;

    return $this;
  }

  
  public function getName()
  {
    return $this->_name;
  }

  
  public function getNameAttribute()
  {
    $name = trim($this->getName());
    if ($name != '') {
      return 'name="' . CleanOperations::cleanStringForHtml($name) . '"';
    } else {
      return '';
    }
  }
}

class UnorderedList extends HtmlTag
{
  
  private $_listItemAry = array();

  
  protected function _getTagName()
  {
    return 'ul';
  }

  
  public function addListItems($pListElement)
  {
    if (! is_array($pListElement)) {
      $pListElement = array($pListElement);
    }
    $this->_listItemAry = array_merge($this->_listItemAry, $pListElement);
  }

  
  public function getListItems()
  {
    return $this->_listItemAry;
  }

  
  public function getInhalt()
  {
    $inhalt = '';
    foreach ($this->_listItemAry as $listElement) {
      $inhalt .= '<li>' . CleanOperations::cleanStringContainingHtml($listElement) . '</li>' . "\n";
    }

    return $inhalt;
  }
}