Skip to content

Commit

Permalink
Второе практическое задание
Browse files Browse the repository at this point in the history
  • Loading branch information
kdn2517 committed Feb 1, 2018
1 parent 8bf8748 commit 29e31e5
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -28,3 +28,6 @@
6) Следуем инструкциям http://fkn.ktu10.com/?q=node/9428

Удачной разработки!

Для второго практического задания добавляем новый столбец в таблицу:
ALTER TABLE `articles` ADD `active` TINYINT(1) NOT NULL AFTER `content`;
29 changes: 25 additions & 4 deletions classes/Article.php
Expand Up @@ -41,6 +41,11 @@ class Article
*/
public $content50char = null;
/**
* @var int активность статьи (1 - статья активна, показывается на главной
* странице; 0 - статья не активна, видит только админ)
*/
public $activeArticle = null;
/**
* Устанавливаем свойства с помощью значений в заданном массиве
*
* @param assoc Значения свойств
Expand Down Expand Up @@ -90,8 +95,14 @@ public function __construct($data=array())
$this->content = $data['content'];
$this->content50char = mb_substr($data['content'], 0, 50) . '...';
}
}

if(isset($data['active'])) {
$this->activeArticle = $data['active'];
} else {
$this->activeArticle = 0;
}

}

/**
* Устанавливаем свойства с помощью значений формы редактирования записи в заданном массиве
Expand Down Expand Up @@ -151,7 +162,15 @@ public static function getList($numRows=1000000,
$categoryId=null, $order="publicationDate DESC")
{
$conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
$categoryClause = $categoryId ? "WHERE categoryId = :categoryId" : "";

if($categoryId) {
$categoryClause = "WHERE categoryId = :categoryId";
} elseif(preg_match("/admin.php/", $_SERVER['REQUEST_URI'])) {
$categoryClause = '';
} else {
$categoryClause = 'WHERE active = 1';
}

$sql = "SELECT SQL_CALC_FOUND_ROWS *, UNIX_TIMESTAMP(publicationDate)
AS publicationDate
FROM articles $categoryClause
Expand Down Expand Up @@ -207,13 +226,14 @@ public function insert() {

// Вставляем статью
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "INSERT INTO articles ( publicationDate, categoryId, title, summary, content ) VALUES ( FROM_UNIXTIME(:publicationDate), :categoryId, :title, :summary, :content )";
$sql = "INSERT INTO articles ( publicationDate, categoryId, title, summary, content, active ) VALUES ( FROM_UNIXTIME(:publicationDate), :categoryId, :title, :summary, :content, :active)";
$st = $conn->prepare ( $sql );
$st->bindValue( ":publicationDate", $this->publicationDate, PDO::PARAM_INT );
$st->bindValue( ":categoryId", $this->categoryId, PDO::PARAM_INT );
$st->bindValue( ":title", $this->title, PDO::PARAM_STR );
$st->bindValue( ":summary", $this->summary, PDO::PARAM_STR );
$st->bindValue( ":content", $this->content, PDO::PARAM_STR );
$st->bindValue(":active", $this->activeArticle, PDO::PARAM_INT);
$st->execute();
$this->id = $conn->lastInsertId();
$conn = null;
Expand All @@ -229,14 +249,15 @@ public function update() {

// Обновляем статью
$conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
$sql = "UPDATE articles SET publicationDate=FROM_UNIXTIME(:publicationDate), categoryId=:categoryId, title=:title, summary=:summary, content=:content WHERE id = :id";
$sql = "UPDATE articles SET publicationDate=FROM_UNIXTIME(:publicationDate), categoryId=:categoryId, title=:title, summary=:summary, content=:content, active=:active WHERE id = :id";
$st = $conn->prepare ( $sql );
$st->bindValue( ":publicationDate", $this->publicationDate, PDO::PARAM_INT );
$st->bindValue( ":categoryId", $this->categoryId, PDO::PARAM_INT );
$st->bindValue( ":title", $this->title, PDO::PARAM_STR );
$st->bindValue( ":summary", $this->summary, PDO::PARAM_STR );
$st->bindValue( ":content", $this->content, PDO::PARAM_STR );
$st->bindValue( ":id", $this->id, PDO::PARAM_INT );
$st->bindValue(":active", $this->activeArticle, PDO::PARAM_INT);
$st->execute();
$conn = null;
}
Expand Down
7 changes: 6 additions & 1 deletion style.css
Expand Up @@ -339,4 +339,9 @@
border: 1px solid #800;
color: #fff;
}


#checkboxActivity {
width: 25px;
height: 25px;
cursor: pointer;
}
11 changes: 11 additions & 0 deletions templates/admin/editArticle.php
Expand Up @@ -45,6 +45,17 @@
<label for="publicationDate">Publication Date</label>
<input type="date" name="publicationDate" id="publicationDate" placeholder="YYYY-MM-DD" required maxlength="10" value="<?php echo $results['article']->publicationDate ? date( "Y-m-d", $results['article']->publicationDate ) : "" ?>" />
</li>

<li>
<label for="checkActivity">Active</label>
<input type="checkbox" name="active" value="1" id="checkboxActivity"
<?php
if($results['article']->activeArticle == 1) {
echo 'checked = "checked"';
}
?>
>
</li>


</ul>
Expand Down
12 changes: 12 additions & 0 deletions templates/admin/listArticles.php
Expand Up @@ -17,6 +17,7 @@
<th>Publication Date</th>
<th>Article</th>
<th>Category</th>
<th>Active</th>
</tr>

<!--<?php echo "<pre>"; print_r ($results['articles'][2]->publicationDate); echo "</pre>"; ?> Обращаемся к дате массива $results. Дата = 0 -->
Expand All @@ -42,6 +43,17 @@
echo "Без категории";
}?>
</td>

<td>
<?php
if($article->activeArticle) {
echo 'Active';
} else {
echo 'Not active';
}
?>
</td>

</tr>

<?php } ?>
Expand Down

0 comments on commit 29e31e5

Please sign in to comment.