Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions irakli_kandelaki/challenge_two/challenge-two.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php include_once "getdata.php" ?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Chivo:
wght@300;400;700&family=Lobster&family=Poppins:wght@200;400&family=Roboto:wght@400;500;700&display=swap"
rel="stylesheet">
<link rel="stylesheet" href="../style/style.css" type="text/css">
<title>Challenge N2</title>
</head>
<body>
<div class="challenge_two-container">
<nav class="navigation">
<a href="../index.php">Challenge 1</a>
<a href="/challenge_two/challenge-two.php">Challenge 2</a>
<label for="username" class="link-to-input">Input GIT</label>
</nav>
<section class="main-section-git">
<!-- Renders a form until we have a data to print it to screen -->

<!-- If the user exists, both were checked, render the user's repos and followers -->
<?php if (isset($_POST) && $haveData === true): ?>
<?php if ($_POST['repos'] === 'yes' && $_POST['followers'] === 'yes'): ?>
<div class="git-container">
<?php
include 'renderRepos.php';
include 'renderFollowers.php';
?>
</div>

<!-- If only repos were checked, render repos -->
<?php elseif ($_POST['repos'] === 'yes'): ?>
<?php include 'renderRepos.php' ?>

<!-- if only followers were checked, render followers -->
<?php elseif ($_POST['followers'] === 'yes'): ?>
<div class='only-followers'>
<?php include 'renderFollowers.php' ?>
</div>

<!-- If none were checked, render form and show the error message -->
<?php else: ?>
<?php include 'renderForm.php' ?>
<?php $errors[] = "You must check at least one of the boxes"?>
<p class='error-message'>You must check at least one of the boxes</p>
<?php endif ?>
<!-- Render form at the beginning, when the user has not yet submitted the data -->
<?php elseif (isset($_POST) && $haveData === false): ?>
<?php include 'renderForm.php' ?>
<?php foreach ($errors as $error): ?>
<p class="error-message"><?= $error ?></p>
<?php endforeach ?>
<?php endif ?>
</section>
</div>
</body>
</html>
21 changes: 21 additions & 0 deletions irakli_kandelaki/challenge_two/getFollowers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
$followers_nested = [];
for ($i = 0; $i < $number_of_pages_followers; $i++) {
$url_followers = "https://api.github.com/users/{$username}/followers?per_page=100&page=$i";
$resource2 = curl_init($url_followers);
curl_setopt_array($resource2, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'User-Agent: PHP',
'Content-Type: application/json'],
]);
$result_followers = json_decode(curl_exec($resource2));
$followers_nested[] = $result_followers;
}

$followers = unnest_array($followers_nested);

if ($followers !== ['']) {
$haveDataFollowers = true;
};
?>
27 changes: 27 additions & 0 deletions irakli_kandelaki/challenge_two/getRepos.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
$repos_nested = [];
for ($i = 1; $i <= $number_of_pages_repos; $i++) {
// Using curl to fetch data about user's repos.
// if there's more than one page to be fetched, we can do it like this :)
$urlRepos = "https://api.github.com/users/".$username."/repos?per_page=100&page=".$i;
$resource_user = curl_init($urlRepos);
curl_setopt_array($resource_user, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'User-Agent: PHP',
'Content-Type: application/json'],
]);
$result = json_decode(curl_exec($resource_user));
$repos_nested[] = $result;
curl_close($resource_user);
}

$repos = unnest_array($repos_nested);

// If the user does not exist, save error message
if (!$resource_user) {
$errors[] = "Github user not found";
} else {
$haveData = true;
}
?>
55 changes: 55 additions & 0 deletions irakli_kandelaki/challenge_two/getdata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
$haveData = false;
$haveDataFollowers = false;
$errors = [];

// get the number of followers or repos the user has
function get_followers_and_repos($url, $username) {
$url_user = $url . $username;
$resource = curl_init($url_user);
curl_setopt_array($resource, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'User-Agent: PHP',
'Content-Type: application/json'],
]);
$result = json_decode(curl_exec($resource), true);
curl_close($resource);
return $result;
}

// calculate number of pages that we have to fetch
function calculate_pages($info) {
return ceil($info / 100);
}

// unnest the given array
function unnest_array($arr) {
$unnested = [];
// Unnesting the array
for ($i = 0; $i < count($arr); $i++) {
for ($j = 0; $j < count($arr[$i]); $j++) {
$unnested[] = $arr[$i][$j];
}
}
return $unnested;
}

if ($_POST["username"]) {
$username = $_POST["username"];

// Get data about the number of repos and followers the user has
$data = get_followers_and_repos("https://api.github.com/users/", $username);
// Calculate how many pages of followers and repos there are
// to find out how many times we have to call github api
$number_of_pages_followers = calculate_pages($data["followers"]);
$number_of_pages_repos = calculate_pages($data["public_repos"]);

// Get data about the User's repositories and put them into the array
require 'getRepos.php';

// Get data about the user's followers
require 'getFollowers.php';
}

?>
18 changes: 18 additions & 0 deletions irakli_kandelaki/challenge_two/renderFollowers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!-- Render followers -->
<section class="upload-section follower-section section-results">
<h2><?= $username ?>'s followers</h2>
<ul>
<?php foreach ($followers as $i => $follower): ?>
<li class='list-item-followers'>
<span class='follower-num'><?= ++$i ?>)</span>
<div class='img-name-wrapper'>
<a href="<?= $follower->html_url ?>" target='_blank' class='follower-link-image'>
<img src="<?= $follower->avatar_url ?>" class='follower-image'>
</a>
<span class='follower-title'><?= $follower->login ?></span>
</div>
<a href="<?= $follower->html_url ?>" target='_blank' class='follower-link'>See profile</a>
</li>
<?php endforeach ?>
</ul>
</section>
15 changes: 15 additions & 0 deletions irakli_kandelaki/challenge_two/renderForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!-- Render form -->
<form class="form-get" method="post" action="/challenge_two/challenge-two.php">
<h1>Input your git Username to see magic!</h1>
<input type="text" id="username" name="username" class="input input-text">
<p class="checklist-title">What do you want to see?</p>
<div class="checkbox">
<input type="checkbox" name="repos" id="repos" value="yes">
<label for="repos">Repositories</label>
</div>
<div class="checkbox">
<input type="checkbox" name="followers" id="followers" value="yes">
<label for="followers">Followers</label>
</div>
<button type="submit" name="submit" class="label-button-two">Submit</button>
</form>
15 changes: 15 additions & 0 deletions irakli_kandelaki/challenge_two/renderRepos.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!-- Render repositories -->
<section class="main-section repo-section section-results">
<h2><?= $username ?>'s repositories</h2>
<ol class="list list-repos">
<?php foreach ($repos as $i => $repo): ?>
<li class='list-item'>
<span class='repo-num'><?=++$i?>)</span>
<a href=". $repo->html_url ." target='_blank' class='repo-name-link'>
<span class='repo-title'><?= $repo->name ?></span>
</a>
<a href=". $repo->html_url ." target='_blank' class='repo-link'>Go to repo</a>
</li>
<?php endforeach ?>
</ol>
</section>
48 changes: 48 additions & 0 deletions irakli_kandelaki/check_data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
// Creating variables for the uploaded image
$target_dir = "./uploads/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
$target_file = str_replace(' ', '-', $target_file);
$image_file_type = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));

// Creating variables for name and last name
$name = ucfirst($_POST['name']);
$last_name = ucfirst($_POST['last-name']);

// Creating an array for errors
$errors = [];

// Check the information after the submit
if(isset($_POST["submit"])) {

// Check if name and last name were input
if (!$name || !$last_name) {
$errors[] = "Please fill all of the fields";
}

// Check if name and last name were inputted correctly
if (!ctype_alpha($name) || !ctype_alpha($last_name)) {
$errors[] = "Name and last name should only contain letters";
}

// Check if the image was uploaded

if ($_FILES['image']) {
// Check the type of an image
if ($image_file_type !== 'png' && $image_file_type !== 'jpg'
&& $image_file_type !== 'jpeg' && $image_file_type !== 'gif') {
$errors[] = "Please upload a valid image";
}

// Check if the image is too large
if ($_FILES["image"]["size"] > 500000) {
$errors[] = "Sorry, the file is too large";
}

// If there were no errors, save the image
if (!$errors) {
move_uploaded_file($_FILES["image"]["tmp_name"], $target_file);
}
}
}
?>
Binary file added irakli_kandelaki/images/question-mark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 69 additions & 0 deletions irakli_kandelaki/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php require "check_data.php" ?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Chivo:
wght@300;400;700&family=Lobster&family=Poppins:wght@200;400&family=Roboto:wght@400;500;700&display=swap"
rel="stylesheet">
<link rel="stylesheet" href="./style/style.css" type="text/css">
<title>Challenge N1</title>
</head>
<body>
<div class="index-container">
<nav class="navigation">
<a href="index.php">Challenge 1</a>
<a href="../challenge_two/challenge-two.php">Challenge 2</a>
<label for="name" class="link-to-input">Name</label>
<label for="last-name" class="link-to-input">Last name</label>
<label for="image" class="link-to-input">Upload photo</label>
</nav>
<section class="main-section">
<form class="form-upload" action="/index.php" method="post" enctype="multipart/form-data">
<div class="form form--name">
<label for="name" class="label">Name</label>
<input type="text" id="name" name="name" class="input input-text" required>
</div>
<div class="form form--email">
<label for="last-name" class="label">Last name</label>
<input type="text" id="last-name" name="last-name" class="input input-text" required>
</div>
<div class="form form--image">
<label for="image" class="label label-button">Upload your image</label>
<input type="file" id="image" name="image" class="hidden">
</div>
<button type="submit" name="submit" value="Submit" class="label-button">Submit</button>
</form>
<!-- Displays who are you ONLY if post is null -->
<?php if (empty($_POST)): ?>
<h1 class="main-title">Who are you?</h1>
<?php endif ?>
</section>
<section class="upload-section">
<div class="about-user">
<?php if (!empty($_POST)): ?>
<!-- Print the errors to the user if there are any -->
<?php if ($errors): ?>
<?php foreach($errors as $error): ?>
<h4 class="not-uploaded-message"><?= $error ?></h4>
<?php endforeach ?>
<img src="/images/question-mark.png" class="image">
<!-- If there are no errors, print the input information -->
<?php else: ?>
<div class="about-user upload-successful">
<p class="hello-message">Hello <?=$name . ' ' . $last_name?></p>
<img src=<?=$target_file?> class="uploaded-image">
</div>
<?php endif ?>
<?php else: ?>
<img src="/images/question-mark.png" class="image">
<?php endif ?>
</div>
</section>
</div>
</body>
</html>
Loading