PageRenderTime 123ms CodeModel.GetById 60ms app.highlight 7ms RepoModel.GetById 54ms app.codeStats 0ms

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

https://bitbucket.org/SallyCMS/trunk
PHP | 184 lines | 86 code | 25 blank | 73 comment | 8 complexity | 261a4cf34d7e058a5d71b125b9ae5b33 MD5 | raw file
  1<?php
  2/*
  3 * Copyright (c) 2012, webvariants GbR, http://www.webvariants.de
  4 *
  5 * This file is released under the terms of the MIT license. You can find the
  6 * complete text in the attached LICENSE file or online at:
  7 *
  8 * http://www.opensource.org/licenses/mit-license.php
  9 */
 10
 11/**
 12 * @ingroup table
 13 */
 14class sly_Table_Column extends sly_Viewable {
 15	protected $width;          ///< string
 16	protected $sortkey;        ///< string
 17	protected $direction;      ///< string
 18	protected $htmlAttributes; ///< array
 19	protected $content;        ///< string
 20
 21	private $table; ///< sly_Table
 22	private $idx;   ///< int
 23
 24	/**
 25	 * @param string $content
 26	 * @param string $width
 27	 * @param string $sortkey
 28	 * @param array  $htmlAttributes
 29	 */
 30	public function __construct($content, $width = '', $sortkey = '', $htmlAttributes = array()) {
 31		$this->content        = $content;
 32		$this->width          = $width;
 33		$this->sortkey        = $sortkey;
 34		$this->htmlAttributes = $htmlAttributes;
 35	}
 36
 37	/**
 38	 * @param  string $content
 39	 * @param  mixed  $classes     CSS classes as either a string or an array
 40	 * @param  string $sortkey
 41	 * @param  array  $attributes
 42	 * @param  string $width
 43	 * @return sly_Table_Column
 44	 */
 45	public static function factory($content, $classes = '', $sortkey = '', array $attributes = null, $width = '') {
 46		$attributes = (array) $attributes;
 47
 48		if (!empty($classes)) {
 49			$classes = is_string($classes) ? trim($classes) : implode(' ', array_filter(array_unique($classes)));
 50			$attributes['class'] = $classes;
 51		}
 52
 53		return new self($content, $width, $sortkey, $attributes);
 54	}
 55
 56	/**
 57	 * @param  string $uri       URI to the icon
 58	 * @param  string $link      optional URI to link to
 59	 * @param  mixed  $classes   CSS classes as either a string or an array
 60	 * @param  string $sortkey
 61	 * @return sly_Table_Column
 62	 */
 63	public static function icon($uri, $link = null, $title = null, $classes = '', $sortkey = '') {
 64		$attributes = array('class' => 'sly-icon');
 65		$icon       = sprintf('<img src="" alt="" title="%s" />', $uri, sly_html($title));
 66
 67		if ($link) {
 68			$icon = sprintf('<a href="%s">%s</a>', sly_html($link), $icon);
 69		}
 70
 71		if (!empty($classes)) {
 72			$classes = is_string($classes) ? trim($classes) : implode(' ', array_filter(array_unique($classes)));
 73			$attributes['class'] .= ' '.$classes;
 74		}
 75
 76		return new self($icon, '', $sortkey, $attributes);
 77	}
 78
 79	/**
 80	 * @param  string $uri       URI to the icon
 81	 * @param  string $link      optional URI to link to
 82	 * @param  mixed  $classes   CSS classes as either a string or an array
 83	 * @param  string $sortkey
 84	 * @return sly_Table_Column
 85	 */
 86	public static function sprite($spriteClass, $link = null, $title = null, $classes = '', $sortkey = '') {
 87		$attributes = array('class' => 'sly-icon');
 88		$sprite     = sly_Util_HTML::getSpriteLink($link, $title, $spriteClass);
 89
 90		if (!empty($classes)) {
 91			$classes = is_string($classes) ? trim($classes) : implode(' ', array_filter(array_unique($classes)));
 92			$attributes['class'] .= ' '.$classes;
 93		}
 94
 95		return new self($sprite, '', $sortkey, $attributes);
 96	}
 97
 98	/**
 99	 * @param  string $name
100	 * @param  string $value
101	 * @return sly_Table_Column  self
102	 */
103	public function setAttribute($name, $value) {
104		$this->htmlAttributes[$name] = trim($value);
105		return $this;
106	}
107
108	/**
109	 * @param  string $name
110	 * @param  string $value
111	 * @return string         found attribute value or null
112	 */
113	public function getAttribute($name) {
114		return sly_setarraytype($this->htmlAttributes, $name, 'string', null);
115	}
116
117	/**
118	 * @param  string $content
119	 * @return sly_Table_Column  self
120	 */
121	public function setContent($content) {
122		$this->content = $content;
123		return $this;
124	}
125
126	/**
127	 * @param  sly_Table $table
128	 * @return sly_Table_Column  self
129	 */
130	public function setTable(sly_Table $table) {
131		$this->table = $table;
132		return $this;
133	}
134
135	/**
136	 * @param  int $idx
137	 * @return sly_Table_Column  self
138	 */
139	public function setIndex($idx) {
140		$this->idx = (int) $idx;
141		return $this;
142	}
143
144	/**
145	 * @return int
146	 */
147	public function getColspan() {
148		return sly_setarraytype($this->htmlAttributes, 'colspan', 'int', 1);
149	}
150
151	/**
152	 * @param  sly_Request $request  the request to use or null for the global one
153	 * @return string
154	 */
155	public function render(sly_Request $request = null) {
156		if (!empty($this->width)) {
157			$this->htmlAttributes['style'] = 'width:'.$this->width;
158		}
159
160		$request = $request ? $request : sly_Core::getRequest();
161		$id      = $this->table->getID();
162
163		if ($request->get($id.'_sortby', 'string') === $this->sortkey) {
164			$this->direction = $request->get($id.'_direction', 'string') === 'desc' ? 'desc' : 'asc';
165		}
166		else {
167			$this->direction = 'none';
168		}
169
170		return $this->renderView('column.phtml', array('table' => $this->table, 'index' => $this->idx));
171	}
172
173	/**
174	 * @throws sly_Exception
175	 * @param  string $file
176	 * @return string
177	 */
178	protected function getViewFile($file) {
179		$full = SLY_COREFOLDER.'/views/table/'.$file;
180		if (file_exists($full)) return $full;
181
182		throw new sly_Exception(t('view_not_found', $file));
183	}
184}