PageRenderTime 48ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/mediastuttgart/sallycms-0.7
PHP | 182 lines | 85 code | 25 blank | 72 comment | 9 complexity | c9c30e3fb615629bf10ca0e865add4ec MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright (c) 2013, 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. * @ingroup table
  12. */
  13. class sly_Table_Column extends sly_Viewable {
  14. protected $width; ///< string
  15. protected $sortkey; ///< string
  16. protected $direction; ///< string
  17. protected $htmlAttributes; ///< array
  18. protected $content; ///< string
  19. private $table; ///< sly_Table
  20. private $idx; ///< int
  21. /**
  22. * @param string $content
  23. * @param string $width
  24. * @param string $sortkey
  25. * @param array $htmlAttributes
  26. */
  27. public function __construct($content, $width = '', $sortkey = '', $htmlAttributes = array()) {
  28. $this->content = $content;
  29. $this->width = $width;
  30. $this->sortkey = $sortkey;
  31. $this->htmlAttributes = $htmlAttributes;
  32. }
  33. /**
  34. * @param string $content
  35. * @param mixed $classes CSS classes as either a string or an array
  36. * @param string $sortkey
  37. * @param array $attributes
  38. * @param string $width
  39. * @return sly_Table_Column
  40. */
  41. public static function factory($content, $classes = '', $sortkey = '', array $attributes = null, $width = '') {
  42. $attributes = (array) $attributes;
  43. if (!empty($classes)) {
  44. $classes = is_string($classes) ? trim($classes) : implode(' ', array_filter(array_unique($classes)));
  45. $attributes['class'] = $classes;
  46. }
  47. return new self($content, $width, $sortkey, $attributes);
  48. }
  49. /**
  50. * @param string $uri URI to the icon
  51. * @param string $link optional URI to link to
  52. * @param mixed $classes CSS classes as either a string or an array
  53. * @param string $sortkey
  54. * @return sly_Table_Column
  55. */
  56. public static function icon($uri, $link = null, $title = null, $classes = '', $sortkey = '') {
  57. $attributes = array('class' => 'sly-icon');
  58. $icon = sprintf('<img src="" alt="" title="%s" />', $uri, sly_html($title));
  59. if ($link) {
  60. $icon = sprintf('<a href="%s">%s</a>', sly_html($link), $icon);
  61. }
  62. if (!empty($classes)) {
  63. $classes = is_string($classes) ? trim($classes) : implode(' ', array_filter(array_unique($classes)));
  64. $attributes['class'] .= ' '.$classes;
  65. }
  66. return new self($icon, '', $sortkey, $attributes);
  67. }
  68. /**
  69. * @param string $uri URI to the icon
  70. * @param string $link optional URI to link to
  71. * @param mixed $classes CSS classes as either a string or an array
  72. * @param string $sortkey
  73. * @return sly_Table_Column
  74. */
  75. public static function sprite($spriteClass, $link = null, $title = null, $classes = '', $sortkey = '') {
  76. $attributes = array('class' => 'sly-icon');
  77. $sprite = sly_Util_HTML::getSpriteLink($link, $title, $spriteClass);
  78. if (!empty($classes)) {
  79. $classes = is_string($classes) ? trim($classes) : implode(' ', array_filter(array_unique($classes)));
  80. $attributes['class'] .= ' '.$classes;
  81. }
  82. return new self($sprite, '', $sortkey, $attributes);
  83. }
  84. /**
  85. * @param string $name
  86. * @param string $value
  87. * @return sly_Table_Column self
  88. */
  89. public function setAttribute($name, $value) {
  90. $this->htmlAttributes[$name] = trim($value);
  91. return $this;
  92. }
  93. /**
  94. * @param string $name
  95. * @param string $value
  96. * @return string found attribute value or null
  97. */
  98. public function getAttribute($name) {
  99. return sly_setarraytype($this->htmlAttributes, $name, 'string', null);
  100. }
  101. /**
  102. * @param string $content
  103. * @return sly_Table_Column self
  104. */
  105. public function setContent($content) {
  106. $this->content = $content;
  107. return $this;
  108. }
  109. /**
  110. * @param sly_Table $table
  111. * @return sly_Table_Column self
  112. */
  113. public function setTable(sly_Table $table) {
  114. $this->table = $table;
  115. return $this;
  116. }
  117. /**
  118. * @param int $idx
  119. * @return sly_Table_Column self
  120. */
  121. public function setIndex($idx) {
  122. $this->idx = (int) $idx;
  123. return $this;
  124. }
  125. /**
  126. * @return int
  127. */
  128. public function getColspan() {
  129. return sly_setarraytype($this->htmlAttributes, 'colspan', 'int', 1);
  130. }
  131. /**
  132. * @return string
  133. */
  134. public function render() {
  135. if (!empty($this->width)) {
  136. $this->htmlAttributes['style'] = 'width:'.$this->width;
  137. }
  138. $id = $this->table->getID();
  139. if (sly_get($id.'_sortby', 'string') === $this->sortkey) {
  140. $this->direction = sly_get($id.'_direction', 'string') == 'desc' ? 'desc' : 'asc';
  141. }
  142. else {
  143. $this->direction = 'none';
  144. }
  145. return $this->renderView('column.phtml', array('table' => $this->table, 'index' => $this->idx));
  146. }
  147. /**
  148. * @throws sly_Exception
  149. * @param string $file
  150. * @return string
  151. */
  152. protected function getViewFile($file) {
  153. $full = SLY_COREFOLDER.'/views/table/'.$file;
  154. if (file_exists($full)) return $full;
  155. throw new sly_Exception(t('view_not_found', $file));
  156. }
  157. }