PageRenderTime 84ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/php/AjaxTableEditor.php

https://bitbucket.org/nyfer/realestateapp
PHP | 2972 lines | 2737 code | 132 blank | 103 comment | 605 complexity | db7ac00bf0d4f59d5840c892f33e570e MD5 | raw file
  1. <?php
  2. /*
  3. * Mysql Ajax Table Editor
  4. *
  5. * Copyright (c) 2008 Chris Kitchen <info@mysqlajaxtableeditor.com>
  6. * All rights reserved.
  7. *
  8. * See COPYING file for license information.
  9. *
  10. * Download the latest version from
  11. * http://www.mysqlajaxtableeditor.com
  12. */
  13. class AjaxTableEditor
  14. {
  15. var $action;
  16. var $retArr = array();
  17. var $warnings = array();
  18. var $html;
  19. var $numResults;
  20. var $inputIdPrefix;
  21. var $start;
  22. var $display;
  23. var $orderByColumn;
  24. var $ascOrDesc;
  25. var $searchString;
  26. var $selectClause;
  27. var $joinClause;
  28. var $whereClause;
  29. var $dbName;
  30. var $tableColumns = array();
  31. var $tableName;
  32. var $varPrefix;
  33. var $primaryKeyCol;
  34. var $userColumns = array();
  35. var $errorFun;
  36. var $permissions;
  37. var $tableTitle;
  38. var $valError;
  39. var $addRowTitle;
  40. var $editRowTitle;
  41. var $editMultTitle;
  42. var $viewRowTitle;
  43. var $showHideTitle;
  44. var $orderColTitle;
  45. var $searchType;
  46. var $numAdvSearches;
  47. var $opts;
  48. var $matchAll;
  49. var $advSearches = array();
  50. var $defNumAdvSearches;
  51. var $afterAddFun;
  52. var $afterEditFun;
  53. var $afterCopyFun;
  54. var $afterDeleteFun;
  55. var $extraOrderByInfo;
  56. var $information = array();
  57. var $userButtons = array();
  58. var $userIcons = array();
  59. var $tableInfo;
  60. var $oddRowColor;
  61. var $evenRowColor;
  62. var $userDefDisplay;
  63. var $displayNumInc;
  64. var $dbAndTable;
  65. var $maxDispNum;
  66. var $beginTime;
  67. var $viewQuery;
  68. var $recordInfo;
  69. var $replaceWithId;
  70. var $highlightHash;
  71. var $useHightlight;
  72. var $userActions = array();
  73. var $reqMarker;
  74. var $viewTableInfo;
  75. var $editTableInfo;
  76. var $addTableInfo;
  77. var $showHideTableInfo;
  78. var $addScreenFun;
  79. var $editScreenFun;
  80. var $viewScreenFun;
  81. var $tableScreenFun;
  82. var $showHideScreenFun;
  83. var $orderColScreenFun;
  84. var $valErrorStyles;
  85. var $noValErrorStyles;
  86. var $iconColPosition;
  87. var $mateUserId;
  88. var $pagePercent;
  89. var $pageRange;
  90. var $pageBegin;
  91. var $pageEnd;
  92. var $showAll;
  93. var $queryLineBreak;
  94. var $viewHtmlFun;
  95. var $handleHackingFun;
  96. var $removeIcons;
  97. var $langVars;
  98. var $iconTitle;
  99. var $sqlFilters;
  100. var $allowEditMult;
  101. var $defaultJsCalFormat;
  102. var $disableMultCbFun;
  103. var $filterSearches;
  104. var $filterColId;
  105. var $asColumnInfo;
  106. var $defaultUploadMaxSize;
  107. var $paginationLinks;
  108. function setDefaults()
  109. {
  110. $this->dbAndTable = !empty($this->dbName) ? $this->dbName.'.'.$this->tableName : $this->tableName;
  111. $this->warnings = array();
  112. $this->varPrefix = $this->primaryKeyCol.'-'.$this->tableName;
  113. $this->start = 0;
  114. $this->displayNum = isset($_SESSION[$this->varPrefix.'displayNum']) ? $_SESSION[$this->varPrefix.'displayNum'] : 100;
  115. $this->ascOrDesc = 'asc';
  116. $this->orderByColumn = $this->primaryKeyCol;
  117. $this->tableTitle = mb_convert_case(str_replace('_',' ',$this->tableName),MB_CASE_TITLE, "UTF-8");
  118. $this->valError = false;
  119. $this->addRowTitle = $this->langVars->ttlAddRow;
  120. $this->editRowTitle = $this->langVars->ttlEditRow;
  121. $this->editMultTitle = $this->langVars->ttlEditMult;
  122. $this->viewRowTitle = $this->langVars->ttlViewRow;
  123. $this->executeRowTitle = $this->langVars->ttlExecuteRow; //my add !
  124. $this->defNumAdvSearches = 3;
  125. $this->numAdvSearches = isset($_SESSION[$this->varPrefix.'numAdvSearches']) ? $_SESSION[$this->varPrefix.'numAdvSearches'] : $this->defNumAdvSearches;
  126. $this->opts = array('like' => $this->langVars->optLike, 'not like' => $this->langVars->optNotLike, '=' => $this->langVars->optEq, '<>' => $this->langVars->optNotEq, '>' => $this->langVars->optGreat, '<' => $this->langVars->optLess, '>=' => $this->langVars->optGreatEq, '<=' => $this->langVars->optLessEq);
  127. $this->searchType = isset($_SESSION[$this->varPrefix.'searchType']) ? $_SESSION[$this->varPrefix.'searchType'] : 'quick';
  128. $this->matchAll = isset($_SESSION[$this->varPrefix.'matchAll']) ? $_SESSION[$this->varPrefix.'matchAll'] : true;
  129. $this->advSearches = isset($_SESSION[$this->varPrefix.'advSearches']) ? $_SESSION[$this->varPrefix.'advSearches'] : array();
  130. $this->filterSearches = isset($_SESSION[$this->varPrefix.'filterSearches']) ? $_SESSION[$this->varPrefix.'filterSearches'] : array();
  131. $this->oddRowColor = '#FFFFFF';
  132. $this->evenRowColor = '#EDEDED';
  133. $this->userDefDisplay = false;
  134. $this->displayNumInc = 5;
  135. $this->maxDispNum = 200;
  136. $this->inputIdPrefix = '';
  137. $this->viewQuery = false;
  138. $this->recordInfo = true;
  139. $this->replaceWithId = '#primaryColValue#';
  140. $this->highlightHash = 'background-color: #FFF547';
  141. $this->useHighlight = true;
  142. $this->userActions = array();
  143. $this->reqMarker = '*';
  144. $this->viewTableInfo = 'class="mateTable"';
  145. $this->editTableInfo = 'class="mateTable"';
  146. $this->addTableInfo = 'class="mateTable"';
  147. $this->showHideTableInfo = 'class="mateTable"';
  148. $this->valErrorStyles = 'color: #f90d0d;';
  149. $this->noValErrorStyles = 'color: #333;';
  150. $this->iconColPosition = 'last';
  151. $this->showHideTitle = $this->langVars->ttlShowHide;
  152. $this->orderColTitle = $this->langVars->ttlOrderCols;
  153. $this->pagePercent = 20;
  154. $this->pageRange = 10;
  155. $this->pageBegin = 5;
  156. $this->pageEnd = 5;
  157. $this->showAll = 200;
  158. $this->queryLineBreak = "\n";
  159. $this->allowEditMult = true;
  160. $this->defaultJsCalFormat = '%d %B %Y';
  161. $this->defaultUploadMaxSize = 47185920;
  162. $this->paginationLinks = false;
  163. //$this->$userIcons = array('format_fun' => array(&$this,'disablelinks'));
  164. }
  165. function setConfig($var,$val)
  166. {
  167. if($var == 'varPrefix')
  168. {
  169. // If the variable prefix changed, update variables with session values
  170. $varPrefix = $val;
  171. foreach($_SESSION as $sessKey => $sessValue)
  172. {
  173. if(substr($sessKey,0,strlen($varPrefix)) == $varPrefix)
  174. {
  175. $varName = str_replace($varPrefix,'',$sessKey);
  176. $this->{$varName} = $sessValue;
  177. }
  178. }
  179. }
  180. if(!isset($_SESSION[$this->varPrefix.$var]))
  181. {
  182. $this->{$var} = $val;
  183. }
  184. }
  185. function doDefault()
  186. {
  187. if(isset($this->userActions[$this->action]) && is_callable($this->userActions[$this->action]))
  188. {
  189. call_user_func($this->userActions[$this->action],$this->info);
  190. }
  191. else
  192. {
  193. $this->warnings[] = sprintf($this->langVars->errNoAction,$this->action);
  194. }
  195. }
  196. function displayInformation()
  197. {
  198. if(!empty($this->information))
  199. {
  200. $this->retArr[] = array('layer_id' => 'information', 'where' => 'innerHTML', 'value' => implode('<br /><br />',$this->information));
  201. }
  202. }
  203. function displayWarnings()
  204. {
  205. if(!empty($this->warnings))
  206. {
  207. $this->retArr[] = array('where' => 'javascript', 'value' => 'alert(\''.implode('\n',$this->warnings).'\');');
  208. }
  209. }
  210. function doQuery($query)
  211. {
  212. $result = mysql_query($query);
  213. if (!$result)
  214. {
  215. $message = '<br /><br />'.$this->langVars->errQuery.' <br />'.$query.'<br /><br /> '.$this->langVars->errMysql.'<br /> '.mysql_error();
  216. call_user_func($this->errorFun,$message,__FILE__,__LINE__);
  217. }
  218. return $result;
  219. }
  220. function escapeData($data)
  221. {
  222. if (ini_get('magic_quotes_gpc'))
  223. {
  224. $data = stripslashes($data);
  225. }
  226. if(isset($this->dbc))
  227. {
  228. return mysql_real_escape_string (trim ($data), $this->dbc);
  229. }
  230. else
  231. {
  232. return mysql_real_escape_string (trim ($data));
  233. }
  234. }
  235. function startTimer()
  236. {
  237. $time = microtime();
  238. $time = explode(' ', $time);
  239. $time = $time[1] + $time[0];
  240. $this->beginTime = $time;
  241. }
  242. function endTimer()
  243. {
  244. $time = microtime();
  245. $time = explode(' ', $time);
  246. $time = $time[1] + $time[0];
  247. $endtime = $time;
  248. $totaltime = ($endtime - $this->beginTime);
  249. $this->information[] = $totaltime;
  250. }
  251. function main($action,$info = '')
  252. {
  253. //$this->startSession();
  254. //$this->mysqlConnect();
  255. //$this->checkLoginInfo(array('Admin'));
  256. //$this->startTimer();
  257. $this->action = $action;
  258. $this->info = $info;
  259. $this->handleFlow();
  260. //$this->endTimer();
  261. $this->displayInformation();
  262. $this->displayWarnings();
  263. }
  264. function handleFlow()
  265. {
  266. switch ($this->action)
  267. {
  268. case 'clear_filters':
  269. $_SESSION[$this->varPrefix.'filterSearches'] = array();
  270. $this->filterSearches = array();
  271. $this->updateHtml();
  272. break;
  273. case 'handle_filter_search':
  274. $this->handleFilterSearch();
  275. break;
  276. case 'update_mult_rows':
  277. $this->updateMultRows();
  278. break;
  279. case 'edit_mult_rows':
  280. $this->editMultRows();
  281. break;
  282. case 'reset_column_order':
  283. $this->resetColumnOrder();
  284. break;
  285. case 'update_column_order':
  286. $this->updateColumnOrder();
  287. break;
  288. case 'order_columns_screen':
  289. $this->orderColumnsScreen();
  290. break;
  291. case 'show_column':
  292. $this->showColumn();
  293. break;
  294. case 'hide_column':
  295. $this->hideColumn();
  296. break;
  297. case 'show_hide_columns':
  298. $this->showHideColumns();
  299. break;
  300. case 'view_row':
  301. $this->viewRow();
  302. break;
  303. case 'update_row':
  304. $this->updateRow();
  305. break;
  306. case 'cancel_edit_row':
  307. $this->updateHtml();
  308. break;
  309. case 'edit_row':
  310. $this->editRow();
  311. break;
  312. case 'delete_row':
  313. $this->deleteRow();
  314. break;
  315. case 'delete_mult_rows':
  316. $this->deleteMultRows();
  317. break;
  318. case 'update_html':
  319. $this->updateHtml();
  320. break;
  321. case 'handle_search':
  322. $_SESSION[$this->varPrefix.'searchType'] = 'quick';
  323. $this->searchType = 'quick';
  324. $_SESSION[$this->varPrefix.'ss'] = $this->info;
  325. $_SESSION[$this->varPrefix.'start'] = 0;
  326. $this->updateHtml();
  327. //$this->retArr[] = array('where' => 'javascript', 'value' => "resetScrollTop();");
  328. break;
  329. case 'page_num_changed':
  330. $_SESSION[$this->varPrefix.'start'] = $this->escapeData($this->info);
  331. $this->updateHtml();
  332. break;
  333. case 'display_num_changed':
  334. $this->displayNum = $this->escapeData($this->info);
  335. $_SESSION[$this->varPrefix.'displayNum'] = $this->displayNum;
  336. $_SESSION[$this->varPrefix.'start'] = 0;
  337. $this->updateHtml();
  338. $this->retArr[] = array('where' => 'javascript', 'value' => "resetScrollTop();");
  339. break;
  340. case 'order_by_changed':
  341. $_SESSION[$this->varPrefix.'obc'] = $this->escapeData($this->info[0]);
  342. $_SESSION[$this->varPrefix.'aod'] = $this->escapeData($this->info[1]);
  343. $this->displayTable();
  344. break;
  345. case 'add_row':
  346. $this->addRow();
  347. break;
  348. case 'insert_row':
  349. $this->insertRow();
  350. break;
  351. case 'show_advanced_search':
  352. $_SESSION[$this->varPrefix.'numAdvSearches'] = $this->defNumAdvSearches;
  353. $this->numAdvSearches = $this->defNumAdvSearches;
  354. $_SESSION[$this->varPrefix.'ss'] = '';
  355. $this->searchString = '';
  356. $_SESSION[$this->varPrefix.'searchType'] = 'advanced';
  357. $this->searchType = 'advanced';
  358. $this->updateHtml();
  359. break;
  360. case 'show_quick_search':
  361. $_SESSION[$this->varPrefix.'advSearches'] = array();
  362. $this->advSearches = array();
  363. $_SESSION[$this->varPrefix.'searchType'] = 'quick';
  364. $this->searchType = 'quick';
  365. $this->updateHtml();
  366. break;
  367. case 'advanced_search':
  368. $_SESSION[$this->varPrefix.'searchType'] = 'advanced';
  369. $this->searchType = 'advanced';
  370. $this->advancedSearch();
  371. $this->updateHtml();
  372. //$this->retArr[] = array('where' => 'javascript', 'value' => "resetScrollTop();");
  373. break;
  374. case 'match_all':
  375. $_SESSION[$this->varPrefix.'matchAll'] = true;
  376. break;
  377. case 'match_any':
  378. $_SESSION[$this->varPrefix.'matchAll'] = false;
  379. break;
  380. case 'clear_adv_search':
  381. $_SESSION[$this->varPrefix.'matchAll'] = true;
  382. $this->matchAll = true;
  383. $_SESSION[$this->varPrefix.'numAdvSearches'] = $this->defNumAdvSearches;
  384. $this->numAdvSearches = $this->defNumAdvSearches;
  385. $_SESSION[$this->varPrefix.'advSearches'] = array();
  386. $this->advSearches = array();
  387. $this->updateHtml();
  388. break;
  389. case 'add_adv_search':
  390. $_SESSION[$this->varPrefix.'numAdvSearches']++;
  391. $this->numAdvSearches++;
  392. $this->updateHtml();
  393. break;
  394. case 'copy_mult_rows':
  395. $this->copyMultRows();
  396. $this->updateHtml();
  397. break;
  398. case 'copy_row':
  399. $this->copyRow();
  400. $this->updateHtml();
  401. break;
  402. case 'user_icon_clicked':
  403. $this->userIconClicked();
  404. break;
  405. case 'user_button_clicked':
  406. $this->userButtonClicked();
  407. break;
  408. // my add !
  409. case 'execute_transfer':
  410. $this->execute_transfer();
  411. break;
  412. default :
  413. $this->doDefault();
  414. }
  415. }
  416. function handleFilterSearch()
  417. {
  418. foreach($this->info->filters as $filterInfo)
  419. {
  420. if(strlen($filterInfo->filterStr) > 0)
  421. {
  422. $_SESSION[$this->varPrefix.'filterSearches'][$filterInfo->filterCol] = $filterInfo->filterStr;
  423. }
  424. else if(isset($_SESSION[$this->varPrefix.'filterSearches'][$filterInfo->filterCol]))
  425. {
  426. unset($_SESSION[$this->varPrefix.'filterSearches'][$filterInfo->filterCol]);
  427. }
  428. }
  429. $this->filterSearches = isset($_SESSION[$this->varPrefix.'filterSearches']) ? $_SESSION[$this->varPrefix.'filterSearches'] : array();
  430. $_SESSION[$this->varPrefix.'start'] = 0;
  431. $this->updateHtml();
  432. if(isset($this->info->currentFilterId) && strlen($this->info->currentFilterId) > 0)
  433. {
  434. $this->retArr[] = array('where' => 'javascript', 'value' => '$("'.$this->info->currentFilterId.'").focus();');
  435. }
  436. }
  437. function resetColumnOrder()
  438. {
  439. $query = "update mate_columns set order_num = '0' where mate_var_prefix = '".$this->escapeData($this->varPrefix)."'";
  440. $result = $this->doQuery($query);
  441. if($result)
  442. {
  443. $this->orderColumnsScreen();
  444. }
  445. }
  446. function unsetColSessVars($column)
  447. {
  448. if($this->searchType == 'advanced' && isset($_SESSION[$this->varPrefix.'advSearches']))
  449. {
  450. foreach($_SESSION[$this->varPrefix.'advSearches'] as $key => $searchInfo)
  451. {
  452. if(isset($searchInfo['cols']) && $searchInfo['cols'] == $column)
  453. {
  454. $_SESSION[$this->varPrefix.'advSearches'][$key] = array();
  455. }
  456. }
  457. }
  458. if(isset($_SESSION[$this->varPrefix.'filterSearches'][$column]))
  459. {
  460. unset($_SESSION[$this->varPrefix.'filterSearches'][$column]);
  461. }
  462. if(isset($_SESSION[$this->varPrefix.'obc']) && $_SESSION[$this->varPrefix.'obc'] == $column)
  463. {
  464. unset($_SESSION[$this->varPrefix.'obc']);
  465. }
  466. }
  467. function unsetHiddenColumns()
  468. {
  469. if(stristr($this->permissions,'H') && $this->setMateUserId())
  470. {
  471. foreach($this->tableColumns as $col => $info)
  472. {
  473. if(stristr($info['perms'],'H') && $col != $this->primaryKeyCol)
  474. {
  475. $hidden = isset($info['hidden']) ? $info['hidden'] : false;
  476. if($this->columnIsHidden($col,$hidden))
  477. {
  478. unset($this->tableColumns[$col]);
  479. }
  480. }
  481. }
  482. }
  483. }
  484. function columnIsHidden($column,$hidden = false)
  485. {
  486. $rv = false;
  487. $query = "select id, hidden from mate_columns where mate_user_id = '$this->mateUserId' and mate_var_prefix = '$this->varPrefix' and mate_column = '$column'";
  488. $result = $this->doQuery($query);
  489. if($row = mysql_fetch_assoc($result))
  490. {
  491. if($row['hidden'] == 'Yes')
  492. {
  493. $rv = true;
  494. }
  495. }
  496. else if($hidden)
  497. {
  498. $rv = true;
  499. }
  500. return $rv;
  501. }
  502. function showColumn()
  503. {
  504. if($this->setMateUserId())
  505. {
  506. $column = $this->escapeData($this->info);
  507. $query = "select id from mate_columns where mate_user_id = '$this->mateUserId' and mate_var_prefix = '$this->varPrefix' and mate_column = '$column'";
  508. $result = $this->doQuery($query);
  509. if($row = mysql_fetch_assoc($result))
  510. {
  511. $mateColId = $row['id'];
  512. $query = "update mate_columns set hidden = 'No' where id = '$mateColId'";
  513. $result = $this->doQuery($query);
  514. }
  515. else
  516. {
  517. $query = "insert into mate_columns set mate_user_id = '$this->mateUserId', mate_var_prefix = '$this->varPrefix', mate_column = '$column', hidden = 'No', date_updated = NOW()";
  518. $result = $this->doQuery($query);
  519. }
  520. }
  521. }
  522. function hideColumn()
  523. {
  524. if($this->setMateUserId())
  525. {
  526. $column = $this->escapeData($this->info);
  527. if(isset($this->tableColumns[$column]['perms']) && stristr($this->tableColumns[$column]['perms'],'H'))
  528. {
  529. $query = "select id from mate_columns where mate_user_id = '$this->mateUserId' and mate_var_prefix = '$this->varPrefix' and mate_column = '$column'";
  530. $result = $this->doQuery($query);
  531. if($row = mysql_fetch_assoc($result))
  532. {
  533. $mateColId = $row['id'];
  534. $query = "update mate_columns set hidden = 'Yes' where id = '$mateColId'";
  535. $result = $this->doQuery($query);
  536. }
  537. else
  538. {
  539. $query = "insert into mate_columns set mate_user_id = '$this->mateUserId', mate_var_prefix = '$this->varPrefix', mate_column = '$column', hidden = 'Yes', date_updated = NOW()";
  540. $result = $this->doQuery($query);
  541. }
  542. $this->unsetColSessVars($column);
  543. }
  544. }
  545. }
  546. function setMateUserId()
  547. {
  548. if(strlen($this->mateUserId) > 0)
  549. {
  550. return true;
  551. }
  552. else if(isset($_COOKIE['mate_user_id']) && strlen($_COOKIE['mate_user_id']) > 0)
  553. {
  554. $this->mateUserId = $_COOKIE['mate_user_id'];
  555. return true;
  556. }
  557. else
  558. {
  559. $this->mateUserId = md5(uniqid(rand(),1));
  560. if(setcookie('mate_user_id',$this->mateUserId,time() + 60*60*24*7*365*2))
  561. {
  562. return true;
  563. }
  564. else
  565. {
  566. $this->warnings[] = 'Your browser must accept cookies in order to show/hide columns.';
  567. return false;
  568. }
  569. }
  570. }
  571. function showHideColumns()
  572. {
  573. if(stristr($this->permissions,'H'))
  574. {
  575. if($this->setMateUserId())
  576. {
  577. $html = '<table '.$this->showHideTableInfo.'><tr style="font-weight: bold;"><td>'.$this->langVars->ttlColumn.'</td><td>'.$this->langVars->ttlCheckBox.'</td></tr>';
  578. foreach($this->tableColumns as $col => $info)
  579. {
  580. if(stristr($info['perms'],'H') && $col != $this->primaryKeyCol)
  581. {
  582. $checked = 'checked="checked"';
  583. $hidden = isset($info['hidden']) ? $info['hidden'] : false;
  584. if($this->columnIsHidden($col,$hidden))
  585. {
  586. $checked = '';
  587. }
  588. $html .= '<tr><td><label for="'.$col.'_hide_cb">'.$info['display_text'].'</label>:</td><td><input type="checkbox" id="'.$col.'_hide_cb" onchange="showHideColumn(this,\''.$col.'\');" '.$checked.' /></tr>';
  589. }
  590. }
  591. $html .= '</table>';
  592. $this->retArr[] = array('layer_id' => 'titleLayer', 'where' => 'innerHTML', 'value' => $this->showHideTitle);
  593. $this->retArr[] = array('layer_id' => 'recordLayer', 'where' => 'innerHTML', 'value' => '');
  594. $this->retArr[] = array('layer_id' => 'filterLayer', 'where' => 'innerHTML', 'value' => '');
  595. $this->retArr[] = array('layer_id' => 'searchButtonsLayer', 'where' => 'innerHTML', 'value' => '<button onclick="toAjaxTableEditor(\'update_html\',\'\');">'.$this->langVars->btnBack.'</button>');
  596. $this->retArr[] = array('layer_id' => 'tableLayer', 'where' => 'innerHTML', 'value' => $html);
  597. //$this->retArr[] = array('where' => 'javascript', 'value' => "resetScrollTop();");
  598. if(is_callable($this->showHideScreenFun))
  599. {
  600. call_user_func($this->showHideScreenFun);
  601. }
  602. }
  603. }
  604. }
  605. function orderColumnsScreen()
  606. {
  607. if(stristr($this->permissions,'O'))
  608. {
  609. $html = '';
  610. $this->unsetHiddenColumns();
  611. $this->setColumnOrder();
  612. if($this->setMateUserId())
  613. {
  614. $html .= '<ul id="columnOrderList">';
  615. foreach($this->tableColumns as $col => $info)
  616. {
  617. if(stristr($info['perms'],'O'))
  618. {
  619. $html .= '<li id="columnOrderList_'.str_replace('_','-.-UNDERSCORE-.-',$col).'" class="orderColumnClass">'.$info['display_text'].'</li>';
  620. }
  621. }
  622. $html .= '</ul>';
  623. $this->retArr[] = array('layer_id' => 'titleLayer', 'where' => 'innerHTML', 'value' => $this->orderColTitle);
  624. $this->retArr[] = array('layer_id' => 'recordLayer', 'where' => 'innerHTML', 'value' => '');
  625. $this->retArr[] = array('layer_id' => 'filterLayer', 'where' => 'innerHTML', 'value' => '');
  626. $this->retArr[] = array('layer_id' => 'searchButtonsLayer', 'where' => 'innerHTML', 'value' => '<button onclick="toAjaxTableEditor(\'reset_column_order\',\'\');">'.$this->langVars->btnReset.'</button> <button onclick="toAjaxTableEditor(\'update_html\',\'\');">'.$this->langVars->btnBack.'</button>');
  627. $this->retArr[] = array('layer_id' => 'tableLayer', 'where' => 'innerHTML', 'value' => $html);
  628. //$this->retArr[] = array('where' => 'javascript', 'value' => "resetScrollTop();");
  629. $javascript = 'Sortable.create("columnOrderList",{dropOnEmpty:false,constraint:false,onUpdate:function(element){var info = Sortable.serialize(element.id); toAjaxTableEditor("update_column_order",info); } });';
  630. $this->retArr[] = array('where' => 'javascript', 'value' => $javascript);
  631. if(is_callable($this->orderColScreenFun))
  632. {
  633. call_user_func($this->orderColScreenFun);
  634. }
  635. }
  636. }
  637. }
  638. function updateColumnOrder()
  639. {
  640. if($this->setMateUserId())
  641. {
  642. if(stristr($this->permissions,'O'))
  643. {
  644. parse_str($this->info);
  645. $orderNum = 1;
  646. foreach($columnOrderList as $col)
  647. {
  648. $col = str_replace('-.-UNDERSCORE-.-','_',$col);
  649. if(isset($this->tableColumns[$col]['perms']) && stristr($this->tableColumns[$col]['perms'],'O'))
  650. {
  651. $query = "select id from mate_columns where mate_user_id = '$this->mateUserId' and mate_var_prefix = '$this->varPrefix' and mate_column = '$col'";
  652. $result = $this->doQuery($query);
  653. if($row = mysql_fetch_assoc($result))
  654. {
  655. $mateColId = $row['id'];
  656. $query = "update mate_columns set order_num = '$orderNum' where id = '$mateColId'";
  657. $result = $this->doQuery($query);
  658. }
  659. else
  660. {
  661. $query = "insert into mate_columns set mate_user_id = '$this->mateUserId', mate_var_prefix = '$this->varPrefix', mate_column = '$col', order_num = '$orderNum', hidden = 'No', date_updated = NOW()";
  662. $result = $this->doQuery($query);
  663. }
  664. $orderNum++;
  665. }
  666. }
  667. }
  668. }
  669. }
  670. function setColumnOrder()
  671. {
  672. if(stristr($this->permissions,'O'))
  673. {
  674. if($this->setMateUserId())
  675. {
  676. $newTableColumns = array();
  677. $query = "select mate_column from mate_columns where mate_user_id = '$this->mateUserId' and mate_var_prefix = '$this->varPrefix' and hidden = 'No' and order_num > 0 order by order_num asc";
  678. $result = $this->doQuery($query);
  679. while($row = mysql_fetch_assoc($result))
  680. {
  681. if(isset($this->tableColumns[$row['mate_column']]) && stristr($this->tableColumns[$row['mate_column']]['perms'],'O'))
  682. {
  683. $newTableColumns[$row['mate_column']] = $this->tableColumns[$row['mate_column']];
  684. unset($this->tableColumns[$row['mate_column']]);
  685. }
  686. }
  687. foreach($this->tableColumns as $col => $info)
  688. {
  689. $newTableColumns[$col] = $info;
  690. }
  691. $this->tableColumns = $newTableColumns;
  692. }
  693. }
  694. }
  695. function userButtonClicked()
  696. {
  697. $buttonKey = $this->info->buttonKey;
  698. if(isset($this->userButtons[$buttonKey]['pass_id_array']) && $this->userButtons[$buttonKey]['pass_id_array'])
  699. {
  700. $params = isset($this->userButtons[$buttonKey]['params']) ? $this->userButtons[$buttonKey]['params'] : array();
  701. call_user_func($this->userButtons[$buttonKey]['call_back_fun'],get_object_vars($this->info->checkboxes),$params);
  702. }
  703. else
  704. {
  705. foreach($this->info->checkboxes as $info)
  706. {
  707. $id = $this->escapeData($info);
  708. $query = "select * from $this->tableName where $this->primaryKeyCol = '$id'";
  709. $result = $this->doQuery($query);
  710. if($row = mysql_fetch_assoc($result))
  711. {
  712. if(isset($this->userButtons[$buttonKey]['call_back_fun']) && is_callable($this->userButtons[$buttonKey]['call_back_fun']))
  713. {
  714. $params = isset($this->userButtons[$buttonKey]['params']) ? $this->userButtons[$buttonKey]['params'] : array();
  715. call_user_func($this->userButtons[$buttonKey]['call_back_fun'],$row,$params);
  716. }
  717. }
  718. }
  719. }
  720. if(!(isset($this->userButtons[$buttonKey]['no_update']) && $this->userButtons[$buttonKey]['no_update']))
  721. {
  722. $this->updateHtml();
  723. }
  724. }
  725. function userIconClicked()
  726. {
  727. $id = $this->escapeData($this->info[0]);
  728. $iconKey = $this->escapeData($this->info[1]);
  729. $query = "select * from $this->tableName where $this->primaryKeyCol = '$id'";
  730. $result = $this->doQuery($query);
  731. if($row = mysql_fetch_assoc($result))
  732. {
  733. if(isset($this->userIcons[$iconKey]['call_back_fun']) && is_callable($this->userIcons[$iconKey]['call_back_fun']))
  734. {
  735. call_user_func($this->userIcons[$iconKey]['call_back_fun'],$row);
  736. }
  737. }
  738. if(!(isset($this->userIcons[$iconKey]['no_update']) && $this->userIcons[$iconKey]['no_update']))
  739. {
  740. $this->updateHtml();
  741. }
  742. }
  743. function copyMultRows()
  744. {
  745. foreach($this->info as $id)
  746. {
  747. $afterCopyArray = array();
  748. $id = $this->escapeData($id);
  749. if(!$this->hasRightsToRow($id))
  750. {
  751. $this->handleHacking();
  752. }
  753. $query = "select * from $this->tableName where $this->primaryKeyCol = '$id'";
  754. $result = $this->doQuery($query);
  755. if($row = mysql_fetch_assoc($result))
  756. {
  757. $sets = array();
  758. foreach($this->tableColumns as $col => $info)
  759. {
  760. if(stristr($info['perms'],'C') && !isset($info['join']['real_column']))
  761. {
  762. $val = $row[$col];
  763. if(isset($this->tableColumns[$col]['on_copy_fun']) && is_callable($this->tableColumns[$col]['on_copy_fun']))
  764. {
  765. $val = call_user_func($this->tableColumns[$col]['on_copy_fun'],$col,$val,$row);
  766. }
  767. $val = $this->escapeData($val);
  768. $sets[] = $this->addTickMarks($col)." = '".$val."'";
  769. $afterCopyArray[$col] = $val;
  770. }
  771. }
  772. $query2 = "insert into $this->tableName set ".implode(', ',$sets);
  773. $result2 = $this->doQuery($query2);
  774. if($result2 && isset($this->afterCopyFun) && is_callable($this->afterCopyFun))
  775. {
  776. call_user_func($this->afterCopyFun,mysql_insert_id(),$afterCopyArray);
  777. }
  778. }
  779. }
  780. }
  781. function copyRow()
  782. {
  783. $afterCopyArray = array();
  784. $id = $this->escapeData($this->info);
  785. if(!$this->hasRightsToRow($id))
  786. {
  787. $this->handleHacking();
  788. }
  789. $query = "select * from $this->tableName where $this->primaryKeyCol = '$id'";
  790. $result = $this->doQuery($query);
  791. if($row = mysql_fetch_assoc($result))
  792. {
  793. $sets = array();
  794. foreach($this->tableColumns as $col => $info)
  795. {
  796. if(stristr($info['perms'],'C') && !isset($info['join']['real_column']))
  797. {
  798. $val = $row[$col];
  799. if(isset($this->tableColumns[$col]['on_copy_fun']) && is_callable($this->tableColumns[$col]['on_copy_fun']))
  800. {
  801. $val = call_user_func($this->tableColumns[$col]['on_copy_fun'],$col,$val,$row);
  802. }
  803. $val = $this->escapeData($val);
  804. $sets[] = $this->addTickMarks($col)." = '".$val."'";
  805. $afterCopyArray[$col] = $val;
  806. }
  807. }
  808. $query2 = "insert into $this->tableName set ".implode(', ',$sets);
  809. $result2 = $this->doQuery($query2);
  810. if($result2 && isset($this->afterCopyFun) && is_callable($this->afterCopyFun))
  811. {
  812. call_user_func($this->afterCopyFun,mysql_insert_id(),$afterCopyArray);
  813. }
  814. }
  815. }
  816. function advancedSearch()
  817. {
  818. $_SESSION[$this->varPrefix.'start'] = 0;
  819. $this->info = get_object_vars($this->info);
  820. foreach($this->info as $i => $info)
  821. {
  822. $this->advSearches[$i] = get_object_vars($this->info[$i]);
  823. }
  824. $_SESSION[$this->varPrefix.'advSearches'] = $this->advSearches;
  825. }
  826. function exportInfo()
  827. {
  828. $this->unsetHiddenColumns();
  829. $this->setColumnOrder();
  830. $this->setPagingVars();
  831. $this->formatJoinClause();
  832. $this->formatSelectClause();
  833. $this->formatWhereClause();
  834. $this->setNumResults();
  835. $csvInfo = '';
  836. $csvLb = "\r\n";
  837. $this->extraOrderByInfo = empty($this->extraOrderByInfo) ? '' : ', '.$this->extraOrderByInfo;
  838. $query = $this->selectClause.' '.$this->joinClause.' '.$this->whereClause.' order by '.$this->addTickMarks($this->orderByColumn).' '.$this->getAscOrDesc().' '.$this->extraOrderByInfo;
  839. $result = $this->doQuery($query);
  840. if(mysql_num_rows($result) > 0)
  841. {
  842. $csvRow = array();
  843. foreach($this->tableColumns as $col => $info)
  844. {
  845. if(stristr($info['perms'],'X'))
  846. {
  847. $csvRow[] = '"'.$info['display_text'].'"';
  848. }
  849. }
  850. $csvInfo .= implode(',', $csvRow).$csvLb;
  851. while($row = mysql_fetch_assoc($result))
  852. {
  853. $csvRow = array();
  854. foreach($this->tableColumns as $col => $info)
  855. {
  856. $value = '';
  857. if(stristr($info['perms'],'X'))
  858. {
  859. $value = $row[$col];
  860. if(isset($info['csv_export_fun']) && is_callable($info['csv_export_fun']))
  861. {
  862. $value = call_user_func($info['csv_export_fun'],$col,$value,$row);
  863. }
  864. $csvRow[] = '"'.str_replace(array("\r", "\n", '"'),array('','','""'),$value).'"';
  865. }
  866. }
  867. $csvInfo .= implode(',', $csvRow).$csvLb;
  868. }
  869. return $csvInfo;
  870. }
  871. }
  872. function formatIcons($id,$row)
  873. {
  874. $html = '';
  875. $numIcons = 0;
  876. if(stristr($this->permissions,'V') && stristr($this->removeIcons,'V') === false)
  877. {
  878. $html .= '<li class="info"><a href="javascript: toAjaxTableEditor(\'view_row\',\''.$id.'\');" title="'.$this->langVars->ttlInfo.'"></a></li>';
  879. $numIcons++;
  880. }
  881. if(stristr($this->permissions,'E') && stristr($this->removeIcons,'E') === false)
  882. {
  883. $html .= '<li class="edit"><a href="javascript: toAjaxTableEditor(\'edit_row\',\''.$id.'\');" title="'.$this->langVars->ttlEdit.'"></a></li>';
  884. $numIcons++;
  885. }
  886. if(stristr($this->permissions,'C') && stristr($this->removeIcons,'C') === false)
  887. {
  888. $html .= '<li class="copy"><a href="javascript: toAjaxTableEditor(\'copy_row\',\''.$id.'\');" title="'.$this->langVars->ttlCopy.'"></a></li>';
  889. $numIcons++;
  890. }
  891. if(stristr($this->permissions,'D') && stristr($this->removeIcons,'D') === false)
  892. {
  893. $html .= '<li class="delete"><a href="javascript: confirmDeleteRow(\''.$id.'\')" title="'.$this->langVars->ttlDelete.'"></a></li>';
  894. $numIcons++;
  895. }
  896. // my add !
  897. if(stristr($this->permissions,'Z') && stristr($this->removeIcons,'Z') === false)
  898. {
  899. //$html .= '<li class="execute_transfer"><a href="javascript: toAjaxTableEditor(\'execute_transfer\',\''.$id.'\');" title="'.$this->langVars->ttlExecuteRow.'"></a></li>';
  900. $html .= '<li class="execute" id="execute_row_li"><a href="mng_handler.php?execute=1&table_name='.$this->tableName.'&id='.$id.'"title="execute" ></a> </li>';
  901. //$html .= '<li class="execute" id="execute_row_li"><a href="javascript: toAjaxTableEditor(\'execute_row\',\''.$id.'\');" title="'.$this->langVars->ttlExecuteRow.'"></a></li>';
  902. $numIcons++;
  903. }
  904. if(stristr($this->permissions,'W') && stristr($this->removeIcons,'W') === false)
  905. {
  906. //$html .= '<li class="execute_transfer"><a href="javascript: toAjaxTableEditor(\'execute_transfer\',\''.$id.'\');" title="'.$this->langVars->ttlExecuteRow.'"></a></li>';
  907. $html .= '<li class="execute" id="execute_row_li"><a href="mng_handler.php?mng_undo_transaction=1&transaction_type='.$this->tableName.'&transaction_id='.$id.'"title="undo" ></a> </li>';
  908. //$html .= '<li class="execute" id="execute_row_li"><a href="javascript: toAjaxTableEditor(\'execute_row\',\''.$id.'\');" title="'.$this->langVars->ttlExecuteRow.'"></a></li>';
  909. $numIcons++;
  910. }
  911. if(stristr($this->permissions,'B') && stristr($this->removeIcons,'B') === false)
  912. {
  913. //$html .= '<li class="execute_transfer"><a href="javascript: toAjaxTableEditor(\'execute_transfer\',\''.$id.'\');" title="'.$this->langVars->ttlExecuteRow.'"></a></li>';
  914. $html .= '<li class="pay" id="execute_row_li"><a href="mng_handler.php?pay_transfer=1&table_name='.$this->tableName.'&id='.$id.'"title="pay" ></a> </li>';
  915. //$html .= '<li class="execute" id="execute_row_li"><a href="javascript: toAjaxTableEditor(\'execute_row\',\''.$id.'\');" title="'.$this->langVars->ttlExecuteRow.'"></a></li>';
  916. $numIcons++;
  917. }
  918. if(stristr($this->permissions,'Y') && stristr($this->removeIcons,'Y') === false)
  919. {
  920. //$html .= '<li class="execute_transfer"><a href="javascript: toAjaxTableEditor(\'execute_transfer\',\''.$id.'\');" title="'.$this->langVars->ttlExecuteRow.'"></a></li>';
  921. $html .= '<li class="pay" id="execute_row_li"><a href="mng_handler.php?pay_transfer=1&table_name='.$this->tableName.'&id='.$id.'"title="pay" ></a> </li>';
  922. //$html .= '<li class="execute" id="execute_row_li"><a href="javascript: toAjaxTableEditor(\'execute_row\',\''.$id.'\');" title="'.$this->langVars->ttlExecuteRow.'"></a></li>';
  923. $numIcons++;
  924. }
  925. if(stristr($this->permissions,'N') && stristr($this->removeIcons,'N') === false)
  926. {
  927. if($row['matched']!='0'|| $row['ignore']!='0' ){
  928. }
  929. else{
  930. $html .= '<li class="match" id="match_row_li"><a href="mng_barclays_handler.php?match=1&table_name='.$this->tableName.'&id='.$id.'">match</a></li>
  931. </br><li class="ignore" id="ignore_row_li"><a href="mng_barclays_handler.php?ignore=1&table_name='.$this->tableName.'&id='.$id.'">ignore</a></li>';
  932. $numIcons++;
  933. }
  934. }
  935. /*if(stristr($this->permissions,'I') && stristr($this->removeIcons,'I') === false)
  936. {
  937. if(!strcmp($row['ignore'],'0')){
  938. $html .= '</br><li class="ignore" id="ignore_row_li"><a href="barclays_process_handler.php?ignore=1&table_name='.$this->tableName.'&id='.$id.'">ignore</a></li>';
  939. $numIcons++;
  940. }
  941. }*/
  942. //
  943. foreach($this->userIcons as $iconKey => $info)
  944. {
  945. if(isset($info['call_back_fun']) && is_callable($info['call_back_fun']))
  946. {
  947. $confirmMsg = isset($info['confirm_msg']) ? $info['confirm_msg'] : '';
  948. $html .= '<li class="'.$info['class'].'"><a href="javascript: userIconClicked(\'user_icon_clicked\',new Array(\''.$id.'\',\''.$iconKey.'\'),\''.$confirmMsg.'\')" title="'.$info['title'].'"></a></li>';
  949. $numIcons++;
  950. }
  951. else if(isset($info['icon_html']))
  952. {
  953. $info['icon_html'] = str_replace($this->replaceWithId,$id,$info['icon_html']);
  954. $html .= $info['icon_html'];
  955. $numIcons++;
  956. }
  957. else if(isset($info['format_fun']) && is_callable($info['format_fun']))
  958. {
  959. $userIconInfo = call_user_func($info['format_fun'],$row);
  960. $html .= $userIconInfo['icon_html'];
  961. $numIcons = $numIcons + $userIconInfo['num_icons'];
  962. }
  963. }
  964. if($numIcons > 0)
  965. {
  966. $width = $numIcons * 26;
  967. $html = '<td nowrap="nowrap"><ul class="actions" style="width: '.$width.'px;">'.$html.'</ul></td>';
  968. }
  969. return $html;
  970. }
  971. function viewRow()
  972. {
  973. if(stristr($this->permissions,'V'))
  974. {
  975. $html = '';
  976. $id = $this->escapeData($this->info);
  977. $this->formatJoinClause();
  978. $this->formatSelectClause();
  979. $query = $this->selectClause.' '.$this->joinClause.' where '.$this->tableName.'.'.$this->primaryKeyCol." = '$id'";
  980. $result = $this->doQuery($query);
  981. if($row = mysql_fetch_assoc($result))
  982. {
  983. $html .= '<table '.$this->viewTableInfo.'>';
  984. foreach($this->tableColumns as $col => $info)
  985. {
  986. if(stristr($info['perms'],'V'))
  987. {
  988. $value = $row[$col];
  989. if(isset($info['view_fun']) && is_callable($info['view_fun']))
  990. {
  991. $value = call_user_func($info['view_fun'],$col,$value,$row);
  992. }
  993. $value = strlen(trim($value)) > 0 ? $value : '&nbsp;';
  994. $html .= '<tr><td id="'.$col.'_label_cell" class="labelCell">'.$info['display_text'].':</td><td id="'.$col.'_value_cell" class="valueCell">'.$value.'</td></tr>';
  995. }
  996. }
  997. if(isset($this->viewHtmlFun) && is_callable($this->viewHtmlFun))
  998. {
  999. $html .= call_user_func($this->viewHtmlFun,$row);
  1000. }
  1001. $html .= '</tr></table><div id="viewRowButtons">';
  1002. if(stristr($this->permissions,'E'))
  1003. {
  1004. $html .= '<button class="ajaxButton" onclick="toAjaxTableEditor(\'edit_row\',\''.$id.'\');">'.$this->langVars->btnEdit.'</button>';
  1005. }
  1006. $html .= '<button class="ajaxButton" onclick="toAjaxTableEditor(\'update_html\',\'\');">'.$this->langVars->btnBack.'</button></div>';
  1007. }
  1008. $this->retArr[] = array('layer_id' => 'titleLayer', 'where' => 'innerHTML', 'value' => $this->viewRowTitle);
  1009. $this->retArr[] = array('layer_id' => 'recordLayer', 'where' => 'innerHTML', 'value' => '');
  1010. $this->retArr[] = array('layer_id' => 'filterLayer', 'where' => 'innerHTML', 'value' => '');
  1011. $this->retArr[] = array('layer_id' => 'searchButtonsLayer', 'where' => 'innerHTML', 'value' => '');
  1012. $this->retArr[] = array('layer_id' => 'tableLayer', 'where' => 'innerHTML', 'value' => $html);
  1013. //$this->retArr[] = array('where' => 'javascript', 'value' => "resetScrollTop();");
  1014. if(is_callable($this->viewScreenFun))
  1015. {
  1016. call_user_func($this->viewScreenFun);
  1017. }
  1018. }
  1019. }
  1020. function updateMultRows()
  1021. {
  1022. $this->valError = false;
  1023. $sets = array();
  1024. $afterEditArray = array();
  1025. $idArr = is_object($this->info->idArr) ? get_object_vars($this->info->idArr) : $this->info->idArr;
  1026. $inputInfo = get_object_vars($this->info->inputInfo);
  1027. $insertId = $idArr;
  1028. $hasFileUpload = isset($inputInfo['submit_mate_file_upload']) ? true : false;
  1029. unset($inputInfo['submit_mate_file_upload']);
  1030. foreach($inputInfo as $col => $val)
  1031. {
  1032. $col = str_replace($this->inputIdPrefix,'',$col);
  1033. if(isset($this->tableColumns[$col]))
  1034. {
  1035. // Check to make sure the column has edit permissions.
  1036. if(!stristr($this->tableColumns[$col]['perms'],'E'))
  1037. {
  1038. $this->handleHacking();
  1039. }
  1040. if(isset($this->tableColumns[$col]['on_edit_fun']) && is_callable($this->tableColumns[$col]['on_edit_fun']))
  1041. {
  1042. $val = call_user_func($this->tableColumns[$col]['on_edit_fun'],$col,$val,$this->info);
  1043. }
  1044. // Check Validation
  1045. if(isset($this->tableColumns[$col]['val_fun']) && is_callable($this->tableColumns[$col]['val_fun']) && !call_user_func($this->tableColumns[$col]['val_fun'],$col,$val,$this->info))
  1046. {
  1047. $this->valError = true;
  1048. $this->retArr[] = array('where' => 'javascript', 'value' => '$("'.$col.'_label_cell").setStyle("'.$this->valErrorStyles.'");');
  1049. }
  1050. else if(isset($this->tableColumns[$col]['req']) && $this->tableColumns[$col]['req'] && strlen($val) == 0)
  1051. {
  1052. $this->valError = true;
  1053. $this->retArr[] = array('where' => 'javascript', 'value' => '$("'.$col.'_label_cell").setStyle("'.$this->valErrorStyles.'");');
  1054. }
  1055. else if(!isset($this->tableColumns[$col]['hidden_edit']) || !$this->tableColumns[$col]['hidden_edit'])
  1056. {
  1057. $this->retArr[] = array('where' => 'javascript', 'value' => '$("'.$col.'_label_cell").setStyle("'.$this->noValErrorStyles.'");');
  1058. }
  1059. $afterEditArray[$col] = $val;
  1060. $val = $this->escapeData($val);
  1061. if(isset($this->tableColumns[$col]['mysql_edit_fun']))
  1062. {
  1063. $sets[] = $this->addTickMarks($col)." = ".$this->tableColumns[$col]['mysql_edit_fun']."('".$val."')";
  1064. }
  1065. else
  1066. {
  1067. $sets[] = $this->addTickMarks($col)." = '".$val."'";
  1068. }
  1069. }
  1070. }
  1071. if($this->valError)
  1072. {
  1073. $this->retArr[] = array('where' => 'javascript', 'value' => 'enableButtons();');
  1074. $this->retArr[] = array('layer_id' => 'titleLayer', 'where' => 'innerHTML', 'value' => $this->editRowTitle.'<div style="color: #f90d0d;">'.$this->langVars->errVal.'</div>');
  1075. }
  1076. else if(count($sets) > 0)
  1077. {
  1078. foreach($idArr as $index => $id)
  1079. {
  1080. $idArr[$index] = $this->escapeData($id);
  1081. }
  1082. if(!$this->hasRightsToRows($idArr) || !stristr($this->permissions,'E'))
  1083. {
  1084. $this->handleHacking();
  1085. }
  1086. $query = "update $this->tableName set ".implode(', ',$sets)." where $this->primaryKeyCol in ('".implode("','",$idArr)."')";
  1087. $result = $this->doQuery($query);
  1088. if($result)
  1089. {
  1090. if(!empty($this->afterEditFun) && is_callable($this->afterEditFun))
  1091. {
  1092. call_user_func($this->afterEditFun,$idArr,$this->primaryKeyCol,$afterEditArray);
  1093. }
  1094. }
  1095. if($hasFileUpload)
  1096. {
  1097. $idInputHtml = '<input type="hidden" name="'.$this->inputIdPrefix.$this->primaryKeyCol.'" value="'.htmlspecialchars(serialize($insertId)).'" />';
  1098. $this->retArr[] = array('where' => 'javascript', 'value' => '$(\''.$this->varPrefix.'_add_edit_form\').insert(\''.$idInputHtml.'\');');
  1099. $this->retArr[] = array('where' => 'javascript', 'value' => 'submitFileUploadForm(\''.$this->varPrefix.'\');');
  1100. }
  1101. else
  1102. {
  1103. $this->updateHtml();
  1104. }
  1105. }
  1106. }
  1107. function updateRow()
  1108. {
  1109. $this->valError = false;
  1110. $sets = array();
  1111. $afterEditArray = array();
  1112. $oldPrimaryKeyValue = $this->escapeData($this->info->old_primary_key_value);
  1113. if(!$this->hasRightsToRow($oldPrimaryKeyValue) || !stristr($this->permissions,'E'))
  1114. {
  1115. $this->handleHacking();
  1116. }
  1117. unset($this->info->old_primary_key_value);
  1118. $this->info = get_object_vars($this->info);
  1119. $insertId = isset($this->info[$this->primaryKeyCol]) ? $this->info[$this->primaryKeyCol] : $oldPrimaryKeyValue;
  1120. $hasFileUpload = isset($this->info['submit_mate_file_upload']) ? true : false;
  1121. unset($this->info['submit_mate_file_upload']);
  1122. foreach($this->info as $col => $val)
  1123. {
  1124. $col = str_replace($this->inputIdPrefix,'',$col);
  1125. if(isset($this->tableColumns[$col]))
  1126. {
  1127. // Check to make sure the column has edit permissions.
  1128. if(!stristr($this->tableColumns[$col]['perms'],'E'))
  1129. {
  1130. $this->handleHacking();
  1131. }
  1132. if(isset($this->tableColumns[$col]['on_edit_fun']) && is_callable($this->tableColumns[$col]['on_edit_fun']))
  1133. {
  1134. $val = call_user_func($this->tableColumns[$col]['on_edit_fun'],$col,$val,$this->info);
  1135. }
  1136. // Check Validation
  1137. if(isset($this->tableColumns[$col]['val_fun']) && is_callable($this->tableColumns[$col]['val_fun']) && !call_user_func($this->tableColumns[$col]['val_fun'],$col,$val,$this->info))
  1138. {
  1139. $this->valError = true;
  1140. $this->retArr[] = array('where' => 'javascript', 'value' => '$("'.$col.'_label_cell").setStyle("'.$this->valErrorStyles.'");');
  1141. }
  1142. else if(isset($this->tableColumns[$col]['req']) && $this->tableColumns[$col]['req'])
  1143. {
  1144. if(strlen($val) == 0)
  1145. {
  1146. $this->valError = true;
  1147. $this->retArr[] = array('where' => 'javascript', 'value' => '$("'.$col.'_label_cell").setStyle("'.$this->valErrorStyles.'");');
  1148. }
  1149. else if(isset($this->tableColumns[$col]['calendar']) && substr($val,0,10) == '0000-00-00')
  1150. {
  1151. $this->valError = true;
  1152. $this->retArr[] = array('where' => 'javascript', 'value' => '$("'.$col.'_label_cell").setStyle("'.$this->valErrorStyles.'");');
  1153. }
  1154. }
  1155. else if(!isset($this->tableColumns[$col]['hidden_edit']) || !$this->tableColumns[$col]['hidden_edit'])
  1156. {
  1157. $this->retArr[] = array('where' => 'javascript', 'value' => '$("'.$col.'_label_cell").setStyle("'.$this->noValErrorStyles.'");');
  1158. }
  1159. $val = $this->escapeData($val);
  1160. if(isset($this->tableColumns[$col]['mysql_edit_fun']))
  1161. {
  1162. $sets[] = $this->addTickMarks($col)." = ".$this->tableColumns[$col]['mysql_edit_fun']."('".$val."')";
  1163. $afterEditArray[$col] = $val;
  1164. }
  1165. else
  1166. {
  1167. // Harel Levin 29/07/2011
  1168. if(isset($this->tableColumns[$col]['calendar']))
  1169. {
  1170. $date_array = explode('-',$val);
  1171. include_once ('FormManager.php');
  1172. $form_manager = new FormManager();
  1173. $val = $form_manager->setUnixTimeFromDateTime($date_array[0], $date_array[1], $date_array[2]);
  1174. }
  1175. $sets[] = $this->addTickMarks($col)." = '".$val."'";
  1176. $afterEditArray[$col] = $val;
  1177. }
  1178. }
  1179. }
  1180. if($this->valError)
  1181. {
  1182. $this->retArr[] = array('where' => 'javascript', 'value' => 'enableButtons();');
  1183. $this->retArr[] = array('layer_id' => 'titleLayer', 'where' => 'innerHTML', 'value' => $this->editRowTitle.'<div style="color: #f90d0d;">'.$this->langVars->errVal.'</div>');
  1184. }
  1185. else if(count($sets) > 0)
  1186. {
  1187. $query = "update $this->tableName set ".implode(', ',$sets)." where $this->primaryKeyCol = '$oldPrimaryKeyValue'";
  1188. $result = $this->doQuery($query);
  1189. if($result)
  1190. {
  1191. if(!empty($this->afterEditFun) && is_callable($this->afterEditFun))
  1192. {
  1193. call_user_func($this->afterEditFun,$oldPrimaryKeyValue,$this->primaryKeyCol,$afterEditArray);
  1194. }
  1195. }
  1196. if($hasFileUpload)
  1197. {
  1198. $idInputHtml = '<input type="hidden" name="'.$this->inputIdPrefix.$this->primaryKeyCol.'" value="'.htmlspecialchars(serialize($insertId)).'" />';
  1199. $this->retArr[] = array('where' => 'javascript', 'value' => '$(\''.$this->varPrefix.'_add_edit_form\').insert(\''.$idInputHtml.'\');');
  1200. $this->retArr[] = array('where' => 'javascript', 'value' => 'submitFileUploadForm(\''.$this->varPrefix.'\');');
  1201. }
  1202. else
  1203. {
  1204. $this->updateHtml();
  1205. }
  1206. }
  1207. }
  1208. function insertRow()
  1209. {
  1210. $this->valError = false;
  1211. $this->info = get_object_vars($this->info);
  1212. $hasFileUpload = isset($this->info['submit_mate_file_upload']) ? true : false;
  1213. $insertId = isset($this->info[$this->primaryKeyCol]) ? $this->info[$this->primaryKeyCol] : '';
  1214. unset($this->info['submit_mate_file_upload']);
  1215. $sets = array();
  1216. $afterAddArray = array();
  1217. if(!stristr($this->permissions,'A'))
  1218. {
  1219. $this->handleHacking();
  1220. }
  1221. foreach($this->info as $col => $val)
  1222. {
  1223. if(is_array($val))
  1224. {
  1225. $temp_info = $this->info;
  1226. for($i=0;$i<count($val);$i++)
  1227. {
  1228. $temp_info[$col] = $val[$i];
  1229. $this->info = (object) $temp_info;
  1230. $temp_info[$this->primaryKeyCol]++;
  1231. $this->insertRow();
  1232. }
  1233. return;
  1234. }
  1235. $col = str_replace($this->inputIdPrefix,'',$col);
  1236. // Check to make sure the column has add permissions
  1237. if(!stristr($this->tableColumns[$col]['perms'],'A'))
  1238. {
  1239. $this->handleHacking();
  1240. }
  1241. if(isset($this->tableColumns[$col]['on_add_fun']) && is_callable($this->tableColumns[$col]['on_add_fun']))
  1242. {
  1243. $val = call_user_func($this->tableColumns[$col]['on_add_fun'],$col,$val,$this->info);
  1244. }
  1245. // Check Validation
  1246. if(isset($this->tableColumns[$col]['val_fun']) && is_callable($this->tableColumns[$col]['val_fun']) && !call_user_func($this->tableColumns[$col]['val_fun'],$col,$val,$this->info))
  1247. {
  1248. $this->valError = true;
  1249. $this->retArr[] = array('where' => 'javascript', 'value' => '$("'.$col.'_label_cell").setStyle("'.$this->valErrorStyles.'");');
  1250. }
  1251. else if(isset($this->tableColumns[$col]['req']) && $this->tableColumns[$col]['req'])
  1252. {
  1253. if(strlen($val) == 0)
  1254. {
  1255. $this->valError = true;
  1256. $this->retArr[] = array('where' => 'javascript', 'value' => '$("'.$col.'_label_cell").setStyle("'.$this->valErrorStyles.'");');
  1257. }
  1258. else if(isset($this->tableColumns[$col]['calendar']) && substr($val,0,10) == '0000-00-00')
  1259. {
  1260. $this->valError = true;
  1261. $this->retArr[] = array('where' => 'javascript', 'value' => '$("'.$col.'_label_cell").setStyle("'.$this->valErrorStyles.'");');
  1262. }
  1263. else if(strlen($val) > 0 && (!isset($this->tableColumns[$col]['hidden_add']) || !$this->tableColumns[$col]['hidden_add']))
  1264. {
  1265. $this->retArr[] = array('where' => 'javascript', 'value' => '$("'.$col.'_label_cell").setStyle("'.$this->noValErrorStyles.'");');
  1266. }
  1267. }
  1268. else if(!isset($this->tableColumns[$col]['hidden_add']) || !$this->tableColumns[$col]['hidden_add'])
  1269. {
  1270. $this->retArr[] = array('where' => 'javascript', 'value' => '$("'.$col.'_label_cell").setStyle("'.$this->noValErrorStyles.'");');
  1271. }
  1272. $val = $this->escapeData($val);
  1273. if(isset($this->tableColumns[$col]['mysql_add_fun']))
  1274. {
  1275. $sets[] = $this->addTickMarks($col)." = ".$this->tableColumns[$col]['mysql_add_fun']."('".$val."')";
  1276. $afterAddArray[$col] = $val;
  1277. }
  1278. else
  1279. {
  1280. // Harel Levin 29/07/2011
  1281. if(isset($this->tableColumns[$col]['calendar']))
  1282. {
  1283. $date_array = explode('-',$val);
  1284. include_once ('FormManager.php');
  1285. $form_manager = new FormManager();
  1286. $val = $form_manager->setUnixTimeFromDateTime($date_array[0], $date_array[1], $date_array[2]);
  1287. }
  1288. $sets[] = $this->addTickMarks($col)." = '".$val."'";
  1289. $afterAddArray[$col] = $val;
  1290. }
  1291. }
  1292. if($this->valError)
  1293. {
  1294. $this->retArr[] = array('where' => 'javascript', 'value' => 'enableButtons();');
  1295. $this->retArr[] = array('layer_id' => 'titleLayer', 'where' => 'innerHTML', 'value' => $this->addRowTitle.'<div style="color: #f90d0d;">'.$this->langVars->errVal.'</div>');
  1296. }
  1297. else
  1298. {
  1299. $query = "insert into $this->tableName set ".implode(', ',$sets);
  1300. $result = $this->doQuery($query);
  1301. if($result)
  1302. {
  1303. $insertId = strlen($insertId) > 0 ? $insertId : mysql_insert_id();
  1304. if(!empty($this->afterAddFun) && is_callable($this->afterAddFun))
  1305. {
  1306. call_user_func($this->afterAddFun,$insertId,$this->primaryKeyCol,$afterAddArray);
  1307. }
  1308. }
  1309. if($hasFileUpload)
  1310. {
  1311. $idInputHtml = '<input type="hidden" name="'.$this->inputIdPrefix.$this->primaryKeyCol.'" value="'.htmlspecialchars(serialize($insertId)).'" />';
  1312. $idInputHtml .= '<input type="hidden" name="'.$this->inputIdPrefix.'_mate_was_inserting_new_row" value="true" />';
  1313. $this->retArr[] = array('where' => 'javascript', 'value' => '$(\''.$this->varPrefix.'_add_edit_form\').insert(\''.$idInputHtml.'\');');
  1314. $this->retArr[] = array('where' => 'javascript', 'value' => 'submitFileUploadForm(\''.$this->varPrefix.'\');');
  1315. }
  1316. else
  1317. {
  1318. $this->updateHtml();
  1319. }
  1320. }
  1321. }
  1322. function addRow()
  1323. {
  1324. if(stristr($this->permissions,'A'))
  1325. {
  1326. $html = '';
  1327. $rowHtml = '';
  1328. $jsAddFun = 'addRow(\''.$this->varPrefix.'\');';
  1329. $calJs = array();
  1330. $formInfo = '';
  1331. $iFrameHtml = '';
  1332. $hasFileUpload = false;
  1333. foreach($this->tableColumns as $col => $info)
  1334. {
  1335. if(stristr($info['perms'],'A') && !isset($info['join']['real_column']))
  1336. {
  1337. $defVal = isset($info['default']) ? $info['default'] : '';
  1338. $inputInfo = isset($info['input_info']) ? $info['input_info'] : '';
  1339. $inputHtml = '';
  1340. if(isset($info['add_fun']) && is_callable($info['add_fun']))
  1341. {
  1342. $defVal = call_user_func($info['add_fun'],$col,$defVal,$inputInfo);
  1343. }
  1344. if(isset($info['format_input_fun']) && is_callable($info['format_input_fun']))
  1345. {
  1346. $inputHtml .= call_user_func($info['format_input_fun'],$col,$defVal,$inputInfo);
  1347. }
  1348. else if(isset($info['hidden_add']) && $info['hidden_add'])
  1349. {
  1350. $inputHtml .= '<input type="hidden" id="'.$this->inputIdPrefix.$col.'" value="'.htmlspecialchars($defVal).'" '.$inputInfo.' />';
  1351. }
  1352. else if(isset($info['select_query']))
  1353. {
  1354. $inputHtml .= $this->getSelect($info['select_query'],$this->inputIdPrefix.$col,$defVal,$inputInfo);
  1355. }
  1356. else if(isset($info['select_array']) && is_array($info['select_array']))
  1357. {
  1358. if(isset($info['multi_currency']))
  1359. $inputHtml .= $this->getSelectFromArray($info['select_array'],$this->inputIdPrefix.$col,'-',$inputInfo,true);
  1360. else
  1361. $inputHtml .= $this->getSelectFromArray($info['select_array'],$this->inputIdPrefix.$col,$defVal,$inputInfo);
  1362. }
  1363. else if(isset($info['textarea']))
  1364. {
  1365. $numRows = isset($info['textarea']['rows']) ? $info['textarea']['rows'] : 7;
  1366. $numCols = isset($info['textarea']['cols']) ? $info['textarea']['cols'] : 25;
  1367. $inputHtml .= '<textarea id="'.$this->inputIdPrefix.$col.'" rows="'.$numRows.'" cols="'.$numCols.'" value="'.htmlspecialchars($defVal).'" '.$inputInfo.' >'.htmlspecialchars($defVal).'</textarea>';
  1368. }
  1369. else if(isset($info['checkbox']))
  1370. {
  1371. $checkedValue = isset($info['checkbox']['checked_value']) ? $info['checkbox']['checked_value'] : '';
  1372. $unCheckedValue = isset($info['checkbox']['un_checked_value']) ? $info['checkbox']['un_checked_value'] : '';
  1373. $checked = $defVal == $checkedValue ? 'checked="checked"' : '';
  1374. $inputHtml .= '<input type="checkbox" id="'.$this->inputIdPrefix.$col.'" value="'.$defVal.'" onclick="updateCheckBoxValue(this,\''.$checkedValue.'\',\''.$unCheckedValue.'\');" '.$checked.' '.$inputInfo.' />';
  1375. }
  1376. else if(isset($info['file_upload']) && is_array($info['file_upload']))
  1377. {
  1378. $inputHtml .= '<input type="file" name="'.$this->inputIdPrefix.$col.'" id="'.$this->inputIdPrefix.$col.'" '.$inputInfo.' />';
  1379. $hasFileUpload = true;
  1380. }
  1381. else if(isset($info['join']) && is_array($info['join']))
  1382. {
  1383. $joinTable = !empty($info['join']['db']) ? $info['join']['db'].'.'.$info['join']['table'] : $info['join']['table'];
  1384. $info['join']['display_mask'] = isset($info['join']['display_mask']) ? $info['join']['display_mask'] : $joinTable.'.'.$info['join']['column'];
  1385. $selectQuery = 'select distinct('.$info['join']['column'].'), '.$info['join']['display_mask'].' from '.$joinTable.' order by '.$info['join']['display_mask'].' asc';
  1386. $inputHtml .= $this->getSelect($selectQuery,$this->inputIdPrefix.$col,$defVal,$inputInfo);
  1387. }
  1388. else if(isset($info['calendar']))
  1389. {
  1390. $inputHtml .= '<span><input type="hidden" id="'.$this->inputIdPrefix.$col.'" value="'.htmlspecialchars($defVal).'" /></span>';
  1391. }
  1392. else
  1393. {
  1394. $maxLength = isset($info['maxlen']) ? 'maxlength="'.$info['maxlen'].'"' : '';
  1395. $inputHtml .= '<input type="text" id="'.$this->inputIdPrefix.$col.'" value="'.htmlspecialchars($defVal).'" '.$maxLength.' onKeyPress="if(enterPressed(event)){'.$jsAddFun.' return false;}" '.$inputInfo.' />';
  1396. }
  1397. if(isset($info['hidden_add']) && $info['hidden_add'])
  1398. {
  1399. $rowHtml .= $inputHtml;
  1400. }
  1401. else
  1402. {
  1403. if(isset($info['req']) && $info['req'])
  1404. {
  1405. $inputHtml .= $this->reqMarker;
  1406. }
  1407. $rowHtml .= '<tr><td id="'.$col.'_label_cell" class="labelCell"><label for="'.$this->inputIdPrefix.$col.'">'.$info['display_text'].':</label></td><td id="'.$col.'_input_cell" class="inputCell">'.$inputHtml.'</td></tr>';
  1408. }
  1409. if(isset($info['calendar']))
  1410. {
  1411. $calJs[] = $this->getCalJs($info['calendar'],$col);
  1412. }
  1413. }
  1414. }
  1415. if($hasFileUpload)
  1416. {
  1417. $formInfo = 'method="post" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data" target="'.$this->varPrefix.'_file_upload_frame" onSubmit="return false;"';
  1418. $iFrameHtml .= '<div style="display: none">';
  1419. $iFrameHtml .= '<iframe src="" name="'.$this->varPrefix.'_file_upload_frame" id="'.$this->varPrefix.'_file_upload_frame"></iframe>';
  1420. $iFrameHtml .= '</div>';
  1421. }
  1422. $html .= '<form id="'.$this->varPrefix.'_add_edit_form" name="'.$this->varPrefix.'_add_edit_form" '.$formInfo.'><table '.$this->addTableInfo.'>';
  1423. $html .= $rowHtml;
  1424. $html .= '</table></form><div id="addRowButtons"><button class="ajaxButton" onclick="'.$jsAddFun.'">'.$this->langVars->btnAdd.'</button><button class="ajaxButton" onclick="toAjaxTableEditor(\'update_html\',\'\');">'.$this->langVars->btnCancel.'</button></div>';
  1425. $html .= $iFrameHtml;
  1426. $this->retArr[] = array('layer_id' => 'titleLayer', 'where' => 'innerHTML', 'value' => $this->addRowTitle);
  1427. $this->retArr[] = array('layer_id' => 'recordLayer', 'where' => 'innerHTML', 'value' => '');
  1428. $this->retArr[] = array('layer_id' => 'filterLayer', 'where' => 'innerHTML', 'value' => '');
  1429. $this->retArr[] = array('layer_id' => 'searchButtonsLayer', 'where' => 'innerHTML', 'value' => '');
  1430. $this->retArr[] = array('layer_id' => 'tableLayer', 'where' => 'innerHTML', 'value' => $html);
  1431. if(count($calJs) > 0)
  1432. {
  1433. $this->retArr[] = array('where' => 'javascript', 'value' => implode("\n",$calJs));
  1434. }
  1435. //$this->retArr[] = array('where' => 'javascript', 'value' => "resetScrollTop();");
  1436. if(is_callable($this->addScreenFun))
  1437. {
  1438. call_user_func($this->addScreenFun);
  1439. }
  1440. }
  1441. }
  1442. function editMultRows()
  1443. {
  1444. if(stristr($this->permissions,'E'))
  1445. {
  1446. if($this->allowEditMult)
  1447. {
  1448. $html = '';
  1449. $rowHtml = '';
  1450. $calJs = array();
  1451. $formInfo = '';
  1452. $iFrameHtml = '';
  1453. $hasFileUpload = false;
  1454. $this->info = get_object_vars($this->info);
  1455. $jsUpdateFun = "updateMultRows(new Array('".implode("','",$this->info)."'),'$this->varPrefix');";
  1456. $id = $this->escapeData(current($this->info));
  1457. $query = "select * from $this->tableName where $this->primaryKeyCol = '$id'";
  1458. $result = $this->doQuery($query);
  1459. if($row = mysql_fetch_assoc($result))
  1460. {
  1461. foreach($this->tableColumns as $col => $info)
  1462. {
  1463. if(stristr($info['perms'],'E') && !isset($info['join']['real_column']) && $col != $this->primaryKeyCol)
  1464. {
  1465. if(isset($row[$col]) && strlen($row[$col]) > 0)
  1466. {
  1467. $defVal = $row[$col];
  1468. }
  1469. else if(isset($info['default']))
  1470. {
  1471. $defVal = $info['default'];
  1472. }
  1473. else
  1474. {
  1475. $defVal = '';
  1476. }
  1477. $inputInfo = isset($info['input_info']) ? $info['input_info'] : '';
  1478. $inputInfo .= ' disabled="disabled"';
  1479. $inputHtml = '';
  1480. if(isset($info['edit_fun']) && is_callable($info['edit_fun']))
  1481. {
  1482. $defVal = call_user_func($info['edit_fun'],$col,$defVal,$row);
  1483. }
  1484. if(isset($info['format_input_fun']) && is_callable($info['format_input_fun']))
  1485. {
  1486. $inputHtml .= call_user_func($info['format_input_fun'],$col,$defVal,$row,$inputInfo);
  1487. }
  1488. else if(isset($info['hidden_edit']) && $info['hidden_edit'])
  1489. {
  1490. $inputHtml .= '<input type="hidden" id="'.$this->inputIdPrefix.$col.'" value="'.htmlspecialchars($defVal).'" '.$inputInfo.' />';
  1491. }
  1492. else if(isset($info['select_query']))
  1493. {
  1494. $inputHtml .= $this->getSelect($info['select_query'],$this->inputIdPrefix.$col,$defVal,$inputInfo);
  1495. }
  1496. else if(isset($info['select_array']) && is_array($info['select_array']))
  1497. {
  1498. $inputHtml .= $this->getSelectFromArray($info['select_array'],$this->inputIdPrefix.$col,$defVal,$inputInfo);
  1499. }
  1500. else if(isset($info['textarea']))
  1501. {
  1502. $numRows = isset($info['textarea']['rows']) ? $info['textarea']['rows'] : 7;
  1503. $numCols = isset($info['textarea']['cols']) ? $info['textarea']['cols'] : 25;
  1504. $inputHtml .= '<textarea id="'.$this->inputIdPrefix.$col.'" rows="'.$numRows.'" cols="'.$numCols.'" value="'.htmlspecialchars($defVal).'" '.$inputInfo.' >'.htmlspecialchars($defVal).'</textarea>';
  1505. }
  1506. else if(isset($info['checkbox']))
  1507. {
  1508. $checkedValue = isset($info['checkbox']['checked_value']) ? $info['checkbox']['checked_value'] : '';
  1509. $unCheckedValue = isset($info['checkbox']['un_checked_value']) ? $info['checkbox']['un_checked_value'] : '';
  1510. $checked = $defVal == $checkedValue ? 'checked="checked"' : '';
  1511. $inputHtml .= '<input type="checkbox" id="'.$this->inputIdPrefix.$col.'" value="'.$defVal.'" onclick="updateCheckBoxValue(this,\''.$checkedValue.'\',\''.$unCheckedValue.'\');" '.$checked.' '.$inputInfo.' />';
  1512. }
  1513. else if(isset($info['file_upload']) && is_array($info['file_upload']))
  1514. {
  1515. $defVal = isset($info['display_mask']) && isset($row[$info['display_mask']]) ? $row[$info['display_mask']] : '';
  1516. $defVal = strlen($defVal) > 0 ? htmlspecialchars($defVal).'<br />' : '';
  1517. $inputHtml .= $defVal.'<input type="file" name="'.$this->inputIdPrefix.$col.'" id="'.$this->inputIdPrefix.$col.'" '.$inputInfo.' />';
  1518. $inputHtml .= '<input type="hidden" name="'.$this->inputIdPrefix.$this->primaryKeyCol.'" value="'.htmlspecialchars(serialize($this->info)).'" />';
  1519. $hasFileUpload = true;
  1520. }
  1521. else if(isset($info['join']) && is_array($info['join']))
  1522. {
  1523. $joinTable = !empty($info['join']['db']) ? $info['join']['db'].'.'.$info['join']['table'] : $info['join']['table'];
  1524. $info['join']['display_mask'] = isset($info['join']['display_mask']) ? $info['join']['display_mask'] : $joinTable.'.'.$info['join']['column'];
  1525. $selectQuery = 'select distinct('.$info['join']['column'].'), '.$info['join']['display_mask'].' from '.$joinTable.' order by '.$info['join']['display_mask'].' asc';
  1526. $inputHtml .= $this->getSelect($selectQuery,$this->inputIdPrefix.$col,$defVal,$inputInfo);
  1527. }
  1528. else if(isset($info['calendar']))
  1529. {
  1530. $inputHtml .= '<span><input type="hidden" id="'.$this->inputIdPrefix.$col.'" value="'.htmlspecialchars($defVal).'" /></span>';
  1531. }
  1532. else
  1533. {
  1534. $maxLength = isset($info['maxlen']) ? 'maxlength="'.$info['maxlen'].'"' : '';
  1535. $inputHtml .= '<input type="text" id="'.$this->inputIdPrefix.$col.'" value="'.htmlspecialchars($defVal).'" '.$maxLength.' onKeyPress="if(enterPressed(event)){'.$jsUpdateFun.' return false;}" '.$inputInfo.' />';
  1536. }
  1537. if(isset($info['hidden_edit']) && $info['hidden_edit'])
  1538. {
  1539. $rowHtml .= $inputHtml;
  1540. }
  1541. else
  1542. {
  1543. if(isset($info['req']) && $info['req'])
  1544. {
  1545. $inputHtml .= '<span id="'.$col.'_req_mark" style="display: none;">'.$this->reqMarker.'</span>';
  1546. }
  1547. $rowHtml .= '<tr>';
  1548. $rowHtml .= '<td id="'.$col.'_label_cell" class="labelCell"><label for="'.$this->inputIdPrefix.$col.'">'.$info['display_text'].':</label></td>';
  1549. $rowHtml .= '<td id="'.$col.'_input_cell" class="inputCell">'.$inputHtml.'</td>';
  1550. $rowHtml .= '<td><input type="checkbox" id="'.$this->inputIdPrefix.$col.'_em_cb" onchange="disableEnableInput(\''.$this->inputIdPrefix.$col.'\',this);" /></td>';
  1551. $rowHtml .= '</tr>';
  1552. }
  1553. if(isset($info['calendar']))
  1554. {
  1555. $calJs[] = $this->getCalJs($info['calendar'],$col);
  1556. }
  1557. }
  1558. }
  1559. if($hasFileUpload)
  1560. {
  1561. $formInfo = 'method="post" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data" target="'.$this->varPrefix.'_file_upload_frame" onSubmit="return false;"';
  1562. $iFrameHtml .= '<div style="display: none">';
  1563. $iFrameHtml .= '<iframe src="" name="'.$this->varPrefix.'_file_upload_frame" id="'.$this->varPrefix.'_file_upload_frame"></iframe>';
  1564. $iFrameHtml .= '</div>';
  1565. }
  1566. $html .= '<form id="'.$this->varPrefix.'_add_edit_form" name="'.$this->varPrefix.'_add_edit_form" '.$formInfo.'><table '.$this->editTableInfo.'>';
  1567. $html .= $rowHtml;
  1568. $html .= '</table></form><div id="editRowButtons"><button class="ajaxButton" onclick="'.$jsUpdateFun.'">'.$this->langVars->btnUpdate.'</button><button class="ajaxButton" onclick="toAjaxTableEditor(\'update_html\',\'\');">'.$this->langVars->btnCancel.'</button></div>';
  1569. $html .= $iFrameHtml;
  1570. }
  1571. $this->retArr[] = array('layer_id' => 'titleLayer', 'where' => 'innerHTML', 'value' => $this->editMultTitle);
  1572. $this->retArr[] = array('layer_id' => 'recordLayer', 'where' => 'innerHTML', 'value' => '');
  1573. $this->retArr[] = array('layer_id' => 'filterLayer', 'where' => 'innerHTML', 'value' => '');
  1574. $this->retArr[] = array('layer_id' => 'searchButtonsLayer', 'where' => 'innerHTML', 'value' => '');
  1575. $this->retArr[] = array('layer_id' => 'tableLayer', 'where' => 'innerHTML', 'value' => $html);
  1576. if(count($calJs) > 0)
  1577. {
  1578. $this->retArr[] = array('where' => 'javascript', 'value' => implode("\n",$calJs));
  1579. }
  1580. //$this->retArr[] = array('where' => 'javascript', 'value' => "resetScrollTop();");
  1581. if(is_callable($this->editScreenFun))
  1582. {
  1583. call_user_func($this->editScreenFun);
  1584. }
  1585. }
  1586. else
  1587. {
  1588. $this->warnings[] = $this->langVars->edit1Row;
  1589. }
  1590. }
  1591. }
  1592. function editRow()
  1593. {
  1594. if(stristr($this->permissions,'E'))
  1595. {
  1596. $html = '';
  1597. $rowHtml = '';
  1598. $calJs = array();
  1599. $id = $this->escapeData($this->info);
  1600. $jsUpdateFun = 'updateRow(\''.$id.'\',\''.$this->varPrefix.'\');';
  1601. $formInfo = '';
  1602. $iFrameHtml = '';
  1603. $hasFileUpload = false;
  1604. $query = "select * from $this->tableName where $this->primaryKeyCol = '$id'";
  1605. $result = $this->doQuery($query);
  1606. if($row = mysql_fetch_assoc($result))
  1607. {
  1608. foreach($this->tableColumns as $col => $info)
  1609. {
  1610. if(stristr($info['perms'],'E') && !isset($info['join']['real_column']))
  1611. {
  1612. if(isset($row[$col]) && strlen($row[$col]) > 0)
  1613. {
  1614. // If it is a calendar and there is a default and no date use the default
  1615. if(isset($info['default']) && isset($info['calendar']) && $info['calendar'] && substr($row[$col],0,10) == '0000-00-00')
  1616. {
  1617. $defVal = $info['default'];
  1618. }
  1619. else
  1620. {
  1621. $defVal = $row[$col];
  1622. }
  1623. }
  1624. else if(isset($info['default']))
  1625. {
  1626. $defVal = $info['default'];
  1627. }
  1628. else
  1629. {
  1630. $defVal = '';
  1631. }
  1632. $inputInfo = isset($info['input_info']) ? $info['input_info'] : '';
  1633. $inputHtml = '';
  1634. if(isset($info['edit_fun']) && is_callable($info['edit_fun']))
  1635. {
  1636. $defVal = call_user_func($info['edit_fun'],$col,$defVal,$row);
  1637. }
  1638. if(isset($info['format_input_fun']) && is_callable($info['format_input_fun']))
  1639. {
  1640. $inputHtml .= call_user_func($info['format_input_fun'],$col,$defVal,$row,$inputInfo);
  1641. }
  1642. else if(isset($info['hidden_edit']) && $info['hidden_edit'])
  1643. {
  1644. $inputHtml .= '<input type="hidden" id="'.$this->inputIdPrefix.$col.'" value="'.htmlspecialchars($defVal).'" '.$inputInfo.' />';
  1645. }
  1646. else if(isset($info['select_query']))
  1647. {
  1648. $inputHtml .= $this->getSelect($info['select_query'],$this->inputIdPrefix.$col,$defVal,$inputInfo);
  1649. }
  1650. else if(isset($info['select_array']) && is_array($info['select_array']))
  1651. {
  1652. $inputHtml .= $this->getSelectFromArray($info['select_array'],$this->inputIdPrefix.$col,$defVal,$inputInfo);
  1653. }
  1654. else if(isset($info['textarea']))
  1655. {
  1656. $numRows = isset($info['textarea']['rows']) ? $info['textarea']['rows'] : 7;
  1657. $numCols = isset($info['textarea']['cols']) ? $info['textarea']['cols'] : 25;
  1658. $inputHtml .= '<textarea id="'.$this->inputIdPrefix.$col.'" rows="'.$numRows.'" cols="'.$numCols.'" value="'.htmlspecialchars($defVal).'" '.$inputInfo.' >'.htmlspecialchars($defVal).'</textarea>';
  1659. }
  1660. else if(isset($info['checkbox']))
  1661. {
  1662. $checkedValue = isset($info['checkbox']['checked_value']) ? $info['checkbox']['checked_value'] : '';
  1663. $unCheckedValue = isset($info['checkbox']['un_checked_value']) ? $info['checkbox']['un_checked_value'] : '';
  1664. $checked = $defVal == $checkedValue ? 'checked="checked"' : '';
  1665. $inputHtml .= '<input type="checkbox" id="'.$this->inputIdPrefix.$col.'" value="'.$defVal.'" onclick="updateCheckBoxValue(this,\''.$checkedValue.'\',\''.$unCheckedValue.'\');" '.$checked.' '.$inputInfo.' />';
  1666. }
  1667. else if(isset($info['file_upload']) && is_array($info['file_upload']))
  1668. {
  1669. $defVal = isset($info['display_mask']) && isset($row[$info['display_mask']]) ? $row[$info['display_mask']] : '';
  1670. $defVal = strlen($defVal) > 0 ? htmlspecialchars($defVal).'<br />' : '';
  1671. $inputHtml .= $defVal.'<input type="file" name="'.$this->inputIdPrefix.$col.'" id="'.$this->inputIdPrefix.$col.'" '.$inputInfo.' />';
  1672. $hasFileUpload = true;
  1673. }
  1674. else if(isset($info['join']) && is_array($info['join']))
  1675. {
  1676. $joinTable = !empty($info['join']['db']) ? $info['join']['db'].'.'.$info['join']['table'] : $info['join']['table'];
  1677. $info['join']['display_mask'] = isset($info['join']['display_mask']) ? $info['join']['display_mask'] : $joinTable.'.'.$info['join']['column'];
  1678. $selectQuery = 'select distinct('.$info['join']['column'].'), '.$info['join']['display_mask'].' from '.$joinTable.' order by '.$info['join']['display_mask'].' asc';
  1679. $inputHtml .= $this->getSelect($selectQuery,$this->inputIdPrefix.$col,$defVal,$inputInfo);
  1680. }
  1681. else if(isset($info['calendar']))
  1682. {
  1683. $inputHtml .= '<span><input type="hidden" id="'.$this->inputIdPrefix.$col.'" value="'.htmlspecialchars($defVal).'" /></span>';
  1684. }
  1685. else
  1686. {
  1687. $maxLength = isset($info['maxlen']) ? 'maxlength="'.$info['maxlen'].'"' : '';
  1688. $inputHtml .= '<input type="text" id="'.$this->inputIdPrefix.$col.'" value="'.htmlspecialchars($defVal).'" '.$maxLength.' onKeyPress="if(enterPressed(event)){'.$jsUpdateFun.' return false;}" '.$inputInfo.' />';
  1689. }
  1690. if(isset($info['hidden_edit']) && $info['hidden_edit'])
  1691. {
  1692. $rowHtml .= $inputHtml;
  1693. }
  1694. else
  1695. {
  1696. if(isset($info['req']) && $info['req'])
  1697. {
  1698. $inputHtml .= $this->reqMarker;
  1699. }
  1700. $rowHtml .= '<tr><td id="'.$col.'_label_cell" class="labelCell"><label for="'.$this->inputIdPrefix.$col.'">'.$info['display_text'].':</label></td><td id="'.$col.'_input_cell" class="inputCell">'.$inputHtml.'</td></tr>';
  1701. }
  1702. if(isset($info['calendar']))
  1703. {
  1704. $calJs[] = $this->getCalJs($info['calendar'],$col).'$("show_'.$this->inputIdPrefix.$col.'").innerHTML = "'.date("d-m-Y",$defVal).'"';
  1705. }
  1706. }
  1707. }
  1708. if($hasFileUpload)
  1709. {
  1710. $formInfo = 'method="post" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data" target="'.$this->varPrefix.'_file_upload_frame" onSubmit="return false;"';
  1711. $iFrameHtml .= '<div style="display: none">';
  1712. $iFrameHtml .= '<iframe src="" name="'.$this->varPrefix.'_file_upload_frame" id="'.$this->varPrefix.'_file_upload_frame"></iframe>';
  1713. $iFrameHtml .= '</div>';
  1714. }
  1715. $html .= '<form id="'.$this->varPrefix.'_add_edit_form" name="'.$this->varPrefix.'_add_edit_form" '.$formInfo.'><table '.$this->editTableInfo.'>';
  1716. $html .= $rowHtml;
  1717. $html .= '</table></form><div id="editRowButtons"><button class="ajaxButton" onclick="'.$jsUpdateFun.'">'.$this->langVars->btnUpdate.'</button><button class="ajaxButton" onclick="toAjaxTableEditor(\'update_html\',\'\');">'.$this->langVars->btnCancel.'</button></div>';
  1718. $html .= $iFrameHtml;
  1719. }
  1720. $this->retArr[] = array('layer_id' => 'titleLayer', 'where' => 'innerHTML', 'value' => $this->editRowTitle);
  1721. $this->retArr[] = array('layer_id' => 'recordLayer', 'where' => 'innerHTML', 'value' => '');
  1722. $this->retArr[] = array('layer_id' => 'filterLayer', 'where' => 'innerHTML', 'value' => '');
  1723. $this->retArr[] = array('layer_id' => 'searchButtonsLayer', 'where' => 'innerHTML', 'value' => '');
  1724. $this->retArr[] = array('layer_id' => 'tableLayer', 'where' => 'innerHTML', 'value' => $html);
  1725. if(count($calJs) > 0)
  1726. {
  1727. $this->retArr[] = array('where' => 'javascript', 'value' => implode("\n",$calJs));
  1728. }
  1729. //$this->retArr[] = array('where' => 'javascript', 'value' => "resetScrollTop();");
  1730. if(is_callable($this->editScreenFun))
  1731. {
  1732. call_user_func($this->editScreenFun);
  1733. }
  1734. }
  1735. }
  1736. function getCalJs($calInfo,$col)
  1737. {
  1738. if(is_array($calInfo))
  1739. {
  1740. $dateFormat = isset($calInfo['format']) ? $calInfo['format'] : $this->defaultJsCalFormat;
  1741. $resetDate = isset($calInfo['reset']) ? 'true' : 'null';
  1742. $extraInfo = isset($calInfo['extra_info']) ? $calInfo['extra_info'] : 'null';
  1743. }
  1744. else
  1745. {
  1746. // Maintain backwards compatibility
  1747. $dateFormat = is_string($calInfo) ? $calInfo : $this->defaultJsCalFormat;
  1748. $resetDate = 'null';
  1749. $extraInfo = 'null';
  1750. }
  1751. return 'prepareForCalendar(document.getElementById(\''.$this->inputIdPrefix.$col.'\'),\''.$this->inputIdPrefix.$col.'\',\''.$dateFormat.'\','.$resetDate.','.$extraInfo.');';
  1752. }
  1753. function deleteRow()
  1754. {
  1755. if(stristr($this->permissions,'D'))
  1756. {
  1757. $id = $this->escapeData($this->info);
  1758. if(!$this->hasRightsToRow($id))
  1759. {
  1760. $this->handleHacking();
  1761. }
  1762. $query = "delete from $this->tableName where $this->primaryKeyCol = '$id'";
  1763. $result = $this->doQuery($query);
  1764. if($result)
  1765. {
  1766. if(isset($this->afterDeleteFun) && is_callable($this->afterDeleteFun))
  1767. {
  1768. call_user_func($this->afterDeleteFun,$id,$this->primaryKeyCol);
  1769. }
  1770. $this->updateHtml();
  1771. }
  1772. }
  1773. }
  1774. function deleteMultRows()
  1775. {
  1776. if(stristr($this->permissions,'D'))
  1777. {
  1778. foreach($this->info as $id)
  1779. {
  1780. $id = $this->escapeData($id);
  1781. if(!$this->hasRightsToRow($id))
  1782. {
  1783. $this->handleHacking();
  1784. }
  1785. $query = "delete from $this->tableName where $this->primaryKeyCol = '$id'";
  1786. $result = $this->doQuery($query);
  1787. if($result)
  1788. {
  1789. if(isset($this->afterDeleteFun) && is_callable($this->afterDeleteFun))
  1790. {
  1791. call_user_func($this->afterDeleteFun,$id,$this->primaryKeyCol);
  1792. }
  1793. }
  1794. }
  1795. $this->updateHtml();
  1796. }
  1797. }
  1798. function updateHtml()
  1799. {
  1800. $this->displayTable();
  1801. $this->displayBottomInfo();
  1802. $this->retArr[] = array('layer_id' => 'titleLayer', 'where' => 'innerHTML', 'value' => $this->tableTitle);
  1803. //$this->retArr[] = array('where' => 'javascript', 'value' => "resetScrollTop();");
  1804. }
  1805. function displayFilters()
  1806. {
  1807. $fColumns = array();
  1808. if(stristr($this->permissions,'F'))
  1809. {
  1810. /*
  1811. foreach($this->tableColumns as $col => $info)
  1812. {
  1813. if(stristr($info['perms'],'T') && stristr($info['perms'],'F'))
  1814. {
  1815. $fColumns[] = $col;
  1816. }
  1817. }
  1818. */
  1819. $this->retArr[] = array('where' => 'javascript', 'value' => 'displayFilters(\''.$this->varPrefix.'\');');
  1820. if(strlen($this->filterColId) > 0)
  1821. {
  1822. $this->retArr[] = array('where' => 'javascript', 'value' => '$("'.$this->filterColId.'").focus();');
  1823. }
  1824. }
  1825. }
  1826. function displayBottomInfo()
  1827. {
  1828. if($this->paginationLinks == true)
  1829. {
  1830. $html = '<div style="padding: 5px;">'.$this->langVars->lblPage.' '.$this->getPaginationLinks().'&nbsp;';
  1831. }
  1832. else
  1833. {
  1834. $html = '<div style="padding: 5px;">'.$this->langVars->lblPage.' '.$this->getPageDropDown().'&nbsp;';
  1835. }
  1836. if(stristr($this->permissions,'U'))
  1837. {
  1838. $html .= $this->langVars->lblDisplay.' '.$this->getDispNumDropDown().'&nbsp;';
  1839. }
  1840. if(stristr($this->permissions,'A'))
  1841. {
  1842. $html .= '<button onclick="toAjaxTableEditor(\'add_row\',\'\');">'.$this->langVars->btnAdd.'</button>&nbsp;';
  1843. }
  1844. if(stristr($this->permissions,'E') && !stristr($this->permissions,'I'))
  1845. {
  1846. $html .= '<button onclick="editCopyViewDelete(\''.$this->varPrefix.'\',\'edit_row\');">'.$this->langVars->btnEdit.'</button>&nbsp;';
  1847. }
  1848. if(stristr($this->permissions,'V') && !stristr($this->permissions,'I'))
  1849. {
  1850. $html .= '<button onclick="editCopyViewDelete(\''.$this->varPrefix.'\',\'view_row\');">'.$this->langVars->btnView.'</button>&nbsp;';
  1851. }
  1852. if(stristr($this->permissions,'C') && !stristr($this->permissions,'I'))
  1853. {
  1854. $html .= '<button onclick="editCopyViewDelete(\''.$this->varPrefix.'\',\'copy_mult_rows\');">'.$this->langVars->btnCopy.'</button>&nbsp;';
  1855. }
  1856. if(stristr($this->permissions,'D') && !stristr($this->permissions,'I'))
  1857. {
  1858. $html .= '<button onclick="editCopyViewDelete(\''.$this->varPrefix.'\',\'delete_mult_rows\');">'.$this->langVars->btnDelete.'</button>&nbsp;';
  1859. }
  1860. if(stristr($this->permissions,'X'))
  1861. {
  1862. $html .= '<button onclick="window.location=\''.$_SERVER['PHP_SELF'].'?export=1\'">'.$this->langVars->btnExport.'</button>&nbsp;';
  1863. }
  1864. if(stristr($this->permissions,'H'))
  1865. {
  1866. $html .= '<button onclick="toAjaxTableEditor(\'show_hide_columns\',\'\');">'.$this->langVars->btnShowHide.'</button>&nbsp;';
  1867. }
  1868. if(stristr($this->permissions,'O'))
  1869. {
  1870. $html .= '<button onclick="toAjaxTableEditor(\'order_columns_screen\',\'\');">'.$this->langVars->btnOrderCols.'</button>&nbsp;';
  1871. }
  1872. //if(stristr($this->permissions,'F') && !stristr($this->permissions,'Q') && !stristr($this->permissions,'S'))
  1873. if(stristr($this->permissions,'F'))
  1874. {
  1875. $html .= '<button onclick="handleFilterSearch();">'.$this->langVars->btnFilters.'</button>&nbsp;';
  1876. $html .= '<button onclick="toAjaxTableEditor(\'clear_filters\',\'\');">'.$this->langVars->btnCFilters.'</button>&nbsp;';
  1877. }
  1878. if(stristr($this->permissions,'S') && $this->searchType == 'quick')
  1879. {
  1880. $html .= '<button onclick="toAjaxTableEditor(\'show_advanced_search\',\'\');">'.$this->langVars->btnASearch.'</button>&nbsp;';
  1881. }
  1882. else if(stristr($this->permissions,'Q') && $this->searchType == 'advanced')
  1883. {
  1884. $html .= '<button onclick="toAjaxTableEditor(\'show_quick_search\',\'\');">'.$this->langVars->btnQSearch.'</button>&nbsp;';
  1885. }
  1886. foreach($this->userButtons as $key => $info)
  1887. {
  1888. if(isset($info['button_html']))
  1889. {
  1890. $html .= $info['button_html'].'&nbsp;';
  1891. }
  1892. else if(isset($info['button_info']))
  1893. {
  1894. $html .= '<button '.$info['button_info'].'>'.$info['label'].'</button>&nbsp;';
  1895. }
  1896. else if(isset($info['call_back_fun']) && is_callable($info['call_back_fun']) && stristr($this->permissions,'M'))
  1897. {
  1898. $confirmMsg = isset($info['confirm_msg']) ? $info['confirm_msg'] : '';
  1899. $html .= '<button onclick="userButtonClicked(\''.$this->varPrefix.'\',\''.$key.'\',\''.$confirmMsg.'\')">'.$info['label'].'</button>&nbsp;';
  1900. }
  1901. }
  1902. $html .= '</div>';
  1903. if(stristr($this->permissions,'Q') && $this->searchType == 'quick')
  1904. {
  1905. $html .= $this->langVars->lblSearch.': <input type="text" id="searchString" value="'.$this->searchString.'" size="25" onKeyPress="if(enterPressed(event)){handleSearch(); return false;}" />&nbsp;<button onclick="handleSearch();">'.$this->langVars->lblSearch.'</button>&nbsp;<button onclick="clearSearch();">'.$this->langVars->btnCSearch.'</button>';
  1906. }
  1907. else if(stristr($this->permissions,'S') && $this->searchType == 'advanced')
  1908. {
  1909. $html .= $this->getAdvancedSearchHtml();
  1910. }
  1911. $this->retArr[] = array('layer_id' => 'searchButtonsLayer', 'where' => 'innerHTML', 'value' => $html);
  1912. }
  1913. function displayTable()
  1914. {
  1915. $this->unsetHiddenColumns();
  1916. $this->setColumnOrder();
  1917. $this->setPagingVars();
  1918. $this->formatJoinClause();
  1919. $this->formatSelectClause();
  1920. $this->formatWhereClause();
  1921. $this->setNumResults();
  1922. $this->displayTableHtml();
  1923. $this->displayFilters();
  1924. }
  1925. function formatSelectClause()
  1926. {
  1927. $sets = array();
  1928. foreach($this->tableColumns as $col => $info)
  1929. {
  1930. if(isset($info['join']) && is_array($info['join']))
  1931. {
  1932. $info['join']['display_mask'] = isset($info['join']['display_mask']) ? $info['join']['display_mask'] : $info['join']['alias'].'.'.$info['join']['column'];
  1933. $sets[] = $info['join']['display_mask'].' as '.$this->addTickMarks($col);
  1934. }
  1935. else if(isset($info['display_mask']))
  1936. {
  1937. $sets[] = $info['display_mask'].' as '.$this->addTickMarks($col);
  1938. }
  1939. else
  1940. {
  1941. $sets[] = $this->addTickMarks($this->dbAndTable.'.'.$col);
  1942. }
  1943. }
  1944. $this->selectClause = 'select '.implode(', '.$this->queryLineBreak,$sets).$this->queryLineBreak.'from '.$this->dbAndTable.$this->queryLineBreak;
  1945. }
  1946. function formatJoinClause()
  1947. {
  1948. $sets = array();
  1949. $counter = 0;
  1950. $joins = array();
  1951. foreach($this->tableColumns as $col => $info)
  1952. {
  1953. $saveJoin = true;
  1954. if(isset($info['join']) && is_array($info['join']))
  1955. {
  1956. if($col == $this->primaryKeyCol && isset($info['join']['display_mask']))
  1957. {
  1958. $this->warnings[] = 'Joining on the primary key with a display mask is not supported.';
  1959. }
  1960. // Set join table var, if there is a database concat with table
  1961. $joinTable = !empty($info['join']['db']) ? $info['join']['db'].'.'.$info['join']['table'] : $info['join']['table'];
  1962. // Set join type
  1963. $joinType = isset($info['join']['type']) ? $info['join']['type'] : 'left';
  1964. // Set join column
  1965. $joinColumn = isset($info['join']['real_column']) ? $info['join']['real_column'] : $col;
  1966. // Set foriegn join column
  1967. $foriegnJoinColumn = $info['join']['column'];
  1968. // Set join identifier
  1969. $joinIdentifier = $joinTable.$joinColumn.$foriegnJoinColumn.$joinType;
  1970. if(isset($joins[$joinIdentifier]))
  1971. {
  1972. // If this table was joined before, get the old alias and set save join to false
  1973. $this->tableColumns[$col]['join']['alias'] = $joins[$joinIdentifier];
  1974. $joinAlias = $this->tableColumns[$col]['join']['alias'];
  1975. $saveJoin = false;
  1976. }
  1977. else
  1978. {
  1979. // If this is a new join and there is no user defined alias create one.
  1980. if(!isset($this->tableColumns[$col]['join']['alias']))
  1981. {
  1982. $this->tableColumns[$col]['join']['alias'] = $info['join']['table'].'_'.substr(md5(uniqid(rand(),1)), 3, 5).'_'.$counter;
  1983. }
  1984. $joinAlias = $this->tableColumns[$col]['join']['alias'];
  1985. // Store join and alias
  1986. $joins[$joinIdentifier] = $joinAlias;
  1987. }
  1988. // Replace old table and database with alias (this must be done before the select clause is formatted)
  1989. // Use preg replace so the substitution only happens once on each table/column definition (this is prevent errors when column names contain the table names)
  1990. // Need to use explode/implode for concatenating columns
  1991. if(isset($info['join']['display_mask']))
  1992. {
  1993. $this->tableColumns[$col]['join']['display_mask'] = implode(',',preg_replace('/'.preg_quote($joinTable).'/',preg_quote($joinAlias),explode(',',$info['join']['display_mask']),1));
  1994. $this->tableColumns[$col]['join']['display_mask'] = implode(',',preg_replace('/'.preg_quote($info['join']['table']).'/',preg_quote($joinAlias),explode(',',$info['join']['display_mask']),1));
  1995. }
  1996. else
  1997. {
  1998. $this->tableColumns[$col]['join']['display_mask'] = $info['join']['column'];
  1999. }
  2000. if($saveJoin)
  2001. {
  2002. $sets[] = $joinType.' join '.$this->addTickMarks($joinTable).' as '.$this->addTickMarks($joinAlias).' on '.$this->addTickMarks($this->dbAndTable.'.'.$joinColumn).' = '.$this->addTickMarks($joinAlias.'.'.$foriegnJoinColumn);
  2003. $counter++;
  2004. }
  2005. }
  2006. }
  2007. if(!empty($sets))
  2008. {
  2009. $this->joinClause = implode($this->queryLineBreak,$sets).$this->queryLineBreak;
  2010. }
  2011. }
  2012. function formatWhereClause()
  2013. {
  2014. $sets = array();
  2015. $whereClause = '';
  2016. // Quick search
  2017. if(!empty($this->searchString) && $this->searchType == 'quick')
  2018. {
  2019. foreach($this->tableColumns as $col => $info)
  2020. {
  2021. if(stristr($info['perms'],'Q'))
  2022. {
  2023. if(isset($info['join']) && is_array($info['join']))
  2024. {
  2025. $joinTable = !empty($info['join']['db']) ? $info['join']['db'].'.'.$info['join']['table'] : $info['join']['table'];
  2026. $info['join']['display_mask'] = isset($info['join']['display_mask']) ? $info['join']['display_mask'] : $joinTable.'.'.$info['join']['column'];
  2027. $sets[] = $info['join']['display_mask']." like '%".$this->escapeData($this->searchString)."%'";
  2028. }
  2029. else if(isset($info['display_mask']))
  2030. {
  2031. $sets[] = $info['display_mask']." like '%".$this->escapeData($this->searchString)."%'";
  2032. }
  2033. else
  2034. {
  2035. $sets[] = $this->addTickMarks($this->dbAndTable.'.'.$col)." like '%".$this->escapeData($this->searchString)."%'";
  2036. }
  2037. }
  2038. }
  2039. }
  2040. // Advanced search
  2041. else if(!empty($this->advSearches) && $this->searchType == 'advanced')
  2042. {
  2043. foreach($this->advSearches as $i => $asInfo)
  2044. {
  2045. if(!empty($asInfo['cols']) && stristr($this->tableColumns[$asInfo['cols']]['perms'],'S'))
  2046. {
  2047. if(!isset($this->opts[$asInfo['opts']]))
  2048. {
  2049. // Unknown search operator
  2050. $this->handleHacking();
  2051. continue;
  2052. }
  2053. $asInfo['strs'] = $this->escapeData($asInfo['strs']);
  2054. if(isset($this->tableColumns[$asInfo['cols']]['join']) && is_array($this->tableColumns[$asInfo['cols']]['join']))
  2055. {
  2056. $info = $this->tableColumns[$asInfo['cols']];
  2057. $joinTable = !empty($info['join']['db']) ? $info['join']['db'].'.'.$info['join']['table'] : $info['join']['table'];
  2058. $info['join']['display_mask'] = isset($info['join']['display_mask']) ? $info['join']['display_mask'] : $joinTable.'.'.$info['join']['column'];
  2059. if($asInfo['opts'] == 'like' || $asInfo['opts'] == 'not like')
  2060. $sets[] = $info['join']['display_mask'].' '.$asInfo['opts']." '%".$asInfo['strs']."%'";
  2061. else
  2062. $sets[] = $info['join']['display_mask'].' '.$asInfo['opts']." '".$asInfo['strs']."'";
  2063. }
  2064. else if(isset($this->tableColumns[$asInfo['cols']]['display_mask']))
  2065. {
  2066. $displayMask = $this->tableColumns[$asInfo['cols']]['display_mask'];
  2067. if($asInfo['opts'] == 'like' || $asInfo['opts'] == 'not like')
  2068. $sets[] = $displayMask.' '.$asInfo['opts']." '%".$asInfo['strs']."%'";
  2069. else
  2070. $sets[] = $displayMask.' '.$asInfo['opts']." '".$asInfo['strs']."'";
  2071. }
  2072. else
  2073. {
  2074. if($asInfo['opts'] == 'like' || $asInfo['opts'] == 'not like')
  2075. $sets[] = $this->addTickMarks($this->dbAndTable.'.'.$asInfo['cols']).' '.$asInfo['opts']." '%".$asInfo['strs']."%'";
  2076. else
  2077. $sets[] = $this->addTickMarks($this->dbAndTable.'.'.$asInfo['cols']).' '.$asInfo['opts']." '".$asInfo['strs']."'";
  2078. }
  2079. }
  2080. }
  2081. }
  2082. $glue = 'or';
  2083. if($this->searchType == 'advanced' && $this->matchAll)
  2084. {
  2085. $glue = 'and';
  2086. }
  2087. if(!empty($sets))
  2088. {
  2089. $whereClause .= '('.implode(' '.$glue.$this->queryLineBreak,$sets).')';
  2090. }
  2091. // Format filters
  2092. if(!empty($this->filterSearches))
  2093. {
  2094. $filterSets = array();
  2095. foreach($this->filterSearches as $filterCol => $filterStr)
  2096. {
  2097. if(stristr($this->tableColumns[$filterCol]['perms'],'F'))
  2098. {
  2099. // If it is a join column
  2100. if(isset($this->tableColumns[$filterCol]['join']) && is_array($this->tableColumns[$filterCol]['join']))
  2101. {
  2102. $info = $this->tableColumns[$filterCol];
  2103. $joinTable = !empty($info['join']['db']) ? $info['join']['db'].'.'.$info['join']['table'] : $info['join']['table'];
  2104. $info['join']['display_mask'] = isset($info['join']['display_mask']) ? $info['join']['display_mask'] : $joinTable.'.'.$info['join']['column'];
  2105. $filterSets[] = $info['join']['display_mask']." like '%".$this->escapeData($filterStr)."%'";
  2106. }
  2107. else if(isset($this->tableColumns[$filterCol]['display_mask']))
  2108. {
  2109. $displayMask = $this->tableColumns[$filterCol]['display_mask'];
  2110. $filterSets[] = $displayMask." like '%".$this->escapeData($filterStr)."%'";
  2111. }
  2112. else
  2113. {
  2114. $filterSets[] = $this->addTickMarks($this->dbAndTable.'.'.$filterCol)." like '%".$this->escapeData($filterStr)."%'";
  2115. }
  2116. }
  2117. }
  2118. if(!empty($filterSets))
  2119. {
  2120. $filterClause = '('.implode(' and'.$this->queryLineBreak,$filterSets).')';
  2121. if(strlen($whereClause) > 0)
  2122. {
  2123. $whereClause .= $this->queryLineBreak.' and '.$this->queryLineBreak.$filterClause;
  2124. }
  2125. else
  2126. {
  2127. $whereClause .= $filterClause;
  2128. }
  2129. }
  2130. }
  2131. // Format data filters
  2132. $sets2 = array();
  2133. foreach($this->tableColumns as $col => $info)
  2134. {
  2135. $sets3 = array();
  2136. if(isset($info['data_filters']))
  2137. {
  2138. if(isset($info['join']['display_mask']))
  2139. {
  2140. foreach($info['data_filters']['filters'] as $df)
  2141. {
  2142. $sets3[] = $info['join']['display_mask'].' '.$df;
  2143. }
  2144. }
  2145. else
  2146. {
  2147. $tableName = empty($this->dbName) ? $this->tableName : $this->dbName.'.';
  2148. foreach($info['data_filters']['filters'] as $df)
  2149. {
  2150. $sets3[] = $this->addTickMarks($tableName.'.'.$col).' '.$df;
  2151. }
  2152. }
  2153. if(isset($info['data_filters']['criteria']) && $info['data_filters']['criteria'] == 'any')
  2154. {
  2155. $sets2[] = '('.implode(' or'.$this->queryLineBreak,$sets3).')';
  2156. }
  2157. else
  2158. {
  2159. $sets2[] = implode(' and'.$this->queryLineBreak,$sets3);
  2160. }
  2161. }
  2162. }
  2163. if(!empty($sets2))
  2164. {
  2165. if(empty($whereClause))
  2166. {
  2167. $whereClause .= ' '.implode(' and '.$this->queryLineBreak,$sets2);
  2168. }
  2169. else
  2170. {
  2171. $whereClause .= ' and '.$this->queryLineBreak.implode(' and '.$this->queryLineBreak,$sets2);
  2172. }
  2173. }
  2174. if(!empty($whereClause))
  2175. {
  2176. $this->whereClause .= ' where '.$whereClause.$this->queryLineBreak;
  2177. }
  2178. if(!empty($this->sqlFilters))
  2179. {
  2180. if(empty($this->whereClause))
  2181. {
  2182. $this->whereClause .= 'where '.$this->sqlFilters.$this->queryLineBreak;
  2183. }
  2184. else
  2185. {
  2186. $this->whereClause .= ' and '.$this->sqlFilters.$this->queryLineBreak;
  2187. }
  2188. }
  2189. }
  2190. function setNumResults()
  2191. {
  2192. $this->numResults = 0;
  2193. $query = 'select count(*) as num_results from '.$this->tableName.' '.$this->joinClause.' '.$this->whereClause;
  2194. $result = $this->doQuery($query);
  2195. if($row = mysql_fetch_assoc($result))
  2196. {
  2197. $this->numResults = $row['num_results'];
  2198. }
  2199. }
  2200. function setPagingVars()
  2201. {
  2202. if(isset($_SESSION[$this->varPrefix.'start']))
  2203. {
  2204. $this->start = $_SESSION[$this->varPrefix.'start'];
  2205. }
  2206. if(isset($_SESSION[$this->varPrefix.'obc']))
  2207. {
  2208. $this->orderByColumn = $_SESSION[$this->varPrefix.'obc'];
  2209. }
  2210. if(isset($_SESSION[$this->varPrefix.'aod']))
  2211. {
  2212. $this->ascOrDesc = $_SESSION[$this->varPrefix.'aod'];
  2213. }
  2214. if(isset($_SESSION[$this->varPrefix.'ss']))
  2215. {
  2216. $this->searchString = $_SESSION[$this->varPrefix.'ss'];
  2217. }
  2218. }
  2219. function displayTableHtml()
  2220. {
  2221. $html = '';
  2222. $numRows = 0;
  2223. $this->extraOrderByInfo = empty($this->extraOrderByInfo) ? '' : ', '.$this->extraOrderByInfo;
  2224. // Added tick marks using implod/explode in case custom order by columns have periods in them.
  2225. $query = $this->selectClause.' '.$this->joinClause.' '.$this->whereClause.' order by '.$this->addTickMarks($this->orderByColumn).' '.$this->getAscOrDesc().' '.$this->extraOrderByInfo.' limit '.$this->start.', '.$this->displayNum;
  2226. if($this->viewQuery)
  2227. {
  2228. $this->information[] = '<div id="mateViewQuery" align="left">'.nl2br($query).'</div>';
  2229. }
  2230. $result = $this->doQuery($query);
  2231. $mysqlNumRows = mysql_num_rows($result);
  2232. $html .= '<div><form id="'.$this->varPrefix.'_table_form" style="margin: 0px;"><table '.$this->tableInfo.'>';
  2233. if($mysqlNumRows > 0 || stristr($this->permissions,'F'))
  2234. {
  2235. $html .= '<tr id="'.$this->varPrefix.'_header_row" class="header">';
  2236. if(stristr($this->permissions,'M'))
  2237. {
  2238. $html .= '<td id="select_all_cb_cell" width="40" align="left"><input type="checkbox" id="select_all_cb" onclick="selectCbs(this,\''.$this->varPrefix.'\');" /></td>';
  2239. }
  2240. if(stristr($this->permissions,'I') && $this->iconColPosition == 'first')
  2241. {
  2242. $html .= strlen($this->iconTitle) > 0 ? '<td>'.$this->iconTitle.'</td>' : '<td>&nbsp;</td>';
  2243. }
  2244. foreach($this->tableColumns as $col => $info)
  2245. {
  2246. if(stristr($info['perms'],'T'))
  2247. {
  2248. $colHeaderInfo = isset($info['col_header_info']) ? $info['col_header_info'] : '';
  2249. if(stristr($this->permissions,'F') && stristr($info['perms'],'F'))
  2250. {
  2251. $filterStr = isset($this->filterSearches[$col]) ? $this->filterSearches[$col] : '';
  2252. $filterData = json_encode(array($col,$filterStr));
  2253. //var_dump($filterData);
  2254. //$colHeaderInfo .= ' headers="'.$filterData.'"';
  2255. $colHeaderInfo .= ' filterCol="'.$col.'" filterStr="'.htmlspecialchars($filterStr).'"';
  2256. }
  2257. if($this->orderByColumn == $col)
  2258. {
  2259. list($oppAscOrDesc,$arrow) = $this->ascOrDesc == 'asc' ? array('desc','&uarr;') : array('asc','&darr;');
  2260. $html .= '<td '.$colHeaderInfo.' ><a href="javascript: toAjaxTableEditor(\'order_by_changed\', new Array(\''.$col.'\',\''.$oppAscOrDesc.'\'));">'.$info['display_text'].'</a> '.$arrow.'</td>';
  2261. }
  2262. else
  2263. {
  2264. $html .= '<td '.$colHeaderInfo.' ><a href="javascript: toAjaxTableEditor(\'order_by_changed\', new Array(\''.$col.'\',\'asc\'));">'.$info['display_text'].'</a></td>';
  2265. }
  2266. }
  2267. }
  2268. foreach($this->userColumns as $column)
  2269. {
  2270. $html .= isset($column['title']) ? '<td>'.$column['title'].'</td>' : '<td>&nbsp;</td>';
  2271. }
  2272. if(stristr($this->permissions,'I') && $this->iconColPosition == 'last')
  2273. {
  2274. $html .= strlen($this->iconTitle) > 0 ? '<td>'.$this->iconTitle.'</td>' : '<td>&nbsp;</td>';
  2275. }
  2276. $html .= '</tr>';
  2277. }
  2278. if($mysqlNumRows > 0)
  2279. {
  2280. $bgColor = $this->evenRowColor;
  2281. while($row = mysql_fetch_assoc($result))
  2282. {
  2283. $numRows++;
  2284. $bgColor = $bgColor == $this->oddRowColor ? $this->evenRowColor : $this->oddRowColor;
  2285. $cb = '';
  2286. $rowInfo = '';
  2287. if(stristr($this->permissions,'M'))
  2288. {
  2289. if(is_callable($this->disableMultCbFun) && call_user_func($this->disableMultCbFun,$row))
  2290. {
  2291. //$cb = '<td>&nbsp;</td>';
  2292. $cb = '<td><input class="rowCheckBox" type="checkbox" id="cb_'.$numRows.'" value="'.$row[$this->primaryKeyCol].'" disabled="disabled" /></td>';
  2293. }
  2294. else
  2295. {
  2296. $rowInfo = 'onclick="cellClicked(\''.$numRows.'\');" style="cursor: pointer;"';
  2297. $cb = '<td><input class="rowCheckBox" type="checkbox" id="cb_'.$numRows.'" onclick="checkBoxClicked(this)" value="'.$row[$this->primaryKeyCol].'" /></td>';
  2298. }
  2299. }
  2300. $html .= '<tr bgcolor="'.$bgColor.'" id="row_'.$numRows.'" '.$rowInfo.' class="ajaxRow">'.$cb;
  2301. if(stristr($this->permissions,'I') && $this->iconColPosition == 'first')
  2302. {
  2303. $html .= $this->formatIcons($row[$this->primaryKeyCol],$row);
  2304. }
  2305. foreach($this->tableColumns as $col => $info)
  2306. {
  2307. if(stristr($info['perms'],'T'))
  2308. {
  2309. //print $col;
  2310. $value = $row[$col];
  2311. //print_r( $info) ;
  2312. $tableCellInfo = isset($info['table_cell_info']) ? $info['table_cell_info'] : '';
  2313. if(isset($info['table_fun']) && is_callable($info['table_fun']))
  2314. {
  2315. $value = call_user_func($info['table_fun'],$col,$value,$row);
  2316. }
  2317. $value = strlen(trim($value)) > 0 ? $value : '&nbsp;';
  2318. if(isset($info['sub_str']) && strlen($value) > $info['sub_str'])
  2319. {
  2320. $value = substr($value,0,$info['sub_str']).'...';
  2321. }
  2322. if($this->searchString && $this->useHighlight && $value != '&nbsp;')
  2323. {
  2324. $value = $this->highlightSearchString($this->searchString,$value);
  2325. }
  2326. // Harel Levin 29/07/2011
  2327. if(isset($info['calendar']))
  2328. $value = date('d-m-Y',$value);
  2329. $html .= '<td '.$tableCellInfo.'>'.$value.'</td>';
  2330. }
  2331. }
  2332. foreach($this->userColumns as $column)
  2333. {
  2334. if(isset($column['call_back_fun']) && is_callable($column['call_back_fun']))
  2335. {
  2336. $html .= call_user_func($column['call_back_fun'],$row);
  2337. }
  2338. }
  2339. if(stristr($this->permissions,'I') && $this->iconColPosition == 'last')
  2340. {
  2341. $html .= $this->formatIcons($row[$this->primaryKeyCol],$row);
  2342. }
  2343. $html .= '</tr>';
  2344. }
  2345. $html .= '</table></form></div>';
  2346. }
  2347. else
  2348. {
  2349. $html .= '</table></form></div>';
  2350. $html .= '<div><b>'.$this->langVars->ttlNoRecord.'</b></div>';
  2351. /*
  2352. if(stristr($this->permissions,'F'))
  2353. {
  2354. $this->retArr[] = array('layer_id' => 'filterLayer', 'where' => 'innerHTML', 'value' => '');
  2355. }
  2356. */
  2357. }
  2358. $this->retArr[] = array('layer_id' => 'tableLayer', 'where' => 'innerHTML', 'value' => $html);
  2359. if($this->recordInfo)
  2360. {
  2361. if($this->numResults > 0)
  2362. {
  2363. $end = $this->displayNum + $this->start;
  2364. $end = $end < $this->numResults ? $end : $this->numResults;
  2365. $start = $this->start + 1;
  2366. $recordHtml = '<div>'.sprintf($this->langVars->ttlDispRecs,$start,$end,number_format($this->numResults)).'</div>';
  2367. }
  2368. else
  2369. {
  2370. $recordHtml = '<div>'.$this->langVars->ttlDispNoRecs.'</div>';
  2371. }
  2372. $this->retArr[] = array('layer_id' => 'recordLayer', 'where' => 'innerHTML', 'value' => $recordHtml);
  2373. }
  2374. if(is_callable($this->tableScreenFun))
  2375. {
  2376. call_user_func($this->tableScreenFun);
  2377. }
  2378. }
  2379. function getDispNumDropDown()
  2380. {
  2381. $value = $this->displayNumInc;
  2382. $html = '<select id="display_number" onchange="toAjaxTableEditor(\'display_num_changed\',this.value);">';
  2383. while($value < $this->numResults && $value < $this->maxDispNum)
  2384. {
  2385. $value = $value + $this->displayNumInc;
  2386. if($value == $this->displayNum)
  2387. {
  2388. $html .= '<option value="'.$value.'" selected="selected">'.$value.'</option>';
  2389. }
  2390. else
  2391. {
  2392. $html .= '<option value="'.$value.'">'.$value.'</option>';
  2393. }
  2394. }
  2395. $html .= '</select>';
  2396. return $html;
  2397. }
  2398. function getPageDropDown()
  2399. {
  2400. $pages = array();
  2401. $curPage = round($this->start / $this->displayNum);
  2402. $numPages = ceil($this->numResults / $this->displayNum);
  2403. if($numPages == 0)
  2404. {
  2405. $pages = array(0);
  2406. }
  2407. else if($numPages < $this->showAll)
  2408. {
  2409. $pages = range(1, $numPages);
  2410. }
  2411. else
  2412. {
  2413. for($i = 1; $i <= $this->pageBegin; $i++)
  2414. {
  2415. $pages[] = $i;
  2416. }
  2417. for($i = $numPages - $this->pageEnd; $i <= $numPages; $i++)
  2418. {
  2419. $pages[] = $i;
  2420. }
  2421. $i = $this->pageBegin;
  2422. $x = $numPages - $this->pageEnd;
  2423. $metBoundary = false;
  2424. while($i <= $x)
  2425. {
  2426. if($i >= ($curPage - $this->pageRange) && $i <= ($curPage + $this->pageRange))
  2427. {
  2428. $i++;
  2429. $metBoundary = true;
  2430. }
  2431. else
  2432. {
  2433. $i = $i + floor($numPages / $this->pagePercent);
  2434. if ($i > ($curPage - $this->pageRange) && !$metBoundary)
  2435. {
  2436. $i = $curPage - $this->pageRange;
  2437. }
  2438. }
  2439. if ($i > 0 && $i <= $x)
  2440. {
  2441. $pages[] = $i;
  2442. }
  2443. }
  2444. sort($pages);
  2445. $pages = array_unique($pages);
  2446. }
  2447. $html = '<select id="page_number" onchange="toAjaxTableEditor(\'page_num_changed\',this.value);">';
  2448. foreach($pages as $i)
  2449. {
  2450. $value = ($i - 1) * $this->displayNum;
  2451. $value = $value < 0 ? 0 : $value;
  2452. if(($i - 1) == $curPage)
  2453. {
  2454. $html .= '<option value="'.$value.'" selected="selected" style="font-weight: bold">'.$i.'</option>';
  2455. }
  2456. else
  2457. {
  2458. $html .= '<option value="'.$value.'">'.$i.'</option>';
  2459. }
  2460. }
  2461. $html .= '</select>';
  2462. return $html;
  2463. }
  2464. function getPaginationLinks()
  2465. {
  2466. $pages = array();
  2467. $this->pagePercent = 5;
  2468. $this->pageRange = 2;
  2469. $this->pageBegin = 3;
  2470. $this->pageEnd = 2;
  2471. $this->showAll = 10;
  2472. $curPage = round($this->start / $this->displayNum);
  2473. $numPages = ceil($this->numResults / $this->displayNum);
  2474. if($numPages == 0)
  2475. {
  2476. $pages = array(0);
  2477. }
  2478. else if($numPages < $this->showAll)
  2479. {
  2480. $pages = range(1, $numPages);
  2481. }
  2482. else
  2483. {
  2484. for($i = 1; $i <= $this->pageBegin; $i++)
  2485. {
  2486. $pages[] = $i;
  2487. }
  2488. for($i = $numPages - $this->pageEnd; $i <= $numPages; $i++)
  2489. {
  2490. $pages[] = $i;
  2491. }
  2492. $i = $this->pageBegin;
  2493. $x = $numPages - $this->pageEnd;
  2494. $metBoundary = false;
  2495. while($i <= $x)
  2496. {
  2497. if($i >= ($curPage - $this->pageRange) && $i <= ($curPage + $this->pageRange))
  2498. {
  2499. $i++;
  2500. $metBoundary = true;
  2501. }
  2502. else
  2503. {
  2504. $i = $i + floor($numPages / $this->pagePercent);
  2505. if ($i > ($curPage - $this->pageRange) && !$metBoundary)
  2506. {
  2507. $i = $curPage - $this->pageRange;
  2508. }
  2509. }
  2510. if ($i > 0 && $i <= $x)
  2511. {
  2512. $pages[] = $i;
  2513. }
  2514. }
  2515. sort($pages);
  2516. $pages = array_unique($pages);
  2517. }
  2518. //var_dump($pages);
  2519. $html = '';
  2520. foreach($pages as $i)
  2521. {
  2522. $value = ($i - 1) * $this->displayNum;
  2523. $value = $value < 0 ? 0 : $value;
  2524. if(($i - 1) == $curPage)
  2525. {
  2526. $html .= '<span class="selPage"><a href="javascript: void(0);" value="'.$value.'" onclick="toAjaxTableEditor(\'page_num_changed\','.intval($value).');">'.$i.'</a></span>';
  2527. }
  2528. else
  2529. {
  2530. //var_dump( $value);
  2531. $html .= '<span class="navPage"><a href="javascript: void(0);" value="'.$value.'" onclick="toAjaxTableEditor(\'page_num_changed\','.intval($value).');">'.$i.'</a></span>';
  2532. }
  2533. }
  2534. return $html;
  2535. }
  2536. function getAdvancedSearchHtml()
  2537. {
  2538. $html = '<div id="searchLayer" >';
  2539. for($i = 0; $i < $this->numAdvSearches; $i++)
  2540. {
  2541. $html .= '<p><span id="as_cols_span_'.$i.'"><select id="as_cols_'.$i.'" '.$this->asColumnInfo.'><option value="">'.$this->langVars->lblSelect.'</option>';
  2542. foreach ($this->tableColumns as $col => $info)
  2543. {
  2544. if(stristr($info['perms'],'S'))
  2545. {
  2546. if(isset($this->advSearches[$i]['cols']) && $this->advSearches[$i]['cols'] == $col)
  2547. {
  2548. $html .= '<option value="'.$col.'" selected>'.$info['display_text'].'</option>';
  2549. }
  2550. else
  2551. {
  2552. $html .= '<option value="'.$col.'">'.$info['display_text'].'</option>';
  2553. }
  2554. }
  2555. }
  2556. $html .= '</select></span>&nbsp;<span id="as_opts_span_'.$i.'"><select id="as_opts_'.$i.'">';
  2557. foreach ($this->opts as $sign => $text)
  2558. {
  2559. if(isset($this->advSearches[$i]['opts']) && $this->advSearches[$i]['opts'] == $sign)
  2560. {
  2561. $html .= '<option value="'.$sign.'" selected>'.$text.'</option>';
  2562. }
  2563. else
  2564. {
  2565. $html .= '<option value="'.$sign.'">'.$text.'</option>';
  2566. }
  2567. }
  2568. $html .= '</select></span>&nbsp;';
  2569. if(isset($this->advSearches[$i]['strs']))
  2570. {
  2571. $html .= '<span id="as_strs_span_'.$i.'"><input type="text" id="as_strs_'.$i.'" size="28" value="'.$this->advSearches[$i]['strs'].'" onKeyPress="if(enterPressed(event)){handleAdvancedSearch(\''.$this->numAdvSearches.'\'); return false;}" /></span></p>';
  2572. }
  2573. else
  2574. {
  2575. $html .= '<span id="as_strs_span_'.$i.'"><input type="text" id="as_strs_'.$i.'" size="28" value="" onKeyPress="if(enterPressed(event)){handleAdvancedSearch(\''.$this->numAdvSearches.'\'); return false;}" /></span></p>';
  2576. }
  2577. }
  2578. $allChecked = $this->matchAll ? 'checked="checked"' : '';
  2579. $anyChecked = $this->matchAll ? '' : 'checked="checked"';
  2580. $html .= '<p align="center">';
  2581. $html .= '<p>'.$this->langVars->lblMatch;
  2582. $html .= '<input type="radio" name="match" value="all" id="match_all" '.$allChecked.' onclick="toAjaxTableEditor(\'match_all\',\'\');"> <label for="match_all">'.$this->langVars->lblAllCrit.'</label>';
  2583. $html .= '<input type="radio" name="match" value="any" id="match_any" '.$anyChecked.' onclick="toAjaxTableEditor(\'match_any\',\'\');"> <label for="match_any">'.$this->langVars->lblAnyCrit.'</label>';
  2584. $html .= '</p>';
  2585. $html .= '<button onclick="handleAdvancedSearch(\''.$this->numAdvSearches.'\');">'.$this->langVars->lblSearch.'</button>&nbsp;<button onclick="toAjaxTableEditor(\'clear_adv_search\',\'\');">'.$this->langVars->btnCSearch.'</button>&nbsp;';
  2586. $html .= '<button onclick="toAjaxTableEditor(\'add_adv_search\',\'\');">'.$this->langVars->btnAddCrit.'</button>';
  2587. $html .= '<input type="hidden" id="num_adv_searches" value="'.$this->numAdvSearches.'" />';
  2588. $html .= '</p></div>';
  2589. return $html;
  2590. }
  2591. function highlightSearchString($needle,$haystack)
  2592. {
  2593. if(!empty($needle))
  2594. {
  2595. $highlight = '<span style="'.$this->highlightHash.'">\1</span>';
  2596. //$pattern = '#(%s)#i';
  2597. $pattern = '#(?!<.*?)(%s)(?![^<>]*?>)#i';
  2598. $regex = sprintf($pattern, preg_quote($needle));
  2599. return preg_replace($regex,$highlight,$haystack);
  2600. }
  2601. else
  2602. {
  2603. return $haystack;
  2604. }
  2605. }
  2606. function getSelect($query,$colName,$defaultValue = '', $otherInfo = '', $multiple = false)
  2607. {
  2608. $html = '<select '.($multiple?'multiple="multiple" ':'').'id="'.$colName.'" name="'.$colName.'" '.$otherInfo.'><option value="" selected="selected">'.$this->langVars->lblSelect.'</option>';
  2609. $result = $this->doQuery($query);
  2610. while($row = mysql_fetch_array($result,MYSQL_NUM))
  2611. {
  2612. if($row[0] == $defaultValue)
  2613. $html .= '<option value="'.$row[0].'" selected="selected">'.$row[1].'</option>';
  2614. else
  2615. $html .= '<option value="'.$row[0].'">'.$row[1].'</option>';
  2616. }
  2617. $html .= '</select>';
  2618. return $html;
  2619. }
  2620. function getSelectFromArray($selArr,$nameAndId,$defaultValue = '', $otherInfo = '', $multiple = false)
  2621. {
  2622. $html = '<select '.($multiple?'multiple="multiple" ':'').'id="'.$nameAndId.'" name="'.$nameAndId.'" '.$otherInfo.'><option value="" selected="selected">'.$this->langVars->lblSelect.'</option>';
  2623. foreach($selArr as $value => $display)
  2624. {
  2625. if($value == $defaultValue)
  2626. $html .= '<option value="'.$value.'" selected="selected">'.$display.'</option>';
  2627. else
  2628. $html .= '<option value="'.$value.'">'.$display.'</option>';
  2629. }
  2630. $html .= '</select>';
  2631. return $html;
  2632. }
  2633. function addTickMarks($string)
  2634. {
  2635. return '`'.implode('`.`',explode('.',$string)).'`';
  2636. }
  2637. /* Thanks to Otto Ebeling and Max Technologies Ltd for helping with the security functions. */
  2638. function getAscOrDesc()
  2639. {
  2640. if ($this->ascOrDesc == 'desc')
  2641. {
  2642. return 'desc';
  2643. }
  2644. else
  2645. {
  2646. return 'asc';
  2647. }
  2648. }
  2649. function hasRightsToRow($id)
  2650. {
  2651. $originalColumns = $this->tableColumns;
  2652. // Unset the hidden columns here because if there are joins there can be more or less rows returned.
  2653. $this->unsetHiddenColumns();
  2654. $this->formatJoinClause();
  2655. $this->formatSelectClause();
  2656. $this->formatWhereClause();
  2657. $whereClause = strlen($this->whereClause) > 0 ? "and ".$this->tableName.'.'.$this->primaryKeyCol." = '$id'" : "where ".$this->tableName.'.'.$this->primaryKeyCol." = '$id'";
  2658. $query = $this->selectClause.' '.$this->joinClause.' '.$this->whereClause.' '.$whereClause;
  2659. $this->selectClause = '';
  2660. $this->whereClause = '';
  2661. $this->joinClause = '';
  2662. $this->tableColumns = $originalColumns;
  2663. $result = $this->doQuery($query);
  2664. if($row = mysql_fetch_assoc($result))
  2665. {
  2666. return true;
  2667. }
  2668. else
  2669. {
  2670. return false;
  2671. }
  2672. }
  2673. function hasRightsToRows($idArr)
  2674. {
  2675. $originalColumns = $this->tableColumns;
  2676. // Unset the hidden columns here because if there are joins there can be more or less rows returned.
  2677. $this->unsetHiddenColumns();
  2678. $this->formatJoinClause();
  2679. $this->formatSelectClause();
  2680. $this->formatWhereClause();
  2681. $whereClause = strlen($this->whereClause) > 0 ? "and ".$this->tableName.'.'.$this->primaryKeyCol." in ('".implode("','",$idArr)."')" : "where ".$this->tableName.'.'.$this->primaryKeyCol." in ('".implode("','",$idArr)."')";
  2682. $query = $this->selectClause.' '.$this->joinClause.' '.$this->whereClause.' '.$whereClause;
  2683. $this->selectClause = '';
  2684. $this->whereClause = '';
  2685. $this->joinClause = '';
  2686. $this->tableColumns = $originalColumns;
  2687. $result = $this->doQuery($query);
  2688. if(mysql_num_rows($result) == count($idArr))
  2689. {
  2690. return true;
  2691. }
  2692. else
  2693. {
  2694. return false;
  2695. }
  2696. }
  2697. function handleHacking()
  2698. {
  2699. if(isset($this->handleHackingFun) && is_callable($this->handleHackingFun))
  2700. {
  2701. call_user_func($this->handleHackingFun);
  2702. }
  2703. exit();
  2704. }
  2705. /* End security functions */
  2706. function handleFileUpload()
  2707. {
  2708. $valErrorHtml = '';
  2709. $valErrors = array();
  2710. $fileColumns = array('name','size','type');
  2711. $id = isset($_POST[$this->inputIdPrefix.$this->primaryKeyCol]) ? unserialize($_POST[$this->inputIdPrefix.$this->primaryKeyCol]) : '';
  2712. $displayEdit = isset($_POST[$this->inputIdPrefix.'_mate_was_inserting_new_row']) ? true : false;
  2713. foreach($this->tableColumns as $col => $colInfo)
  2714. {
  2715. $sets = array();
  2716. // Make sure the column is defined as a file upload column and that a file was uploaded
  2717. if(isset($colInfo['file_upload']) && isset($_FILES[$col]) && isset($_FILES[$col]['name']) && strlen($_FILES[$col]['name']) > 0)
  2718. {
  2719. if(!stristr($colInfo['perms'],'E'))
  2720. {
  2721. $this->handleHacking();
  2722. }
  2723. if(isset($colInfo['file_upload']['upload_fun']) && is_callable($colInfo['file_upload']['upload_fun']))
  2724. {
  2725. $valErrors = call_user_func($colInfo['file_upload']['upload_fun'],$col,$_FILES[$col]);
  2726. }
  2727. else
  2728. {
  2729. if(isset($colInfo['val_fun']) && is_callable($colInfo['val_fun']))
  2730. {
  2731. $valErrors = call_user_func($colInfo['val_fun'],$col,$_FILES[$col]);
  2732. }
  2733. $maxSize = isset($colInfo['file_upload']['max_size']) ? $colInfo['file_upload']['max_size'] : $this->defaultUploadMaxSize;
  2734. if($_FILES[$col]['size'] > $maxSize)
  2735. {
  2736. $valErrors[] = sprintf($this->langVars->errFileSize,$colInfo['display_text']);
  2737. }
  2738. if(count($valErrors) == 0)
  2739. {
  2740. foreach($fileColumns as $fileCol)
  2741. {
  2742. if(isset($colInfo['file_upload'][$fileCol]))
  2743. {
  2744. $sets[] = $colInfo['file_upload'][$fileCol]." = '".$this->escapeData($_FILES[$col][$fileCol])."'";
  2745. }
  2746. }
  2747. $sets[] = $col." = '".addslashes(file_get_contents($_FILES[$col]['tmp_name']))."'";
  2748. if(is_array($id) && count($id) > 0)
  2749. {
  2750. if(!$this->hasRightsToRows($id))
  2751. {
  2752. $this->handleHacking();
  2753. }
  2754. $query = "update $this->tableName set ".implode(', ',$sets)." where $this->primaryKeyCol in ('".implode("','",$id)."')";
  2755. }
  2756. else
  2757. {
  2758. if(!$this->hasRightsToRow($id))
  2759. {
  2760. $this->handleHacking();
  2761. }
  2762. $query = "update $this->tableName set ".implode(', ',$sets)." where $this->primaryKeyCol = $id";
  2763. }
  2764. $result = $this->doQuery($query);
  2765. }
  2766. }
  2767. }
  2768. else if(isset($colInfo['file_upload']) && isset($colInfo['req']) && $colInfo['req'])
  2769. {
  2770. $valErrors[] = sprintf($this->langVars->errFileReq,$colInfo['display_text']);
  2771. }
  2772. if($displayEdit && isset($colInfo['file_upload']['after_add_fun']) && is_callable($colInfo['file_upload']['after_add_fun']))
  2773. {
  2774. call_user_func($colInfo['file_upload']['after_add_fun'],$id,$_FILES[$col]);
  2775. }
  2776. if(!$displayEdit && isset($colInfo['file_upload']['after_edit_fun']) && is_callable($colInfo['file_upload']['after_edit_fun']))
  2777. {
  2778. call_user_func($colInfo['file_upload']['after_edit_fun'],$id,$_FILES[$col]);
  2779. }
  2780. }
  2781. $displayEditParam = $displayEdit ? 'true' : 'false';
  2782. $idParam = $displayEdit ? $id : '';
  2783. if(is_array($valErrors) && count($valErrors) > 0)
  2784. {
  2785. $valErrorHtml = '<span style="color: red;">'.implode('<br />',$valErrors).'</span>';
  2786. }
  2787. echo '<script type="text/javascript">parent.uploadFinished(\''.str_replace("'","\'",$valErrorHtml).'\',\''.$idParam.'\','.$displayEditParam.');</script>';
  2788. }
  2789. function setLangVars()
  2790. {
  2791. if(class_exists('LangVars'))
  2792. {
  2793. $this->langVars = new LangVars();
  2794. }
  2795. else
  2796. {
  2797. $langVarsPath = str_replace('AjaxTableEditor.php','lang/LangVars-en.php',__file__);
  2798. if(is_file($langVarsPath))
  2799. {
  2800. require_once($langVarsPath);
  2801. $this->langVars = new LangVars();
  2802. }
  2803. else
  2804. {
  2805. $this->warnings[] = 'The language file could not be found.';
  2806. }
  2807. }
  2808. }
  2809. function execute_transfer(){
  2810. $this->retArr[] = array('layer_id' => '', 'where' => '', 'value' => 'fff');
  2811. if(is_callable($this->tableScreenFun))
  2812. {
  2813. call_user_func($this->tableScreenFun);
  2814. }
  2815. }
  2816. function AjaxTableEditor($tableName,$primaryCol,$errorFun,$permissions,$tableColumns)
  2817. {
  2818. $this->setLangVars();
  2819. $this->tableName = $tableName;
  2820. $this->primaryKeyCol = $primaryCol;
  2821. $this->errorFun = $errorFun;
  2822. $this->permissions = $permissions;
  2823. $this->tableColumns = $tableColumns;
  2824. $this->setDefaults();
  2825. }
  2826. }
  2827. function dateFormat($col,$value,$row){
  2828. if (preg_match('/time$/',$col)&& $value>100000000){ // the $value > 10000000 is to prevent the function convert a date (like in coinstar payments)
  2829. return date("d/m/Y",$value);
  2830. }
  2831. if (preg_match('/sum$/',$col)||preg_match('/amount/',$col)||preg_match('/credit$/',$col)||preg_match('/debit$/',$col)||preg_match('/ballance$/',$col)){ // the $value > 10000000 is to prevent the function convert a date (like in coinstar payments)
  2832. return number_format($value, 2, '.', ',');
  2833. }
  2834. else {
  2835. return $value ;
  2836. }
  2837. }
  2838. ?>