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