Beispiel #1
0
 function write()
 {
     global $current_user;
     $current_user->can_throw('books_edit');
     $id = isset(Request::$post['id']) ? (int) Request::$post['id'] : false;
     if (!$id) {
         $this->newAuthor();
         return;
     }
     $person = Persons::getInstance()->getByIdLoaded($id);
     if (!$person) {
         return;
     }
     $savedData = $person->data;
     /* @var $book Book */
     $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');
     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');
     }
     Request::$post['lang_code'] = Config::$langs[Request::$post['lang_code']];
     $to_update = array();
     if (isset($_FILES['picture']) && $_FILES['picture']['tmp_name']) {
         $folder = Config::need('static_path') . '/upload/authors/' . ceil($person->id / 5000);
         @mkdir($folder);
         // inserting new cover
         $query = 'INSERT INTO `person_covers` SET `id_person`=' . $person->id;
         Database::query($query);
         $cover_id = Database::lastInsertId();
         // generating file names
         $filename_normal = $folder . '/default_' . $person->id . '_' . $cover_id . '.jpg';
         $filename_small = $folder . '/small_' . $person->id . '_' . $cover_id . '.jpg';
         $filename_big = $folder . '/big_' . $person->id . '_' . $cover_id . '.jpg';
         $filename_orig = $folder . '/orig_' . $person->id . '_' . $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);
         if ($savedData['has_cover']) {
             $current_user->gainActionPoints('authors_add_cover', $person->id, BiberLog::TargetType_person);
         } else {
             $current_user->gainActionPoints('authors_edit_cover', $person->id, BiberLog::TargetType_person);
         }
     }
     foreach ($fields as $field => $personfield) {
         if (!isset(Request::$post[$field])) {
             throw new Exception('field missed #' . $field);
         }
         if ($person->data[$personfield] != Request::$post[$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);
         }
         if ($field == 'bio') {
             list($full, $short) = $person->processBio($value);
             $q[] = '`bio`=' . Database::escape($full) . '';
             $q[] = '`short_bio`=' . Database::escape($short) . '';
             $value = $person->data['bio'] = $full;
             $person->data['short_bio'] = $short;
         } else {
             $q[] = '`' . $field . '`=' . Database::escape($value) . '';
             $person->data[$field] = $value;
         }
     }
     if (count($q)) {
         $query = 'UPDATE `persons` SET ' . implode(',', $q) . ' WHERE `id`=' . $person->id;
         Database::query($query);
         unset($to_update['authorlastSave']);
         PersonLog::addLog($to_update, $savedData, $person->id);
         PersonLog::saveLog($person->id, BiberLog::TargetType_person, $current_user->id, BiberLog::BiberLogType_personEdit);
         Persons::getInstance()->dropCache($person->id);
         $current_user->gainActionPoints(BiberLog::$actionTypes[BiberLog::BiberLogType_personEdit], $person->id, BiberLog::TargetType_person);
         $search = Search::getInstance();
         /* @var $search Search */
         $search->setAuthorToFullUpdate($person->id);
     }
     ob_end_clean();
     header('Location:' . Config::need('www_path') . '/a/' . $person->id);
     exit;
 }
Beispiel #2
0
    function write() {
        global $current_user;
        $id = isset(Request::$post['id']) ? (int) Request::$post['id'] : false;

        if (!$id)
            $this->newAuthor();


        $persons = Persons::getByIdsLoaded(array($id));
        $person = is_array($persons) ? $persons[$id] : false;
        if (!$person)
            return;
        $savedData = $person->data;
        /* @var $book Book */

        $fields = array(
            'lang_code' => 'author_lang', //lang_code
            'bio' => 'bio',
            'first_name' => 'first_name',
            'middle_name' => 'middle_name',
            'last_name' => 'last_name',
            //'id_user' => 'id_user',
            'homepage' => 'homepage',
            'wiki_url' => 'wiki_url',
            'date_birth' => 'date_birth',
            'date_death' => 'date_death'
        );

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


        foreach ($fields as $field => $personfield) {
            if (!isset(Request::$post[$field])) {
                throw new Exception('field missed #' . $field);
            }
            if ($person->data[$personfield] != Request::$post[$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);
            }

            if ($field == 'bio') {
                list($full, $short) = $person->processBio($value);
                $q[] = '`bio`=' . Database::escape($full) . '';
                $q[] = '`short_bio`=' . Database::escape($short) . '';
                $value = $person->data['bio'] = $full;
                $person->data['short_bio'] = $short;
            } else {
                $q[] = '`' . $field . '`=' . Database::escape($value) . '';
                $person->data[$field] = $value;
            }
        }

        if (count($q)) {
            $query = 'UPDATE `persons` SET ' . implode(',', $q) . ' WHERE `id`=' . $person->id;
            Database::query($query);
            unset($to_update['authorlastSave']);
            PersonLog::addLog($to_update, $savedData);
            PersonLog::saveLog($person->id, BiberLog::TargetType_person, $current_user->id, BiberLog::BiberLogType_personEdit);
        }
    }
Beispiel #3
0
 private static function setEdition($id1, $id2)
 {
     global $current_user;
     $type = self::RELATION_TYPE_EDITION;
     try {
         $person1 = Persons::getInstance()->getByIdLoaded($id1);
         $person2 = Persons::getInstance()->getByIdLoaded($id2);
         /* @var $person1 Person */
         /* @var $person2 Person */
         if ($person2->getLangId() != Config::$langs['ru']) {
             $type = self::RELATION_TYPE_TRANSLATE;
         }
         Database::query('START TRANSACTION');
         if ($id1 == $id2) {
             self::setError('Онанизмъ!');
             return false;
         }
         if ($person1->getBasketId() && $person1->getBasketId() == $person2->getBasketId()) {
             self::setError('Книги уже связаны!');
             return false;
         }
         // смотрим книги.
         $basket_id = max($person1->getBasketId(), $person2->getBasketId());
         $basket_old = 0;
         if (!$basket_id) {
             $query = 'INSERT INTO `pbasket` SET `time`=' . time();
             Database::query($query);
             $basket_id = Database::lastInsertId();
         }
         $to_change = array();
         $to_old = array();
         if ($basket_id == $person1->getBasketId()) {
             if ($person2->getBasketId()) {
                 $basket_old = $person2->getBasketId();
                 // из 2 корзины всё в первую
                 $query = 'SELECT * FROM `person_basket` WHERE `id_basket`=' . $person2->getBasketId();
                 $relations = Database::sql2array($query);
                 foreach ($relations as $relation) {
                     $to_change[$relation['id_person']] = $relation['id_person'];
                 }
                 $query = 'SELECT * FROM `person_basket` WHERE `id_basket`=' . $person1->getBasketId();
                 $relations = Database::sql2array($query);
                 foreach ($relations as $relation) {
                     $to_old[$relation['id_person']] = $relation['id_person'];
                 }
                 $query = 'UPDATE `person_basket` SET `id_basket`=' . $basket_id . ' WHERE `id_basket`=' . $person2->getBasketId();
                 Database::query($query);
                 $query = 'UPDATE `persons` SET `id_basket`=' . $basket_id . ' WHERE `id_basket`=' . $person2->getBasketId();
                 Database::query($query);
             } else {
                 $query = 'SELECT * FROM `person_basket` WHERE `id_basket`=' . $person1->getBasketId();
                 $relations = Database::sql2array($query);
                 foreach ($relations as $relation) {
                     $to_old[$relation['id_person']] = $relation['id_person'];
                 }
             }
         } else {
             if ($basket_id == $person2->getBasketId()) {
                 if ($person1->getBasketId()) {
                     $basket_old = $person1->getBasketId();
                     // из 1 корзины всё во вторую
                     $query = 'SELECT * FROM `person_basket` WHERE `id_basket`=' . $person1->getBasketId();
                     $relations = Database::sql2array($query);
                     foreach ($relations as $relation) {
                         $to_change[$relation['id_person']] = $relation['id_person'];
                     }
                     $query = 'SELECT * FROM `person_basket` WHERE `id_basket`=' . $person2->getBasketId();
                     $relations = Database::sql2array($query);
                     foreach ($relations as $relation) {
                         $to_old[$relation['id_person']] = $relation['id_person'];
                     }
                     $query = 'UPDATE `person_basket` SET `id_basket`=' . $basket_id . ' WHERE `id_basket`=' . $person1->getBasketId();
                     Database::query($query);
                     $query = 'UPDATE `persons` SET `id_basket`=' . $basket_id . ' WHERE `id_basket`=' . $person1->getBasketId();
                     Database::query($query);
                 } else {
                     $query = 'SELECT * FROM `person_basket` WHERE `id_basket`=' . $person2->getBasketId();
                     $relations = Database::sql2array($query);
                     foreach ($relations as $relation) {
                         $to_old[$relation['id_person']] = $relation['id_person'];
                     }
                 }
             }
         }
         // 2 книги в корзинку
         if ($person1->getBasketId() != $basket_id) {
             $to_change[$person1->id] = $person1->id;
         }
         if ($person2->getBasketId() != $basket_id) {
             $to_change[$person2->id] = $person2->id;
         }
         $query = 'UPDATE `persons` SET `id_basket`=' . $basket_id . ' WHERE `id` IN (' . $person1->id . ',' . $person2->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());
             PersonLog::addLog($now, $was, $person1->id);
             $ids = array_merge($to_change, $to_old);
             PersonLog::saveLog($ids, PersonLog::TargetType_person, $current_user->id, BiberLog::BiberLogType_personAddRelation);
         }
         foreach ($ids as $id) {
             Persons::getInstance()->dropCache($id);
         }
         $query = 'REPLACE INTO `person_basket`(`id_person`,`id_basket`) VALUES (' . $person1->id . ',' . $basket_id . '),(' . $person2->id . ',' . $basket_id . ')';
         Database::query($query);
         Database::query('COMMIT');
     } catch (Exception $e) {
         self::setError($e->getMessage());
         return false;
     }
     return true;
 }