PageRenderTime 35ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/extensions/yiibooster/widgets/TbEditableDetailView.php

https://gitlab.com/zenfork/vektor
PHP | 143 lines | 61 code | 21 blank | 61 comment | 11 complexity | 98317556db323f726328bc7aea2db228 MD5 | raw file
  1. <?php
  2. /**
  3. * TbEditableDetailView class file.
  4. *
  5. * @author Vitaliy Potapov <noginsk@rambler.ru>
  6. * @link https://github.com/vitalets/x-editable-yii
  7. * @copyright Copyright &copy; Vitaliy Potapov 2012
  8. * @version 1.3.1
  9. */
  10. Yii::import('booster.widgets.TbEditableField');
  11. Yii::import('zii.widgets.CDetailView');
  12. /**
  13. * TbEditableDetailView widget makes editable CDetailView (several attributes of single model shown as name-value table).
  14. *
  15. * @package widgets
  16. */
  17. class TbEditableDetailView extends CDetailView
  18. {
  19. /**
  20. * @var string submit url for all editables in detailview
  21. */
  22. /*
  23. commented due to using magic methods and setting any of default TbEditableField param
  24. from top level config of TbEditableDetailView
  25. */
  26. //public $url = null;
  27. /**
  28. * @var array additional params to send on server
  29. */
  30. /*
  31. commented due to using magic methods and setting any of default TbEditableField param
  32. from top level config of TbEditableDetailView
  33. */
  34. //public $params = null;
  35. public function init()
  36. {
  37. if (!$this->data instanceof CModel) {
  38. throw new CException('Property "data" should be of CModel class.');
  39. }
  40. //set bootstrap css
  41. /* TODO if(yii::app()->editable->form === 'bootstrap') { */
  42. $this->htmlOptions = array('class'=> 'table table-bordered table-striped table-hover');
  43. //disable loading Yii's css for bootstrap
  44. $this->cssFile = false;
  45. // }
  46. parent::init();
  47. }
  48. protected function renderItem($options, $templateData)
  49. {
  50. //apply editable if not set 'editable' params or set and not false
  51. $apply = !empty($options['name']) && (!isset($options['editable']) || $options['editable'] !== false);
  52. if ($apply) {
  53. //ensure $options['editable'] is array
  54. if(!isset($options['editable'])) $options['editable'] = array();
  55. //merge options with defaults: url, params, etc.
  56. $options['editable'] = CMap::mergeArray($this->_data, $options['editable']);
  57. //options to be passed into TbEditableField (constructed from $options['editable'])
  58. $widgetOptions = array(
  59. 'model' => $this->data,
  60. 'attribute' => $options['name']
  61. );
  62. //if value in detailview options provided, set text directly (as value here means text)
  63. if(isset($options['value']) && $options['value'] !== null) {
  64. $widgetOptions['text'] = $templateData['{value}'];
  65. $widgetOptions['encode'] = false;
  66. }
  67. $widgetOptions = CMap::mergeArray($widgetOptions, $options['editable']);
  68. $widget = $this->controller->createWidget('TbEditableField', $widgetOptions);
  69. //'apply' maybe changed during init of widget (e.g. if related model has unsafe attribute)
  70. if($widget->apply) {
  71. ob_start();
  72. $widget->run();
  73. $templateData['{value}'] = ob_get_clean();
  74. }
  75. }
  76. parent::renderItem($options, $templateData);
  77. }
  78. //***************************************************************************************
  79. // Generic getter/setter implementation to accept default configuration for TbEditableField
  80. //***************************************************************************************
  81. /** Data for default fields of TbEditableField */
  82. private $_data = array();
  83. /** Valid attributes for TbEditableField (singleton) */
  84. private $_editableProperties;
  85. /**
  86. * Get the properties available for {@link TbEditableField}.
  87. *
  88. * These properties can also be set for the {@link TbEditableDetailView} as default values.
  89. */
  90. private function getEditableProperties() {
  91. if(!isset($this->_editableProperties)) {
  92. $reflection = new ReflectionClass('TbEditableField');
  93. $this->_editableProperties = array_map(function($d){return $d->getName();},$reflection->getProperties());
  94. }
  95. return $this->_editableProperties;
  96. }
  97. /**
  98. * (non-PHPdoc)
  99. * @see CComponent::__get()
  100. */
  101. public function __get($key) {
  102. return (array_key_exists($key,$this->_data) ? $this->_data[$key] : parent::__get($key));
  103. }
  104. /**
  105. * (non-PHPdoc)
  106. * @see CComponent::__set()
  107. */
  108. public function __set($key, $value) {
  109. if(in_array($key,$this->getEditableProperties())) {
  110. $this->_data[$key] = $value;
  111. } else {
  112. parent::__set($key,$value);
  113. }
  114. }
  115. /**
  116. * (non-PHPdoc)
  117. * @see CComponent::__isset()
  118. */
  119. public function __isset($name) {
  120. return array_key_exists($name,$this->_data)||parent::__isset($name);
  121. }
  122. }