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

class InsertGeolocated extends CI_Controller
{

    public function _construct()
    {
        parent::__construct();
        $this->load->model('insert_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, 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";
                        $fields = array(
                            "tag" => "member",
                            "type" => "create_geolocated_user",
                            "lastname" => addslashes($user_info[0]),
                            "phone" => "+226" . str_replace(' ', '', $user_info[3]),
                            "phone_transaction" => "+226" . str_replace(' ', '', $user_info[2]),
                            "address" => "",
                            "lang" => "fr",
                            "email" => "orangeburkina@ilink-app.com",
                            "password" => "1234",
                            "town" => ["id" => 11, "country_id" => 35, "name" => "Ouagadougou", "status" => 1],
                            "network" => ["id" => 34, "country_id" => 35, "name" => "Orange Burkina Faso"],
                            "latitude" => -1,
                            "longitude" => -1,
                            "category" => "Geolocated",
                            "member" => "lBWqyGbqFd"
                        );


                        $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";

                    }
                }
                $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);
        }
    }
}
?>
