PageRenderTime 240ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/core/Table/Code/MainOptions.php

https://gitlab.com/ElvisAns/tiki
PHP | 233 lines | 167 code | 13 blank | 53 comment | 60 complexity | f33896bccff630ea3c29ac65e42f29c9 MD5 | raw file
  1. <?php
  2. // (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
  3. //
  4. // All Rights Reserved. See copyright.txt for details and a complete list of authors.
  5. // Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
  6. // $Id$
  7. //this script may only be included - so its better to die if called directly.
  8. if (strpos($_SERVER['SCRIPT_NAME'], basename(__FILE__)) !== false) {
  9. header('location: index.php');
  10. exit;
  11. }
  12. /**
  13. * Class Table_Code_MainOptions
  14. *
  15. * For the options within the main section of the Tablesorter jQuery function
  16. *
  17. * @package Tiki
  18. * @subpackage Table
  19. * @uses Table_Code_Manager
  20. */
  21. class Table_Code_MainOptions extends Table_Code_Manager
  22. {
  23. /**
  24. * Generates the code for the main options
  25. */
  26. public function setCode()
  27. {
  28. $mo = [];
  29. $mo[] = 'showProcessing: ' . (parent::$s['showProcessing'] === 'y' ? 'true' : 'false');
  30. $mo[] = 'ignoreCase: ' . (parent::$s['ignoreCase'] === 'y' ? 'true' : 'false');
  31. $mo[] = 'sortLocaleCompare : ' . (parent::$s['sortLocaleCompare'] === 'y' ? 'true' : 'false');
  32. /*** onRenderHeader option - change html elements before table renders. Repeated for each column. ***/
  33. $orh = [];
  34. /* First handle column-specific code since the array index is used for the column number */
  35. $columns = empty(parent::$s['columns']) ? [] : parent::$s['columns'];
  36. $allcols = [];
  37. foreach ($columns as $col => $info) {
  38. //turn off column resizing per settings
  39. if (isset($info['resizable']) && $info['resizable'] === false) {
  40. $allcols[$col]['addClass'][] = 'resizable-false';
  41. }
  42. //row grouping and sorter settings
  43. if (parent::$sorts && parent::$sortcol) {
  44. //row grouping setting
  45. if (parent::$group) {
  46. if (! empty($info['sort']['group'])) {
  47. $allcols[$col]['addClass'][] = 'group-' . $info['sort']['group'];
  48. } else {
  49. $allcols[$col]['addClass'][] = 'group-false';
  50. }
  51. }
  52. if (! empty($info['sort']['group']) && parent::$group !== false) {
  53. $allcols[$col]['addClass'][] = 'group-' . $info['sort']['group'];
  54. }
  55. if (isset($info['sort']['type']) && $info['sort']['type'] !== true) {
  56. //add class for sort data type or for no sort
  57. $sclass = $info['sort']['type'] === false ? 'false' : $info['sort']['type'];
  58. $allcols[$col]['addClass'][] = 'sorter-' . $sclass;
  59. }
  60. }
  61. //filters
  62. if (parent::$filters && parent::$filtercol) {
  63. //set filter to false for no filter
  64. if (isset($info['filter']['type']) && $info['filter']['type'] === false) {
  65. $allcols[$col]['addClass'][] = 'filter-false';
  66. } else {
  67. //add placeholders
  68. if (isset($info['filter']['placeholder'])) {
  69. $allcols[$col]['data']['placeholder'] = $info['filter']['placeholder'];
  70. }
  71. // add special filter type
  72. if (isset($info['filter']['type']) && $info['filter']['type'] === 'dropdown' && ! isset($info['filter']['options'])) {
  73. if (! empty($allcols[$col]['addClass'])) {
  74. $allcols[$col]['addClass'] = array_filter($allcols[$col]['addClass'], function ($class) {
  75. return ! strstr($class, 'sorter-');
  76. });
  77. }
  78. $allcols[$col]['addClass'][] = 'sorter-dropdown';
  79. $allcols[$col]['addClass'][] = 'filter-parsed';
  80. $allcols[$col]['addClass'][] = 'filter-match';
  81. }
  82. }
  83. }
  84. //column select
  85. if (parent::$s['colselect']['type'] === true) {
  86. if (isset($info['priority'])) {
  87. $allcols[$col]['attr']['data-priority'] = $info['priority'];
  88. }
  89. if (! empty($info['hidden'])) {
  90. $allcols[$col]['addClass'][] = 'columnSelector-false';
  91. }
  92. }
  93. }
  94. unset($col, $info);
  95. //process columns
  96. if (count($allcols) > 0) {
  97. foreach ($allcols as $col => $info) {
  98. if (parent::$usecolselector) {
  99. $orh[$col] = 'if (id == \'' . substr($col, 1) . '\'){';
  100. } else {
  101. $orh[$col] = 'if (index == ' . $col . '){';
  102. }
  103. $orh[$col] .= '$(this)';
  104. foreach ($info as $attr => $val) {
  105. if ($attr == 'addClass') {
  106. $args = implode(' ', $val);
  107. } else {
  108. foreach ($info[$attr] as $type => $val2) {
  109. $args = $type . '\',\'' . $val2;
  110. }
  111. }
  112. $orh[$col] .= '.' . $attr . '(\'' . $args . '\')';
  113. }
  114. $orh[$col] .= ';}';
  115. }
  116. unset($col, $info);
  117. }
  118. /* Handle code that applies to all columns now that the array index is not important*/
  119. //get rid of self-links
  120. if (isset(parent::$s['selflinks']) && parent::$s['selflinks']) {
  121. $orh[] = '$(this).find(\'a\').replaceWith(\'<span>\' + $(this).find(\'a\').text() + \'</span>\');';
  122. }
  123. //no sort on all columns
  124. if (! parent::$sorts) {
  125. $orh[] = '$(this).addClass(\'sorter-false\');';
  126. }
  127. if (count($orh) > 0) {
  128. array_unshift($mo, 'headerTemplate: \'{content} {icon}\'');
  129. if (parent::$usecolselector) {
  130. array_unshift($orh, 'var id = $(this).attr(\'id\');');
  131. }
  132. $mo[] = $this->iterate($orh, 'onRenderHeader: function(index){', $this->nt2 . '}', $this->nt3, '', '');
  133. }
  134. /*** end onRenderHeader section ***/
  135. /*** widgets ***/
  136. //standard ones
  137. $w[] = 'stickyHeaders';
  138. //sort2Hash doesn't work with ajax yet
  139. if (! parent::$ajax) {
  140. $w[] = 'sort2Hash';
  141. }
  142. //only fancytable uses this and it is set in wikiplugin_fancytable.php
  143. //other tables don't show up full width due to use of table-responsive class in wrapper div
  144. if (isset(parent::$s['resizable']) && parent::$s['resizable']) {
  145. $w[] = 'resizable';
  146. }
  147. if (parent::$group) {
  148. $w[] = 'group';
  149. }
  150. //saveSort
  151. if (isset(parent::$s['sorts']['type']) && strpos(parent::$s['sorts']['type'], 'save') !== false) {
  152. $w[] = 'saveSort';
  153. }
  154. //filter
  155. if (parent::$filters) {
  156. $w[] = 'filter';
  157. }
  158. //pager
  159. if (parent::$pager) {
  160. $w[] = 'pager';
  161. }
  162. //column selector
  163. if (parent::$s['colselect']) {
  164. $w[] = 'columnSelector';
  165. }
  166. //math
  167. if (parent::$math || parent::$mathcol) {
  168. $w[] = 'math';
  169. }
  170. if (count($w) > 0) {
  171. $mo[] = $this->iterate($w, 'widgets : [', ']', '\'', '\'', ',');
  172. }
  173. /*** end widget section ***/
  174. //debug - uncomment the line below to show log of events in the browser console
  175. // $mo[] = 'debug: true';
  176. //server side sorting
  177. if (parent::$sorts && parent::$ajax) {
  178. $mo[] = 'serverSideSorting: true';
  179. }
  180. //Turn multi-column sort off (on by default by shift-clicking column headers)
  181. if (isset(parent::$s['sorts']['multisort']) && parent::$s['sorts']['multisort'] === false) {
  182. $mo[] = 'sortMultiSortKey : \'none\'';
  183. }
  184. //Sort list
  185. if (! empty(parent::$s['sorts']['sortlist'])) {
  186. $mo[] = 'sortList : [[' . parent::$s['sorts']['sortlist']['col'] . ',' . parent::$s['sorts']['sortlist']['dir'] . ']]';
  187. } elseif (parent::$sorts && parent::$sortcol) {
  188. $sl = [];
  189. $i = 0;
  190. foreach (parent::$s['columns'] as $col => $info) {
  191. $info = ! empty($info['sort']) ? $info['sort'] : [];
  192. $colpointer = parent::$usecolselector ? $i : $col;
  193. if (! empty($info['dir'])) {
  194. if ($info['dir'] === 'asc') {
  195. $sl[] = $colpointer . ',' . '0';
  196. } elseif ($info['dir'] === 'desc') {
  197. $sl[] = $colpointer . ',' . '1';
  198. }
  199. }
  200. $i++;
  201. }
  202. unset($col, $info);
  203. if (is_array($sl) && ! empty($sl)) {
  204. $mo[] = $this->iterate($sl, 'sortList : [', ']', '[', ']', ',');
  205. }
  206. }
  207. //tiki popover needs to be re-applied due to late loading of tablesorter html
  208. $p[] = $this->nt3 . '$(document).tiki_popover();';
  209. $mo[] = $this->iterate($p, 'initialized: function(table){', $this->nt2 . '}', '', '', '');
  210. //Sort image attribute
  211. if (! empty(parent::$s['sorts']['imgattr'])) {
  212. $mo[] = 'imgAttr: \'title\'';
  213. }
  214. //process main options and add to overall code
  215. if (count($mo) > 0) {
  216. $code = $this->iterate($mo, '', '', $this->nt2, '');
  217. parent::$code[self::$level1][self::$level2] = $code;
  218. }
  219. }
  220. }