PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/www/app/AdminModule/components/Datagrid/Columns/PositionColumn.php

https://github.com/bazo/Mokuji
PHP | 119 lines | 59 code | 23 blank | 37 comment | 10 complexity | 233b3d87e971b1fda6dc176a24a94df6 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. <?php
  2. require_once dirname(__FILE__) . '/NumericColumn.php';
  3. /**
  4. * Representation of positioning data grid column, that provides moving entries up or down.
  5. *
  6. * @author Roman Sklenář
  7. * @copyright Copyright (c) 2009 Roman Sklenář (http://romansklenar.cz)
  8. * @license New BSD License
  9. * @example http://nettephp.com/extras/datagrid
  10. * @package Nette\Extras\DataGrid
  11. * @version $Id$
  12. */
  13. class PositionColumn extends NumericColumn
  14. {
  15. /** @var array */
  16. public $moves = array();
  17. /** @var string signal handler of move action */
  18. public $destination;
  19. /** @var bool */
  20. public $useAjax;
  21. /** @var int */
  22. protected $min;
  23. /** @var int */
  24. protected $max;
  25. /**
  26. * Checkbox column constructor.
  27. * @param string column's textual caption
  28. * @param string destination or signal to handler which do the move rutine
  29. * @param array textual labels for generated links
  30. * @param bool use ajax? (add class self::$ajaxClass into generated link)
  31. * @return void
  32. */
  33. public function __construct($caption = NULL, $destination = NULL, array $moves = NULL, $useAjax = TRUE)
  34. {
  35. parent::__construct($caption, 0);
  36. $this->useAjax = $useAjax;
  37. if (empty($moves)) {
  38. $this->moves['up'] = 'Move up';
  39. $this->moves['down'] = 'Move down';
  40. } else {
  41. $this->moves = $moves;
  42. }
  43. // try set handler if is not set
  44. if ($destination === NULL) {
  45. $this->destination = $this->getName . 'Move!';
  46. } else {
  47. $this->destination = $destination;
  48. }
  49. $this->monitor('DataGrid');
  50. }
  51. /**
  52. * This method will be called when the component (or component's parent)
  53. * becomes attached to a monitored object. Do not call this method yourself.
  54. * @param IComponent
  55. * @return void
  56. */
  57. protected function attached($dataGrid)
  58. {
  59. if ($dataGrid instanceof DataGrid) {
  60. $dataSource = clone $dataGrid->dataSource;
  61. $dataSource->orderBy(array());
  62. $this->min = (int) $dataSource->select($this->getName())->orderBy($this->getName(), 'ASC')->fetchSingle();
  63. $this->max = (int) $dataSource->select($this->getName())->orderBy($this->getName(), 'DESC')->fetchSingle();
  64. }
  65. parent::attached($dataGrid);
  66. }
  67. /**
  68. * Formats cell's content.
  69. * @param mixed
  70. * @param DibiRow|array
  71. * @return string
  72. */
  73. public function formatContent($value, $data = NULL)
  74. {
  75. $control = $this->getDataGrid(TRUE)->lookup('Nette\Application\Control', TRUE);
  76. $uplink = $control->link($this->destination, array('key' => $value, 'dir' => 'up'));
  77. $downlink = $control->link($this->destination, array('key' => $value, 'dir' => 'down'));
  78. $up = Html::el('a')->title($this->moves['up'])->href($uplink)->add(Html::el('span')->class('up'));
  79. $down = Html::el('a')->title($this->moves['down'])->href($downlink)->add(Html::el('span')->class('down'));
  80. if ($this->useAjax) {
  81. $up->class(self::$ajaxClass);
  82. $down->class(self::$ajaxClass);
  83. }
  84. // disable top up & top bottom links
  85. if ($value == $this->min) {
  86. $up->href(NULL);
  87. $up->class('inactive');
  88. }
  89. if ($value == $this->max) {
  90. $down->href(NULL);
  91. $down->class('inactive');
  92. }
  93. $positioner = Html::el('span')->class('positioner')->add($up)->add($down);
  94. return $positioner . $value;
  95. }
  96. }