SOCMED/home.php

170 lines
4.2 KiB
PHP
Raw Normal View History

2020-01-02 21:48:25 +07:00
<?php
include('application/config/config.php');
include('application/model/user.php');
include('application/model/home.php');
include('foto.php');
session_start();
//route
if( !isset($_SESSION['username'])){ //if not login
header('Location: index.php');
$_SESSION['alert']='Anda Harus Login Terlebih Dahulu';
}else{
if (isset($_GET['action'])){
switch($_GET['action']){
case 'logout' :
logout();
break;
case 'regis' :
regis();
break;
case 'appFriend' :
appFriend($_GET['id']);
break;
case 'rejFriend' :
rejFriend($_GET['id']);
break;
}
}
if(!cekRegistrasi($_SESSION['username'])){ //jika belum lengkapi profil
include(view_file.'register.php');
}
else if(isset($_GET['search'])){
search($_GET['search']);
}
else if(isset($_GET['action']) && $_GET['action'] == 'search'){
cari();
}
else{
$data = homeContent();
include(view_file.'home.php');
}
}
//function
function cekRegistrasi($data){//buat cek registrasi
$user = new User();
if($user->cekProfil($data))return true;
else return false;
}
function regis(){//buat registrasi
$u = new User();
$data = array( 'nama' => $_POST['name'],
'dob' => date("Y-m-d",strtotime($_POST['dob'])),
'telp' => $_POST['telp'],
'address' => $_POST['address'],
'hometown' => $_POST['hometown'],
'Fakultas' => $_POST['fakultas'],
'Jurusan' => $_POST['jurusan'],
'Angkatan' => $_POST['angkatan'],
'Kelas' => $_POST['kelas'],
'relationship' => $_POST['relati'],
'user' => $_SESSION['username']
);
if ($u->inputProfil($data)){
//echo ' insert bisa ';
unset($_SESSION['notif-alert']);
$idp = $u->getId();
if (!empty($_FILES['file']['name'])){
$foto = new Foto($_FILES);
if($foto->insert($_SESSION['username'])){
$hasil = $foto->getHasil();
$photo = $hasil['lokasi'];
//echo $photo;
if($u->inputAvatar($photo,$idp))header('Location: home.php');
}
else {
$_SESSION['notif-alert'] = 'Foto yang anda Masukkan Salah';
if($u->delProfil($idp))header('Location: home.php');
}//jika foto salah delete data tadi dan kembali
}
else header('Location: home.php');
}
else{
$_SESSION['notif-alert'] = 'Periksa kembali inputan';
header('Location: home.php');
}
}
function homeContent(){
$h = new Home();
$data['member'] = $h->getAllmember();
$data['post'] = $h->getAllpost();
$data['comm'] = $h->getAllcomm();
//$data['act'] = $h->getAllact();
$data['notiffriend'] = $h->getNotiffriend();
//buat database error
unset($_SESSION['notif-warning']);
//$_SESSION['notif-warning'] = $h->getSqlerr();
return $data;
}
function appFriend($id){
$u = new User();
if($u->aproveFriend($id)){
header('Location: home.php');
}
}
function rejFriend($id){
$u = new User();
if($u->rejectFriend($id)){
header('Location: home.php');
}
}
function search($key){
$u = new User();
$cari = $u->Search($key);
echo '<p id="searchresults">';
echo '<span class="category">User</span>';
if (isset($cari['user']) && (mysql_num_rows($cari['user'])>0)){
while ($row = mysql_fetch_assoc($cari['user'])) {
echo '<a href="profile.php?id='.$row['username'].'">';
echo '<img src="'.$row['avatar'].'" alt="" />';
echo '<span class="searchheading">'.$row['nama'].'</span>';
echo '<span>Class of '.$row['angkatan'].'</span>';
echo '</a>';
}
}else{
echo '<span class="no">No Result</span>';
}
echo '<span class="category">Community</span>';
if (isset($cari['community']) && (mysql_num_rows($cari['community'])>0)){
while ($row = mysql_fetch_assoc($cari['community'])) {
echo '<a href="community.php?id='.$row['id'].'">';
echo '<img src="'.$row['photo'].'" alt="" />';
echo '<span class="searchheading">'.$row['name'].'</span>';
echo '<span class="desc">'.$row['type'].'</span>';
echo '</a>';
}
}else{
echo '<span class="no">---No Result---</span>';
}
echo '</p>';
}
function cari(){
$u = new User();
$cari = $u->Cari();
$i = 0;
$hasil= array();
while ($row = mysql_fetch_assoc($cari['user'])){
$hasil[$i]['key'] = $row['username'];
$hasil[$i]['value'] = $row['nama'];
$i++;
}
echo json_encode($hasil);
}
function logout(){//buat logout
unset($_SESSION['username']);
session_destroy();
header('Location: index.php');
}
?>