function insert_model($name, $wechat = null, $qq = null, $weibo = null, $gender = "female", $birthday = 0)
{
    global $db;
    $model_id = find_model($name);
    if ($model_id) {
        // 说明模特已经在数据库中了
        return $model_id;
    }
    $stmt = $db->prepare("INSERT INTO Models (name, wechat, qq, weibo, gender, birthday)\n                                      VALUES(:name, :wechat, :qq, :weibo, :gender, :b_day)");
    $stmt->bindParam(":name", $name);
    $stmt->bindParam(":wechat", $wechat);
    $stmt->bindParam(":qq", $qq);
    $stmt->bindParam(":weibo", $weibo);
    $stmt->bindParam(":gender", $gender);
    $stmt->bindParam(":b_day", $birthday, PDO::PARAM_INT);
    try {
        $stmt->execute();
        return $db->lastInsertId();
    } catch (PDOException $e) {
        error_log("ERROR while insert model: " . $e->getMessage());
        return false;
    }
}
Beispiel #2
0
/**
 * @post update
 */
function update_post_controller($id)
{
    __is_guest();
    if (!empty($_POST)) {
        if (checked_token($_POST['_token'])) {
            __session_start();
            $_SESSION['old'] = [];
            $_SESSION['errors'] = [];
            $rules = ['title' => FILTER_SANITIZE_STRING, 'content' => FILTER_SANITIZE_STRING, 'status' => ['filter' => FILTER_CALLBACK, 'options' => function ($s) {
                if (in_array($s, ['published', 'unpublished'])) {
                    return $s;
                } else {
                    return 'unpublished';
                }
            }], 'published_at' => ['filter' => FILTER_CALLBACK, 'options' => function ($checkbox) {
                if ($checkbox == 'yes') {
                    return new DateTime('now');
                }
            }]];
            $sanitize = filter_input_array(INPUT_POST, $rules);
            $id = (int) $id;
            // test if errors
            if (empty($_POST['title'])) {
                $_SESSION['errors']['title'] = 'title is required';
            }
            if (!empty($_SESSION['errors'])) {
                $_SESSION['old'] = $sanitize;
                redirect('post/create');
                // exit
            }
            if (!empty($_FILES['file']) && is_uploaded_file($_FILES['file']['tmp_name'])) {
                try {
                    $dateFile = upload($_FILES['file']);
                    beginTransaction();
                    update_post_model($id, $sanitize);
                    create_media_model(['filename' => $dateFile['filename'], 'post_id' => $id, 'size' => $dateFile['size']]);
                    commit();
                    setFlashMessage("success stored");
                    redirect('dashboard');
                } catch (Exception $e) {
                    if ($e instanceof RuntimeException) {
                        $_SESSION['old'] = $sanitize;
                        $_SESSION['errors']['upload'] = $e->getMessage();
                        redirect('post/create');
                    }
                    rollback();
                    $_SESSION['old'] = $sanitize;
                    $_SESSION['errors']['file'] = $e->getMessage();
                    redirect('post/create');
                }
            } else {
                try {
                    beginTransaction();
                    update_post_model($id, $sanitize);
                    $media_id = (int) $_POST['m_id'];
                    if (!empty($_POST['m_id']) && !empty($_POST['delete_filename'])) {
                        $media = find_model($media_id, 'medias');
                        $m = $media->fetch();
                        destroy_model($media_id, 'medias');
                    }
                    commit();
                    if (!empty($m)) {
                        unlink(getEnv('UPLOAD_DIRECTORY') . '/' . htmlentities($m['m_filename']));
                    }
                    setFlashMessage(trans('success_updated_post', $sanitize['title']));
                    redirect('dashboard');
                } catch (Exception $e) {
                    rollback();
                    $_SESSION['old'] = $sanitize;
                    $_SESSION['errors']['file'] = $e->getMessage();
                    redirect('post/create');
                }
                throw new RuntimeException('418');
            }
        }
    }
}