Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Task
- {
- public $id = null;
- public $username = null;
- public $email = null;
- public $text = null;
- public $publicationDate = null;
- public function __construct( $data=array() ) {
- if ( isset( $data['id'] ) ) $this->id = (int) $data['id'];
- if ( isset( $data['username'] ) ) $this->username = $data['username'];
- if ( isset( $data['email'] ) ) $this->email = $data['email'];
- if ( isset( $data['text'] ) ) $this->text = $data['text'];
- if ( isset( $data['publicationDate'] ) ) $this->publicationDate = (int) $data['publicationDate'];
- if ( isset( $data['status'] ) ) $this->status = (int) $data['status'];
- }
- public function storeFormValues ( $params ) {
- // Сохраняем все параметры
- $this->__construct( $params );
- // Разбираем и сохраняем дату публикации
- if ( isset($params['publicationDate']) ) {
- $publicationDate = explode ( '-', $params['publicationDate'] );
- if ( count($publicationDate) == 3 ) {
- list ( $y, $m, $d ) = $publicationDate;
- $this->publicationDate = mktime ( 0, 0, 0, $m, $d, $y );
- }
- }
- }
- /**
- * Create task
- */
- public function insert() {
- // Check id
- 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 );
- $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
- //upload file
- move_uploaded_file($_FILES["file"]["tmp_name"],"MyUploadImages/" . $_FILES["file"]["name"]);
- $file="MyUploadImages/".$_FILES["file"]["name"];
- $sql = "INSERT INTO tasks ( username, email, text, imagePath, publicationDate, status ) VALUES ( :username, :email, :text, '$file', FROM_UNIXTIME(:publicationDate), 0 )";
- $st = $conn->prepare ( $sql );
- $st->bindValue( ":username", $this->username, PDO::PARAM_STR );
- $st->bindValue( ":email", $this->email, PDO::PARAM_STR );
- $st->bindValue( ":text", $this->text, PDO::PARAM_STR );
- $st->bindValue( ":publicationDate", $this->publicationDate, PDO::PARAM_INT );
- $st->execute();
- $this->id = $conn->lastInsertId();
- $conn = null;
- }
- }
- ?>
- <form action=".?action=<?php echo $results['newTask']?>" method="post">
- <input type="hidden" name="taskId" value="<?php echo $results['task']->id ?>"/>
- <input type="text" name="username" id="username" class="form-control" placeholder="Имя пользователя" required autofocus maxlength="255" value="<?php echo htmlspecialchars( $results['task']->username )?>" />
- <input type="email" name="email" id="email" class="form-control" placeholder="Ваш E-mail"><?php echo htmlspecialchars( $results['task']->email )?>
- <textarea name="text" id="text" class="form-control" placeholder="Описание задачи" required maxlength="100000" style="height: 30em;"><?php echo htmlspecialchars( $results['task']->text )?></textarea>
- <input name="file" type="file" id="file" />
- <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 ) : "" ?>" />
- <input type="submit" class="btn btn-default" name="saveChanges" value="Save" />
- </div>
- </form>
- <?php
- require( "config/config.php" );
- $action = isset( $_GET['action'] ) ? $_GET['action'] : "";
- switch ( $action ) {
- case 'newTask':
- newTask();
- break;
- }
- function newTask() {
- $results = array();
- $results['pageTitle'] = "new Task";
- $results['newTask'] = "newTask";
- if ( isset( $_POST['saveChanges'] ) ) {
- $task = new Task;
- $task->storeFormValues( $_POST );
- $task->insert();
- header( "Location: index.php?status=changesSaved" );
- } elseif ( isset( $_POST['cancel'] ) ) {
- header( "Location: index.php" );
- } else {
- $results['task'] = new Task;
- require( TEMPLATE_PATH . "/newTask.php" );
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement