/WikiZam/extensions/Skinzam/model/SkinzamTablePager.php

https://github.com/Seizam/seizamcore · PHP · 353 lines · 154 code · 38 blank · 161 comment · 11 complexity · 4ee4d8a67496135daf785f3a10bd8b3c MD5 · raw file

  1. <?php
  2. if (!defined('MEDIAWIKI')) {
  3. die(-1);
  4. }
  5. /**
  6. * Use TablePager for prettified Transactions listing.
  7. */
  8. class SkinzamTablePager extends TablePager {
  9. # Fields to be redeclared for default behavior
  10. protected $selectTables; # Array
  11. protected $selectFields = '*'; # Array
  12. protected $selectConds = array(); # Array
  13. protected $selectOptions = array();
  14. protected $selectJoinConditions = array();
  15. protected $sortableFields = '*'; # Array
  16. protected $tableClasses = array(); # Array
  17. protected $defaultSort; # String (field)
  18. public $mDefaultDirection = true; # Boolean (true=decrescent)
  19. public $forceDefaultLimit = 10; # if > 0 use instead of $wgUser->getOption( 'rclimit' )
  20. protected $messagesPrefix = 'sz-'; # String
  21. # Do not redeclare
  22. protected $fieldNames = null; # The main array fieldName => i18n
  23. protected $fieldFullNames = null; # Fallback array fieldName => fieldFullName for offset
  24. private $defaultTableClasses = array('TablePager');
  25. protected $even = true;
  26. private $header = '';
  27. private $footer = '';
  28. public function __construct() {
  29. parent::__construct();
  30. if ($this->forceDefaultLimit > 0)
  31. list( $this->mLimit, /* $offset */ ) = $this->mRequest->getLimitOffset($this->forceDefaultLimit, '');
  32. }
  33. /**
  34. * ABSTRACT! Can be redeclared, don't forget to call parent and merge results.
  35. * Format a table cell. The return value should be HTML, but use an empty
  36. * string not &#160; for empty cells. Do not include the <td> and </td>.
  37. *
  38. * The current result row is available as $this->mCurrentRow, in case you
  39. * need more context.
  40. *
  41. * @param $name String: the database field name
  42. * @param $value String: the value retrieved from the database
  43. */
  44. function formatValue($name, $value) {
  45. return $value;
  46. }
  47. /**
  48. * Get any extra attributes to be applied to the given cell. Don't
  49. * take this as an excuse to hardcode styles; use classes and
  50. * CSS instead. Row context is available in $this->mCurrentRow
  51. *
  52. * @param $field The column
  53. * @param $value The cell contents
  54. * @return Associative array
  55. */
  56. function getCellAttrs($field, $value) {
  57. $classes = $this->getCellClasses($field, $value);
  58. $classes[] = $field;
  59. return array('class' => implode(' ', $classes));
  60. }
  61. /**
  62. * SEMI-ABSTRACT! Can be redeclared, don't forget to call parent and merge results.
  63. * Get array of class names to be applied to the given row.
  64. *
  65. * @param $row Object: the database result row
  66. * @return Array
  67. */
  68. function getCellClasses($field, $value) {
  69. return array();
  70. }
  71. /**
  72. * Get class names to be applied to the given row.
  73. *
  74. * @param $row Object: the database result row
  75. * @return Array
  76. */
  77. function getRowAttrs($row) {
  78. $classes = $this->getRowClasses($row);
  79. # Apply class="even" to every even row (theader included)
  80. if ($this->even)
  81. $classes[] = 'mw-line-even';
  82. else
  83. $classes[] = 'mw-line-odd';
  84. $this->even = !$this->even;
  85. return array('class' => implode(' ', $classes));
  86. }
  87. /**
  88. * SEMI-ABSTRACT! Can be redeclared.
  89. * Get array of class names to be applied to the given row.
  90. *
  91. * @param $row Object: the database result row
  92. * @return Array
  93. */
  94. function getRowClasses($row) {
  95. return array();
  96. }
  97. /**
  98. * Use this function to pass the name of db tables
  99. *
  100. * @param string $selectTable
  101. */
  102. public function setSelectTables($selectTables) {
  103. $this->selectTables = $selectTables;
  104. }
  105. /**
  106. * Use this function to pass an array of fields to be displayed ('*' for all).
  107. *
  108. * @param array $selectFields
  109. */
  110. public function setSelectFields($selectFields) {
  111. $this->selectFields = $selectFields;
  112. $this->setFieldNames();
  113. }
  114. /**
  115. * Use this function to pass an array of SQL conditions for the query.
  116. *
  117. * @param array $selectConds
  118. */
  119. public function setSelectConds($selectConds) {
  120. $this->selectConds = $selectConds;
  121. }
  122. /**
  123. * Use this function to pass an array of SQL options for the query.
  124. *
  125. * @param array $selectOptions
  126. */
  127. public function setSelectOptions($selectOptions) {
  128. $this->selectOptions = $selectOptions;
  129. }
  130. /**
  131. * Use this function to pass an array of SQL join conditions for the query.
  132. *
  133. * @param array $selectJoinConditions
  134. */
  135. public function setSelectJoinConditions($selectJoinConditions) {
  136. $this->selectJoinConditions = $selectJoinConditions;
  137. }
  138. /**
  139. * Use this function to pass an array of fields to be made sortable ('*' for all).
  140. *
  141. * @param array $sortableFields
  142. */
  143. public function setFieldSortable($sortableFields) {
  144. $this->sortableFields = $sortableFields;
  145. }
  146. /**
  147. * Determine if $field should be sortable
  148. *
  149. * @param string $field
  150. * @return boolean
  151. */
  152. function isFieldSortable($field) {
  153. if (!is_array($this->sortableFields)) {
  154. if ($this->sortableFields === '*') {
  155. return true;
  156. } else {
  157. return false;
  158. }
  159. } else if (in_array($field, $this->sortableFields))
  160. return true;
  161. }
  162. /**
  163. * This function should be overridden to provide all parameters
  164. * needed for the main paged query. It returns an associative
  165. * array with the following elements:
  166. * tables => Table(s) for passing to Database::select()
  167. * fields => Field(s) for passing to Database::select(), may be *
  168. * conds => WHERE conditions
  169. * options => option array
  170. * join_conds => JOIN conditions
  171. *
  172. * @return Array
  173. */
  174. function getQueryInfo() {
  175. $infos = array();
  176. $infos['tables'] = $this->selectTables;
  177. $infos['fields'] = $this->selectFields;
  178. $infos['conds'] = $this->selectConds;
  179. $infos['options'] = $this->selectOptions;
  180. $infos['join_conds'] = $this->selectJoinConditions;
  181. return $infos;
  182. }
  183. /**
  184. * Get class names to be applied to table
  185. *
  186. * @return string
  187. */
  188. function getTableClass() {
  189. return implode(' ', array_merge($this->tableClasses, $this->defaultTableClasses));
  190. }
  191. /**
  192. * Set class names to be applied to table
  193. *
  194. */
  195. public function setTableClass($classes) {
  196. if (!is_array($classes))
  197. $classes = explode(' ', $classes);
  198. $this->tableClasses = array_merge($this->tableClasses, $classes);
  199. }
  200. /**
  201. * Get default field for sorting
  202. *
  203. */
  204. function getDefaultSort() {
  205. return $this->defaultSort;
  206. }
  207. /**
  208. * Set default field for sorting
  209. *
  210. */
  211. public function setDefaultSort($field) {
  212. $this->defaultSort = $field;
  213. }
  214. function setFieldNames($fieldNames = null) {
  215. if (is_null($fieldNames))
  216. foreach ($this->selectFields as $field) {
  217. $alias = $this->getAliasFromString($field);
  218. $fieldNames[$alias] = htmlspecialchars($alias);
  219. $fieldFullNames[$alias] = $field;
  220. }
  221. $this->fieldNames = $fieldNames;
  222. $this->fieldFullNames = $fieldFullNames;
  223. }
  224. /**
  225. *
  226. * @param String $fieldName
  227. * @return String The field name without table prefix
  228. */
  229. private function getAliasFromString($fieldName) {
  230. $burst = explode('.', $fieldName);
  231. return end($burst);
  232. }
  233. /**
  234. * Get array of field names to be displayed in thead
  235. *
  236. */
  237. function getFieldNames() {
  238. if (is_null($this->fieldNames))
  239. $this->setFieldNames();
  240. return $this->fieldNames;
  241. }
  242. /**
  243. *
  244. * @return String FullName of Sort String
  245. */
  246. function getIndexField() {
  247. return $this->fieldFullNames[$this->getAliasFromString($this->mSort)];
  248. }
  249. /**
  250. * Set prefix for i18n messages
  251. *
  252. */
  253. public function setMessagesPrefix($prefix) {
  254. $this->messagesPrefix = $prefix;
  255. }
  256. /**
  257. * Set Header Text
  258. *
  259. * @param string $html
  260. */
  261. public function setHeader($html) {
  262. $this->header = Html::rawElement('div', array('class' => 'table_header informations'), $html);
  263. }
  264. /**
  265. * Set Footer Text
  266. *
  267. * @param string $html
  268. */
  269. public function setFooter($html) {
  270. $this->footer = Html::rawElement('div', array('class' => 'table_footer informations'), $html);
  271. }
  272. /**
  273. *
  274. * @return string
  275. */
  276. public function getHeader() {
  277. return $this->header;
  278. }
  279. /**
  280. *
  281. * @return string
  282. */
  283. public function getFooter() {
  284. return $this->footer;
  285. }
  286. /**
  287. *
  288. * @param String $html
  289. * @return String
  290. */
  291. public function makeHeader($html) {
  292. $this->setHeader($html);
  293. return $this->header;
  294. }
  295. /**
  296. *
  297. * @param String $html
  298. * @return String
  299. */
  300. public function makeFooter($html) {
  301. $this->setFooter($html);
  302. return $this->footer;
  303. }
  304. /**
  305. * Easy Form Printout
  306. *
  307. * @return string
  308. */
  309. public function getWholeHtml() {
  310. return $this->getHeader()
  311. . $this->getBody()
  312. . $this->getNavigationBar()
  313. . $this->getFooter();
  314. }
  315. }