class User {
public $image;
public function __construct($image)
{
$this->image = $image;
}
public function getUser($id)
{
$db = Db::getConnection();
$sql = "SELECT * FROM user WHERE id = :id";
$stmt = $db->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetch();
}
}
class Image {
private $user_id;
public function __construct($id)
{
$this->user_id = $id;
}
public function getImage()
{
$db = Db::getConnection();
$sql = "SELECT IF(image IS NULL or image = '','no_image.png',image) as image FROM `user` WHERE id = :id";
$result = $db->prepare($sql);
$result->bindParam(':id', $this->user_id, PDO::PARAM_INT);
$result->execute();
return $result->fetchColumn();
}
public static function UploadImage($image)
{
$usersImage = $image['userfile']['tmp_name'];
$imageName = $image['userfile']['name'];
if(is_uploaded_file($image['userfile']['tmp_name'])){
move_uploaded_file($usersImage, $_SERVER['DOCUMENT_ROOT']."/template/images/users/".$image['userfile']['name']);
}
return $imageName;
}
public function UpdateImage($image) {
$usersImage = $image['userphoto']['tmp_name'];
$imageName = $image['userphoto']['name'];
if(is_uploaded_file($image['userphoto']['tmp_name'])){
move_uploaded_file($usersImage, $_SERVER['DOCUMENT_ROOT']."/template/images/users/".$image['userphoto']['name']);
}
$db = Db::getConnection();
$sql = "UPDATE user SET image = '$imageName' WHERE id = $this->user_id ";
$db->query($sql);
return true;
}
}
class UserController extends BaseController {
public function actionProfile($id) {
$user = new User(new Image($id));
if(isset($_POST['submit_photo'])) {
$user->image->updateImage($_FILES);
}
return $this->render('user/profile.php',[
'user' => $user->getUser($id),
'image'=> $user->image->getImage()
]);
}
namespace Repository;
class User {
protected $db;
public function __construct(Adapter $db) {
$this->db = $db;
}
public function getUserById($id) {
$sql = "SELECT * FROM user WHERE id = :id";
$stmt = $this->db->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetch();
}
public function getImageById($id) {
$sql = "SELECT IF(image IS NULL or image = '','no_image.png',image) as image FROM `user` WHERE id = :id";
$result = $this->db->prepare($sql);
$result->bindParam(':id', $id, PDO::PARAM_INT);
$result->execute();
return $result->fetchColumn();
}
public function updateImageById($imageName, $id) {
$sql = "UPDATE user SET image = '$imageName' WHERE id = $id";
return $this->db->query($sql);
}
}
namespace Service;
class Image {
protected $userRepo;
function __construct(Repository\User $userRepo) {
$this->userRepo = $userRepo;
}
public function UploadImage(array $image) {
$usersImage = $image['userfile']['tmp_name'];
$imageName = $image['userfile']['name'];
if (is_uploaded_file($image['userfile']['tmp_name'])) {
move_uploaded_file($usersImage, $_SERVER['DOCUMENT_ROOT'] . "/template/images/users/" . $image['userfile']['name']);
}
return $imageName;
}
public function updateImage(array $image, $id) {
if($imageName = $this->uploadImage($image, $id)) {
$this->userRepo->updateImageById($imageName, $id);
return true;
}
}
}
class UserController extends BaseController {
protected $imageService;
protected $userRepo;
function __construct(Service\Image $imageService, Repository\User $userRepo) {
$this->imageService = $imageService;
$this->userRepo = $userRepo;
}
public function actionProfile($id) {
if (isset($_POST['submit_photo'])) {
$this->imageService->updateImage($_FILES, $id);
}
return $this->render('user/profile.php', [
'user' => $this->userRepo->getUserById($id),
'image' => $this->userRepo->getImageById($id)
]);
}
}