Source of file FormentryContainer.php

Size: 3,774 Bytes - Last Modified: 2016-05-18T03:08:27+02:00

buildproject/core/module_system/admin/formentries/FormentryContainer.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
<?php
/*"******************************************************************************************************
*   (c) 2007-2016 by Kajona, www.kajona.de                                                              *
*       Published under the GNU LGPL v2.1, see /system/licence_lgpl.txt                                 *
********************************************************************************************************/

namespace Kajona\System\Admin\Formentries;

use Kajona\System\Admin\FormentryInterface;
use Kajona\System\Admin\FormentryPrintableInterface;
use Kajona\System\System\Carrier;
use Kajona\System\System\Exception;
use Kajona\System\System\Reflection;


/**
 * A formelement which provides an div container. The container can optional contain other formentry elements.
 *
 * @author  christoph.kappestein@gmail.com
 * @since   4.8
 * @package module_formgenerator
 */
class FormentryContainer extends FormentryBase implements FormentryPrintableInterface {

    protected $arrFields = array();
    protected $strOpener = "";

    public function __construct($strFormName, $strSourceProperty)
    {
        parent::__construct($strFormName, $strSourceProperty);
    }

    /**
     * @param FormentryInterface $formentry
     * @return FormentryBase|FormentryInterface
     */
    public function addField(FormentryBase $objField, $strKey = "")
    {
        if($strKey == "")
            $strKey = $objField->getStrEntryName();

        $this->arrFields[$strKey] = $objField;

        return $objField;
    }

    /**
     * Renders the field itself.
     * In most cases, based on the current toolkit.
     *
     * @return string
     */
    public function renderField() {
        $objToolkit = Carrier::getInstance()->getObjToolkit("admin");
        $strReturn = "";
        if($this->getStrHint() != null)
            $strReturn .= $objToolkit->formTextRow($this->getStrHint());

        $arrFields = array();
        foreach($this->arrFields as $objField) {
            /** @var FormentryInterface $objField */
            $arrFields[] = $objField->renderField();
        }

        $strReturn.= $objToolkit->formInputContainer($this->getStrEntryName(), $this->getStrLabel(), $arrFields, $this->strOpener);

        return $strReturn;
    }

    public function setValueToObject()
    {
        $objSourceObject = $this->getObjSourceObject();
        if($objSourceObject == null)
            return "";

        $objReflection = new Reflection($objSourceObject);
        $strSetter = $objReflection->getSetter($this->getStrSourceProperty());
        if($strSetter === null)
            throw new Exception("unable to find setter for value-property ".$this->getStrSourceProperty()."@".get_class($objSourceObject), Exception::$level_ERROR);

        return $objSourceObject->{$strSetter}(json_encode($this->getStrValue()));
    }

    public function validateValue() {
        return true;
    }

    /**
     * Returns a textual representation of the formentries' value.
     * May contain html, but should be stripped down to text-only.
     *
     * @return string
     */
    public function getValueAsText() {
        $arrFields = array();
        foreach($this->arrFields as $objField) {
            /** @var FormentryPrintableInterface $objField */
            if($objField instanceof FormentryPrintableInterface) {
                $arrFields[] = $objField->getValueAsText();
            }
        }
        return implode(", ", $arrFields);
    }

    /**
     * @return string
     */
    public function getStrOpener()
    {
        return $this->strOpener;
    }

    /**
     * @param string $strOpener
     */
    public function setStrOpener($strOpener)
    {
        $this->strOpener = $strOpener;

        return $this;
    }
}