Ejemplo n.º 1
0
	function write() {
		global $current_user;
		//$this->setWriteParameter('register_module', 'result', false);

		$id = isset(Request::$post['id']) ? (int) Request::$post['id'] : false;


		if (!$id) {
			$this->newBook();
			return;
		}


		$books = Books::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', //lang_code
		    'annotation' => 'description'
		);

		Request::$post['lang_code'] = Config::$langs[Request::$post['lang_code']];
		$to_update = array();
		if (isset($_FILES['cover']) && $_FILES['cover']['tmp_name']) {
			$folder = Config::need('static_path') . '/upload/covers/' . (ceil($book->id / 5000));
			@mkdir($folder);
			chmod($folder, 755);
			$filename = $folder . '/' . $book->id . '.jpg';
			$upload = new UploadAvatar($_FILES['cover']['tmp_name'], 100, 100, "simple", $filename);
			if ($upload->out)
				$to_update['is_cover'] = 1;
			else {
				throw new Exception('cant copy file to ' . $filename, 100);
			}
		}

		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] : 'fb2';
			$filetype = $filetype == 'fb2' ? 1 : 0;
			if (!$filetype)
				throw new Exception('only fb2 allowed');
			$destinationDir = Config::need('files_path') . DIRECTORY_SEPARATOR . getBookFileDirectory($book->id, $filetype);
			@mkdir($destinationDir, 755);
			// добавляем запись в базу
			$filesize = $_FILES['file']['size'];
			$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);
			if ($id_file = 78037 || $id_file = Database::lastInsertId()) {
				$destinationFile = getBookFilePath($id_file, $book->id, $filetype, Config::need('files_path'));
				move_uploaded_file($_FILES['file']['tmp_name'], $destinationFile);
			}
			if ($filetype == 1) {
				$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) {
			if (in_array($field, array('ISBN', 'year'))) {
				$value = is_numeric($value) ? $value : 0;
			}
			$q[] = '`' . $field . '`=' . Database::escape($value) . '';
		}

		if (count($q)) {
			$query = 'UPDATE `book` SET ' . implode(',', $q) . ' WHERE `id`=' . $book->id;
			Database::query($query);
			BookLog::addLog($to_update, $book->data);
			BookLog::saveLog($book->id, BookLog::TargetType_book, $current_user->id, BiberLog::BiberLogType_bookEdit);
		}
		ob_end_clean();
		header('Location:' . Config::need('www_path') . '/b/' . $book->id);
		exit();
	}
Ejemplo n.º 2
0
 private static function setEdition($id1, $id2)
 {
     global $current_user;
     $type = self::RELATION_TYPE_EDITION;
     try {
         $book1 = Books::getInstance()->getByIdLoaded($id1);
         $book2 = Books::getInstance()->getByIdLoaded($id2);
         /* @var $book1 Book */
         /* @var $book2 Book */
         if ($book2->getLangId() != Config::$langs['ru']) {
             //if ($book1->getLangId() != $book2->getLangId()) {
             $type = self::RELATION_TYPE_TRANSLATE;
         }
         Database::query('START TRANSACTION');
         if ($id1 == $id2) {
             self::setError('Онанизмъ!');
             return false;
         }
         if ($book1->getBasketId() && $book1->getBasketId() == $book2->getBasketId()) {
             self::setError('Книги уже связаны!');
             return false;
         }
         // смотрим книги.
         $basket_id = max($book1->getBasketId(), $book2->getBasketId());
         $basket_old = 0;
         if (!$basket_id) {
             $query = 'INSERT INTO `basket` SET `time`=' . time();
             Database::query($query);
             $basket_id = Database::lastInsertId();
         }
         $to_change = array();
         $to_old = array();
         if ($basket_id == $book1->getBasketId()) {
             if ($book2->getBasketId()) {
                 $basket_old = $book2->getBasketId();
                 // из 2 корзины всё в первую
                 $query = 'SELECT * FROM `book_basket` WHERE `id_basket`=' . $book2->getBasketId();
                 $relations = Database::sql2array($query);
                 foreach ($relations as $relation) {
                     $to_change[$relation['id_book']] = $relation['id_book'];
                 }
                 $query = 'SELECT * FROM `book_basket` WHERE `id_basket`=' . $book1->getBasketId();
                 $relations = Database::sql2array($query);
                 foreach ($relations as $relation) {
                     $to_old[$relation['id_book']] = $relation['id_book'];
                 }
                 $query = 'UPDATE `book_basket` SET `id_basket`=' . $basket_id . ' WHERE `id_basket`=' . $book2->getBasketId();
                 Database::query($query);
                 $query = 'UPDATE `book` SET `id_basket`=' . $basket_id . ' WHERE `id_basket`=' . $book2->getBasketId();
                 Database::query($query);
             } else {
                 $query = 'SELECT * FROM `book_basket` WHERE `id_basket`=' . $book1->getBasketId();
                 $relations = Database::sql2array($query);
                 foreach ($relations as $relation) {
                     $to_old[$relation['id_book']] = $relation['id_book'];
                 }
             }
         } else {
             if ($basket_id == $book2->getBasketId()) {
                 if ($book1->getBasketId()) {
                     $basket_old = $book1->getBasketId();
                     // из 1 корзины всё во вторую
                     $query = 'SELECT * FROM `book_basket` WHERE `id_basket`=' . $book1->getBasketId();
                     $relations = Database::sql2array($query);
                     foreach ($relations as $relation) {
                         $to_change[$relation['id_book']] = $relation['id_book'];
                     }
                     $query = 'SELECT * FROM `book_basket` WHERE `id_basket`=' . $book2->getBasketId();
                     $relations = Database::sql2array($query);
                     foreach ($relations as $relation) {
                         $to_old[$relation['id_book']] = $relation['id_book'];
                     }
                     $query = 'UPDATE `book_basket` SET `id_basket`=' . $basket_id . ' WHERE `id_basket`=' . $book1->getBasketId();
                     Database::query($query);
                     $query = 'UPDATE `book` SET `id_basket`=' . $basket_id . ' WHERE `id_basket`=' . $book1->getBasketId();
                     Database::query($query);
                 } else {
                     $query = 'SELECT * FROM `book_basket` WHERE `id_basket`=' . $book2->getBasketId();
                     $relations = Database::sql2array($query);
                     foreach ($relations as $relation) {
                         $to_old[$relation['id_book']] = $relation['id_book'];
                     }
                 }
             }
         }
         // 2 книги в корзинку
         if ($book1->getBasketId() != $basket_id) {
             $to_change[$book1->id] = $book1->id;
             //$to_old[$book2->id] = $book2->id;
         }
         if ($book2->getBasketId() != $basket_id) {
             $to_change[$book2->id] = $book2->id;
             //$to_old[$book1->id] = $book1->id;
         }
         $query = 'UPDATE `book` SET `id_basket`=' . $basket_id . ' WHERE `id` IN (' . $book1->id . ',' . $book2->id . ')';
         Database::query($query);
         // сохраняем в лог создание релейшна для книг, у которых поменялся id_basket
         if (count($to_change)) {
             $now = array('id_basket' => $basket_id, 'new_relations' => $to_change, 'old_relations' => $to_old);
             $was = array('id_basket' => $basket_old, 'new_relations' => array(), 'old_relations' => array());
             BookLog::addLog($now, $was, $book1->id);
             $ids = array_merge($to_change, $to_old);
             BookLog::saveLog($ids, BookLog::TargetType_book, $current_user->id, BiberLog::BiberLogType_bookAddRelation);
         }
         foreach ($ids as $id) {
             Books::getInstance()->dropCache($id);
         }
         $query = 'REPLACE INTO `book_basket`(`id_book`,`id_basket`) VALUES (' . $book1->id . ',' . $basket_id . '),(' . $book2->id . ',' . $basket_id . ')';
         Database::query($query);
         Database::query('COMMIT');
     } catch (Exception $e) {
         self::setError($e->getMessage());
         return false;
     }
     return true;
 }
Ejemplo n.º 3
0
 function del_author()
 {
     global $current_user;
     $this->ca();
     $id_person = (int) $_POST['item_id'];
     $id_book = (int) $_POST['id'];
     $query = 'SELECT `person_role` FROM `book_persons` WHERE `id_book`=' . $id_book . ' AND `id_person`=' . $id_person;
     $old_role = Database::sql2single($query);
     if ($old_role) {
         if ($id_person && $id_book) {
             $query = 'DELETE FROM `book_persons` WHERE `id_book`=' . $id_book . ' AND `id_person`=' . $id_person;
             Database::query($query);
             $this->data['success'] = 1;
             $this->data['item_id'] = $id_person;
             BookLog::addLog(array('id_person' => 0, 'person_role' => 0), array('id_person' => $id_person, 'person_role' => $old_role), $id_book);
             BookLog::saveLog($id_book, BookLog::TargetType_book, $current_user->id, BiberLog::BiberLogType_bookEditPerson);
             $search = Search::getInstance();
             /* @var $search Search */
             $search->updateBook(new Book($id_book));
             return;
         }
     } else {
         $this->data['error'] = 'Нет такого автора';
     }
     $this->data['item_id'] = $id_person;
     $this->data['success'] = 0;
 }
Ejemplo n.º 4
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;
    }