Using DTE Duplicate how do I auto-update some fields like Publish in the new $ID?

Using DTE Duplicate how do I auto-update some fields like Publish in the new $ID?

koniahinkoniahin Posts: 186Questions: 39Answers: 7

Duplicate, in itself, work fine. However when I duplicate the field I need to:

  {
    extend: "selected",
    text: 'Duplicate',
    action: function ( e, dt, node, config ) {
      // Start in edit mode, and then change to create
      editor
      .edit( table.rows( {selected: true} ).indexes(), {
        title: 'Duplicate record',
        buttons: 'Create from existing'
      } )
      .mode( 'create' );
    }
  },

1) Set publish to No
2) With 2 date field I need to update them the first one to now and the second to 30 days ahead. This code gets those values:

$today = date('Y-m-d');
$date_plus_30 = date('Y-m-d', strtotime("+30 days"));

If not using duplicate I do something like:

  {
    label: 'Date begins (this must be updated)',
    name:  'date_begins',
    default: '<?php echo $today; ?>',
    attr: { placeholder: "Format: YYYY-MM-DD" },
  },

  {
    label: 'Date ends (this must be updated)',
    name:  'date_ends',
    default: '<?php echo $date_plus_30; ?>',
    attr: { placeholder: "Format: YYYY-MM-DD" },
  },

However with duplicate those values get overwritten.

Answers

  • colincolin Posts: 15,177Questions: 1Answers: 2,590

    This example from this thread may help, it's showing how to update fields when duplicating. Hope that helps,

    Colin

  • koniahinkoniahin Posts: 186Questions: 39Answers: 7

    I looked at the example but it goes above my head. Let's try this a different way. Should the following code added to the controller file work?

    ->on( 'postCreate', function ( $editor, $id, $values) {
    ->query( 'update', 'classes' )
    ->set( 'classes.publish', 'No' )
    ->where( 'classes.id', $id, '=' )
    ->exec();

    As is, it causes the table to not load with the error:

    DataTables warning: table id=datatable - Ajax error. For more information about this error, please see http://datatables.net/tn/7

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin

    I would do it on the client-side in this case actually:

    {
      extend: "selected",
      text: 'Duplicate',
      action: function ( e, dt, node, config ) {
        var d30 = new Date();
        d30.setDate( d30.getDate() + 30 );
    
        // Start in edit mode, and then change to create
        editor
        .edit( table.rows( {selected: true} ).indexes(), {
          title: 'Duplicate record',
          buttons: 'Create from existing'
        } )
        .mode( 'create' )
        .set('publish', 'No')
        .set('date_begins', new Date())
        .set('date_ends', d30);
      }
    },
    

    Allan

  • koniahinkoniahin Posts: 186Questions: 39Answers: 7

    That is much more like it. The last question would be how do you convert the date format to the format I use, 2021-07-03, yyyy-mm-dd ?

  • colincolin Posts: 15,177Questions: 1Answers: 2,590
    edited July 2021

    There are two datetime options - displayFormat and wireFormat. To change how the field is displayed, tweak the displayFormat.

    Colin

  • koniahinkoniahin Posts: 186Questions: 39Answers: 7

    Neither my contractor nor I could get it to work with the datetime tip. Thinking out of the box I gambled and set the values using PHP

    $today = date('Y-m-d');
    $date_plus_30 = date('Y-m-d', strtotime("+30 days"));
    

    and

    {
      extend: "selected",
      text: 'Duplicate',
      action: function ( e, dt, node, config ) {
        editor
        .edit( table.rows( {selected: true} ).indexes(), {
          title: 'Duplicate record',
          buttons: 'Create from existing'
        } )
        .mode( 'create' )
        .set('publish', 'No')
        .set('date_begins', '<?php echo $today; ?>')
        .set('date_ends', '<?php echo $date_plus_30; ?>')
      }
    },
    

    That worked.

Sign In or Register to comment.