WhEditable::buildHtmlOptions PHP Method

buildHtmlOptions() public method

Builds html options
public buildHtmlOptions ( )
    public function buildHtmlOptions()
    {
        //html options
        $htmlOptions = array('href' => '#', 'rel' => $this->liveSelector ? $this->liveSelector : $this->getSelector());
        //set data-pk
        if ($this->pk !== null) {
            $htmlOptions['data-pk'] = is_array($this->pk) ? CJSON::encode($this->pk) : $this->pk;
        }
        //if input type assumes autotext (e.g. select) we define value directly in data-value
        //and do not fill element contents
        if ($this->_prepareToAutotext) {
            //for date we use 'format' to put it into value (if text not defined)
            if ($this->type == 'date') {
                //if date comes as object, format it to string
                if ($this->value instanceof DateTime || is_long($this->value)) {
                    /*
                     * unfortunatly datepicker's format does not match Yii locale dateFormat,
                     * we need replacements below to convert date correctly
                     */
                    $count = 0;
                    $format = str_replace('MM', 'MMMM', $this->format, $count);
                    if (!$count) {
                        $format = str_replace('M', 'MMM', $format, $count);
                    }
                    if (!$count) {
                        $format = str_replace('m', 'M', $format);
                    }
                    if ($this->value instanceof DateTime) {
                        $timestamp = $this->value->getTimestamp();
                    } else {
                        $timestamp = $this->value;
                    }
                    $this->value = Yii::app()->dateFormatter->format($format, $timestamp);
                }
            }
            if (is_scalar($this->value)) {
                $this->htmlOptions['data-value'] = $this->value;
            }
            //if not scalar, value will be added to js options instead of html options
        }
        //merging options
        $this->htmlOptions = CMap::mergeArray($this->htmlOptions, $htmlOptions);
        //convert arrays to json string, otherwise yii can not render it:
        //"htmlspecialchars() expects parameter 1 to be string, array given"
        foreach ($this->htmlOptions as $k => $v) {
            $this->htmlOptions[$k] = is_array($v) ? CJSON::encode($v) : $v;
        }
    }