<?php

/**
 * Populates the DTT_name and DTT_description fields which were added in this version.
 * The name is set to just be a string representation of the start date (in UTC time for now), and the description is
 * just left blank
 */

class EE_DMS_4_2_0_datetime_fields extends EE_Data_Migration_Script_Stage_Table
{
    public function __construct()
    {
        global $wpdb;
        $this->_pretty_name = esc_html__("Datetime Fields", "event_espresso");
        $this->_old_table   = $wpdb->prefix . "esp_datetime";
        parent::__construct();
    }


    protected function _migrate_old_row($old_row)
    {
        // foreach question_group_question entry with this QST_ID, we want to set its
        // QSG_order equal to this question's QST_order
        global $wpdb;

        $updated = $wpdb->update(
            $this->_old_table,
            [
                                 'DTT_name' => '',
                                 'DTT_description' => '',
                             ],
            [
                'DTT_ID' => $old_row['DTT_ID'],
            ],
            [
                '%s',// DTT_name,
                '%s',// DTT_description
            ],
            [
                '%d',// DTT_ID
            ]
        );
        if (false === $updated) {
            $this->add_error(
                sprintf(
                    esc_html__(
                        "Error in updating table %s setting DTT_name = '' and DTT_description = '' where DTT_ID = %d",
                        'event_espresso'
                    ),
                    $this->_old_table,
                    $old_row['QST_ID']
                )
            );
        }
        // nothing to map really
    }
}
