public function post() { if (Input::has('api_key') && Input::has('content')) { $api_key = Input::get('api_key'); $content = Input::get('content'); $settings = Settings::where('api_key', '=', $api_key)->first(); $user_id = $settings->user_id; $default_networks = json_decode($settings->default_networks, true); $schedule = Carbon::now(); if (Input::has('queue')) { $schedule_id = $settings->schedule_id; $interval = Schedule::find($schedule_id); if ($interval->rule == 'add') { $schedule = $current_datetime->modify('+ ' . $interval->period); } else { if ($interval->rule == 'random') { $current_day = date('d'); $from_datetime = Carbon::now(); $to_datetime = $from_datetime->copy()->modify('+ ' . $interval->period); $days_to_add = $from_datetime->diffInDays($to_datetime); $day = mt_rand($current_day, $current_day + $days_to_add); $hour = mt_rand(1, 23); $minute = mt_rand(0, 59); $second = mt_rand(0, 59); //year, month and timezone is null $schedule = Carbon::create(null, null, $day, $hour, $minute, $second, null); } } if (empty($schedule)) { $schedule = $current_datetime->addHours(1); } } if (!empty($default_networks)) { $post = new Post(); $post->user_id = $user_id; $post->content = $content; $post->date_time = $schedule; $post->save(); $post_id = $post->id; foreach ($default_networks as $network_id) { $post_network = new PostNetwork(); $post_network->user_id = $user_id; $post_network->post_id = $post_id; $post_network->network_id = $network_id; $post_network->status = 1; $post_network->save(); } Queue::later($schedule, 'SendPost@fire', array('post_id' => $post_id)); $response_data = array('type' => 'success', 'text' => 'Your post was scheduled! It will be published on ' . $schedule->format('l jS \\o\\f F \\a\\t h:i A')); return $response_data; } } }
public function updatePost() { $user_id = Auth::user()->id; $rules = array('content' => 'required'); $validator = Validator::make(Input::all(), $rules); $post_id = Input::get('id'); if ($validator->fails()) { return Redirect::back()->withErrors($validator); } if (Input::has('network')) { $content = Input::get('content'); $post = Post::where('user_id', '=', $user_id)->where('id', '=', $post_id)->first(); $post->content = $content; $post->save(); $networks = Input::get('network'); $post_networks = PostNetwork::where('post_id', '=', $post_id)->lists('network_id'); foreach ($post_networks as $network_id) { if (!in_array($network_id, $networks)) { $post_network = PostNetwork::where('network_id', '=', $network_id)->where('post_id', '=', $post_id)->first(); if (!empty($post_network)) { $post_network->status = 0; $post_network->save(); } } } foreach ($networks as $network_id) { $post_network = PostNetwork::where('network_id', '=', $network_id)->where('post_id', '=', $post_id)->first(); if (!empty($post_network)) { $post_network->status = 1; $post_network->save(); } else { $post_network = new PostNetwork(); $post_network->user_id = $user_id; $post_network->post_id = $post_id; $post_network->network_id = $network_id; $post_network->status = 1; $post_network->save(); } } } $response_data = array('type' => 'success', 'text' => 'Post was updated!', 'post' => array('id' => $post_id, 'title' => substr($content, 0, 24), 'content' => $content)); return $response_data; }