Example #1
0
 function newAuthor()
 {
     global $current_user;
     $current_user->can_throw('books_edit');
     // добавляем книгу
     $fields = array('lang_code' => 'author_lang', 'bio' => 'bio', 'first_name' => 'first_name', 'middle_name' => 'middle_name', 'last_name' => 'last_name', 'homepage' => 'homepage', 'wiki_url' => 'wiki_url', 'date_birth' => 'date_birth', 'date_death' => 'date_death');
     Request::$post['lang_code'] = Config::$langs[Request::$post['lang_code']];
     if (!Request::$post['first_name'] || !Request::$post['last_name']) {
         throw new Exception('no author\'s name');
     }
     if (!Request::$post['lang_code']) {
         throw new Exception('no author\'s language');
     }
     $to_update = array();
     foreach ($fields as $field => $personfield) {
         if (!isset(Request::$post[$field])) {
             throw new Exception('field missed #' . $field);
         }
         $to_update[$personfield] = Request::$post[$field];
     }
     $q = array();
     if (count($to_update)) {
         $to_update['authorlastSave'] = time();
     }
     foreach ($to_update as $field => $value) {
         if ($field == 'date_birth' || $field == 'date_death') {
             $value = getDateFromString($value);
         }
         $person = new Person();
         if ($field == 'bio') {
             list($full, $short) = $person->processBio($value);
             $q[] = '`bio`=' . Database::escape($full) . '';
             $q[] = '`short_bio`=' . Database::escape($short) . '';
         } else {
             $q[] = '`' . $field . '`=' . Database::escape($value) . '';
         }
     }
     if (count($q)) {
         $q[] = '`a_add_time`=' . time();
         $query = 'INSERT INTO `persons` SET ' . implode(',', $q);
         Database::query($query);
         $lid = Database::lastInsertId();
         if ($lid) {
             if (isset($_FILES['picture']) && $_FILES['picture']['tmp_name']) {
                 $folder = Config::need('static_path') . '/upload/authors/' . ceil($lid / 5000);
                 @mkdir($folder);
                 $query = 'INSERT INTO `person_covers` SET `id_person`=' . $lid;
                 Database::query($query);
                 $cover_id = Database::lastInsertId();
                 $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['has_cover'] = $cover_id;
                 $thumb = new Thumb();
                 $thumb->createThumbnails($_FILES['picture']['tmp_name'], array($filename_small, $filename_normal, $filename_big, $filename_orig), self::$cover_sizes);
                 $query = 'UPDATE `persons` SET `has_cover`=' . $cover_id . ' WHERE `id`=' . $lid;
                 Database::query($query);
             }
             unset($to_update['authorlastSave']);
             PersonLog::addLog($to_update, array(), $lid);
             PersonLog::saveLog($lid, BookLog::TargetType_person, $current_user->id, BiberLog::BiberLogType_personNew);
             $event = new Event();
             $event->event_AuthorAdd($current_user->id, $lid);
             $event->push();
             $search = Search::getInstance();
             /* @var $search Search */
             $search->setAuthorToFullUpdate($lid);
             ob_end_clean();
             Persons::getInstance()->dropCache($lid);
             $current_user->gainActionPoints(BiberLog::$actionTypes[BiberLog::BiberLogType_personNew], $lid, BiberLog::TargetType_person);
             header('Location:' . Config::need('www_path') . '/a/' . $lid);
             exit;
         }
     }
 }