Skip to content

Commit dbd2aee

Browse files
committed
We can now truncate table from the admin, removed dot from lorem Ipsum
1 parent 3045c10 commit dbd2aee

7 files changed

+85
-22
lines changed

admin/admin.js

+25-5
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,54 @@ $(document).ready(function() {
2424
$(this).tab('show');
2525
});
2626

27+
// Insert Single
2728
$("#add-single-action").click(function(e) {
2829
e.preventDefault();
2930
insertSingle(e);
3031
});
32+
33+
function insertSingle()
34+
{
35+
$.ajax({
36+
type: "POST",
37+
url: "ajax.php",
38+
data: {action_type:"insert_single", element_name:$("#element-name").val(), element_address:$("#element-address").val(), element_description:$("#element-description").val()}
39+
}).done(function( result )
40+
{
41+
$("#msg").html( result );
42+
});
43+
}
44+
45+
// Insert Multiple
3146
$("#add-multiple-action").click(function(e) {
3247
e.preventDefault();
3348
insertRandomElements(e);
3449
});
3550

36-
// Ajax requests
37-
function insertSingle()
51+
function insertRandomElements()
3852
{
3953
$.ajax({
4054
type: "POST",
4155
url: "ajax.php",
42-
data: {action_type:"insert_single", element_name:$("#element-name").val(), element_address:$("#element-address").val(), element_description:$("#element-description").val()}
56+
data: {action_type:"insert_multiple", num_random_elements:$("#element-number").val()}
4357
}).done(function( result )
4458
{
4559
$("#msg").html( result );
4660
});
4761
}
4862

49-
function insertRandomElements()
63+
// Truncate Table
64+
$("#remove-all").click(function(e) {
65+
e.preventDefault();
66+
removeAllElements(e);
67+
});
68+
69+
function removeAllElements()
5070
{
5171
$.ajax({
5272
type: "POST",
5373
url: "ajax.php",
54-
data: {action_type:"insert_multiple", num_random_elements:$("#element-number").val()}
74+
data: {action_type:"remove_all"}
5575
}).done(function( result )
5676
{
5777
$("#msg").html( result );

admin/ajax.php

+28
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
echo "<div class='alert alert-error'>You need to fill all the forms</div>";
5454
}
5555
}
56+
5657
/****************************************************************
5758
* Insert random elements
5859
****************************************************************/
@@ -79,4 +80,31 @@
7980
echo "<div class='alert alert-error'>You need to specify a number</div>";
8081
}
8182
}
83+
84+
85+
/****************************************************************
86+
* Truncate Selected Table (remove all elements from the table)
87+
****************************************************************/
88+
if ($action_type == "remove_all")
89+
{
90+
$Elements = new ElementManager();
91+
92+
try
93+
{
94+
$time_start = microtime(true);
95+
if ($Elements->dbHelper->remove_all())
96+
{
97+
$time_end = microtime(true);
98+
$execution_time = ($time_end - $time_start);
99+
echo '<div class="alert alert-success">';
100+
echo 'Successfully <b class="label label-info">removed all elements</b> in database.<br />';
101+
echo 'Total Execution Time: <b class="label label-info">' . $execution_time . '</b> seconds';
102+
echo '</div>';
103+
}
104+
}
105+
catch (Exception $e)
106+
{
107+
echo $e;
108+
}
109+
}
82110
?>

admin/form.php

+10-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
if ($access != 'authorized')
2323
die('You are not allowed to view this file');
2424

25+
require_once("../lib/LoremIpsum.class.php");
26+
2527
?>
2628

2729
<ul class="nav nav-tabs" id="navigation-tabs">
@@ -150,9 +152,9 @@
150152
<div id="database-actions">
151153
<h2>Database actions</h2>
152154
<p><span class="label label-warning">Warning</span> changes are permanent.</p>
153-
<a class="btn btn-success"><i class="icon icon-white icon-download"></i> Export database</a>
154-
<a class="btn btn-primary"><i class="icon icon-white icon-file"></i> Import database</a>
155-
<a class="btn btn-danger"><i class="icon icon-white icon-trash"></i> Empty Database</a>
155+
<a id="export" class="btn btn-success"><i class="icon icon-white icon-download"></i> Export database</a>
156+
<a id="import" class="btn btn-primary"><i class="icon icon-white icon-file"></i> Import database</a>
157+
<a id="remove-all" class="btn btn-danger"><i class="icon icon-white icon-trash"></i> Empty Database</a>
156158
</div>
157159
</div>
158160

@@ -203,11 +205,12 @@
203205
</fieldset>
204206
</form>
205207
<div id="random-sample">
206-
<h2>Sample</h2>
208+
<h2>Randomly generated element sample</h2>
207209
<div class="well">
208-
<h3>@todo: Random Element Name</h3>
209-
<p>@todo: Random Element address</p>
210-
<p>@todo: Random Element description</p>
210+
<?php $textgen = new LoremIpsumGenerator(); ?>
211+
<h3><?php echo $textgen->getContent(3, "txt", false); ?></h3>
212+
<p><?php echo $textgen->getContent(100, "txt", true); ?></p>
213+
<p><?php echo $textgen->getContent(4, "txt", false); ?></p>
211214
</div>
212215
</div>
213216
</div>

configuration-sample.php

-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676

7777
/**
7878
* Number of links displayed in the navigation menu
79-
* @todo: implement this
8079
*/
8180
define('PAGINATION_NUM_LINKS', -1); // -1 to display all links
8281

lib/LoremIpsum.class.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ private function getPlain($count, $loremipsum, $returnStr = true)
277277
for($i = $curr; $i < ($curr + $senSize); $i++)
278278
$sentence[] = $words[$i];
279279

280-
$this->punctuate($sentence);
280+
//$this->punctuate($sentence);
281281
$curr = $curr + $senSize;
282282
$sentences[] = $sentence;
283283
}

lib/database-helper.php

+17-6
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function testConnection()
6161
}
6262

6363
/**
64-
* Connect to database using defined credentials defined in configuration.php
64+
* Connect to database using defined credentials in configuration.php
6565
*/
6666
public function connectToDatabase()
6767
{
@@ -87,7 +87,6 @@ public function connectToDatabase()
8787
}
8888
}
8989

90-
9190
private function request($sql, $insert = false)
9291
{
9392
if ($this->isConnected)
@@ -114,10 +113,7 @@ private function request($sql, $insert = false)
114113
}
115114
}
116115

117-
/**
118-
*
119-
*/
120-
public function request_infos($id)
116+
public function request_infos($id)
121117
{
122118
$sql = "SELECT address, description from " . DB_TABLE_NAME . " WHERE id=" . $id . " " . $this->limit . ";";
123119
return $this->request($sql);
@@ -157,5 +153,20 @@ public function paginationLimit($current_position, $element_per_page)
157153
$limit = " LIMIT " . $start . ", " . $element_per_page;
158154
$this->limit = $limit;
159155
}
156+
157+
public function remove_all()
158+
{
159+
$sql = "TRUNCATE " . DB_TABLE_NAME . ";";
160+
$request = mysqli_query($this->con, $sql);
161+
if (!$request)
162+
{
163+
throw new Exception('<div class="alert alert-error"><b>SQL request failed</b>: <i>' . mysqli_error($this->con) . '</i></div>');
164+
return false;
165+
}
166+
else
167+
{
168+
return true;
169+
}
170+
}
160171
}
161172
?>

lib/pagination.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,18 @@ function generatePagination($currentPage, $totalPages, $numLinks = 5, $displayPr
4747
}
4848
else
4949
{
50+
// Even number of links
5051
if ($numLinks % 2 == 0)
5152
{
5253
$leftLinks = floor($numLinks / 2) - 1;
5354
$rightLinks = floor($numLinks / 2);
5455
}
55-
else {
56+
// Odd number of links
57+
else
58+
{
5659
$leftLinks = floor($numLinks / 2);
5760
$rightLinks = floor($numLinks / 2);
5861
}
59-
6062

6163
$firstPage = $currentPage - $leftLinks;
6264
$lastPage = $currentPage + $rightLinks;

0 commit comments

Comments
 (0)