public function actionAdd()
 {
     $orig_id = isset($_POST["orig_id"]) ? (int) $_POST["orig_id"] : (int) $_GET["orig_id"];
     $post_id = isset($_POST["post_id"]) ? (int) $_POST["post_id"] : (int) $_GET["post_id"];
     if ($orig_id) {
         $orig = Orig::model()->with("chap.book.membership")->findByPk($orig_id);
         if (!$orig) {
             throw new CHttpException(404, "Вы пытаетесь добавить несуществующий фрагмент оригинала в «мои обсуждения». Скорее всего, его удалили");
         } elseif (!$orig->chap->can("read")) {
             throw new CHttpException(403, "Вы не можете добавить этот фрагмент в «мои обсуждения» так как у вас больше нет доступа этот перевод.");
         } else {
             $orig->setTrack();
         }
     } elseif ($post_id) {
         $post = BlogPost::model()->with("book", "seen")->findByPk($post_id);
         if (!$post) {
             throw new CHttpException(404, "Вы пытаетесь добавить несуществующий пост в «мои обсуждения». Скорее всего, его удалили.");
         } else {
             if ($post->book_id != 0 and !$post->book->can("blog_r")) {
                 throw new CHttpException(403, "Вы не можете добавить этот пост в «мои обсуждения» так как у вас нет доступа в блог перевода.");
             } else {
                 $post->setTrack();
             }
         }
     } else {
         throw new CHttpException(500, "Неверный запрос.");
     }
     if ($_POST["ajax"]) {
         echo json_encode(array("status" => "ok", "id" => $orig_id ? $orig_id : $post_id));
         Yii::app()->end();
     } else {
         $this->redirect("/my/comments/?mode=" . ($orig_id ? "o" : "p"));
     }
 }
Example #2
0
 public function actionRemove($book_id, $chap_id, $orig_id)
 {
     $ajax = isset($_POST["ajax"]) ? (int) $_POST["ajax"] : (int) $_GET["ajax"];
     $chap = $this->loadChapter($book_id, $chap_id);
     if (!$chap->book->can("chap_edit")) {
         throw new CHttpException(403, "Вы не можете редактировать оригинал в этом переводе.");
     }
     /** @var Orig $orig */
     $orig = Orig::model()->findByPk((int) $orig_id, array("condition" => "chap_id = :chap_id", "params" => array(":chap_id" => $chap->id)));
     if (!$orig) {
         throw new CHttpException(404, "Фрагмента оригинала не существует. Вероятно, его кто-то уже удалил.");
     }
     $orig->chap = $chap;
     /** @var Translation[] $trs все переводы, которые сейчас будут удалены */
     $trs = Translation::model()->chapter($chap->id)->findAllByAttributes(array("orig_id" => $orig->id));
     $n_trs = count($trs);
     if (!$orig->delete()) {
         throw new CHttpException(500, "Не получилось удалить фрагмент оригинала.");
     }
     $chap->setModified();
     // (chapters, books).(n_verses, n_vars, d_vars, last_tr)
     $chap->n_verses--;
     $chap->book->n_verses--;
     $chap->n_vars -= $n_trs;
     $chap->book->n_vars -= $n_trs;
     if ($n_trs > 0) {
         $chap->d_vars--;
         $chap->book->d_vars--;
     }
     $chap->last_tr = new CDbExpression("now()");
     $chap->book->last_tr = new CDbExpression("now()");
     // users.(n_trs, rate_t), groups.(n_trs, rating)
     $sql = array();
     $params = array(":book_id" => $chap->book_id);
     foreach ($trs as $i => $tr) {
         if ($tr->user_id == 0) {
             continue;
         }
         $sql[] = "UPDATE users  SET n_trs = n_trs - 1, rate_t = rate_t - :rating{$i} WHERE id = :user_id{$i};";
         $sql[] = "UPDATE groups SET n_trs = n_trs - 1, rating = rating - :rating{$i} WHERE book_id = :book_id AND user_id = :user_id{$i};";
         $params[":user_id{$i}"] = $tr->user_id;
         $params[":rating{$i}"] = $tr->rating;
     }
     $chap->save(false, array("n_verses", "n_vars", "d_vars", "last_tr"));
     $chap->book->save(false, array("n_verses", "n_vars", "d_vars", "last_tr"));
     if (count($sql) > 0) {
         $sql = "BEGIN;\n" . join("\n", $sql) . "\nCOMMIT;";
         Yii::app()->db->createCommand($sql)->execute($params);
     }
     echo json_encode(array("status" => "ok", "n_vars" => $chap->n_vars, "d_vars" => $chap->d_vars, "n_verses" => $chap->n_verses));
 }
Example #3
0
 public function actionOrig_download($book_id, $chap_id)
 {
     $chap = $this->loadChapter($book_id, $chap_id);
     $options = new GenOptions();
     $options->chap = $chap;
     $options->setAttributes($_GET);
     if (!$options->validate()) {
         $this->redirect($chap->getUrl("orig?" . $_SERVER["QUERY_STRING"]));
     }
     $options->saveOptions();
     $crit = new CDbCriteria(array("order" => $chap->book->typ == "S" ? "t.t1" : "t.ord"));
     $orig = Orig::model()->chapter($chap->id)->findAll($crit);
     $G = ReadyGenerator::factory($options, $chap, $orig);
     $data = $G->generateOrig(true);
     if ($options->enc != "UTF-8") {
         $data = iconv("UTF-8", $options->enc . "//IGNORE", $data);
     }
     if (1) {
         $logRouter = Yii::app()->getComponent("log");
         if (isset($logRouter)) {
             $routes = $logRouter->getRoutes();
             foreach ($routes as $route) {
                 if ($route instanceof CWebLogRoute) {
                     $route->enabled = false;
                 }
             }
         }
         $fname = str_replace(" ", "_", $chap->title . "-orig." . GenOptions::$extensions[$options->format]);
         Yii::app()->request->sendFile($fname, $data);
     } else {
         echo "<h3>Result (" . strlen($data) . ") bytes</h3><pre>";
         echo htmlspecialchars($data);
         echo "</pre>";
     }
 }
Example #4
0
 /**
  * Инициализирует новый фрагмент в главе: для субтитров считает тайминг, для текста - ord
  */
 public function initNew($after_id = 0)
 {
     $after = false;
     if ($after_id) {
         $after = Orig::model()->findByPk((int) $_GET["after"], "chap_id = :chap_id", array(":chap_id" => $this->chap_id));
     }
     if ($this->chap->book->typ == "S") {
         if ($after) {
             $t1 = $after->mstime("t2") + 1;
             $next = Orig::model()->find(array("condition" => "chap_id = :chap_id AND t1 >= :t1 AND id != :id", "params" => array(":chap_id" => $this->chap_id, ":t1" => $after->t1, ":id" => $after->id), "order" => "t1"));
             if ($next) {
                 $t2 = $next->mstime("t1") - 1;
             } else {
                 $t2 = $after->mstime("t2") + 2001;
             }
             if ($t2 < $t1) {
                 $t2 = $t1;
             }
             $this->t1 = Orig::ms2std($t1);
             $this->t2 = Orig::ms2std($t2);
         } else {
             list($this->t1, $this->t2) = Yii::app()->db->createCommand("SELECT COALESCE(MAX(t2) + interval '0:0:1', '0:0:0') as t1, COALESCE(MAX(t2) + interval '0:0:2', '0:0:1') as t2 FROM orig WHERE chap_id = :chap_id")->queryRow(false, array(":chap_id" => $this->chap_id));
         }
     } else {
     }
     if ($after) {
         $this->ord = $after->ord + 1;
     } else {
         $this->ord = Yii::app()->db->createCommand("SELECT COALESCE(MAX(ord), 0) + 1 FROM orig WHERE chap_id = :chap_id")->queryScalar(array(":chap_id" => $this->chap_id));
     }
 }
Example #5
0
 public function verse($t1, $t2, $body, $ord = null)
 {
     echo "<SYNC Start=" . Orig::std2ms($t1) . ">{$this->eol}";
     echo "<P>" . trim($body) . "</P>{$this->eol}{$this->eol}";
 }
Example #6
0
 public function actionBookmark_set()
 {
     if (!is_array($_POST["B"])) {
         throw new CHttpException(500, "Неверный запрос.");
     }
     // Есть ли уже такая закладка?
     $bookmark = Bookmark::model()->findByAttributes(array("user_id" => Yii::app()->user->id, "typ" => substr($_POST["B"]["typ"], 0, 1), "obj_id" => (int) $_POST["B"]["obj_id"]));
     if (!$bookmark) {
         $bookmark = new Bookmark();
         $bookmark->user_id = Yii::app()->user->id;
     }
     $bookmark->setAttributes($_POST["B"]);
     if ($bookmark->typ == "o") {
         $orig = Orig::model()->with("chap.book")->findByPk($bookmark->obj_id);
         if (!$orig) {
             throw new CHttpException(404, "Фрагмент оригинала удалён.");
         }
         $bookmark->url = $orig->url;
     } elseif ($bookmark->typ == "c") {
         $chap = Chapter::model()->with("book")->findByPk($bookmark->obj_id);
         if (!$chap) {
             throw new CHttpException(404, "Глава удалена");
         }
         $bookmark->url = $chap->url;
     } elseif ($bookmark->typ == "b") {
         $book = Book::model()->findByPk($bookmark->obj_id);
         if (!$book) {
             throw new CHttpException(404, "Глава удалена");
         }
         $bookmark->url = $book->url;
     } else {
         throw new CHttpException(500, "Неверный запрос. Если вам интересны грязные подробности, то я не знаю, что такое тип закладки '{$bookmark->typ}'.");
     }
     if ($bookmark->isNewRecord) {
         $bookmark->ord = Yii::app()->db->createCommand("SELECT MAX(ord) FROM bookmarks WHERE typ = :typ AND user_id = :user_id")->queryScalar(array(":typ" => $bookmark->typ, ":user_id" => Yii::app()->user->id)) + 1;
     }
     if (!$bookmark->save()) {
         throw new CHttpException(500, $bookmark->getErrorsString());
     }
     $json = array("id" => $bookmark->id, "title" => $bookmark->title);
     echo json_encode($json);
 }