/** * 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)); $doc_details = Input::all(); $rules = array('title' => 'required'); $validation = Validator::make($doc_details, $rules); if ($validation->fails()) { die($validation); return Redirect::to('dashboard/docs')->withInput()->withErrors($validation); } 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(); return Response::json($this->growlMessage('Document created successfully', 'success')); } catch (Exception $e) { return Response::json($this->growlMessage($e->getMessage(), 'error')); } }
/** * 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')); } }
public function saveDocumentEdits($documentId) { if (!Auth::check()) { return Redirect::to('documents')->with('error', 'You must be logged in'); } $content = Input::get('content'); $contentId = Input::get('content_id'); if (empty($content)) { return Redirect::to('documents')->with('error', "You must provide content to save"); } if (!empty($contentId)) { $docContent = DocContent::find($contentId); } else { $docContent = new DocContent(); } if (!$docContent instanceof DocContent) { return Redirect::to('documents')->with('error', 'Could not locate document to save'); } $document = Doc::find($documentId); if (!$document instanceof Doc) { return Redirect::to('documents')->with('error', "Could not locate the document"); } if (!$document->canUserEdit(Auth::user())) { return Redirect::to('documents')->with('error', 'You are not authorized to save that document.'); } $docContent->doc_id = $documentId; $docContent->content = $content; try { DB::transaction(function () use($docContent, $content, $documentId, $document) { $docContent->save(); }); } catch (\Exception $e) { return Redirect::to('documents')->with('error', "There was an error saving the document: {$e->getMessage()}"); } //Fire document edited event for admin notifications $doc = Doc::find($docContent->doc_id); Event::fire(MadisonEvent::DOC_EDITED, $doc); try { $document->indexContent($docContent); } catch (\Exception $e) { return Redirect::to('documents')->with('error', "Document saved, but there was an error with Elasticsearch: {$e->getMessage()}"); } return Redirect::to('documents')->with('success_message', 'Document Saved Successfully'); }
/** * Recursive function to save children of a given node as DocContent items. */ public function saveChildren($node, $parent_id, $child_priority) { if (!isset($parent_id) || $parent_id == 0) { throw new Exception("Error saving content."); } //Check the node is in the document structure elements @($valid = in_array($node->tagName, $this->structure)); if (!$valid) { //If the node has no children, return if (!$node->hasChildNodes()) { return; } else { //Otherwise save the children, passing on the parent id ( this isn't a valid parent ) $c = 0; foreach ($node->childNodes as $child) { $this->saveChildren($child, $parent_id, $c++); } return; } } //Save this item $contentItem = new DocContent(); $contentItem->doc_id = $this->bill->id; $contentItem->content = $this->getNodeContent($node); $contentItem->child_priority = $child_priority; $contentItem->parent_id = $parent_id; $contentItem->save(); if ($node->childNodes->length == 0) { return; } $c = 0; foreach ($node->childNodes as $child) { $this->saveChildren($child, $contentItem->id, $c++); } }
/** * PUT route for saving documents. */ public function putDocs($id = '') { $user = Auth::user(); if (!$user->can('admin_manage_documents')) { return Redirect::to('/dashboard')->with('message', "You do not have permission"); } $content = Input::get('content'); $content_id = Input::get('content_id'); if ($content_id) { try { $doc_content = DocContent::find($content_id); } catch (Exception $e) { return Redirect::to('dashboard/docs/' . $id)->with('error', 'Error saving the document: ' . $e->getMessage()); } } else { $doc_content = new DocContent(); } $doc_content->doc_id = $id; $doc_content->content = $content; $doc_content->save(); Event::fire(MadisonEvent::DOC_EDITED, $doc); $doc = Doc::find($id); $doc->indexContent($doc_content); return Redirect::to('dashboard/docs/' . $id)->with('success_message', 'Document Saved Successfully'); }
public static function createEmptyDocument(array $params) { $defaults = array('content' => "New Document Content", 'sponsor' => null, 'sponsorType' => null); $params = array_replace_recursive($defaults, $params); if (is_null($params['sponsor'])) { throw new \Exception("Sponsor Param Required"); } $document = new Doc(); DB::transaction(function () use($document, $params) { $document->title = $params['title']; $document->save(); switch ($params['sponsorType']) { case static::SPONSOR_TYPE_INDIVIDUAL: $document->userSponsor()->sync(array($params['sponsor'])); break; case static::SPONSOR_TYPE_GROUP: $document->groupSponsor()->sync(array($params['sponsor'])); break; default: throw new \Exception("Invalid Sponsor Type"); } $template = new DocContent(); $template->doc_id = $document->id; $template->content = "New Document Content"; $template->save(); $document->init_section = $template->id; $document->save(); }); Event::fire(MadisonEvent::NEW_DOCUMENT, $document); return $document; }