/** * Save a new template to database. * * @return Builder\Themes\ThemesModel */ public function store() { if (!$this->app['sentry']->getUser()->hasAccess('templates.create')) { return new Response($this->app['translator']->trans('noPermTemplateCreate'), 403); } if (!$this->input->get('name')) { return new Response($this->app['translator']->trans('enterNameForTemplate'), 400); } $exists = $this->model->where('user_id', Sentry::getUser()->id)->where('name', $this->input->get('name'))->first(); if ($exists) { return new Response($this->app['translator']->trans('templateWithNameExists'), 400); } $rand = str_random(10); $this->model->user_id = Sentry::getUser()->id; $this->model->name = $this->input->get('name'); $this->model->color = $this->input->get('color'); $this->model->category = $this->input->get('category'); $this->model->thumbnail = 'assets/images/thumbnails/templates/template-' . $rand . '.png'; if ($this->model->save()) { foreach ($this->input->get('pages') as $k => $page) { $pModel = new \Builder\Projects\PageModel(); foreach ($page as $name => $value) { $pModel->{$name} = is_array($value) ? json_encode($value) : $value; } $this->model->pages()->save($pModel); } $model = $this->model->with('pages')->find($this->model->id); $model->thumbId = $rand; return $model; } }