Beispiel #1
0
 public static function create($content, $updateId, $userId, $replyTo)
 {
     if (!is_numeric($updateId) || !is_numeric($userId)) {
         return false;
     }
     if (!empty($replyTo)) {
         if (!is_numeric($replyTo)) {
             return false;
         }
         $stmt = CW::$app->db->executeQuery('SELECT `reply_to` FROM `comments` WHERE `update_id` = ' . $updateId);
         $result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
         if (0 < count($result)) {
             if (null !== $result[0]['reply_to'] && null !== $replyTo) {
                 return false;
             }
         }
     }
     CW::$app->db->beginTransaction();
     if (null !== $replyTo) {
         $query = "INSERT INTO `comments` (`user_id`, `update_id`, `content`, `reply_to`, `posted_on`) VALUES (:user_id, :update_id, :content, :reply_to, :posted_on)";
         $params = [':user_id' => $userId, ':update_id' => $updateId, ':content' => $content, ':reply_to' => $replyTo, ':posted_on' => time()];
     } else {
         $query = "INSERT INTO `comments` (`user_id`, `update_id`, `content`, `posted_on`) VALUES (:user_id, :update_id, :content, :posted_on)";
         $params = [':user_id' => $userId, ':update_id' => $updateId, ':content' => $content, ':posted_on' => time()];
     }
     $stmt = CW::$app->db->prepare($query);
     $stmt->execute($params);
     $newCommentId = CW::$app->db->getLastInsertedId();
     Update::addActivity($updateId, $userId, Update::ACTIVITY_TYPE_COMMENT);
     CW::$app->db->executeUpdate("UPDATE `updates` SET `comments` = `comments` + 1 WHERE `id` = {$updateId}");
     CW::$app->db->commit();
     $comment = new self();
     $comment->id = $newCommentId;
     $comment->content = $content;
     $comment->replyTo = $replyTo;
     $comment->update_id = $updateId;
     return $comment;
 }
 public function save()
 {
     $imageDg = $this->image->getImage();
     $ext = "jpeg";
     $stmt = CW::$app->db->prepare("INSERT INTO `updates` (`user_id`, `description`, `is_gif`, `created_at`) VALUES (:userId, :description, " . ($this->image->isGif() ? 1 : 0) . ", :created_at)");
     if (0 >= $stmt->execute([':userId' => \CW::$app->user->identity->id, ':description' => $this->title, ':created_at' => time()])) {
         return false;
     }
     $this->newUpdateId = CW::$app->db->getLastInsertedId();
     if (!Update::addActivity($this->newUpdateId, CW::$app->user->identity->id, Update::ACTIVITY_TYPE_POST)) {
         return false;
     }
     $this->addTags();
     $a = [];
     foreach ($this->categories as $category) {
         $c = (int) $category;
         $a[] = "({$this->newUpdateId}, {$c})";
     }
     $categoryInsert = 'INSERT INTO `update_categories` (`update_id`, `category_id`) VALUES ' . implode(',', $a);
     if (0 >= CW::$app->db->executeUpdate($categoryInsert)) {
         return false;
     }
     $updateDir = CW::$app->params['sitePath'] . 'public_html/images/updates/' . $this->newUpdateId;
     if (!file_exists(CW::$app->params['sitePath'] . 'public_html/images/updates')) {
         mkdir(CW::$app->params['sitePath'] . 'public_html/images/updates');
     }
     mkdir($updateDir);
     if ('image/gif' === $this->image->getExt()) {
         if (!file_exists(CW::$app->params['sitePath'] . 'public_html/images/tmp')) {
             mkdir(CW::$app->params['sitePath'] . 'public_html/images/tmp');
         }
         $i = ImageHelper::loadImage($this->image->getImage());
         if (!$i) {
             $this->addError('image', 'Invalid gif.');
             return false;
         }
         imagejpeg(ImageHelper::scaleImage($i->getImage(), 500), CW::$app->params['sitePath'] . "public_html/images/updates/{$this->newUpdateId}/poster.jpeg");
         $inpFile = CW::$app->params['sitePath'] . "public_html/images/tmp/{$this->newUpdateId}_medium.gif";
         $outFileMedium = "{$updateDir}/medium";
         foreach (['mp4', 'webm'] as $ext) {
             $mediumBytes = ImageHelper::resizeGif($this->image->getImage(), 500);
             file_put_contents($inpFile, $mediumBytes);
             ImageHelper::gifToVideo($inpFile, "{$outFileMedium}.{$ext}");
             unlink($inpFile);
         }
         $imageType = Image::IMAGE_TYPE_VIDEO;
         $type = Image::TYPE_IMAGE;
     } else {
         $imageBig = ImageHelper::scaleImage($imageDg, Update::IMAGE_BIG_WIDTH, PHP_INT_MAX);
         $imageMedium = ImageHelper::scaleImage($imageDg, Update::IMAGE_MEDIUM_WIDTH, PHP_INT_MAX);
         $highImage = imagesy($imageMedium) > imagesx($imageMedium) + 150;
         if ($highImage) {
             $imageMedium = ImageHelper::cropImage(0, 0, imagesx($imageMedium), 300, $imageMedium);
         }
         $imageSmall = ImageHelper::scaleImage($imageDg, Update::IMAGE_SMALL_WIDTH, PHP_INT_MAX);
         if ($highImage) {
             $imageSmall = ImageHelper::cropImage(0, 0, imagesx($imageSmall), 150, $imageSmall);
         }
         imagejpeg($imageBig, sprintf("%s/%dxX.%s", $updateDir, Update::IMAGE_BIG_WIDTH, $ext));
         imagejpeg($imageMedium, sprintf("%s/%dxX.%s", $updateDir, Update::IMAGE_MEDIUM_WIDTH, $ext));
         imagejpeg($imageSmall, sprintf("%s/%dxX.%s", $updateDir, Update::IMAGE_SMALL_WIDTH, $ext));
         imagedestroy($imageBig);
         imagedestroy($imageMedium);
         imagedestroy($imageSmall);
         $imageType = Image::IMAGE_TYPE_NORMAL;
         $type = $highImage ? Image::TYPE_HIGH_IMAGE : Image::TYPE_IMAGE;
     }
     Image::create(['rel_id' => $this->newUpdateId, 'rel_type' => Image::REL_TYPE_UPDATE, 'type' => $type, 'image_type' => $imageType]);
     return true;
 }