示例#1
0
 function set()
 {
     global $current_user;
     $this->data['success'] = 1;
     if (!$current_user->authorized) {
         $this->error('Auth');
         return;
     }
     /* @var $current_user User */
     $id_user = false;
     if (isset($_POST['id_user'])) {
         if (!$current_user->can('ocr_edit')) {
             $this->error('You must be biber to do that');
             return;
         } else {
             $id_user = (int) $_POST['id_user'];
         }
     }
     $_POST['status'] = isset($_POST['status']) ? $_POST['status'] : -1;
     $_POST['state'] = isset($_POST['state']) ? $_POST['state'] : -1;
     $id_user = $id_user ? $id_user : $current_user->id;
     $id_book = max(0, (int) $_POST['id_book']);
     if (!is_numeric($_POST['status'])) {
         foreach (Ocr::$statuses as $s) {
             if ($s['name'] == $_POST['status']) {
                 $_POST['status'] = $s['id'];
             }
         }
     }
     if (!is_numeric($_POST['state'])) {
         foreach (Ocr::$states as $s) {
             if ($s['name'] == $_POST['state']) {
                 $_POST['state'] = $s['id'];
             }
         }
     }
     $user = Users::getById($id_user);
     /*@var $user User*/
     $user->load();
     $status = max(-1, (int) $_POST['status']);
     $state = max(-1, (int) $_POST['state']);
     try {
         Ocr::setStatus($id_user, $id_book, $status, $state);
     } catch (Exception $e) {
         $this->error($e->getMessage());
     }
     if ($state == Ocr::STATE_APPROVED) {
         $user->gainActionPoints('ocr_add', $id_book, BiberLog::TargetType_book);
     }
 }
示例#2
0
 public function postOcrShare()
 {
     $rules = array('shareEmail' => 'required|email');
     $validator = Validator::make(Input::all(), $rules);
     if ($validator->fails()) {
         $data['status'] = 'error';
         $data['errors'] = $validator->messages()->toArray();
     } else {
         $data['status'] = 'success';
         $to = Input::get('shareEmail');
         $request = Ocr::find(Input::get('request_id'));
         define('BUDGETS_DIR', public_path('uploads'));
         // I define this in a constants.php file
         if (!is_dir(BUDGETS_DIR)) {
             mkdir(BUDGETS_DIR, 0755, true);
         }
         $outputName = str_random(10);
         // str_random is a [Laravel helper](http://laravel.com/docs/helpers#strings)
         $pdfPath = BUDGETS_DIR . '/' . $outputName . '.pdf';
         $link = Input::get('pdf');
         Mail::send('emails.form.ocr_share', compact('link'), function ($message) use($pdfPath, $to) {
             $message->from('*****@*****.**', 'REFERECOM');
             $message->to($to)->subject('Letter from Referecom');
         });
         $data['status'] = 'success';
         $user = Sentry::getUser()->id;
         $timeline = new Timeline();
         $timeline->activity_type = 'share-Form';
         $timeline->form_ops = $request->id;
         $timeline->user_id = $user;
         $timeline->shared_email = Input::get('shareEmail');
         $timeline->save();
     }
     return Response::json($data);
 }
示例#3
0
    function newBook()
    {
        // добавляем книгу
        global $current_user;
        /* @var $current_user CurrentUser */
        $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['title'] = trim(prepare_review(Request::$post['title'], ''));
        Request::$post['annotation'] = trim(prepare_review(Request::$post['annotation'], false, '<img>'));
        if (!Request::$post['title']) {
            throw new Exception('title missed');
        }
        foreach ($fields as $field => $bookfield) {
            if (!isset(Request::$post[$field])) {
                throw new Exception('field missed #' . $field);
            }
            $to_update[$bookfield] = Request::$post[$field];
        }
        if (Request::$post['n'] && Request::$post['m']) {
            $to_update['book_type'] = Book::BOOK_TYPE_MAGAZINE;
            $to_update['title'] = Database::sql2single('SELECT `title` FROM `magazines` WHERE `id`=' . (int) Request::$post['m']);
        }
        $q = array();
        foreach ($to_update as $field => $value) {
            if (in_array($field, array('year'))) {
                $value = (int) $value;
            }
            $q[] = '`' . $field . '`=' . Database::escape($value) . '';
        }
        if (count($q)) {
            $q[] = '`add_time`=' . time();
            $query = 'INSERT INTO `book` SET ' . implode(',', $q);
            Database::query($query);
            if ($lid = Database::lastInsertId()) {
                if (Request::$post['n'] && Request::$post['m']) {
                    // журнал - вставляем
                    $query = 'INSERT INTO `book_magazines` SET `id_book`=' . $lid . ',id_magazine=' . (int) Request::$post['m'] . ',`year`=' . $to_update['year'] . ',`n`=' . (int) Request::$post['n'];
                    Database::query($query, false);
                    $query = 'UPDATE `magazines` SET `books_count`=(SELECT COUNT(1) FROM `book_magazines` WHERE `id_magazine`=' . (int) Request::$post['m'] . ')
						WHERE `id`=' . (int) Request::$post['m'];
                    Database::query($query);
                }
                if (isset(Request::$post['author_id']) && Request::$post['author_id']) {
                    $query = 'INSERT INTO `book_persons` SET `id_book`=' . $lid . ',`id_person`=' . (int) Request::$post['author_id'] . ',`person_role`=' . Book::ROLE_AUTHOR;
                    Database::query($query, false);
                    BookLog::addLog(array('id_person' => (int) Request::$post['author_id'], 'person_role' => Book::ROLE_AUTHOR), array('id_person' => 0, 'person_role' => 0), $lid);
                    BookLog::saveLog($lid, BookLog::TargetType_book, $current_user->id, BiberLog::BiberLogType_bookEditPerson);
                    Notify::notifyAuthorNewBook((int) Request::$post['author_id'], $lid);
                }
                if (isset($_FILES['cover']) && $_FILES['cover']['tmp_name']) {
                    $folder = Config::need('static_path') . '/upload/covers/' . ceil($lid / 5000);
                    @mkdir($folder);
                    // inserting new cover
                    $query = 'INSERT INTO `book_covers` SET `id_book`=' . $lid;
                    Database::query($query);
                    $cover_id = Database::lastInsertId();
                    // generating file names
                    $filename_normal = $folder . '/default_' . $lid . '_' . $cover_id . '.jpg';
                    $filename_small = $folder . '/small_' . $lid . '_' . $cover_id . '.jpg';
                    $filename_big = $folder . '/big_' . $lid . '_' . $cover_id . '.jpg';
                    $filename_orig = $folder . '/orig_' . $lid . '_' . $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);
                    $query = 'UPDATE `book` SET `is_cover`=' . $cover_id . ' WHERE `id`=' . $lid;
                    Database::query($query);
                    $current_user->gainActionPoints('books_add_cover', $lid, BiberLog::TargetType_book);
                }
                // 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 = '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), $lid);
                    $to_update_ = array();
                    if ($id_file) {
                        $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);
                        }
                        if ($filetype == 1) {
                            // FB2
                            $parser = new FB2Parser($destinationFile);
                            $parser->parseDescription();
                            $toc = $parser->getTOCHTML();
                            $to_update_['description'] = $parser->getProperty('annotation');
                            $to_update_['title'] = $parser->getProperty('book-title');
                            $to_update_['table_of_contents'] = $toc;
                        }
                        $q = array();
                        foreach ($to_update_ as $field => &$value) {
                            if (in_array($field, array('year'))) {
                                $value = is_numeric($value) ? $value : 0;
                            }
                            $q[] = '`' . $field . '`=' . Database::escape($value) . '';
                        }
                        if (count($q)) {
                            $query = 'UPDATE `book` SET ' . implode(',', $q) . ' WHERE `id`=' . $lid;
                            Database::query($query);
                            BookLog::addLog($to_update_, $book->data, $book->id);
                        }
                        $to_update = array_merge($to_update, $to_update_);
                    }
                } else {
                    // it is no any files!
                    Ocr::afterBookCreate($lid, $current_user->id);
                }
                BookLog::addLog($to_update, array(), $lid);
                BookLog::saveLog($lid, BookLog::TargetType_book, $current_user->id, BiberLog::BiberLogType_bookNew);
                ob_end_clean();
                $event = new Event();
                $event->event_BooksAdd($current_user->id, $lid);
                $event->push();
                $search = Search::getInstance();
                /* @var $search Search */
                $search->updateBook(new Book($lid));
                Books::getInstance()->dropCache($lid);
                header('Location:' . Config::need('www_path') . '/b/' . $lid);
                Database::query('COMMIT');
                $current_user->gainActionPoints('books_new_nofile', $lid, BiberLog::TargetType_book);
                exit;
            }
        }
    }