PageRenderTime 63ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/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
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Debashish
  5. * Date: 10/1/2016
  6. * Time: 2:34 PM
  7. */
  8. namespace News;
  9. use PDO;
  10. use Session\Session;
  11. class Article
  12. {
  13. public $title;
  14. public $category = 1;
  15. public $image;
  16. public $description;
  17. public $puid;
  18. public $link;
  19. public function __construct()
  20. {
  21. try {
  22. #session_start();
  23. $this->link = new PDO("mysql:host=localhost;dbname=news_portal", "root","");
  24. $this->link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  25. if (!$this->link) {
  26. echo "ERROR: " .$this->link->errorInfo();
  27. }
  28. } catch (PDOException $ex) {
  29. echo $ex->getMessage();
  30. }
  31. }
  32. public function prepare($data = "")
  33. {
  34. if(array_key_exists("title", $data )) {
  35. if (!empty($data["title"])) {
  36. $this->title = filter_var($data["title"], FILTER_SANITIZE_STRING);
  37. } else {
  38. Session::set('errTitle', "Enter Title");
  39. }
  40. }
  41. if(array_key_exists("image",$data)){
  42. if(!empty($data["image"]["name"])) {
  43. $this->image = $data["image"];
  44. }else{
  45. Session::set("errImage", 'Choose an image');
  46. }
  47. }
  48. if(array_key_exists("description",$data)){
  49. if(!empty($data["description"])) {
  50. $this->description = $data["description"];
  51. }else{
  52. Session::set("errDescription", 'Enter Description');
  53. }
  54. }
  55. if(array_key_exists("category", $data)){
  56. if(empty($data["category"])){
  57. Session::set("errCategory","Select a category");
  58. }else{
  59. $this->category = $data["category"];
  60. }
  61. }
  62. if(array_key_exists('puid',$data )){
  63. $this->puid = $data["puid"];
  64. }
  65. return $this;
  66. }
  67. public function store(){
  68. try{
  69. if(!empty($this->title) && !empty($this->description) && !empty($this->image) && !empty($this->category)){
  70. $uniqId = md5(uniqid(rand(),true));
  71. #validate and upload new logo
  72. $dir = "upload/";
  73. $filename = $this->image["name"];
  74. $targetPath = $dir . time() . "_" . $filename;
  75. $filesize = $this->image["size"];
  76. $filetype = strtolower(end(explode(".", $this->image["name"])));
  77. $type = array("jpeg","jpg","png","gif");
  78. if($filesize > 1048576){
  79. Session::set("errImage","File is too large. Should be >1MB");
  80. }elseif(!in_array($filetype, $type)){
  81. Session::set("errImage","Logo should be jpeg, jpg, png or gif");
  82. }else{
  83. //Utility::dd($this->b_uid);
  84. move_uploaded_file($this->image["tmp_name"], $targetPath);
  85. #update site info
  86. $sql = "INSERT INTO news(user_id,title, unique_id,description,cat_id,image)
  87. VALUES(:user_id, :title, :unique_id, :description, :cat_id, :image) ";
  88. $stmt = $this->link->prepare($sql);
  89. $stmt->execute(array(
  90. ":user_id" => Session::get("user_id"),
  91. ":title" => $this->title,
  92. ":unique_id" => $uniqId,
  93. ":description" => $this->description,
  94. ":cat_id" => $this->category,
  95. ":image" => $targetPath
  96. ));
  97. if ($stmt) {
  98. Session::set("msg","Your article is waiting for aproval");
  99. }else{
  100. Session::set("msg","Field can not be empty");
  101. }
  102. }
  103. }
  104. header("location: AddPosts.php");
  105. }catch (PDOException $ex) {
  106. echo $ex->getMessage();
  107. }
  108. }
  109. #all articles
  110. public function index(){
  111. try {
  112. $sql = "SELECT n.*,u.id,u.username FROM news n INNER JOIN users u ON n.user_id = u.id";
  113. $stmt = $this->link->prepare($sql);
  114. $stmt->execute();
  115. if($stmt->rowCount() > 0){
  116. While($row = $stmt->fetch(PDO::FETCH_ASSOC)){
  117. $posts[] = $row;
  118. }
  119. }else{
  120. $posts = "";
  121. }
  122. return $posts;
  123. } catch (PDOException $ex) {
  124. echo $ex->getMessage();
  125. }
  126. }
  127. #show single article
  128. public function show(){
  129. try {
  130. $sql = "SELECT n.*,u.username FROM news n INNER JOIN users u ON n.user_id=u.id WHERE n.unique_id = :unique_id";
  131. $stmt = $this->link->prepare($sql);
  132. $stmt->execute(array(":unique_id" => $this->puid));
  133. if($stmt->rowCount() > 0){
  134. While($row = $stmt->fetch(PDO::FETCH_ASSOC)){
  135. $posts[] = $row;
  136. }
  137. }else{
  138. $posts = "";
  139. }
  140. return $posts;
  141. } catch (PDOException $ex) {
  142. echo $ex->getMessage();
  143. }
  144. }
  145. #update article
  146. public function update(){
  147. try{
  148. if(!empty($this->title) && !empty($this->description)){
  149. if(!empty($this->image)){
  150. #validate and upload new logo
  151. $dir = "upload/";
  152. $filename = $this->image["name"];
  153. $targetPath = $dir . time() . "_" . $filename;
  154. $filesize = $this->image["size"];
  155. $filetype = strtolower(end(explode(".", $this->image["name"])));
  156. $type = array("jpeg","jpg","png","gif");
  157. if($filesize > 1048576){
  158. Session::set("errImage","File is too large. Should be >1MB");
  159. }elseif(!in_array($filetype, $type)){
  160. Session::set("errImage","Logo should be jpeg, jpg, png or gif");
  161. }else{
  162. #delete previous image
  163. $sql = "SELECT image FROM news WHERE unique_id = :uid";
  164. $stmt = $this->link->prepare($sql);
  165. $stmt->execute(array(":uid" => $this->puid));
  166. $image = $stmt->fetch(PDO::FETCH_ASSOC);
  167. #if image file exists then unlink.
  168. if(!empty($image["image"])){
  169. unlink($image["image"]);
  170. }
  171. //Utility::dd($this->b_uid);
  172. move_uploaded_file($this->image["tmp_name"], $targetPath);
  173. #update article info
  174. $sql = "UPDATE news SET
  175. title = :title,
  176. description = :description,
  177. image = :image
  178. WHERE unique_id = :uid";
  179. $stmt = $this->link->prepare($sql);
  180. $stmt->execute(array(
  181. ":title" => $this->title,
  182. ":description" => $this->description,
  183. ":image" => $targetPath,
  184. "uid" => $this->puid
  185. ));
  186. if ($stmt) {
  187. Session::set("msg","Article is updated");
  188. }else{
  189. Session::set("msg","Failed to update article");
  190. }
  191. }
  192. }else{
  193. #update article info
  194. $sql = "UPDATE news SET
  195. title = :title,
  196. description = :description
  197. WHERE unique_id = :uid";
  198. $stmt = $this->link->prepare($sql);
  199. $stmt->execute(array(
  200. ":title" => $this->title,
  201. ":description" => $this->description,
  202. "uid" => $this->puid
  203. ));
  204. if ($stmt) {
  205. Session::set("msg","Article is updated");
  206. }else{
  207. Session::set("msg","Failed to update article");
  208. }
  209. }
  210. }
  211. header("location: Edit.php?puid=$this->puid");
  212. }catch (PDOException $ex) {
  213. echo $ex->getMessage();
  214. }
  215. }
  216. #all pending articles
  217. public function pending_posts(){
  218. try {
  219. $sql = "SELECT n.*,u.id,u.username FROM news n INNER JOIN users u ON n.user_id = u.id WHERE is_approved = :approved";
  220. $stmt = $this->link->prepare($sql);
  221. $stmt->execute(array(":approved" => 0));
  222. if($stmt->rowCount() > 0){
  223. While($row = $stmt->fetch(PDO::FETCH_ASSOC)){
  224. $posts[] = $row;
  225. }
  226. }else{
  227. $posts = "";
  228. }
  229. return $posts;
  230. } catch (PDOException $ex) {
  231. echo $ex->getMessage();
  232. }
  233. }
  234. #all approved articles
  235. public function approved_posts(){
  236. try {
  237. $sql = "SELECT n.*,u.id,u.username FROM news n INNER JOIN users u ON n.user_id = u.id WHERE is_approved = :approved";
  238. $stmt = $this->link->prepare($sql);
  239. $stmt->execute(array(":approved" => 1));
  240. if($stmt->rowCount() > 0){
  241. While($row = $stmt->fetch(PDO::FETCH_ASSOC)){
  242. $posts[] = $row;
  243. }
  244. }else{
  245. $posts = "";
  246. }
  247. return $posts;
  248. } catch (PDOException $ex) {
  249. echo $ex->getMessage();
  250. }
  251. }
  252. #approve article
  253. public function approve(){
  254. if(!empty($this->puid)){
  255. $sql = "UPDATE news SET is_approved = :value WHERE unique_id = :puid";
  256. $stmt = $this->link->prepare($sql);
  257. $stmt->execute(array(":value" => 1, ":puid" => $this->puid));
  258. if($stmt){
  259. Session::set("msg","Article Approved");
  260. }else{
  261. Session::set("msg","Failed to approve article");
  262. }
  263. }
  264. header("location: PendingPosts.php");
  265. }
  266. #decline article
  267. public function decline(){
  268. if(!empty($this->puid)){
  269. $sql = "DELETE FROM news WHERE unique_id = :puid";
  270. $stmt = $this->link->prepare($sql);
  271. $stmt->execute(array(":puid" => $this->puid));
  272. if($stmt){
  273. Session::set("msg","Article Deleted");
  274. }else{
  275. Session::set("msg","Failed to delete article");
  276. }
  277. }
  278. header("location: PendingPosts.php");
  279. }
  280. #decline article
  281. public function delete(){
  282. if(!empty($this->puid)){
  283. $sql = "DELETE FROM news WHERE unique_id = :puid";
  284. $stmt = $this->link->prepare($sql);
  285. $stmt->execute(array(":puid" => $this->puid));
  286. if($stmt){
  287. Session::set("msg","Article Deleted");
  288. }else{
  289. Session::set("msg","Failed to delete article");
  290. }
  291. }
  292. header("location: Posts.php");
  293. }
  294. #individual articles
  295. #all pending articles
  296. public function myposts(){
  297. try {
  298. $sql = "SELECT * FROM news WHERE user_id = :user_id";
  299. $stmt = $this->link->prepare($sql);
  300. $stmt->execute(array(":user_id" => Session::get("user_id")));
  301. if($stmt->rowCount() > 0){
  302. While($row = $stmt->fetch(PDO::FETCH_ASSOC)){
  303. $posts[] = $row;
  304. }
  305. }else{
  306. $posts = "";
  307. }
  308. return $posts;
  309. } catch (PDOException $ex) {
  310. echo $ex->getMessage();
  311. }
  312. }
  313. }