Skip to content

Commit

Permalink
User can now generate baby reg link by clicking Share icon: #130
Browse files Browse the repository at this point in the history
  • Loading branch information
khalidsaadat committed Mar 31, 2022
1 parent 91cc017 commit 691a223
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 13 deletions.
46 changes: 46 additions & 0 deletions app/controllers/BabyRegistryController.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,50 @@
<?php
class BabyRegistryController extends Controller{

private function crypto_rand_secure($min, $max) {
$range = $max - $min;
if ($range < 0) return $min; // not so random...
$log = log($range, 2);
$bytes = (int) ($log / 8) + 1; // length in bytes
$bits = (int) $log + 1; // length in bits
$filter = (int) (1 << $bits) - 1; // set all lower bits to 1
do {
$rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes)));
$rnd = $rnd & $filter; // discard irrelevant bits
} while ($rnd >= $range);
return $min + $rnd;
}

private function getToken($length=12){
$token = "";
$codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$codeAlphabet.= "0123456789";
for($i=0;$i<$length;$i++){
$token .= $codeAlphabet[$this->crypto_rand_secure(0,strlen($codeAlphabet))];
}
return $token;
}

// Generate shareable uniqe key
public function generate() {
$data = [];
$token = $this->getToken(37);

$baby_reg_id = $_POST['baby_registry_id'];

// Baby reg token
$baby_reg_token = $this->model('BabyRegistryToken');
$baby_reg_token->baby_registry_id = $baby_reg_id;
$baby_reg_token->token = $token;
$baby_reg_token->status = 1;
$baby_reg_token->insert();

$data['success'] = true;
$data['token'] = $token;

echo json_encode($data);
}

// Function that shows the index page when you type 'localhost'
public function index(){

Expand All @@ -12,4 +56,6 @@ public function add() {
$this->view('BASE_STRUCTURES/empty_view');
}



}
50 changes: 50 additions & 0 deletions app/models/BabyRegistryToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
class BabyRegistryToken extends Model{

public function __construct()
{
parent::__construct();
}

public function getAll(){
$stmt = self::$_connection->prepare("SELECT * FROM baby_registry_tokens");
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_CLASS, 'BabyRegistryToken');
return $stmt->fetchAll();
}

public function find($token_id){
$stmt = self::$_connection->prepare("SELECT * FROM baby_registry_tokens WHERE token_id = :token_id");
$stmt->execute(['token_id'=>$token_id]);
$stmt->setFetchMode(PDO::FETCH_CLASS, 'BabyRegistryToken');
return $stmt->fetch();
}

public function findByBabyRegistryId($baby_registry_id){
$stmt = self::$_connection->prepare("SELECT * FROM baby_registry_tokens WHERE baby_registry_id = :baby_registry_id");
$stmt->execute(['baby_registry_id'=>$baby_registry_id]);
$stmt->setFetchMode(PDO::FETCH_CLASS, 'BabyRegistryToken');
return $stmt->fetch();
}

public function insert(){
$stmt = self::$_connection->prepare("INSERT INTO baby_registry_tokens(baby_registry_id, token, status) VALUES(:baby_registry_id, :token, :status)");
$stmt->execute(['baby_registry_id'=>$this->baby_registry_id,
'token'=>$this->token,
'status'=>$this->status]);
$_SESSION['updated_address_id'] = self::$_connection->lastInsertId();
}

public function delete(){
$stmt = self::$_connection->prepare("DELETE FROM baby_registry_tokens WHERE brand_id = :brand_id");
$stmt->execute(['brand_id'=>$this->brand_id]);
}

public function updateStatus(){
$stmt = self::$_connection->prepare("UPDATE baby_registry_tokens SET brand_name = :brand_name WHERE brand_id = :brand_id");
$stmt->execute(['brand_name'=>$this->brand_name,
'brand_id'=>$this->brand_id]);
}

}
?>
81 changes: 68 additions & 13 deletions app/views/baby_registry/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@
<li class="nav-item" role="presentation">
<a class="nav-link active" id="my-registry-tab" data-toggle="pill" href="#my-registry" role="tab" aria-controls="my-registry" aria-selected="true">My Registry</a>
</li>
<li class="nav-item" role="presentation">
<a class="nav-link" id="add-items-tab" data-toggle="pill" href="#add-items" role="tab" aria-controls="add-items" aria-selected="false">Add Items</a>
</li>
<li class="nav-item" role="presentation">
<a class="nav-link" id="settings-tab" data-toggle="pill" href="#settings" role="tab" aria-controls="settings" aria-selected="false">Settings</a>
</li>
Expand All @@ -72,19 +69,75 @@
<div class="container-fluid">
<h2 class="mb-3 font-weight-bold">My Baby Registry</h2>

something
<form class="share_form" method="post">
<div class="col-lg-12" style="background: #f5f7f7; border: 1px solid #EFF2F2; border-radius: 5px; padding: 15px; margin-bottom: 15px;">
<input type="hidden" name="baby_registry_id" id="baby_registry_id" value="1">
<div>
<span style="font-weight: bold; font-size: 18px;">Baby Registry Title</span>
</div>
<div>
<span style="color: #575959; font-size: 14px;">
Created by <strong>Anum and Shabia</strong> on 1 April, 2022
</span>
</div>


</div>
</div>
<div class="tab-pane fade" id="add-items" role="tabpanel" aria-labelledby="add-items-tab">
<div class="container-fluid">
<h2 class="mb-3 font-weight-bold">Add Items</h2>

sometihng
<hr>
<div style="display: flex; align-items: center;">
<img src="/assets/icons/settings.png" height="14" style="vertical-align:middle; padding-right: 5px;"><span style="font-size: 11px;">SETTINGS</span>
<span style="padding-left: 10px; padding-right: 10px;"></span>


<button type="submit" style="background: none; border: none;" data-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria-controls="collapseExample">
<img src="/assets/icons/link.png" height="14" style="vertical-align:middle; padding-right: 5px;">
<span style="font-size: 11px;" onclick="">SHARE</span>
</button>


<span style="padding-left: 10px; padding-right: 10px;"></span>

<img src="/assets/icons/option.png" height="14" style="vertical-align:middle; padding-right: 5px;"><span style="font-size: 11px;">MORE</span>
</div>

<div class="collapse" id="collapseExample">
<div class="card card-body">
<div class="row">
<div class="col-lg-7">
<a href="" id="shareable_link_url" style="color: blue;"></a>
</div>
<div class="col-lg-4">
<img src="/assets/icons/copy.png" height="14" style="vertical-align:middle; padding-right: 5px;">
</div>
</div>
</div>
</div>


</div>
</form>

<div class="col-lg-12" style="background: #f5f7f7; border: 1px solid #EFF2F2; border-radius: 5px; padding: 15px;">
<div>
<span style="font-weight: bold; font-size: 18px;">Another Title</span>
</div>
<div>
<span style="color: #575959; font-size: 14px;">
Created by <strong>Khalid</strong> on 1 February, 2022
</span>
</div>

<hr>
<div style="display: flex; align-items: center;">
<img src="/assets/icons/settings.png" height="14" style="vertical-align:middle; padding-right: 5px;"><span style="font-size: 11px;">SETTINGS</span>
<span style="padding-left: 10px; padding-right: 10px;"></span>

<img src="/assets/icons/link.png" height="14" style="vertical-align:middle; padding-right: 5px;">
<span style="font-size: 11px;" onclick="">SHARE</span>

<span style="padding-left: 10px; padding-right: 10px;"></span>

<img src="/assets/icons/option.png" height="14" style="vertical-align:middle; padding-right: 5px;"><span style="font-size: 11px;">MORE</span>
</div>
</div>

</div>
</div>
<div class="tab-pane fade" id="settings" role="tabpanel" aria-labelledby="settings-tab">
Expand All @@ -103,6 +156,8 @@
</section>
<!-- Checkout Section End -->



<?php
// global footer - do not write file extension (.php)
$this->view('include/footer');
Expand Down
40 changes: 40 additions & 0 deletions app/views/include/footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,46 @@ function shownTabsHandler(e) {
</script>
<!-- generate baby reg link using jquery -->
<script>
$('form.share_form').submit(function(event) {
event.preventDefault();
var $form = $(this);
var serialized_data = $form.serialize();
request = $.ajax({
url: "/babyregistry/generate",
type: "post",
data: serialized_data,
dataType: "json",
encode: true,
}).done(function(response) {
if(!response.success) {
console.log('error');
}
else {
// get the token and show it
console.log(response.token);
var url = "/babyregistry/shareable/" + response.token;
$("#shareable_link_url").attr({href: url});
$("#shareable_link_url").html("");
$("#shareable_link_url").append("/babyregistry/shareable/" + response.token);
}
});
});
</script>
<!-- Copy a text -->
<script>
function copyText() {
}
</script>
<script>
$(document).ready(function () {
//Initialize tooltips
Expand Down
Binary file added assets/icons/copy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/link.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/option.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 691a223

Please sign in to comment.