<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Transfere extends CI_Controller
{

    public function _construct()
    {
        parent::__construct();
        $this->load->model('insert_model');
        $this->load->model('user_model');
    }

    public function index()
    {
        $url = 'https://ilink-app.com/mobilebackend/interacted/MembersAction.php';

        $row = 1;
        if (($handle = fopen("https://ilink-app.com/backofficebeta/application/controllers/BASE.csv", "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 1, ",")) !== FALSE ) {
                $num = count($data);
                echo "<p> $num fields in line $row: <br /></p>\n";
                if($row==1) {
                    $webhost        = 'localhost';
                    $webusername    = 'root';
                    $webpassword    = 'vps@2017GA';
                    $webdbname      = 'ilink_beta';
                    $webcon         = mysqli_connect($webhost, $webusername, $webpassword, $webdbname);
                    if (mysqli_connect_errno())
                    {
                        echo 'Failed to connect to MySQL: ' . mysqli_connect_error();
                    }
                    $results = mysqli_query($webcon, "SELECT phone FROM `users` WHERE `country_code`='Gabon' and network='Libertis' and `category`='geolocated'");

                    $mobhost        = 'localhost';
                    $mobusername    = 'root';
                    $mobpassword    = 'vps@2017GA';
                    $mobdbname      = 'iLink_test';
                    $mobcon         = mysqli_connect($mobhost, $mobusername, $mobpassword, $mobdbname);
                    if (mysqli_connect_errno())
                    {
                        echo 'Failed to connect to MySQL: ' . mysqli_connect_error();
                    }
                    if($results) {
                        while ($row = mysqli_fetch_array($results)) {

                            //$user_info = explode(";", $data[$c]);
                            //echo $data[$c] . "<br />\n";
                            foreach ($row as $key => $val) {
                                $row[$key] = mysqli_real_escape_string($mobcon, $row[$key]);
                            }
                            $fields = null;
                            if ($row['country_code'] == 'Cameroun') {

                            } elseif ($row['country_code'] == 'France') {

                            } elseif ($row['country_code'] == 'Gabon') {
                                $fields = array(
                                    "tag" => "member",
                                    "type" => "create_geolocated_user",
                                    "lastname" => addslashes($row['lastname']),
                                    "phone" => $row['phone'],
                                    "phone_transaction" => $row['phone'],
                                    "address" => $row['firstname'],
                                    "lang" => "fr",
                                    "email" => $row['email'],
                                    "password" => "1234",
                                    "town" => ["id" => 1, "country_id" => 78, "name" => "Gabon", "status" => 1],
                                    "network" => ["id" => 14, "country_id" => 78, "name" => "Mobicash"],
                                    "latitude" => $row['latitude'],
                                    "longitude" => $row['longitude'],
                                    "category" => "super",
                                    "member" => "lBkGLqdC7f"
                                );
                            }


                            $fields_string = http_build_query($fields);
                            //url-ify the data for the POST
                            /*foreach ($fields as $key => $value) {
                                $fields_string .= $key . '=' . $value . '&';
                            }
                            rtrim($fields_string, '&');*/

                            $ch = curl_init();


                            curl_setopt($ch, CURLOPT_URL, $url);
                            curl_setopt($ch, CURLOPT_POST, 1);
                            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
                            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));

                            //execute post
                            $result = curl_exec($ch);

                            //close connection
                            curl_close($ch);

                            //var_dump($fields_string);

                            //echo json_encode($fields) . "<br />\n";

                        }
                    }else{
                        echo json_encode($results);
                    }
                }
                $row++;
            }
            fclose($handle);
        }

        for ($c = 6001; $c <= 7873; $c++) {
            $query = $this->db->query("SELECT phone,transactionNumber FROM networks_agents WHERE agent_id=".$c);
            if($query->num_rows()>0){
                echo "N° ".$c." "."<br />\n";
                $update_phone = str_replace('+24100', '+2410', $query->row()->phone);
                $update_transactionNumber = str_replace('+24100', '+2410',$query->row()->transactionNumber);

                $this->db->trans_start();
                $this->db->query("UPDATE networks_agents SET phone='".$update_phone."' WHERE agent_id ='".$c."'");
                $this->db->query("UPDATE networks_agents SET transactionNumber='".$update_transactionNumber."' WHERE agent_id ='".$c."'");
                $this->db->trans_complete();

                if ($this->db->trans_status() === FALSE)
                {
                    echo "Echec ligne n° ".$c;
                }else{
                    echo "Transaction réussite ligne n° ".$c;
                }
                echo "<br />\n";
            }else{
                echo "something wrong";
            }
        }
    }

    public function getDistincQuartier(){

        $row = 1;
        $count = 1;
        if (($handle = fopen("https://ilink-app.com/backofficebeta/application/controllers/BASE_AGENT_TEST.csv", "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE ) {
                $num = count($data);
                //echo "<p> $num fields in line $row: <br /></p>\n";
                if($row>2) {

                    for ($c = 0; $c < $num; $c++) {
                        $user_info = explode(";", $data[$c]);
                        //echo $data[$c] . "<br />\n";
                        if( stristr($user_info[3], "akebe")) {
                            echo "N° ".$count;
                            echo "<br /></p>\n";
                            $count++;

                            $fields = array(
                                "username" => addslashes($user_info[0]),
                                "phone" => "+241" . str_replace(' ', '', $user_info[1]),
                                "phone_transaction" => "+241" . str_replace(' ', '', $user_info[2]),
                                "address" => $user_info[3],
                                "lang" => "fr"
                            );

                            echo json_encode($fields);
                        }

                    }
                }
                $row++;
            }
            fclose($handle);
        }
    }

    public function update_network(){

        $webhost        = 'localhost';
        $webusername    = 'root';
        $webpassword    = 'vps@2017GA';
        $webdbname      = 'ilink_beta';
        $webcon         = mysqli_connect($webhost, $webusername, $webpassword, $webdbname);
        if (mysqli_connect_errno())
        {
            echo 'Failed to connect to MySQL: ' . mysqli_connect_error();
        }
        $results = mysqli_query($webcon, "SELECT phone FROM `users` WHERE `country_code`='Gabon' and network='Libertis' and `category`='geolocated'");

        $mobhost        = 'localhost';
        $mobusername    = 'root';
        $mobpassword    = 'vps@2017GA';
        $mobdbname      = 'iLink_test';
        $mobcon         = mysqli_connect($mobhost, $mobusername, $mobpassword, $mobdbname);
        if (mysqli_connect_errno())
        {
            echo 'Failed to connect to MySQL: ' . mysqli_connect_error();
        }
        $res1 = null;
        $code_parrain ='LADreUhH5S';
        $id_network =77;
        $this->db->trans_begin();
        if($results) {
            while ($row = mysqli_fetch_array($results)) {

                foreach ($row as $key => $val) {
                    $row[$key] = mysqli_real_escape_string($mobcon, $row[$key]);
                }
                $res1 = $this->user_model->update_networkAgent($row['phone'],$id_network);
                if($res1){
                    $id_codeG = $this->user_model->getCodeG($row['phone']);
                    if($id_codeG!=false){
                        $res2 = $this->user_model->updateCodeG($id_codeG,$code_parrain);
                        if($res2){
                            echo "id du code Ok\n";
                        }else{
                            echo "erreur\n";
                        }
                    }
                }
                if($this->db->trans_status() === FALSE){
                    $this->db->trans_rollback();
                    echo "transaction annulée";
                }else{
                    $this->db->trans_commit();
                    echo "Completed";
                }

            }


        }else{
            echo json_encode($results);
        }

    }

    public function insert_super(){

        $url = 'https://ilink-app.com/mobilebackend/interacted/MembersAction.php';

        $row = 1;
        if (($handle = fopen("https://ilink-app.com/backofficebeta/application/controllers/BASE.csv", "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 1, ",")) !== FALSE ) {
                $num = count($data);
                echo "<p> $num fields in line $row: <br /></p>\n";
                if($row==1) {
                    $webhost        = 'localhost';
                    $webusername    = 'root';
                    $webpassword    = 'vps@2017GA';
                    $webdbname      = 'ilink_beta';
                    $webcon         = mysqli_connect($webhost, $webusername, $webpassword, $webdbname);
                    if (mysqli_connect_errno())
                    {
                        echo 'Failed to connect to MySQL: ' . mysqli_connect_error();
                    }
                    $results = mysqli_query($webcon, "SELECT phone FROM `users` WHERE `country_code`='Gabon' and network='Libertis' and `category`='geolocated'");

                    $mobhost        = 'localhost';
                    $mobusername    = 'root';
                    $mobpassword    = 'vps@2017GA';
                    $mobdbname      = 'iLink_test';
                    $mobcon         = mysqli_connect($mobhost, $mobusername, $mobpassword, $mobdbname);
                    if (mysqli_connect_errno())
                    {
                        echo 'Failed to connect to MySQL: ' . mysqli_connect_error();
                    }
                    if($results) {
                        while ($row = mysqli_fetch_array($results)) {

                            //$user_info = explode(";", $data[$c]);
                            //echo $data[$c] . "<br />\n";
                            foreach ($row as $key => $val) {
                                $row[$key] = mysqli_real_escape_string($mobcon, $row[$key]);
                            }
                            $fields = null;
                            if ($row['country_code'] == 'Cameroun') {

                            } elseif ($row['country_code'] == 'France') {

                            } elseif ($row['country_code'] == 'Gabon') {
                                $fields = array(
                                    "tag" => "member",
                                    "type" => "create_geolocated_user",
                                    "lastname" => addslashes($row['lastname']),
                                    "phone" => $row['phone'],
                                    "phone_transaction" => $row['phone'],
                                    "address" => $row['firstname'],
                                    "lang" => "fr",
                                    "email" => $row['email'],
                                    "password" => "1234",
                                    "town" => ["id" => 1, "country_id" => 78, "name" => "Gabon", "status" => 1],
                                    "network" => ["id" => 14, "country_id" => 78, "name" => "Mobicash"],
                                    "latitude" => $row['latitude'],
                                    "longitude" => $row['longitude'],
                                    "category" => "super",
                                    "member" => "lBkGLqdC7f"
                                );
                            }


                            $fields_string = http_build_query($fields);
                            //url-ify the data for the POST
                            /*foreach ($fields as $key => $value) {
                                $fields_string .= $key . '=' . $value . '&';
                            }
                            rtrim($fields_string, '&');*/

                            $ch = curl_init();


                            curl_setopt($ch, CURLOPT_URL, $url);
                            curl_setopt($ch, CURLOPT_POST, 1);
                            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
                            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));

                            //execute post
                            $result = curl_exec($ch);

                            //close connection
                            curl_close($ch);

							//var_dump($fields_string);

                            //echo json_encode($fields) . "<br />\n";

                        }
                    }else{
                        echo json_encode($results);
                    }
                }
                $row++;
            }
            fclose($handle);
        }

    }

    public function creation_super(){

        $data['message'] = "Login first!";
        $this->load->view('generer_super', $data);
    }

    public function insert_super_ag(){

        $url = 'https://ilink-app.com/mobilebackendtestA/interacted/MembersAction.php';

        if(isset($_POST))
        {
            $num_transac = (int)$this->input->post('transaction');
            $phone = (int)$this->input->post('telephone');
            $end = 1;

            $network = $this->user_model->getGabonNetwork();


            foreach ($network->result() AS $row){

                $code_hyper = $this->user_model->getCodeHyper(77);
				//  var_dump($code_hyper);

                if($code_hyper!=false){
                    $nom = $this->input->post('nom');
                    $fields = array(
                        "tag" => "member",
                        "type" => "create_geolocated_user",
                        "lastname" => addslashes($nom),
                        "phone" => $phone,
                        "phone_transaction" => $num_transac,
                        "address" => "Awendjé",
                        "lang" => "fr",
                        "email" => str_replace(' ', '', strtolower($row->name))."@ilink-app.com",
                        "password" => "1234",
                        "town" => ["id" => 1, "country_id" => 78, "name" => "Gabon", "status" => 1],
                        "network" => ["id" => $row->id, "country_id" => 78, "name" => $row->name],
                        "latitude" => -1,
                        "longitude" => -1,
                        "category" => "super",
                        "member" => $code_hyper
                    );

                    $fields_string = http_build_query($fields);
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL, $url);
                    curl_setopt($ch, CURLOPT_POST, 1);
                    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
                    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
                    $result = curl_exec($ch);
                    curl_close($ch);

                    //var_dump($fields_string);
                    $num_transac++;
                    $phone++;
                }
            }

        }
    }

    public function insert_airtel_mobi(){
        $url = 'https://ilink-app.com/mobilebackendtest/interacted/MembersAction.php';

        if(isset($_POST))
        {
            $num_transac = (int)$this->input->post('transaction');
            $phone = (int)$this->input->post('telephone');
            $end = 1;

            $code_hyper = 'XaU1lVW5DO';
            $nom = $this->input->post('nom');
            $email ="airtelmoney.ga@ilink-app.com";
            $idNetwork = 14;
            $nameNetwork = "Airtel Money";

            for ($i = 1; $i <= 2; $i++){


                    $fields = array(
                        "tag" => "member",
                        "type" => "create_geolocated_user",
                        "lastname" => addslashes($nom),
                        "phone" => $phone,
                        "phone_transaction" => $num_transac,
                        "address" => "Awendjé",
                        "lang" => "fr",
                        "email" => $email,
                        "password" => "1234",
                        "town" => ["id" => 1, "country_id" => 78, "name" => "Gabon", "status" => 1],
                        "network" => ["id" => $idNetwork, "country_id" => 78, "name" => $nameNetwork],
                        "latitude" => -1,
                        "longitude" => -1,
                        "category" => "super",
                        "member" => $code_hyper
                    );

                    $fields_string = http_build_query($fields);
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL, $url);
                    curl_setopt($ch, CURLOPT_POST, 1);
                    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
                    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
                    $result = curl_exec($ch);
                    curl_close($ch);

                    //var_dump($fields_string);
                    $num_transac++;
                    $phone++;
                    $email ="mobicash.ga@ilink-app.com";
                    $idNetwork = 77;
                    $nameNetwork = "Mobicash";

            }

        }
    }

    public function insert_satcon_agents(){
        $url = 'https://ilink-app.com/mobilebackendtest/interacted/MembersAction.php';

        if(isset($_POST))
        {
            $num_transac = (int)$this->input->post('transaction');
            $phone = (int)$this->input->post('telephone');
            $end = 1;

            $code_hyper = 'aTWyxm5vAq';
            $nom = $this->input->post('nom');
            $email ="satcon.ga@ilink-app.com";
            $idNetwork = 223;
            $nameNetwork = "Satcon";
                $fields = array(
                    "tag" => "member",
                    "type" => "create_geolocated_user",
                    "lastname" => addslashes($nom),
                    "phone" => $phone,
                    "phone_transaction" => $num_transac,
                    "address" => "Awendjé",
                    "lang" => "fr",
                    "email" => $email,
                    "password" => "1234",
                    "town" => ["id" => 1, "country_id" => 78, "name" => "Gabon", "status" => 1],
                    "network" => ["id" => $idNetwork, "country_id" => 78, "name" => $nameNetwork],
                    "latitude" => -1,
                    "longitude" => -1,
                    "category" => "super",
                    "member" => $code_hyper
                );

                $fields_string = http_build_query($fields);
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
                curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
                $result = curl_exec($ch);
                curl_close($ch);

                //var_dump($fields_string);

        }
    }

    public function create_hyper_super__for_cameroun(){

        $url = 'https://ilink-app.com/mobilebackendtest/interacted/MembersAction.php';

        $id_country = 38;
        $country_name = 'Cameroun';
        $list_network = $this->user_model->getAllNetworksInfos($id_country);
        $phone = 800405;
        $num_transac = 801405;

        $this->db->trans_begin();
        foreach ($list_network->result() AS $row){

                $nom = 'Super - '.$row->name;
                $email = str_replace('+','plus',strtolower(str_replace(' ','',$row->name.'.cm@ilink-app.com')));
                $code_hyper = '';
                //$code_hyper = $this->user_model->getCodeHyper($row->id);

                switch ($row->id) {
                    case 61:
                        $code_hyper = "1qGVCj9mUW";
                        break;
                    case 72:
                        $code_hyper = "SIY5RRDHuq";
                        break;
                    case 83:
                        $code_hyper = "LxugQ1sEv3";
                        break;
                    case 96:
                        $code_hyper = "inmycWlKg7";
                        break;
                    case 101:
                        $code_hyper = "GUhpKC4xP4";
                        break;
                    case 110:
                        $code_hyper = "xEHh8bromY";
                        break;
                    case 119:
                        $code_hyper = "QV1yez3Vvv";
                        break;
                    case 128:
                        $code_hyper = "U3oZgJ3Raj";
                        break;
                    case 136:
                        $code_hyper = "IYI8LZy80k";
                        break;
                    case 147:
                        $code_hyper = "ojGmDqWUXN";
                        break;
                    case 159:
                        $code_hyper = "Yw2hMiEUUi";
                        break;
                    case 172:
                        $code_hyper = "rXLofF9iCq";
                        break;
                    case 185:
                        $code_hyper = "pNlXXMIxrr";
                        break;
                    case 198:
                        $code_hyper = "YeqY1vRtqp";
                        break;
                    case 210:
                        $code_hyper = "2X9MKvWAdV";
                        break;
                }


                for ($i = 11; $i <= 15; $i++){
                    $fields = array(
                        "tag" => "member",
                        "type" => "create_geolocated_user",
                        "lastname" => addslashes($nom.' '.$i),
                        "phone" => '+237'.$phone,
                        "phone_transaction" => '+237'.$num_transac,
                        "address" => "Douala",
                        "lang" => "fr",
                        "email" => $email,
                        "password" => "1234",
                        "town" => ["id" => 2, "country_id" => $id_country, "name" => $country_name, "status" => 1],
                        "network" => ["id" => $row->id, "country_id" => $id_country, "name" => $row->name],
                        "latitude" => 4.0303481,
                        "longitude" => 9.6955774,
                        "category" => "super",
                        "member" => $code_hyper
                    );

                    $fields_string = http_build_query($fields);
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL, $url);
                    curl_setopt($ch, CURLOPT_POST, 1);
                    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
                    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
                    $result = curl_exec($ch);
                    curl_close($ch);

                    //var_dump($fields_string);
                    $num_transac++;
                    $phone++;

                }


            if ($this->db->trans_status() === FALSE)
            {
                $this->db->trans_rollback();
            }
            else
            {
                $this->db->trans_commit();
            }
        }
    }

    public function create_hyper_super__for_burkina(){

        $url = 'https://ilink-app.com/mobilebackendtest/interacted/MembersAction.php';

        $id_country = 35;
        $country_name = 'Burkina Faso';
        $list_network = $this->user_model->getAllNetworksInfos($id_country);
        $phone = 800330;
        $num_transac = 801330;

        $this->db->trans_begin();
        foreach ($list_network->result() AS $row){

            $nom = 'Super - '.$row->name;
            $email = str_replace('+','plus',strtolower(str_replace(' ','',$row->name.'.bf@ilink-app.com')));
            $code_hyper = '';
            //$code_hyper = $this->user_model->getCodeHyper($row->id);

            switch ($row->id) {
                case 34:
                    $code_hyper = "FwdhzYphN9"; //Orange Money
                    break;
                case 80:
                    $code_hyper = "mKDhlm6qHE"; //Mobicash
                    break;
                case 92:
                    $code_hyper = "hCx1XWUdkF"; //Western Union
                    break;
                case 107:
                    $code_hyper = "K8QyRT6lx1"; //Express Union
                    break;
                case 113:
                    $code_hyper = "40dBWQO01Y"; //Money Express
                    break;
                case 126:
                    $code_hyper = "6VZn319rLg"; //UBA
                    break;
                case 134:
                    $code_hyper = "OnYzGrYOmn"; //Canal +
                    break;
                case 144:
                    $code_hyper = "yricgPggVQ"; //Small World
                    break;
                case 155:
                    $code_hyper = "8a3ce5WmjX"; //Juba
                    break;
                case 168:
                    $code_hyper = "dLrX56TGff"; //Wari
                    break;
                case 181:
                    $code_hyper = "ARILdZBjMZ"; //Ria
                    break;
                case 194:
                    $code_hyper = "6vNWB5NUCF"; //Sigue
                    break;
                case 207:
                    $code_hyper = "u4PJg75tDr"; //Ecobank
                    break;
                case 218:
                    $code_hyper = "uCphmoVjkQ"; //DHL
                    break;
                case 224:
                    $code_hyper = "0EbLos93CE"; //MoneyGram
                    break;
            }


            for ($i = 1; $i <= 5; $i++){
                $fields = array(
                    "tag" => "member",
                    "type" => "create_geolocated_user",
                    "lastname" => addslashes($nom.' '.$i),
                    "phone" => '+226'.$phone,
                    "phone_transaction" => '+226'.$num_transac,
                    "address" => "Ouagadougou",
                    "lang" => "fr",
                    "email" => $email,
                    "password" => "1234",
                    "town" => ["id" => 11, "country_id" => $id_country, "name" => $country_name, "status" => 1],
                    "network" => ["id" => $row->id, "country_id" => $id_country, "name" => $row->name],
                    "latitude" => -1.50914,
                    "longitude" => 12.3136,
                    "category" => "super",
                    "member" => $code_hyper
                );

                $fields_string = http_build_query($fields);
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
                curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
                $result = curl_exec($ch);
                curl_close($ch);

                //var_dump($fields_string);
                $num_transac++;
                $phone++;

            }


            if ($this->db->trans_status() === FALSE)
            {
                $this->db->trans_rollback();
            }
            else
            {
                $this->db->trans_commit();
            }
        }
    }

    public function create_hyper_super__for_congo(){

        $url = 'https://ilink-app.com/mobilebackendtest/interacted/MembersAction.php';

        $id_country = 50;
        $country_name = 'Congo';
        $list_network = $this->user_model->getAllNetworksInfos($id_country);
        $phone = 800344;
        $num_transac = 801344;

        $this->db->trans_begin();
        foreach ($list_network->result() AS $row){

            $nom = 'Super - '.$row->name;
            $email = str_replace('+','plus',strtolower(str_replace(' ','',$row->name.'.cg@ilink-app.com')));
            $code_hyper = '';
            //$code_hyper = $this->user_model->getCodeHyper($row->id);

            switch ($row->id) {
                case 15:
                    $code_hyper = "tsKrqQ9Ysu"; //Airtel Money Congo
                    break;
                case 73:
                    $code_hyper = "i2y8cQSd5v"; //MTN Congo
                    break;
                case 85:
                    $code_hyper = "zXDRW92SAs"; //Western Union Congo
                    break;
                case 102:
                    $code_hyper = "uLQsOjz18F"; //Express Union Congo
                    break;
                case 120:
                    $code_hyper = "XmogcV7QS5"; //UBA Congo
                    break;
                case 129:
                    $code_hyper = "dfOz5pA0Xr"; //Canal + Congo
                    break;
                case 137:
                    $code_hyper = "CnsnYwAlUX"; //Small World Congo
                    break;
                case 148:
                    $code_hyper = "uqAD5k5wr0"; //Juba Congo
                    break;
                case 161:
                    $code_hyper = "eKk9RTafzd"; //Wari Congo
                    break;
                case 174:
                    $code_hyper = "kCXVFm2qDy"; //Ria Congo
                    break;
                case 187:
                    $code_hyper = "kRiO8jGA9k"; //Sigue Congo
                    break;
                case 200:
                    $code_hyper = "gUIkTEZzSW"; //Ecobank Congo
                    break;
                case 211:
                    $code_hyper = "361EmZOKjr"; //DHL Congo
                    break;
                case 98:
                    $code_hyper = "zhCnuFtqjb"; //MoneyGram Congo
                    break;
            }


            for ($i = 1; $i <= 15; $i++){
                $fields = array(
                    "tag" => "member",
                    "type" => "create_geolocated_user",
                    "lastname" => addslashes($nom.' '.$i),
                    "phone" => '+242'.$phone,
                    "phone_transaction" => '+242'.$num_transac,
                    "address" => "Brazzaville",
                    "lang" => "fr",
                    "email" => $email,
                    "password" => "1234",
                    "town" => ["id" => 4, "country_id" => $id_country, "name" => $country_name, "status" => 1],
                    "network" => ["id" => $row->id, "country_id" => $id_country, "name" => $row->name],
                    "latitude" => 4.2634,
                    "longitude" => 15.2429,
                    "category" => "super",
                    "member" => $code_hyper
                );

                $fields_string = http_build_query($fields);
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
                curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
                $result = curl_exec($ch);
                curl_close($ch);

                //var_dump($fields_string);
                $num_transac++;
                $phone++;

            }


            if ($this->db->trans_status() === FALSE)
            {
                $this->db->trans_rollback();
            }
            else
            {
                $this->db->trans_commit();
            }
        }
    }

    public function create_hyper(){

        $url = 'https://ilink-app.com/mobilebackendtest/interacted/MembersAction.php';
        $countries = $this->user_model->getCountries();
        foreach ($countries->result() AS $country){
            if($country->id ==35){
                $id_country = $country->id;
                $country_name = $country->name;
                $code_country = $country->code_country;
                $code_dial = $country->code_dial;
                $ville = $country->ville;
                $id_ville = $country->id_ville;

                $list_network = $this->user_model->getAllNetworksInfos($id_country);
                $phone = 99900001;
                $num_transac = 99900101;

                $this->db->trans_begin();
                foreach ($list_network->result() AS $row) {

                    if ($row->id == 228){

                        $nom = 'Hyper - ' . $row->name;
                        $email = str_replace('+', 'plus', strtolower(str_replace(' ', '', $row->name . '.' . $code_country . '@ilink-app.com')));
                        $code_hyper = $this->user_model->getFreeCode();

                        $fields = array(
                            "tag" => "member",
                            "type" => "create_geolocated_user",
                            "lastname" => addslashes($nom),
                            "phone" => $code_dial . $phone,
                            "phone_transaction" => $code_dial . $num_transac,
                            "address" => $ville,
                            "lang" => "fr",
                            "email" => $email,
                            "password" => "1234",
                            "town" => ["id" => $id_ville, "country_id" => $id_country, "name" => $country_name, "status" => 1],
                            "network" => ["id" => $row->id, "country_id" => $id_country, "name" => $row->name],
                            "latitude" => -1.50914,
                            "longitude" => 12.3136,
                            "category" => "hyper",
                            "member" => $code_hyper
                        );

                        $fields_string = http_build_query($fields);
                        $ch = curl_init();
                        curl_setopt($ch, CURLOPT_URL, $url);
                        curl_setopt($ch, CURLOPT_POST, 1);
                        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
                        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
                        $result = curl_exec($ch);
                        curl_close($ch);

                        //var_dump($fields_string);
                        $num_transac++;
                        $phone++;

                        if ($this->db->trans_status() === FALSE) {
                            $this->db->trans_rollback();
                        } else {
                            $this->db->trans_commit();
                        }

                    }
                }


            }
        }
    }

    public function active_hyper(){
        $url = 'https://ilink-app.com/mobilebackendtest/interacted/MembersAction.php';

        $hypervisers = $this->user_model->getInfosActivationHyperviseur();
        foreach ($hypervisers->result() AS $hyperviser){
            $fields = array(
                "tag" => "member",
                "type" => "validate_hyper_backoffice",
                "lang" => "fr",
                "phone" => $hyperviser->phone,
                "code_validation" => $hyperviser->validation_code,
                "nbre_super" => 200,
                "nbre_geo" => 2000
            );
            $fields_string = http_build_query($fields);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
            $result = curl_exec($ch);
            curl_close($ch);
        }
    }

    public function create_super(){

        $url = 'https://ilink-app.com/mobilebackendtest/interacted/MembersAction.php';
        $countries = $this->user_model->getCountries();
        foreach ($countries->result() AS $country){

            if($country->id ==38){
                $id_country = $country->id;
                $country_name = $country->name;
                $code_country = $country->code_country;
                $code_dial = $country->code_dial;
                $ville = $country->ville;
                $id_ville = $country->id_ville;

                $list_network = $this->user_model->getAllNetworksInfos($id_country);
                $phone = 99900014;
                $num_transac = 99900114;
                $nom [] = null;
                $nom[1] = 'Ewombe Patricia';
                $nom[2] = 'Michelle TAWA';
                $nom[3] = 'Maxime MADIBA MBAPPE';
                $nom[4] = 'Ndemba Anais';
                $nom[5] = 'Balla Simplice Steven';
                $nom[6] = 'NDOME Eyango Hondrea';
                $nom[7] = 'NYAME Guy Thierry';
                $nom[8] = 'Valentin BABAGNAK';
                $nom[9] = 'Christian Junior KOOH DALLE';
                $nom[10] = 'Lottin Jeannot';
                $nom[11] = 'Priscille ENGONO';
                $nom[12] = 'AGENT-NYASSA-PK28';

                $this->db->trans_begin();
                foreach ($list_network->result() AS $row) {
                echo $row->name;
                    if($row->id==231){
                        $nom = 'Benoit MBANGUE';
                        $email = str_replace('+', 'plus', strtolower(str_replace(' ', '', $row->name . '.' . $code_country . '@ilink-app.com')));
                        $code_hyper = $this->user_model->getCodeMembreHyper($row->id);

                        for ($i = 1; $i <= 1; $i++) {

                            $fields = array(
                                "tag" => "member",
                                "type" => "create_geolocated_user",
                                "lastname" => addslashes($nom),
                                "phone" => $code_dial . $phone,
                                "phone_transaction" => $code_dial . $num_transac,
                                "address" => $ville,
                                "lang" => "fr",
                                "email" => $email,
                                "password" => "1234",
                                "town" => ["id" => $id_ville, "country_id" => $id_country, "name" => $country_name, "status" => 1],
                                "network" => ["id" => $row->id, "country_id" => $id_country, "name" => $row->name],
                                "latitude" => 4.05,
                                "longitude" => 9.7,
                                "category" => "super",
                                "member" => $code_hyper
                            );

                            $fields_string = http_build_query($fields);
                            $ch = curl_init();
                            curl_setopt($ch, CURLOPT_URL, $url);
                            curl_setopt($ch, CURLOPT_POST, 1);
                            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
                            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
                            $result = curl_exec($ch);
                            curl_close($ch);


                            //var_dump($fields_string);
                            $num_transac++;
                            $phone++;
                        }

                        if ($this->db->trans_status() === FALSE) {
                            $this->db->trans_rollback();
                        } else {
                            $this->db->trans_commit();
                        }
                    }else{

                    }


                }


            }else{

            }
        }
    }

    public function show_geolocatedPoints(){

        $webhost        = 'localhost';
        $webusername    = 'root';
        $webpassword    = 'vps@2017GA';
        $webdbname      = 'iLink_cannary';
        $webcon         = mysqli_connect($webhost, $webusername, $webpassword, $webdbname);
        if (mysqli_connect_errno())
        {
            echo 'Failed to connect to MySQL: ' . mysqli_connect_error();
        }
        $results = mysqli_query($webcon, "SELECT agents.lastname,networks.name,networks_agents.phone,networks_agents.transactionNumber FROM `agents` 
                                                    INNER JOIN networks_agents ON agents.id=networks_agents.agent_id 
                                                    INNER JOIN networks ON networks.id=networks_agents.network_id
                                                    WHERE networks.country_id=78
                                                    ORDER BY agents.lastname");

        if($results) {
            while ($row = mysqli_fetch_array($results)) {
                echo $row['lastname'].' '.$row['name'].' '.$row['transactionNumber'].' '.$row['phone'].' '.$row['transactionNumber']."<br />";
            }
        }else{
            echo 'Something wrong !';
        }
    }

    public function test(){

        $data['active'] = "demandes";
        $data['alert'] = "";
        $data['token'] = $this->session->userdata('token');
        $data['email'] = "nzaleh@gmail.com";
        $data['firstname'] = "Hanry";
        $data['lastname'] = "Nzale";
        $data['code_parrain'] = "VGVTttvf";
        $data['phone'] = "+24107916421";
        $data['adresse'] = "IAI";
        $data['category'] = "Admin";
        $data['network'] = 1;
        $data['pays'] = $this->user_model->getAllCountries();
        $data['pays'] = $this->user_model->getAllCountries();
        $data['hyper'] = $this->user_model->getAllHyper($data['pays']->first_row()->id);
        $data['ville'] = $this->user_model->getVilleNetworkByHyper($data['hyper']->first_row()->code_membre);
        $this->load->view('header', $data);
        $this->load->view('test');
        $this->load->view('footer');
    }
    //ok
    public function campagne_geocoding_orangemoney(){

        function get_lat_lng($address){

            $address_encoding = htmlentities(urlencode($address));
            $api = "AIzaSyClYkvZ3XUD9EKZv3Z2BLzkr7wBoV2aQ98";

            $url = "https://maps.googleapis.com/maps/api/geocode/json?address=".$address_encoding."&key=".$api;
            $contents = file_get_contents($url); 
            if($contents !== false){
                $contents = utf8_encode($contents);
                $results = json_decode($contents);
                $data = $results->results;
                $lat = $data[0]->geometry->location->lat;
                $lng = $data[0]->geometry->location->lng;
                $adress = $data[0]->formatted_address;
                $ville = $data[0]->address_components[2]->long_name;
                $position["lat"] = $lat;
                $position["lng"] = $lng;
                $position["adresse"] = $address;
                $results = $data;

                if ($results[0]) {
                    $num = sizeof($results[0]->address_components);
                         for ($i=0; $i<$num; $i++) {
                             $num2 = sizeof($results[0]->address_components[$i]->types);
                            for ($b=0;$b<$num2;$b++) {
                                if ($results[0]->address_components[$i]->types[$b] == "locality") {
                                        $city= $results[0]->address_components[$i];
                                        break;
                                    }
                            }
                        }
                    } 
                $position["ville"] = $city->long_name;
                return $position;
            }else{
                return null;
            }
        }
        $url = 'https://ilink-app.com/mobilebackendtest/interacted/MembersAction.php';
        $row = 1;
        $nom = "";
        $address = "";
        $num_contact ="";
        $default_num = 33900001018;

        if (($handle = fopen("https://ilink-app.com/backofficeiLinkTest/application/controllers/Orange_money_paris.csv", "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                $num = count($data);
                if($row==3){
                    echo "<p> $num fields in line $row: <br /></p>\n";
                    for ($c=0; $c < $num; $c++) {
                        $infos = explode(";", $data[$c]);
                        
                        $network_name = "Orange Money";
                        $network_id = 60;
                        $email = 'orangemoney.fr@ilink-app.com';
                        $code_super = "eNz6BDONDs";

                        $nom = $infos[0];
                        $address = $infos[1].', '.$infos[2];
                        $num_contact = '+33'.$infos[4];

                        $position = get_lat_lng($address);
                        $lat = $position['lat'];
                        $lng = $position['lng'];
                        $ville = $position['ville'];
                        $address = $position['adresse'];
                        $town_id = $this->user_model->getTownId($ville);

                        $default_num++;
                        $exist = $this->user_model->checkPosition($lat,$lng);
                        if($exist == false){
                            $fields = array(
                                "tag" => "member",
                                "type" => "create_geolocated_user",
                                "lastname" => addslashes($nom),
                                "phone" => $num_contact,
                                "phone_transaction" => '+'.$default_num,
                                "address" => addslashes($address),
                                "lang" => "fr",
                                "email" => $email,
                                "password" => "1234",
                                "town" => ["id" => $town_id, "country_id" => 74, "name" => "France", "status" => 1],
                                "network" => ["id" => $network_id, "country_id" => 74, "name" => $network_name],
                                "latitude" => $lat,
                                "longitude" => $lng,
                                "category" => "geolocated",
                                "member" => $code_super
                            );
                        }else{
                            
                        }
                        //echo json_encode($fields)."<br /></p>\n";
                        $this->db->trans_begin();
                        $fields_string = http_build_query($fields);
                        $ch = curl_init();
                        curl_setopt($ch, CURLOPT_URL, $url);
                        curl_setopt($ch, CURLOPT_POST, 1);
                        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
                        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
                        $result = curl_exec($ch);
                        curl_close($ch);
                        if ($this->db->trans_status() === FALSE) {
                            $this->db->trans_rollback();
                        } else {
                            $this->db->trans_commit();
                        }

                        //echo $infos[0] .' -- '.$infos[1].' -- '.$infos[2].' -- '.$infos[3].' ('.$lat.'/'.$lng. ") <br />\n";
                    }
                }
                $row++;
            }
            fclose($handle);
        }

        /*$address = "";
        $position = get_lat_lng($address);
        $lat = $position['lat'];
        $lng = $position["lng"];*/


    }
    //ok
    public function campagne_geocoding_western(){

        function get_lat_lng($address){

            $address_encoding = htmlentities(urlencode($address));
            $api = "AIzaSyClYkvZ3XUD9EKZv3Z2BLzkr7wBoV2aQ98";

            $url = "https://maps.googleapis.com/maps/api/geocode/json?address=".$address_encoding."&key=".$api;
            $contents = file_get_contents($url); 
            if($contents !== false){
                $contents = utf8_encode($contents);
                $results = json_decode($contents);
                $data = $results->results;
                $lat = $data[0]->geometry->location->lat;
                $lng = $data[0]->geometry->location->lng;
                $adress = $data[0]->formatted_address;
                $ville = $data[0]->address_components[2]->long_name;
                $position["lat"] = $lat;
                $position["lng"] = $lng;
                $position["adresse"] = $address;
                $results = $data;

                if ($results[0]) {
                    $num = sizeof($results[0]->address_components);
                         for ($i=0; $i<$num; $i++) {
                             $num2 = sizeof($results[0]->address_components[$i]->types);
                            for ($b=0;$b<$num2;$b++) {
                                if ($results[0]->address_components[$i]->types[$b] == "locality") {
                                        $city= $results[0]->address_components[$i];
                                        break;
                                    }
                            }
                        }
                    } 
                $position["ville"] = $city->long_name;
                return $position;
            }else{
                return null;
            }
        }
        function remove_accent($str)
        {
            $a = array('À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð',
                            'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'ß', 'à', 'á', 'â', 'ã',
                            'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ñ', 'ò', 'ó', 'ô', 'õ',
                            'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ', 'Ā', 'ā', 'Ă', 'ă', 'Ą', 'ą', 'Ć', 'ć', 'Ĉ',
                            'ĉ', 'Ċ', 'ċ', 'Č', 'č', 'Ď', 'ď', 'Đ', 'đ', 'Ē', 'ē', 'Ĕ', 'ĕ', 'Ė', 'ė', 'Ę', 'ę',
                            'Ě', 'ě', 'Ĝ', 'ĝ', 'Ğ', 'ğ', 'Ġ', 'ġ', 'Ģ', 'ģ', 'Ĥ', 'ĥ', 'Ħ', 'ħ', 'Ĩ', 'ĩ', 'Ī', 'ī',
                            'Ĭ', 'ĭ', 'Į', 'į', 'İ', 'ı', 'Ĳ', 'ĳ', 'Ĵ', 'ĵ', 'Ķ', 'ķ', 'Ĺ', 'ĺ', 'Ļ', 'ļ', 'Ľ', 'ľ',
                            'Ŀ', 'ŀ', 'Ł', 'ł', 'Ń', 'ń', 'Ņ', 'ņ', 'Ň', 'ň', 'ŉ', 'Ō', 'ō', 'Ŏ', 'ŏ', 'Ő', 'ő', 'Œ',
                            'œ', 'Ŕ', 'ŕ', 'Ŗ', 'ŗ', 'Ř', 'ř', 'Ś', 'ś', 'Ŝ', 'ŝ', 'Ş', 'ş', 'Š', 'š', 'Ţ', 'ţ', 'Ť', 
                            'ť', 'Ŧ', 'ŧ', 'Ũ', 'ũ', 'Ū', 'ū', 'Ŭ', 'ŭ', 'Ů', 'ů', 'Ű', 'ű', 'Ų', 'ų', 'Ŵ', 'ŵ', 'Ŷ', 
                            'ŷ', 'Ÿ', 'ÿ', 'Ź', 'ź', 'Ż', 'ż', 'Ž', 'ž', 'ſ', 'ƒ', 'Ơ', 'ơ', 'Ư', 'ư', 'Ǎ', 'ǎ', 'Ǐ', 'ǐ',
                            'Ǒ', 'ǒ', 'Ǔ', 'ǔ', 'Ǖ', 'ǖ', 'Ǘ', 'ǘ', 'Ǚ', 'ǚ', 'Ǜ', 'ǜ', 'Ǻ', 'ǻ', 'Ǽ', 'ǽ', 'Ǿ', 'ǿ');

            $b = array('A', 'A', 'A', 'A', 'A', 'A', 'AE', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I', 'D', 'N', 'O',
                            'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 's', 'a', 'a', 'a', 'a', 'a', 'a', 'ae', 'c',
                            'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'n', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u',
                            'y', 'y', 'A', 'a', 'A', 'a', 'A', 'a', 'C', 'c', 'C', 'c', 'C', 'c', 'C', 'c', 'D', 'd', 'D',
                            'd', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'E', 'e', 'G', 'g', 'G', 'g', 'G', 'g', 'G', 'g',
                            'H', 'h', 'H', 'h', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'I', 'i', 'IJ', 'ij', 'J', 'j', 'K',
                            'k', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'L', 'l', 'N', 'n', 'N', 'n', 'N', 'n', 'n', 'O', 'o',
                            'O', 'o', 'O', 'o', 'OE', 'oe', 'R', 'r', 'R', 'r', 'R', 'r', 'S', 's', 'S', 's', 'S', 's', 'S',
                            's', 'T', 't', 'T', 't', 'T', 't', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'W',
                            'w', 'Y', 'y', 'y', 'Y', 'Z', 'z', 'Z', 'z', 'Z', 'z', 's', 'f', 'O', 'o', 'U', 'u', 'A', 'a', 'I', 'i',
                            'O', 'o', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'U', 'u', 'A', 'a', 'AE', 'ae', 'O', 'o');
            return str_replace($a, $b, $str);
        } 
        $url = 'https://ilink-app.com/mobilebackendtest/interacted/MembersAction.php';
        $row = 1;
        $nom = "";
        $address = "";
        $num_contact ="";
        $n1=1;
        $n2=0;
        $ok = true;
        $ct = 0;
        $default_num = 33900004034;
        $lastname = "";

        if (($handle = fopen("https://ilink-app.com/backofficeiLinkTest/application/controllers/western_union_paris.csv", "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 10000, ";")) !== FALSE) {
                $num = count($data);
                //echo "<p> $num fields in line $row: <br /></p>\n";
                
                for ($c=0; $c < $num; $c++) {
                    //echo $data[$c] . "<br />\n";
                    if($n1 == 1){
                        $lastname = $data[$c];
                        $n1++;
                    }elseif($n1==2){
                        $tmp = utf8_encode(str_replace( chr( 194 ) . chr( 160 ), ' ', $data[$c] ));
                        $inf = explode("+", str_replace( chr( 194 ) . chr( 160 ), ' ', $tmp ));
                        $address = $inf[0];
                        $contact_correct = str_replace('-','',$inf[1]);
                        $num_contact = '+'.$contact_correct;
                        $nom = $lastname;
                        /*echo $lastname. "<br />\n";
                        echo $address. "<br />\n";
                        echo $num_contact. "<br />\n";*/

                        $network_name = "Western Union";
                        $network_id = 84;
                        $email = 'westernunion.fr@ilink-app.com';
                        $code_super = "fDvQe9ZANz";

                        $position = get_lat_lng($address);
                        $lat = $position['lat'];
                        $lng = $position['lng'];
                        $ville = remove_accent($position['ville']);
                        $ville = str_replace('A¿', 'y', $ville);
                        $address = $position['adresse'];
                        /*$town_id = $this->user_model->getTownId(addslashes($ville));
                        if($town_id==null){
                            $res = $this->user_model->create_town(74,addslashes($ville));
                            if($res){
                                $town_id = $this->user_model->getTownId(addslashes($ville));
                                $ret = $this->user_model->update_town_agent($lat,$lng,$town_id);
                                if($ret){
                                    echo "Mise à jour Ok!";
                                }else{
                                   echo "Erreur lors de la mise à jour";
                                }
                            }else{
                                echo "Erreur lors de la création de la ville";
                            }
                        }*/

                        
                        /*if($num_contact == "+33969366050"){
                            $num_contact = $default_num;
                            $default_num++;
                        }
                        for($i=10; $i < 34; $i++){
                            if($num_contact == '+339000000'.$i){
                                $num_contact = $default_num;
                                $default_num++;
                            }
                        }
                        for($i=0; $i < 10; $i++){
                            if($num_contact == '+3390000000'.$i){
                                $num_contact = $default_num;
                                $default_num++;
                            }
                        }*/

                        $fields = array(
                            "tag" => "member",
                            "type" => "create_geolocated_user",
                            "lastname" => addslashes($nom),
                            "phone" => $num_contact,
                            "phone_transaction" => '+'.$default_num,
                            "address" => addslashes($address),
                            "lang" => "fr",
                            "email" => $email,
                            "password" => "1234",
                            "town" => ["id" => 3, "country_id" => 74, "name" => "France", "status" => 1],
                            "network" => ["id" => $network_id, "country_id" => 74, "name" => $network_name],
                            "latitude" => $lat,
                            "longitude" => $lng,
                            "category" => "geolocated",
                            "member" => $code_super
                        );
                        $default_num++;
                        //echo json_encode($fields)."<br /></p>\n";;
                        $this->db->trans_begin();
                        $fields_string = http_build_query($fields);
                        $ch = curl_init();
                        curl_setopt($ch, CURLOPT_URL, $url);
                        curl_setopt($ch, CURLOPT_POST, 1);
                        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
                        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
                        $result = curl_exec($ch);
                        curl_close($ch);
                        if ($this->db->trans_status() === FALSE) {
                            $this->db->trans_rollback();
                        } else {
                            $this->db->trans_commit();
                        }

                        $n1 = 1;
                    }
                    
                    /*$data[0] = utf8_encode($data[0]);
                    $data[0] = str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] );
                    $inf = explode("+", str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] ));
                    $address = str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] ).' '.$inf[0];
                    $contact_correct = str_replace('-','',$inf[1]);
                    $num_contact ='+'.$contact_correct;
                    echo "<p> $num fields in line $row: <br /></p>\n";
                    echo $lastname."<br /></p>\n";
                    echo json_encode($data)."<br /></p>\n";*/
                }
                $row++;
            }
            fclose($handle);
        }

        /*$address = "";
        $position = get_lat_lng($address);
        $lat = $position['lat'];
        $lng = $position["lng"];*/


    }
    //ok
    public function campagne_geocoding_moneygram(){

        function get_lat_lng($address){

            $address_encoding = htmlentities(urlencode($address));
            $api = "AIzaSyClYkvZ3XUD9EKZv3Z2BLzkr7wBoV2aQ98";

            $url = "https://maps.googleapis.com/maps/api/geocode/json?address=".$address_encoding."&key=".$api;
            $contents = file_get_contents($url); 
            if($contents !== false){
                $contents = utf8_encode($contents);
                $results = json_decode($contents);
                $data = $results->results;
                $lat = $data[0]->geometry->location->lat;
                $lng = $data[0]->geometry->location->lng;
                $adress = $data[0]->formatted_address;
                $ville = $data[0]->address_components[2]->long_name;
                $position["lat"] = $lat;
                $position["lng"] = $lng;
                $position["adresse"] = $address;
                $results = $data;

                if ($results[0]) {
                    $num = sizeof($results[0]->address_components);
                         for ($i=0; $i<$num; $i++) {
                             $num2 = sizeof($results[0]->address_components[$i]->types);
                            for ($b=0;$b<$num2;$b++) {
                                if ($results[0]->address_components[$i]->types[$b] == "locality") {
                                        $city= $results[0]->address_components[$i];
                                        break;
                                    }
                            }
                        }
                    } 
                $position["ville"] = $city->long_name;
                return $position;
            }else{
                return null;
            }
        }
        $url = 'https://ilink-app.com/mobilebackendtest/interacted/MembersAction.php';
        $row = 1;
        $nom = "";
        $address = "";
        $num_contact ="";
        $n1=1;
        $n2=0;
        $ok = true;
        $ct = 0;
        $default_num = 33900003034;
        $lastname = "";

        if (($handle = fopen("https://ilink-app.com/backofficeiLinkTest/application/controllers/MONEYGRAM.csv", "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 10000, ";")) !== FALSE) {
                $num = count($data);
                for ($c=0; $c < $num; $c++) {
                    //echo $data[$c] . "<br />\n";
                    if($n1 == 1){
                        $lastname = $data[$c];
                        $n1++;
                    }elseif($n1==2){
                        $adress1 = $data[$c];
                        $n1++;

                        
                        /*if($num_contact == "+33969366050"){
                            $num_contact = $default_num;
                            $default_num++;
                        }
                        for($i=10; $i < 34; $i++){
                            if($num_contact == '+339000000'.$i){
                                $num_contact = $default_num;
                                $default_num++;
                            }
                        }
                        for($i=0; $i < 10; $i++){
                            if($num_contact == '+3390000000'.$i){
                                $num_contact = $default_num;
                                $default_num++;
                            }
                        }*/

                        /*
                        */

                        
                    }elseif($n1 == 3){
                        $address = $adress1.' '.$data[$c];
                        $n1++;
                    }elseif($n1 == 4){
                        $num_contact = $data[$c];
                        /*echo $lastname."<br /></p>\n";
                        echo $address."<br /></p>\n";
                        echo $num_contact."<br /></p>\n";*/

                        $num_contact = '+33'.$num_contact;
                        $nom = $lastname;
                        /*echo $lastname. "<br />\n";
                        echo $address. "<br />\n";
                        echo $num_contact. "<br />\n";*/

                        $network_name = "MoneyGram";
                        $network_id = 97;
                        $email = 'moneygram.fr@ilink-app.com';
                        $code_super = "Q4kVHVRHvo";

                        $position = get_lat_lng($address);
                        $lat = $position['lat'];
                        $lng = $position['lng'];
                        $ville = $position['ville'];
                        $address = $position['adresse'];
                        //$town_id = $this->user_model->getTownId($ville);

                        $exist = $this->user_model->checkPosition($lat,$lng);
                        if($exist == false){
                            $fields = array(
                                "tag" => "member",
                                "type" => "create_geolocated_user",
                                "lastname" => addslashes($nom),
                                "phone" => $num_contact,
                                "phone_transaction" => '+'.$default_num,
                                "address" => addslashes($address),
                                "lang" => "fr",
                                "email" => $email,
                                "password" => "1234",
                                "town" => ["id" => 3, "country_id" => 74, "name" => "France", "status" => 1],
                                "network" => ["id" => $network_id, "country_id" => 74, "name" => $network_name],
                                "latitude" => $lat,
                                "longitude" => $lng,
                                "category" => "geolocated",
                                "member" => $code_super
                            );
                        }else{

                        }
                        $default_num++;
                        //echo json_encode($fields)."<br /></p>\n";
                        $this->db->trans_begin();
                        $fields_string = http_build_query($fields);
                        $ch = curl_init();
                        curl_setopt($ch, CURLOPT_URL, $url);
                        curl_setopt($ch, CURLOPT_POST, 1);
                        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
                        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
                        $result = curl_exec($ch);
                        curl_close($ch);
                        if ($this->db->trans_status() === FALSE) {
                            $this->db->trans_rollback();
                        } else {
                            $this->db->trans_commit();
                        }


                        $n1 = 1;
                    }
                    
                    /*$data[0] = utf8_encode($data[0]);
                    $data[0] = str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] );
                    $inf = explode("+", str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] ));
                    $address = str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] ).' '.$inf[0];
                    $contact_correct = str_replace('-','',$inf[1]);
                    $num_contact ='+'.$contact_correct;
                    echo "<p> $num fields in line $row: <br /></p>\n";
                    echo $lastname."<br /></p>\n";
                    echo json_encode($data)."<br /></p>\n";*/
                }
                $row++;
            }
            fclose($handle);
        }

        /*$address = "";
        $position = get_lat_lng($address);
        $lat = $position['lat'];
        $lng = $position["lng"];*/


    }
    //ok
    public function campagne_geocoding_ria(){

        function get_lat_lng($address){

            $address_encoding = htmlentities(urlencode($address));
            $api = "AIzaSyClYkvZ3XUD9EKZv3Z2BLzkr7wBoV2aQ98";

            $url = "https://maps.googleapis.com/maps/api/geocode/json?address=".$address_encoding."&key=".$api;
            $contents = file_get_contents($url); 
            if($contents !== false){
                $contents = utf8_encode($contents);
                $results = json_decode($contents);
                $data = $results->results;
                $lat = $data[0]->geometry->location->lat;
                $lng = $data[0]->geometry->location->lng;
                $adress = $data[0]->formatted_address;
                $ville = $data[0]->address_components[2]->long_name;
                $position["lat"] = $lat;
                $position["lng"] = $lng;
                $position["adresse"] = $address;
                $results = $data;

                if ($results[0]) {
                    $num = sizeof($results[0]->address_components);
                         for ($i=0; $i<$num; $i++) {
                             $num2 = sizeof($results[0]->address_components[$i]->types);
                            for ($b=0;$b<$num2;$b++) {
                                if ($results[0]->address_components[$i]->types[$b] == "locality") {
                                        $city= $results[0]->address_components[$i];
                                        break;
                                    }
                            }
                        }
                    } 
                $position["ville"] = $city->long_name;
                return $position;
            }else{
                return null;
            }
        }
        $url = 'https://ilink-app.com/mobilebackendtest/interacted/MembersAction.php';
        $row = 1;
        $nom = "";
        $address = "";
        $num_contact ="";
        $n1=1;
        $n2=0;
        $ok = true;
        $ct = 0;
        $default_num = 33900002000;
        $lastname = "";

        if (($handle = fopen("https://ilink-app.com/backofficeiLinkTest/application/controllers/ria_paris.csv", "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 10000, ";")) !== FALSE) {
                $num = count($data);
                $num = 1;
                //echo "<p> $num fields in line $row: <br /></p>\n";
                for ($c=0; $c < $num; $c++) {
                    //echo $data[$c] . "<br />\n";
                    if($n1 == 1){
                        $adress1 = $data[$c];
                        $n1++;
                        
                    }elseif($n1==2){
                        $address_temp = $adress1.' '.$data[$c];
                        $n1++;
                    }elseif($n1 == 3){
                        $phone = $data[$c];
                        $inf_ctc = explode("+", $phone); 
                        $num_contact = '+'.$inf_ctc[1];
                        $nom = "Boutique Ria";

                        $network_name = "Ria";
                        $network_id = 173;
                        $email = 'ria.fr@ilink-app.com';
                        $code_super = "UZ8RdNwd9W";

                        $position = get_lat_lng(utf8_encode($address_temp));
                        $lat = $position['lat'];
                        $lng = $position['lng'];
                        $ville = $position['ville'];
                        $address = $position['adresse'];
                        $town_id = $this->user_model->getTownId($ville);

                        $exist = $this->user_model->checkPosition($lat,$lng);
                        if($exist == false){
                            $fields = array(
                                "tag" => "member",
                                "type" => "create_geolocated_user",
                                "lastname" => addslashes($nom),
                                "phone" => $num_contact,
                                "phone_transaction" => '+'.$default_num,
                                "address" => $address,
                                "lang" => "fr",
                                "email" => $email,
                                "password" => "1234",
                                "town" => ["id" => 3, "country_id" => 74, "name" => "France", "status" => 1],
                                "network" => ["id" => $network_id, "country_id" => 74, "name" => $network_name],
                                "latitude" => $lat,
                                "longitude" => $lng,
                                "category" => "geolocated",
                                "member" => $code_super
                            );
                        }else{

                        }
                        $default_num++;
                        //echo json_encode($fields)."<br /></p>\n";
                        $this->db->trans_begin();
                        $fields_string = http_build_query($fields);
                        $ch = curl_init();
                        curl_setopt($ch, CURLOPT_URL, $url);
                        curl_setopt($ch, CURLOPT_POST, 1);
                        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
                        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
                        $result = curl_exec($ch);
                        curl_close($ch);
                        if ($this->db->trans_status() === FALSE) {
                            $this->db->trans_rollback();
                        } else {
                            $this->db->trans_commit();
                        }
                        $n1 = 1;
                    }
                    
                    /*$data[0] = utf8_encode($data[0]);
                    $data[0] = str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] );
                    $inf = explode("+", str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] ));
                    $address = str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] ).' '.$inf[0];
                    $contact_correct = str_replace('-','',$inf[1]);
                    $num_contact ='+'.$contact_correct;
                    echo "<p> $num fields in line $row: <br /></p>\n";
                    echo $lastname."<br /></p>\n";
                    echo json_encode($data)."<br /></p>\n";*/
                }
                $row++;
            }
            fclose($handle);
        }

        /*$address = "";
        $position = get_lat_lng($address);
        $lat = $position['lat'];
        $lng = $position["lng"];*/


    }  
    
    public function campagne_geocoding_sigue(){

        function get_lat_lng($address){

            $address_encoding = htmlentities(urlencode($address));
            $api = "AIzaSyClYkvZ3XUD9EKZv3Z2BLzkr7wBoV2aQ98";

            $url = "https://maps.googleapis.com/maps/api/geocode/json?address=".$address_encoding."&key=".$api;
            $contents = file_get_contents($url); 
            if($contents !== false){
                $contents = utf8_encode($contents);
                $results = json_decode($contents);
                $data = $results->results;
                $lat = $data[0]->geometry->location->lat;
                $lng = $data[0]->geometry->location->lng;
                $adress = $data[0]->formatted_address;
                $ville = $data[0]->address_components[2]->long_name;
                $position["lat"] = $lat;
                $position["lng"] = $lng;
                $position["adresse"] = $address;
                $results = $data;

                if ($results[0]) {
                    $num = sizeof($results[0]->address_components);
                         for ($i=0; $i<$num; $i++) {
                             $num2 = sizeof($results[0]->address_components[$i]->types);
                            for ($b=0;$b<$num2;$b++) {
                                if ($results[0]->address_components[$i]->types[$b] == "locality") {
                                        $city= $results[0]->address_components[$i];
                                        break;
                                    }
                            }
                        }
                    } 
                $position["ville"] = $city->long_name;
                return $position;
            }else{
                return null;
            }
        }
        $url = 'https://ilink-app.com/mobilebackendtest/interacted/MembersAction.php';
        $row = 1;
        $nom = "";
        $address = "";
        $num_contact ="";
        $n1=1;
        $n2=0;
        $ok = true;
        $ct = 0;
        $default_num = 33900004000;
        $lastname = "";

        if (($handle = fopen("https://ilink-app.com/backofficeiLinkTest/application/controllers/SIGUE.csv", "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 10000, ";")) !== FALSE) {
                $num = count($data);
                $num = 1;
                //echo "<p> $num fields in line $row: <br /></p>\n";
                for ($c=0; $c < $num; $c++) {
                    //echo $data[$c] . "<br />\n";
                    if($n1 == 1){
                        $address_temp = $data[$c];
                        $n1++;
                        //echo $address_temp;
                    }elseif($n1 == 2){
                        $infos = utf8_encode($data[$c]);
                        $inf_ctc = explode("Tél : 0", str_replace( chr( 194 ) . chr( 160 ), ' ', $infos ));
                        $num_contact = '+33'.str_replace(' ','',$inf_ctc[1]);
                        $nom = "Sigue ".$inf_ctc[0];

                        $network_name = "Sigue";
                        $network_id = 186;
                        $email = 'sigue.fr@ilink-app.com';
                        $code_super = "utC4SjNUHe";

                        $position = get_lat_lng(utf8_encode($address_temp));
                        
                        $adress = $position["adresse"];
                        $lat = $position['lat'];
                        $lng = $position['lng'];

                        $fields = array(
                            "tag" => "member",
                            "type" => "create_geolocated_user",
                            "lastname" => addslashes($nom),
                            "phone" => $num_contact,
                            "phone_transaction" => '+'.$default_num,
                            "address" => $adress,
                            "lang" => "fr",
                            "email" => $email,
                            "password" => "1234",
                            "town" => ["id" => 3, "country_id" => 74, "name" => "France", "status" => 1],
                            "network" => ["id" => $network_id, "country_id" => 74, "name" => $network_name],
                            "latitude" => $lat,
                            "longitude" => $lng,
                            "category" => "geolocated",
                            "member" => $code_super
                        );
                        $default_num++;
                        //echo json_encode($fields)."<br /></p>\n";
                        $this->db->trans_begin();
                        $fields_string = http_build_query($fields);
                        $ch = curl_init();
                        curl_setopt($ch, CURLOPT_URL, $url);
                        curl_setopt($ch, CURLOPT_POST, 1);
                        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
                        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
                        $result = curl_exec($ch);
                        curl_close($ch);
                        if ($this->db->trans_status() === FALSE) {
                            $this->db->trans_rollback();
                        } else {
                            $this->db->trans_commit();
                        }
                        $n1 = 1;
                    }
                    
                    /*$data[0] = utf8_encode($data[0]);
                    $data[0] = str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] );
                    $inf = explode("+", str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] ));
                    $address = str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] ).' '.$inf[0];
                    $contact_correct = str_replace('-','',$inf[1]);
                    $num_contact ='+'.$contact_correct;
                    echo "<p> $num fields in line $row: <br /></p>\n";
                    echo $lastname."<br /></p>\n";
                    echo json_encode($data)."<br /></p>\n";*/
                }
                $row++;
            }
            fclose($handle);
        }

        /*$address = "";
        $position = get_lat_lng($address);
        $lat = $position['lat'];
        $lng = $position["lng"];*/


    }

    public function campagne_geocoding_dubai_western(){

        function get_lat_lng($address){

            $address_encoding = htmlentities(urlencode($address));
            $api = "AIzaSyClYkvZ3XUD9EKZv3Z2BLzkr7wBoV2aQ98";

            $url = "https://maps.googleapis.com/maps/api/geocode/json?address=".$address_encoding."&key=".$api;
            $contents = file_get_contents($url); 
            if($contents !== false){
                $contents = utf8_encode($contents);
                $results = json_decode($contents);
                $data = $results->results;
                if($data != []){
                    $lat = $data[0]->geometry->location->lat;
                    $lng = $data[0]->geometry->location->lng;
                    $adress = $data[0]->formatted_address;
                    $position["lat"] = $lat;
                    $position["lng"] = $lng;
                    $position["adresse"] = $address;
                    if ($data[0]) {
                        $num = sizeof($data[0]->address_components);
                             for ($i=0; $i<$num; $i++) {
                                 $num2 = sizeof($data[0]->address_components[$i]->types);
                                for ($b=0;$b<$num2;$b++) {
                                    if ($data[0]->address_components[$i]->types[$b] == "locality") {
                                            $city= $data[0]->address_components[$i];
                                            break;
                                        }
                                }
                            }
                        } 
                    if(isset($city->long_name)){
                        $position["ville"] = $city->long_name;
                    }else{
                        $position["ville"] = null;
                    }
                }else{
                    $position = null;
                }
                return $position;
            }else{
                return null;
            }
        }
        $url = 'https://ilink-app.com/mobilebackendtest/interacted/MembersAction.php';
        $row = 1;
        $nom = "";
        $address = "";
        $num_contact ="";
        $n1=1;
        $n2=2;
        $ok = true;
        $ct = 0;
        $default_num = 97100001001;
        $lastname = "";

        if (($handle = fopen("https://ilink-app.com/backofficeiLinkTest/application/controllers/western_union_dubai.csv", "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 10000, ";")) !== FALSE) {
                $num = count($data);
                $num = 1;
                //echo "<p> $num fields in line $row: <br /></p>\n";
                for ($c=0; $c < $num; $c++) {
                    //echo $data[$c] . "<br />\n";
                    if($n1 == 1){
                        $nom = $data[$c];
                        $n1++;
                        //echo $address_temp;
                    }elseif($n1 == 2){
                        $infos = utf8_encode($data[$c]);
                        $inf_ctc = explode("+", str_replace( chr( 194 ) . chr( 160 ), ' ', $infos ));
                        

                        $network_name = "Western Union";
                        $network_id = 94;
                        $email = 'westernunion.ae@ilink-app.com';
                        $code_super = "b4r2uqbyeN";
                        //$town_id = $this->user_model->getTownId($town_name);

                        $position = get_lat_lng(utf8_encode($inf_ctc[0]));
                        if($position == null){
                            //$n1 = 1;
                            //break;
                        }else{
                            
                        //echo $nom."<br /></p>\n";
                        //echo json_encode($inf_ctc)."<br /></p>\n";
                        }
                        $num_contact = '+'.str_replace('-','',$inf_ctc[1]);
                        
                        $adress = $position["adresse"];
                        $lat = $position['lat'];
                        $lng = $position['lng'];

                        echo 'Point '.$n2.' '.json_encode($position)."<br /></p>\n";
                        echo $nom."<br /></p>\n";
                        echo json_encode($inf_ctc)."<br /></p>\n";
                        $n2++;
                        
                        if($position['ville'] != null && $position['ville'] == 'Dubai'){
                            $fields = array(
                                "tag" => "member",
                                "type" => "create_geolocated_user",
                                "lastname" => addslashes($nom),
                                "phone" => $num_contact,
                                "phone_transaction" => '+'.$default_num,
                                "address" => $adress,
                                "lang" => "fr",
                                "email" => $email,
                                "password" => "1234",
                                "town" => ["id" => 13, "country_id" => 233, "name" => "United Arab Emirates", "status" => 1],
                                "network" => ["id" => $network_id, "country_id" => 74, "name" => $network_name],
                                "latitude" => $lat,
                                "longitude" => $lng,
                                "category" => "geolocated",
                                "member" => $code_super
                            );
                            $default_num++;
                            //echo json_encode($fields)."<br /></p>\n";
                            /*$this->db->trans_begin();
                            $fields_string = http_build_query($fields);
                            $ch = curl_init();
                            curl_setopt($ch, CURLOPT_URL, $url);
                            curl_setopt($ch, CURLOPT_POST, 1);
                            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
                            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
                            $result = curl_exec($ch);
                            curl_close($ch);
                            if ($this->db->trans_status() === FALSE) {
                                $this->db->trans_rollback();
                            } else {
                                $this->db->trans_commit();
                            }*/
                        }
                        $n1 = 1;
                    }
                    
                    /*$data[0] = utf8_encode($data[0]);
                    $data[0] = str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] );
                    $inf = explode("+", str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] ));
                    $address = str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] ).' '.$inf[0];
                    $contact_correct = str_replace('-','',$inf[1]);
                    $num_contact ='+'.$contact_correct;
                    echo "<p> $num fields in line $row: <br /></p>\n";
                    echo $lastname."<br /></p>\n";
                    echo json_encode($data)."<br /></p>\n";*/
                }
                $row++;
            }
            fclose($handle);
        }

        /*$address = "";
        $position = get_lat_lng($address);
        $lat = $position['lat'];
        $lng = $position["lng"];*/


    }

    public function campagne_geocoding_dubai_moneygram(){

        function get_lat_lng($address){

            $address_encoding = htmlentities(urlencode($address));
            $api = "AIzaSyClYkvZ3XUD9EKZv3Z2BLzkr7wBoV2aQ98";

            $url = "https://maps.googleapis.com/maps/api/geocode/json?address=".$address_encoding."&key=".$api;
            $contents = file_get_contents($url); 
            if($contents !== false){
                $contents = utf8_encode($contents);
                $results = json_decode($contents);
                $data = $results->results;
                if($data != []){
                    $lat = $data[0]->geometry->location->lat;
                    $lng = $data[0]->geometry->location->lng;
                    $adress = $data[0]->formatted_address;
                    $position["lat"] = $lat;
                    $position["lng"] = $lng;
                    $position["adresse"] = $address;
                    $position["ville"] = null;
                    if ($data[0]) {
                        $num = sizeof($data[0]->address_components);
                             for ($i=0; $i<$num; $i++) {
                                 $num2 = sizeof($data[0]->address_components[$i]->types);
                                for ($b=0;$b<$num2;$b++) {
                                    if ($data[0]->address_components[$i]->types[$b] == "locality") {
                                            $city= $data[0]->address_components[$i];
                                            if($city->long_name){
                                                echo $address. "<br />\n";
                                                echo $city->long_name. "<br />\n";
                                                $position["ville"] = $city->long_name;
                                            }
                                            break;
                                        }
                                }
                            }
                        } 
                    
                }else{
                    $position = null;
                }
                return $position;
            }else{
                return null;
            }
        }
        $url = 'https://ilink-app.com/mobilebackendtest/interacted/MembersAction.php';
        $row = 1;
        $nom = "";
        $address = "";
        $num_contact ="";
        $n1=1;
        $n2=1;
        $ok = true;
        $ct = 0;
        $default_num = 97100006621;
        $lastname = "";

        if (($handle = fopen("https://ilink-app.com/backofficeiLinkTest/application/controllers/moneygram_dubai.csv", "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 10000, ";")) !== FALSE) {
                $num = count($data);
                $num = 1;
                //echo "<p> $num fields in line $row: <br /></p>\n";
                for ($c=0; $c < $num; $c++) {
                    //echo $data[$c] . "<br />\n";
                    if($n1 == 1){
                        $nom = $data[$c];
                        $n1++;
                        //echo $address_temp;
                    }elseif($n1 == 2){
                        $adress_temp = str_replace( chr( 194 ) . chr( 160 ), ' ',utf8_encode($data[$c]));
                        $n1++;
                    }elseif($n1 == 3){
                        $adress_temp = $adress_temp.' '.str_replace( chr( 194 ) . chr( 160 ), ' ',utf8_encode($data[$c]));
                        $n1++;
                    }elseif($n1 == 4){
                        /*$infos = utf8_encode($data[$c]);
                        $inf_ctc = explode("+", str_replace( chr( 194 ) . chr( 160 ), ' ', $infos ));*/
                        

                        $network_name = "MoneyGram";
                        $network_id = 81;
                        $email = 'moneygram.ae@ilink-app.com';
                        $code_super = "ynSAWooVMT";
                        //$town_id = $this->user_model->getTownId($town_name);
                        echo json_encode(utf8_encode($adress_temp))."<br /></p>\n";

                        $position = get_lat_lng(utf8_encode($adress_temp));

                        echo json_encode(utf8_encode($position))."<br /></p>\n";
                        
                        if($position == null){
                            $n1 = 1;
                            break;
                        }else{
                            $num_contact = $data[$c];
                            if($num_contact == '971%'){
                                $num_contact = '+'.$num_contact;
                            }else{
                                $num_contact = '+971'.$num_contact;
                            }
                        }
                        
                        $adress = $position["adresse"];
                        $lat = $position['lat'];
                        $lng = $position['lng'];
                        

                        if($position['ville'] != null && $position['ville'] == 'Ajman'){
                            $fields = array(
                                "tag" => "member",
                                "type" => "create_geolocated_user",
                                "lastname" => addslashes($nom),
                                "phone" => $num_contact,
                                "phone_transaction" => '+'.$default_num,
                                "address" => $adress,
                                "lang" => "fr",
                                "email" => $email,
                                "password" => "1234",
                                "town" => ["id" => 31, "country_id" => 233, "name" => "United Arab Emirates", "status" => 1],
                                "network" => ["id" => $network_id, "country_id" => 233, "name" => $network_name],
                                "latitude" => $lat,
                                "longitude" => $lng,
                                "category" => "geolocated",
                                "member" => $code_super
                            );
                            //echo json_encode($fields)."<br /></p>\n";
                            $default_num++;
                        
                        /*$this->db->trans_begin();
                        $fields_string = http_build_query($fields);
                        $ch = curl_init();
                        curl_setopt($ch, CURLOPT_URL, $url);
                        curl_setopt($ch, CURLOPT_POST, 1);
                        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
                        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
                        $result = curl_exec($ch);
                        curl_close($ch);
                        if ($this->db->trans_status() === FALSE) {
                            $this->db->trans_rollback();
                        } else {
                            $this->db->trans_commit();
                        }*/
                        }
                        
                        $n1 = 1;
                    }
                    
                    /*$data[0] = utf8_encode($data[0]);
                    $data[0] = str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] );
                    $inf = explode("+", str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] ));
                    $address = str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] ).' '.$inf[0];
                    $contact_correct = str_replace('-','',$inf[1]);
                    $num_contact ='+'.$contact_correct;
                    echo "<p> $num fields in line $row: <br /></p>\n";
                    echo $lastname."<br /></p>\n";
                    echo json_encode($data)."<br /></p>\n";*/
                }
                $row++;
            }
            fclose($handle);
        }

        /*$address = "";
        $position = get_lat_lng($address);
        $lat = $position['lat'];
        $lng = $position["lng"];*/


    }

    public function campagne_geocoding_wari(){

        function get_lat_lng($address){

            $address_encoding = htmlentities(urlencode($address));
            $api = "AIzaSyClYkvZ3XUD9EKZv3Z2BLzkr7wBoV2aQ98";

            $url = "https://maps.googleapis.com/maps/api/geocode/json?address=".$address_encoding."&key=".$api;
            $contents = file_get_contents($url);
            if($contents !== false){
                $contents = utf8_encode($contents);
                $results = json_decode($contents);
                //echo json_encode($results->status). "<br />\n";
                if($results->status == "OK"){
                    $data = $results->results;
                    //echo json_encode($url). "<br />\n";
                    //echo json_encode($contents). "<br />\n";
                    $lat = $data[0]->geometry->location->lat;
                    $lng = $data[0]->geometry->location->lng;
                    $adress = $data[0]->formatted_address;
                    //$ville = $data[0]->address_components[2]->long_name;
                    $position["lat"] = $lat;
                    $position["lng"] = $lng;
                    $position["adresse"] = $address;
                    $results = $data;

                    /*if ($results[0]) {
                        $num = sizeof($results[0]->address_components);
                            for ($i=0; $i<$num; $i++) {
                                $num2 = sizeof($results[0]->address_components[$i]->types);
                                for ($b=0;$b<$num2;$b++) {
                                    if ($results[0]->address_components[$i]->types[$b] == "locality") {
                                            $city= $results[0]->address_components[$i];
                                            break;
                                        }
                                }
                            }
                    }*/ 
                    //$position["ville"] = $city->long_name;
                    return $position;
                }else{
                    return null;
                }
            }else{
                return null;
            }
        }
        $url = 'https://ilink-app.com/mobilebackendtest/interacted/MembersAction.php';
        $row = 1;
        $nom = "";
        $address = "";
        $num_contact ="";
        $n1=1;
        $n2=1;
        $ok = true;
        $ct = 0;
        $default_num = 33900006121;
        $lastname = "";

        if (($handle = fopen("https://ilink-app.com/backofficeiLinkTest/application/controllers/WARI_paris.csv", "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 10000, ";")) !== FALSE) {
                $num = count($data);
                $num = 1;
                //echo "<p> $num fields in line $row: <br /></p>\n";
                if($row>516){
                for ($c=0; $c < $num; $c++) {
                    //echo $data[$c] . "<br />\n";
                    
                    if($n1 == 1){
                        $nom = $data[$c];
                        $n1++;
                        
                    }elseif($n1==2){
                        $address_temp = $data[$c];
                        $n1++;
                    }elseif($n1 == 3){
                        $n1++;
                    }elseif($n1 == 4){
                        $phone = $data[$c];
                        $num_contact = str_replace(' ', '', str_replace('00', '+', $phone));

                        $network_name = "Wari";
                        $network_id = 160;
                        $email = 'wari.fr@ilink-app.com';
                        $code_super = "h6pLJn8tcb";

                        $position = get_lat_lng(utf8_encode($address_temp));
                        if($position == null){
                            $n1 = 1;
                            break;
                        }
                        $lat = $position['lat'];
                        $lng = $position['lng'];
                        //$ville = $position['ville'];
                        $address = $position['adresse'];
                        //$town_id = $this->user_model->getTownId($ville);

                            $fields = array(
                                "tag" => "member",
                                "type" => "create_geolocated_user",
                                "lastname" => addslashes($nom),
                                "phone" => $num_contact,
                                "phone_transaction" => '+'.$default_num,
                                "address" => $address,
                                "lang" => "fr",
                                "email" => $email,
                                "password" => "1234",
                                "town" => ["id" => 3, "country_id" => 74, "name" => "France", "status" => 1],
                                "network" => ["id" => $network_id, "country_id" => 74, "name" => $network_name],
                                "latitude" => $lat,
                                "longitude" => $lng,
                                "category" => "geolocated",
                                "member" => $code_super
                            );
                        
                        $default_num++;
                        //echo 'Groupe '.$n2."<br /></p>\n";
                        //echo json_encode($fields)."<br /></p>\n";
                        $n2++;
                        $this->db->trans_begin();
                        $fields_string = http_build_query($fields);
                        $ch = curl_init();
                        curl_setopt($ch, CURLOPT_URL, $url);
                        curl_setopt($ch, CURLOPT_POST, 1);
                        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
                        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
                        $result = curl_exec($ch);
                        curl_close($ch);
                        if ($this->db->trans_status() === FALSE) {
                            $this->db->trans_rollback();
                        } else {
                            $this->db->trans_commit();
                        }
                        $n1 = 1;
                    }
                    
                    /*$data[0] = utf8_encode($data[0]);
                    $data[0] = str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] );
                    $inf = explode("+", str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] ));
                    $address = str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] ).' '.$inf[0];
                    $contact_correct = str_replace('-','',$inf[1]);
                    $num_contact ='+'.$contact_correct;
                    echo "<p> $num fields in line $row: <br /></p>\n";
                    echo $lastname."<br /></p>\n";
                    echo json_encode($data)."<br /></p>\n";*/
                }
                }
                $row++;
            }
            fclose($handle);
        }

        /*$address = "";
        $position = get_lat_lng($address);
        $lat = $position['lat'];
        $lng = $position["lng"];*/


    }
    
    public function campagne_geocoding_express_union(){

        function get_lat_lng($address){

            $address_encoding = htmlentities(urlencode($address));
            $api = "AIzaSyClYkvZ3XUD9EKZv3Z2BLzkr7wBoV2aQ98";

            $url = "https://maps.googleapis.com/maps/api/geocode/json?address=".$address_encoding."&key=".$api;
            $contents = file_get_contents($url); 
            if($contents !== false){
                $contents = utf8_encode($contents);
                $results = json_decode($contents);
                $data = $results->results;
                $lat = $data[0]->geometry->location->lat;
                $lng = $data[0]->geometry->location->lng;
                $adress = $data[0]->formatted_address;
                $ville = $data[0]->address_components[2]->long_name;
                $position["lat"] = $lat;
                $position["lng"] = $lng;
                $position["adresse"] = $address;
                $results = $data;

                if ($results[0]) {
                    $num = sizeof($results[0]->address_components);
                         for ($i=0; $i<$num; $i++) {
                             $num2 = sizeof($results[0]->address_components[$i]->types);
                            for ($b=0;$b<$num2;$b++) {
                                if ($results[0]->address_components[$i]->types[$b] == "locality") {
                                        $city= $results[0]->address_components[$i];
                                        break;
                                    }
                            }
                        }
                    } 
                $position["ville"] = $city->long_name;
                return $position;
            }else{
                return null;
            }
        }
        $url = 'https://ilink-app.com/mobilebackendtest/interacted/MembersAction.php';
        $row = 1;
        $nom = "";
        $address = "";
        $num_contact ="";
        $n1=1;
        $n2=0;
        $ok = true;
        $ct = 0;
        $default_num = 33900007000;
        $lastname = "";

        if (($handle = fopen("https://ilink-app.com/backofficeiLinkTest/application/controllers/Express_union.csv", "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 10000, ";")) !== FALSE) {
                $num = count($data);
                //echo "<p> $num fields in line $row: <br /></p>\n";
                for ($c=0; $c < $num; $c++) {
                    //echo $data[$c] . "<br />\n";
                    if($n1 == 1){
                        $nom = $data[$c];
                        $n1++;
                        //echo $address_temp;
                    }elseif($n1 == 2){
                        $address_temp = $data[$c];
                        $n1++;
                    }elseif($n1 == 3){
                        $address_temp = $address_temp.', '.$data[$c];
                        $n1++;
                    }elseif($n1 == 4){
                        $num_contact = '+33'.str_replace(' ','',$data[$c]);

                        $network_name = "Express Union";
                        $network_id = 99;
                        $email = 'expressunion.fr@ilink-app.com';
                        $code_super = "fUdUbrsUcr";

                        $position = get_lat_lng(utf8_encode($address_temp));
                        $lat = $position['lat'];
                        $lng = $position['lng'];
                        $address = str_replace( chr( 194 ) . chr( 160 ), ' ', $position['adresse']);

                        $fields = array(
                            "tag" => "member",
                            "type" => "create_geolocated_user",
                            "lastname" => addslashes($nom),
                            "phone" => $num_contact,
                            "phone_transaction" => '+'.$default_num,
                            "address" => $address,
                            "lang" => "fr",
                            "email" => $email,
                            "password" => "1234",
                            "town" => ["id" => 3, "country_id" => 74, "name" => "France", "status" => 1],
                            "network" => ["id" => $network_id, "country_id" => 74, "name" => $network_name],
                            "latitude" => $lat,
                            "longitude" => $lng,
                            "category" => "geolocated",
                            "member" => $code_super
                        );
                        $default_num++;
                        //echo var_dump($fields)."<br /></p>\n";
                        $this->db->trans_begin();
                        $fields_string = http_build_query($fields);
                        $ch = curl_init();
                        curl_setopt($ch, CURLOPT_URL, $url);
                        curl_setopt($ch, CURLOPT_POST, 1);
                        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
                        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
                        $result = curl_exec($ch);
                        curl_close($ch);
                        if ($this->db->trans_status() === FALSE) {
                            $this->db->trans_rollback();
                        } else {
                            $this->db->trans_commit();
                        }
                        $n1 = 1;
                    }
                    
                    /*$data[0] = utf8_encode($data[0]);
                    $data[0] = str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] );
                    $inf = explode("+", str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] ));
                    $address = str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] ).' '.$inf[0];
                    $contact_correct = str_replace('-','',$inf[1]);
                    $num_contact ='+'.$contact_correct;
                    echo "<p> $num fields in line $row: <br /></p>\n";
                    echo $lastname."<br /></p>\n";
                    echo json_encode($data)."<br /></p>\n";*/
                }
                $row++;
            }
            fclose($handle);
        }

        /*$address = "";
        $position = get_lat_lng($address);
        $lat = $position['lat'];
        $lng = $position["lng"];*/


    }

    public function campagne_geocoding_juba_dubai(){

        function get_lat_lng($address){

            $address_encoding = htmlentities(urlencode($address));
            $api = "AIzaSyClYkvZ3XUD9EKZv3Z2BLzkr7wBoV2aQ98";

            $url = "https://maps.googleapis.com/maps/api/geocode/json?address=".$address_encoding."&key=".$api;
            $contents = file_get_contents($url); 
            if($contents !== false){
                $contents = utf8_encode($contents);
                $results = json_decode($contents);
                $data = $results->results;
                $lat = $data[0]->geometry->location->lat;
                $lng = $data[0]->geometry->location->lng;
                $adress = $data[0]->formatted_address;
                $ville = $data[0]->address_components[2]->long_name;
                $position["lat"] = $lat;
                $position["lng"] = $lng;
                $position["adresse"] = $address;
                $results = $data;

                if ($results[0]) {
                    $num = sizeof($results[0]->address_components);
                         for ($i=0; $i<$num; $i++) {
                             $num2 = sizeof($results[0]->address_components[$i]->types);
                            for ($b=0;$b<$num2;$b++) {
                                if ($results[0]->address_components[$i]->types[$b] == "locality") {
                                        $city= $results[0]->address_components[$i];
                                        break;
                                    }
                            }
                        }
                    } 
                $position["ville"] = $city->long_name;
                return $position;
            }else{
                return null;
            }
        }
        $url = 'https://ilink-app.com/mobilebackendtest/interacted/MembersAction.php';
        $row = 1;
        $nom = "";
        $address = "";
        $num_contact ="";
        $default_num = 97100002002;

        $network_name = "Juba";
        $network_id = 157;
        $email = 'juba.ae@ilink-app.com';
        $code_super = "PoGOE9Fn2J";

        $nom = 'INTERNATIONAL DEVELOPMENT EXCHANGE';
        $address = 'Sharjah dawara saaa, United Arab Emirates';
        $num_contact = '+971505547229';

        $position = get_lat_lng($address);
        $lat = $position['lat'];
        $lng = $position['lng'];
        $ville = $position['ville'];
        $address = $position['adresse'];
        //$town_id = $this->user_model->getTownId($ville);
        
        
            $fields = array(
                "tag" => "member",
                "type" => "create_geolocated_user",
                "lastname" => addslashes($nom),
                "phone" => $num_contact,
                "phone_transaction" => '+'.$default_num,
                "address" => addslashes($address),
                "lang" => "fr",
                "email" => $email,
                "password" => "1234",
                "town" => ["id" => 30, "country_id" => 233, "name" => "United Arab Emirates", "status" => 1],
                "network" => ["id" => $network_id, "country_id" => 233, "name" => $network_name],
                "latitude" => $lat,
                "longitude" => $lng,
                "category" => "geolocated",
                "member" => $code_super
            );
        $default_num++;
        //echo json_encode($fields)."<br /></p>\n";
        $this->db->trans_begin();
        $fields_string = http_build_query($fields);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
        $result = curl_exec($ch);
        curl_close($ch);
        if ($this->db->trans_status() === FALSE) {
            $this->db->trans_rollback();
        } else {
            $this->db->trans_commit();
        }

    }

    public function campagne_geocoding_dubai_ria(){

        function get_lat_lng($address){

            $address_encoding = htmlentities(urlencode($address));
            $api = "AIzaSyClYkvZ3XUD9EKZv3Z2BLzkr7wBoV2aQ98";

            $url = "https://maps.googleapis.com/maps/api/geocode/json?address=".$address_encoding."&key=".$api;
            $contents = file_get_contents($url); 
            if($contents !== false){
                $contents = utf8_encode($contents);
                $results = json_decode($contents);
                $data = $results->results;
                if($data != []){
                    $lat = $data[0]->geometry->location->lat;
                    $lng = $data[0]->geometry->location->lng;
                    $adress = $data[0]->formatted_address;
                    $position["lat"] = $lat;
                    $position["lng"] = $lng;
                    $position["adresse"] = $address;
                    $position["ville"] = null;
                    if ($data[0]) {
                        $num = sizeof($data[0]->address_components);
                             for ($i=0; $i<$num; $i++) {
                                 $num2 = sizeof($data[0]->address_components[$i]->types);
                                for ($b=0;$b<$num2;$b++) {
                                    if ($data[0]->address_components[$i]->types[$b] == "locality") {
                                            $city= $data[0]->address_components[$i];
                                            if($city->long_name){
                                                //echo $city->long_name. "<br />\n";
                                                $position["ville"] = $city->long_name;
                                            }
                                            break;
                                        }
                                }
                            }
                        } 
                    
                }else{
                    $position = null;
                }
                return $position;
            }else{
                return null;
            }
        }
        $url = 'https://ilink-app.com/mobilebackendtest/interacted/MembersAction.php';
        $row = 1;
        $nom = "";
        $address = "";
        $num_contact ="";
        $n1=1;
        $n2=1;
        $ok = true;
        $ct = 0;
        $default_num = 97100003010;
        $lastname = "";

        if (($handle = fopen("https://ilink-app.com/backofficeiLinkTest/application/controllers/ria_dubai.csv", "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 10000, ";")) !== FALSE) {
                $num = count($data);
                $num = 1;
                //echo "<p> $num fields in line $row: <br /></p>\n";
                for ($c=0; $c < $num; $c++) {
                    //echo $data[$c] . "<br />\n";
                    if($n1 == 1){
                        $nom = $data[$c];
                        $n1++;
                        //echo $address_temp;
                    }elseif($n1 == 2){
                        $infos = utf8_encode($data[$c]);
                        $adress1 = str_replace( chr( 194 ) . chr( 160 ), ' ', $infos );
                        $n1++;
                    }elseif($n1 == 3){
                        $infos = utf8_encode($data[$c]);
                        $adress_temp = $adress1.' '.str_replace( chr( 194 ) . chr( 160 ), ' ', $infos );
                        

                        $network_name = "Ria";
                        $network_id = 183;
                        $email = 'ria.ae@ilink-app.com';
                        $code_super = "z3GRV23fOV";
                        //$town_id = $this->user_model->getTownId($town_name);

                        $position = get_lat_lng($adress_temp);
                        $num_contact = '+'.$default_num;
                        $adress = $position["adresse"];
                        $lat = $position['lat'];
                        $lng = $position['lng'];
                        
                        if($position['ville'] != null && $position['ville'] == 'Sharjah'){
                            //echo "<p>fields number $n2: <br /></p>\n";
                            $n2++;
                            $fields = array(
                                "tag" => "member",
                                "type" => "create_geolocated_user",
                                "lastname" => addslashes($nom),
                                "phone" => $num_contact,
                                "phone_transaction" => '+'.$default_num,
                                "address" => $adress,
                                "lang" => "fr",
                                "email" => $email,
                                "password" => "1234",
                                "town" => ["id" => 30, "country_id" => 233, "name" => "United Arab Emirates", "status" => 1],
                                "network" => ["id" => $network_id, "country_id" => 74, "name" => $network_name],
                                "latitude" => $lat,
                                "longitude" => $lng,
                                "category" => "geolocated",
                                "member" => $code_super
                            );
                            $default_num++;
                            
                            //echo json_encode($fields)."<br /></p>\n";
                            $this->db->trans_begin();
                            $fields_string = http_build_query($fields);
                            $ch = curl_init();
                            curl_setopt($ch, CURLOPT_URL, $url);
                            curl_setopt($ch, CURLOPT_POST, 1);
                            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
                            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
                            $result = curl_exec($ch);
                            curl_close($ch);
                            if ($this->db->trans_status() === FALSE) {
                                $this->db->trans_rollback();
                            } else {
                                $this->db->trans_commit();
                            }
                        }
                        $n1 = 1;
                    }
                    
                    /*$data[0] = utf8_encode($data[0]);
                    $data[0] = str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] );
                    $inf = explode("+", str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] ));
                    $address = str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] ).' '.$inf[0];
                    $contact_correct = str_replace('-','',$inf[1]);
                    $num_contact ='+'.$contact_correct;
                    echo "<p> $num fields in line $row: <br /></p>\n";
                    echo $lastname."<br /></p>\n";
                    echo json_encode($data)."<br /></p>\n";*/
                }
                $row++;
            }
            fclose($handle);
        }

        /*$address = "";
        $position = get_lat_lng($address);
        $lat = $position['lat'];
        $lng = $position["lng"];*/


    }

    public function campagne_geocoding_dubai_small_world(){

        function get_lat_lng($address){

            $address_encoding = htmlentities(urlencode($address));
            $api = "AIzaSyClYkvZ3XUD9EKZv3Z2BLzkr7wBoV2aQ98";

            $url = "https://maps.googleapis.com/maps/api/geocode/json?address=".$address_encoding."&key=".$api;
            $contents = file_get_contents($url); 
            if($contents !== false){
                $contents = utf8_encode($contents);
                $results = json_decode($contents);
                $data = $results->results;
                if($data != []){
                    $lat = $data[0]->geometry->location->lat;
                    $lng = $data[0]->geometry->location->lng;
                    $adress = $data[0]->formatted_address;
                    $position["lat"] = $lat;
                    $position["lng"] = $lng;
                    $position["adresse"] = $address;
                    $position["ville"] = null;
                    if ($data[0]) {
                        $num = sizeof($data[0]->address_components);
                             for ($i=0; $i<$num; $i++) {
                                 $num2 = sizeof($data[0]->address_components[$i]->types);
                                for ($b=0;$b<$num2;$b++) {
                                    if ($data[0]->address_components[$i]->types[$b] == "locality") {
                                            $city= $data[0]->address_components[$i];
                                            if($city->long_name){
                                                //echo $address. "<br />\n";
                                                //echo $city->long_name. "<br />\n";
                                                $position["ville"] = $city->long_name;
                                            }
                                            break;
                                        }
                                }
                            }
                        } 
                    
                }else{
                    $position = null;
                }
                return $position;
            }else{
                return null;
            }
        }
        $url = 'https://ilink-app.com/mobilebackendtest/interacted/MembersAction.php';
        $row = 1;
        $nom = "";
        $address = "";
        $num_contact ="";
        $n1=1;
        $n2=1;
        $ok = true;
        $ct = 0;
        $default_num = 97100005301;
        $lastname = "";

        if (($handle = fopen("https://ilink-app.com/backofficeiLinkTest/application/controllers/small_world_dubai.csv", "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 10000, ";")) !== FALSE) {
                $num = count($data);
                $num = 1;
                //echo "<p> $num fields in line $row: <br /></p>\n";
                for ($c=0; $c < $num; $c++) {
                    //echo $data[$c] . "<br />\n";
                    if($n1 == 1){
                        $nom = $data[$c];
                        $n1++;
                        //echo $address_temp;
                    }elseif($n1 == 2){
                        $infos = utf8_encode($data[$c]);
                        $adress_temp = str_replace( chr( 194 ) . chr( 160 ), ' ', $infos );
                        $n1++;
                    }elseif($n1 == 3){
                        $infos = utf8_encode($data[$c]);
                        $inf = explode('/',$infos);
                        if(isset($inf[1])){
                            $infos = $inf[0];
                        }else{
                            
                        }
                        $num_contact = str_replace('-','',str_replace( 'CONTACT00', '+', $infos ));
                        $num_contact = str_replace( 'CONTACT04', '+9714', $num_contact);
                        $num_contact = str_replace( 'CONTACT06', '+9716', $num_contact);
                        $num_contact = str_replace( 'CONTACT+9', '+9', $num_contact);
                        $num_contact = str_replace( 'CONTACT9', '+9', $num_contact);
                        $num_contact = str_replace( 'CONTACTEZ LE00', '+', $num_contact);
                        $num_contact = str_replace( ' ', '', $num_contact);
                        

                        $network_name = "Small World";
                        $network_id = 146;
                        $email = 'smallworld.ae@ilink-app.com';
                        $code_super = "L6WtcGgYrh";
                        //$town_id = $this->user_model->getTownId($town_name);

                        $position = get_lat_lng($adress_temp);
                        $adress = $position["adresse"];
                        $lat = $position['lat'];
                        $lng = $position['lng'];
                        
                        if($position['ville'] != null && $position['ville'] == 'Ajman'){
                            //echo "<p>fields number $n2: <br /></p>\n";
                            //echo $position['ville']." <br /></p>\n";
                            $n2++;
                            $fields = array(
                                "tag" => "member",
                                "type" => "create_geolocated_user",
                                "lastname" => addslashes($nom),
                                "phone" => $num_contact,
                                "phone_transaction" => '+'.$default_num,
                                "address" => $adress,
                                "lang" => "fr",
                                "email" => $email,
                                "password" => "1234",
                                "town" => ["id" => 31, "country_id" => 233, "name" => "United Arab Emirates", "status" => 1],
                                "network" => ["id" => $network_id, "country_id" => 74, "name" => $network_name],
                                "latitude" => $lat,
                                "longitude" => $lng,
                                "category" => "geolocated",
                                "member" => $code_super
                            );
                            $default_num++;
                            
                            //echo json_encode($fields)."<br /></p>\n";
                            //var_dump($fields);
                            $this->db->trans_begin();
                            $fields_string = http_build_query($fields);
                            $ch = curl_init();
                            curl_setopt($ch, CURLOPT_URL, $url);
                            curl_setopt($ch, CURLOPT_POST, 1);
                            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
                            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
                            $result = curl_exec($ch);
                            curl_close($ch);
                            if ($this->db->trans_status() === FALSE) {
                                $this->db->trans_rollback();
                            } else {
                                $this->db->trans_commit();
                            }
                        }
                        $n1 = 1;
                    }
                    
                    /*$data[0] = utf8_encode($data[0]);
                    $data[0] = str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] );
                    $inf = explode("+", str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] ));
                    $address = str_replace( chr( 194 ) . chr( 160 ), ' ', $data[0] ).' '.$inf[0];
                    $contact_correct = str_replace('-','',$inf[1]);
                    $num_contact ='+'.$contact_correct;
                    echo "<p> $num fields in line $row: <br /></p>\n";
                    echo $lastname."<br /></p>\n";
                    echo json_encode($data)."<br /></p>\n";*/
                }
                $row++;
            }
            fclose($handle);
        }

        /*$address = "";
        $position = get_lat_lng($address);
        $lat = $position['lat'];
        $lng = $position["lng"];*/


    }

    public function anbg(){
        $url = "https://www.anbg.ga/Result-CT/traitement-recherche.php?recherche=FRA&choix=Pays";
        $contents = file_get_contents($url); 
        echo($contents);
    }

    
}
