Editable::buildHtmlOptions PHP Method

buildHtmlOptions() public method

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' || $this->type == 'datetime') {
                //if date comes as object OR timestamp, format it to string
                if ($this->value instanceof DateTime || is_long($this->value) || is_string($this->value) && ctype_digit($this->value)) {
                    /*
                     * unfortunatly bootstrap datepicker's format does not match 
                     * Yii locale dateFormat, we need replacements below to convert 
                     * date correctly.
                     * 
                     * Yii format: 
                     * http://www.unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns
                     * 
                     * Datepicker format: 
                     * https://github.com/eternicode/bootstrap-datepicker#format
                     * 
                     * Datetimepicker format: 
                     * https://github.com/smalot/bootstrap-datetimepicker#format 
                     */
                    //months: M --> MMM, m --> M
                    $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->type == 'datetime') {
                        //minutes: i --> m
                        $format = str_replace('i', 'm', $format);
                        //hours: h --> H, H --> h
                        $count = 0;
                        $format = str_replace('h', 'H', $format, $count);
                        if (!$count) {
                            $format = str_replace('H', 'h', $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;
        }
    }