Hello,
Is there a possibility of duplicate the record? For example, button duplicate on the "More".
Thanks so much.
⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠
Posted 25 January 2017 - 17:28 PM
Hello,
Is there a possibility of duplicate the record? For example, button duplicate on the "More".
Thanks so much.
Posted 01 February 2017 - 15:38 PM
Hi, you can achieve this in the following way (this makes an exact copy of the record, leaving out the id):
In your main function (where you generate the crud):
$crud->add_action('Duplicar Actividad', base_url() . '/assets/img/add.png', site_url() . '/objetivos/duplicar/');
And the function "Duplicar":
function duplicar($primary_key) {//gets the primary key of the row you're duplicating //Query to the database to get the whole row $query = $this->db->get_where('ACTIVIDADES', array('ID_ACTIVIDAD' => $primary_key)); if ($query->num_rows() > 0) { $res = $query->result(); $row = $res[0]; } //puts the data on an array $data = array( 'ID_OBJETIVO_OPERATIVO' => $row->ID_OBJETIVO_OPERATIVO, 'NOMBRE_ACTIVIDAD' => $row->NOMBRE_ACTIVIDAD, 'DESCRIPCION_ACTIVIDAD' => $row->DESCRIPCION_ACTIVIDAD, 'FECHA_INICIO' => $row->FECHA_INICIO, 'FECHA_FIN' => $row->FECHA_FIN, 'TIPO_INDICADOR' => $row->TIPO_INDICADOR, 'DESCRIPCION_INDICADOR' => $row->DESCRIPCION_INDICADOR, 'FECHA_META' => $row->FECHA_META, 'VALOR_META' => $row->VALOR_META, 'PORCENTAJE_META' => $row->PORCENTAJE_META, 'META_MONETARIA' => $row->META_MONETARIA, 'MOTIVO_REPROGRAMACION' => $row->MOTIVO_REPROGRAMACION, 'USUARIO_CREACION' => $row->USUARIO_CREACION, ); //insert the data as a new record $this->db->insert('ACTIVIDADES', $data); //redirects to the page you were header('location:' . site_url('/objetivos/act_obj/' . $row->ID_OBJETIVO_OPERATIVO)); exit; }
Hope it helps :D
Posted 02 February 2017 - 08:32 AM
Well there is a way - but then u need to do some modifications in your library
Add The following function
protected function copy_row($state_info) { $fields = $this->get_add_fields(); $types = $this->get_field_types(); $field_values = $this->get_edit_values($state_info->primary_key); foreach($fields as $field) { $fieldName = $field->field_name; if(property_exists($field_values,$fieldName)) { if($types[$fieldName]->crud_type == 'upload_file') { //Make a copy of the same and then assign the new value to the same $oldFileName = $field_values->{$fieldName}; $oldFileName = substr($oldFileName, 6); $newFileName = substr(uniqid(),-5).'-'. $oldFileName; $sourcePath = $types[$fieldName]->extras->upload_path . DIRECTORY_SEPARATOR . $field_values->{$fieldName}; $newPath = $types[$fieldName]->extras->upload_path . DIRECTORY_SEPARATOR . $newFileName; if(is_file($sourcePath)) { copy($sourcePath, $newPath); $this->getModel()->set_add_value($field->field_name, $newFileName); } } else { $this->getModel()->set_add_value($field->field_name, $field_values->{$fieldName}); } } } }
In the following function .. u need to append the following code
public function render() { ...... ...... case 19://copy if($this->unset_add) { throw new Exception('You don\'t have permissions for this operation', 14); die(); } if($this->theme === null) $this->set_theme($this->default_theme); $this->setThemeBasics(); $this->set_basic_Layout(); //Set the default values $state_info = $this->getStateInfo(); $this->copy_row($state_info); $this->showAddForm(); break; }
Need to update the states array in following
class grocery_CRUD_States extends grocery_CRUD_Layout { protected $states = array( .... .... 19 => 'copy' );
Append the following code in the following function ..
public function getStateInfo() { ... ... case 19: // read if($first_parameter !== null) { $state_info = (object)array('primary_key' => $first_parameter); } else { throw new Exception('On the state "copy" the Primary key cannot be null', 6); die(); } break; }
In the following function - u need to change
protected function showAddForm() { ..... $data->buttons = $this->form_buttons; /* NEW */ $data->field_values = $this->get_add_values(null); ..... $data->validation_url = $this->getValidationInsertUrl(); //$data->input_fields = $this->get_add_input_fields(); /* NEW */ $data->input_fields = $this->get_add_input_fields($data->field_values);
Replace the function get_add_input_fields with the following
protected function get_add_input_fields($field_values = null) { $fields = $this->get_add_fields(); $types = $this->get_field_types(); $input_fields = array(); foreach($fields as $field_num => $field) { $field_info = $types[$field->field_name]; $field_value = !empty($field_values) && isset($field_values->{$field->field_name}) ? $field_values->{$field->field_name} : null; //Code Changes by Amit Shah - for default field value /* $field_default_values=$this->get_field_default_values($field->field_name); if (! empty($field_default_values) and empty($field_value) ){ //if $field_info is not set at this point we need to construct one if (!isset($field_info)){ $field_info = (object)array(); } $field_info->custom_default=True; //this is what we look for to ensure we only change the behaviour we want to $field_info->crud_type=$field_default_values['type']; $field_info->type=$field_default_values['type']; $field_info->default=$field_default_values['value']; $field_info->extras=$field_default_values['value']; $field_value=$field_default_values['value']; } */ if(!isset($this->callback_add_field[$field->field_name])) { $field_input = $this->get_field_input($field_info, $field_value); } else { $field_input = $field_info; $field_input->input = call_user_func($this->callback_add_field[$field->field_name], $field_value, null, $field_info); //make our little change //allow default field rendering behaviour if user returns false //in the callback if ($field_input->input === False) { $field_input = $this->get_field_input($field_info, $field_value); } } switch ($field_info->crud_type) { case 'invisible': unset($this->add_fields[$field_num]); unset($fields[$field_num]); continue; break; case 'hidden': $this->add_hidden_fields[] = $field_input; unset($this->add_fields[$field_num]); unset($fields[$field_num]); continue; break; } $input_fields[$field->field_name] = $field_input; } return $input_fields; }
This changes should help you incorporate a copy function ...
so like we have edit function working with edit/<pk> .... we can have the same with copy/<pk>
or u can say - add a action that calls to the <app_path>/<controller>/<function>/copy
that will anyways add the pk to the link...
In case u stuck somewhere - do let me know so i can update it.. i have used this piece of code nearly a year ago... as it was a requirement from client. But post that i dont remember using it anywhere. But it surely should work for you.
Happy GCing :)