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

/osj-osj2dpostsdelete.php

http://osjobber.googlecode.com/
PHP | 523 lines | 392 code | 61 blank | 70 comment | 67 complexity | 83738c879b67e13fab6e5f96b3aa6624 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0
  1. <?php
  2. define("EW_PAGE_ID", "delete", TRUE); // Page ID
  3. define("EW_TABLE_NAME", 'osj-posts', TRUE);
  4. ?>
  5. <?php
  6. session_start(); // Initialize session data
  7. ob_start(); // Turn on output buffering
  8. ?>
  9. <?php include "osj-ewcfg50.php" ?>
  10. <?php include "osj-ewmysql50.php" ?>
  11. <?php include "osj-phpfn50.php" ?>
  12. <?php include "osj-osj2dpostsinfo.php" ?>
  13. <?php include "osj-userfn50.php" ?>
  14. <?php include "osj-osj2dusersinfo.php" ?>
  15. <?php
  16. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
  17. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
  18. header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1
  19. header("Cache-Control: post-check=0, pre-check=0", false);
  20. header("Pragma: no-cache"); // HTTP/1.0
  21. ?>
  22. <?php
  23. // Open connection to the database
  24. $conn = ew_Connect();
  25. ?>
  26. <?php
  27. $Security = new cAdvancedSecurity();
  28. ?>
  29. <?php
  30. if (!$Security->IsLoggedIn()) $Security->AutoLogin();
  31. $Security->LoadCurrentUserLevel('osj-posts');
  32. if (!$Security->IsLoggedIn()) {
  33. $Security->SaveLastUrl();
  34. Page_Terminate("osj-login.php");
  35. }
  36. if (!$Security->CanDelete()) {
  37. $Security->SaveLastUrl();
  38. Page_Terminate("osj-osj2dpostslist.php");
  39. }
  40. if ($Security->IsLoggedIn() && $Security->CurrentUserID() == "") {
  41. $_SESSION[EW_SESSION_MESSAGE] = "You do not have the right permission to view the page";
  42. Page_Terminate("osj-login.php");
  43. }
  44. ?>
  45. <?php
  46. // Common page loading event (in userfn*.php)
  47. Page_Loading();
  48. ?>
  49. <?php
  50. // Page load event, used in current page
  51. Page_Load();
  52. ?>
  53. <?php
  54. $osj2Dposts->Export = @$_GET["export"]; // Get export parameter
  55. $sExport = $osj2Dposts->Export; // Get export parameter, used in header
  56. $sExportFile = $osj2Dposts->TableVar; // Get export file, used in header
  57. ?>
  58. <?php
  59. // Load Key Parameters
  60. $sKey = "";
  61. $bSingleDelete = TRUE; // Initialize as single delete
  62. $arRecKeys = array();
  63. $nKeySelected = 0; // Initialize selected key count
  64. $sFilter = "";
  65. if (@$_GET["post_id"] <> "") {
  66. $osj2Dposts->post_id->setQueryStringValue($_GET["post_id"]);
  67. if (!is_numeric($osj2Dposts->post_id->QueryStringValue)) {
  68. Page_Terminate($osj2Dposts->getReturnUrl()); // Prevent sql injection, exit
  69. }
  70. $sKey .= $osj2Dposts->post_id->QueryStringValue;
  71. } else {
  72. $bSingleDelete = FALSE;
  73. }
  74. if ($bSingleDelete) {
  75. $nKeySelected = 1; // Set up key selected count
  76. $arRecKeys[0] = $sKey;
  77. } else {
  78. if (isset($_POST["key_m"])) { // Key in form
  79. $nKeySelected = count($_POST["key_m"]); // Set up key selected count
  80. $arRecKeys = ew_StripSlashes($_POST["key_m"]);
  81. }
  82. }
  83. if ($nKeySelected <= 0) Page_Terminate($osj2Dposts->getReturnUrl()); // No key specified, exit
  84. // Build filter
  85. foreach ($arRecKeys as $sKey) {
  86. $sFilter .= "(";
  87. // Set up key field
  88. $sKeyFld = $sKey;
  89. if (!is_numeric($sKeyFld)) {
  90. Page_Terminate($osj2Dposts->getReturnUrl()); // Prevent sql injection, exit
  91. }
  92. $sFilter .= "`post_id`=" . ew_AdjustSql($sKeyFld) . " AND ";
  93. if (substr($sFilter, -5) == " AND ") $sFilter = substr($sFilter, 0, strlen($sFilter)-5) . ") OR ";
  94. }
  95. if (substr($sFilter, -4) == " OR ") $sFilter = substr($sFilter, 0, strlen($sFilter)-4);
  96. // Set up filter (Sql Where Clause) and get Return Sql
  97. // Sql constructor in osj2Dposts class, osj2Dpostsinfo.php
  98. $osj2Dposts->CurrentFilter = $sFilter;
  99. // Get action
  100. if (@$_POST["a_delete"] <> "") {
  101. $osj2Dposts->CurrentAction = $_POST["a_delete"];
  102. } else {
  103. $osj2Dposts->CurrentAction = "I"; // Display record
  104. }
  105. switch ($osj2Dposts->CurrentAction) {
  106. case "D": // Delete
  107. $osj2Dposts->SendEmail = TRUE; // Send email on delete success
  108. if (DeleteRows()) { // delete rows
  109. $_SESSION[EW_SESSION_MESSAGE] = "Delete Successful"; // Set up success message
  110. Page_Terminate($osj2Dposts->getReturnUrl()); // Return to caller
  111. }
  112. }
  113. // Load records for display
  114. $rs = LoadRecordset();
  115. $nTotalRecs = $rs->RecordCount(); // Get record count
  116. if ($nTotalRecs <= 0) { // No record found, exit
  117. $rs->Close();
  118. Page_Terminate($osj2Dposts->getReturnUrl()); // Return to caller
  119. }
  120. ?>
  121. <?php include "osj-header.php" ?>
  122. <script type="text/javascript">
  123. <!--
  124. var EW_PAGE_ID = "delete"; // Page id
  125. var EW_SHOW_HIGHLIGHT = "Show highlight";
  126. var EW_HIDE_HIGHLIGHT = "Hide highlight";
  127. //-->
  128. </script>
  129. <script language="JavaScript" type="text/javascript">
  130. <!--
  131. // Write your client script here, no need to add script tags.
  132. // To include another .js script, use:
  133. // ew_ClientScriptInclude("my_javascript.js");
  134. //-->
  135. </script>
  136. <p><span class="phpmaker">Delete from TABLE: Posts<br><br><a href="<?php echo $osj2Dposts->getReturnUrl() ?>">Go Back</a></span></p>
  137. <?php
  138. if (@$_SESSION[EW_SESSION_MESSAGE] <> "") {
  139. ?>
  140. <p><span class="ewmsg"><?php echo $_SESSION[EW_SESSION_MESSAGE] ?></span></p>
  141. <?php
  142. $_SESSION[EW_SESSION_MESSAGE] = ""; // Clear message
  143. }
  144. ?>
  145. <form action="osj-osj2dpostsdelete.php" method="post">
  146. <p>
  147. <input type="hidden" name="a_delete" id="a_delete" value="D">
  148. <?php foreach ($arRecKeys as $sKey) { ?>
  149. <input type="hidden" name="key_m[]" id="key_m[]" value="<?php echo ew_HtmlEncode($sKey) ?>">
  150. <?php } ?>
  151. <table class="ewTable">
  152. <tr class="ewTableHeader">
  153. <td valign="top">Title</td>
  154. <td valign="top">Description</td>
  155. </tr>
  156. <?php
  157. $nRecCount = 0;
  158. $i = 0;
  159. while (!$rs->EOF) {
  160. $nRecCount++;
  161. // Set row class and style
  162. $osj2Dposts->CssClass = "ewTableRow";
  163. $osj2Dposts->CssStyle = "";
  164. // Display alternate color for rows
  165. if ($nRecCount % 2 <> 1) {
  166. $osj2Dposts->CssClass = "ewTableAltRow";
  167. }
  168. // Get the field contents
  169. LoadRowValues($rs);
  170. // Render row value
  171. $osj2Dposts->RowType = EW_ROWTYPE_VIEW; // view
  172. RenderRow();
  173. ?>
  174. <tr<?php echo $osj2Dposts->DisplayAttributes() ?>>
  175. <td<?php echo $osj2Dposts->post_title->CellAttributes() ?>>
  176. <?php if ($osj2Dposts->post_title->HrefValue <> "") { ?>
  177. <a href="osj-osj2dpostsview.php?post_id=<?php echo $osj2Dposts->post_title->HrefValue ?>"><div<?php echo $osj2Dposts->post_title->ViewAttributes() ?>><?php echo $osj2Dposts->post_title->ViewValue ?></div></a>
  178. <?php } else { ?>
  179. <div<?php echo $osj2Dposts->post_title->ViewAttributes() ?>><?php echo $osj2Dposts->post_title->ViewValue ?></div>
  180. <?php } ?>
  181. </td>
  182. <td<?php echo $osj2Dposts->post_description->CellAttributes() ?>>
  183. <div<?php echo $osj2Dposts->post_description->ViewAttributes() ?>><?php echo $osj2Dposts->post_description->ViewValue ?></div>
  184. </td>
  185. </tr>
  186. <?php
  187. $rs->MoveNext();
  188. }
  189. $rs->Close();
  190. ?>
  191. </table>
  192. <p>
  193. <input type="submit" name="Action" id="Action" value="Confirm Delete">
  194. </form>
  195. <script language="JavaScript" type="text/javascript">
  196. <!--
  197. // Write your table-specific startup script here
  198. // document.write("page loaded");
  199. //-->
  200. </script>
  201. <?php include "osj-footer.php" ?>
  202. <?php
  203. // If control is passed here, simply terminate the page without redirect
  204. Page_Terminate();
  205. // -----------------------------------------------------------------
  206. // Subroutine Page_Terminate
  207. // - called when exit page
  208. // - clean up connection and objects
  209. // - if url specified, redirect to url, otherwise end response
  210. function Page_Terminate($url = "") {
  211. global $conn;
  212. // Page unload event, used in current page
  213. Page_Unload();
  214. // Global page unloaded event (in userfn*.php)
  215. Page_Unloaded();
  216. // Close Connection
  217. $conn->Close();
  218. // Go to url if specified
  219. if ($url <> "") {
  220. ob_end_clean();
  221. header("Location: $url");
  222. }
  223. exit();
  224. }
  225. ?>
  226. <?php
  227. // ------------------------------------------------
  228. // Function DeleteRows
  229. // - Delete Records based on current filter
  230. function DeleteRows() {
  231. global $conn, $Security, $osj2Dposts;
  232. $DeleteRows = TRUE;
  233. $sWrkFilter = $osj2Dposts->CurrentFilter;
  234. if ($Security->CurrentUserID() <> "" && !$Security->IsAdmin()) { // Non system admin
  235. $sWrkFilter = $osj2Dposts->AddUserIDFilter($sWrkFilter, $Security->CurrentUserID()); // Add User ID filter
  236. }
  237. // Set up filter (Sql Where Clause) and get Return Sql
  238. // Sql constructor in osj2Dposts class, osj2Dpostsinfo.php
  239. $osj2Dposts->CurrentFilter = $sWrkFilter;
  240. $sSql = $osj2Dposts->SQL();
  241. $conn->raiseErrorFn = 'ew_ErrorFn';
  242. $rs = $conn->Execute($sSql);
  243. $conn->raiseErrorFn = '';
  244. if ($rs === FALSE) {
  245. return FALSE;
  246. } elseif ($rs->EOF) {
  247. $_SESSION[EW_SESSION_MESSAGE] = "No records found"; // No record found
  248. $rs->Close();
  249. return FALSE;
  250. }
  251. $conn->BeginTrans();
  252. WriteAuditTrailDummy("*** batch delete begin ***"); // Batch delete begin
  253. // Clone old rows
  254. $rsold = ($rs) ? $rs->GetRows() : array();
  255. if ($rs) $rs->Close();
  256. // Call row deleting event
  257. if ($DeleteRows) {
  258. foreach ($rsold as $row) {
  259. $DeleteRows = $osj2Dposts->Row_Deleting($row);
  260. if (!$DeleteRows) break;
  261. }
  262. }
  263. if ($DeleteRows) {
  264. $sKey = "";
  265. foreach ($rsold as $row) {
  266. $sThisKey = "";
  267. if ($sThisKey <> "") $sThisKey .= EW_COMPOSITE_KEY_SEPARATOR;
  268. $sThisKey .= $row['post_id'];
  269. $conn->raiseErrorFn = 'ew_ErrorFn';
  270. $DeleteRows = $conn->Execute($osj2Dposts->DeleteSQL($row)); // Delete
  271. $conn->raiseErrorFn = '';
  272. if ($DeleteRows === FALSE)
  273. break;
  274. if ($sKey <> "") $sKey .= ", ";
  275. $sKey .= $sThisKey;
  276. }
  277. } else {
  278. // Set up error message
  279. if ($osj2Dposts->CancelMessage <> "") {
  280. $_SESSION[EW_SESSION_MESSAGE] = $osj2Dposts->CancelMessage;
  281. $osj2Dposts->CancelMessage = "";
  282. } else {
  283. $_SESSION[EW_SESSION_MESSAGE] = "Delete cancelled";
  284. }
  285. }
  286. if ($DeleteRows) {
  287. $conn->CommitTrans(); // Commit the changes
  288. if ($DeleteRows) {
  289. foreach ($rsold as $row) {
  290. WriteAuditTrailOnDelete($row);
  291. }
  292. }
  293. WriteAuditTrailDummy("*** batch delete success ***"); // Batch delete success
  294. } else {
  295. $conn->RollbackTrans(); // Rollback changes
  296. WriteAuditTrailDummy("*** batch delete rollback ***"); // Batch delete rollback
  297. }
  298. // Call recordset deleted event
  299. if ($DeleteRows) {
  300. foreach ($rsold as $row) {
  301. $osj2Dposts->Row_Deleted($row);
  302. }
  303. }
  304. return $DeleteRows;
  305. }
  306. ?>
  307. <?php
  308. // Load recordset
  309. function LoadRecordset($offset = -1, $rowcnt = -1) {
  310. global $conn, $osj2Dposts;
  311. // Call Recordset Selecting event
  312. $osj2Dposts->Recordset_Selecting($osj2Dposts->CurrentFilter);
  313. // Load list page sql
  314. $sSql = $osj2Dposts->SelectSQL();
  315. if ($offset > -1 && $rowcnt > -1) $sSql .= " LIMIT $offset, $rowcnt";
  316. // Load recordset
  317. $conn->raiseErrorFn = 'ew_ErrorFn';
  318. $rs = $conn->Execute($sSql);
  319. $conn->raiseErrorFn = '';
  320. // Call Recordset Selected event
  321. $osj2Dposts->Recordset_Selected($rs);
  322. return $rs;
  323. }
  324. ?>
  325. <?php
  326. // Load row based on key values
  327. function LoadRow() {
  328. global $conn, $Security, $osj2Dposts;
  329. $sFilter = $osj2Dposts->SqlKeyFilter();
  330. if (!is_numeric($osj2Dposts->post_id->CurrentValue)) {
  331. return FALSE; // Invalid key, exit
  332. }
  333. $sFilter = str_replace("@post_id@", ew_AdjustSql($osj2Dposts->post_id->CurrentValue), $sFilter); // Replace key value
  334. if ($Security->CurrentUserID() <> "" && !$Security->IsAdmin()) { // Non system admin
  335. $sFilter = $osj2Dposts->AddUserIDFilter($sFilter, $Security->CurrentUserID()); // Add User ID filter
  336. }
  337. // Call Row Selecting event
  338. $osj2Dposts->Row_Selecting($sFilter);
  339. // Load sql based on filter
  340. $osj2Dposts->CurrentFilter = $sFilter;
  341. $sSql = $osj2Dposts->SQL();
  342. if ($rs = $conn->Execute($sSql)) {
  343. if ($rs->EOF) {
  344. $LoadRow = FALSE;
  345. } else {
  346. $LoadRow = TRUE;
  347. $rs->MoveFirst();
  348. LoadRowValues($rs); // Load row values
  349. // Call Row Selected event
  350. $osj2Dposts->Row_Selected($rs);
  351. }
  352. $rs->Close();
  353. } else {
  354. $LoadRow = FALSE;
  355. }
  356. return $LoadRow;
  357. }
  358. // Load row values from recordset
  359. function LoadRowValues(&$rs) {
  360. global $osj2Dposts;
  361. $osj2Dposts->post_id->setDbValue($rs->fields('post_id'));
  362. $osj2Dposts->user_id->setDbValue($rs->fields('user_id'));
  363. $osj2Dposts->post_creation_date->setDbValue($rs->fields('post_creation_date'));
  364. $osj2Dposts->post_title->setDbValue($rs->fields('post_title'));
  365. $osj2Dposts->post_description->setDbValue($rs->fields('post_description'));
  366. $osj2Dposts->post_email->setDbValue($rs->fields('post_email'));
  367. $osj2Dposts->post_categories->setDbValue($rs->fields('post_categories'));
  368. $osj2Dposts->post_url->setDbValue($rs->fields('post_url'));
  369. $osj2Dposts->post_edit_date->setDbValue($rs->fields('post_edit_date'));
  370. }
  371. ?>
  372. <?php
  373. // Render row values based on field settings
  374. function RenderRow() {
  375. global $conn, $Security, $osj2Dposts;
  376. // Call Row Rendering event
  377. $osj2Dposts->Row_Rendering();
  378. // Common render codes for all row types
  379. // post_title
  380. $osj2Dposts->post_title->CellCssStyle = "";
  381. $osj2Dposts->post_title->CellCssClass = "";
  382. // post_description
  383. $osj2Dposts->post_description->CellCssStyle = "";
  384. $osj2Dposts->post_description->CellCssClass = "";
  385. if ($osj2Dposts->RowType == EW_ROWTYPE_VIEW) { // View row
  386. // post_title
  387. $osj2Dposts->post_title->ViewValue = $osj2Dposts->post_title->CurrentValue;
  388. $osj2Dposts->post_title->CssStyle = "";
  389. $osj2Dposts->post_title->CssClass = "";
  390. $osj2Dposts->post_title->ViewCustomAttributes = "";
  391. // post_description
  392. $osj2Dposts->post_description->ViewValue = $osj2Dposts->post_description->CurrentValue;
  393. if (!is_null($osj2Dposts->post_description->ViewValue)) $osj2Dposts->post_description->ViewValue = str_replace("\n", "<br>", $osj2Dposts->post_description->ViewValue);
  394. $osj2Dposts->post_description->CssStyle = "";
  395. $osj2Dposts->post_description->CssClass = "";
  396. $osj2Dposts->post_description->ViewCustomAttributes = "";
  397. // post_title
  398. if (!is_null($osj2Dposts->post_id->CurrentValue)) {
  399. $osj2Dposts->post_title->HrefValue = ((!empty($osj2Dposts->post_id->ViewValue)) ? $osj2Dposts->post_id->ViewValue : $osj2Dposts->post_id->CurrentValue);
  400. if ($osj2Dposts->Export <> "") $osj2Dposts->post_title->HrefValue = ew_ConvertFullUrl($osj2Dposts->post_title->HrefValue);
  401. } else {
  402. $osj2Dposts->post_title->HrefValue = "";
  403. }
  404. // post_description
  405. $osj2Dposts->post_description->HrefValue = "";
  406. } elseif ($osj2Dposts->RowType == EW_ROWTYPE_ADD) { // Add row
  407. } elseif ($osj2Dposts->RowType == EW_ROWTYPE_EDIT) { // Edit row
  408. } elseif ($osj2Dposts->RowType == EW_ROWTYPE_SEARCH) { // Search row
  409. }
  410. // Call Row Rendered event
  411. $osj2Dposts->Row_Rendered();
  412. }
  413. ?>
  414. <?php
  415. // Write Audit Trail start/end for grid update
  416. function WriteAuditTrailDummy($typ) {
  417. $table = 'osj-posts';
  418. // Write Audit Trail
  419. $filePfx = "log";
  420. $curDate = date("Y/m/d");
  421. $curTime = date("H:i:s");
  422. $id = ew_ScriptName();
  423. $user = CurrentUserID();
  424. $action = $typ;
  425. ew_WriteAuditTrail($filePfx, $curDate, $curTime, $id, $user, $action, $table, "", "", "", "");
  426. }
  427. ?>
  428. <?php
  429. // Write Audit Trail (delete page)
  430. function WriteAuditTrailOnDelete(&$rs) {
  431. global $osj2Dposts;
  432. $table = 'osj-posts';
  433. // Get key value
  434. $key = "";
  435. if ($key <> "") $key .= EW_COMPOSITE_KEY_SEPARATOR;
  436. $key .= $rs['post_id'];
  437. // Write Audit Trail
  438. $filePfx = "log";
  439. $curDate = date("Y/m/d");
  440. $curTime = date("H:i:s");
  441. $id = ew_ScriptName();
  442. $user = CurrentUserID();
  443. $action = "D";
  444. $newvalue = "";
  445. foreach (array_keys($rs) as $fldname) {
  446. if ($osj2Dposts->fields[$fldname]->FldDataType <> EW_DATATYPE_BLOB) { // Ignore Blob Field
  447. $oldvalue = ($osj2Dposts->fields[$fldname]->FldDataType == EW_DATATYPE_MEMO) ? "<MEMO>" : $rs[$fldname]; // Memo Field
  448. ew_WriteAuditTrail($filePfx, $curDate, $curTime, $id, $user, $action, $table, $fldname, $key, $oldvalue, $newvalue);
  449. }
  450. }
  451. }
  452. ?>
  453. <?php
  454. // Page Load event
  455. function Page_Load() {
  456. //echo "Page Load";
  457. }
  458. // Page Unload event
  459. function Page_Unload() {
  460. //echo "Page Unload";
  461. }
  462. ?>