public function cud($in) { $wo = $this->wo; $tableName = $this->tableName; if (!WOOOF::hasContent($tableName)) { $wo->log(WOOOF_loggingLevels::WOOOF_ERROR, "1000 No value for tableName"); return false; } $tplContentTop = ''; $tplContent = ''; $tplErrorMessage = ''; $tplMessage = ''; $wo->debug("Generic.cud for '{$tableName}'."); $table = new WOOOF_dataBaseTable($wo->db, $tableName); if (!$table->constructedOk) { $wo->log(WOOOF_loggingLevels::WOOOF_ERROR, "1010 Failed to init '{$tableName}'!"); return false; } // form html // $htmlFragment = ''; $id = $wo->getFromArray($in, '_id'); $action = $wo->getFromArray($in, '_action'); $do = $wo->getFromArray($in, '_doAction') == '1'; $redirectToPageAfterSuccess = $wo->getFromArray($in, 'redirectToPageAfterSuccess', ''); $showDetails = $wo->getFromArray($in, '_showDetails') == '1'; if (!WOOOF::hasContent($action)) { if (WOOOF::hasContent($wo->getFromArray($in, '_editButton'))) { $action = 'edit'; } elseif (WOOOF::hasContent($wo->getFromArray($in, '_deleteButton'))) { $action = 'delete'; } elseif (WOOOF::hasContent($wo->getFromArray($in, '_viewButton'))) { $action = 'view'; } } $tplContentTop .= "<h1>'{$tableName}' Form</h1>"; $tplContentTop .= "<h2>[action: {$action}] [id: {$id}] [do: {$do}]</h2>"; // Checks... // if (!in_array($action, array('insert', 'edit', 'delete', 'view'))) { $wo->log(WOOOF_loggingLevels::WOOOF_ERROR, "1020 Unknown action [{$action}]"); return false; } // action is valid here... if (!WOOOF::hasContent($id) and $action != 'insert') { $wo->log(WOOOF_loggingLevels::WOOOF_ERROR, "1030 id must be provided for action [{$action}]"); return false; } if (WOOOF::hasContent($id) and $action == 'insert') { $wo->log(WOOOF_loggingLevels::WOOOF_ERROR, "1040 id must not be provided for action [{$action}]"); return false; } if ($action != 'view') { $tplContent .= <<<SOMEHTMLSTRING \t\t\t<form method="post" action="_genericEdit.php" enctype="multipart/form-data"> \t\t\t\t<input type="hidden" name="_tableName" \tvalue="{$tableName}">\t\t\t\t \t\t\t\t<input type="hidden" name="_doAction" \tvalue="1">\t\t\t\t SOMEHTMLSTRING; } $succ = false; $columnsToFill = array(); for ($counter = 0; $counter < count($table->columns) / 2; $counter++) { $metaData = $table->columns[$counter]->getColumnMetaData(); $columnsToFill[] = $metaData['name']; } $submitButtonLabel = 'Save'; $errorsString = ''; // Do the action // if ($do) { if ($action == 'insert') { $succ = $table->handleInsertFromPost($columnsToFill); } elseif ($action == 'edit') { // $_POST['name'] .= 'x'; // demonstrate post-processing / alteration of input $succ = $table->updateRowFromPost($id, $columnsToFill, $errorsString); } elseif ($action == 'delete') { $succ = $table->deleteRow($id); } else { $wo->handleShowStopperError("1050 Unexpected action [{$action}]"); } if ($succ !== FALSE) { $wo->db->commit(); // commit... // TODO: somehow display an ok message (at the page we will redirect) if ($redirectToPageAfterSuccess != '') { header('Location: ' . $redirectToPageAfterSuccess); die; } $tplMessage .= 'Action perfomed ok.'; if ($action == 'insert') { $action = 'edit'; $id = $succ; } } else { // $succ is FALSE $tplErrorMessage .= "Failed to {$action}: " . $wo->getLastErrorAsStringAndClear() . ' ' . $errorsString . '<br>'; $wo->db->rollback(); } // $succ or not } // Draw the data // // TODO: Allow optional htmlFragment param $formBody = ''; if ($action == 'insert') { $formBody = $table->presentRowForInsert(); } elseif ($action == 'edit') { $formBody = $table->presentRowForUpdate($id); } elseif ($action == 'view') { $formBody = $table->presentRowReadOnly($id); } elseif ($action == 'delete') { if (!$do or $succ === FALSE) { $submitButtonLabel = 'Delete'; $formBody = $table->presentRowReadOnly($id); } } else { $wo->log(WOOOF_loggingLevels::WOOOF_ERROR, "1060 Unknown action [{$action}]"); return FALSE; } if ($formBody === FALSE) { $tplContent .= "Failed to perform {$action}!"; $tplErrorMessage .= $wo->getLastErrorAsStringAndClear() . '<br>'; } else { $tplContent .= $formBody; } // Finish up things // if ($action != 'view') { // edit, insert, delete // $action and $id may change after their initial value. $tplContent .= <<<SOMEHTMLSTRING \t\t\t\t<input type="hidden" name="_action" \tvalue="{$action}">\t\t\t\t \t\t\t\t<input type="hidden" name="_id" \t\tvalue="{$id}">\t\t\t\t \t\t\t\t<br> \t\t\t\t<input class="btn btn-medium" type="submit" name="submit" value="{$submitButtonLabel}" > \t\t\t</form> SOMEHTMLSTRING; } if ($action == 'view') { if ($showDetails) { $detailsContent = $this->viewDetailsRecords(array('id' => $id)); if ($detailsContent === FALSE) { return FALSE; } $tplContent .= '<br><br>' . $detailsContent; } else { $tplContent .= '<a href="_genericEdit.php?_tableName=' . $tableName . '&_id=' . $id . '&_action=' . $action . '&_showDetails=1' . '">Show Details Records...</a>' . '<br>'; } } // action is view $tplContent .= <<<SOMEHTMLSTRING \t\t<br> \t\t<a href="_genericList.php?_tableName={$tableName}">Goto '{$tableName}' list...</a> SOMEHTMLSTRING; return array('contentTop' => $tplContentTop, 'content' => $tplContent, 'errorMessage' => $tplErrorMessage, 'message' => $tplMessage); }