Example #1
0
    function write()
    {
        global $current_user;
        $points_gained = false;
        /* @var $current_user CurrentUser */
        Database::query('START TRANSACTION');
        $current_user->can_throw('books_edit');
        if (!isset(Request::$post['lang_code']) || !Request::$post['lang_code']) {
            throw new Exception('field missed #lang_code');
        }
        $id = isset(Request::$post['id']) ? (int) Request::$post['id'] : false;
        if (Request::post('isbn')) {
            Request::$post['isbn'] = extractISBN(Request::$post['isbn']);
        }
        if (!$id) {
            $this->newBook();
            return;
        }
        $books = Books::getInstance()->getByIdsLoaded(array($id));
        $book = is_array($books) ? $books[$id] : false;
        if (!$book) {
            return;
        }
        /* @var $book Book */
        $fields = array('title' => 'title', 'subtitle' => 'subtitle', 'isbn' => 'ISBN', 'year' => 'year', 'lang_code' => 'id_lang', 'annotation' => 'description', 'rightholder' => 'id_rightholder');
        Request::$post['lang_code'] = Config::$langs[Request::$post['lang_code']];
        Request::$post['annotation'] = trim(prepare_review(Request::$post['annotation'], false, '<img>'));
        Request::$post['title'] = trim(prepare_review(Request::$post['title'], ''));
        Request::$post['year'] = (int) Request::$post['year'];
        $magazineData = array();
        if ($book->data['book_type'] == Book::BOOK_TYPE_MAGAZINE) {
            $magazineData = Database::sql2row('SELECT * FROM `magazines` M LEFT JOIN book_magazines BM ON BM.id_magazine=M.id WHERE BM.id_book=' . $book->id);
            $book->data['n'] = max(0, $magazineData['n']);
            $book->data['year'] = $magazineData['year'];
            Request::$post['n'] = isset(Request::$post['n']) && Request::$post['n'] ? Request::$post['n'] : $magazineData['n'];
        }
        $to_update_m = array();
        $to_update = array();
        if (isset(Request::$post['quality'])) {
            if ($book->data['quality'] != (int) Request::$post['quality']) {
                $to_update['quality'] = (int) Request::$post['quality'];
            }
        }
        if (isset(Request::$post['n'])) {
            if (isset($book->data['n']) && $book->data['n'] != (int) Request::$post['n']) {
                $to_update_m['n'] = (int) Request::$post['n'];
                Request::$post['title'] = $magazineData['title'];
                Request::$post['subtitle'] = '№ ' . $to_update_m['n'] . ' за ' . Request::$post['year'] . ' год';
            }
            if (isset($book->data['year']) && $book->data['year'] != (int) Request::$post['year']) {
                $to_update_m['n'] = (int) Request::$post['n'];
                Request::$post['title'] = $magazineData['title'];
                Request::$post['subtitle'] = '№ ' . $to_update_m['n'] . ' за ' . Request::$post['year'] . ' год';
            }
        }
        if (isset($_FILES['cover']) && $_FILES['cover']['tmp_name']) {
            $folder = Config::need('static_path') . '/upload/covers/' . ceil($book->id / 5000);
            @mkdir($folder);
            // inserting new cover
            $query = 'INSERT INTO `book_covers` SET `id_book`=' . $book->id;
            Database::query($query);
            $cover_id = Database::lastInsertId();
            // generating file names
            $filename_normal = $folder . '/default_' . $book->id . '_' . $cover_id . '.jpg';
            $filename_small = $folder . '/small_' . $book->id . '_' . $cover_id . '.jpg';
            $filename_big = $folder . '/big_' . $book->id . '_' . $cover_id . '.jpg';
            $filename_orig = $folder . '/orig_' . $book->id . '_' . $cover_id . '.jpg';
            $to_update['is_cover'] = $cover_id;
            $thumb = new Thumb();
            $thumb->createThumbnails($_FILES['cover']['tmp_name'], array($filename_small, $filename_normal, $filename_big, $filename_orig), self::$cover_sizes);
            if ($book->data['is_cover']) {
                $current_user->gainActionPoints('books_edit_cover', $book->id, BiberLog::TargetType_book);
            } else {
                $current_user->gainActionPoints('books_add_cover', $book->id, BiberLog::TargetType_book);
            }
            $points_gained = true;
        }
        // file loading
        if (isset($_FILES['file']) && isset($_FILES['file']['tmp_name']) && $_FILES['file']['tmp_name']) {
            $filetype_ = explode('.', $_FILES['file']['name']);
            $filetype_ = isset($filetype_[count($filetype_) - 1]) ? $filetype_[count($filetype_) - 1] : '';
            $fts = Config::need('filetypes');
            $filetype = false;
            foreach ($fts as $ftid => $ftname) {
                if ($ftname == $filetype_) {
                    $filetype = $ftid;
                }
            }
            if (!$filetype) {
                throw new Exception('wrong filetype:' . $filetype_);
            }
            $destinationDir = Config::need('files_path') . DIRECTORY_SEPARATOR . getBookFileDirectory($book->id, $filetype);
            @mkdir($destinationDir, 0755);
            // добавляем запись в базу
            $filesize = $_FILES['file']['size'];
            $query = 'SELECT * FROM `book_files` WHERE `id_book`=' . $book->id;
            $files = Database::sql2array($query, 'filetype');
            // replacing file
            if (isset($files[$filetype])) {
                $old_id_file = $files[$filetype]['id'];
                $old_id_file_author = $files[$filetype]['id_file_author'];
                $old_filesize = $files[$filetype]['filesize'];
                $query = 'DELETE FROM `book_files` WHERE `id`=' . $old_id_file;
                Database::query($query);
                $query = 'INSERT IGNORE INTO `book_files` SET
				`id_book`=' . $book->id . ',
				`filetype`=' . $filetype . ',
				`id_file_author`=' . $current_user->id . ',
				`modify_time`=' . time() . ',
				`filesize`=' . $filesize;
                Database::query($query);
                $id_file = Database::lastInsertId();
                BookLog::addLog(array('id_file' => $id_file, 'filetype' => $filetype, 'id_file_author' => $current_user->id, 'filesize' => $filesize), array('id_file' => $old_id_file, 'filetype' => 0, 'id_file_author' => $old_id_file_author, 'filesize' => $old_filesize), $book->id);
                Database::query($query);
                $current_user->gainActionPoints('books_edit_file', $book->id, BiberLog::TargetType_book);
            } else {
                $query = 'INSERT INTO `book_files` SET
				`id_book`=' . $book->id . ',
				`filetype`=' . $filetype . ',
				`id_file_author`=' . $current_user->id . ',
				`modify_time`=' . time() . ',
				`filesize`=' . $filesize;
                Database::query($query);
                $id_file = Database::lastInsertId();
                BookLog::addLog(array('id_file' => $id_file, 'filetype' => $filetype, 'id_file_author' => $current_user->id, 'filesize' => $filesize), array('id_file' => 0, 'filetype' => 0, 'id_file_author' => 0, 'filesize' => 0), $book->id);
                $current_user->gainActionPoints('books_add_file', $book->id, BiberLog::TargetType_book);
            }
            if ($id_file) {
                $points_gained = true;
                if (!$book->data['id_main_file'] || isset($files[$filetype])) {
                    $to_update['id_main_file'] = $id_file;
                }
                $destinationFile = getBookFilePath($id_file, $book->id, $filetype, Config::need('files_path'));
                if (!move_uploaded_file($_FILES['file']['tmp_name'], $destinationFile)) {
                    throw new Exception('Cant save file to ' . $destinationFile);
                }
                // event for new File
                $event = new Event();
                $event->event_BooksAddFile($current_user->id, $book->id);
                $event->push();
                if ($filetype == 1) {
                    // FB2
                    $parser = new FB2Parser($destinationFile);
                    $parser->parseDescription();
                    $toc = $parser->getTOCHTML();
                    Request::$post['annotation'] = $parser->getProperty('annotation');
                    Request::$post['title'] = $parser->getProperty('book-title');
                    $to_update['table_of_contents'] = $toc;
                }
            }
        }
        foreach ($fields as $field => $bookfield) {
            if (!isset(Request::$post[$field])) {
                throw new Exception('field missed #[' . $field . ']');
            }
            if ($book->data[$bookfield] != Request::$post[$field]) {
                $to_update[$bookfield] = Request::$post[$field];
            }
        }
        $q = array();
        foreach ($to_update as $field => &$value) {
            $q[] = '`' . $field . '`=' . Database::escape($value) . '';
        }
        $push_event = true;
        if (count($q)) {
            if (count($to_update) == 1) {
                foreach ($to_update as $kk => $vv) {
                    if ($kk == 'id_main_file') {
                        $push_event = false;
                    }
                }
            }
            $query = 'UPDATE `book` SET ' . implode(',', $q) . ' WHERE `id`=' . $book->id;
            Database::query($query);
            if (count($to_update_m)) {
                $to_update['n'] = $to_update_m['n'];
            }
            BookLog::addLog($to_update, $book->data, $book->id);
            foreach ($to_update as $f => $v) {
                $book->data[$f] = $v;
            }
            $search = Search::getInstance();
            /* @var $search Search */
            $search->updateBook($book);
            if ($push_event) {
                $event = new Event();
                $event->event_BooksEdit($current_user->id, $book->id);
                $event->push();
            }
            if (!$points_gained) {
                $current_user->gainActionPoints('books_edit', $book->id, BiberLog::TargetType_book);
            }
        }
        BookLog::saveLog($book->id, BookLog::TargetType_book, $current_user->id, BiberLog::BiberLogType_bookEdit);
        Books::getInstance()->dropCache($book->id);
        if (count($to_update_m)) {
            if ($to_update_m['n'] && $book->data['book_type'] == Book::BOOK_TYPE_MAGAZINE) {
                Database::query('UPDATE `book_magazines` SET `n`=' . $to_update_m['n'] . ',`year`=' . (int) $book->data['year'] . ' WHERE `id_book`=' . $book->id);
            }
        }
        ob_end_clean();
        header('Location:' . Config::need('www_path') . '/b/' . $book->id);
        Database::query('COMMIT');
        exit;
    }