PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/include/extensions/widgets/grid/XButtonColumn.php

http://chenjin.googlecode.com/
PHP | 121 lines | 48 code | 7 blank | 66 comment | 8 complexity | 6a9f00447b6957dd6494be75736ab69a MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * XButtonColumn class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright Copyright &copy; 2008-2011 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * XButtonColumn represents a grid view column that renders one or several buttons.
  12. *
  13. * By default, it will display three buttons, "view", "update" and "delete", which triggers the corresponding
  14. * actions on the model of the row.
  15. *
  16. * By configuring {@link buttons} and {@link template} properties, the column can display other buttons
  17. * and customize the display order of the buttons.
  18. *
  19. * @author Qiang Xue <qiang.xue@gmail.com>
  20. * @version $Id: XButtonColumn.php 149 2011-07-22 18:39:37Z mole1230 $
  21. * @package zii.widgets.grid
  22. * @since 1.1
  23. */
  24. class XButtonColumn extends XGridColumn
  25. {
  26. /**
  27. * @var string the template that is used to render the content in each data cell.
  28. * These default tokens are recognized: {view}, {update} and {delete}. If the {@link buttons} property
  29. * defines additional buttons, their IDs are also recognized here. For example, if a button named 'preview'
  30. * is declared in {@link buttons}, we can use the token '{preview}' here to specify where to display the button.
  31. */
  32. public $template = '';
  33. /**
  34. * @var array the configuration for additional buttons. Each array element specifies a single button
  35. * which has the following format:
  36. * <pre>
  37. * 'buttonID' => array(
  38. * 'label'=>'...', // text label of the button
  39. * 'url'=>'...', // a PHP expression for generating the URL of the button
  40. * 'imageUrl'=>'...', // image URL of the button. If not set or false, a text link is used
  41. * 'options'=>array(...), // HTML options for the button tag
  42. * 'click'=>'...', // a JS function to be invoked when the button is clicked
  43. * 'visible'=>'...', // a PHP expression for determining whether the button is visible
  44. * )
  45. * </pre>
  46. * In the PHP expression for the 'url' option and/or 'visible' option, the variable <code>$row</code>
  47. * refers to the current row number (zero-based), and <code>$data</code> refers to the data model for
  48. * the row.
  49. *
  50. * Note that in order to display these additional buttons, the {@link template} property needs to
  51. * be configured so that the corresponding button IDs appear as tokens in the template.
  52. */
  53. public $buttons = array();
  54. /**
  55. * Initializes the column.
  56. * This method registers necessary client script for the button column.
  57. */
  58. public function init()
  59. {
  60. foreach ($this->buttons as $id => $button) {
  61. if (strpos($this->template, '{' . $id . '}') === false) {
  62. unset($this->buttons[$id]);
  63. }
  64. }
  65. }
  66. /**
  67. * Renders the data cell content.
  68. * This method renders the view, update and delete buttons in the data cell.
  69. * @param integer $row the row number (zero-based)
  70. * @param mixed $data the data associated with the row
  71. */
  72. protected function renderDataCellContent($row, $data)
  73. {
  74. $tr = array();
  75. ob_start();
  76. foreach ($this->buttons as $id => $button) {
  77. $this->renderButton($id, $button, $row, $data);
  78. $tr['{' . $id . '}'] = ob_get_contents();
  79. ob_clean();
  80. }
  81. ob_end_clean();
  82. echo strtr($this->template, $tr);
  83. }
  84. /**
  85. * Renders a link button.
  86. * @param string $id the ID of the button
  87. * @param array $button the button configuration which may contain 'label', 'url', 'imageUrl' and 'options' elements.
  88. * See {@link buttons} for more details.
  89. * @param integer $row the row number (zero-based)
  90. * @param mixed $data the data object associated with the row
  91. */
  92. protected function renderButton($id, $button, $row, $data)
  93. {
  94. if (isset($button['visible']) && !$this->evaluateExpression($button['visible'], array('row' => $row, 'data' => $data))) {
  95. return;
  96. }
  97. $url = isset($button['url']) ? $this->evaluateExpression($button['url'], array('data' => $data, 'row' => $row)) : 'javascript:;';
  98. $options = isset($button['options']) ? $button['options'] : array();
  99. if (isset($button['evalLabel'])) {
  100. $label = $this->evaluateExpression($button['evalLabel'], array('row' => $row, 'data' => $data));
  101. } else {
  102. $label = isset($button['label']) ? $button['label'] : $id;
  103. }
  104. if (isset($button['evalData'])) {
  105. $evalData = (array) $this->evaluateExpression($button['evalData'], array('row' => $row, 'data' => $data));
  106. } else {
  107. $evalData = array();
  108. }
  109. if (isset($button['attrData'])) {
  110. $options['data-attr'] = json_encode(array_merge($button['attrData'], $evalData));
  111. }
  112. echo CHtml::link($label, $url, $options);
  113. }
  114. }