<?php

class MCheckedTextField extends MTextField
{
    public $checked = false;

    public function __construct( $name='',$value='',$label='', $size=10, $hint='', $validator=NULL, $isReadOnly=false, $checked = false )
    {
        $this->checked = $checked;
        parent::__construct($name,$value,$label, $size, $hint, $validator, $isReadOnly);
    }

    public function generate()
    {
        $this->generateInner();
        $content = $this->getInner();

        if ( $this->jsHint != '' )
        {
            $jsHint = $this->jsHint;
            $id = '';

            if ( $this->manager->checkMobile() )
            {
                $id = $this->getId() . 'MobileTooltip';
                $dojoType = 'dojox.mobile.Tooltip';
                $this->page->addDojoRequire($dojoType);
                $attributes = "id=\"$id\" dojoType=\"$dojoType\" aroundNode=\"{$this->getId()}\" ";
                $this->addEvent('focus', "dijit.registry.byId('$id').show(this, ['after','below-centered','above-centered']);");
                $this->addEvent('blur', "dijit.registry.byId('$id').hide();");

                $hint = new MDiv($id, $jsHint, null, $attributes);
                $content = $hint->generate() . $content;
            }
            else
            {
                $dojoType = 'dijit.Tooltip';
                $this->page->addDojoRequire($dojoType);
                $attributes = "dojoType=\"$dojoType\" connectId=\"{$this->getId()}\" ";

                $hint = new MDiv('', '', null, $attributes);
                $hint->addAttribute("label",$jsHint);
                $content = $hint->generate() . $content;
            }
        }

        if ( $this->hint && MBaseForm::$showHints )
        {
            $hint = new MHint($this->hint);
            $content .= $hint->generate();
        }

        $this->generateEvent();

        $icone = "fa fa-times text-danger fa-fw";
        if ($this->checked)
            $icone = "fa fa-check text-primary fa-fw";

        $icone = new MSpan(NULL, new MSpan('', '', $icone), 'input-group-addon');
        $html = new MDiv(NULL, $content . $icone->generate(), 'input-group');
        $html = $html->generate();
        $content = $html;
        return $this->painter->generateToString($content);
    }
}

