private function _add_edit_shared_func($update = FALSE) { // Get the fields for table. $this->data['fields'] = $this->db->field_data($this->data['table']); $this->data['skip'] = array('Id', 'UpdatedAt', 'CreatedAt', 'Status', 'Order'); $this->data['relations'] = array(); // Detect Enums. foreach ($this->data['fields'] as $key => $row) { // Deal with look ups. if (isset($this->data['bucket']['CMS_BucketsLookUps'][$row->name])) { $this->_do_looksup($this->data['bucket']['CMS_BucketsLookUps'][$row->name], $row, $key); } // Deal with Enums if ($row->type == 'enum' && $row->name != $this->data['table'] . 'Status') { $sql = "SHOW COLUMNS FROM " . $this->data['table'] . " WHERE Field = '" . $row->name . "'"; $d = $this->db->query($sql)->row_array(); $e = $d['Type']; $e = str_ireplace('enum(', '', $e); $e = str_ireplace(')', '', $e); $e = str_ireplace("'", '', $e); $e = explode(',', $e); $this->data['fields'][$key]->enums = array(); foreach ($e as $key2 => $row2) { $this->data['fields'][$key]->enums[$row2] = $row2; } } } // Manage bucket relations. $relations = $this->data['bucket']['CMS_BucketsRelations']; if (!empty($relations)) { $this->data['relations'] = json_decode($relations, TRUE); foreach ($this->data['relations'] as $key => $row) { // Get options to relationship. $this->data['relations'][$key]['options'] = array(); $this->db->order_by($row['table'] . 'Title'); $o = $this->db->get($row['table'])->result_array(); foreach ($o as $key2 => $row2) { $this->data['relations'][$key]['options'][$row2[$row['table'] . 'Id']] = $row2[$row['table'] . 'Title']; $this->data['relations'][$key]['tags'][] = $row2[$row['table'] . 'Title']; } // Get selected $this->data['relations'][$key]['selected'] = array(); if ($this->data['type'] == 'edit') { $this->load->model('cms_relations_model'); $this->cms_relations_model->set_bucket($this->data['bucket']['CMS_BucketsName']); $this->cms_relations_model->set_table($row['table']); $this->cms_relations_model->set_entry($this->uri->segment(4)); $d = $this->cms_relations_model->get(); foreach ($d as $key2 => $row2) { $this->data['relations'][$key]['selected'][] = $row2['CMS_RelationsTableId']; } } } } // Manage posted data. if ($this->input->post('submit')) { $this->load->library('form_validation'); // Set validation foreach ($this->data['fields'] as $key => $row) { if (in_array(str_ireplace($this->data['table'], '', $row->name), $this->data['skip'])) { continue; } if (isset($_POST[$row->name])) { $q[$row->name] = $this->input->post($row->name); } if ($row->name == $this->data['table'] . 'Title') { $this->form_validation->set_rules($row->name, str_ireplace($this->data['table'], '', $row->name), 'trim|required'); } else { $this->form_validation->set_rules($row->name, str_ireplace($this->data['table'], '', $row->name), 'trim'); } } // Set validation on any relations. foreach ($this->data['relations'] as $key => $row) { $this->form_validation->set_rules($row['table'], $row['name'], ''); } $this->form_validation->set_rules($this->data['table'] . 'Status', 'Status', 'trim|required'); // Deal with any date options. if ($this->input->post('dates') && is_array($_POST['dates'])) { foreach ($_POST['dates'] as $key => $row) { if (!isset($_POST[$row])) { continue; } $q[$row] = date('Y-m-d', strtotime($_POST[$row])); } } // Deal with any datetimes options. if ($this->input->post('datetimes') && is_array($_POST['datetimes'])) { foreach ($_POST['datetimes'] as $key => $row) { if (!isset($_POST[$row])) { continue; } $q[$row] = date('Y-m-d G:i:s', strtotime($_POST[$row])); } } // Deal with any pre validation formatting. $q = $this->_do_pre_validation_formatting($q); // Validate the post. if ($this->form_validation->run() != FALSE) { $q[$this->data['table'] . 'Status'] = $this->input->post($this->data['table'] . 'Status'); // Deal with an extra col. Make it Json. if (isset($q[$this->data['table'] . 'Extra'])) { $q[$this->data['table'] . 'Extra'] = json_encode($q[$this->data['table'] . 'Extra']); } if ($update) { $this->db->where($this->data['table'] . 'Id', $this->uri->segment(4)); $this->db->update($this->data['table'], $q); $this->_do_relation($this->uri->segment(4)); $this->_do_tags($this->uri->segment(4)); // Fire after event. CMS\Libraries\Event::fire('after.update', array($this->data['table'], $this->uri->segment(4), 'data' => $q)); } else { $this->db->select_max($this->data['table'] . 'Order', 'max'); $m = $this->db->get($this->data['table'])->result_array(); $q[$this->data['table'] . 'Order'] = isset($m[0]['max']) ? $m[0]['max'] + 1 : 0; $q[$this->data['table'] . 'CreatedAt'] = date('Y-m-d G:i:s'); $q[$this->data['table'] . 'UpdatedAt'] = date('Y-m-d G:i:s'); // Hook just before insert. if (isset($this->data['cms']['cp_hooks']['bucket_before_insert'])) { if (!empty($this->data['cms']['cp_hooks']['bucket_before_insert']['library'])) { $this->load->library($this->data['cms']['cp_hooks']['bucket_before_insert']['library']); $q = $this->{strtolower($this->data['cms']['cp_hooks']['bucket_before_insert']['library'])}->{$this->data['cms']['cp_hooks']['bucket_before_insert']['method']}($this->data['table'], $q); } } $this->db->insert($this->data['table'], $q); $id = $this->db->insert_id(); $this->_do_relation($id); $this->_do_tags($id); // Fire after event. CMS\Libraries\Event::fire('after.insert', array($this->data['table'], $id, 'data' => $q)); } // See if this is an ajax request. if ($this->input->is_ajax_request()) { if ($update) { $this->output->set_content_type('application/json')->set_output(json_encode(array('Id' => $this->uri->segment(4)))); return true; } else { $this->output->set_content_type('application/json')->set_output(json_encode(array('Id' => $id))); return true; } } // Where do we redirect? if ($this->input->post('redirect_url')) { if ($this->input->post('btn') == 'save_add') { redirect($this->input->post('redirect_url')); } else { if ($this->input->post('btn') == 'save_continue') { redirect($this->input->post('redirect_url')); } else { redirect($this->data['cms']['cp_base'] . '/buckets/listview/' . $this->uri->segment(3)); } } } else { redirect($this->data['cms']['cp_base'] . '/buckets/listview/' . $this->uri->segment(3)); } } } if ($this->input->is_ajax_request()) { $this->load->view('cms/buckets/add-edit', $this->data); } else { $this->load->view('cms/templates/app-header', $this->data); $this->load->view('cms/buckets/add-edit', $this->data); $this->load->view('cms/templates/app-footer', $this->data); } }
function update($data, $id) { $data = $this->_format_post($data); $q = $this->_set_data($data); $this->db->where($this->table . 'Id', $id); $this->db->update($this->table, $q); // Fire after event. CMS\Libraries\Event::fire('after.update', array($this->table, $id, 'data' => $data)); return 1; }