Skip to content

Commit 2f8cb1d

Browse files
authored
compact( ) in Basic::view( ); updated Basic_Form
1 parent b316e18 commit 2f8cb1d

File tree

10 files changed

+53
-125
lines changed

10 files changed

+53
-125
lines changed

classes/Basic_Form.php

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,61 +11,61 @@
1111
class Basic_Form
1212
{
1313

14-
public function open($class, $method='post')
14+
public function open($class='form-horizontal', $method='post')
1515
{
16-
17-
?><form class="<?= $class ?>" action="" method="<?= $method ?>"><?php
18-
16+
?>
17+
<form class="<?= $class ?>" action="" method="<?= $method ?>">
18+
<?php
1919
}
2020

21-
public function input($type, $name, $label, $value = NULL)
21+
public function input($type, $name, $label, $value=NULL)
2222
{
23-
24-
?><div class="form-group">
23+
?>
24+
<div class="form-group">
2525
<label class="control-label col-sm-2" for="<?= $name ?>"><?= $label ?>:</label>
2626
<div class="col-sm-10">
2727
<input type="<?= $type ?>" class="form-control" id="<?= $name ?>" placeholder="Enter <?= $label ?>" name="<?= $name ?>" value="<?= Basic::esc($value) ?>">
2828
</div>
29-
</div><?php
30-
29+
</div>
30+
<?php
3131
}
3232

3333

34-
public function textArea($name, $label, $value = NULL)
34+
public function textArea($name, $label, $value=NULL)
3535
{
36-
37-
?><div class="form-group">
36+
?>
37+
<div class="form-group">
3838
<label class="control-label col-sm-2" for="<?= $name ?>"><?= $label ?>:</label>
3939
<div class="col-sm-10">
4040
<textarea class="form-control" rows="5" id="<?= $name ?>" placeholder="Enter <?= $label ?>" name="<?= $name ?>"><?= Basic::esc($value) ?></textarea>
4141
</div>
42-
</div><?php
43-
42+
</div>
43+
<?php
4444
}
4545

46-
public function button($name, $label, $class)
46+
public function button($name, $label, $class='btn btn-default')
4747
{
48-
49-
?><div class="form-group">
48+
?>
49+
<div class="form-group">
5050
<div class="col-sm-offset-2 col-sm-10">
5151
<button type="submit" class="<?= $class ?>" name="<?= $name ?>"><?= $label ?></button>
5252
</div>
53-
</div><?php
54-
53+
</div>
54+
<?php
5555
}
5656

5757
public function csrfToken()
5858
{
59-
60-
?><input type="hidden" name="csrf-token" value="<?= Basic::csrf_token() ?>"><?php
61-
59+
?>
60+
<input type="hidden" name="csrf-token" value="<?= Basic::csrf_token() ?>">
61+
<?php
6262
}
6363

6464
public function close()
6565
{
66-
67-
?></form><?php
68-
66+
?>
67+
</form>
68+
<?php
6969
}
7070

7171
}
Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
<?php
22

3-
/**
4-
* In the controller file, you can handle and process variables,
5-
* classes and functions; use if-elseif statements; load models, and
6-
* include files. The variables can then be used in the view file.
7-
*/
8-
93
class EncryptionController
104
{
115

126
public function index()
137
{
14-
158
$page_title = 'Data Encryption';
9+
$plaintext = 'ABC123';
10+
$encrypted = Basic::encrypt($plaintext);
11+
$decrypted = Basic::decrypt($encrypted);
1612

17-
$data = compact('page_title');
18-
Basic::view('encryption', $data);
19-
13+
Basic::view('encryption', compact('page_title', 'plaintext', 'encrypted', 'decrypted'));
2014
}
2115

2216
}

controllers/HomeController.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
<?php
22

3-
/**
4-
* In the controller file, you can handle and process variables,
5-
* classes and functions; use if-elseif statements; load models, and
6-
* include files. The variables can then be used in the view file.
7-
*/
8-
93
class HomeController
104
{
115

126
public function index()
137
{
8+
$page_title = 'Starter Application';
149

15-
$data = ['page_title' => 'Starter Application'];
16-
Basic::view('home', $data);
17-
10+
Basic::view('home', compact('page_title'));
1811
}
1912

2013
}

controllers/PostController.php

Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
<?php
22

3-
/**
4-
* In the controller file, you can handle and process variables,
5-
* classes and functions; use if-elseif statements; load models, and
6-
* include files. The variables can then be used in the view file.
7-
*/
8-
93
class PostController
104
{
115

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

2014
if (! isset($_GET['order'])) $_GET['order'] = 0;
2115
if (! is_numeric($_GET['order'])) {
22-
$error_message = 'Post order value should be numeric.';
2316
$page_title = 'Error in order parameter';
17+
$error_message = 'Post order value should be numeric.';
18+
2419

25-
$data = compact('error_message', 'page_title');
26-
Basic::view('error', $data);
20+
Basic::view('error', compact('page_title', 'error_message'));
2721
}
2822
if (isset($_GET['order']) && $_GET['order'] < 0) $_GET['order'] = 0;
2923

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

3933
$page_title = 'List of Posts';
4034

41-
$data = compact('stmt', 'total', 'per_page', 'page_title');
42-
Basic::view('post_list', $data);
35+
Basic::view('post_list', compact('page_title', 'per_page', 'stmt', 'total'));
4336

4437
}
4538

@@ -53,114 +46,85 @@ public function view()
5346
}
5447

5548
if (isset($_POST['goto-edit'])) {
56-
5749
header('Location: ' . BASE_URL . 'post/edit/' . Basic::segment(3));
5850
exit();
59-
6051
}
6152

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

6556
if ($row) {
66-
6757
$page_title = 'View Post';
6858

69-
$data = compact('row', 'page_title');
70-
Basic::view('post_view', $data);
71-
59+
Basic::view('post_view', compact('page_title', 'row'));
7260
} else {
73-
7461
$error_message = 'The Post ID does not exist.';
7562
$page_title = 'Error in Post ID';
7663

77-
$data = compact('error_message', 'page_title');
78-
Basic::view('error', $data);
79-
64+
Basic::view('error', compact('page_title', 'error_message'));
8065
}
8166

8267
}
8368

8469
public function add()
8570
{
86-
8771
if ($this->isPostAdd()) {
88-
8972
$post = new PostModel;
9073
$new_id = $post->add();
9174

9275
header('Location: ' . BASE_URL . 'post/view/' . $new_id);
9376
exit();
94-
9577
}
9678

97-
$data = ['page_title' => 'Add a Post'];
98-
Basic::view('post_add', $data);
79+
$page_title = 'Add a Post';
9980

81+
Basic::view('post_add', compact('page_title'));
10082
}
10183

10284
public function edit()
10385
{
104-
10586
$post = new PostModel;
10687

10788
if ($this->isPostEdit()) {
108-
10989
$post->edit( Basic::segment(3) );
11090

11191
header('Location: ' . BASE_URL . 'post/view/' . Basic::segment(3));
11292
exit();
113-
11493
}
11594

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

11897
if ($row) {
119-
12098
$page_title = 'Edit Post';
12199

122-
$data = compact('row', 'page_title');
123-
Basic::view('post_edit', $data);
124-
100+
Basic::view('post_edit', compact('page_title', 'row'));
125101
} else {
126-
127102
$error_message = "The Post ID does not exist.";
128103
$page_title = 'Error in Post ID';
129104

130-
$data = compact('error_message', 'page_title');
131-
Basic::view('error', $data);
132-
105+
Basic::view('error', compact('page_title', 'error_message'));
133106
}
134-
135107
}
136108

137109
public function delete()
138110
{
139-
140111
$post = new PostModel;
141112
$post->delete( Basic::segment(3) );
142-
143113
}
144114

145115
private function isPostAdd()
146116
{
147-
148117
if ( isset($_POST['submit-post']) && isset($_POST['csrf-token']) && isset($_SESSION['csrf-token']) && $_POST['csrf-token'] == $_SESSION['csrf-token'] ) return TRUE;
149-
150118
}
151119

152120
private function isPostEdit()
153121
{
154-
155122
if ( isset($_POST['edit-post']) && isset($_POST['csrf-token']) && isset($_SESSION['csrf-token']) && $_POST['csrf-token'] == $_SESSION['csrf-token'] ) return TRUE;
156-
157123
}
158124

159125
private function isPostDelete()
160126
{
161-
162127
if ( isset($_POST['delete-post']) && isset($_POST['csrf-token']) && isset($_SESSION['csrf-token']) && $_POST['csrf-token'] == $_SESSION['csrf-token'] ) return TRUE;
163-
164128
}
165129

166130
}

controllers/RequestController.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,21 @@
11
<?php
22

3-
/**
4-
* In the controller file, you can handle and process variables,
5-
* classes and functions; use if-elseif statements; load models, and
6-
* include files. The variables can then be used in the view file.
7-
*/
8-
93
class RequestController
104
{
115

126
public function index()
137
{
148
// Execute if "Search" button is clicked
159
if ( isset($_POST['search-patient']) ) {
16-
1710
$page_title = 'API Response';
1811
$input = ['search' => $_POST['patient-name']]; // $data_input as an array
1912
$output = Basic::api_call('POST', BASE_URL . 'api/request', $input, 'Peter', 12345);
2013

21-
$data = compact('page_title', 'output');
22-
Basic::view('request', $data);
23-
14+
Basic::view('request', compact('page_title', 'output'));
2415
} else {
16+
$page_title = 'API Request';
2517

26-
$data = ['page_title' => 'API Request'];
27-
Basic::view('request', $data);
28-
18+
Basic::view('request', compact('page_title'));
2919
}
3020
}
3121

controllers/SampleController.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
<?php
22

3-
/**
4-
* In the controller file, you can handle and process variables,
5-
* classes and functions; use if-elseif statements; load models, and
6-
* include files. The variables can then be used in the view file.
7-
*/
8-
93
class SampleController
104
{
115

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

25-
$data = compact('page_title', 'param1', 'param2', 'param3', 'person');
26-
Basic::view('sample_route', $data);
19+
Basic::view('sample_route', compact('page_title', 'param1', 'param2', 'param3', 'person'));
2720

2821
} elseif ( ! is_numeric(Basic::segment(3)) || ! is_numeric(Basic::segment(4)) || Basic::segment(5) !== FALSE ) {
29-
3022
$error_message = 'You can place only 2 numbers as parameters after the /route string, such as /route/1/2 .';
31-
$data = compact('page_title', 'error_message');
32-
Basic::view('error', $data);
3323

24+
Basic::view('error', compact('page_title', 'error_message'));
3425
}
3526

3627
}

views/encryption.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
// Show Header and Menu
33
require_once 'template/header.php';
44
require_once 'template/menu.php';
5-
6-
$plaintext = 'ABC123';
7-
$encrypted = Basic::encrypt($plaintext);
8-
$decrypted = Basic::decrypt($encrypted);
95
?>
106
<!-- Page Content -->
117
<div class="container">

views/post_add.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
<h1 class="mt-5 text-center">Add Post</h1>
1111
<?php
1212
$form = new Basic_Form;
13-
$form->open('form-horizontal');
13+
$form->open();
1414
$form->input('text', 'title', 'Title');
1515
$form->textArea('content', 'Content');
16-
$form->button('submit-post', 'Submit', 'btn btn-default');
16+
$form->button('submit-post', 'Submit');
1717
$form->csrfToken();
1818
$form->close();
1919
?>

0 commit comments

Comments
 (0)