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

/php/AjaxTableEditorFucked.php

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