PageRenderTime 47ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/src/cmsArticle.php

https://gitlab.com/AnwarAbir/brighter2morrow
PHP | 490 lines | 424 code | 36 blank | 30 comment | 23 complexity | c4bfc06daaf6ac081a915b3b316c0c48 MD5 | raw file
  1. <?php
  2. namespace App;
  3. use PDO;
  4. class cmsArticle {
  5. public $conn;
  6. public $dbUser = 'root';
  7. public $dbPass = '';
  8. public $id = '';
  9. public $username = '';
  10. public $fname = '';
  11. public $lname = '';
  12. public $email = '';
  13. public $password = '';
  14. public $image = '';
  15. public $imageExtention = '';
  16. public $imageSize = '';
  17. public $pno = '';
  18. public $hno = '';
  19. public $ono = '';
  20. public $title = '';
  21. public $htmlSummary = '';
  22. public $htmlDetails = '';
  23. public $subTitle = '';
  24. public $cAddress = '';
  25. public $pAddress = '';
  26. public $flag = '';
  27. public $category = '';
  28. public $articleID = '';
  29. public function __construct() {
  30. try {
  31. $this->conn = new PDO('mysql:host=localhost;dbname=brighter2morrow', $this->dbUser, $this->dbPass);
  32. $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  33. } catch (PDOException $e) {
  34. echo 'ERROR: ' . $e->getMessage();
  35. }
  36. }
  37. public function setFlag($data = '') {
  38. $this->flag = $data;
  39. return $this;
  40. }
  41. function setId($id) {
  42. $this->id = $id;
  43. return $this;
  44. }
  45. function setEmail($email) {
  46. $this->email = $email;
  47. return $this;
  48. }
  49. public function setValues($data = array()) {
  50. $this->email = $data['email'];
  51. $this->password = $data['password'];
  52. if (isset($data['username'])) {
  53. $this->username = $data['username'];
  54. }
  55. return $this;
  56. }
  57. public function setArticlevalues($data = array()) {
  58. $this->title = $data['title'];
  59. $this->subTitle = $data['subtitle'];
  60. $this->htmlSummary = $data['summary'];
  61. $this->htmlDetails = $data['description'];
  62. $this->category = $data['category'];
  63. if (isset($data['image']) && !empty($data['image'])) {
  64. $this->image = $data['image'];
  65. $this->imageExtention = $data['extention'];
  66. $this->imageSize = $data['size'];
  67. }
  68. return $this;
  69. }
  70. public function setProfileValues($data = array()) {
  71. $this->fname = $data['fname'];
  72. $this->lname = $data['lname'];
  73. $this->pno = $data['pno'];
  74. $this->hno = $data['hno'];
  75. $this->ono = $data['ono'];
  76. $this->cAddress = $data['cAddress'];
  77. $this->pAddress = $data['pAddress'];
  78. if (isset($data['image']) && !empty($data['image'])) {
  79. $this->image = $data['image'];
  80. $this->imageExtention = $data['extention'];
  81. $this->imageSize = $data['size'];
  82. }
  83. }
  84. public function logIn() {
  85. $statement = $this->conn->prepare("SELECT * FROM `users` WHERE `email` = :email");
  86. try {
  87. $statement->execute(array(':email' => "$this->email"));
  88. return $statement->fetch();
  89. } catch (PDOException $e) {
  90. echo 'ERROR: ' . $e->getMessage();
  91. }
  92. }
  93. public function registration() {
  94. $query = "INSERT INTO `brighter2morrow`.`users` (`id`, `unique_id`, `username`, `password`, `email`, `created_at`, `is_active`) "
  95. . "VALUES (NULL, '" . uniqid() . "', :username, :password, :email, '" . date("Y-m-d h:i:s") . "', NULL)";
  96. try {
  97. $stmt = $this->conn->prepare($query);
  98. $stmt->execute(
  99. array(
  100. ':username' => "$this->username",
  101. ':password' => "$this->password",
  102. ':email' => "$this->email"
  103. )
  104. );
  105. header('location:login.php');
  106. } catch (PDOException $e) {
  107. echo 'ERROR: ' . $e->getMessage();
  108. }
  109. }
  110. public function addPost() {
  111. $query = "SELECT `id` FROM `users` WHERE `email`='" . $this->email . "'";
  112. $stm1 = $this->conn->prepare($query);
  113. $stm1->execute();
  114. $idData = $stm1->fetch();
  115. $this->id = $idData['id'];
  116. $query1 = "INSERT INTO `brighter2morrow`.`articles` (`id`, `users_id`, `title`, `sub_title`, `html_summary`, `html_details`, `created_at`) "
  117. . "VALUES (NULL, :id, :title, :subTitle, :htmlSummary, :htmlDetails, '" . date("Y-m-d h:i:s") . "')";
  118. try {
  119. $stmt = $this->conn->prepare($query1);
  120. $stmt->execute(
  121. array(
  122. ':id' => "$this->id",
  123. ':title' => "$this->title",
  124. ':subTitle' => "$this->subTitle",
  125. ':htmlSummary' => "$this->htmlSummary",
  126. ':htmlDetails' => "$this->htmlDetails"
  127. )
  128. );
  129. // header('location:createArticle.php');
  130. } catch (PDOException $e) {
  131. echo 'ERROR: ' . $e->getMessage();
  132. }
  133. $queryArticleID = "SELECT`id` FROM `articles` WHERE `users_id` = '" . $this->id . "' ORDER BY `id` DESC";
  134. $statement = $this->conn->prepare($queryArticleID);
  135. try {
  136. $statement->execute();
  137. $recentArticleId = $statement->fetch();
  138. } catch (PDOException $e) {
  139. echo 'ERROR: ' . $e->getMessage();
  140. }
  141. $this->articleID = $recentArticleId['id'];
  142. $queryInsertCategory = "INSERT INTO `articles_categories_mapping` (`id`, `article_id`, `category_id`, `created_at`) "
  143. . "VALUES (NULL, :articleID, :categoryID, '" . date("Y-m-d h:i:s") . "')";
  144. try {
  145. $stmt = $this->conn->prepare($queryInsertCategory);
  146. $stmt->execute(
  147. array(
  148. ':articleID' => "$this->articleID",
  149. ':categoryID' => "$this->category"
  150. )
  151. );
  152. } catch (PDOException $e) {
  153. echo 'ERROR: ' . $e->getMessage();
  154. }
  155. if (isset($this->image) && !empty($this->image)) {
  156. $queryImage = "UPDATE `images` SET `image_name`=:image ,`extention`=:extension,`size`=:size,`modified_at`='" . date("Y-m-d h:i:s") . "' WHERE `images`.`user_id` = :id";
  157. $stm1 = $this->conn->prepare($queryImage);
  158. $stm1->execute(
  159. array(
  160. ':id' => "$this->id",
  161. ':image' => "$this->image",
  162. ':extension' => "$this->imageExtention",
  163. ':size' => "$this->imageSize"
  164. )
  165. );
  166. $queryImageID = "SELECT `id` FROM `images` WHERE `user_id` = '" . $this->id . "' ORDER BY `id` DESC ";
  167. $statement = $this->conn->prepare($queryImageID);
  168. try {
  169. $statement->execute();
  170. $recentImageId = $statement->fetch();
  171. } catch (PDOException $e) {
  172. echo 'ERROR: ' . $e->getMessage();
  173. }
  174. $imageID = $recentImageId['id'];
  175. $queryImageMapping = "INSERT INTO `articles_images_mapping` (`id`, `articles_id`, `images_id`, `created_at`) "
  176. . "VALUES (NULL, :articleID, :imageID, '" . date("Y-m-d h:i:s") . "')";
  177. try {
  178. $stmt = $this->conn->prepare($queryImageMapping);
  179. $stmt->execute(
  180. array(
  181. ':articleID' => "$this->articleID",
  182. ':imageID' => "$imageID"
  183. )
  184. );
  185. header('location:createArticle.php');
  186. } catch (PDOException $e) {
  187. echo 'ERROR: ' . $e->getMessage();
  188. }
  189. }
  190. header('location:createArticle.php');
  191. }
  192. public function getArticle() {
  193. $query = "SELECT * FROM `articles`, `users` WHERE `articles`.`users_id` = `users`.`id` ORDER BY `articles`.`id` DESC";
  194. $stmt = $this->conn->prepare($query);
  195. try {
  196. $stmt->execute();
  197. return $stmt->fetchAll();
  198. } catch (PDOException $e) {
  199. echo 'ERROR: ' . $e->getMessage();
  200. }
  201. }
  202. public function getProfileArtcile() {
  203. $query = "SELECT COUNT(`users_id`) AS id FROM `articles` WHERE `users_id` = :id ";
  204. $stmt = $this->conn->prepare($query);
  205. try {
  206. $stmt->execute(
  207. array(
  208. ':id' => "$this->id"
  209. )
  210. );
  211. return $stmt->fetch();
  212. } catch (PDOException $e) {
  213. echo 'ERROR: ' . $e->getMessage();
  214. }
  215. }
  216. public function profile() {
  217. $query = "SELECT * FROM `profiles`,`users` WHERE profiles.user_id = users.id AND users.email =:email ";
  218. $stmt = $this->conn->prepare($query);
  219. try {
  220. $stmt->execute(
  221. array(
  222. ':email' => "$this->email"
  223. )
  224. );
  225. return $stmt->fetch();
  226. } catch (PDOException $e) {
  227. echo 'ERROR: ' . $e->getMessage();
  228. }
  229. }
  230. public function getImage() {
  231. $query = "SELECT * FROM `images` WHERE `user_id` IN (SELECT id FROM users WHERE users.email = :email) AND images.`id` NOT IN (SELECT articles_images_mapping.`images_id` FROM `articles_images_mapping`)";
  232. $stmt = $this->conn->prepare($query);
  233. try {
  234. $stmt->execute(
  235. array(
  236. ':email' => "$this->email"
  237. )
  238. );
  239. return $stmt->fetch();
  240. } catch (PDOException $e) {
  241. echo 'ERROR: ' . $e->getMessage();
  242. }
  243. }
  244. public function getPostDetails() {
  245. $query = "SELECT * FROM `articles` WHERE `id` = :id";
  246. $stmt = $this->conn->prepare($query);
  247. try {
  248. $stmt->execute(
  249. array(
  250. ':id' => "$this->id"
  251. )
  252. );
  253. return $stmt->fetch();
  254. } catch (PDOException $e) {
  255. echo 'ERROR: ' . $e->getMessage();
  256. }
  257. }
  258. public function userList() {
  259. $query = "SELECT * FROM `users` WHERE `deleted_at` is null and `email` != :email ORDER BY `id` DESC";
  260. $stm1 = $this->conn->prepare($query);
  261. $stm1->execute(
  262. array(
  263. ':email' => "$this->email"
  264. )
  265. );
  266. $allData = $stm1->fetchAll();
  267. return $allData;
  268. }
  269. public function isActive() {
  270. $query = "SELECT * FROM `users` WHERE `email`= '" . $this->email . "' ";
  271. $stm1 = $this->conn->prepare($query);
  272. $stm1->execute(
  273. );
  274. $data = $stm1->fetch();
  275. // print_r($data);
  276. // die();
  277. if ($data['is_active'] == 0) {
  278. $query = "UPDATE `brighter2morrow`.`users` SET `is_active` = '1' WHERE `email` = :email";
  279. $stm1 = $this->conn->prepare($query);
  280. $stm1->execute(
  281. array(
  282. ':email' => "$this->email"
  283. )
  284. );
  285. } else {
  286. $query = "UPDATE `brighter2morrow`.`users` SET `is_active` = 0 WHERE `email` = :email";
  287. $stm1 = $this->conn->prepare($query);
  288. $stm1->execute(
  289. array(
  290. ':email' => "$this->email"
  291. )
  292. );
  293. }
  294. header('location:../admin/userlist.php');
  295. }
  296. public function update() {
  297. if ($this->flag == 'Update Profile') {
  298. // if (!empty($this->image)) {
  299. // $query = "UPDATE `images` SET `image_name`=:image ,`extention`=:extension,`size`=:size,`modified_at`='" . date("Y-m-d h:i:s") . "' WHERE `images`.`user_id` = :id";
  300. // $stm1 = $this->conn->prepare($query);
  301. // $stm1->execute(
  302. // array(
  303. // ':id' => "$this->id",
  304. // ':image' => "$this->image",
  305. // ':extension' => "$this->imageExtention",
  306. // ':size' => "$this->imageSize"
  307. // )
  308. // );
  309. // }
  310. if (isset($this->image) && !empty($this->image)) {
  311. $query = "UPDATE `profiles` SET `first_name`=:fname,`last_name`=:lname,`personal_phone`=:pno,`home_phone`=:hno,`office_phone`=:ono,`current_address`=:cAddress,`permanent_address`=:pAddress,`modified_at`='" . date("Y-m-d h:i:s") . "',`profile_pic` = '" . $this->image . "' WHERE `user_id` = :id";
  312. } else {
  313. $query = "UPDATE `profiles` SET `first_name`=:fname,`last_name`=:lname,`personal_phone`=:pno,`home_phone`=:hno,`office_phone`=:ono,`current_address`=:cAddress,`permanent_address`=:pAddress,`modified_at`='" . date("Y-m-d h:i:s") . "' WHERE `user_id` = :id";
  314. }
  315. $statement = $this->conn->prepare($query);
  316. $statement->execute(
  317. array(
  318. ':id' => "$this->id",
  319. ':fname' => "$this->fname",
  320. ':lname' => "$this->lname",
  321. ':pno' => "$this->pno",
  322. ':hno' => "$this->hno",
  323. ':ono' => "$this->ono",
  324. ':cAddress' => "$this->cAddress",
  325. ':pAddress' => "$this->pAddress"
  326. )
  327. );
  328. header("location:profile.php");
  329. } else {
  330. $query = "UPDATE `users` SET `password`=:password,`email`=:email,`modified_at`= '" . date("Y-m-d h:i:s") . "' WHERE `id`=:id ";
  331. $statement = $this->conn->prepare($query);
  332. $statement->execute(
  333. array(
  334. ':id' => "$this->id",
  335. ':email' => "$this->email",
  336. ':password' => "$this->password"
  337. )
  338. );
  339. header("location:profile.php");
  340. }
  341. }
  342. public function stroe() {
  343. $query = "SELECT `id` FROM `users` WHERE `email`='" . $this->email . "'";
  344. $stm1 = $this->conn->prepare($query);
  345. $stm1->execute();
  346. $this->id = $stm1->fetch()['id'];
  347. // $queryImage = "INSERT INTO `images` (`id`, `user_id`, `image_name`, `extention`, `size`, `created_at`) "
  348. // . "VALUES (NULL, :id, :image, :extension, :size, '" . date("Y-m-d h:i:s") . "')";
  349. // $stmt = $this->conn->prepare($queryImage);
  350. // try {
  351. // $stmt->execute(
  352. // array(
  353. // ':id' => "$this->id",
  354. // ':image' => "$this->image",
  355. // ':extension' => "$this->imageExtention",
  356. // ':size' => "$this->imageSize"
  357. // )
  358. // );
  359. // } catch (PDOException $e) {
  360. // echo 'ERROR: ' . $e->getMessage();
  361. // }
  362. if (isset($this->image) && !empty($this->image)) {
  363. $sql = "INSERT INTO `profiles` (`id`, `user_id`, `first_name`, `last_name`, `personal_phone`, `home_phone`, `office_phone`, `current_address`, `permanent_address`, `profile_pic`, `created_at`) "
  364. . "VALUES (NULL, :id, :fname, :lname, :pno, :hno, :ono, :cAddress, :pAddress, '" . $this->image . "', '" . date("Y-m-d h:i:s") . "')";
  365. } else {
  366. $sql = "INSERT INTO `profiles` (`id`, `user_id`, `first_name`, `last_name`, `personal_phone`, `home_phone`, `office_phone`, `current_address`, `permanent_address`, `created_at`) "
  367. . "VALUES (NULL, :id, :fname, :lname, :pno, :hno, :ono, :cAddress, :pAddress, '" . date("Y-m-d h:i:s") . "')";
  368. }
  369. try {
  370. $stmt = $this->conn->prepare($sql);
  371. $stmt->execute(
  372. array(
  373. ':id' => "$this->id",
  374. ':fname' => "$this->fname",
  375. ':lname' => "$this->lname",
  376. ':pno' => "$this->pno",
  377. ':hno' => "$this->hno",
  378. ':ono' => "$this->ono",
  379. ':cAddress' => "$this->cAddress",
  380. ':pAddress' => "$this->pAddress"
  381. )
  382. );
  383. header('location:profile.php');
  384. } catch (PDOException $e) {
  385. echo 'ERROR: ' . $e->getMessage();
  386. }
  387. }
  388. public function getCategory() {
  389. $query = "SELECT * FROM `categories` WHERE `parent_id` = 0 ";
  390. try {
  391. $stmt = $this->conn->prepare($query);
  392. $stmt->execute();
  393. return $stmt->fetchAll();
  394. } catch (PDOException $exc) {
  395. echo 'ERROR: ' . $e->getMessage();
  396. }
  397. }
  398. public function getPendingUser() {
  399. $query = "SELECT COUNT(`id`) AS ID FROM `users` WHERE `is_active` = 0";
  400. try {
  401. $stmt = $this->conn->prepare($query);
  402. $stmt->execute();
  403. return $stmt->fetch();
  404. } catch (PDOException $exc) {
  405. echo 'ERROR: ' . $e->getMessage();
  406. }
  407. }
  408. public function getPostDetailsImage() {
  409. $query = "SELECT * FROM `articles_images_mapping`,images WHERE articles_images_mapping.images_id = images.id AND articles_images_mapping.articles_id = " . $this->id;
  410. try {
  411. $stmt = $this->conn->prepare($query);
  412. $stmt->execute();
  413. return $stmt->fetch();
  414. } catch (PDOException $exc) {
  415. echo 'ERROR: ' . $e->getMessage();
  416. }
  417. }
  418. public function addCategory($data = array()) {
  419. if (isset($data['category']) && !empty($data['category'])) {
  420. $query = "INSERT INTO `categories` (`id`, `title`, `parent_id`, `created_at`) "
  421. . "VALUES (NULL, '" . $data['newCategory'] . "', '" . $data['category'] . "', '" . date("Y-m-d h:i:s") . "')";
  422. } else {
  423. $query = "INSERT INTO `categories` (`id`, `title`, `created_at`) "
  424. . "VALUES (NULL, '" . $data['newCategory'] . "', '" . date("Y-m-d h:i:s") . "')";
  425. }
  426. try {
  427. $stmt = $this->conn->prepare($query);
  428. $stmt->execute();
  429. header("location:http://localhost/brighter2morrow/views/common/category.php");
  430. } catch (PDOException $exc) {
  431. echo 'ERROR: ' . $e->getMessage();
  432. }
  433. }
  434. public function countArticle() {
  435. $query = "SELECT COUNT(`id`) FROM `articles` ";
  436. try {
  437. $stmt = $this->conn->prepare($query);
  438. $stmt->execute();
  439. return $stmt->fetch();
  440. } catch (PDOException $exc) {
  441. echo 'ERROR: ' . $e->getMessage();
  442. }
  443. }
  444. public function countNewUser() {
  445. $query = "SELECT * FROM `users` WHERE 1 ORDER BY `id` DESC ";
  446. try {
  447. $stmt = $this->conn->prepare($query);
  448. $stmt->execute();
  449. return $stmt->fetchAll();
  450. } catch (PDOException $exc) {
  451. echo 'ERROR: ' . $e->getMessage();
  452. }
  453. }
  454. }