public function view() { $body_type = BodyTypeModel::factory()->getById($this->body_type_id); $cars = CarModel::factory()->getByBodyTypeId($this->body_type_id); $this->set('body_type', $body_type); $this->set('cars', $cars); }
public function edit($id = null, $parent_id = null, $duplicate_id = null) { //2nd arg is for adding new records only, 3rd arg is for duplicating existing records only $model = CarModel::factory(); //This function serves several purposes: // * Display "add new" form // * Display "edit existing" form // * Process submitted form (validate data and save to db) // //We can determine which action to take based on a combination of // whether or not valid data was POST'ed, and whether or not an $id was provided... if ($_POST) { $error = $model->validate($_POST); $result = $error->has() ? 'error' : 'success'; } else { $result = empty($id) && empty($duplicate_id) ? 'add' : 'edit'; } //form was submitted and data is valid -- save to db and redirect... if ($result == 'success') { $id = $model->save($_POST); $this->flash('Car Saved!'); if (!empty($_POST['save-and-add'])) { $this->redirect('add', $_POST['body_type_id']); } else { if (!empty($_POST['save-and-duplicate'])) { $this->redirect('duplicate', $id); } else { $this->redirect("view?body_type={$_POST['body_type_id']}"); } } //form was submitted with invalid data -- display errors and repopulate form fields with user's submitted data... } else { if ($result == 'error') { $this->set('error', $error); //C5 automagically displays these errors for us in the view //C5 form helpers will automatically repopulate form fields from $_POST data, // but we need to manually repopulate any data that isn't in $_POST, // or data that is used in places other than form fields... $this->set('body_type_id', $this->post('body_type_id', $parent_id)); //for the 'add' form action and the cancel button //Populate the 'colors' checkbox list with user's submitted choices checked $colors = ColorModel::factory()->getAll(); $chosen_color_ids = $this->post('color_ids', array()); foreach ($colors as $key => $color) { $colors[$key]['has'] = in_array($color['id'], $chosen_color_ids); } $this->set('colors', $colors); //form was not submitted, user wants to add a new record -- populate any form fields that should have default values... } else { if ($result == 'add') { $this->set('body_type_id', $parent_id); //for the form action and the cancel button $this->set('colors', ColorModel::factory()->getAll()); //populate the 'colors' checkbox list with nothing checked //form was not submitted, user wants to edit or duplicate an existing record -- populate form fields with db data... } else { if ($result == 'edit') { $retrieve_id = empty($duplicate_id) ? $id : $duplicate_id; $record = $model->getById($retrieve_id); if (!$record) { $this->render404AndExit(); } $this->setArray($record); //sets variables for every field in $record $this->set('colors', ColorModel::factory()->getAllWithCar($retrieve_id)); //populate the 'colors' checkbox list with saved choices checked } } } } //now populate data that is the same regardless of the action taken... $this->set('id', $id); $this->set('body_type_options', BodyTypeModel::factory()->getSelectOptions(array(0 => '<Choose One>'))); $this->set('manufacturer_options', ManufacturerModel::factory()->getSelectOptions(array(0 => '<Choose One>'))); $this->set('currency_symbol', Package::getByHandle('automobiles')->config('currency_symbol')); //finally, display the form with the data we populated above $this->render('edit'); }