PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/admincrud/extensions/bootstrap/widgets/TbEditableDetailView.php

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