Table_View.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. <?php
  2. class Table_View {
  3. /**
  4. * Uniq name for all application, data will be saved in cache (session).
  5. */
  6. var $_name;
  7. /**
  8. * Data_Source class - model to get data for table
  9. * Data_Source->get_list( $params );// pagenav, filters, etc
  10. * Data_Source->get_item( $key );// get one item by key
  11. */
  12. var $_source;
  13. /**
  14. * Column names list
  15. */
  16. var $_cols;
  17. /**
  18. * Params from pagenav, filters, etc.
  19. * pagenav params:
  20. * 'limitstart'
  21. * 'limit'
  22. */
  23. var $_url_params;
  24. var $_url_params_default;
  25. var $_filters;
  26. var $_params;
  27. var $_tbl_class;
  28. function __construct( $name, $source ) {
  29. $this->_name = $name;
  30. $this->_source = $source;
  31. $this->_cols = $this->_source->get_cols();
  32. $this->_filters = array();
  33. $this->_params = array();
  34. $this->_url_params = array();
  35. $this->_tbl_class = 'tbl-view';
  36. $this->_base_url_params = array();
  37. $this->_url_params_default['limitstart'] = 0;
  38. $this->_url_params_default['limit'] = 10;
  39. $this->_url_params_default['order_by'] = 'ID';
  40. $this->_url_params_default['order_dir'] = 'ASC';
  41. $this->_url_params = V::extend($this->_url_params_default, $this->_url_params);
  42. $this->_load_params();// load from session cache
  43. $this->_fetch_params();// fetch from request
  44. $this->_save_params();// save in session cache
  45. }
  46. function get_url_param_source() {
  47. $url_param_source = $this->get_param('url_param_source');
  48. if (!$url_param_source) $url_param_source = $_GET;
  49. return $url_param_source;
  50. }
  51. function set_search_field_prefix( $prefix ) {
  52. $this->set_param('search_field_prefix', $prefix);
  53. }
  54. function get_search_field_prefix() {
  55. return V::get('search_field_prefix', "f_", $this->_params);
  56. }
  57. function set_edit_field_prefix( $prefix ) {
  58. $this->set_param('edit_field_prefix', $prefix);
  59. }
  60. function get_edit_field_prefix() {
  61. return V::get('edit_field_prefix', "e_", $this->_params);
  62. }
  63. function _fetch_params() {
  64. $url_param_source = $this->get_url_param_source();
  65. if (isset($url_param_source['limitstart'])) $this->set_url_param('limitstart', $url_param_source['limitstart']);
  66. if (isset($url_param_source['order_by'])) $this->set_url_param('order_by', $url_param_source['order_by']);
  67. if (isset($url_param_source['order_dir'])) $this->set_url_param('order_dir', $url_param_source['order_dir']);
  68. // search
  69. foreach ($this->_cols as $field_name) {
  70. $this->_url_params_default[$this->get_search_field_prefix() . $field_name] = '%';
  71. }
  72. // first set values from GET
  73. $prefix = $this->get_search_field_prefix();
  74. foreach ($this->_cols as $field_name) {
  75. if (isset($_GET[$prefix . $field_name])) {
  76. $this->set_url_param($prefix . $field_name, $_GET[$prefix . $field_name]);
  77. }
  78. }
  79. // POST override value from GET
  80. if (!empty($_POST)) {
  81. foreach ($_POST as $k => $v) {
  82. if (substr($k, 0, 2) == 'f_') {
  83. $field_name = substr($k, 2);
  84. if (in_array($field_name, $this->_cols)) {
  85. $this->set_url_param($prefix . $field_name, $v);
  86. }
  87. }
  88. }
  89. }
  90. }
  91. function _save_params() {
  92. if (empty($_SESSION[$this->_name])) $_SESSION[$this->_name] = array();
  93. $_SESSION[$this->_name]['params'] = $this->_params;
  94. $_SESSION[$this->_name]['url_params'] = $this->_url_params;
  95. }
  96. function _load_params() {
  97. if (!empty($_SESSION[$this->_name])) {
  98. if (!empty($_SESSION[$this->_name]['params'])) {
  99. foreach ($_SESSION[$this->_name]['params'] as $key => $val) {
  100. $this->_params[$key] = $val;
  101. }
  102. }
  103. if (!empty($_SESSION[$this->_name]['url_params'])) {
  104. foreach ($_SESSION[$this->_name]['url_params'] as $key => $val) {
  105. $this->_url_params[$key] = $val;
  106. }
  107. }
  108. }
  109. }
  110. function set_cols( $cols ) {
  111. $this->_cols = $cols;
  112. }
  113. function set_base_url_params( $url_params ) {
  114. $this->_base_url_params = $url_params;
  115. }
  116. function set_param( $key, $value ) {
  117. $this->_params[$key] = $value;
  118. }
  119. function get_param( $key ) {
  120. return V::get($key, '', $this->_params);
  121. }
  122. function set_url_param( $key, $value ) {
  123. $this->_url_params[$key] = $value;
  124. }
  125. function get_url_param( $key ) {
  126. $default = V::get($key, '', $this->_url_params_default);
  127. return V::get($key, $default, $this->_url_params);
  128. }
  129. function add_filter( $field_name, $filter ) {
  130. $this->_filters[$field_name] = $filter;
  131. }
  132. /**
  133. * @return array of url params for App::link function.
  134. */
  135. function to_html_link_params( $params = array() ) {
  136. $ret = array();
  137. $base_url_params = V::extend($this->_base_url_params, $this->_url_params);
  138. $url_params = V::extend($base_url_params, $params);
  139. foreach ($url_params as $k => $v) {
  140. if (substr($k, 0, 2) == 'f_') {// search field prefix
  141. if ($v != '%') {
  142. $ret [$k] = $v;
  143. }
  144. } else {
  145. if (isset($this->_url_params_default[$k]) && $this->_url_params_default[$k] == $url_params[$k]) {
  146. } else {
  147. $ret [$k] = $v;
  148. }
  149. }
  150. }
  151. return $ret;
  152. }
  153. function search_field_to_html( $field_name, $value ) {
  154. $out = '';
  155. $type = $this->_source->get_field_sql_type($field_name);
  156. if (!$type) {
  157. return $out;
  158. }
  159. $out .= App::field_search($this->get_search_field_prefix() . $field_name, $type, $value, array('class'=>'i'));
  160. return $out;
  161. }
  162. function edit_field_to_html( $field_name, $value ) {
  163. $out = '';
  164. $type = $this->_source->get_field_sql_type($field_name);
  165. if (!$type) {
  166. return $value;
  167. }
  168. $out .= App::field($this->get_edit_field_prefix() . $field_name, $type, $value, array('class'=>'i'));
  169. return $out;
  170. }
  171. function edit_form_to_html( $edit_id ) {
  172. $out = '';
  173. $back_link = App::link("wróć", $this->to_html_link_params(), array('title'=>"wróć do tabeli", 'ico_after_text'=>'back'));
  174. $edit_id = intval($edit_id);
  175. if ($edit_id <= 0) {
  176. return '<p class="err">' . "Bledny parametr ID" . '<br />' . $back_link . '</p>';
  177. }
  178. $item = $this->_fetch_item($edit_id);
  179. if (!$item) {
  180. return '<p class="err">' . "Rekord ID=" . $edit_id . " nie istnieje" . '<br />' . $back_link . '</p>';
  181. }
  182. // allow edit callback?
  183. $out_msg = '';
  184. $out_error = '';
  185. if (!empty($_POST['sent']) && $_POST['sent'] == '1') {
  186. $affected = $this->_source->save_item($item, $_POST, $this->get_edit_field_prefix());
  187. if ($affected < 0) {
  188. $out_error .= "Blad podczas edytowania rekordu - nic nie zmieniono";
  189. } else {
  190. if ($affected == 2) {
  191. $out_msg .= "Zmieniono rekord [".$item->ID."]";
  192. }
  193. else if ($affected == 1) {
  194. $out_msg .= "Zmieniono rekord [".$item->ID."] (Error: nie zapisano hostorii)";
  195. }
  196. if ($out_msg) {
  197. $out .= '<p>' . $out_msg . " - " . $back_link . '</p>';
  198. return $out;
  199. }
  200. }
  201. }
  202. $params = array('_tbl_task'=>'hist', '_edit_id'=>$item->ID);
  203. $hist_link = App::link("historia", $this->to_html_link_params($params), array('title'=>"Historia", 'ico_after_text'=>'history'));
  204. $out .= '<b>' . "Edycja rekordu " . $item->ID . '</b>' . " - " . $hist_link . " - " . $back_link . '<br />';
  205. if ($out_error) {
  206. $out .= '<p>' . $out_error . '</p>';
  207. }
  208. $out .= '<form action="" method="POST">';
  209. $out .= '<table class="' . $this->_tbl_class . '" cellspacing="0" cellpadding="0" border="1">';
  210. $out .= '<tfoot>';
  211. $out .= '<tr>';
  212. $out .= '<td colspan="2" style="text-align:left">';
  213. $out .= '<input type="hidden" name="' . "sent" . '" value="' . "1" . '" />';
  214. $out .= '<input type="submit" value="' . "Zapisz" . '" />';
  215. $out .= '</td>';
  216. $out .= '</tr>';
  217. $out .= '</tfoot>';
  218. $out .= '<tbody>';
  219. foreach ($this->_cols as $field_name) {
  220. $field_label = $field_name;
  221. $field_value = $item->$field_name;
  222. if ($field_name != 'ID') {// donw allow edit ID field
  223. if ($this->_source->field_allow_write($field_name)) {
  224. if (isset($_POST[$this->get_edit_field_prefix() . $field_name])) {
  225. $field_value = $_POST[$this->get_edit_field_prefix() . $field_name];
  226. }
  227. $field_value = $this->edit_field_to_html($field_name, $field_value);
  228. }
  229. }
  230. $out .= '<tr>';
  231. $out .= '<th>' . $field_label . '</th>';
  232. $out .= '<td>' . $field_value . '</td>';
  233. $out .= '</tr>';
  234. }
  235. $out .= '</tbody>';
  236. $out .= '</table>';
  237. $out .= '</form>';
  238. return $out;
  239. }
  240. function create_form_to_html() {
  241. $out = '';
  242. $back_link = App::link("wróć", $this->to_html_link_params(), array('title'=>"wróć do tabeli", 'ico_after_text'=>'back'));
  243. // allow edit callback?
  244. if (!empty($_POST['sent']) && $_POST['sent'] == '1') {
  245. $insert_id = $this->_source->add_item($_POST, $this->get_edit_field_prefix());
  246. if ($insert_id > 0) {
  247. $out .= '<p>' . "Dodano nowy rekord [" . $insert_id . "]" . " - " . $back_link;
  248. $params = array('_tbl_task'=>'edit', '_edit_id'=>$insert_id);
  249. $edit_link = App::link("[" . $insert_id . "]", $this->to_html_link_params($params), array('title'=>"wróć do tabeli", 'ico_after_text'=>'edit'));
  250. $out .= " lub edytuj rekord " . $edit_link . '</p>';
  251. return $out;
  252. }
  253. }
  254. $out .= '<b>' . "Dodawanie nowego rekordu " . '</b>' . " - " . $back_link . '<br />';
  255. $out .= '<form action="" method="POST">';
  256. $out .= '<table class="' . $this->_tbl_class . '" cellspacing="0" cellpadding="0" border="1">';
  257. $out .= '<tfoot>';
  258. $out .= '<tr>';
  259. $out .= '<td colspan="2" style="text-align:left">';
  260. $out .= '<input type="hidden" name="' . "sent" . '" value="' . "1" . '" />';
  261. $out .= '<input type="submit" value="' . "Zapisz" . '" />';
  262. $out .= '</td>';
  263. $out .= '</tr>';
  264. $out .= '</tfoot>';
  265. $out .= '<tbody>';
  266. foreach ($this->_cols as $field_name) {
  267. $field_label = $field_name;
  268. $field_value = '';
  269. if ($field_name != 'ID') {// donw allow edit ID field
  270. if ($this->_source->field_allow_create($field_name)) {
  271. if (isset($_POST[$this->get_edit_field_prefix() . $field_name])) {
  272. $field_value = $_POST[$this->get_edit_field_prefix() . $field_name];
  273. }
  274. }
  275. $field_value = $this->edit_field_to_html($field_name, $field_value);
  276. }
  277. $out .= '<tr>';
  278. $out .= '<th>' . $field_label . '</th>';
  279. $out .= '<td>' . $field_value . '</td>';
  280. $out .= '</tr>';
  281. }
  282. $out .= '</tbody>';
  283. $out .= '</table>';
  284. $out .= '</form>';
  285. return $out;
  286. }
  287. function hist_to_html( $id ) {
  288. $out = '';
  289. $back_link = App::link("wróć", $this->to_html_link_params(), array('title'=>"wróć do tabeli", 'ico_after_text'=>'back'));
  290. $edit_id = intval($id);
  291. if ($id <= 0) {
  292. return '<p class="err">' . "Bledny parametr ID" . '<br />' . $back_link . '</p>';
  293. }
  294. $item = $this->_fetch_item($edit_id);
  295. if (!$item) {
  296. return '<p class="err">' . "Rekord ID=" . $edit_id . " nie istnieje" . '<br />' . $back_link . '</p>';
  297. }
  298. $out .= '<p><b>' . "Historia edycji rekordu " . $item->ID . '</b>' . " - " . $back_link . '</p>';
  299. $items = $this->_fetch_hist_items($id);
  300. $item->ID_USERS2 = 0;
  301. array_unshift($items, $item);
  302. $out .= '<style type="text/css">' . "
  303. .tbl-view .hist-current td {background-color:#FFEDD5; border-bottom:2px solid red;}
  304. .tbl-view .hist-empty {color:#666;}
  305. .tbl-view .hist-not-empty {background-color:#eee;}
  306. " . '</style>';
  307. $out .= '<table class="' . $this->_tbl_class . '" cellspacing="0" cellpadding="0" border="1">';
  308. $out .= '<thead>';
  309. $out .= '<tr id="item-' . $item->ID . '">';
  310. foreach ($this->_cols as $field_name) {
  311. $out .= '<td>';
  312. $out .= $field_name;
  313. $out .= '</td>';
  314. }
  315. $out .= '</tr>';
  316. $out .= '</thead>';
  317. $out .= '<tbody>';
  318. foreach ($items as $item) {
  319. $cls = '';
  320. if ($item->ID_USERS2 == 0) {
  321. $cls = ' class="hist-current"';
  322. }
  323. $out .= '<tr' . $cls . '>';
  324. foreach ($this->_cols as $field_name) {
  325. $cls = '';
  326. $val = '&nbsp;';
  327. if (isset($item->$field_name)) {
  328. $val = $item->$field_name;
  329. }
  330. if ($field_name != 'ID') {
  331. $cls = ($val == 'N/S;')? 'hist-empty' : 'hist-not-empty';
  332. }
  333. if ($cls) $cls = ' class="' . $cls . '"';
  334. $out .= '<td' . $cls . '>';
  335. $out .= $val;
  336. $out .= '</td>';
  337. }
  338. $out .= '</tr>';
  339. }
  340. if (count($items) < 2) {
  341. $out .= '<tr id="item-' . $item->ID . '">';
  342. $out .= '<td colspan="' . count($this->_cols) . '">' . "Brak danych w tabeli HIST" . '</td>';
  343. $out .= '</tr>';
  344. }
  345. $out .= '</tbody>';
  346. $out .= '</table>';
  347. return $out;
  348. }
  349. function _view_col_is_hidden( $col_name ) {
  350. if (array_key_exists($col_name, $_SESSION[$this->_name]['hidden_cols'])) {
  351. return $_SESSION[$this->_name]['hidden_cols'][$col_name];
  352. }
  353. }
  354. function _view_col_hide( $col_name ) {
  355. $_SESSION[$this->_name]['hidden_cols'][$col_name] = true;
  356. }
  357. function _view_col_show( $col_name ) {
  358. unset($_SESSION[$this->_name]['hidden_cols'][$col_name]);
  359. }
  360. function config_form_to_html() {
  361. $url_param_source = $this->get_url_param_source();
  362. if (!empty($url_param_source['_hide_col'])) {
  363. if ($url_param_source['_hide_col'] != 'ID') {
  364. $this->_view_col_hide($url_param_source['_hide_col']);
  365. }
  366. }
  367. if (!empty($url_param_source['_show_col'])) {
  368. if ($url_param_source['_show_col'] != 'ID') {
  369. $this->_view_col_show($url_param_source['_show_col']);
  370. }
  371. }
  372. $out = '';
  373. $back_link = App::link("wróć", $this->to_html_link_params(), array('title'=>"wróć do tabeli", 'ico_after_text'=>'back'));
  374. $out .= '<p><b>' . "Konfiguracja tabeli " . '</b>' . " - " . $back_link . '</p>';
  375. $out .= '<table class="tbl-view" cellspacing="0" cellpadding="0" border="1">';
  376. foreach ($this->_cols as $field_name) {
  377. $out .= '<tr>';
  378. $out .= '<td>' . $field_name . '</td>';
  379. $out .= '<td>';
  380. if ($field_name != 'ID') {
  381. if ($this->_view_col_is_hidden($field_name)) {
  382. $link_params = array('_tbl_task'=>'config', '_show_col'=>$field_name);
  383. $out .= App::link("show", $this->to_html_link_params($link_params), array('class'=>'btn'));
  384. } else {
  385. $link_params = array('_tbl_task'=>'config', '_hide_col'=>$field_name);
  386. $out .= App::link("hide", $this->to_html_link_params($link_params), array('class'=>'btn'));
  387. }
  388. }
  389. $out .= '</td>';
  390. $out .= '</tr>';
  391. }
  392. $out .= '</table>';
  393. return $out;
  394. }
  395. function table_to_html() {
  396. $out = '';
  397. if (empty($this->_cols)) {
  398. trigger_error("No cols set in " . __CLASS__, E_USER_NOTICE);
  399. return $out;
  400. }
  401. $items = $this->_fetch_items();
  402. $total = $this->_fetch_total();
  403. $out .= '<style type="text/css">' . "
  404. .order-dir {color:blue; text-decoration:none; font-weight:bold;}
  405. .order-dir:hover {}
  406. .order-current {color:red;}
  407. .order-current:hover {}
  408. .tbl-view .find td.selected {border-bottom: 2px solid red;}
  409. .tbl-view tbody tr.selected td {background-color:#FFEDD5;}
  410. .tbl-view-txt-zwin tbody td {white-space:nowrap; max-width:200px; overflow:hidden;}
  411. .tbl-view .tbl-cell-links,
  412. .tbl-view .config td,
  413. .tbl-view .config th{background-color:#D7EED7;}
  414. " . '</style>';
  415. $out .= '<form action="" method="POST">';
  416. $out .= '<table class="' . $this->_tbl_class . '" cellspacing="0" cellpadding="0" border="1">';
  417. $out .= '<thead>';
  418. $out .= '<tr>';
  419. $out .= '<td class="tbl-cell-links">';
  420. // TODO: tbl config, perm edit?
  421. $link_params = array('_tbl_task'=>'create');
  422. $out .= ' '.App::link("Add", $this->to_html_link_params($link_params), array('ico'=>'add', 'title'=>"Dodaj nowy rekord"));
  423. $link_params = array('_tbl_task'=>'config');
  424. $js = "jQuery(this).parents('thead').find('.config').toggle(); return false;";
  425. $out .= ' '.App::link("konfiguracja", $this->to_html_link_params($link_params), array('ico'=>'excel', 'title'=>"Konfiguracja", 'onclick'=>$js));
  426. $out .= '</td>';
  427. foreach ($this->_cols as $field_name) {
  428. if ($this->_view_col_is_hidden($field_name)) {
  429. continue;
  430. }
  431. $cur_order_by = false;
  432. $cur_order_by_link = '';
  433. // order by field
  434. if ($this->get_url_param('order_by') == $field_name) {
  435. $cur_order_by = true;
  436. if ($this->get_url_param('order_dir') == 'DESC') {
  437. $cur_order_by_link .= App::link("^", $this->to_html_link_params(array('order_by'=>$field_name, 'order_dir'=>'ASC')), array('title'=>"asc", 'class'=>"order-dir order-current"));
  438. } else {
  439. $cur_order_by_link .= App::link("v", $this->to_html_link_params(array('order_by'=>$field_name, 'order_dir'=>'DESC')), array('title'=>"desc", 'class'=>"order-dir order-current"));
  440. }
  441. } else {
  442. $cur_order_by_link .= App::link("^", $this->to_html_link_params(array('order_by'=>$field_name, 'order_dir'=>'ASC')), array('title'=>"asc", 'class'=>"order-dir"));
  443. $cur_order_by_link .= " ";
  444. $cur_order_by_link .= App::link("v", $this->to_html_link_params(array('order_by'=>$field_name, 'order_dir'=>'DESC')), array('title'=>"desc", 'class'=>"order-dir"));
  445. }
  446. $out .= '<td>';
  447. // TODO: if set header for this column
  448. $out .= str_replace('_', ' ', $field_name);
  449. if ($cur_order_by_link) {
  450. $out .= " " . '<nobr>' . $cur_order_by_link . '</nobr>';
  451. }
  452. $out .= '</td>';
  453. }
  454. $out .= '</tr>';
  455. $out .= '<tr class="config" style="display:none">';
  456. $out .= '<td>' . "&nbsp;" . '</td>';
  457. foreach ($this->_cols as $field_name) {
  458. if ($this->_view_col_is_hidden($field_name)) {
  459. continue;
  460. }
  461. $out .= '<th>';
  462. if ($field_name != 'ID') {
  463. if (!$this->_view_col_is_hidden($field_name)) {
  464. $link_params = array('_tbl_task'=>'config', '_hide_col'=>$field_name);
  465. $out .= App::link("x", $this->to_html_link_params($link_params), array('title'=>"Ukryj kolumnę " . $field_name, 'ico'=>'del.png'));
  466. }
  467. }
  468. $out .= '</th>';
  469. }
  470. $out .= '</tr>';
  471. $out .= '<tr class="config" style="display:none">';
  472. $out .= '<td colspan="' . (count($this->_cols) + 1) . '">';// TODO: visible cols count
  473. // widok: full, line
  474. $js = "var tbl=jQuery(this).parents('table:first');tbl.toggleClass('tbl-view-txt-zwin');if(tbl.hasClass('tbl-view-txt-zwin')){jQuery(this).attr('title','Pełny tekst')}else{jQuery(this).attr('title','krótki tekst')};return false;";
  475. $link_params = array('_tbl_task'=>'config', 'widok'=>"full");
  476. $out .= App::link("tekst", $this->to_html_link_params($link_params), array('title'=>"Pełny tekst", 'onclick'=>$js));
  477. $out .= '</td>';
  478. $out .= '</tr>';
  479. if ($this->get_param('show_search')) {
  480. $out .= '<tr class="find">';
  481. $out .= '<td>';
  482. $out .= '<input type="image" value="' . "Szukaj" . '" src="' . "icon/search.png" . '" title="' . "Szukaj" . '" />';
  483. // TODO: add clear btn for this form
  484. $filter_selected = false;
  485. foreach ($this->_cols as $field_name) {
  486. $val = $this->get_url_param($this->get_search_field_prefix() . $field_name);
  487. if ($val != '%') $filter_selected = true;
  488. }
  489. if ($filter_selected) {
  490. $out .= '<input type="image" value="' . "Czyść" . '" src="' . "icon/del.png" . '" title="' . "Czyść" . '" onclick="return reset_search_form(this);" />';
  491. }
  492. $out .= '</td>';
  493. foreach ($this->_cols as $field_name) {
  494. if ($this->_view_col_is_hidden($field_name)) {
  495. continue;
  496. }
  497. $val = $this->get_url_param($this->get_search_field_prefix() . $field_name);
  498. $cls = '';
  499. if ($val != '%') $cls .= 'selected';
  500. if ($cls) $cls = ' class="' . $cls . '"';
  501. $out .= '<td'.$cls.'>';
  502. // TODO: filters for this field
  503. $out .= $this->search_field_to_html($field_name, $val);
  504. $out .= '</td>';
  505. }
  506. $out .= '</tr>';
  507. }
  508. $out .= '</thead>';
  509. $out .= '<tfoot>';
  510. $out .= '<td colspan="' . (count($this->_cols) + 1) . '">';// TODO: visible cols count
  511. $page_nav = new stdClass();
  512. $page_nav->total = $total;
  513. $page_nav->limit = $this->_url_params['limit'];
  514. $page_nav->current = $this->_url_params['limitstart'];
  515. $page_nav->offset_prev = $page_nav->current - $page_nav->limit;
  516. if ($page_nav->offset_prev < 0) $page_nav->offset_prev = 0;
  517. $page_nav->offset_next = $page_nav->current + $page_nav->limit;
  518. $page_nav->offset_end = floor($total / $page_nav->limit) * $page_nav->limit;
  519. $link_params = array('limitstart'=>0);
  520. $out .= App::link("&lt;&lt;", $this->to_html_link_params($link_params), array('class'=>'btn'));
  521. $link_params = array('limitstart'=>$page_nav->offset_prev);
  522. $out .= ' '.App::link("&lt; -" . $page_nav->limit, $this->to_html_link_params($link_params), array('class'=>'btn'));
  523. $out .= ' <b>' . $page_nav->current . '</b> <i title="' . "wszystkich " . $page_nav->total . '">(' . $page_nav->total . ')</i> ';
  524. $link_params = array('limitstart'=>$page_nav->offset_next);
  525. $out .= ' '.App::link("+" . $page_nav->limit . " &gt;", $this->to_html_link_params($link_params), array('class'=>'btn'));
  526. $link_params = array('limitstart'=>$page_nav->offset_end);
  527. $out .= ' '.App::link("&gt;&gt;", $this->to_html_link_params($link_params), array('class'=>'btn'));
  528. $out .= '</td>';
  529. $out .= '</tfoot>';
  530. $out .= '<tbody>';
  531. foreach ($items as $item) {
  532. $out .= '<tr id="item-' . $item->ID . '">';
  533. $out .= '<td>';
  534. // edit - allow edit callback?
  535. $link_params = array('_tbl_task'=>'edit', '_edit_id'=>$item->ID);
  536. $out .= ' '.App::link("Edit", $this->to_html_link_params($link_params), array('ico'=>'edit.png', 'title'=>"Edytuj " . $item->ID));
  537. // hist
  538. $link_params = array('_tbl_task'=>'hist', '_edit_id'=>$item->ID);
  539. $out .= ' '.App::link("Historia", $this->to_html_link_params($link_params), array('ico'=>'history', 'title'=>"Historia rekordu " . $item->ID));
  540. $out .= '</td>';
  541. foreach ($this->_cols as $field_name) {
  542. if ($this->_view_col_is_hidden($field_name)) {
  543. continue;
  544. }
  545. $out .= '<td>';
  546. if (isset($item->$field_name)) {
  547. $out .= $item->$field_name;
  548. } else {
  549. $out .= '&nbsp;';
  550. }
  551. $out .= '</td>';
  552. }
  553. $out .= '</tr>';
  554. }
  555. $out .= '</tbody>';
  556. $out .= '</table>';
  557. $out .= '</form>';
  558. $out .= '<script type="text/javascript">' . "
  559. jQuery(document).ready(function(){
  560. jQuery('." . $this->_tbl_class . "').addClass('tbl-view-txt-zwin');
  561. jQuery('." . $this->_tbl_class . " tbody tr').click(function(){
  562. jQuery(this).toggleClass('selected');
  563. console.log( jQuery(this).attr('id') )
  564. });
  565. });
  566. function reset_search_form(n){
  567. console.log(n)
  568. var p=jQuery(n).parents('.find:first');
  569. console.log(p)
  570. if (!p) return false;
  571. p.find('td').removeClass('selected');
  572. fields=p.find('input').val('%');
  573. fields=p.find('select').each(function(ind,field){
  574. switch (field.type.toLowerCase()){
  575. case 'text':
  576. field.value='%';
  577. break;
  578. case 'select-one':
  579. // field.options[0].selected = true;
  580. // break;
  581. case 'select-multiple':
  582. for (z = 0; z < field.options.length; z++) {
  583. field.options[z].selected = false;
  584. if (field.options[z].hasAttribute('selected')) {
  585. field.options[z].removeAttribute('selected');
  586. }
  587. }
  588. // select first element
  589. field.options[0].selected = true;
  590. field.options[0].setAttribute('selected', 'selected');
  591. break;
  592. default:
  593. }
  594. });
  595. return false;
  596. }
  597. " . '</script>';
  598. return $out;
  599. }
  600. /**
  601. * @param '_tbl_task' ('edit', 'hist', 'create', 'config')
  602. * @param '_edit_id'
  603. */
  604. function to_html() {
  605. $url_param_source = $this->get_url_param_source();
  606. $_tbl_task = $url_param_source['_tbl_task'];
  607. switch ($_tbl_task) {
  608. case 'edit':
  609. return $this->edit_form_to_html($url_param_source['_edit_id']);
  610. break;
  611. case 'hist':
  612. return $this->hist_to_html($url_param_source['_edit_id']);
  613. break;
  614. case 'create':
  615. return $this->create_form_to_html();
  616. break;
  617. case 'config':
  618. return $this->config_form_to_html();
  619. break;
  620. default:
  621. return $this->table_to_html();
  622. }
  623. }
  624. function &_fetch_items() {
  625. return $this->_source->get_items($this->_url_params);
  626. }
  627. function &_fetch_item( $id ) {
  628. return $this->_source->get_item($id);
  629. }
  630. function &_fetch_hist_items( $id ) {
  631. return $this->_source->get_hist_items($id);
  632. }
  633. function _fetch_total() {
  634. return $this->_source->get_total($this->_url_params);
  635. }
  636. }