Exemplo n.º 1
0
 public function createPost()
 {
     $data = Input::all();
     $validator = Validator::make($data, Post::$rules);
     if ($validator->passes()) {
         try {
             $post = new Post();
             $post->post = $data['post'];
             $post->user_id = Auth::user()->id;
             $post->created_at = Carbon::now();
             $post->feeling = $data['feel'];
             $post->type = $data['type'];
             ///////////////////06-May-2015-Ehsan
             if (isset($data['hideName'])) {
                 $post->hide_name = $data['hideName'];
             }
             if ($post->type == 2) {
                 $campus = UserCampus::whereUserId(Auth::user()->id)->first();
                 if ($campus) {
                     $campus = $campus->campus_id;
                 } else {
                     $campus = 1;
                 }
                 $post->campus_id = $campus;
             }
             if (Auth::user()->disable == '2') {
                 return 'false';
             }
             if ($post->save()) {
                 $notification = new Notification();
                 $notification->user_id = $post->user_id;
                 $notification->type = 1;
                 $notification->post_id = $post->id;
                 $notification->created_at = Carbon::now();
                 $notification->updated_at = Carbon::now();
                 $notification->save();
                 $notification = new Notification();
                 $notification->user_id = $post->user_id;
                 $notification->type = 2;
                 $notification->post_id = $post->id;
                 $notification->created_at = Carbon::now();
                 $notification->updated_at = Carbon::now();
                 $notification->save();
                 $notification = new Notification();
                 $notification->user_id = $post->user_id;
                 $notification->type = 3;
                 $notification->post_id = $post->id;
                 $notification->created_at = Carbon::now();
                 $notification->updated_at = Carbon::now();
                 $notification->save();
                 if ($post->type >= 1 && $post->type <= 3) {
                     /*26-11-Ehsan*/
                     preg_match_all('/#([a-zA-Z0-9\\x{0980}-\\x{09FF}_])+/u', $post->post, $matches);
                     foreach ($matches[0] as $key) {
                         $tag = HashTag::whereTag($key)->first();
                         $tagged_post = TaggedPost::wherePostId($post->id)->whereTagId($tag->id)->first();
                         if ($tagged_post) {
                             continue;
                         }
                         if ($tag) {
                             $tagged_post = new TaggedPost();
                             $tagged_post->post_id = $post->id;
                             $tagged_post->tag_id = $tag->id;
                             $tagged_post->save();
                         } else {
                             $tag = new HashTag();
                             $tag->tag = $key;
                             $tag->save();
                             $tagged_post = new TaggedPost();
                             $tagged_post->post_id = $post->id;
                             $tagged_post->tag_id = $tag->id;
                             $tagged_post->save();
                         }
                     }
                 }
                 // Post Process
                 $now = Carbon::parse($post->created_at);
                 $feeling = DB::table('feelings')->find($post->feeling)->name;
                 $following = Following::whereFollowerId(Auth::user()->id)->whereFollowingId($post->user_id)->get()->count();
                 $confession = Confession::whereUserId($post->user_id)->first();
                 if ($confession) {
                     $confess_time = Carbon::parse($confession->created_at);
                     $confess_time = $confess_time->diffInHours();
                     //$confess_time = $confess_time->diffInSeconds();
                     if ($confess_time < 24) {
                         $confession = $confession->confess;
                     } else {
                         $confession = null;
                     }
                 }
                 $user = User::find($post->user_id);
                 $url = Picture::find($user->picture);
                 $url = $url->url;
                 $text = htmlentities($post->post);
                 //9-2-Start
                 $pos = strpos($text, 'watch?v=');
                 if ($pos != 0) {
                     $pos = $pos + 8;
                     $str = substr($text, $pos, 11);
                     # code...
                 } else {
                     $str = null;
                 }
                 //9-2-End
                 /* Eve-26-May-Ehsan */
                 /* !!!Eve-26-May-Ehsan */
                 $text = preg_replace('#((https?|ftp)://(\\S*?\\.\\S*?))([\\s)\\[\\]{},;"\':<]|\\.\\s|$)#i', '<a target="_blank" href="$1">$1</a>', $text);
                 if ($post->type >= 1 && $post->type <= 3) {
                     /*26-11-Ehsan*/
                     $text = preg_replace('/#([a-zA-Z0-9\\x{0980}-\\x{09FF}_])+/u', '<a class="tags">$0</a>', $text);
                     //5-7-Ehsan
                 }
                 $text = nl2br($text);
                 $text = Emojione::shortnameToImage($text);
                 /*Start-26-11-Ehsan*/
                 $name = null;
                 if (($post->type == 1 || $post->type == 3) && !$post->hide_name) {
                     $name = $user->username;
                 }
                 /*End-26-11-Ehsan*/
                 $data = array('id' => $post->id, 'post' => $text, 'user_id' => $post->user_id, 'img' => asset($url), 'confess' => $confession, 'following' => $following, 'type' => $post->type, 'like' => Like::wherePostId($post->id)->get()->count(), 'dislike' => Dislike::wherePostId($post->id)->get()->count(), 'liked' => Like::wherePostId($post->id)->whereUserId(Auth::user()->id)->get()->count(), 'disliked' => Dislike::wherePostId($post->id)->whereUserId(Auth::user()->id)->get()->count(), 'comment' => Comment::wherePostId($post->id)->get()->count(), 'feeling' => $feeling, 'name' => $name, 'vidsrc' => $str, 'ago' => $now->diffForHumans());
                 //
                 return json_encode($data);
             }
         } catch (Exception $e) {
             return $e->getMessage();
         }
     }
     return 'false';
 }