public function run() { if (!$this->hasModel() && !isset($this->name)) { throw new CHttpException(500, 'Parameters "model" and "attribute" or "name" have to be set!'); } $ok = $this->attribute; if (!empty($this->model) && $ok) { $oCKeditor = new CKeditor(); $this->value = CHtml::resolveValue($this->model, $this->attribute); list($this->name, $id) = $this->resolveNameID(); } elseif (!empty($this->name)) { $oCKeditor = new CKeditor(); $this->value = isset($this->value) ? $this->value : null; } $oCKeditor->basePath = $this->baseurl . "/ckeditor/"; $oCKeditor->config['height'] = $this->height; $oCKeditor->config['width'] = $this->width; if (isset($this->config) && is_array($this->config)) { foreach ($this->config as $key => $value) { $oCKeditor->config[$key] = $value; } } if ($this->filespath && $this->filesurl) { $oCKeditor->config['filebrowserBrowseUrl'] = $this->kcFinderPath . 'browse.php?type=files'; $oCKeditor->config['filebrowserImageBrowseUrl'] = $this->kcFinderPath . 'browse.php?type=images'; $oCKeditor->config['filebrowserFlashBrowseUrl'] = $this->kcFinderPath . 'browse.php?type=flash'; $oCKeditor->config['filebrowserUploadUrl'] = $this->kcFinderPath . 'upload.php?type=files'; $oCKeditor->config['filebrowserImageUploadUrl'] = $this->kcFinderPath . 'upload.php?type=images'; $oCKeditor->config['filebrowserFlashUploadUrl'] = $this->kcFinderPath . 'upload.php?type=flash'; $session = new CHttpSession(); $session->open(); $session['KCFINDER'] = array('disabled' => false, 'uploadURL' => $this->filesurl, 'uploadDir' => realpath($this->filespath) . '/'); } $oCKeditor->editor($this->name, isset($this->value) ? $this->value : null); }
/** * Initializes the widget. */ public function init() { if (is_null($this->theme)) { $this->theme = 'default'; } // check if options parameter is a json string if (is_string($this->options)) { if (!($this->options = CJSON::decode($this->options))) { throw new CException('The options parameter is not valid JSON.'); } } // merge options with default values $defaultOptions = array(); $this->options = CMap::mergeArray($defaultOptions, $this->options); // merge options with Javascript events $normalizedEvents = array(); foreach ($this->events as $name => $handler) { $normalizedEvents[$name] = new CJavaScriptExpression($handler); } $this->options = CMap::mergeArray($normalizedEvents, $this->options); // set field initial value if (isset($this->options['formatSubmit'])) { if (!isset($this->htmlOptions['data-value']) && $this->hasModel()) { $this->htmlOptions['data-value'] = CHtml::resolveValue($this->model, $this->attribute); } } // prepend pickadate scripts to the begining of the additional js files array if (!empty($this->language)) { array_unshift($this->scripts, "translations/{$this->language}"); } array_unshift($this->scripts, 'picker.date'); array_unshift($this->scripts, $this->baseScript); }
public function run() { list($name, $id) = $this->resolveNameID(); if (isset($this->htmlOptions['id'])) { $id = $this->htmlOptions['id']; } else { $this->htmlOptions['id'] = $id; } if (isset($this->htmlOptions['name'])) { $name = $this->htmlOptions['name']; } if (isset($this->model)) { echo CHtml::textArea($name, CHtml::resolveValue($this->model, $this->attribute), $this->htmlOptions); } else { echo CHtml::textArea($name, $this->value, $this->htmlOptions); } $this->registerScripts($id); }
public static function activeRadioButtonList($model, $attribute, $data, $htmlOptions = array()) { CHtml::resolveNameID($model, $attribute, $htmlOptions); $selection = CHtml::resolveValue($model, $attribute); if ($model->hasErrors($attribute)) { CHtml::addErrorCss($htmlOptions); } $name = $htmlOptions['name']; unset($htmlOptions['name']); if (array_key_exists('uncheckValue', $htmlOptions)) { $uncheck = $htmlOptions['uncheckValue']; unset($htmlOptions['uncheckValue']); } else { $uncheck = ''; } $hiddenOptions = isset($htmlOptions['id']) ? array('id' => CHtml::ID_PREFIX . $htmlOptions['id']) : array('id' => false); $hidden = $uncheck !== null ? CHtml::hiddenField($name, $uncheck, $hiddenOptions) : ''; return $hidden . self::radioButtonList($name, $selection, $data, $htmlOptions); }
public function run() { if ($this->selector == null) { list($this->name, $this->id) = $this->resolveNameId(); $this->selector = '#' . $this->id; } if (!isset($this->htmlOptions['value'])) { if ($this->hasModel()) { $this->value = CHtml::resolveValue($this->model, $this->attribute); } } else { $this->value = $this->htmlOptions['value']; unset($this->htmlOptions['value']); } $this->htmlOptions['autocomplete'] = 'off'; self::initClientScript($this->scriptPosition, $this->bootstrapVersion); $options = $this->options ? CJavaScript::encode($this->options) : ''; Yii::app()->clientScript->registerScript(__CLASS__ . '#' . $this->id, "jQuery('{$this->selector}').datepicker({$options})"); echo CHtml::textField($this->name, $this->value, $this->htmlOptions); }
/** * Runs the widget. */ public function run() { if (!isset($this->uploadFolder) || !isset($this->uploadActionUrl) || !isset($this->cropActionUrl)) { throw new CException("uploadFolder, uploadActionUrl, and cropActionUrl are required."); } list($name, $id) = $this->resolveNameID(); $imagePath = $this->value; $imageTag = ''; if ($this->hasModel()) { if ($this->form) { $content = $this->form->hiddenField($this->model, $this->attribute, $this->htmlOptions); } else { $content = CHtml::activeHiddenField($this->model, $this->attribute, $this->htmlOptions); } $imagePath = CHtml::resolveValue($this->model, $this->attribute); if (isset($this->model->attributes[$this->existingFileAttribute])) { if ($this->model->attributes[$this->existingFileAttribute] != '') { if (isset($this->existingFileAttribute)) { $imagePath = CHtml::resolveValue($this->model, $this->existingFileAttribute); } if (isset($this->existingFileFolder) && ($imagePath != null && $imagePath != '')) { $imagePath = $this->existingFileFolder . '/' . $imagePath; } } } } else { $content = CHtml::hiddenField($name, $this->value, $this->htmlOptions); } if ($imagePath != null && $imagePath != '') { $imageTag = CHtml::image($imagePath . '?_=' . time()); } $content .= CHtml::tag('div', array('id' => $id . $this->uploadIdPostFix), '', true); $content .= CHtml::tag('div', array('id' => $id . $this->imageIdPostFix), $imageTag, true); echo CHtml::tag($this->tag, $this->tagHtmlOptions, $content, true); $this->registerClientScript($id); }
/** * Renders an input list. * @param boolean $checkbox flag that indicates if the list is a checkbox-list. * @param CModel $model the data model * @param string $attribute the attribute * @param array $data value-label pairs used to generate the input list. * @param array $htmlOptions additional HTML options. * @return string the generated input list. * @since 0.9.5 */ protected function inputsList($checkbox, $model, $attribute, $data, $htmlOptions = array()) { CHtml::resolveNameID($model, $attribute, $htmlOptions); $select = CHtml::resolveValue($model, $attribute); if ($model->hasErrors($attribute)) { if (isset($htmlOptions['class'])) { $htmlOptions['class'] .= ' ' . CHtml::$errorCss; } else { $htmlOptions['class'] = CHtml::$errorCss; } } $name = $htmlOptions['name']; unset($htmlOptions['name']); if (array_key_exists('uncheckValue', $htmlOptions)) { $uncheck = $htmlOptions['uncheckValue']; unset($htmlOptions['uncheckValue']); } else { $uncheck = ''; } $hiddenOptions = isset($htmlOptions['id']) ? array('id' => CHtml::ID_PREFIX . $htmlOptions['id']) : array('id' => false); $hidden = $uncheck !== null ? CHtml::hiddenField($name, $uncheck, $hiddenOptions) : ''; if (isset($htmlOptions['template'])) { $template = $htmlOptions['template']; } else { $template = '<label class="{labelCssClass}">{input}{label}</label>'; } unset($htmlOptions['template'], $htmlOptions['separator'], $htmlOptions['hint']); if ($checkbox && substr($name, -2) !== '[]') { $name .= '[]'; } unset($htmlOptions['checkAll'], $htmlOptions['checkAllLast']); $labelOptions = isset($htmlOptions['labelOptions']) ? $htmlOptions['labelOptions'] : array(); unset($htmlOptions['labelOptions']); $items = array(); $baseID = CHtml::getIdByName($name); $id = 0; $method = $checkbox ? 'checkBox' : 'radioButton'; $labelCssClass = $checkbox ? 'checkbox' : 'radio'; if (isset($htmlOptions['inline'])) { $labelCssClass .= '-inline'; unset($htmlOptions['inline']); } foreach ($data as $value => $label) { $checked = !is_array($select) && !strcmp($value, $select) || is_array($select) && in_array($value, $select); $htmlOptions['value'] = $value; $htmlOptions['id'] = $baseID . '_' . $id++; $option = CHtml::$method($name, $checked, $htmlOptions); $label = CHtml::label($label, $htmlOptions['id'], $labelOptions); $items[] = strtr($template, array('{labelCssClass}' => $labelCssClass, '{input}' => $option, '{label}' => $label)); } return $hidden . implode('', $items); }
"> <?php $nscmaculopathy_html_options = array('options' => array()); foreach ($nsc_maculopathys as $macu) { $nscmaculopathy_html_options['options'][(string) $macu->id] = array('data-booking' => $macu->booking_weeks, 'class' => $macu->class, 'data-code' => $macu->code); } echo CHtml::activeDropDownList($element, $side . '_nscmaculopathy_id', CHtml::listData($nsc_maculopathys, 'id', 'name'), $nscmaculopathy_html_options); ?> </div> <span class="grade-info-icon" data-info-type="maculopathy"><img src="<?php echo $this->getAssetPathForElement($element); ?> /img/icon_info.png" style="height:20px" /></span> <div class="quicklook grade-info" style="display: none;"> <?php $selected_value = CHtml::resolveValue($element, $side . '_nscmaculopathy_id'); if (!$selected_value && count($nsc_maculopathys)) { $selected_value = $nsc_maculopathys[0]->id; } foreach ($nsc_maculopathys as $macu) { $show_div = false; if ($selected_value == $macu->id) { $show_div = true; } echo '<div ' . ($show_div ? ' ' : 'style="display: none;" ') . 'class="' . CHtml::modelName($element) . '_' . $side . '_nscmaculopathy_desc desc" id="' . CHtml::modelName($element) . '_' . $side . '_nscmaculopathy_desc_' . $macu->code . '">' . $macu->description . '</div>'; } ?> </div> <!-- div containing the full list of descriptions for nsc maculopathy --> <div id="<?php echo CHtml::modelName($element) . '_' . $side . '_all_maculopathy_desc';
public function testResolveValue() { $testModel = new CHtmlTestFormModel(); $this->assertEquals('stringAttrValue', CHtml::resolveValue($testModel, 'stringAttr')); $this->assertEquals('v1', CHtml::resolveValue($testModel, 'arrayAttr[k1]')); $this->assertEquals('v2', CHtml::resolveValue($testModel, 'arrayAttr[k2]')); $this->assertEquals($testModel->arrayAttr['k3'], CHtml::resolveValue($testModel, 'arrayAttr[k3]')); $this->assertEquals('v4', CHtml::resolveValue($testModel, 'arrayAttr[k3][k4]')); $this->assertEquals('v5', CHtml::resolveValue($testModel, 'arrayAttr[k3][k5]')); $this->assertEquals('v6', CHtml::resolveValue($testModel, 'arrayAttr[k6]')); $this->assertNull(CHtml::resolveValue($testModel, 'arrayAttr[k7]')); $this->assertNull(CHtml::resolveValue($testModel, 'arrayAttr[k7][k8]')); $this->assertEquals($testModel->arrayAttr, CHtml::resolveValue($testModel, '[ignored-part]arrayAttr')); $this->assertEquals('v1', CHtml::resolveValue($testModel, '[ignored-part]arrayAttr[k1]')); $this->assertEquals('v4', CHtml::resolveValue($testModel, '[ignore-this]arrayAttr[k3][k4]')); }
/** * Generates an uneditable input for a model attribute. * @param CModel $model the data model. * @param string $attribute the attribute. * @param array $htmlOptions additional HTML attributes. * @return string the generated input. */ public static function activeUneditableField($model, $attribute, $htmlOptions = array()) { parent::resolveNameID($model, $attribute, $htmlOptions); $value = parent::resolveValue($model, $attribute); TbArray::removeValues(array('name', 'id'), $htmlOptions); return self::uneditableField($value, $htmlOptions); }
public function run() { $nameID = $this->resolveNameID(); $htmlOptions = $this->htmlOptions; $htmlOptions['name'] = $nameID[0]; $htmlOptions['id'] = $nameID[1]; $htmlOptions['type'] = 'text/plain'; if (!isset($htmlOptions['value'])) { $content = CHtml::resolveValue($this->model, $this->attribute); } else { $content = $htmlOptions['value']; } echo CHtml::tag('script', $htmlOptions, $content); }
public function init() { parent::init(); // ensure necessary assets are loaded // JJD 8/3/11 make EJuiAutoCompleteFkField work for child rows where attribute like [$i]FieldName // get the ID which will be created for the actual field when it is rendered. // don't let resolveNameID() change $this->attribute which is needed to generate the actual field $attr = $this->attribute; $tempHtmlOpts = array(); CHtml::resolveNameID($this->model, $attr, $tempHtmlOpts); $id = $tempHtmlOpts['id']; $this->_fieldID = $id; $this->_saveID = $id . '_save'; $this->_lookupID = $id . '_lookup'; $related = $this->model->{$this->relName}; // get the related record $value = CHtml::resolveValue($this->model, $this->attribute); $this->_display = !empty($value) ? $related->{$this->displayAttr} : ''; // $this->_display=($value!==null ? $related->{$this->displayAttr} : ''); // nineinchnick comment #6809 handle zero as valid FK value. not sure works in all cases if (!isset($this->options['minLength'])) { $this->options['minLength'] = 2; } if (!isset($this->options['maxHeight'])) { $this->options['maxHeight'] = '100'; } $this->htmlOptions['size'] = $this->autoCompleteLength; // fix problem with Chrome 10 validating maxLength for the auto-complete field $this->htmlOptions['maxlength'] = $this->autoCompleteLength; // setup javascript to do the work $this->options['create'] = "js:function(event, ui){\$(this).val('" . addslashes($this->_display) . "');}"; // show initial display value // after user picks from list, save the ID in model/attr field, and Value in _save field for redisplay $this->options['select'] = "js:function(event, ui){\$('#" . $this->_fieldID . "').val(ui.item.id);\$('#" . $this->_saveID . "').val(ui.item.value);}"; // when the autoComplete field loses focus, refresh the field with current value of _save // this is either the previous value if user didn't pick anything; or the new value if they did $this->htmlOptions['onblur'] = "\$(this).val(\$('#" . $this->_saveID . "').val());"; }
/** * Generates a radio button list for a model attribute. * The model attribute value is used as the selection. * If the attribute has input error, the input field's CSS class will * be appended with {@link errorCss}. * @param CModel $model the data model * @param string $attribute the attribute * @param array $data value-label pairs used to generate the radio button list. * Note, the values will be automatically HTML-encoded, while the labels will not. * @param array $htmlOptions addtional HTML options. The options will be applied to * each radio button input. The following special options are recognized: * <ul> * <li>template: string, specifies how each radio button is rendered. Defaults * to "{input} {label}", where "{input}" will be replaced by the generated * radio button input tag while "{label}" will be replaced by the corresponding radio button label.</li> * <li>separator: string, specifies the string that separates the generated radio buttons. Defaults to new line (<br/>).</li> * <li>encode: boolean, specifies whether to encode HTML-encode tag attributes and values. Defaults to true.</li> * </ul> * Since version 1.1.7, a special option named 'uncheckValue' is available that can be used to specify the value * returned when the radio button is not checked. By default, this value is ''. Internally, a hidden field is * rendered so that when the radio button is not checked, we can still obtain the posted uncheck value. * If 'uncheckValue' is set as NULL, the hidden field will not be rendered. * @return string the generated radio button list * @see radioButtonList */ public static function activeInlineRadioButtonList($model, $attribute, $data, $htmlOptions = array()) { CHtml::resolveNameID($model, $attribute, $htmlOptions); $selection = CHtml::resolveValue($model, $attribute); if ($model->hasErrors($attribute)) { CHtml::addErrorCss($htmlOptions); } $name = self::popOption('name', $htmlOptions); $unCheck = self::popOption('uncheckValue', $htmlOptions, ''); $hiddenOptions = isset($htmlOptions['id']) ? array('id' => CHtml::ID_PREFIX . $htmlOptions['id']) : array('id' => false); $hidden = $unCheck !== null ? CHtml::hiddenField($name, $unCheck, $hiddenOptions) : ''; return $hidden . self::inlineRadioButtonList($name, $selection, $data, $htmlOptions); }
<?php $form = $this->beginWidget('booster.widgets.TbActiveForm', array('id' => 'info-form', 'enableAjaxValidation' => true, 'type' => 'horizontal', 'htmlOptions' => array('enctype' => "multipart/form-data", 'class' => 'well'))); $file = CHtml::resolveValue($model, 'resume_file'); ?> <fieldset> <style type="text/css">.col-sm-5 { padding-left: 0px; } .col-sm-4 { padding-left: 0px; } </style> <legend> 基本信息 <?php if (!empty($file = CHtml::resolveValue($model, 'resume_file'))) { $url = $this->createUrl('download', array('id' => $model->id)); $icon = CHtml::tag('i', array("style" => "", 'class' => 'glyphicon glyphicon-download text-primary'), '', true); echo CHtml::link($icon, $url, array('style' => 'float:right')); } ?> <div class="progress progress-striped active" role="progressbar" style="display: inline;float:right;"> <div id="resume-file-bar" class="progress-bar progress-bar-success" style="width: 25%;"> <?php echo CHtml::activeLabel($model, 'resume_file', array('style' => 'overflow: hidden;width: 100%;color:#000000;', 'id' => 'file-input-label', 'for' => 'file-input-button')); ?> <?php echo CHtml::activeFileField($model, 'resume_file', array('id' => 'file-input-button', 'style' => 'visibility: hidden;color:#000000', 'aria-valuemin' => 0, 'aria-valuemax' => 100, 'onchange' => 'void function(e){$("#ytfile-input-button").val(e.target.files[0].name);$("#resume-file-bar").width("100%");$("#file-input-label").text("简历文件:"+e.target.files[0].name);}(event)')); ?> </div>
/** * Renders an input list. * @param string $type the input type. Valid types are 'checkbox' and 'radio'. * @param CModel $model the data model * @param string $attribute the attribute * @param array $data value-label pairs used to generate the radio button list. * @param array $htmlOptions additional HTML options. * @return string the generated input list. * @since 0.9.5 */ protected function inputsList($type, $model, $attribute, $data, $htmlOptions = array()) { CHtml::resolveNameID($model, $attribute, $htmlOptions); $selection = CHtml::resolveValue($model, $attribute); if ($model->hasErrors($attribute)) { if (isset($htmlOptions['class'])) { $htmlOptions['class'] .= ' ' . CHtml::$errorCss; } else { $htmlOptions['class'] = CHtml::$errorCss; } } $name = $htmlOptions['name']; unset($htmlOptions['name']); if (array_key_exists('uncheckValue', $htmlOptions)) { $uncheck = $htmlOptions['uncheckValue']; unset($htmlOptions['uncheckValue']); } else { $uncheck = ''; } $hiddenOptions = isset($htmlOptions['id']) ? array('id' => CHtml::ID_PREFIX . $htmlOptions['id']) : array('id' => false); $hidden = $uncheck !== null ? CHtml::hiddenField($name, $uncheck, $hiddenOptions) : ''; unset($htmlOptions['template'], $htmlOptions['separator'], $htmlOptions['labelOptions']); $items = array(); $baseID = CHtml::getIdByName($name); $id = 0; $method = $type === 'checkbox' ? 'checkBox' : 'radioButton'; foreach ($data as $value => $label) { $checked = !strcmp($value, $selection); $htmlOptions['value'] = $value; $htmlOptions['id'] = $baseID . '_' . $id++; $option = CHtml::$method($name, $checked, $htmlOptions); $items[] = '<label>' . $option . '<span>' . $label . '</span></label>'; } return $hidden . '<ul class="inputs-list"><li>' . implode('</li><li>', $items) . '</li></ul>'; }
/** * Generates an uneditable input for a model attribute. * @param CModel $model the data model. * @param string $attribute the attribute. * @param array $htmlOptions additional HTML attributes. * @return string the generated input. */ public static function activeUneditableField($model, $attribute, $htmlOptions = array()) { CHtml::resolveNameID($model, $attribute, $htmlOptions); $value = CHtml::resolveValue($model, $attribute); return self::uneditableField($value, $htmlOptions); }
/** Render widget html and register client scripts */ public function run() { list($name, $id) = $this->resolveNameID(); if (isset($this->htmlOptions['id'])) { $id = $this->htmlOptions['id']; } else { $this->htmlOptions['id'] = $id; } if (isset($this->htmlOptions['name'])) { $name = $this->htmlOptions['name']; } if ($this->multiple && substr($name, -2) !== '[]') { $name .= '[]'; } if ($this->multiple && $this->sendEmpty) { echo CHtml::hiddenField(substr($name, 0, -2), '', array('id' => false)); } if (isset($this->model)) { echo CHtml::dropDownList($name, CHtml::resolveValue($this->model, $this->attribute), $this->data, $this->htmlOptions); } else { echo CHtml::dropDownList($name, $this->value, $this->data, $this->htmlOptions); } $this->registerScripts($id); }
/** * Renders an input list. * * @param boolean $checkbox flag that indicates if the list is a checkbox-list. * @param CModel $model the data model * @param string $attribute the attribute * @param array $data value-label pairs used to generate the input list. * @param array $htmlOptions additional HTML options. * @return string the generated input list. * * @since 0.9.5 */ protected function inputsList($checkbox, $model, $attribute, $data, $htmlOptions = array()) { CHtml::resolveNameID($model, $attribute, $htmlOptions); $select = CHtml::resolveValue($model, $attribute); if ($model->hasErrors($attribute)) { if (isset($htmlOptions['class'])) { $htmlOptions['class'] .= ' ' . CHtml::$errorCss; } else { $htmlOptions['class'] = CHtml::$errorCss; } } $name = $htmlOptions['name']; unset($htmlOptions['name']); if (array_key_exists('uncheckValue', $htmlOptions)) { $uncheck = $htmlOptions['uncheckValue']; unset($htmlOptions['uncheckValue']); } else { $uncheck = ''; } $hiddenOptions = isset($htmlOptions['id']) ? array('id' => CHtml::ID_PREFIX . $htmlOptions['id']) : array('id' => false); $hidden = $uncheck !== null ? CHtml::hiddenField($name, $uncheck, $hiddenOptions) : ''; $template = isset($htmlOptions['template']) ? $htmlOptions['template'] : '<label class="{labelCssClass}">{input}{label}</label>'; $container = isset($htmlOptions['container']) ? $htmlOptions['container'] : ''; unset($htmlOptions['template'], $htmlOptions['separator'], $htmlOptions['hint']); if ($checkbox && substr($name, -2) !== '[]') { $name .= '[]'; } $checkAllLast = ''; if (isset($htmlOptions['checkAll'])) { $checkAllLabel = $htmlOptions['checkAll']; $checkAllLast = isset($htmlOptions['checkAllLast']) && $htmlOptions['checkAllLast']; } unset($htmlOptions['checkAll'], $htmlOptions['checkAllLast']); $labelOptions = isset($htmlOptions['labelOptions']) ? $htmlOptions['labelOptions'] : array(); unset($htmlOptions['labelOptions']); $items = array(); $baseID = isset($htmlOptions['baseID']) ? $htmlOptions['baseID'] : CHtml::getIdByName($name); unset($htmlOptions['baseID']); $id = 0; $checkAll = true; $method = $checkbox ? 'checkBox' : 'radioButton'; $labelCssClass = $checkbox ? 'checkbox' : 'radio'; if (isset($htmlOptions['inline'])) { $labelCssClass .= ' inline'; unset($htmlOptions['inline']); } foreach ($data as $value => $label) { $checked = !is_array($select) && !strcmp($value, $select) || is_array($select) && in_array($value, $select); $checkAll = $checkAll && $checked; $htmlOptions['value'] = $value; $htmlOptions['id'] = $baseID . '_' . $id++; $option = CHtml::$method($name, $checked, $htmlOptions); $label = CHtml::label($label, $htmlOptions['id'], $labelOptions); $items[] = strtr($template, array('{labelCssClass}' => $labelCssClass, '{input}' => $option, '{label}' => $label)); } if (isset($checkAllLabel)) { $htmlOptions['value'] = 1; $htmlOptions['id'] = $id = $baseID . '_all'; $option = CHtml::$method($id, $checkAll, $htmlOptions); $label = CHtml::label($checkAllLabel, $id, $labelOptions); $item = strtr($template, array('{labelCssClass}' => $labelCssClass, '{input}' => $option, '{label}' => $label)); if ($checkAllLast) { $items[] = $item; } else { array_unshift($items, $item); } $name = strtr($name, array('[' => '\\[', ']' => '\\]')); $js = <<<EOD jQuery('#{$id}').click(function() { \t\$("input[name='{$name}']").prop('checked', this.checked); }); jQuery("input[name='{$name}']").click(function() { \t\$('#{$id}').prop('checked', !jQuery("input[name='{$name}']:not(:checked)").length); }); jQuery('#{$id}').prop('checked', !jQuery("input[name='{$name}']:not(:checked)").length); EOD; /** @var $cs CClientScript */ $cs = Yii::app()->getClientScript(); $cs->registerCoreScript('jquery'); $cs->registerScript($id, $js); } if (empty($container)) { return $hidden . implode('', $items); } else { return $hidden . CHtml::tag($container, array('id' => $baseID), implode('', $items)); } }
public function run() { if ($this->selector == null) { list($this->name, $this->id) = $this->resolveNameId(); $this->selector = '#' . $this->id; if (isset($this->options['ajax'])) { $this->htmlOptions['autocomplete'] = 'off'; } if (isset($this->htmlOptions['placeholder'])) { $this->options['placeholder'] = $this->htmlOptions['placeholder']; } if (!isset($this->htmlOptions['multiple'])) { $data = array(); if (isset($this->options['placeholder'])) { $data[''] = ''; } $this->data = $data + $this->data; } if ($this->hasModel()) { $attribute = $this->attribute; CHtml::resolveName($this->model, $attribute); // strip off square brackets if any if (isset($this->options['ajax'])) { if (!isset($this->htmlOptions['value'])) { $value = CHtml::resolveValue($this->model, $this->attribute); $values = array(); if ($this->model instanceof CActiveRecord) { $relations = $this->model->relations(); if (isset($relations[$attribute]) && $relations[$attribute][0] != CActiveRecord::BELONGS_TO && $relations[$attribute][0] != CActiveRecord::HAS_ONE) { foreach ($value as $object) { $values[] = $object->getPrimaryKey(); } $this->htmlOptions['value'] = implode(',', $values); } } else { if (is_array($value)) { foreach ($value as $object) { $values[] = (string) $object; } $this->htmlOptions['value'] = implode(',', $values); } } } echo CHtml::activeTextField($this->model, $this->attribute, $this->htmlOptions); } else { echo CHtml::activeDropDownList($this->model, $this->attribute, $this->data, $this->htmlOptions); } } else { $this->htmlOptions['id'] = $this->id; if (isset($this->options['ajax'])) { echo CHtml::textField($this->name, $this->value, $this->htmlOptions); } else { echo CHtml::dropDownList($this->name, $this->value, $this->data, $this->htmlOptions); } } } self::initClientScript(); $cs = Yii::app()->clientScript; if ($this->sortable) { $cs->registerCoreScript('jquery.ui'); } $options = CJavaScript::encode(CMap::mergeArray($this->defaultOptions, $this->options)); ob_start(); echo "jQuery('{$this->selector}').select2({$options})"; foreach ($this->events as $event => $handler) { echo ".on('{$event}', " . CJavaScript::encode($handler) . ")"; } echo ';'; if ($this->sortable) { echo <<<JavaScript jQuery('{$this->selector}').select2("container").find("ul.select2-choices").sortable({ \tcontainment: 'parent', \tstart: function() { jQuery('{$this->selector}').select2("onSortStart"); }, \tupdate: function() { jQuery('{$this->selector}').select2("onSortEnd"); } }); JavaScript; } $cs->registerScript(__CLASS__ . '#' . $this->id, ob_get_clean()); }