Example #1
0
 public function articleSave()
 {
     //If saving a new article
     if ($_POST["save"] == "Přidat") {
         $article = new Article();
         $article->setIdUser($_POST["author"]);
         if (isset($_POST["image-article"])) {
             $data = explode(',', $_POST["image-article"]);
             if (count($data) == 2 && $data[0] == "data:image/png;base64" && base64_decode($data[1])) {
                 $dir = "includes/images/fullsize/";
                 $img = imagecreatefromstring(base64_decode($data[1]));
                 do {
                     $name = token(20) . ".png";
                     $path = $dir . $name;
                 } while (file_exists($path));
                 $i = new Image();
                 $i->setPath($name)->setThumbnailPath($name)->setType("fullsize")->save();
                 imagepng($img, $path);
                 $dir = "includes/images/960x540/";
                 $path = $dir . $name;
                 imagepng(resizeImg($img, 960, 540), $path);
                 $dir = "includes/images/50x50/";
                 $path = $dir . $name;
                 imagepng(resizeImg($img, 50, 50), $path);
             } else {
                 $this->addPopup('danger', 'Něco se pokazilo při nahrávání obrázku. Zkuste to prosím znovu.');
                 redirectTo('/administrace/clanky/pridat');
             }
         } else {
             $this->addPopup('danger', 'Něco se pokazilo při nahrávání obrázku. Zkuste to prosím znovu.');
             redirectTo('/administrace/clanky/pridat');
         }
         $im = ImageQuery::create()->filterByPath($name)->findOne();
         $article->setIdImage($im->getId());
         $article->setIdCategory($_POST["category"]);
         $article->setTitle($_POST["title"]);
         $article->setKeywords(str_replace(", ", ",", $_POST["keywords"]));
         $article->setContent($_POST["content"]);
         $article->save();
         $article = ArticleQuery::create()->orderByCreatedAt('desc')->findOne();
         $article->setUrl($article->getId());
         $article->save();
         $this->addPopup('success', 'Článek byl úspěšně přidán!');
         redirectTo("/administrace");
     } elseif ($_POST["save"] == "Upravit") {
         $article = ArticleQuery::create()->findPk($_POST["article"]);
         $article->setIdCategory($_POST["category"]);
         $article->setTitle($_POST["title"]);
         $article->setKeywords(str_replace(", ", ",", $_POST["keywords"]));
         $article->setContent($_POST["content"]);
         if (isset($_POST["image-article"])) {
             $data = explode(',', $_POST["image-article"]);
             if (count($data) == 2 && $data[0] == "data:image/png;base64" && base64_decode($data[1])) {
                 $dir = "includes/images/fullsize/";
                 $img = imagecreatefromstring(base64_decode($data[1]));
                 do {
                     $name = token(20) . ".png";
                     $path = $dir . $name;
                 } while (file_exists($path));
                 $i = new Image();
                 $i->setPath($name)->setThumbnailPath($name)->setType("fullsize")->save();
                 imagepng($img, $path);
                 $dir = "includes/images/960x540/";
                 $path = $dir . $name;
                 imagepng(resizeImg($img, 960, 540), $path);
                 $dir = "includes/images/50x50/";
                 $path = $dir . $name;
                 imagepng(resizeImg($img, 50, 50), $path);
                 $im = ImageQuery::create()->filterByPath($name)->findOne();
                 $article->setIdImage($im->getId());
             } else {
                 $this->addPopup('danger', 'Něco se pokazilo při nahrávání obrázku. Zkuste to prosím znovu.');
                 redirectTo('/administrace/clanek/' . $id . '/upravit');
             }
         }
         $article->save();
         $this->addPopup('success', 'Článek byl úspěšně upraven!');
         redirectTo("/administrace/clanky");
     }
 }