ProcesLogEdit.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. <?php
  2. /**
  3. * Proces Log Edit form
  4. *
  5. * State saved in session @see save_state, read_state
  6. *
  7. * _komorki - ID jest dodawana automatycznie
  8. * TODO: ID moze byc tez we wskaznikach
  9. * TODO: w _komorki jest tylko ID ze wskaznikow - show Error - blednie zdefiniowane wskazniki
  10. */
  11. class ProcesLogEdit {
  12. private $_log_id;// Log ID
  13. private $_log_wsk_id;// Log wsk ID
  14. private $_tbl_zasob_id;
  15. private $_komorki;// array of wskazniki (typu KOMORKA)
  16. private $_v;// array of form field values
  17. private $_filters;// array of table filter values
  18. private $_filters_readonly;// array of readonly filters
  19. private $_nav_filters;// array of table filter values
  20. private $_nav_filters_defaults;// array of table filter default values
  21. private $_errors;// array of errors in form
  22. private $_id_key;// Record ID
  23. private $_last_id_key;// ostatnio edytowany Record ID - value from DB, must be set after create object
  24. function __construct($log_id, $log_wsk_id) {
  25. $this->_log_id = $log_id;
  26. $this->_log_wsk_id = $log_wsk_id;
  27. $this->_tbl_zasob_id = 0;
  28. $this->_komorki = array();
  29. $this->_v = array();
  30. $this->_errors = array();
  31. $this->_filters = array();
  32. $this->_filters_readonly = array();
  33. $this->_nav_filters = array();
  34. $this->_nav_filters_defaults = array();// name => default
  35. $this->_nav_filters_defaults ['f-_order_by'] = 'ID';
  36. $this->_nav_filters_defaults ['f-_order_desc'] = 'DESC';
  37. $this->_nav_filters_defaults ['f-_offset'] = '';
  38. $this->_nav_filters_defaults ['f-_limit'] = '10';
  39. $this->_id_key = 0;
  40. $this->_last_id_key = 0;
  41. $this->read_state();
  42. }
  43. function __destruct() {
  44. $this->save_state();
  45. }
  46. /**
  47. * Set current edit row data.
  48. * Only first time - used by form set value.
  49. */
  50. function set_row($row) {
  51. if ($this->get('ID') == $row->f_ID) {
  52. return;// only if not in cache
  53. }
  54. $row_fields = get_object_vars($row);
  55. foreach ($row_fields as $k => $v) {
  56. $k_field = (substr($k, 0, 2) == 'f_')? substr($k, 2) : $k;
  57. $this->set($k_field, $v);
  58. }
  59. }
  60. /**
  61. * Clear current edit row data.
  62. */
  63. function clear_row() {
  64. $this->_v = array();
  65. }
  66. function set($k_wsk_id, $val) {
  67. if (substr($k_wsk_id, 0, 2) == 'v-') {
  68. $k_wsk_id = substr($k_wsk_id, 2);
  69. }
  70. if ($k_wsk_id == 'ID' || array_key_exists($k_wsk_id, $this->_komorki)) {
  71. $this->_v[$k_wsk_id] = $val;
  72. }
  73. }
  74. function get($k_wsk_id) {
  75. if (substr($k_wsk_id, 0, 2) == 'v-') {
  76. $k_wsk_id = substr($k_wsk_id, 2);
  77. }
  78. if (array_key_exists($k_wsk_id, $this->_v)) {
  79. return $this->_v[$k_wsk_id];
  80. }
  81. return null;
  82. }
  83. /**
  84. * Set edit values from request or from given args.
  85. * @param $args - array of args - $_POST, $_GET or another array, if not set use $_REQUEST
  86. */
  87. public function set_values_from_request($_args = array()) {
  88. if (empty($_args)) {
  89. $_args = $_REQUEST;
  90. }
  91. foreach ($this->get_komorki() as $k_wsk_id => $v_cell) {
  92. if (isset($_args['v-' . $k_wsk_id])) {
  93. $this->set($k_wsk_id, V::get('v-' . $k_wsk_id, '%', $_args));
  94. }
  95. }
  96. }
  97. /**
  98. * Set filters from request or from given args.
  99. * @param $args - array of args - $_POST, $_GET or another array, if not set use $_REQUEST
  100. */
  101. public function set_filters_from_request($_args = array()) {
  102. if (empty($_args)) {
  103. $_args = $_REQUEST;
  104. }
  105. $this->set_filter('ID', V::get('f-ID', '%', $_args));
  106. foreach ($this->get_komorki() as $k_wsk_id => $v_cell) {
  107. if (isset($_args['f-' . $k_wsk_id])) {
  108. $this->set_filter($k_wsk_id, V::get('f-' . $k_wsk_id, '%', $_args));
  109. }
  110. }
  111. foreach ($this->_nav_filters_defaults as $k_fltr => $v_default) {
  112. if (isset($_args[$k_fltr])) {
  113. $this->set_nav_filter($k_fltr, $_args[$k_fltr]);
  114. }
  115. }
  116. }
  117. public function set_filter($k_wsk_id, $val) {
  118. if (substr($k_wsk_id, 0, 2) == 'f-') $k_wsk_id = substr($k_wsk_id, 2);
  119. if (array_key_exists($k_wsk_id, $this->_filters_readonly)) {
  120. return;// prevent for change readonly values
  121. }
  122. if ($val == '%') {
  123. unset($this->_filters[$k_wsk_id]);
  124. }
  125. if ($k_wsk_id == 'ID' || array_key_exists($k_wsk_id, $this->_komorki)) {
  126. $this->_filters[$k_wsk_id] = $val;
  127. }
  128. }
  129. /**
  130. * Get filter value - default '%'.
  131. */
  132. function get_filter($k_wsk_id) {
  133. if (substr($k_wsk_id, 0, 2) == 'f-') $k_wsk_id = substr($k_wsk_id, 2);
  134. if (array_key_exists($k_wsk_id, $this->_filters_readonly)) {
  135. return $this->_filters_readonly[$k_wsk_id];// TODO: evaluate by values in _HIST
  136. }
  137. if (array_key_exists($k_wsk_id, $this->_filters)) {
  138. return $this->_filters[$k_wsk_id];
  139. }
  140. return '%';
  141. }
  142. function set_nav_filter($fltr, $value) {
  143. if (array_key_exists($fltr, $this->_nav_filters_defaults)) {
  144. $this->_nav_filters[$fltr] = $value;
  145. }
  146. }
  147. function get_nav_filter($fltr) {
  148. return (array_key_exists($fltr, $this->_nav_filters_defaults))? V::get($fltr, $this->_nav_filters_defaults[$fltr], $this->_nav_filters) : '';
  149. }
  150. function add_error($msg) {
  151. $this->_errors []= $msg;
  152. }
  153. function has_errors() {
  154. return !empty($this->_errors);
  155. }
  156. function get_errors() {
  157. return $this->_errors;
  158. }
  159. function get_log_id() {
  160. return $this->_log_id;
  161. }
  162. function get_log_wsk_id() {
  163. return $this->_log_wsk_id;
  164. }
  165. function set_tbl_zasob_id($tbl_zasob_id) {
  166. $this->_tbl_zasob_id = $tbl_zasob_id;
  167. }
  168. function get_tbl_zasob_id() {
  169. return $this->_tbl_zasob_id;
  170. }
  171. function add_komorka($wsk) {
  172. $this->_komorki[$wsk->CW_ID] = $wsk;
  173. // TODO: add $this->_filters
  174. //$this->_filters[$wsk->] = $wsk-> value from field ... - TODO: evaluate variable from _HIST
  175. // TODO: add $this->_filters_readonly
  176. //$this->_filters_readonly[$wsk->] = $wsk-> value from field ... - TODO: evaluate variable from _HIST
  177. }
  178. public function get_wsk_ids() {
  179. return array_keys($this->_komorki);
  180. }
  181. function get_komorki() {
  182. return $this->_komorki;
  183. }
  184. function set_id_key($id_key) {
  185. $this->_id_key = $id_key;
  186. }
  187. function get_id_key() {
  188. return $this->_id_key;
  189. }
  190. function set_last_id_key($last_id_key) {
  191. $this->_last_id_key = $last_id_key;
  192. }
  193. function get_last_id_key() {
  194. return $this->_last_id_key;
  195. }
  196. /**
  197. * Set type of form: read, edit, create, search, note
  198. * edit - record ID, contains 'W' in CP_FORM_TREAT
  199. * create - contains 'C' in CP_FORM_TREAT
  200. * search - table
  201. * note - use notes table (TODO: CRM_LOG_NOTES(id, log_id, wsk_id, value))
  202. * TODO: check if type is allowed before set
  203. */
  204. function set_type($type) {
  205. switch ($type) {
  206. case 'create':
  207. if ($this->allow_create()) {
  208. $this->_type = $type;
  209. $this->clear_row();// clear $this->_v
  210. }
  211. break;
  212. case 'edit':
  213. if ($this->allow_write()) {
  214. $this->_type = $type;
  215. $this->clear_row();// clear $this->_v
  216. }
  217. break;
  218. case 'read':
  219. if ($this->allow_read()) {
  220. $this->_type = $type;
  221. $this->clear_row();// clear $this->_v
  222. }
  223. break;
  224. default:
  225. $this->_type = $type;
  226. }
  227. }
  228. /**
  229. * Get type of form: read, edit, create, search, note
  230. */
  231. function get_type() {
  232. if ($this->_type != '') {
  233. return $this->_type;
  234. }
  235. if ($this->_id_key > 0) {
  236. return 'edit';
  237. }
  238. }
  239. function add_hidden_field($k_field, $v_value) {
  240. $this->_hidden_fields[$k_field] = $v_value;
  241. }
  242. function render_hidden_fields() {
  243. $out = '';
  244. foreach ($this->_hidden_fields as $k_field => $v_value) {
  245. $out .= '<input type="hidden" name="' . $k_field . '" value="' . $v_value . '" />';
  246. }
  247. $out .= '<input type="hidden" name="' . "_log_id" . '" value="' . $this->get_log_id() . '" />';
  248. $out .= '<input type="hidden" name="' . "_tbl_zasob_id" . '" value="' . $this->_tbl_zasob_id . '" />';
  249. $out .= '<input type="hidden" name="' . "_log_wsk_id" . '" value="' . $this->_log_wsk_id . '" />';
  250. $out .= '<input type="hidden" name="' . "_type" . '" value="' . $this->get_type() . '" />';
  251. return $out;
  252. }
  253. /**
  254. * TODO: View form (edit, search, add new, add note).
  255. * Always show table with search filters. Add ID column if not set.
  256. * Row edit action sets $this->_id_key and change view to edit form.
  257. * TODO: change reauests to AJAX.
  258. */
  259. function form() {
  260. $out = '';
  261. $out .= '<form action="" method="post">';
  262. $out .= $this->render_hidden_fields();
  263. switch ($this->get_type()) {
  264. case 'edit':
  265. $out .= $this->render_edit();
  266. break;
  267. case 'read':
  268. $out .= $this->render_read();
  269. break;
  270. case 'create':
  271. $out .= $this->render_create();
  272. break;
  273. default:
  274. $out .= $this->render_table();
  275. }
  276. $out .= '</form>';
  277. return $out;
  278. }
  279. function render_create() {
  280. $out = '';
  281. $out .= '<table cellspacing="0" cellpadding="0" border="1" class="tbl-view">';
  282. $out .= '<thead>';
  283. $out .= '<tr><td>';
  284. $out .= '<p>' . "Nowy rekord do tabeli " . $this->get_table_name() . '</p>';
  285. $out .= '</td></tr>';
  286. $out .= '</thead>';
  287. $out .= '<tbody>';
  288. $t = 0;
  289. foreach ($this->get_komorki() as $k_wsk_id => $v_wsk) {
  290. $k_field = 'v-' . $k_wsk_id;// TODO: $v_wsk->CW_ID - ID wskaznik
  291. $out .= '<tr class="row-' . ($t = 1 - $t) . '"><td>';
  292. if ($v_wsk->OPIS) {
  293. $out .= $v_wsk->OPIS . ' ';
  294. }
  295. if ($v_wsk->OPIS_ZASOB) {
  296. $out .= $v_wsk->OPIS_ZASOB . ' ';
  297. }
  298. $out .= '<br />';
  299. $out .= '<label for="' . $k_field . '">' . $v_wsk->DESC . ': ' . '</label>';
  300. $out .= ' <em style="color:#666">(' . $v_wsk->CP_FORM_TREAT . ')</em> ';
  301. // $v_wsk->CP_FORM_TREAT => R,W,X,C
  302. // TODO: use App::field();
  303. $perms = explode(',', $v_wsk->CP_FORM_TREAT);
  304. if (in_array('C', $perms)) {
  305. $sql_type = $this->get_field_type($k_wsk_id);
  306. if (is_object($sql_type)) {
  307. $out .= App::field($k_field, $sql_type->Type, $this->get($k_wsk_id));
  308. } else {
  309. $out .= '<input type="text" name="' . $k_field . '" value="' . $this->get($k_wsk_id) . '" />';
  310. }
  311. } else if (in_array('R', $perms)) {
  312. $out .= '"' . $this->get($k_wsk_id) . '"';
  313. } else {
  314. $out .= '<span class="red">' . "brak dostêpu" . '</span>';
  315. }
  316. $out .= '</td></tr>';
  317. }
  318. $out .= '</tbody>';
  319. $out .= '<tr class="row-' . ($t = 1 - $t) . '"><td>';
  320. $out .= '<input type="submit" value="' . "Zapisz" . '" />';
  321. if ($this->allow_read()) {
  322. $out .= " lub ";
  323. $js = "this.form['_action'].value='edit_back';";
  324. $out .= '<input type="submit" onclick="' . $js . '" value="' . "Wróæ do wyszukiwania" . '" />';
  325. }
  326. $out .= '</td></tr>';
  327. $out .= '</table>';
  328. return $out;
  329. }
  330. function render_edit() {
  331. $out = '';
  332. $t = 0;
  333. $out .= '<table cellspacing="0" cellpadding="0" border="1" class="tbl-view">';
  334. $row = $this->fetch_row();
  335. if (!$row) {
  336. $out .= '<tr class="row-' . ($t = 1 - $t) . '"><td>';
  337. $out .= '<p>' . "Brak danych!" . '</p>';
  338. $out .= '</td></tr>';
  339. // TODO: check if _id_key isset - if yes show back link witch clear arg _id_key
  340. // optional show error message why cant edit this _id_key
  341. $out .= '</table>';
  342. return $out;
  343. }
  344. $this->set_row($row);
  345. $out .= '<thead>';
  346. $out .= '<tr><td>';
  347. $out .= '<input type="hidden" name="' . "_action" . '" value="' . "save_edit" . '" />';
  348. $out .= '<p>' . '<label for="f_ID">' . "Rekord ID: " . '</label>' . '<b>' . $row->f_ID . '</b>' . '</p>';
  349. $out .= '<input type="hidden" name="' . "_id_key" . '" value="' . $row->f_ID . '" />';
  350. $out .= '</td></tr>';
  351. $out .= '</thead>';
  352. $out .= '<tbody>';
  353. foreach ($this->get_komorki() as $k_wsk_id => $v_wsk) {
  354. $out .= '<tr class="row-' . ($t = 1 - $t) . '"><td>';
  355. $wsk_id = (substr($k_wsk_id, 0, 2) == 'f-')? substr($k_wsk_id, 2) : $k_wsk_id;
  356. $k_field = 'v-' . $wsk_id;
  357. $out .= '<label for="' . $k_field . '">' . $v_wsk->DESC . ': ' . '</label>';
  358. // TODO: $v_wsk->OPIS - opis z zasobu
  359. // TODO: $v_wsk->OPIS_ZASOB - opis ze wskaznika
  360. // TODO: use App::field();
  361. $out .= '<input type="text" name="' . $k_field . '" value="' . $this->get($k_wsk_id) . '" />';
  362. // TODO: $v_wsk->CW_ID - ID wskaznik
  363. // TODO: $v_wsk->CP_FORM_TREAT => R,W,X,C
  364. $out .= '<span style="display:none">' . "[" . $v_wsk->CP_FORM_TREAT . " / " . $v_wsk->CW_ID . "]" . '</span>';
  365. $out .= '</td></tr>';
  366. }
  367. $out .= '</tbody>';
  368. $out .= '<tr class="row-' . ($t = 1 - $t) . '"><td>';
  369. $out .= '<input type="submit" value="' . "Zapisz" . '" />';
  370. // TODO: if allow_read
  371. $out .= " lub ";
  372. $js = "this.form['_action'].value='edit_back';";
  373. $out .= '<input type="submit" onclick="' . $js . '" value="' . "Wróæ do wyszukiwania" . '" />';
  374. $out .= '</td></tr>';
  375. $out .= '</table>';
  376. return $out;
  377. }
  378. function render_read() {
  379. $out = '';
  380. $t = 0;
  381. $out .= '<table cellspacing="0" cellpadding="0" border="1" class="tbl-view">';
  382. $row = $this->fetch_row();
  383. if (!$row) {
  384. $out .= '<tr class="row-' . ($t = 1 - $t) . '"><td>';
  385. $out .= '<p>' . "Brak danych!" . '</p>';
  386. $out .= '</td></tr>';
  387. // TODO: check if _id_key isset - if yes show back link witch clear arg _id_key
  388. // optional show error message why cant edit this _id_key
  389. $out .= '</table>';
  390. return $out;
  391. }
  392. $this->set_row($row);
  393. $out .= '<thead>';
  394. $out .= '<tr><td>';
  395. $out .= '<input type="hidden" name="' . "_action" . '" value="' . "save_edit" . '" />';
  396. $out .= '<p>' . '<label for="f_ID">' . "Rekord ID: " . '</label>' . '<b>' . $row->f_ID . '</b>' . '</p>';
  397. $out .= '<input type="hidden" name="' . "_id_key" . '" value="' . $row->f_ID . '" />';
  398. $out .= '</td></tr>';
  399. $out .= '</thead>';
  400. $out .= '<tbody>';
  401. foreach ($this->get_komorki() as $k_wsk_id => $v_wsk) {
  402. $out .= '<tr class="row-' . ($t = 1 - $t) . '"><td>';
  403. $wsk_id = (substr($k_wsk_id, 0, 2) == 'f-')? substr($k_wsk_id, 2) : $k_wsk_id;
  404. $k_field = 'v-' . $wsk_id;
  405. $out .= '<label for="' . $k_field . '">' . $v_wsk->DESC . ': ' . '</label>';
  406. // TODO: $v_wsk->OPIS - opis z zasobu
  407. // TODO: $v_wsk->OPIS_ZASOB - opis ze wskaznika
  408. // TODO: use App::field();
  409. $out .= '<input type="text" name="' . $k_field . '" value="' . $this->get($k_wsk_id) . '" readonly="readonly" />';
  410. // TODO: $v_wsk->CW_ID - ID wskaznik
  411. // TODO: $v_wsk->CP_FORM_TREAT => R,W,X,C
  412. $out .= '<span style="display:none">' . "[" . $v_wsk->CP_FORM_TREAT . " / " . $v_wsk->CW_ID . "]" . '</span>';
  413. $out .= '</td></tr>';
  414. }
  415. $out .= '</tbody>';
  416. $out .= '<tr class="row-' . ($t = 1 - $t) . '"><td>';
  417. $js = "this.form['_action'].value='edit_back';";
  418. $out .= '<input type="submit" onclick="' . $js . '" value="' . "Wróæ do wyszukiwania" . '" />';
  419. $out .= '</td></tr>';
  420. $out .= '</table>';
  421. return $out;
  422. }
  423. function render_table() {
  424. $out = '';
  425. $cols = array();
  426. $cols['ID'] = array();
  427. $cols['ID']['ID'] = 'ID';
  428. $cols['ID']['DESC'] = 'ID';
  429. $cols['ID']['PERM'] = 'R';// TODO: check by PERMS in other fields
  430. $cols['ID']['OPIS'] = '';
  431. $cols['ID']['OPIS_ZASOB'] = '';
  432. foreach ($this->get_komorki() as $k_wsk_id => $v_wsk) {
  433. $cols[$k_wsk_id] = array();
  434. $cols[$k_wsk_id]['ID'] = $v_wsk->ID;
  435. $cols[$k_wsk_id]['DESC'] = $v_wsk->DESC;
  436. $cols[$k_wsk_id]['PERM'] = $v_wsk->CP_FORM_TREAT;
  437. $cols[$k_wsk_id]['OPIS'] = $v_wsk->OPIS;
  438. $cols[$k_wsk_id]['OPIS_ZASOB'] = $v_wsk->OPIS_ZASOB;
  439. }
  440. $rows = array();
  441. $rows_total = $this->fetch_rows_total();
  442. if ($rows_total > 0) {
  443. $rows = $this->fetch_rows();
  444. }
  445. $f_offset = V::get('f-_offset', '', $this->_nav_filters);
  446. Lib::loadClass('PageNav');
  447. $nav = new PageNav($rows_total, $f_offset);
  448. $nav->set_request_args(array('task'=>'PROCES_LOG', '_log_id'=>$this->get_log_id()));
  449. $nav->set_request_method_post();
  450. $nav->set_request_arg_page_nr_name('f-_offset');
  451. if ($this->_last_id_key) {
  452. $out .= '<p>' . "Ostatnio wybrany/edytowany rekord: " . '<b>' . $this->_last_id_key . '</b>' . '</p>';
  453. }
  454. $filters_set = false;
  455. foreach ($this->_filters as $k => $v) {
  456. // TODO: tylko filtry ktore moze ustawic user (wskaznik tylko R do danego filtra)
  457. if ($v != '%') {
  458. $filters_set = true;
  459. }
  460. }
  461. foreach ($this->_nav_filters_defaults as $k_fltr => $v_default) {
  462. $out .= '<input type="hidden" name="' . $k_fltr . '" value="' . $this->get_nav_filter($k_fltr) . '" />';
  463. }
  464. $out .= '<input type="hidden" name="' . "_action" . '" value="" />';
  465. $out .= '<input type="hidden" name="' . "_id_key" . '" value="" />';
  466. $out .= '<table cellspacing="0" cellpadding="0" border="1" class="tbl-view">';
  467. $out .= '<thead>';
  468. $out .= '<tr class="tbl-head">';
  469. $out .= '<th>';
  470. if ($this->allow_create()) {
  471. // change state to add new record
  472. $js = "this.form['_action'].value='create';this.form['_id_key'].value='';";
  473. $out .= " " . '<input type="image" onclick="' . $js . '" src="icon/add.gif" alt="add" title="' . "Dodaj nowy rekord" . '">';
  474. } else {
  475. $out .= '...';// header for actions
  476. }
  477. $out .= '</th>';
  478. foreach ($cols as $k_wsk_id => $v_field) {
  479. $out .= '<th>';
  480. $out .= str_replace('_' ,' ', $v_field['DESC']);
  481. if ($k_wsk_id == $this->get_nav_filter('f-_order_by')) {
  482. if ('asc' == $this->get_nav_filter('f-_order_desc')) {
  483. $js = "this.form['f-_order_by'].value='$k_wsk_id';this.form['f-_order_desc'].value='desc';";
  484. $out .= " " . '<input type="image" onclick="' . $js . '" src="icon/sort-desc.gif" alt="sort-desc" title="' . "Sortuj malej±co" . '">';
  485. } else {
  486. $js = "this.form['f-_order_by'].value='$k_wsk_id';this.form['f-_order_desc'].value='asc';";
  487. $out .= " " . '<input type="image" onclick="' . $js . '" src="icon/sort-asc.gif" alt="sort-desc" title="' . "Sortuj rosn±co" . '">';
  488. }
  489. } else {
  490. $js = "this.form['f-_order_by'].value='$k_wsk_id';this.form['f-_order_desc'].value='desc';";
  491. $out .= " " . '<input type="image" onclick="' . $js . '" src="icon/sort-desc.gif" alt="sort-desc" title="' . "Sortuj malej±co" . '">';
  492. }
  493. $out .= '</th>';
  494. }
  495. $out .= '</tr>';
  496. $out .= '<tr class="tbl-filters">';
  497. $out .= '<th>';
  498. $out .= '<input type="image" value="' . "Szukaj" . '" src="icon/search.png" title="' . "Szukaj" . '">';
  499. if ($filters_set) {
  500. $js = "return resetForm(this);";
  501. $out .= " " . '<input type="image" value="' . "Szukaj" . '" onclick="' . $js . '" src="icon/shutdown.gif" title="' . "Czy¶æ" . '">';
  502. }
  503. $out .= '</th>';
  504. foreach ($cols as $k_wsk_id => $v_field) {
  505. $filter_field_name = 'f-' . $k_wsk_id;
  506. $readonly = '';
  507. if (array_key_exists($k_wsk_id, $this->_filters_readonly)) {
  508. $readonly = 'readonly="readonly"';
  509. }
  510. $out .= '<th>';
  511. $out .= '<input type="text" name="' . $filter_field_name . '" value="' . $this->get_filter($filter_field_name) . '" ' . $readonly . ' class="i" />';
  512. $out .= '</th>';
  513. }
  514. $out .= '</tr>';
  515. $out .= '</thead>';
  516. $out .= '<tfoot>';
  517. $out .= '<tr>';
  518. $out .= '<td colspan="' . (count($cols) + 1) . '">';
  519. $out .= $nav->render();
  520. $out .= '</td>';
  521. $out .= '</tr>';
  522. $out .= '</tfoot>';
  523. $out .= '<tbody>';
  524. if (empty($rows)) {
  525. $out .= '<tr>';
  526. $out .= '<td colspan="' . (count($cols) + 1) . '">';
  527. $out .= '<p>' . '<b style="color:red">' . "Brak danych!" . '</b>';
  528. if ($rows_total > 0) {
  529. $out .= " Przejd¼ do " . '<a href="' . $nav->get_page_link(1) . '">' . "pierwszej strony" . '</a>';
  530. }
  531. $out .= '</p>';
  532. $out .= '</td>';
  533. $out .= '</tr>';
  534. } else {
  535. $t = 0;
  536. foreach ($rows as $k_row => $v_row) {
  537. $out .= '<tr class="row-' . ($t = 1 - $t) . '">';
  538. $out .= '<td>';
  539. $js = "this.form['_action'].value='';this.form['_id_key'].value='" .$v_row->f_ID . "';";
  540. $out .= '<input type="submit" value="' . "E" . '" onclick="' . $js . '" class="btn" />';
  541. $js = "this.form['_action'].value='select_id_key';this.form['_id_key'].value='" .$v_row->f_ID . "';";
  542. $out .= '<input type="submit" value="' . "W" . '" title="' . "Wybierz rekord " . $v_row->f_ID . '" onclick="' . $js . '" class="btn" />';
  543. $out .= '</td>';
  544. foreach ($cols as $k_field => $v_field) {
  545. $sql_field_name = 'f_' . $k_field;
  546. $out .= '<td>';
  547. $out .= $v_row->$sql_field_name;
  548. $out .= '</td>';
  549. }
  550. $out .= '</tr>';
  551. }
  552. }
  553. $out .= '</tbody>';
  554. $out .= '</table>';
  555. return $out;
  556. }
  557. function action_save_edit() {
  558. // TODO: validate input
  559. // save $this->_v into zasob source
  560. $db = DB::getDB();
  561. Lib::loadClass('BaseDao');
  562. Lib::loadClass('BaseModel');
  563. $dao = new BaseDao($db, $this->get_table_name());
  564. if (!$this->_id_key) {
  565. echo '<p>' . "Error: brak ID rekordu!" . '</p>';
  566. return null;
  567. }
  568. $model = $dao->get_by_id($this->_id_key);
  569. if (!$model) {
  570. echo '<p>' . "Error: rekord [".$this->_id_key."] nie istnieje!" . '</p>';
  571. return null;
  572. }
  573. foreach ($this->get_komorki() as $k_wsk_id => $v_wsk) {
  574. $model->set($v_wsk->DESC, $this->get($k_wsk_id));
  575. }
  576. $ret = $dao->save($model);
  577. if (!$ret) {
  578. return null;
  579. }
  580. return $dao;
  581. }
  582. function action_select_id_key() {
  583. // TODO: validate input
  584. // save $this->_v into zasob source
  585. $db = DB::getDB();
  586. Lib::loadClass('BaseDao');
  587. Lib::loadClass('BaseModel');
  588. $dao = new BaseDao($db, $this->get_table_name());
  589. if (!$this->_id_key) {
  590. echo '<p>' . "Error: brak ID rekordu!" . '</p>';
  591. return null;
  592. }
  593. $model = $dao->get_by_id($this->_id_key);
  594. if (!$model) {
  595. echo '<p>' . "Error: rekord [".$this->_id_key."] nie istnieje!" . '</p>';
  596. return null;
  597. }
  598. $this->set_type('read');
  599. return $model;
  600. }
  601. function get_table_name() {
  602. static $tbl_name;
  603. if (!$tbl_name) {
  604. $zasob_tbl = DB::get_by_id('CRM_LISTA_ZASOBOW', $this->_tbl_zasob_id);
  605. if (!$zasob_tbl) {
  606. $tbl_name = null;
  607. }
  608. $tbl_name = $zasob_tbl->DESC;
  609. }
  610. return $tbl_name;
  611. }
  612. function _sql_where() {
  613. $sql_where = '';
  614. $sql_where_and_arr = array();
  615. $db = DB::getDB();
  616. foreach ($this->_filters as $k_wsk_id => $v_val) {
  617. if ($k_wsk_id == 'ID') {
  618. $sql_field = 'ID';
  619. } else {
  620. if (!array_key_exists($k_wsk_id, $this->_komorki)) {
  621. continue;
  622. }
  623. $sql_field = $this->_komorki[$k_wsk_id]->DESC;
  624. }
  625. if ($v_val != '%') {
  626. $sql_where_and_arr []= "t.`" . $sql_field . "` like '" . $db->_($v_val) . "'";
  627. }
  628. }
  629. $sql_where = (!empty($sql_where_and_arr))? "where " . implode(" and ", $sql_where_and_arr) . "" : "";
  630. return $sql_where;
  631. }
  632. function fetch_rows_total() {
  633. $ret = 0;
  634. $db = DB::getDB();
  635. $sql_table = $this->get_table_name();
  636. $sql_where = $this->_sql_where();
  637. $sql = "select count(1) as cnt
  638. from `" . $sql_table . "` as t
  639. " . $sql_where . "
  640. ";
  641. $res = $db->query($sql);
  642. if ($r = $db->fetch($res)) {
  643. $ret = $r->cnt;
  644. }
  645. return $ret;
  646. }
  647. function fetch_rows() {
  648. $rows = array();
  649. $db = DB::getDB();
  650. $sql_select = array();// as f_ID, f_{$ID_ZASOB}
  651. $sql_table = $this->get_table_name();
  652. $sql_where = $this->_sql_where();
  653. $sql_order_by = 'order by t.`ID` DESC';// default
  654. $sql_limit = 'limit 10';// default
  655. $offset = V::get('f-_offset', 0, $this->_nav_filters);
  656. if ($offset) {
  657. $sql_limit .= ' offset ' . $offset;
  658. }
  659. $sql_order_by_wsk_id = $this->get_nav_filter('f-_order_by');
  660. if (array_key_exists($sql_order_by_wsk_id, $this->_komorki)) {
  661. $sql_order_by = 'order by t.`' . $this->_komorki[$this->get_nav_filter('f-_order_by')]->DESC . '` ' . $this->get_nav_filter('f-_order_desc');
  662. } else if ($sql_order_by_wsk_id == 'ID') {
  663. $sql_order_by = 'order by t.`ID` ' . $this->get_nav_filter('f-_order_desc');
  664. }
  665. $sql_select []= "t.`ID` as f_ID";
  666. foreach ($this->get_komorki() as $k_wsk_id => $v_wsk) {
  667. $sql_select []= "t.`" . $v_wsk->DESC . "` as f_" . $k_wsk_id;
  668. }
  669. $sql = "select " . implode("," , $sql_select) . "
  670. from `" . $sql_table . "` as t
  671. " . $sql_where . "
  672. " . $sql_order_by . "
  673. " . $sql_limit . "
  674. ";
  675. $res = $db->query($sql);
  676. while ($r = $db->fetch($res)) {
  677. $rows []= $r;
  678. }
  679. return $rows;
  680. }
  681. function fetch_row() {
  682. $row = null;
  683. if (!$this->_id_key) {
  684. return $row;
  685. }
  686. $db = DB::getDB();
  687. $sql_select = array();// as f_ID, f_{$ID_ZASOB}
  688. $sql_table = $this->get_table_name();
  689. $sql_where = "";//$this->_sql_where();
  690. $sql_where .= (($sql_where)? " and " : " where ") . " t.`ID`='" . $this->_id_key . "' ";
  691. $sql_select []= "t.`ID` as f_ID";
  692. foreach ($this->get_komorki() as $k_wsk_id => $v_wsk) {
  693. $sql_select []= "t.`" . $v_wsk->DESC . "` as f_" . $k_wsk_id;
  694. }
  695. $sql = "select " . implode("," , $sql_select) . "
  696. from `" . $sql_table . "` as t
  697. " . $sql_where . "
  698. ";
  699. $res = $db->query($sql);
  700. if ($r = $db->fetch($res)) {
  701. $row = $r;
  702. }
  703. return $row;
  704. }
  705. function get_field_type($wsk_id) {
  706. if (!array_key_exists($wsk_id, $this->_komorki)) {
  707. return null;
  708. }
  709. $wsk = $this->_komorki[$wsk_id];
  710. if (isset($wsk->_sql_type)) {
  711. return $wsk->_sql_type;
  712. }
  713. return null;
  714. }
  715. /**
  716. * @param $sql_types array( Field => array(
  717. * [Type] => int(10)
  718. * [Null] => NO
  719. * [Key] => PRI
  720. * [Default] =>
  721. * [Extra] => auto_increment )
  722. */
  723. function set_komorki_sql_types($sql_types) {
  724. foreach ($this->_komorki as $k_wsk_id => $v_wsk) {
  725. if (array_key_exists($v_wsk->DESC, $sql_types)) {
  726. $this->_komorki[$k_wsk_id]->_sql_type = $sql_types[$v_wsk->DESC];
  727. }
  728. }
  729. }
  730. /**
  731. * @returns array of data - $this object as simple array to store in session.
  732. */
  733. function to_array() {
  734. $arr = array();
  735. $arr['_tbl_zasob_id'] = $this->_tbl_zasob_id;
  736. $arr['_komorki'] = $this->_komorki;
  737. $arr['_last_id_key'] = $this->_last_id_key;
  738. return $arr;
  739. }
  740. /**
  741. * @param $state array of data - object state
  742. */
  743. function from_array($arr) {
  744. $this->_tbl_zasob_id = $arr['_tbl_zasob_id'];
  745. $this->_komorki = $arr['_komorki'];
  746. $this->_last_id_key = $arr['_last_id_key'];
  747. }
  748. /**
  749. * @returns array of data - current state as simple array to store in session.
  750. */
  751. function state_to_array() {
  752. $state = array();
  753. $state['_v'] = $this->_v;
  754. $state['_filters'] = $this->_filters;
  755. $state['_nav_filters'] = $this->_nav_filters;
  756. $state['_id_key'] = $this->_id_key;
  757. return $state;
  758. }
  759. /**
  760. * @param $state array of data - object state
  761. */
  762. function state_from_array($state) {
  763. if (!empty($state['_v'])) $this->_v = $state['_v'];
  764. if (!empty($state['_filters'])) $this->_filters = $state['_filters'];
  765. if (!empty($state['_nav_filters'])) $this->_nav_filters = $state['_nav_filters'];
  766. //if (!empty($state['_id_key'])) $this->_id_key = $state['_id_key'];
  767. }
  768. /**
  769. * Save state in session.
  770. * _filters - array of table filter values
  771. * _nav_filters - array of table nav filter values
  772. * _v - array of form field values
  773. * _id_key - Record ID
  774. */
  775. function save_state() {
  776. if (!array_key_exists('PROCES_LOG_STATE', $_SESSION)) {
  777. $_SESSION['PROCES_LOG_STATE'] = array();
  778. $_SESSION['PROCES_LOG_STATE']['_change_ts'] = array();// used in garbage collection
  779. }
  780. $state = $this->state_to_array();
  781. $ses_key = $this->_log_id . '-' . $this->_log_wsk_id;
  782. $_SESSION['PROCES_LOG_STATE'][$ses_key] = $state;
  783. if (array_key_exists($ses_key, $_SESSION['PROCES_LOG_STATE']['_change_ts'])) {
  784. $_SESSION['PROCES_LOG_STATE']['_change_ts'][$ses_key] = time();
  785. } else {
  786. $_SESSION['PROCES_LOG_STATE']['_change_ts'][$ses_key] = time();
  787. }
  788. $this->_garbage_collection();
  789. }
  790. /**
  791. * Read state from session.
  792. * @see save_state
  793. */
  794. function read_state() {
  795. $ses_key = $this->_log_id . '-' . $this->_log_wsk_id;
  796. if (isset($_SESSION['PROCES_LOG_STATE'][$ses_key])) {
  797. $this->state_from_array($_SESSION['PROCES_LOG_STATE'][$ses_key]);
  798. }
  799. }
  800. /**
  801. * Garbage collection (GC) is a form of automatic memory management.
  802. */
  803. function _garbage_collection() {
  804. $max_cache_count = 10;
  805. if (count($_SESSION['PROCES_LOG_STATE']) <= $max_cache_count) {
  806. return;
  807. }
  808. // TODO: sort table _change_ts by value with keys - [$ses_key] = time();
  809. // TODO: remove row from ses by _change_ts
  810. }
  811. function get_step_wsk_desc() {
  812. $ret = '';
  813. if (!empty($this->_komorki)) {
  814. $first_wsk = reset($this->_komorki);
  815. $ret = '(' . $first_wsk->DESC . ') ';
  816. if ($first_wsk->OPIS_ZASOB) {
  817. $ret .= $first_wsk->OPIS_ZASOB;
  818. } else {
  819. $ret .= $first_wsk->OPIS;
  820. }
  821. }
  822. return $ret;
  823. }
  824. function allow_create() {
  825. foreach ($this->_komorki as $k_wsk_id => $v_komorka) {
  826. // CP_FORM_TREAT => R,W,X,C
  827. if (in_array('C', explode(',', $v_komorka->CP_FORM_TREAT))) {
  828. return true;
  829. }
  830. }
  831. return false;
  832. }
  833. function allow_write() {
  834. foreach ($this->_komorki as $k_wsk_id => $v_komorka) {
  835. // CP_FORM_TREAT => R,W,X,C
  836. if (in_array('W', explode(',', $v_komorka->CP_FORM_TREAT))) {
  837. return true;
  838. }
  839. }
  840. return false;
  841. }
  842. function allow_read() {
  843. foreach ($this->_komorki as $k_wsk_id => $v_komorka) {
  844. // CP_FORM_TREAT => R,W,X,C
  845. if (in_array('R', explode(',', $v_komorka->CP_FORM_TREAT))) {
  846. return true;
  847. }
  848. }
  849. return false;
  850. }
  851. }