function save()
 {
     //sementara dimatikan 18 jan 2016 dihidupkan lagi pas update version iOS
     //        if(Efiwebsetting::getData('checkOAuth')=='yes')
     //            IMBAuth::checkOAuth();
     $device_id = addslashes($_POST['device_id']);
     $type = addslashes($_POST['type']);
     if ($device_id == "" || $type == "") {
         $json['status_code'] = 0;
         echo json_encode($json);
         die;
     }
     $acc = isset($_POST['acc_id']) ? addslashes($_POST['acc_id']) : 0;
     $dn = new DeviceModel();
     $dnquery = new DeviceModel();
     // langkah 1 , device ID ada device type ada
     $arrs = $dnquery->getWhere("device_id = '{$device_id}' AND device_type = '{$type}'");
     $dn = $arrs[0];
     if ($dn->did == "") {
         $dn = new DeviceModel();
         $dn->device_id = $device_id;
         $dn->device_type = $type;
         $dn->acc_id = $acc;
         $dn->firstlogin = leap_mysqldate();
     } else {
         //kalau device id ada, acc di update
         $dn->load = 1;
         $dn->acc_id = $acc;
     }
     $dn->dev_active = 1;
     $dn->logindate = leap_mysqldate();
     if ($dn->save()) {
         $json['save_status'] = 1;
         //logged all device login 19 nov 2015 roy
         $logged = new DeviceLogger();
         $logged->log_acc_id = $dn->acc_id;
         $logged->log_date = leap_mysqldate();
         $logged->log_dev_id = $dn->device_id;
         $logged->log_dev_type = $dn->device_type;
         $logged->save();
     } else {
         $json['save_status'] = 0;
     }
     $json['status_code'] = 1;
     if ($_POST['no_echo']) {
     } else {
         echo json_encode($json);
         die;
     }
 }
 function save()
 {
     $device_id = addslashes($_POST['device_id']);
     $type = addslashes($_POST['type']);
     if ($device_id == "" || $type == "") {
         $json['status_code'] = 0;
         echo json_encode($json);
         die;
     }
     $acc = isset($_POST['acc_id']) ? addslashes($_POST['acc_id']) : 0;
     $dn = new DeviceModel();
     $dnquery = new DeviceModel();
     // langkah 1 , device ID ada device type ada
     $arrs = $dnquery->getWhere("device_id = '{$device_id}' AND device_type = '{$type}'");
     $dn = $arrs[0];
     if ($dn->did == "") {
         $dn = new DeviceModel();
         $dn->device_id = $device_id;
         $dn->device_type = $type;
         $dn->acc_id = $acc;
         $dn->firstlogin = leap_mysqldate();
     } else {
         //kalau device id ada, acc di update
         $dn->load = 1;
         $dn->acc_id = $acc;
     }
     $dn->logindate = leap_mysqldate();
     if ($dn->save()) {
         $json['save_status'] = 1;
         //logged all device login 19 nov 2015 roy
         $logged = new DeviceLogger();
         $logged->log_acc_id = $dn->acc_id;
         $logged->log_date = leap_mysqldate();
         $logged->log_dev_id = $dn->device_id;
         $logged->log_dev_type = $dn->device_type;
         $logged->save();
     } else {
         $json['save_status'] = 0;
     }
     $json['status_code'] = 1;
     echo json_encode($json);
     die;
 }
 public function createDummyData()
 {
     //        die("tidak active");
     ?>
     <h1>Create Device Logger</h1>
     <?php 
     $dev = new DeviceLogger();
     //        $dev->getByID(1);
     //        $type = array("android","ios");
     for ($x = 0; $x < 100; $x++) {
         //random if ada account atau tidak
         $adaAcc = rand(0, 9);
         if ($adaAcc > 6) {
             $accID = 0;
         } else {
             $acc = new LL_Account();
             $r = rand(0, 113);
             $arrAcc = $acc->getOrderBy("macc_id ASC LIMIT {$r},1");
             $accku = $arrAcc[0];
             $accID = $accku->macc_id;
         }
         //random type
         $type = "ios";
         $rtype = rand(0, 9);
         if ($rtype > 2) {
             $type = "android";
         }
         $minusan = 12620550 - $x * 100;
         //            $minusan = 75000;
         //random date
         $int = mt_rand(time() - $minusan, time());
         $log = new DeviceLogger();
         $log->log_acc_id = $accID;
         $log->log_dev_id = "dev_" . $accID;
         $log->log_dev_type = $type;
         $log->log_date = date("Y-m-d H:i:s", $int);
         echo $log->save();
         if ($accID) {
             $accl = new LL_AccountLogger();
             $accl->log_acc_id = $accID;
             $accl->log_date = date("Y-m-d H:i:s", $int);
             echo $accl->save();
         }
     }
 }
 function selanjutnya()
 {
     $app_id = addslashes($_POST['app_id']);
     $key = addslashes($_POST['key']);
     $device_id = addslashes($_POST['device_id']);
     $type = addslashes($_POST['type']);
     $lat = addslashes($_POST['lat']);
     $lng = addslashes($_POST['long']);
     $acc = isset($_POST['acc_id']) ? addslashes($_POST['acc_id']) : 0;
     $log_page_id = addslashes($_POST['page_id']);
     if ($device_id == "" || $type == "") {
         $json['status_code'] = 0;
         echo json_encode($json);
         die;
     }
     if ($app_id == "" || $key == "") {
         $json['status_code'] = 0;
         echo json_encode($json);
         die;
     } else {
         $app = new AppAccount();
         $app->getByID($app_id);
         //            $acc = new Account();
         //            $acc->getByID($app->app_client_id);
         if ($app->app_token != $key) {
             $json['status_code'] = 0;
             $json['status_message'] = "wrong key";
             echo json_encode($json);
             die;
         }
     }
     $logged = new DeviceLogger();
     $logged->log_acc_id = $acc;
     $logged->log_date = leap_mysqldate();
     $logged->log_dev_id = $device_id;
     $logged->log_dev_type = $type;
     $logged->log_app_id = $app_id;
     $logged->log_lat = $lat;
     $logged->log_lng = $lng;
     $logged->log_page_id = $log_page_id;
     $logged->save();
     $json['status_code'] = 1;
     echo json_encode($json);
     die;
 }
    public function loadDataApp($app_id)
    {
        if ($app_id == "") {
            die("Please insert App ID");
        }
        $app = new AppAccount();
        $app->getByID($app_id);
        //        pr($app);
        $acc = new Account();
        $acc->getByID($app->app_client_id);
        ?>
        <div class="row">

            <div class="col-md-4">
                <div class="small-box bg-aqua">
                    <div class="inner">
                        <h3><?php 
        echo $acc->admin_pulsa;
        ?>
</h3>
                        <p>Kuota Push Notifications</p>
                    </div>
                    <div class="icon">
                        <i class="ion ion-bag"></i>
                    </div>
                    <!--                            <a href="#" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>-->
                </div>
            </div>
        </div>

        <?php 
        $bulan = addslashes($_GET['m']);
        if ($bulan == "") {
            $bulan = date("F Y");
            $days_ago = date('Y-m-01');
            // hard-coded '01' for first day
            $days_now = date('Y-m-t');
        } else {
            $bulan = urldecode($bulan);
            $days_ago = date('Y-m-01', strtotime($bulan));
            // hard-coded '01' for first day
            $days_now = date('Y-m-t', strtotime($bulan));
        }
        $numberDays = cal_days_in_month(CAL_GREGORIAN, date('n', strtotime($bulan)), date('Y', strtotime($bulan)));
        $days_ago = date('Y-m-01', strtotime($bulan));
        // hard-coded '01' for first day
        $days_now = date('Y-m-01', strtotime('+1 month', strtotime($bulan)));
        //user acquisitions
        $ll = new LL_Account();
        $arrUserNew = $ll->getWhere("(macc_acquire_date BETWEEN '{$days_ago}' AND '{$days_now}')", "macc_acquire_date");
        //new device
        $dev = new DeviceModel();
        $arrDevNew = $dev->getWhere(" (firstlogin BETWEEN '{$days_ago}' AND '{$days_now}') ");
        //device active
        $dlog = new DeviceLogger();
        $arrDevActive = $dlog->getWhere(" (log_date BETWEEN '{$days_ago}' AND '{$days_now}') ");
        //        count($arrDevActive);
        //user active
        $acclog = new LL_AccountLogger();
        $arrUserActive = $acclog->getWhere("(log_date BETWEEN '{$days_ago}' AND '{$days_now}')");
        //        count($arrUserActive);
        $arrStats["New Users"] = $arrUserNew;
        $arrStats["Active Users"] = $arrUserActive;
        $arrStats["New Devices"] = $arrDevNew;
        $arrStats["Active Devices"] = $arrDevActive;
        $t = time();
        ?>
        <div class="row">
            <div class="col-md-12">
                <h1>
                    App Dashboard
                    <small><?php 
        echo $bulan;
        ?>
</small>
                </h1>
                <ol class="breadcrumb">
                    <li>
                        <?php 
        echo Lang::t('Select Timeframe');
        ?>
                    </li>
                    <li class="active">
                        <?php 
        $start = new DateTime('11 months ago');
        // So you don't skip February if today is day the 29th, 30th, or 31st
        $start->modify('first day of this month');
        $end = new DateTime();
        $interval = new DateInterval('P1M');
        $period = new DatePeriod($start, $interval, $end);
        ?>
                        <select id="apptimeselector_<?php 
        echo $t;
        ?>
">
                            <?php 
        foreach ($period as $dt) {
            ?>
                                <option value="<?php 
            echo urlencode($dt->format('F Y'));
            ?>
" <?php 
            if ($dt->format('F Y') == $bulan) {
                echo "selected";
            }
            ?>
>
                                    <?php 
            echo $dt->format('F Y') . "<br>";
            ?>
                                </option>
                            <?php 
        }
        ?>

                        </select>
                        <script>
                            $("#apptimeselector_<?php 
        echo $t;
        ?>
").change(function(){
                                var slc = $("#apptimeselector_<?php 
        echo $t;
        ?>
").val();
                                openLw("App","<?php 
        echo _SPPATH;
        ?>
BIWebProd/app?m="+slc,"fade");
                            });
                        </script>
                    </li>
                </ol>
            </div>
        </div>
        <div class="row">
            <div class="col-md-3">
                <div class="small-box bg-aqua">
                    <div class="inner">
                        <h3><?php 
        echo count($arrUserNew);
        ?>
</h3>
                        <p>New Users</p>
                    </div>
                    <div class="icon">
                        <i class="ion ion-bag"></i>
                    </div>
                    <!--                            <a href="#" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>-->
                </div>
            </div>
            <div class="col-md-3">
                <div class="small-box bg-green">
                    <div class="inner">

                        <h3><?php 
        echo count($arrUserActive);
        ?>
</h3>

                        <p>Active Users</p>
                    </div>
                    <div class="icon">
                        <i class="ion ion-stats-bars"></i>
                    </div>
                    <!--                            <a href="#" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>-->
                </div>
            </div>
            <div class="col-md-3">
                <div class="small-box bg-yellow">
                    <div class="inner">


                        <h3><?php 
        echo count($arrDevNew);
        ?>
</h3>

                        <p>New Devices</p>
                    </div>
                    <div class="icon">
                        <i class="ion ion-stats-bars"></i>
                    </div>
                    <!--                            <a href="#" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>-->
                </div>
            </div>
            <div class="col-md-3">
                <div class="small-box bg-red">
                    <div class="inner">
                        <?php 
        ?>

                        <h3><?php 
        echo count($arrDevActive);
        ?>
</h3>

                        <p>Active Devices</p>
                    </div>
                    <div class="icon">
                        <i class="ion ion-stats-bars"></i>
                    </div>
                    <!--                            <a href="#" class="small-box-footer">More info <i class="fa fa-arrow-circle-right"></i></a>-->
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <div class="box box-default">
                    <div class="box-header with-border">
                        <h3 class="box-title">User Stats</h3>

                        <div class="box-tools pull-right">
                            <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>
                            </button>
                            <button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
                        </div>
                    </div>
                    <div class="box-body chart-responsive">

                        <?php 
        //            $days_ago = date('Y-m-d', strtotime('-30 days', time()));
        //            $days_now =  date("Y-m-d");
        $data = self::getAppStats($arrStats, $days_ago, $days_now);
        ?>


                    </div>
                    <!-- /.box-body -->
                </div>
            </div>

        </div>
        <div class="row">

            <?php 
        $arrDType = array();
        foreach ($arrDevActive as $de) {
            $arrDType[$de->log_dev_type][] = $de;
        }
        $arrColor = array("#00a65a", "#00c0ef");
        foreach ($arrDType as $type => $arrDe) {
            $c = new Charting();
            $c->color = array_pop($arrColor);
            $c->label = $type;
            $c->value = count($arrDe);
            $totalanDevType[$type] = $c->value;
            $arrData[] = $c;
        }
        //                pr($arrData);
        ?>
<div class="col-md-6"><?php 
        Charting::morrisDonut("300px", $arrData, 1, "Device Type", "default");
        ?>

            </div>
            <div class="col-md-6">
                <div class="box box-default">
                    <div class="box-header with-border">
                        <h3 class="box-title">Average</h3>

                        <div class="box-tools pull-right">
                            <button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>
                            </button>
                            <button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
                        </div>
                    </div>
                    <div class="box-body chart-responsive">
                        <div class="average" style="padding: 20px;">
                            <?php 
        //                                pr($data);
        $maxUsers = max($data['New Users']);
        $maxActiveUsers = max($data['Active Users']);
        $maxNewDevice = max($data['New Devices']);
        $maxActiveDevice = max($data['Active Devices']);
        ?>
                            <p class="text-center">
                                <strong></strong>
                            </p>


                            <!-- /.progress-group -->
                            <div class="progress-group">
                                <span class="progress-text">New Users per Day</span>
                                <span class="progress-number"><b><?php 
        echo round(count($arrUserNew) / $numberDays, 2);
        ?>
</b>/<?php 
        echo $maxUsers;
        ?>
</span>

                                <div class="progress sm">
                                    <?php 
        $percent = ceil(count($arrUserNew) / $numberDays) / $maxUsers * 100;
        ?>
                                    <div class="progress-bar progress-bar-red" style="width: <?php 
        echo $percent;
        ?>
%"></div>
                                </div>
                            </div>
                            <!-- /.progress-group -->
                            <div class="progress-group">
                                <?php 
        $rata2 = round(count($arrUserActive) / $numberDays, 2);
        $percent = round($rata2 / $maxActiveUsers * 100);
        ?>
                                <span class="progress-text">Active Users per Day</span>
                                <span class="progress-number"><b><?php 
        echo $rata2;
        ?>
</b>/<?php 
        echo $maxActiveUsers;
        ?>
</span>

                                <div class="progress sm">
                                    <div class="progress-bar progress-bar-yellow" style="width: <?php 
        echo $percent;
        ?>
%"></div>
                                </div>
                            </div>
                            <!-- /.progress-group -->
                            <div class="progress-group">
                                <?php 
        $rata2 = round(count($arrDevNew) / $numberDays, 2);
        $percent = round($rata2 / $maxNewDevice * 100);
        ?>

                                <span class="progress-text">New Device per Day</span>
                                <span class="progress-number"><b><?php 
        echo $rata2;
        ?>
</b>/<?php 
        echo $maxNewDevice;
        ?>
</span>

                                <div class="progress sm">
                                    <div class="progress-bar progress-bar-red" style="width: <?php 
        echo $percent;
        ?>
%"></div>
                                </div>
                            </div>
                            <!-- /.progress-group -->
                            <!-- /.progress-group -->
                            <div class="progress-group">
                                <?php 
        $rata2 = round(count($arrDevActive) / $numberDays, 2);
        $percent = round($rata2 / $maxActiveDevice * 100);
        ?>

                                <span class="progress-text">Active Device per Day</span>
                                <span class="progress-number"><b><?php 
        echo $rata2;
        ?>
</b>/<?php 
        echo $maxActiveDevice;
        ?>
</span>

                                <div class="progress sm">
                                    <div class="progress-bar progress-bar-yellow" style="width: <?php 
        echo $percent;
        ?>
%"></div>
                                </div>
                            </div>
                            <!-- /.progress-group -->
                            <div class="progress-group">
                                <span class="progress-text">Android vs iOS</span>
                                <span class="progress-number"><b><?php 
        echo $totalanDevType['android'];
        ?>
</b>/<?php 
        echo $totalanDevType['android'] + $totalanDevType['ios'];
        ?>
</span>

                                <div class="progress sm">
                                    <?php 
        $percent = round($totalanDevType['android'] / ($totalanDevType['android'] + $totalanDevType['ios']) * 100);
        ?>
                                    <div class="progress-bar progress-bar-aqua" style="width: <?php 
        echo $percent;
        ?>
%"></div>
                                </div>
                            </div>
                            <!-- /.progress-group -->
                            <div class="progress-group">
                                <span class="progress-text">iOS vs Android</span>
                                <span class="progress-number"><b><?php 
        echo $totalanDevType['ios'];
        ?>
</b>/<?php 
        echo $totalanDevType['android'] + $totalanDevType['ios'];
        ?>
</span>

                                <div class="progress sm">
                                    <?php 
        $percent = round($totalanDevType['ios'] / ($totalanDevType['android'] + $totalanDevType['ios']) * 100);
        ?>
                                    <div class="progress-bar progress-bar-green" style="width: <?php 
        echo $percent;
        ?>
%"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- /.box-body -->
                </div>

            </div>
        </div>
        <style>
            ul.legend li{
                list-style: none;
                line-height: 30px;
            }
            ul.legend li div{
                float: left;
                margin-top: 10px;
                margin-right: 15px;
            }
            .legend-item{
                float: left;
                margin: 10px;
                line-height: 30px;
                margin-right: 5px;
            }
            .legend-item div{
                float: left;
                margin-top: 10px;
                margin-right: 5px;
            }
            .breadcrumb {
                padding: 8px 15px;
                margin-bottom: 20px;
                list-style: none;
                background-color: #f5f5f5;
                border-radius: 4px;
            }
            .breadcrumb {
                float: right;
                background: transparent;
                margin-top: 0;
                margin-bottom: 0;
                font-size: 12px;
                padding: 7px 5px;
                position: absolute;
                top: 15px;
                right: 10px;
                border-radius: 2px;
            }
        </style>

    <?php 
    }