/Src/News/Article.php
https://gitlab.com/debashishroy/news-portal · PHP · 323 lines · 287 code · 10 blank · 26 comment · 36 complexity · 644b7ab19f4a26637820d058c94ece0a MD5 · raw file
- <?php
- /**
- * Created by PhpStorm.
- * User: Debashish
- * Date: 10/1/2016
- * Time: 2:34 PM
- */
- namespace News;
- use PDO;
- use Session\Session;
- class Article
- {
- public $title;
- public $category = 1;
- public $image;
- public $description;
- public $puid;
- public $link;
- public function __construct()
- {
- try {
- #session_start();
- $this->link = new PDO("mysql:host=localhost;dbname=news_portal", "root","");
- $this->link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- if (!$this->link) {
- echo "ERROR: " .$this->link->errorInfo();
- }
- } catch (PDOException $ex) {
- echo $ex->getMessage();
- }
- }
- public function prepare($data = "")
- {
- if(array_key_exists("title", $data )) {
- if (!empty($data["title"])) {
- $this->title = filter_var($data["title"], FILTER_SANITIZE_STRING);
- } else {
- Session::set('errTitle', "Enter Title");
- }
- }
- if(array_key_exists("image",$data)){
- if(!empty($data["image"]["name"])) {
- $this->image = $data["image"];
- }else{
- Session::set("errImage", 'Choose an image');
- }
- }
- if(array_key_exists("description",$data)){
- if(!empty($data["description"])) {
- $this->description = $data["description"];
- }else{
- Session::set("errDescription", 'Enter Description');
- }
- }
- if(array_key_exists("category", $data)){
- if(empty($data["category"])){
- Session::set("errCategory","Select a category");
- }else{
- $this->category = $data["category"];
- }
- }
- if(array_key_exists('puid',$data )){
- $this->puid = $data["puid"];
- }
- return $this;
- }
- public function store(){
- try{
- if(!empty($this->title) && !empty($this->description) && !empty($this->image) && !empty($this->category)){
- $uniqId = md5(uniqid(rand(),true));
- #validate and upload new logo
- $dir = "upload/";
- $filename = $this->image["name"];
- $targetPath = $dir . time() . "_" . $filename;
- $filesize = $this->image["size"];
- $filetype = strtolower(end(explode(".", $this->image["name"])));
- $type = array("jpeg","jpg","png","gif");
- if($filesize > 1048576){
- Session::set("errImage","File is too large. Should be >1MB");
- }elseif(!in_array($filetype, $type)){
- Session::set("errImage","Logo should be jpeg, jpg, png or gif");
- }else{
- //Utility::dd($this->b_uid);
- move_uploaded_file($this->image["tmp_name"], $targetPath);
- #update site info
- $sql = "INSERT INTO news(user_id,title, unique_id,description,cat_id,image)
- VALUES(:user_id, :title, :unique_id, :description, :cat_id, :image) ";
- $stmt = $this->link->prepare($sql);
- $stmt->execute(array(
- ":user_id" => Session::get("user_id"),
- ":title" => $this->title,
- ":unique_id" => $uniqId,
- ":description" => $this->description,
- ":cat_id" => $this->category,
- ":image" => $targetPath
- ));
- if ($stmt) {
- Session::set("msg","Your article is waiting for aproval");
- }else{
- Session::set("msg","Field can not be empty");
- }
- }
- }
- header("location: AddPosts.php");
- }catch (PDOException $ex) {
- echo $ex->getMessage();
- }
- }
- #all articles
- public function index(){
- try {
- $sql = "SELECT n.*,u.id,u.username FROM news n INNER JOIN users u ON n.user_id = u.id";
- $stmt = $this->link->prepare($sql);
- $stmt->execute();
- if($stmt->rowCount() > 0){
- While($row = $stmt->fetch(PDO::FETCH_ASSOC)){
- $posts[] = $row;
- }
- }else{
- $posts = "";
- }
- return $posts;
- } catch (PDOException $ex) {
- echo $ex->getMessage();
- }
- }
- #show single article
- public function show(){
- try {
- $sql = "SELECT n.*,u.username FROM news n INNER JOIN users u ON n.user_id=u.id WHERE n.unique_id = :unique_id";
- $stmt = $this->link->prepare($sql);
- $stmt->execute(array(":unique_id" => $this->puid));
- if($stmt->rowCount() > 0){
- While($row = $stmt->fetch(PDO::FETCH_ASSOC)){
- $posts[] = $row;
- }
- }else{
- $posts = "";
- }
- return $posts;
- } catch (PDOException $ex) {
- echo $ex->getMessage();
- }
- }
- #update article
- public function update(){
- try{
- if(!empty($this->title) && !empty($this->description)){
- if(!empty($this->image)){
- #validate and upload new logo
- $dir = "upload/";
- $filename = $this->image["name"];
- $targetPath = $dir . time() . "_" . $filename;
- $filesize = $this->image["size"];
- $filetype = strtolower(end(explode(".", $this->image["name"])));
- $type = array("jpeg","jpg","png","gif");
- if($filesize > 1048576){
- Session::set("errImage","File is too large. Should be >1MB");
- }elseif(!in_array($filetype, $type)){
- Session::set("errImage","Logo should be jpeg, jpg, png or gif");
- }else{
- #delete previous image
- $sql = "SELECT image FROM news WHERE unique_id = :uid";
- $stmt = $this->link->prepare($sql);
- $stmt->execute(array(":uid" => $this->puid));
- $image = $stmt->fetch(PDO::FETCH_ASSOC);
- #if image file exists then unlink.
- if(!empty($image["image"])){
- unlink($image["image"]);
- }
- //Utility::dd($this->b_uid);
- move_uploaded_file($this->image["tmp_name"], $targetPath);
- #update article info
- $sql = "UPDATE news SET
- title = :title,
- description = :description,
- image = :image
- WHERE unique_id = :uid";
- $stmt = $this->link->prepare($sql);
- $stmt->execute(array(
- ":title" => $this->title,
- ":description" => $this->description,
- ":image" => $targetPath,
- "uid" => $this->puid
- ));
- if ($stmt) {
- Session::set("msg","Article is updated");
- }else{
- Session::set("msg","Failed to update article");
- }
- }
- }else{
- #update article info
- $sql = "UPDATE news SET
- title = :title,
- description = :description
- WHERE unique_id = :uid";
- $stmt = $this->link->prepare($sql);
- $stmt->execute(array(
- ":title" => $this->title,
- ":description" => $this->description,
- "uid" => $this->puid
- ));
- if ($stmt) {
- Session::set("msg","Article is updated");
- }else{
- Session::set("msg","Failed to update article");
- }
- }
- }
- header("location: Edit.php?puid=$this->puid");
- }catch (PDOException $ex) {
- echo $ex->getMessage();
- }
- }
- #all pending articles
- public function pending_posts(){
- try {
- $sql = "SELECT n.*,u.id,u.username FROM news n INNER JOIN users u ON n.user_id = u.id WHERE is_approved = :approved";
- $stmt = $this->link->prepare($sql);
- $stmt->execute(array(":approved" => 0));
- if($stmt->rowCount() > 0){
- While($row = $stmt->fetch(PDO::FETCH_ASSOC)){
- $posts[] = $row;
- }
- }else{
- $posts = "";
- }
- return $posts;
- } catch (PDOException $ex) {
- echo $ex->getMessage();
- }
- }
- #all approved articles
- public function approved_posts(){
- try {
- $sql = "SELECT n.*,u.id,u.username FROM news n INNER JOIN users u ON n.user_id = u.id WHERE is_approved = :approved";
- $stmt = $this->link->prepare($sql);
- $stmt->execute(array(":approved" => 1));
- if($stmt->rowCount() > 0){
- While($row = $stmt->fetch(PDO::FETCH_ASSOC)){
- $posts[] = $row;
- }
- }else{
- $posts = "";
- }
- return $posts;
- } catch (PDOException $ex) {
- echo $ex->getMessage();
- }
- }
- #approve article
- public function approve(){
- if(!empty($this->puid)){
- $sql = "UPDATE news SET is_approved = :value WHERE unique_id = :puid";
- $stmt = $this->link->prepare($sql);
- $stmt->execute(array(":value" => 1, ":puid" => $this->puid));
- if($stmt){
- Session::set("msg","Article Approved");
- }else{
- Session::set("msg","Failed to approve article");
- }
- }
- header("location: PendingPosts.php");
- }
- #decline article
- public function decline(){
- if(!empty($this->puid)){
- $sql = "DELETE FROM news WHERE unique_id = :puid";
- $stmt = $this->link->prepare($sql);
- $stmt->execute(array(":puid" => $this->puid));
- if($stmt){
- Session::set("msg","Article Deleted");
- }else{
- Session::set("msg","Failed to delete article");
- }
- }
- header("location: PendingPosts.php");
- }
- #decline article
- public function delete(){
- if(!empty($this->puid)){
- $sql = "DELETE FROM news WHERE unique_id = :puid";
- $stmt = $this->link->prepare($sql);
- $stmt->execute(array(":puid" => $this->puid));
- if($stmt){
- Session::set("msg","Article Deleted");
- }else{
- Session::set("msg","Failed to delete article");
- }
- }
- header("location: Posts.php");
- }
- #individual articles
- #all pending articles
- public function myposts(){
- try {
- $sql = "SELECT * FROM news WHERE user_id = :user_id";
- $stmt = $this->link->prepare($sql);
- $stmt->execute(array(":user_id" => Session::get("user_id")));
- if($stmt->rowCount() > 0){
- While($row = $stmt->fetch(PDO::FETCH_ASSOC)){
- $posts[] = $row;
- }
- }else{
- $posts = "";
- }
- return $posts;
- } catch (PDOException $ex) {
- echo $ex->getMessage();
- }
- }
- }