Exemplo n.º 1
0
 /**
  * Сохранение статьи в базу
  *
  * @param array $arValues - массив значений статьи
  * структура:
  * $arValues = array(
  *      'date' => '2016-01-07 10:00',
  *      'title' => 'Первая статья',
  *      'text' => 'Текст статьи',
  *      'tags' => 'Не обязательное поле с тегами',
  *      'image_name' => 'Не обязательное поле с названием картинки'
  * );
  * @return bool|int
  */
 public function addArticle(array $arValues)
 {
     if (!array_key_exists('date', $arValues) || !array_key_exists('title', $arValues) || !array_key_exists('text', $arValues)) {
         return false;
     }
     $insertData = array('date' => $arValues['date'], 'title' => htmlentities($arValues['title']), 'text' => htmlentities($arValues['text']), 'tags' => array_key_exists('tags', $arValues) && !empty($arValues['tags']) ? htmlentities($arValues['tags']) : null, 'image_name' => array_key_exists('image_name', $arValues) ? $arValues['image_name'] : null);
     $res = $this->dbConnect->insert(self::TABLE_NAME, $insertData);
     if (!$res) {
         $this->error[] = $this->dbConnect->lastError;
     } else {
         if (!is_null($insertData['image_name'])) {
             $img = new uploadImage($this::IMG_UPLOAD_PATH, $this::IMG_UPLOAD_MAX_SIZE);
             $img->copyImageFromTmp($insertData['image_name']);
         }
     }
     return $res;
 }
Exemplo n.º 2
0
<?php

/**
 * Скрипт обработки формы добавления статьи
 */
require_once '/modules/formValidate.php';
require_once '/modules/article.php';
session_start();
$valid = new formValidate();
$article = new article();
$err = $valid->isValidate($article->addValidateRules);
$fileName = '';
if (isset($_FILES['image_name']) && $_FILES['image_name']['size'] > 0) {
    require_once '/modules/uploadImage.php';
    $img = new uploadImage($article::IMG_UPLOAD_PATH, $article::IMG_UPLOAD_MAX_SIZE, $article::IMG_UPLOAD_TYPE);
    $fileName = $img->saveImage('image_name', true);
    if (!$fileName) {
        $_SESSION['errors'] = array($img->error);
        $_SESSION['fieldsValue'] = $_POST;
        header("Location: " . $_SERVER['HTTP_REFERER']);
        exit;
    }
}
$insertData = array('date' => $_POST['date'], 'title' => $_POST['title'], 'text' => $_POST['text'], 'tags' => $_POST['tags'], 'image_name' => !empty($fileName) ? $fileName : basename($_POST['image_data']));
if (is_array($err)) {
    $_SESSION['errors'] = $err;
    $_SESSION['fieldsValue'] = $_POST;
    $_SESSION['fieldsValue']['image_data'] = !empty($_POST['image_data']) ? $article::IMG_UPLOAD_PATH . 'tmp/' . $insertData['image_name'] : '';
    header("Location: " . $_SERVER['HTTP_REFERER']);
    exit;
}