/**
  * @api            {post} /tasks/:slug/submissions Create A Submission
  * @apiGroup       Task Submissions
  * @apiDescription Add a submission.
  * @apiUse         RequiresAuthentication
  *
  * @param Request $request
  *
  * @param Task    $task
  *
  * @return Response
  * @throws Exception
  */
 public function store(Request $request, Task $task)
 {
     $this->requireAuthentication();
     $this->validate($request, ['private' => 'boolean', 'title' => 'required_without:private|required_if:private,0', 'cost' => 'between:1,5', 'time' => 'between:1,5', 'rating' => 'between:1,5']);
     // TODO: Prevent duplicate submissions by the same user
     $submission = new Submission(['taskId' => $task->id, 'userId' => $this->user->id, 'cost' => $request->has('cost') ? $request->input('cost') : null, 'time' => $request->has('time') ? $request->input('time') : null, 'rating' => $request->has('rating') ? $request->input('rating') : null]);
     // Validate the requirements for the task are given
     switch ($task->submissionType) {
         case Task::SUBMISSION_TEXT:
             $this->validate($request, ['text' => 'required']);
             $submission->text = $request->input('text');
             break;
         case Task::SUBMISSION_IMAGE:
             $this->validate($request, ['imageIds' => 'required']);
             break;
         default:
             throw new Exception("Task has an invalid submission type ({$task->submissionType}).");
     }
     /** @var Image[] $images */
     $images = [];
     // Validate any images given
     if ($request->has('imageIds')) {
         $imageIds = $request->input('imageIds');
         $images = $this->validateImages($imageIds);
     }
     if (!($success = $submission->save())) {
         throw new Exception("Unable to save submission.");
     }
     /** @var Submission $submission */
     $submission = $submission->fresh();
     $responseData = ['success' => $success, 'submission' => $submission];
     if (!$request->input('private')) {
         // Make a post for the submission
         $post = $submission->toPost();
         $post->title = $request->input('title');
         $post->text = $request->input('text');
         if (!empty($images)) {
             $post->imageId = $images[0]->id;
             $images[0]->ensureHasThumbnail();
         }
         $post->save();
         $responseData['post'] = $post->fresh();
     }
     foreach ($images as $i => $image) {
         $image->submissionId = $submission->id;
         if (!empty($responseData['post'])) {
             $image->postId = $responseData['post']->id;
         }
         $image->save();
     }
     event(new NewSubmission($submission));
     return $this->response($responseData);
 }
 /**
  * @api            {put} /submissions/:submissionId Update A Submission
  * @apiGroup       Task Submissions
  * @apiDescription Update a submission.
  * @apiParam {bool} approved (Admin use only)
  * @apiUse         RequiresAuthentication
  *
  * @param Request    $request
  * @param Submission $submission
  *
  * @return \Illuminate\Http\JsonResponse
  */
 public function update(Request $request, Submission $submission)
 {
     $this->requireRole(Role::ROLE_MANAGE_SUBMISSIONS);
     $this->validate($request, ['approved' => 'boolean', 'rejectReason' => 'required_if:approved,0']);
     if ($request->has('approved')) {
         if ($request->input('approved')) {
             $submission->approve($this->user);
         } else {
             $submission->reject($request->input('rejectReason'), $this->user);
         }
     }
     return $this->response(['submission' => $submission->fresh()]);
 }
 /**
  * @return Task[][]
  */
 public function getTaskProgress()
 {
     /** @var Task[][] $tasks */
     $progress = ['completed' => [], 'pending' => [], 'rejected' => []];
     // Get all the tasks for the sticker
     $tasks = $this->sticker->tasks;
     foreach ($tasks as $task) {
         /** @var Task $task */
         $submissions = Submission::where('userId', $this->user->id)->where('taskId', $task->id)->orderBy('approved', 'DESC')->get();
         foreach ($submissions as $submission) {
             /** @var Submission $submission */
             if ($submission->isApproved()) {
                 $progress['completed'][] = $task;
                 break;
             } elseif (!$submission->isRejected()) {
                 $progress['pending'][] = $task;
                 break;
             } elseif ($submission->isRejected()) {
                 $progress['rejected'][] = $task;
                 break;
             }
         }
     }
     return $progress;
 }
 /**
  * @api            {get} /posts Get Latest Submission Posts
  * @apiGroup       Task Posts
  * @apiDescription Returns a number of featured posts to display on the home page.
  *
  * TODO: Route should be cached. (By CloudFlare preferably)
  *
  * @return Response
  */
 public function index()
 {
     $submissions = Submission::approved()->orderBy('approvedAt')->take(10)->with(['post.task'])->get();
     // Extract the posts from the submissions
     $posts = [];
     foreach ($submissions as $submission) {
         $posts[] = $submission->post;
     }
     return $this->response(['posts' => $posts]);
 }
 public function getIndex()
 {
     $pendingSubmissions = Submission::notApproved()->count();
     $ordersNeedPrinting = Order::whereNull('printedAt')->count();
     $ordersNeedShipping = Order::whereNull('shippedAt')->count();
     $newMembers = User::where('createdAt', '>=', date('Y-m-d 00:00:00'))->count();
     $newPosts = Post::where('createdAt', '>=', date('Y-m-d 00:00:00'))->count();
     $newPosts += Comment::where('createdAt', '>=', date('Y-m-d 00:00:00'))->count();
     return $this->view('admin::dashboard', ['pendingSubmissions' => $pendingSubmissions, 'ordersNeedPrinting' => $ordersNeedPrinting, 'ordersNeedShipping' => $ordersNeedShipping, 'newMembers' => $newMembers, 'newPosts' => $newPosts]);
 }
 /**
  * Define your route model bindings, pattern filters, etc.
  *
  * @param  \Illuminate\Routing\Router $router
  *
  * @return void
  */
 public function boot(Router $router)
 {
     /**
      * Route model binding
      */
     $router->bind('category', function ($slug) {
         return app('CategoryRepository')->findBySlugOrFail($slug);
     });
     $router->bind('user', function ($username) {
         return app('UserRepository')->findBySlugOrFail($username);
     });
     $router->bind('sticker', function ($slug) {
         return app('StickerRepository')->findBySlugOrFail($slug);
     });
     $router->bind('task', function ($slug) {
         return app('TaskRepository')->findBySlugOrFail($slug);
     });
     $router->bind('submission', function ($id) {
         return \Stickable\Models\Submission::findOrFail($id);
     });
     $router->bind('post', function ($id) {
         return \Stickable\Models\Post::findOrFail($id);
     });
     $router->bind('comment', function ($id) {
         return \Stickable\Models\Comment::findOrFail($id);
     });
     $router->bind('notification', function ($id) {
         return \Stickable\Models\Notification::findOrFail($id);
     });
     $router->bind('event', function ($id) {
         return \Stickable\Models\EventLog::findOrFail($id);
     });
     $router->bind('todo', function ($id) {
         return \Stickable\Models\ToDo::findOrFail($id);
     });
     parent::boot($router);
 }
 /**
  * @return null|string
  */
 public function getImage()
 {
     if ($this->fromUserId) {
         $user = app('UserRepository')->find($this->fromUserId);
         if (!$user) {
             return null;
         }
         if ($image = $user->getImage()) {
             return $image->getThumbUrl();
         } else {
             return Config::get('app.default_user_picture');
         }
     }
     if ($this->submissionId) {
         if ($submission = Submission::find($this->submissionId)) {
             /** @var Submission $submission */
             if ($image = $submission->images()->first()) {
                 /** @var Image $image */
                 return $image->thumb ? $image->getThumbUrl() : $image->getUrl();
             }
         }
     }
     if ($this->stickerId) {
         $sticker = app('StickerRepository')->find($this->stickerId);
         return $sticker->getDesign()->getImageUrl();
     }
     return null;
 }
 protected function getSearchableList()
 {
     return Submission::query();
 }