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

/osj-osj2dcategoriesadd.php

http://osjobber.googlecode.com/
PHP | 508 lines | 382 code | 60 blank | 66 comment | 59 complexity | 8afdd67243de60b637141fe1f15fe541 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0
  1. <?php
  2. define("EW_PAGE_ID", "add", TRUE); // Page ID
  3. define("EW_TABLE_NAME", 'osj-categories', 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-osj2dcategoriesinfo.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-categories');
  32. if (!$Security->IsLoggedIn()) {
  33. $Security->SaveLastUrl();
  34. Page_Terminate("osj-login.php");
  35. }
  36. if (!$Security->CanAdd()) {
  37. $Security->SaveLastUrl();
  38. Page_Terminate("osj-osj2dcategorieslist.php");
  39. }
  40. ?>
  41. <?php
  42. // Common page loading event (in userfn*.php)
  43. Page_Loading();
  44. ?>
  45. <?php
  46. // Page load event, used in current page
  47. Page_Load();
  48. ?>
  49. <?php
  50. $osj2Dcategories->Export = @$_GET["export"]; // Get export parameter
  51. $sExport = $osj2Dcategories->Export; // Get export parameter, used in header
  52. $sExportFile = $osj2Dcategories->TableVar; // Get export file, used in header
  53. ?>
  54. <?php
  55. // Load key values from QueryString
  56. $bCopy = TRUE;
  57. if (@$_GET["category_id"] != "") {
  58. $osj2Dcategories->category_id->setQueryStringValue($_GET["category_id"]);
  59. } else {
  60. $bCopy = FALSE;
  61. }
  62. // Create form object
  63. $objForm = new cFormObj();
  64. // Process form if post back
  65. if (@$_POST["a_add"] <> "") {
  66. $osj2Dcategories->CurrentAction = $_POST["a_add"]; // Get form action
  67. LoadFormValues(); // Load form values
  68. } else { // Not post back
  69. if ($bCopy) {
  70. $osj2Dcategories->CurrentAction = "C"; // Copy Record
  71. } else {
  72. $osj2Dcategories->CurrentAction = "I"; // Display Blank Record
  73. LoadDefaultValues(); // Load default values
  74. }
  75. }
  76. // Perform action based on action code
  77. switch ($osj2Dcategories->CurrentAction) {
  78. case "I": // Blank record, no action required
  79. break;
  80. case "C": // Copy an existing record
  81. if (!LoadRow()) { // Load record based on key
  82. $_SESSION[EW_SESSION_MESSAGE] = "No records found"; // No record found
  83. Page_Terminate($osj2Dcategories->getReturnUrl()); // Clean up and return
  84. }
  85. break;
  86. case "A": // ' Add new record
  87. $osj2Dcategories->SendEmail = TRUE; // Send email on add success
  88. if (AddRow()) { // Add successful
  89. $_SESSION[EW_SESSION_MESSAGE] = "Add New Record Successful"; // Set up success message
  90. Page_Terminate($osj2Dcategories->KeyUrl($osj2Dcategories->getReturnUrl())); // Clean up and return
  91. } else {
  92. RestoreFormValues(); // Add failed, restore form values
  93. }
  94. }
  95. // Render row based on row type
  96. $osj2Dcategories->RowType = EW_ROWTYPE_ADD; // Render add type
  97. RenderRow();
  98. ?>
  99. <?php include "osj-header.php" ?>
  100. <script type="text/javascript">
  101. <!--
  102. var EW_PAGE_ID = "add"; // Page id
  103. var EW_SHOW_HIGHLIGHT = "Show highlight";
  104. var EW_HIDE_HIGHLIGHT = "Hide highlight";
  105. //-->
  106. </script>
  107. <script type="text/javascript">
  108. <!--
  109. function ew_ValidateForm(fobj) {
  110. if (fobj.a_confirm && fobj.a_confirm.value == "F")
  111. return true;
  112. var i, elm, aelm, infix;
  113. var rowcnt = (fobj.key_count) ? Number(fobj.key_count.value) : 1;
  114. for (i=0; i<rowcnt; i++) {
  115. infix = (fobj.key_count) ? String(i+1) : "";
  116. elm = fobj.elements["x" + infix + "_category_id"];
  117. if (elm && !ew_HasValue(elm)) {
  118. if (!ew_OnError(elm, "Please enter required field - category id"))
  119. return false;
  120. }
  121. elm = fobj.elements["x" + infix + "_category_id"];
  122. if (elm && !ew_CheckInteger(elm.value)) {
  123. if (!ew_OnError(elm, "Incorrect integer - category id"))
  124. return false;
  125. }
  126. elm = fobj.elements["x" + infix + "_category_name"];
  127. if (elm && !ew_HasValue(elm)) {
  128. if (!ew_OnError(elm, "Please enter required field - Category Name"))
  129. return false;
  130. }
  131. }
  132. return true;
  133. }
  134. //-->
  135. </script>
  136. <script type="text/javascript">
  137. <!--
  138. var ew_DHTMLEditors = [];
  139. //-->
  140. </script>
  141. <script type="text/javascript">
  142. <!--
  143. // js for Popup Calendar
  144. //-->
  145. </script>
  146. <script type="text/javascript">
  147. <!--
  148. var ew_MultiPagePage = "Page"; // multi-page Page Text
  149. var ew_MultiPageOf = "of"; // multi-page Of Text
  150. var ew_MultiPagePrev = "Prev"; // multi-page Prev Text
  151. var ew_MultiPageNext = "Next"; // multi-page Next Text
  152. //-->
  153. </script>
  154. <script language="JavaScript" type="text/javascript">
  155. <!--
  156. // Write your client script here, no need to add script tags.
  157. // To include another .js script, use:
  158. // ew_ClientScriptInclude("my_javascript.js");
  159. //-->
  160. </script>
  161. <p><span class="phpmaker">Add to TABLE: Categories<br><br><a href="<?php echo $osj2Dcategories->getReturnUrl() ?>">Go Back</a></span></p>
  162. <?php
  163. if (@$_SESSION[EW_SESSION_MESSAGE] <> "") { // Mesasge in Session, display
  164. ?>
  165. <p><span class="ewmsg"><?php echo $_SESSION[EW_SESSION_MESSAGE] ?></span></p>
  166. <?php
  167. $_SESSION[EW_SESSION_MESSAGE] = ""; // Clear message in Session
  168. }
  169. ?>
  170. <form name="fosj2Dcategoriesadd" id="fosj2Dcategoriesadd" action="osj-osj2dcategoriesadd.php" method="post" onSubmit="return ew_ValidateForm(this);">
  171. <p>
  172. <input type="hidden" name="a_add" id="a_add" value="A">
  173. <table class="ewTable">
  174. <tr class="ewTableRow">
  175. <td class="ewTableHeader">category id<span class='ewmsg'>&nbsp;*</span></td>
  176. <td<?php echo $osj2Dcategories->category_id->CellAttributes() ?>><span id="cb_x_category_id"></span></td>
  177. </tr>
  178. <tr class="ewTableAltRow">
  179. <td class="ewTableHeader">parent id<span class='ewmsg'>&nbsp;*</span></td>
  180. <td<?php echo $osj2Dcategories->parent_id->CellAttributes() ?>><span id="cb_x_parent_id">
  181. <select id="x_parent_id" name="x_parent_id"<?php echo $osj2Dcategories->parent_id->EditAttributes() ?>>
  182. <!--option value="">Please Select</option-->
  183. <?php
  184. if (is_array($osj2Dcategories->parent_id->EditValue)) {
  185. $arwrk = $osj2Dcategories->parent_id->EditValue;
  186. $rowswrk = count($arwrk);
  187. for ($rowcntwrk = 0; $rowcntwrk < $rowswrk; $rowcntwrk++) {
  188. $selwrk = (strval($osj2Dcategories->parent_id->CurrentValue) == strval($arwrk[$rowcntwrk][0])) ? " selected" : "";
  189. ?>
  190. <option value="<?php echo ew_HtmlEncode($arwrk[$rowcntwrk][0]) ?>"<?php echo $selwrk ?>>
  191. <?php echo $arwrk[$rowcntwrk][1] ?>
  192. </option>
  193. <?php
  194. }
  195. }
  196. ?>
  197. </select>
  198. </span></td>
  199. </tr>
  200. <tr class="ewTableRow">
  201. <td class="ewTableHeader">Category Name<span class='ewmsg'>&nbsp;*</span></td>
  202. <td<?php echo $osj2Dcategories->category_name->CellAttributes() ?>><span id="cb_x_category_name">
  203. <input type="text" name="x_category_name" id="x_category_name" title="Category Name" size="30" maxlength="255" value="<?php echo $osj2Dcategories->category_name->EditValue ?>"<?php echo $osj2Dcategories->category_name->EditAttributes() ?>>
  204. </span></td>
  205. </tr>
  206. </table>
  207. <p>
  208. <input type="submit" name="btnAction" id="btnAction" value=" Add ">
  209. </form>
  210. <script language="JavaScript" type="text/javascript">
  211. <!--
  212. // Write your table-specific startup script here
  213. // document.write("page loaded");
  214. //-->
  215. </script>
  216. <?php include "osj-footer.php" ?>
  217. <?php
  218. // If control is passed here, simply terminate the page without redirect
  219. Page_Terminate();
  220. // -----------------------------------------------------------------
  221. // Subroutine Page_Terminate
  222. // - called when exit page
  223. // - clean up connection and objects
  224. // - if url specified, redirect to url, otherwise end response
  225. function Page_Terminate($url = "") {
  226. global $conn;
  227. // Page unload event, used in current page
  228. Page_Unload();
  229. // Global page unloaded event (in userfn*.php)
  230. Page_Unloaded();
  231. // Close Connection
  232. $conn->Close();
  233. // Go to url if specified
  234. if ($url <> "") {
  235. ob_end_clean();
  236. header("Location: $url");
  237. }
  238. exit();
  239. }
  240. ?>
  241. <?php
  242. // Load default values
  243. function LoadDefaultValues() {
  244. global $osj2Dcategories;
  245. $osj2Dcategories->parent_id->CurrentValue = 0;
  246. }
  247. ?>
  248. <?php
  249. // Load form values
  250. function LoadFormValues() {
  251. // Load from form
  252. global $objForm, $osj2Dcategories;
  253. $osj2Dcategories->category_id->setFormValue($objForm->GetValue("x_category_id"));
  254. $osj2Dcategories->parent_id->setFormValue($objForm->GetValue("x_parent_id"));
  255. $osj2Dcategories->category_name->setFormValue($objForm->GetValue("x_category_name"));
  256. }
  257. // Restore form values
  258. function RestoreFormValues() {
  259. global $osj2Dcategories;
  260. $osj2Dcategories->category_id->CurrentValue = $osj2Dcategories->category_id->FormValue;
  261. $osj2Dcategories->parent_id->CurrentValue = $osj2Dcategories->parent_id->FormValue;
  262. $osj2Dcategories->category_name->CurrentValue = $osj2Dcategories->category_name->FormValue;
  263. }
  264. ?>
  265. <?php
  266. // Load row based on key values
  267. function LoadRow() {
  268. global $conn, $Security, $osj2Dcategories;
  269. $sFilter = $osj2Dcategories->SqlKeyFilter();
  270. if (!is_numeric($osj2Dcategories->category_id->CurrentValue)) {
  271. return FALSE; // Invalid key, exit
  272. }
  273. $sFilter = str_replace("@category_id@", ew_AdjustSql($osj2Dcategories->category_id->CurrentValue), $sFilter); // Replace key value
  274. // Call Row Selecting event
  275. $osj2Dcategories->Row_Selecting($sFilter);
  276. // Load sql based on filter
  277. $osj2Dcategories->CurrentFilter = $sFilter;
  278. $sSql = $osj2Dcategories->SQL();
  279. if ($rs = $conn->Execute($sSql)) {
  280. if ($rs->EOF) {
  281. $LoadRow = FALSE;
  282. } else {
  283. $LoadRow = TRUE;
  284. $rs->MoveFirst();
  285. LoadRowValues($rs); // Load row values
  286. // Call Row Selected event
  287. $osj2Dcategories->Row_Selected($rs);
  288. }
  289. $rs->Close();
  290. } else {
  291. $LoadRow = FALSE;
  292. }
  293. return $LoadRow;
  294. }
  295. // Load row values from recordset
  296. function LoadRowValues(&$rs) {
  297. global $osj2Dcategories;
  298. $osj2Dcategories->category_id->setDbValue($rs->fields('category_id'));
  299. $osj2Dcategories->parent_id->setDbValue($rs->fields('parent_id'));
  300. $osj2Dcategories->category_name->setDbValue($rs->fields('category_name'));
  301. }
  302. ?>
  303. <?php
  304. // Render row values based on field settings
  305. function RenderRow() {
  306. global $conn, $Security, $osj2Dcategories;
  307. // Call Row Rendering event
  308. $osj2Dcategories->Row_Rendering();
  309. // Common render codes for all row types
  310. // category_id
  311. $osj2Dcategories->category_id->CellCssStyle = "";
  312. $osj2Dcategories->category_id->CellCssClass = "";
  313. // parent_id
  314. $osj2Dcategories->parent_id->CellCssStyle = "";
  315. $osj2Dcategories->parent_id->CellCssClass = "";
  316. // category_name
  317. $osj2Dcategories->category_name->CellCssStyle = "";
  318. $osj2Dcategories->category_name->CellCssClass = "";
  319. if ($osj2Dcategories->RowType == EW_ROWTYPE_VIEW) { // View row
  320. } elseif ($osj2Dcategories->RowType == EW_ROWTYPE_ADD) { // Add row
  321. // category_id
  322. $osj2Dcategories->category_id->EditCustomAttributes = "";
  323. $osj2Dcategories->category_id->EditValue = ew_HtmlEncode($osj2Dcategories->category_id->CurrentValue);
  324. // parent_id
  325. $osj2Dcategories->parent_id->EditCustomAttributes = "";
  326. $sSqlWrk = "SELECT `category_id`, `category_name` FROM `osj-categories`";
  327. $sSqlWrk .= " WHERE (" . "`parent_id` = 0 OR `parent_id` = -1" . ")";
  328. $sSqlWrk .= " ORDER BY `category_name` ";
  329. $rswrk = $conn->Execute($sSqlWrk);
  330. $arwrk = ($rswrk) ? $rswrk->GetRows() : array();
  331. if ($rswrk) $rswrk->Close();
  332. array_unshift($arwrk, array("", "Please Select"));
  333. $osj2Dcategories->parent_id->EditValue = $arwrk;
  334. // category_name
  335. $osj2Dcategories->category_name->EditCustomAttributes = "";
  336. $osj2Dcategories->category_name->EditValue = ew_HtmlEncode($osj2Dcategories->category_name->CurrentValue);
  337. } elseif ($osj2Dcategories->RowType == EW_ROWTYPE_EDIT) { // Edit row
  338. } elseif ($osj2Dcategories->RowType == EW_ROWTYPE_SEARCH) { // Search row
  339. }
  340. // Call Row Rendered event
  341. $osj2Dcategories->Row_Rendered();
  342. }
  343. ?>
  344. <?php
  345. // Add record
  346. function AddRow() {
  347. global $conn, $Security, $osj2Dcategories;
  348. // Check for duplicate key
  349. $bCheckKey = TRUE;
  350. $sFilter = $osj2Dcategories->SqlKeyFilter();
  351. if (trim(strval($osj2Dcategories->category_id->CurrentValue)) == "") {
  352. $bCheckKey = FALSE;
  353. } else {
  354. $sFilter = str_replace("@category_id@", ew_AdjustSql($osj2Dcategories->category_id->CurrentValue), $sFilter); // Replace key value
  355. }
  356. if (!is_numeric($osj2Dcategories->category_id->CurrentValue)) {
  357. $bCheckKey = FALSE;
  358. }
  359. if ($bCheckKey) {
  360. $rsChk = $osj2Dcategories->LoadRs($sFilter);
  361. if ($rsChk && !$rsChk->EOF) {
  362. $_SESSION[EW_SESSION_MESSAGE] = "Duplicate value for primary key";
  363. $rsChk->Close();
  364. return FALSE;
  365. }
  366. }
  367. $rsnew = array();
  368. // Field category_id
  369. // Field parent_id
  370. $osj2Dcategories->parent_id->SetDbValueDef($osj2Dcategories->parent_id->CurrentValue, 0);
  371. $rsnew['parent_id'] =& $osj2Dcategories->parent_id->DbValue;
  372. // Field category_name
  373. $osj2Dcategories->category_name->SetDbValueDef($osj2Dcategories->category_name->CurrentValue, "");
  374. $rsnew['category_name'] =& $osj2Dcategories->category_name->DbValue;
  375. // Call Row Inserting event
  376. $bInsertRow = $osj2Dcategories->Row_Inserting($rsnew);
  377. if ($bInsertRow) {
  378. $conn->raiseErrorFn = 'ew_ErrorFn';
  379. $AddRow = $conn->Execute($osj2Dcategories->InsertSQL($rsnew));
  380. $conn->raiseErrorFn = '';
  381. } else {
  382. if ($osj2Dcategories->CancelMessage <> "") {
  383. $_SESSION[EW_SESSION_MESSAGE] = $osj2Dcategories->CancelMessage;
  384. $osj2Dcategories->CancelMessage = "";
  385. } else {
  386. $_SESSION[EW_SESSION_MESSAGE] = "Insert cancelled";
  387. }
  388. $AddRow = FALSE;
  389. }
  390. if ($AddRow) {
  391. $osj2Dcategories->category_id->setDbValue($conn->Insert_ID());
  392. $rsnew['category_id'] =& $osj2Dcategories->category_id->DbValue;
  393. // Call Row Inserted event
  394. $osj2Dcategories->Row_Inserted($rsnew);
  395. WriteAuditTrailOnAdd($rsnew);
  396. }
  397. return $AddRow;
  398. }
  399. ?>
  400. <?php
  401. // Write Audit Trail start/end for grid update
  402. function WriteAuditTrailDummy($typ) {
  403. $table = 'osj-categories';
  404. // Write Audit Trail
  405. $filePfx = "log";
  406. $curDate = date("Y/m/d");
  407. $curTime = date("H:i:s");
  408. $id = ew_ScriptName();
  409. $user = CurrentUserID();
  410. $action = $typ;
  411. ew_WriteAuditTrail($filePfx, $curDate, $curTime, $id, $user, $action, $table, "", "", "", "");
  412. }
  413. ?>
  414. <?php
  415. // Write Audit Trail (add page)
  416. function WriteAuditTrailOnAdd(&$rs) {
  417. global $osj2Dcategories;
  418. $table = 'osj-categories';
  419. // Get key value
  420. $key = "";
  421. if ($key <> "") $key .= EW_COMPOSITE_KEY_SEPARATOR;
  422. $key .= $rs['category_id'];
  423. // Write Audit Trail
  424. $filePfx = "log";
  425. $curDate = date("Y/m/d");
  426. $curTime = date("H:i:s");
  427. $id = ew_ScriptName();
  428. $user = CurrentUserID();
  429. $action = "A";
  430. $oldvalue = "";
  431. foreach (array_keys($rs) as $fldname) {
  432. if ($osj2Dcategories->fields[$fldname]->FldDataType <> EW_DATATYPE_BLOB) { // Ignore Blob Field
  433. $newvalue = ($osj2Dcategories->fields[$fldname]->FldDataType == EW_DATATYPE_MEMO) ? "<MEMO>" : $rs[$fldname]; // Memo Field
  434. ew_WriteAuditTrail($filePfx, $curDate, $curTime, $id, $user, $action, $table, $fldname, $key, $oldvalue, $newvalue);
  435. }
  436. }
  437. }
  438. ?>
  439. <?php
  440. // Page Load event
  441. function Page_Load() {
  442. //echo "Page Load";
  443. }
  444. // Page Unload event
  445. function Page_Unload() {
  446. //echo "Page Unload";
  447. }
  448. ?>