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

Language PHP Lines 119
MD5 Hash 233b3d87e971b1fda6dc176a24a94df6
Repository https://github.com/bazo/Mokuji.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php

require_once dirname(__FILE__) . '/NumericColumn.php';



/**
 * Representation of positioning data grid column, that provides moving entries up or down.
 *
 * @author     Roman Sklenář
 * @copyright  Copyright (c) 2009 Roman Sklenář (http://romansklenar.cz)
 * @license    New BSD License
 * @example    http://nettephp.com/extras/datagrid
 * @package    Nette\Extras\DataGrid
 * @version    $Id$
 */
class PositionColumn extends NumericColumn
{
	/** @var array */
	public $moves = array();

	/** @var string  signal handler of move action */
	public $destination;

	/** @var bool */
	public $useAjax;

	/** @var int */
	protected $min;

	/** @var int */
	protected $max;


	/**
	 * Checkbox column constructor.
	 * @param  string  column's textual caption
	 * @param  string  destination or signal to handler which do the move rutine
	 * @param  array   textual labels for generated links
	 * @param  bool    use ajax? (add class self::$ajaxClass into generated link)
	 * @return void
	 */
	public function __construct($caption = NULL, $destination = NULL, array $moves = NULL, $useAjax = TRUE)
	{
		parent::__construct($caption, 0);

		$this->useAjax = $useAjax;

		if (empty($moves)) {
			$this->moves['up'] = 'Move up';
			$this->moves['down'] = 'Move down';
		} else {
			$this->moves = $moves;
		}

		// try set handler if is not set
		if ($destination === NULL) {
			$this->destination = $this->getName . 'Move!';
		} else {
			$this->destination = $destination;
		}

		$this->monitor('DataGrid');
	}


	/**
	 * This method will be called when the component (or component's parent)
	 * becomes attached to a monitored object. Do not call this method yourself.
	 * @param  IComponent
	 * @return void
	 */
	protected function attached($dataGrid)
	{
		if ($dataGrid instanceof DataGrid) {
			$dataSource = clone $dataGrid->dataSource;
			$dataSource->orderBy(array());
			$this->min = (int) $dataSource->select($this->getName())->orderBy($this->getName(), 'ASC')->fetchSingle();
			$this->max = (int) $dataSource->select($this->getName())->orderBy($this->getName(), 'DESC')->fetchSingle();
		}

		parent::attached($dataGrid);
	}


	/**
	 * Formats cell's content.
	 * @param  mixed
	 * @param  DibiRow|array
	 * @return string
	 */
	public function formatContent($value, $data = NULL)
	{
		$control = $this->getDataGrid(TRUE)->lookup('Nette\Application\Control', TRUE);
		$uplink = $control->link($this->destination, array('key' => $value, 'dir' => 'up'));
		$downlink = $control->link($this->destination, array('key' => $value, 'dir' => 'down'));

		$up = Html::el('a')->title($this->moves['up'])->href($uplink)->add(Html::el('span')->class('up'));
		$down = Html::el('a')->title($this->moves['down'])->href($downlink)->add(Html::el('span')->class('down'));

		if ($this->useAjax) {
			$up->class(self::$ajaxClass);
			$down->class(self::$ajaxClass);
		}

		// disable top up & top bottom links
		if ($value == $this->min) {
			$up->href(NULL);
			$up->class('inactive');
		}
		if ($value == $this->max) {
			$down->href(NULL);
			$down->class('inactive');
		}

		$positioner = Html::el('span')->class('positioner')->add($up)->add($down);
		return $positioner . $value;
	}
}
Back to Top