Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. class Task
  5. {
  6.  
  7. public $id = null;
  8. public $username = null;
  9. public $email = null;
  10. public $text = null;
  11. public $publicationDate = null;
  12.  
  13. public function __construct( $data=array() ) {
  14. if ( isset( $data['id'] ) ) $this->id = (int) $data['id'];
  15. if ( isset( $data['username'] ) ) $this->username = $data['username'];
  16. if ( isset( $data['email'] ) ) $this->email = $data['email'];
  17. if ( isset( $data['text'] ) ) $this->text = $data['text'];
  18. if ( isset( $data['publicationDate'] ) ) $this->publicationDate = (int) $data['publicationDate'];
  19. if ( isset( $data['status'] ) ) $this->status = (int) $data['status'];
  20. }
  21.  
  22. public function storeFormValues ( $params ) {
  23.  
  24. // Сохраняем все параметры
  25. $this->__construct( $params );
  26. // Разбираем и сохраняем дату публикации
  27. if ( isset($params['publicationDate']) ) {
  28. $publicationDate = explode ( '-', $params['publicationDate'] );
  29.  
  30. if ( count($publicationDate) == 3 ) {
  31. list ( $y, $m, $d ) = $publicationDate;
  32. $this->publicationDate = mktime ( 0, 0, 0, $m, $d, $y );
  33. }
  34. }
  35. }
  36.  
  37. /**
  38. * Create task
  39. */
  40.  
  41. public function insert() {
  42.  
  43. // Check id
  44. if ( !is_null( $this->id ) ) trigger_error ( "Task::insert(): Attempt to insert an Task object that already has its ID property set (to $this->id).", E_USER_ERROR );
  45.  
  46. $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
  47. //upload file
  48. move_uploaded_file($_FILES["file"]["tmp_name"],"MyUploadImages/" . $_FILES["file"]["name"]);
  49. $file="MyUploadImages/".$_FILES["file"]["name"];
  50.  
  51. $sql = "INSERT INTO tasks ( username, email, text, imagePath, publicationDate, status ) VALUES ( :username, :email, :text, '$file', FROM_UNIXTIME(:publicationDate), 0 )";
  52. $st = $conn->prepare ( $sql );
  53. $st->bindValue( ":username", $this->username, PDO::PARAM_STR );
  54. $st->bindValue( ":email", $this->email, PDO::PARAM_STR );
  55. $st->bindValue( ":text", $this->text, PDO::PARAM_STR );
  56. $st->bindValue( ":publicationDate", $this->publicationDate, PDO::PARAM_INT );
  57. $st->execute();
  58. $this->id = $conn->lastInsertId();
  59. $conn = null;
  60. }
  61. }
  62.  
  63. ?>
  64.  
  65. <form action=".?action=<?php echo $results['newTask']?>" method="post">
  66. <input type="hidden" name="taskId" value="<?php echo $results['task']->id ?>"/>
  67.  
  68. <input type="text" name="username" id="username" class="form-control" placeholder="Имя пользователя" required autofocus maxlength="255" value="<?php echo htmlspecialchars( $results['task']->username )?>" />
  69.  
  70. <input type="email" name="email" id="email" class="form-control" placeholder="Ваш E-mail"><?php echo htmlspecialchars( $results['task']->email )?>
  71.  
  72.  
  73. <textarea name="text" id="text" class="form-control" placeholder="Описание задачи" required maxlength="100000" style="height: 30em;"><?php echo htmlspecialchars( $results['task']->text )?></textarea>
  74.  
  75. <input name="file" type="file" id="file" />
  76.  
  77. <input type="date" name="publicationDate" id="date" class="form-control" placeholder="YYYY-MM-DD" required maxlength="10" value="<?php echo $results['task']->publicationDate ? date( "Y-m-d", $results['article']->publicationDate ) : "" ?>" />
  78.  
  79. <input type="submit" class="btn btn-default" name="saveChanges" value="Save" />
  80.  
  81. </div>
  82. </form>
  83.  
  84. <?php
  85.  
  86. require( "config/config.php" );
  87. $action = isset( $_GET['action'] ) ? $_GET['action'] : "";
  88.  
  89. switch ( $action ) {
  90. case 'newTask':
  91. newTask();
  92. break;
  93. }
  94.  
  95. function newTask() {
  96.  
  97. $results = array();
  98. $results['pageTitle'] = "new Task";
  99. $results['newTask'] = "newTask";
  100.  
  101. if ( isset( $_POST['saveChanges'] ) ) {
  102.  
  103. $task = new Task;
  104. $task->storeFormValues( $_POST );
  105. $task->insert();
  106.  
  107. header( "Location: index.php?status=changesSaved" );
  108.  
  109. } elseif ( isset( $_POST['cancel'] ) ) {
  110. header( "Location: index.php" );
  111. } else {
  112.  
  113. $results['task'] = new Task;
  114. require( TEMPLATE_PATH . "/newTask.php" );
  115. }
  116.  
  117. }
  118.  
  119. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement