Ejemplo n.º 1
0
 /**
  * 	Post route for creating / updating documents.
  */
 public function postDocs()
 {
     $user = Auth::user();
     if (!$user->can('admin_manage_documents')) {
         return Response::json($this->growlMessage("You do not have permission", 'error'));
     }
     //Creating new document
     $title = Input::get('title');
     $slug = str_replace(array(' ', '.'), array('-', ''), strtolower($title));
     // If the slug is taken
     if (Doc::where('slug', $slug)->count()) {
         $counter = 0;
         $tooMany = 10;
         do {
             if ($counter > $tooMany) {
                 return Response::json($this->growlMessage('Can\'t create document with that name, please try another.', 'error'));
             }
             $counter++;
             $new_slug = $slug . '-' . str_random(8);
         } while (Doc::where('slug', $new_slug)->count());
         $slug = $new_slug;
     }
     $doc_details = Input::all();
     $rules = array('title' => 'required');
     $validation = Validator::make($doc_details, $rules);
     if ($validation->fails()) {
         return Response::json($this->growlMessage('A valid title is required.', 'error'));
     }
     try {
         $doc = new Doc();
         $doc->title = $title;
         $doc->slug = $slug;
         $doc->save();
         $doc->sponsor()->sync(array($user->id));
         $starter = new DocContent();
         $starter->doc_id = $doc->id;
         $starter->content = "New Doc Content";
         $starter->save();
         $doc->init_section = $starter->id;
         $doc->save();
         $response = $this->growlMessage('Document created successfully', 'success');
         $response['doc'] = $doc->toArray();
         return Response::json($response);
     } catch (Exception $e) {
         return Response::json($this->growlMessage($e->getMessage(), 'error'));
     }
 }