/** * Sets top-level bill information. */ public function createDoc() { $this->checkAttr('domdoc'); $this->setBillTitle(); $this->setBillSlug(); $bill = new Doc(); $bill->title = $this->title; $bill->slug = $this->slug; $bill->save(); try { $starter = new DocContent(); $starter->doc_id = $bill->id; $starter->content = $this->getBillMeta('legis-type') . ' ' . $this->getBillMeta('official-title'); $starter->save(); $bill->init_section = $starter->id; $bill->save(); $this->bill = $bill; $rootElement = $this->domdoc->getElementsByTagName($this->rootTag)->item(0); $c = 0; if (!is_object($rootElement)) { throw new Exception("Couldn't retrieve root element"); } foreach ($rootElement->childNodes as $child) { $this->saveChildren($child, $starter->id, $c++); } } catch (Exception $e) { $bill->delete(); $starter->delete(); throw new Exception($e->getMessage()); } return true; }
/** * Creates a new Doc model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Doc(); if ($model->load(Yii::$app->request->post()) && $model->save()) { Yii::$app->getSession()->addFlash('success', "Запись \"{$model->typeName}\" успешно добавлена."); return $this->redirect(Url::previous() != Yii::$app->homeUrl ? Url::previous() : ['view', 'id' => $model->id]); } else { if (Yii::$app->request->referrer != Yii::$app->request->absoluteUrl) { Url::remember(Yii::$app->request->referrer ? Yii::$app->request->referrer : null); } if (!Yii::$app->request->isPost) { $model->setAttributes(Yii::$app->request->get()); } return $this->render('create', ['model' => $model]); } }
/** * Post route for creating / updating documents. */ public function postDocs() { $user = Auth::user(); if (!$user->can('admin_manage_documents')) { // If there's a group if (Input::get('group_id')) { $groupUser = User::with('groups')->whereHas('groups', function ($query) { $query->where('status', 'active'); $query->where('group_id', Input::get('group_id')); })->find($user->id); if (!isset($groupUser->id)) { return Response::json($this->growlMessage("You do not have permission", 'error')); } } elseif (!$user->getSponsorStatus()) { return Response::json($this->growlMessage("You do not have permission", 'error')); } } //Creating new document $title = Input::get('title'); $slug = Input::get('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(); if (Input::get('group_id')) { $doc->groupSponsors()->sync(array(Input::get('group_id'))); } else { $doc->userSponsors()->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, 200); } catch (Exception $e) { return Response::json($this->growlMessage($e->getMessage(), 'error')); } }
public static function createEmptyDocument(array $params) { $defaults = array('content' => "New Document Content", 'sponsor' => null, 'sponsorType' => null, 'publish_state' => 'unpublished'); $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->publish_state = $params['publish_state']; $document->save(); switch ($params['sponsorType']) { case static::SPONSOR_TYPE_INDIVIDUAL: $document->userSponsors()->sync(array($params['sponsor'])); break; case static::SPONSOR_TYPE_GROUP: $document->groupSponsors()->sync(array($params['sponsor'])); break; default: throw new \Exception("Invalid Sponsor Type"); } $template = new DocContent(); $template->doc_id = $document->id; $template->content = $params['content']; $template->save(); $document->init_section = $template->id; $document->save(); }); Event::fire(MadisonEvent::NEW_DOCUMENT, $document); return $document; }