/** * Generic create() action. * The trick here is that $this->calling_class and $this->calling_method will hold a string * reference for which extended class called this create() method. We need that in order to * get the proper records and access. */ public function create() { // get the "_type" ... page_type, user_type, or block_type $model = Inflector::classify(Inflector::singularize($this->request->params['controller'])); $modelClass = 'minerva\models\\'.$model; $x_type = strtolower($model) . '_type'; // or set it to "all" if there wasn't a param passed $type = ((isset($this->request->params[$x_type])) && (in_array($x_type, $this->library_fields))) ? $this->request->params[$x_type]:'all'; // this just checks access $this->getDocument(array('action' => $this->calling_method, 'request' => $this->request, 'find_type' => false)); // Get the model class we should be using for this (it could be an extended class from a library) $modelClass = $modelClass::getMinervaModel($model, $type); // Get the name for the page, so if another type library uses the "admin" (core) templates for this action, it will be shown $display_name = $modelClass::display_name(); // Lock the schema. We don't want any unwanted data passed to be saved $modelClass::meta('locked', true); // Get the fields so the view template can iterate through them and build the form $fields = $modelClass::schema(); // Don't need to have these fields in the form unset($fields['_id']); // If a page type was passed in the params, we'll need it to save to the page document. $fields[$x_type]['form']['value'] = ($type != 'all') ? $type:null; // If data was passed, set some more data and save if ($this->request->data) { $document = $modelClass::create(); $now = date('Y-m-d h:i:s'); $this->request->data['created'] = $now; $this->request->data['modified'] = $now; $this->request->data['url'] = Util::unique_url(array( 'url' => Inflector::slug($this->request->data['title']), 'model' => $modelClass )); $user = Auth::check('minerva_user'); if($user) { $this->request->data['owner_id'] = $user['_id']; } else { // TODO: possible for anonymous users to create things? do we need to put in any value here? $this->request->data['owner_id'] = ''; } // (note: this will only be useful for UsersController) if(($this->request->params['controller'] == 'users') && (isset($this->request->data['password']))) { $this->request->data['password'] = String::hash($this->request->data['password']); } // Save if($document->save($this->request->data)) { FlashMessage::set('The content has been created successfully.', array('options' => array('type' => 'success', 'pnotify_title' => 'Success', 'pnotify_opacity' => .8))); $this->redirect(array('controller' => $this->request->params['controller'], 'action' => 'index')); } else { FlashMessage::set('The content could not be saved, please try again.', array('options' => array('type' => 'error', 'pnotify_title' => 'Error', 'pnotify_opacity' => .8))); } } if(empty($document)) { $document = $modelClass::create(); // Create an empty page object } $this->set(compact('document', 'fields', 'display_name')); }