
<?php
class PageFmUsersMigration extends CDController{

  protected $db;

  protected $table;

  protected $password;

  private $user_data_key = 'field_5cd1ded13b283';

  public function __construct() {

    $this->db = new wpdb('root','root','cw_migration','localhost');

    $this->table = 'Suscriptores';

    $this->password = 'ffGCK&9vg9b8RT&y';

    $this->view = 'fm/page/users-migration.twig';
  }

  public function get_context(){

    $this->data = CDTimber::get_context();

    // for ($i=0; $i <20 ; $i++) {
    //   $this->data['imports'][] =  $this->ImportSubscriber();
    // }

    $this->data['suscribers_total'] = $this->getSubscribersQuantity();

    $this->data['suscribers_fine'] = $this->getSubscribersWithOutProblems();

    $this->data['suscribers_problem'] = $this->getSubscribersWithProblems();
  }

  private function getSubscribersQuantity(){
    return $this->db->get_var(
      "SELECT COUNT(Id) FROM $this->table"
    );
  }

  private function getSubscribersWithProblems(){
    return $this->db->get_results(
      "SELECT email,Id,Nombre,Apellido FROM $this->table WHERE migracion_id = -1"
    );
  }

  private function getSubscribersWithOutProblems(){
    return $this->db->get_var(
      "SELECT COUNT(Id) FROM $this->table WHERE migracion_id != -1 AND migracion_id != 0"
    );
  }


  private function getNextToImport(){
    $query = "SELECT * FROM $this->table WHERE migracion_id = 0 ORDER BY Id ASC LIMIT 1";

    $results = $this->db->get_results($query);

    if($results){
     return $results[0];
    }else{
      return false;
    }

  }

  private function ImportSubscriber(){
    $next = $this->getNextToImport();
    if(isset($next->Id) ){
      if($next->implicado == 'si'){
        $participation = true;
      }else{
        $participation = false;
      }

      $old_id = $next->Id;

      $user_data = array(
        'first_name' => $next->Nombre,
        'last_name' => $next->Apellido,
        'profession' => $next->Profesion,
        'company' => $next->Empresa,
        'position' => $next->Cargo,
        'phone' => $next->Telefono,
        'email' => $next->Email,
        'address' => $next->Domicilio,
        'postal_code' => $next->CPostal,
        'city' => $next->Ciudad,
        'region' => $next->Provincia,
        'country' => false,
        'is_working_address' => false,
        'company_area' => false,
        'company_area_other' => false,
        'main_product' => $next->main,
        'employees_quantity' => $next->empleados,
        'company_participation' => $participation,
      );

      return $this->createUser($user_data, $old_id);

    }else{
      return false;
    }


  }


  private function createUser($user_data, $old_id){

    $insert_user_data = array(
      'user_login'  => $user_data['email'],
      'user_email'  => $user_data['email'],
      'user_pass'   => $this->password,
      'role' => 'subscriber'
    );

    $user_id = wp_insert_user( $insert_user_data );


    if( !is_wp_error( $user_id ) ){

      update_field($this->user_data_key, $user_data, 'user_'.$user_id);

      $query = "SELECT * FROM $this->table WHERE Id = $old_id";

      $updated = $this->db->query(
      	$this->db->prepare("UPDATE $this->table SET migracion_id = %d WHERE Id = %d", $user_id, $old_id)
      );

      return $user_id;

    }else{
      $updated = $this->db->query(
      	$this->db->prepare("UPDATE $this->table SET migracion_id = %d WHERE Id = %d", -1, $old_id)
      );
      return $user_id->get_error_message(). 'Email: '.$user_data['email'];
    }
  }

}
