Skip to content

Commit

Permalink
Отображение статей, выбранные администратором сайта
Browse files Browse the repository at this point in the history
  • Loading branch information
programm87 committed Apr 23, 2023
1 parent 907f79c commit 559fbc5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion admin.php
Expand Up @@ -174,7 +174,7 @@ function deleteArticle() {
function listArticles() {
$results = array();

$data = Article::getList();
$data = Article::getList(1000000, null, "publicationDate DESC", 1);
$results['articles'] = $data['results'];
$results['totalRows'] = $data['totalRows'];

Expand Down
19 changes: 15 additions & 4 deletions classes/Article.php
Expand Up @@ -37,6 +37,11 @@ class Article
*/
public $content = null;

/**
* @var boolean Разместить статью
*/
public $active = null;

/**
* Создаст объект статьи
*
Expand Down Expand Up @@ -70,6 +75,10 @@ public function __construct($data=array())
if (isset($data['content'])) {
$this->content = $data['content'];
}

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


Expand Down Expand Up @@ -127,11 +136,11 @@ public static function getById($id) {
* @return Array|false Двух элементный массив: results => массив объектов Article; totalRows => общее количество строк
*/
public static function getList($numRows=1000000,
$categoryId=null, $order="publicationDate DESC")
$categoryId=null, $order="publicationDate DESC", $activeter=0)
{
$conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
$fromPart = "FROM articles";
$categoryClause = $categoryId ? "WHERE categoryId = :categoryId" : "";
$categoryClause = $categoryId ? "WHERE categoryId = :categoryId" : ($activeter ? "" : "WHERE active = 1");
$sql = "SELECT *, UNIX_TIMESTAMP(publicationDate)
AS publicationDate
$fromPart $categoryClause
Expand Down Expand Up @@ -182,13 +191,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( ":place", $this->active, PDO::PARAM_INT );
$st->execute();
$this->id = $conn->lastInsertId();
$conn = null;
Expand All @@ -208,14 +218,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";
. " 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( ":active", $this->active, PDO::PARAM_INT );
$st->bindValue( ":id", $this->id, PDO::PARAM_INT );
$st->execute();
$conn = null;
Expand Down
9 changes: 7 additions & 2 deletions templates/admin/editArticle.php
Expand Up @@ -45,8 +45,13 @@
<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="active">Active</label>
<input type="hidden" name="active" value="0">
<input type=checkbox name="active" value="1" <?php echo $results['article']->active ? 'checked' : ""?>>
</li>

</ul>

<div class="buttons">
Expand Down
6 changes: 5 additions & 1 deletion 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 @@ -35,13 +36,16 @@
<!--<?php echo "<pre>"; print_r ($results); echo "</pre>"; ?> Здесь есть доступ к полному объекту $results -->

<?php
if(isset ($article->categoryId)) {
if(! empty($article->categoryId)) {
echo $results['categories'][$article->categoryId]->name;
}
else {
echo "Без категории";
}?>
</td>
<td>
<?php echo $article->active ? 'YES' : 'NO'?>
</td>
</tr>

<?php } ?>
Expand Down
2 changes: 1 addition & 1 deletion templates/homepage.php
Expand Up @@ -12,7 +12,7 @@
<?php echo htmlspecialchars( $article->title )?>
</a>

<?php if (isset($article->categoryId)) { ?>
<?php if (! empty($article->categoryId)) { ?>
<span class="category">
in
<a href=".?action=archive&amp;categoryId=<?php echo $article->categoryId?>">
Expand Down

0 comments on commit 559fbc5

Please sign in to comment.