317 throw new CException(
'Parameter "name" should be provided for TbEditable widget');
331 if (!$this->format && $this->type ==
'date') {
332 $this->format =
'yyyy-mm-dd';
334 if (!$this->format && $this->type ==
'datetime') {
335 $this->format =
'yyyy-mm-dd hh:ii:ss';
344 'rel' => $this->liveSelector ? $this->liveSelector : $this->
getSelector(),
348 if($this->pk !== null) {
354 if ($this->_prepareToAutotext) {
356 if ($this->type ==
'date' || $this->type ==
'datetime') {
358 if($this->
value instanceOf DateTime || is_long($this->
value) || (is_string($this->
value) && ctype_digit($this->
value))) {
378 if($this->type ==
'datetime') {
389 if($this->
value instanceof DateTime) {
390 $timestamp = $this->
value->getTimestamp();
395 $this->
value = Yii::app()->dateFormatter->format(
$format, $timestamp);
399 if(is_scalar($this->
value)) {
406 $this->htmlOptions = CMap::mergeArray($this->htmlOptions,
$htmlOptions);
410 foreach($this->htmlOptions as $k => $v) {
411 $this->htmlOptions[$k] = is_array($v) ? CJSON::encode($v) : $v;
418 $this->url = CHtml::normalizeUrl($this->url);
421 'name' => $this->name,
422 'title' => CHtml::encode($this->title),
426 if ($this->_prepareToAutotext && !is_scalar($this->
value)) {
431 if (Yii::app()->request->enableCsrfValidation) {
432 $csrfTokenName = Yii::app()->request->csrfTokenName;
433 $csrfToken = Yii::app()->request->csrfToken;
434 if(!isset($this->params[$csrfTokenName])) {
435 $this->params[$csrfTokenName] = $csrfToken;
457 if ($this->$option !== null) {
465 if(is_array($this->
source)) {
467 if(isset($this->
source[0]) && is_array($this->
source[0])) {
481 foreach(array(
'validate',
'success',
'display') as $method) {
482 if(isset($this->$method)) {
483 $options[$method]=(strpos($this->$method,
'js:') !== 0 ?
'js:' :
'') . $this->$method;
488 $this->options = CMap::mergeArray($this->options,
$options);
491 if($this->type ==
'date' || $this->type ==
'datetime') {
492 if(!isset($this->options[
'clear'])) {
493 $this->options[
'clear'] = Yii::t(
'TbEditableField.editable',
'x clear');
500 $selector =
"a[rel=\"{$this->htmlOptions['rel']}\"]";
501 if($this->liveTarget) {
502 $selector =
'#'.$this->liveTarget.
' '.$selector;
504 $script =
"$('".$selector.
"')";
507 foreach(array(
'init',
'shown',
'save',
'hidden') as $event) {
508 $eventName =
'on'.ucfirst($event);
509 if (isset($this->$eventName)) {
512 $eventJs = (strpos($this->$eventName,
'js:') !== 0 ?
'js:' :
'') . $this->$eventName;
513 $script .=
"\n.on('".$event.
"', ".CJavaScript::encode($eventJs).
")";
518 $options = CJavaScript::encode($this->options);
519 $script .=
".editable($options);";
522 if($this->liveTarget) {
523 $script .=
"\n $('body').on('ajaxUpdate.editable', function(e){ if(e.target.id == '".$this->liveTarget.
"') yiiEditable(); });";
524 $script =
"(function yiiEditable() {\n ".$script.
"\n}());";
527 Yii::app()->getClientScript()->registerScript(__CLASS__ .
'-' . $selector, $script);
532 public function registerAssets() {
536 if ($this->type ==
'date' || $this->type ==
'combodate') {
538 $widget = Yii::app()->widgetFactory->createWidget(
540 'booster.widgets.TbDatePicker',
541 array(
'options' => isset($this->options[
'datepicker']) ? $this->options[
'datepicker'] : array())
543 $widget->registerLanguageScript();
544 } elseif ($this->type ==
'datetime') {
545 $booster->registerPackage(
'datetimepicker');
548 $widget = Yii::app()->widgetFactory->createWidget(
550 'booster.widgets.TbDateTimePicker',
551 array(
'options' => $this->options[
'datetimepicker'])
553 $widget->registerLanguageScript();
556 if ($this->type ==
'combodate') {
557 $booster->registerPackage(
'moment');
558 } elseif ($this->type ==
'select2') {
559 $booster->registerPackage(
'select2');
562 $booster->registerPackage(
'x-editable');
567 $am = Yii::app()->getAssetManager();
568 $cs = Yii::app()->getClientScript();
569 $form = yii::app()->editable->form;
570 $mode = $this->mode ? $this->mode : yii::app()->editable->defaults[
'mode'];
573 if(
$form === EditableConfig::FORM_BOOTSTRAP) {
574 if (($bootstrap = yii::app()->getComponent(
'bootstrap'))) {
575 $bootstrap->registerCoreCss();
576 $bootstrap->registerCoreScripts();
578 throw new CException(
'You need to setup Yii-bootstrap extension first.');
581 $assetsUrl = $am->publish(Yii::getPathOfAlias(
'editable.assets.bootstrap-editable'));
582 $js =
'bootstrap-editable.js';
583 $css =
'bootstrap-editable.css';
585 } elseif(
$form === EditableConfig::FORM_JQUERYUI) {
586 if(
$mode === EditableConfig::POPUP && Yii::getVersion() <
'1.1.13' ) {
587 throw new CException(
'jQuery UI editable popup supported from Yii 1.1.13+');
593 $assetsUrl = $am->publish(Yii::getPathOfAlias(
'editable.assets.jqueryui-editable'));
594 $js =
'jqueryui-editable.js';
595 $css =
'jqueryui-editable.css';
598 $assetsUrl = $am->publish(Yii::getPathOfAlias(
'editable.assets.jquery-editable'));
599 $js =
'jquery-editable-poshytip.js';
600 $css =
'jquery-editable.css';
603 if(
$mode === EditableConfig::POPUP) {
604 $poshytipUrl = $am->publish(Yii::getPathOfAlias(
'editable.assets.poshytip'));
605 $cs->registerScriptFile($poshytipUrl .
'/jquery.poshytip.js');
606 $cs->registerCssFile($poshytipUrl .
'/tip-yellowsimple/tip-yellowsimple.css');
610 if($this->type ==
'date' || $this->type ==
'dateui') {
616 $cs->registerCssFile($assetsUrl.
'/css/'.$css);
617 $cs->registerScriptFile($assetsUrl.
'/js/'.$js, CClientScript::POS_END);
620 if($this->type ==
'combodate') {
621 $momentUrl = $am->publish(Yii::getPathOfAlias(
'editable.assets.moment'));
622 $cs->registerScriptFile($momentUrl.
'/moment.min.js');
626 if($this->type ==
'select2') {
627 $select2Url = $am->publish(Yii::getPathOfAlias(
'editable.assets.select2'));
628 $cs->registerScriptFile($select2Url.
'/select2.min.js');
629 $cs->registerCssFile($select2Url.
'/select2.css');
633 if($this->type ==
'datetime') {
634 $url = $am->publish(Yii::getPathOfAlias(
'editable.assets.bootstrap-datetimepicker'));
635 $cs->registerScriptFile(
$url.
'/js/bootstrap-datetimepicker.js');
636 $cs->registerCssFile(
$url.
'/css/datetimepicker.css');
653 if($this->apply !==
false || $this->liveTarget) {
656 $this->registerAssets();
660 if($this->apply !==
false) {
669 echo CHtml::openTag(
'a', $this->htmlOptions);
671 echo CHtml::closeTag(
'a');
676 echo $this->encode ? CHtml::encode($this->text) :
$this->text;
682 if($this->liveTarget) {
695 foreach(
$pk as $k => $v) {
696 $buffer[] = $k.
'-'.$v;
698 $pk = join(
'_', $buffer);
703 return $this->name.
'_'.
$pk;
716 && in_array(
$type, array(
739 public static function source($models, $valueField=
'', $textField=
'', $groupField=
'', $groupTextField=
'')
743 $first = reset($models);
746 if($first && (is_string($first) || is_numeric($first))) {
747 foreach($models as $key =>
$text) {
748 $listData[] = array(
'value' => $key,
'text' =>
$text);
754 if($groupField ===
'') {
755 foreach($models as $model) {
758 $listData[] = array(
'value' =>
$value,
'text' =>
$text);
761 if(!$groupTextField) {
762 $groupTextField = $groupField;
765 foreach($models as $model) {
770 if($group === null) {
771 $listData[] = array(
'value' =>
$value,
'text' =>
$text);
773 if(!isset($groups[$group])) {
774 $groups[$group] = array(
'value' => $group,
'text' => $groupText,
'children' => array(),
'index' => count($listData));
775 $listData[] =
'group';
777 $groups[$group][
'children'][] = array(
'value' =>
$value,
'text' =>
$text);
782 foreach($groups as $group) {
783 $index = $group[
'index'];
784 unset($group[
'index']);
785 $listData[$index] = $group;
799 $trigger =
'$("#'.$widget->id.
'").trigger("ajaxUpdate.editable");';
802 if(strpos($widget->afterAjaxUpdate, $trigger) !==
false)
return;
805 if(strlen($widget->afterAjaxUpdate)) {
806 $orig = $widget->afterAjaxUpdate;
807 if(strpos($orig,
'js:')===0) $orig = substr($orig,3);
808 $orig =
"\n($orig).apply(this, arguments);";
812 $widget->afterAjaxUpdate =
"js: function(id, data) {
816 $widget->registerClientScript();
826 $cs=Yii::app()->getClientScript();
827 if($this->themeUrl===null) {
828 $this->themeUrl=
$cs->getCoreScriptUrl().
'/jui/css';
830 $cs->registerCssFile($this->themeUrl.
'/'.$this->theme.
'/'.$this->cssFile);
831 $cs->registerPackage(
'jquery.ui');