Component

Module

Duplicate Record

Duplicate Record

Directory

File Folder Link
index.php \\SYNAS\Allan\DOCUMENTATION\Component\Duplicate Record\rckuc\wolf\app\views\option
style.css \\SYNAS\Allan\DOCUMENTATION\Component\Duplicate Record\rckuc\wolf\admin\themes\black_and_white
backend.js \\SYNAS\Allan\DOCUMENTATION\Component\Duplicate Record\rckuc\wolf\admin\javascript
OptionController.php \\SYNAS\Allan\DOCUMENTATION\Component\Duplicate Record\rckuc\wolf\app\controllers
Framework.php \\SYNAS\Allan\DOCUMENTATION\Component\Duplicate Record\rckuc\wolf

 

Step 1

Update: index.php

Add duplicate button in action column

<td> <a href="<?php echo get_url('option/editoption/'.$option->id); ?>"><img src="<?php echo URL_PUBLIC ?>wolf/admin/images/icon-edit.gif" alt="edit icon" /></a>  <a href="<?php echo get_url('option/delete/'.$option->id); ?>" class="delete_sweetalert" data-name="<?php echo $option->name; ?>"><img src="<?php echo URL_PUBLIC ?>wolf/admin/images/icon-remove.gif" alt="remove icon" /></a> <!-- add duplicate button in action column -->  <a href="<?php echo get_url('option/duplicate/'.$option->id); ?>" class="duplicate_sweetalert" data-name="<?php echo $option->name; ?>"><img src="<?php echo URL_PUBLIC ?>wolf/admin/images/duplicate.png" alt="duplicate icon" /></a> </td>
<td>
  <a href="<?php echo get_url('option/editoption/'.$option->id); ?>"><img src="<?php echo URL_PUBLIC ?>wolf/admin/images/icon-edit.gif" alt="edit icon" /></a>
   <a href="<?php echo get_url('option/delete/'.$option->id); ?>" class="delete_sweetalert" data-name="<?php echo $option->name; ?>"><img src="<?php echo URL_PUBLIC ?>wolf/admin/images/icon-remove.gif" alt="remove icon" /></a>
  <!-- add duplicate button in action column -->
   <a href="<?php echo get_url('option/duplicate/'.$option->id); ?>" class="duplicate_sweetalert" data-name="<?php echo $option->name; ?>"><img src="<?php echo URL_PUBLIC ?>wolf/admin/images/duplicate.png" alt="duplicate icon" /></a>
</td>

Step 2

Update: style.css

.duplicate_sweetalert img { height: 14px; width: 14px; }
.duplicate_sweetalert img {
  height: 14px;
  width: 14px;
}

Step 3

Update: backend.js

1. Apply Sweetalert pop up box for all index.php table duplicate button.
2. Make sure to add class="duplicate_sweetalert" and add data-name="name ;?>" for duplicate button.
// Apply Sweetalert pop up box for all index.php table duplicate button // Make sure to add class="duplicate_sweetalert" and add data-name="name ;?>" for duplicate button $(document).ready(function() { $(document).on('click', 'a.duplicate_sweetalert', function (e) { e.preventDefault(); // Prevent default link behavior let name = $(this).attr('data-name'); let duplicateLink = $(this).attr('href'); Swal.fire({ html: `Are you sure you want to duplicate:"${name}"?`, icon: 'question', showCancelButton: true, confirmButtonText: 'Yes', cancelButtonText: 'Cancel', confirmButtonColor: 'green', cancelButtonColor: '#3085d6', showCloseButton: true }).then((result) => { if (result.isConfirmed) { window.location.href = duplicateLink; } }); }); });
// Apply Sweetalert pop up box for all index.php table duplicate button
// Make sure to add class="duplicate_sweetalert" and add data-name="name ;?>" for duplicate button 
$(document).ready(function() {
    $(document).on('click', 'a.duplicate_sweetalert', function (e) {
        e.preventDefault(); // Prevent default link behavior
        let name = $(this).attr('data-name');
        let duplicateLink = $(this).attr('href');

        Swal.fire({
            html: `Are you sure you want to duplicate:"${name}"?`,
            icon: 'question',
            showCancelButton: true,
            confirmButtonText: 'Yes',
            cancelButtonText: 'Cancel',
            confirmButtonColor: 'green',
            cancelButtonColor: '#3085d6',
            showCloseButton: true
        }).then((result) => {
            if (result.isConfirmed) {
                window.location.href = duplicateLink;
            }
        });
    });
});

Step 4

Update: Framework.php
Add new funtion inside class Record {}

public function duplicate() { // Clone the current object $new = clone $this; // Reset ID so it's treated as a new record $new->id = null; // Save new record to the database if ($new->save()) { return $new; } return false; }
public function duplicate() {
    // Clone the current object
    $new = clone $this;

    // Reset ID so it's treated as a new record
    $new->id = null;

    // Save new record to the database
    if ($new->save()) {
        return $new;
    }

    return false;
}

Step 5

Update: OptionController.php
Add new function duplicate()

function duplicate($id){ $this->_checkPermission(); $option = Option::findById($id); // Customize based on your own scenario $option->name = '(Copy of) ' . $option->name; $option->code = '(Copy of) ' . $option->code; // Suggest to Update $option->created_by_id = AuthUser::getId(); $option->created_on = date('Y-m-d H:i:s'); $option->updated_by_id = null; $option->updated_on = null; if ($option->duplicate()) { $option_id = $option->lastInsertId(); Flash::set('success', __('This Option has been duplicated.')); redirect(get_url('option/editoption/'.$option_id)); } else { Flash::set('error', __('This Option could not be duplicated!')); redirect(get_url('option')); } }
function duplicate($id){
    $this->_checkPermission();

    $option = Option::findById($id);
    // Customize based on your own scenario
    $option->name = '(Copy of) ' . $option->name;
    $option->code = '(Copy of) ' . $option->code;

    // Suggest to Update
    $option->created_by_id = AuthUser::getId();
    $option->created_on = date('Y-m-d H:i:s');
    $option->updated_by_id = null;
    $option->updated_on = null;
    if ($option->duplicate()) {
        $option_id = $option->lastInsertId();
        Flash::set('success', __('This Option has been duplicated.'));
        redirect(get_url('option/editoption/'.$option_id));
    }
    else {
        Flash::set('error', __('This Option could not be duplicated!'));
        redirect(get_url('option'));
    }
}
Code Copied To Clipboard!