consolidate all repos to one for archive
This commit is contained in:
169
semester_4/razvoj_aplikacij_za_internet/php/admin/models/ads.php
Normal file
169
semester_4/razvoj_aplikacij_za_internet/php/admin/models/ads.php
Normal file
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
/*
|
||||
Model za oglas. Vsebuje lastnosti, ki definirajo strukturo oglasa in sovpadajo s stolpci v bazi.
|
||||
Nekatere metode so statične, ker niso vezane na posamezen oglas: poišči vse oglase, vstavi nov oglas, ...
|
||||
Druge so statične, ker so vezane na posamezen oglas: posodobi oglas, izbriši oglas, ...
|
||||
|
||||
V modelu moramo definirati tudi relacije oz. povezane entitete/modele. V primeru oglasa je to $user, ki
|
||||
povezuje oglas z uporabnikom, ki je oglas objavil. Relacija nam poskrbi za nalaganje podatkov o uporabniku,
|
||||
da nimamo samo user_id, ampak tudi username, ...
|
||||
*/
|
||||
|
||||
require_once 'users.php'; // Vključimo model za uporabnike
|
||||
require_once 'ads_image.php'; // Vključimo model za slike
|
||||
require_once 'categorys.php'; // Vključimo model za kategorije
|
||||
|
||||
class Ad
|
||||
{
|
||||
public $id;
|
||||
public $title;
|
||||
public $description;
|
||||
public $user;
|
||||
public $ad_img;
|
||||
public $ads_categorys;
|
||||
|
||||
// Konstruktor
|
||||
public function __construct($id, $title, $description, $user_id)
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->title = $title;
|
||||
$this->description = $description;
|
||||
$this->user = User::findUserName($user_id);
|
||||
$this->ad_img = ads_image::find($id);
|
||||
$this->ads_categorys = Categorys::find($id);
|
||||
}
|
||||
|
||||
// Metoda, ki iz baze vrne vse oglase
|
||||
public static function all()
|
||||
{
|
||||
$db = Db::getInstance(); // pridobimo instanco baze
|
||||
$query = "SELECT * FROM ads ORDER BY ads.date DESC;"; // pripravimo query
|
||||
$res = $db->query($query); // poženemo query
|
||||
$ads = array();
|
||||
while ($ad = $res->fetch_object()) {
|
||||
// Za vsak rezultat iz baze ustvarimo objekt (kličemo konstuktor) in ga dodamo v array $ads
|
||||
array_push($ads, new Ad($ad->id, $ad->title, $ad->description, $ad->user_id));
|
||||
}
|
||||
return $ads;
|
||||
}
|
||||
|
||||
public static function findUser($id)
|
||||
{
|
||||
$db = Db::getInstance();
|
||||
$id = mysqli_real_escape_string($db, $id);
|
||||
$query = "SELECT * FROM ads WHERE ads.user_id = '$id';";
|
||||
$res = $db->query($query);
|
||||
$ads = array();
|
||||
while ($ad = $res->fetch_object()) {
|
||||
// Za vsak rezultat iz baze ustvarimo objekt (kličemo konstuktor) in ga dodamo v array $ads
|
||||
array_push($ads, new Ad($ad->id, $ad->title, $ad->description, $ad->user_id));
|
||||
}
|
||||
return $ads;
|
||||
}
|
||||
|
||||
// Metoda, ki vrne en oglas z specifičnim id-jem iz baze
|
||||
public static function find($id)
|
||||
{
|
||||
$db = Db::getInstance();
|
||||
$id = mysqli_real_escape_string($db, $id);
|
||||
$query = "SELECT * FROM ads WHERE ads.id = '$id';";
|
||||
$res = $db->query($query);
|
||||
if ($ad = $res->fetch_object()) {
|
||||
return new Ad($ad->id, $ad->title, $ad->description, $ad->user_id);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// Metoda, ki doda nov oglas v bazo
|
||||
public static function insert($title, $desc, $img, $categorys)
|
||||
{
|
||||
$db = Db::getInstance();
|
||||
$title = mysqli_real_escape_string($db, $title);
|
||||
$desc = mysqli_real_escape_string($db, $desc);
|
||||
$user_id = $_SESSION["USER_ID"]; // user_id vzamemo iz seje (prijavljen uporabnik)
|
||||
|
||||
$query = "INSERT INTO ads (title, description, user_id, date)
|
||||
VALUES('$title', '$desc', '$user_id', NOW());";
|
||||
|
||||
|
||||
|
||||
if($db->query($query))
|
||||
{
|
||||
$id = $db->insert_id;
|
||||
if(isset($categorys)){
|
||||
foreach($categorys as $category)
|
||||
{
|
||||
$query = "INSERT INTO ads_categorys (ads_id, categorys_id)
|
||||
VALUES ($id, $category)";
|
||||
$db->query($query);
|
||||
}
|
||||
}
|
||||
|
||||
if(isset($img)){
|
||||
$date = date("YmdHms");
|
||||
$img_path = "../slike/" . $date . $img['name'];
|
||||
move_uploaded_file($img['tmp_name'], $img_path);
|
||||
$img_path = "slike/" . $date . $img['name'];
|
||||
$img_path = "/" . $img_path;
|
||||
|
||||
$img_path = mysqli_real_escape_string($db, $img_path);
|
||||
$query = "INSERT INTO ads_image (ads_id, image) VALUES ('$id', '$img_path')";
|
||||
$db->query($query);
|
||||
}
|
||||
return Ad::find($id);
|
||||
}
|
||||
else
|
||||
{
|
||||
return null; // v primeru napake vrnemo null
|
||||
}
|
||||
}
|
||||
|
||||
// Metoda, ki posodobi obstoječ oglas v bazi
|
||||
public function update($title, $desc)
|
||||
{
|
||||
$db = Db::getInstance();
|
||||
$id = mysqli_real_escape_string($db, $this->id);
|
||||
$title = mysqli_real_escape_string($db, $title);
|
||||
$desc = mysqli_real_escape_string($db, $desc);
|
||||
|
||||
$query = "UPDATE ads SET title = '$title', description = '$desc' WHERE id = '$id'";
|
||||
|
||||
if ($db->query($query)) {
|
||||
return $id; //iz baze pridobimo posodobljen oglas in ga vrnemo controllerju
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public function addImg($img)
|
||||
{
|
||||
$db = Db::getInstance();
|
||||
$date = date("YmdHms");
|
||||
$img_path = "../slike/" . $date . $img['name'];
|
||||
move_uploaded_file($img['tmp_name'], $img_path);
|
||||
$img_path = "slike/" . $date . $img['name'];
|
||||
$img_path = "/" . $img_path;
|
||||
|
||||
$img_path = mysqli_real_escape_string($db, $img_path);
|
||||
$query = "INSERT INTO ads_image (ads_id, image) VALUES ('$this->id', '$img_path')";
|
||||
|
||||
if ($db->query($query)) {
|
||||
return $this->id; //iz baze pridobimo posodobljen oglas in ga vrnemo controllerju
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Metoda, ki izbriše oglas iz baze
|
||||
public function delete()
|
||||
{
|
||||
$db = Db::getInstance();
|
||||
$id = mysqli_real_escape_string($db, $this->id);
|
||||
$query = "DELETE FROM ads WHERE id = '$id'";
|
||||
if ($db->query($query)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
|
||||
class ads_image
|
||||
{
|
||||
public $id;
|
||||
public $ads_id;
|
||||
public $image;
|
||||
|
||||
|
||||
// Konstruktor
|
||||
public function __construct($id, $ads_id, $image)
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->ads_id = $ads_id;
|
||||
$this->image = $image;
|
||||
}
|
||||
|
||||
// Metoda, ki vrne uporabnika z določenim ID-jem iz baze
|
||||
public static function find($ads_id)
|
||||
{
|
||||
$db = Db::getInstance();
|
||||
$id = mysqli_real_escape_string($db, $ads_id);
|
||||
$query = "SELECT * FROM ads_image WHERE ads_image.ads_id = '$ads_id'";
|
||||
$res = $db->query($query);
|
||||
$images = array();
|
||||
while ($img = $res->fetch_object()) {
|
||||
array_push($images, new ads_image($img->id, $img->ads_id, $img->image));
|
||||
}
|
||||
return $images;
|
||||
}
|
||||
}
|
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
|
||||
class Categorys
|
||||
{
|
||||
public $id;
|
||||
public $title;
|
||||
|
||||
// Konstruktor
|
||||
public function __construct($id, $title)
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->title = $title;
|
||||
}
|
||||
|
||||
// Metoda, ki vrne uporabnika z določenim ID-jem iz baze
|
||||
public static function all()
|
||||
{
|
||||
$db = Db::getInstance();
|
||||
$query = "SELECT * FROM categorys";
|
||||
$res = $db->query($query);
|
||||
$categorys = array();
|
||||
while ($category = $res->fetch_object()) {
|
||||
array_push($categorys, new Categorys($category->id, $category->title));
|
||||
}
|
||||
return $categorys;
|
||||
}
|
||||
|
||||
public static function find($id)
|
||||
{
|
||||
$db = Db::getInstance();
|
||||
|
||||
$query = "SELECT categorys.id, categorys.title FROM ads_categorys, categorys
|
||||
WHERE ads_categorys.ads_id = '$id'
|
||||
AND ads_categorys.categorys_id = categorys.id";
|
||||
|
||||
$res = $db->query($query);
|
||||
$categorys = array();
|
||||
while ($category = $res->fetch_object()) {
|
||||
array_push($categorys, new Categorys($category->id, $category->title));
|
||||
}
|
||||
return $categorys;
|
||||
}
|
||||
}
|
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
require_once 'users.php';
|
||||
|
||||
class Comments
|
||||
{
|
||||
public $id;
|
||||
public $ads_id;
|
||||
public $user;
|
||||
public $content;
|
||||
public $country;
|
||||
|
||||
|
||||
// Konstruktor
|
||||
public function __construct($id, $ads_id, $users_id, $content, $country)
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->ads_id = $ads_id;
|
||||
$this->user = User::findUserName($users_id);;
|
||||
$this->content = $content;
|
||||
$this->country = $country;
|
||||
}
|
||||
|
||||
public static function insert($ads_id, $users_id, $content)
|
||||
{
|
||||
$url = "http://ip-api.com/json/" . $_SERVER['REMOTE_ADDR'] . "?fields=16385";
|
||||
$data = file_get_contents($url);
|
||||
$data = json_decode($data, true);
|
||||
if($data['status'] == 'success'){
|
||||
$country = $data['country'];
|
||||
} else {
|
||||
$country = 'localHost';
|
||||
}
|
||||
|
||||
|
||||
$db = Db::getInstance();
|
||||
$content = mysqli_real_escape_string($db, $content);
|
||||
$ads_id = mysqli_real_escape_string($db, $ads_id);
|
||||
$query = "INSERT INTO comments (ads_id, users_id, content, date, country)
|
||||
VALUES ('$ads_id', '$users_id', '$content', NOW(), '$country');";
|
||||
if($res = $db->query($query)) {
|
||||
return new Comments($db->insert_id, $ads_id, $users_id, $content, $country);
|
||||
} else {
|
||||
return array();
|
||||
}
|
||||
}
|
||||
|
||||
public static function all()
|
||||
{
|
||||
$db = Db::getInstance();
|
||||
$query = "SELECT * FROM comments ORDER BY comments.date DESC LIMIT 5";
|
||||
$res = $db->query($query);
|
||||
$comments = array();
|
||||
while ($commnet = $res->fetch_object()) {
|
||||
array_push($comments, new Comments($commnet->id, $commnet->ads_id, $commnet->users_id, $commnet->content, $commnet->country));
|
||||
}
|
||||
return $comments;
|
||||
}
|
||||
|
||||
public static function findForAd($ads_id)
|
||||
{
|
||||
$db = Db::getInstance();
|
||||
$id = mysqli_real_escape_string($db, $ads_id);
|
||||
$query = "SELECT * FROM comments WHERE comments.ads_id = '$ads_id'";
|
||||
$res = $db->query($query);
|
||||
$comments = array();
|
||||
while ($commnet = $res->fetch_object()) {
|
||||
array_push($comments, new Comments($commnet->id, $commnet->ads_id, $commnet->users_id, $commnet->content, $commnet->country));
|
||||
}
|
||||
return $comments;
|
||||
}
|
||||
|
||||
public static function find($id)
|
||||
{
|
||||
$db = Db::getInstance();
|
||||
$query = "SELECT * FROM comments WHERE comments.id = '$id'";
|
||||
$res = $db->query($query);
|
||||
if ($commnet = $res->fetch_object()) {
|
||||
return new Comments($commnet->id, $commnet->ads_id, $commnet->users_id, $commnet->content, $commnet->country);
|
||||
}
|
||||
}
|
||||
|
||||
public function delete()
|
||||
{
|
||||
$db = Db::getInstance();
|
||||
$id = mysqli_real_escape_string($db, $this->id);
|
||||
$query = "DELETE FROM comments WHERE id = '$id'";
|
||||
if ($db->query($query)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
/*
|
||||
Prazna datoteka, ki simulira model za 'pages_controller'.
|
||||
Potrebujemo jo, ker bi sicer dobili napako pri dinamičnem nalaganju datotek v funkciji call (routes.php).
|
||||
Kot alternativa, bi lahko modele nalagali v controllerjih, namesto v funkciji call. Potem te datoteke ne bi potrebovali.
|
||||
*/
|
||||
|
||||
require_once 'models/users.php';
|
@@ -0,0 +1,168 @@
|
||||
<?php
|
||||
|
||||
// Model za uporabnika
|
||||
/*
|
||||
Model z uporabniki.
|
||||
Čeprav nimamo users_controller-ja, ta model potrebujemo pri oglasih,
|
||||
saj oglas vsebuje podatke o uporabniku, ki je oglas objavil.
|
||||
Razred implementira metodo find, ki jo uporablja Ads model zato, da
|
||||
user_id zamenja z instanco objekta User z vsemi podatki o uporabniku.
|
||||
*/
|
||||
|
||||
class User
|
||||
{
|
||||
public $id;
|
||||
public $username;
|
||||
public $email;
|
||||
public $ime;
|
||||
public $priimek;
|
||||
public $naslov;
|
||||
public $posta;
|
||||
public $telefon;
|
||||
public $adm;
|
||||
|
||||
// Konstruktor
|
||||
public function __construct($id, $username, $adm, $email, $ime, $priimek, $naslov, $posta, $telefon)
|
||||
{
|
||||
$this->id = $id;
|
||||
$this->username = $username;
|
||||
$this->adm = $adm;
|
||||
$this->email = $email;
|
||||
$this->ime = $ime;
|
||||
$this->priimek = $priimek;
|
||||
$this->naslov = $naslov;
|
||||
$this->posta = $posta;
|
||||
$this->telefon = $telefon;
|
||||
}
|
||||
|
||||
// Metoda, ki vrne uporabnika z določenim ID-jem iz baze
|
||||
public static function find($id)
|
||||
{
|
||||
$db = Db::getInstance();
|
||||
$id = mysqli_real_escape_string($db, $id);
|
||||
$query = "SELECT * FROM users WHERE id = $id;";
|
||||
$res = $db->query($query);
|
||||
if ($user = $res->fetch_object()) {
|
||||
return new User($user->id, $user->username, $user->adm, $user->email, $user->ime, $user->priimek, $user->naslov, $user->posta, $user->telefon);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function findUserName($id)
|
||||
{
|
||||
$db = Db::getInstance();
|
||||
$id = mysqli_real_escape_string($db, $id);
|
||||
$query = "SELECT users.username FROM users WHERE id = $id;";
|
||||
$res = $db->query($query);
|
||||
if ($user = $res->fetch_object()) {
|
||||
return $user->username;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function all()
|
||||
{
|
||||
$list = array();
|
||||
$db = Db::getInstance();
|
||||
$query = "SELECT * FROM users;";
|
||||
$res = $db->query($query);
|
||||
while ($user = $res->fetch_object()) {
|
||||
$list[] = new User($user->id, $user->username, $user->adm, $user->email, $user->ime, $user->priimek, $user->naslov, $user->posta, $user->telefon);
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
|
||||
public static function validate_login($username, $password)
|
||||
{
|
||||
$db = Db::getInstance();
|
||||
$username = mysqli_real_escape_string($db, $username);
|
||||
$pass = sha1($password);
|
||||
$query = "SELECT * FROM users WHERE username='$username' AND password='$pass'";
|
||||
$res = $db->query($query);
|
||||
if($user_obj = $res->fetch_object()){
|
||||
return new User($user_obj->id, $user_obj->username, $user_obj->adm, $user_obj->email, $user_obj->ime, $user_obj->priimek, $user_obj->naslov, $user_obj->posta, $user_obj->telefon);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function username_exists($username){
|
||||
$db = Db::getInstance();
|
||||
$username = mysqli_real_escape_string($db, $username);
|
||||
$query = "SELECT * FROM users WHERE username='$username'";
|
||||
$res = $db->query($query);
|
||||
return mysqli_num_rows($res) > 0;
|
||||
}
|
||||
|
||||
public static function createUser($username, $password, $email, $ime, $priimek, $naslov, $posta, $telefon){
|
||||
$db = Db::getInstance();
|
||||
$username = mysqli_real_escape_string($db, $username);
|
||||
$password = sha1($password);
|
||||
$email = mysqli_real_escape_string($db, $email);
|
||||
$ime = mysqli_real_escape_string($db, $ime);
|
||||
$priimek = mysqli_real_escape_string($db, $priimek);
|
||||
$naslov = mysqli_real_escape_string($db, $naslov);
|
||||
$posta = mysqli_real_escape_string($db, $posta);
|
||||
$telefon = mysqli_real_escape_string($db, $telefon);
|
||||
$query = "INSERT INTO users (username, password, email, ime, priimek, naslov, posta, telefon)
|
||||
VALUES ('$username', '$password', '$email', '$ime', '$priimek', '$naslov', '$posta', '$telefon');";
|
||||
if($db->query($query)){
|
||||
return true;
|
||||
}
|
||||
else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static function insert($username, $password){
|
||||
$db = Db::getInstance();
|
||||
$username = mysqli_real_escape_string($db, $username);
|
||||
$password = sha1($password);
|
||||
$query = "INSERT INTO users (username, password, email, ime, priimek, naslov, posta, telefon)
|
||||
VALUES ('$username', '$password', '', '', '', '', '', '');";
|
||||
if($db->query($query)){
|
||||
return $db->insert_id;
|
||||
}
|
||||
else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function delete()
|
||||
{
|
||||
$db = Db::getInstance();
|
||||
$id = mysqli_real_escape_string($db, $this->id);
|
||||
$query = "DELETE FROM users WHERE id = '$id'";
|
||||
if ($db->query($query)) {
|
||||
$query = "DELETE FROM comments WHERE comments.users_id = '$id'";
|
||||
$db->query($query);
|
||||
$query = "DELETE FROM ads WHERE ads.user_id = '$id'";
|
||||
$db->query($query);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function update( $username, $email, $ime, $priimek, $naslov, $posta, $telefon)
|
||||
{
|
||||
$db = Db::getInstance();
|
||||
$id = mysqli_real_escape_string($db, $this->id);
|
||||
$username = mysqli_real_escape_string($db, $username);
|
||||
$email = mysqli_real_escape_string($db, $email);
|
||||
$ime = mysqli_real_escape_string($db, $ime);
|
||||
$priimek = mysqli_real_escape_string($db, $priimek);
|
||||
$naslov = mysqli_real_escape_string($db, $naslov);
|
||||
$posta = mysqli_real_escape_string($db, $posta);
|
||||
$telefon = mysqli_real_escape_string($db, $telefon);
|
||||
|
||||
$query = "UPDATE users SET
|
||||
username = '$username', email = '$email', ime = '$ime', priimek = '$priimek', naslov = '$naslov', posta = '$posta', telefon = '$telefon' WHERE id = '$id'";
|
||||
|
||||
if ($db->query($query)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user