trunk /sally/core/lib/sly/Table/Column.php

Language PHP Lines 185
MD5 Hash 261a4cf34d7e058a5d71b125b9ae5b33 Estimated Cost $2,030 (why?)
Repository https://bitbucket.org/SallyCMS/trunk View Raw File
  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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
/*
 * Copyright (c) 2012, webvariants GbR, http://www.webvariants.de
 *
 * This file is released under the terms of the MIT license. You can find the
 * complete text in the attached LICENSE file or online at:
 *
 * http://www.opensource.org/licenses/mit-license.php
 */

/**
 * @ingroup table
 */
class sly_Table_Column extends sly_Viewable {
	protected $width;          ///< string
	protected $sortkey;        ///< string
	protected $direction;      ///< string
	protected $htmlAttributes; ///< array
	protected $content;        ///< string

	private $table; ///< sly_Table
	private $idx;   ///< int

	/**
	 * @param string $content
	 * @param string $width
	 * @param string $sortkey
	 * @param array  $htmlAttributes
	 */
	public function __construct($content, $width = '', $sortkey = '', $htmlAttributes = array()) {
		$this->content        = $content;
		$this->width          = $width;
		$this->sortkey        = $sortkey;
		$this->htmlAttributes = $htmlAttributes;
	}

	/**
	 * @param  string $content
	 * @param  mixed  $classes     CSS classes as either a string or an array
	 * @param  string $sortkey
	 * @param  array  $attributes
	 * @param  string $width
	 * @return sly_Table_Column
	 */
	public static function factory($content, $classes = '', $sortkey = '', array $attributes = null, $width = '') {
		$attributes = (array) $attributes;

		if (!empty($classes)) {
			$classes = is_string($classes) ? trim($classes) : implode(' ', array_filter(array_unique($classes)));
			$attributes['class'] = $classes;
		}

		return new self($content, $width, $sortkey, $attributes);
	}

	/**
	 * @param  string $uri       URI to the icon
	 * @param  string $link      optional URI to link to
	 * @param  mixed  $classes   CSS classes as either a string or an array
	 * @param  string $sortkey
	 * @return sly_Table_Column
	 */
	public static function icon($uri, $link = null, $title = null, $classes = '', $sortkey = '') {
		$attributes = array('class' => 'sly-icon');
		$icon       = sprintf('<img src="" alt="" title="%s" />', $uri, sly_html($title));

		if ($link) {
			$icon = sprintf('<a href="%s">%s</a>', sly_html($link), $icon);
		}

		if (!empty($classes)) {
			$classes = is_string($classes) ? trim($classes) : implode(' ', array_filter(array_unique($classes)));
			$attributes['class'] .= ' '.$classes;
		}

		return new self($icon, '', $sortkey, $attributes);
	}

	/**
	 * @param  string $uri       URI to the icon
	 * @param  string $link      optional URI to link to
	 * @param  mixed  $classes   CSS classes as either a string or an array
	 * @param  string $sortkey
	 * @return sly_Table_Column
	 */
	public static function sprite($spriteClass, $link = null, $title = null, $classes = '', $sortkey = '') {
		$attributes = array('class' => 'sly-icon');
		$sprite     = sly_Util_HTML::getSpriteLink($link, $title, $spriteClass);

		if (!empty($classes)) {
			$classes = is_string($classes) ? trim($classes) : implode(' ', array_filter(array_unique($classes)));
			$attributes['class'] .= ' '.$classes;
		}

		return new self($sprite, '', $sortkey, $attributes);
	}

	/**
	 * @param  string $name
	 * @param  string $value
	 * @return sly_Table_Column  self
	 */
	public function setAttribute($name, $value) {
		$this->htmlAttributes[$name] = trim($value);
		return $this;
	}

	/**
	 * @param  string $name
	 * @param  string $value
	 * @return string         found attribute value or null
	 */
	public function getAttribute($name) {
		return sly_setarraytype($this->htmlAttributes, $name, 'string', null);
	}

	/**
	 * @param  string $content
	 * @return sly_Table_Column  self
	 */
	public function setContent($content) {
		$this->content = $content;
		return $this;
	}

	/**
	 * @param  sly_Table $table
	 * @return sly_Table_Column  self
	 */
	public function setTable(sly_Table $table) {
		$this->table = $table;
		return $this;
	}

	/**
	 * @param  int $idx
	 * @return sly_Table_Column  self
	 */
	public function setIndex($idx) {
		$this->idx = (int) $idx;
		return $this;
	}

	/**
	 * @return int
	 */
	public function getColspan() {
		return sly_setarraytype($this->htmlAttributes, 'colspan', 'int', 1);
	}

	/**
	 * @param  sly_Request $request  the request to use or null for the global one
	 * @return string
	 */
	public function render(sly_Request $request = null) {
		if (!empty($this->width)) {
			$this->htmlAttributes['style'] = 'width:'.$this->width;
		}

		$request = $request ? $request : sly_Core::getRequest();
		$id      = $this->table->getID();

		if ($request->get($id.'_sortby', 'string') === $this->sortkey) {
			$this->direction = $request->get($id.'_direction', 'string') === 'desc' ? 'desc' : 'asc';
		}
		else {
			$this->direction = 'none';
		}

		return $this->renderView('column.phtml', array('table' => $this->table, 'index' => $this->idx));
	}

	/**
	 * @throws sly_Exception
	 * @param  string $file
	 * @return string
	 */
	protected function getViewFile($file) {
		$full = SLY_COREFOLDER.'/views/table/'.$file;
		if (file_exists($full)) return $full;

		throw new sly_Exception(t('view_not_found', $file));
	}
}
Back to Top