Skip to content

Commit

Permalink
compact( ) in Basic::view( ); updated Basic_Form
Browse files Browse the repository at this point in the history
  • Loading branch information
ray-ang authored Jul 25, 2020
1 parent b316e18 commit 2f8cb1d
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 125 deletions.
50 changes: 25 additions & 25 deletions classes/Basic_Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,61 +11,61 @@
class Basic_Form
{

public function open($class, $method='post')
public function open($class='form-horizontal', $method='post')
{

?><form class="<?= $class ?>" action="" method="<?= $method ?>"><?php

?>
<form class="<?= $class ?>" action="" method="<?= $method ?>">
<?php
}

public function input($type, $name, $label, $value = NULL)
public function input($type, $name, $label, $value=NULL)
{

?><div class="form-group">
?>
<div class="form-group">
<label class="control-label col-sm-2" for="<?= $name ?>"><?= $label ?>:</label>
<div class="col-sm-10">
<input type="<?= $type ?>" class="form-control" id="<?= $name ?>" placeholder="Enter <?= $label ?>" name="<?= $name ?>" value="<?= Basic::esc($value) ?>">
</div>
</div><?php

</div>
<?php
}


public function textArea($name, $label, $value = NULL)
public function textArea($name, $label, $value=NULL)
{

?><div class="form-group">
?>
<div class="form-group">
<label class="control-label col-sm-2" for="<?= $name ?>"><?= $label ?>:</label>
<div class="col-sm-10">
<textarea class="form-control" rows="5" id="<?= $name ?>" placeholder="Enter <?= $label ?>" name="<?= $name ?>"><?= Basic::esc($value) ?></textarea>
</div>
</div><?php

</div>
<?php
}

public function button($name, $label, $class)
public function button($name, $label, $class='btn btn-default')
{

?><div class="form-group">
?>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="<?= $class ?>" name="<?= $name ?>"><?= $label ?></button>
</div>
</div><?php

</div>
<?php
}

public function csrfToken()
{

?><input type="hidden" name="csrf-token" value="<?= Basic::csrf_token() ?>"><?php

?>
<input type="hidden" name="csrf-token" value="<?= Basic::csrf_token() ?>">
<?php
}

public function close()
{

?></form><?php

?>
</form>
<?php
}

}
14 changes: 4 additions & 10 deletions controllers/EncryptionController.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
<?php

/**
* In the controller file, you can handle and process variables,
* classes and functions; use if-elseif statements; load models, and
* include files. The variables can then be used in the view file.
*/

class EncryptionController
{

public function index()
{

$page_title = 'Data Encryption';
$plaintext = 'ABC123';
$encrypted = Basic::encrypt($plaintext);
$decrypted = Basic::decrypt($encrypted);

$data = compact('page_title');
Basic::view('encryption', $data);

Basic::view('encryption', compact('page_title', 'plaintext', 'encrypted', 'decrypted'));
}

}
11 changes: 2 additions & 9 deletions controllers/HomeController.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
<?php

/**
* In the controller file, you can handle and process variables,
* classes and functions; use if-elseif statements; load models, and
* include files. The variables can then be used in the view file.
*/

class HomeController
{

public function index()
{
$page_title = 'Starter Application';

$data = ['page_title' => 'Starter Application'];
Basic::view('home', $data);

Basic::view('home', compact('page_title'));
}

}
56 changes: 10 additions & 46 deletions controllers/PostController.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
<?php

/**
* In the controller file, you can handle and process variables,
* classes and functions; use if-elseif statements; load models, and
* include files. The variables can then be used in the view file.
*/

class PostController
{

Expand All @@ -19,11 +13,11 @@ public function list()

if (! isset($_GET['order'])) $_GET['order'] = 0;
if (! is_numeric($_GET['order'])) {
$error_message = 'Post order value should be numeric.';
$page_title = 'Error in order parameter';
$error_message = 'Post order value should be numeric.';


$data = compact('error_message', 'page_title');
Basic::view('error', $data);
Basic::view('error', compact('page_title', 'error_message'));
}
if (isset($_GET['order']) && $_GET['order'] < 0) $_GET['order'] = 0;

Expand All @@ -38,8 +32,7 @@ public function list()

$page_title = 'List of Posts';

$data = compact('stmt', 'total', 'per_page', 'page_title');
Basic::view('post_list', $data);
Basic::view('post_list', compact('page_title', 'per_page', 'stmt', 'total'));

}

Expand All @@ -53,114 +46,85 @@ public function view()
}

if (isset($_POST['goto-edit'])) {

header('Location: ' . BASE_URL . 'post/edit/' . Basic::segment(3));
exit();

}

$post = new PostModel;
$row = $post->view( Basic::segment(3) );

if ($row) {

$page_title = 'View Post';

$data = compact('row', 'page_title');
Basic::view('post_view', $data);

Basic::view('post_view', compact('page_title', 'row'));
} else {

$error_message = 'The Post ID does not exist.';
$page_title = 'Error in Post ID';

$data = compact('error_message', 'page_title');
Basic::view('error', $data);

Basic::view('error', compact('page_title', 'error_message'));
}

}

public function add()
{

if ($this->isPostAdd()) {

$post = new PostModel;
$new_id = $post->add();

header('Location: ' . BASE_URL . 'post/view/' . $new_id);
exit();

}

$data = ['page_title' => 'Add a Post'];
Basic::view('post_add', $data);
$page_title = 'Add a Post';

Basic::view('post_add', compact('page_title'));
}

public function edit()
{

$post = new PostModel;

if ($this->isPostEdit()) {

$post->edit( Basic::segment(3) );

header('Location: ' . BASE_URL . 'post/view/' . Basic::segment(3));
exit();

}

$row = $post->view( Basic::segment(3) );

if ($row) {

$page_title = 'Edit Post';

$data = compact('row', 'page_title');
Basic::view('post_edit', $data);

Basic::view('post_edit', compact('page_title', 'row'));
} else {

$error_message = "The Post ID does not exist.";
$page_title = 'Error in Post ID';

$data = compact('error_message', 'page_title');
Basic::view('error', $data);

Basic::view('error', compact('page_title', 'error_message'));
}

}

public function delete()
{

$post = new PostModel;
$post->delete( Basic::segment(3) );

}

private function isPostAdd()
{

if ( isset($_POST['submit-post']) && isset($_POST['csrf-token']) && isset($_SESSION['csrf-token']) && $_POST['csrf-token'] == $_SESSION['csrf-token'] ) return TRUE;

}

private function isPostEdit()
{

if ( isset($_POST['edit-post']) && isset($_POST['csrf-token']) && isset($_SESSION['csrf-token']) && $_POST['csrf-token'] == $_SESSION['csrf-token'] ) return TRUE;

}

private function isPostDelete()
{

if ( isset($_POST['delete-post']) && isset($_POST['csrf-token']) && isset($_SESSION['csrf-token']) && $_POST['csrf-token'] == $_SESSION['csrf-token'] ) return TRUE;

}

}
16 changes: 3 additions & 13 deletions controllers/RequestController.php
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
<?php

/**
* In the controller file, you can handle and process variables,
* classes and functions; use if-elseif statements; load models, and
* include files. The variables can then be used in the view file.
*/

class RequestController
{

public function index()
{
// Execute if "Search" button is clicked
if ( isset($_POST['search-patient']) ) {

$page_title = 'API Response';
$input = ['search' => $_POST['patient-name']]; // $data_input as an array
$output = Basic::api_call('POST', BASE_URL . 'api/request', $input, 'Peter', 12345);

$data = compact('page_title', 'output');
Basic::view('request', $data);

Basic::view('request', compact('page_title', 'output'));
} else {
$page_title = 'API Request';

$data = ['page_title' => 'API Request'];
Basic::view('request', $data);

Basic::view('request', compact('page_title'));
}
}

Expand Down
13 changes: 2 additions & 11 deletions controllers/SampleController.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
<?php

/**
* In the controller file, you can handle and process variables,
* classes and functions; use if-elseif statements; load models, and
* include files. The variables can then be used in the view file.
*/

class SampleController
{

Expand All @@ -22,15 +16,12 @@ public function route()
// Display page
if ( is_numeric(Basic::segment(3)) && is_numeric(Basic::segment(4)) && Basic::segment(5) == FALSE ) {

$data = compact('page_title', 'param1', 'param2', 'param3', 'person');
Basic::view('sample_route', $data);
Basic::view('sample_route', compact('page_title', 'param1', 'param2', 'param3', 'person'));

} elseif ( ! is_numeric(Basic::segment(3)) || ! is_numeric(Basic::segment(4)) || Basic::segment(5) !== FALSE ) {

$error_message = 'You can place only 2 numbers as parameters after the /route string, such as /route/1/2 .';
$data = compact('page_title', 'error_message');
Basic::view('error', $data);

Basic::view('error', compact('page_title', 'error_message'));
}

}
Expand Down
4 changes: 0 additions & 4 deletions views/encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
// Show Header and Menu
require_once 'template/header.php';
require_once 'template/menu.php';

$plaintext = 'ABC123';
$encrypted = Basic::encrypt($plaintext);
$decrypted = Basic::decrypt($encrypted);
?>
<!-- Page Content -->
<div class="container">
Expand Down
4 changes: 2 additions & 2 deletions views/post_add.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
<h1 class="mt-5 text-center">Add Post</h1>
<?php
$form = new Basic_Form;
$form->open('form-horizontal');
$form->open();
$form->input('text', 'title', 'Title');
$form->textArea('content', 'Content');
$form->button('submit-post', 'Submit', 'btn btn-default');
$form->button('submit-post', 'Submit');
$form->csrfToken();
$form->close();
?>
Expand Down
Loading

0 comments on commit 2f8cb1d

Please sign in to comment.