public function pushLocation()
    {
        $identifier = time() . rand(0, 100);
        $l = new LocationModel();
        $arrCities = $l->getWhere("id_location!='0' GROUP BY city ORDER BY city ASC");
        ?>

        <div id="formgroup_city<?php 
        echo $identifier;
        ?>
" class="form-group">
            <label for="city<?php 
        echo $identifier;
        ?>
" class=" col-sm-2 control-label">City</label>

            <div class="col-sm-10">
                <select class="form-control" name="city<?php 
        echo $identifier;
        ?>
" id="city<?php 
        echo $identifier;
        ?>
">
                    <option value="-1" selected="">Select City</option>
                    <option value="0">ALL CITIES</option>
                    <?php 
        foreach ($arrCities as $city) {
            ?>
                        <option value="<?php 
            echo $city->city;
            ?>
"><?php 
            echo $city->city;
            ?>
</option>
                        <?php 
        }
        ?>

                </select>
                <span class="help-block" id="warning_city<?php 
        echo $identifier;
        ?>
"></span>
            </div>
            <div class="clearfix"></div>
        </div>

        <div id="formgroup_district<?php 
        echo $identifier;
        ?>
" class="form-group">
            <label for="district<?php 
        echo $identifier;
        ?>
" class=" col-sm-2 control-label">District</label>

            <div class="col-sm-10">
                <select disabled class="form-control" name="district<?php 
        echo $identifier;
        ?>
"
                        id="district<?php 
        echo $identifier;
        ?>
">
                    <option value="-1" selected="">Select District</option>

                </select>
                <span class="help-block" id="warning_district<?php 
        echo $identifier;
        ?>
"></span>
            </div>
            <div class="clearfix"></div>
        </div>

        <div id="formgroup_target_device<?php 
        echo $identifier;
        ?>
" class="form-group">
            <label for="target_device<?php 
        echo $identifier;
        ?>
" class=" col-sm-2 control-label">Target User(s)</label>

            <div class="col-sm-10">
                <div class="input-group">
                    <input type="text" name="target_device<?php 
        echo $identifier;
        ?>
" value="0"
                           id="target_device<?php 
        echo $identifier;
        ?>
" class="form-control" disabled readonly>
                    <span class='input-group-addon'>User(s)</span>
                    <span class="help-block" id="warning_target_device<?php 
        echo $identifier;
        ?>
"></span>
                </div>
            </div>
            <div class="clearfix"></div>
        </div>

        <div id="formgroup_user_ids<?php 
        echo $identifier;
        ?>
" class="form-group" hidden>
            <label for="user_ids<?php 
        echo $identifier;
        ?>
" class=" col-sm-2 control-label">User Ids</label>

            <div class="col-sm-10">
                <input type="text" name="user_ids<?php 
        echo $identifier;
        ?>
" value=""
                       id="user_ids<?php 
        echo $identifier;
        ?>
" class="form-control">
                <span class="help-block" id="warning_user_ids<?php 
        echo $identifier;
        ?>
"></span>
            </div>
            <div class="clearfix"></div>
        </div>

        <div id="formgroup_title<?php 
        echo $identifier;
        ?>
" class="form-group">
            <label for="title<?php 
        echo $identifier;
        ?>
" class=" col-sm-2 control-label">Title</label>

            <div class="col-sm-10">
                <input type="text" name="title<?php 
        echo $identifier;
        ?>
" value=""
                       id="title<?php 
        echo $identifier;
        ?>
" class="form-control">
                <span class="help-block" id="warning_title<?php 
        echo $identifier;
        ?>
"></span>
            </div>
            <div class="clearfix"></div>
        </div>

        <div id="formgroup_message<?php 
        echo $identifier;
        ?>
" class="form-group">
            <label for="message<?php 
        echo $identifier;
        ?>
" class=" col-sm-2 control-label">Message</label>

            <div class="col-sm-10">

                <textarea class="form-control" rows="5" id="message<?php 
        echo $identifier;
        ?>
"
                          name="message<?php 
        echo $identifier;
        ?>
"></textarea>
                <span class="help-block" id="warning_message<?php 
        echo $identifier;
        ?>
"></span>
            </div>
            <div class="clearfix"></div>
        </div>

        <div id="formgroup_image<?php 
        echo $identifier;
        ?>
" class="form-group">
            <label for="image<?php 
        echo $identifier;
        ?>
" class=" col-sm-2 control-label">Image</label>

            <div class="col-sm-10">
                <?php 
        $b = new \Leap\View\InputFoto("image{$identifier}", "image{$identifier}", "");
        $b->p();
        ?>
                <span class="help-block" id="warning_message<?php 
        echo $identifier;
        ?>
"></span>
            </div>
            <div class="clearfix"></div>
        </div>

        <div class="form-group">
            <div class="col-sm-12">
                <button type="button" class="btn btn-default" id="button_submit<?php 
        echo $identifier;
        ?>
">submit</button>
            </div>
        </div>

        <script>
            var targetDevice = $('#target_device<?php 
        echo $identifier;
        ?>
');
            var userIds = $('#user_ids<?php 
        echo $identifier;
        ?>
');
            var cities = $('#city<?php 
        echo $identifier;
        ?>
');
            var districts = $('#district<?php 
        echo $identifier;
        ?>
');

            cities.change(function () {
                districts.prop("disabled", false);
                var v = $(this).val();
                if (v == -1) {
                    targetDevice.val(0);
                    userIds.val("");
                    districts.prop("disabled", true);
                    return;
                } else if (v == 0) {
                    targetDevice.val(0);
                    userIds.val("");
                    districts.prop("disabled", true);
                }
                $.ajax({
                    method: "GET",
                    url: _sppath + "PushMR/getDistrictByCity",
                    data: {city: cities.val()}
                })
                    .done(function (msg) {
                        districts.empty();
                        districts.append($("<option></option>")
                            .attr("value", "-1")
                            .text("Select District"));
                        districts.append($("<option></option>")
                            .attr("value", "0")
                            .text("SELECT ALL"));
                        var json_obj = $.parseJSON(msg);
                        var status_code = json_obj.status_code;
                        if (status_code == 0) {
                            alert(json_obj.status_message);
                            return;
                        }
                        var results = json_obj.results;//parse JSON
                        var dist = results.district;
//                        console.log(results);
                        for (var i in dist) {
                            districts.append($("<option></option>")
                                .attr("value", dist[i].value)
                                .text(dist[i].text));
                        }
                        var user_count = json_obj.results.user_count;
                        var user_ids = json_obj.results.user_ids;
                        userIds.val(user_ids);
                        targetDevice.val(user_count);
                        districts.prop("disabled", results.length <= 0);
                    });

            });

            districts.change(function () {

                if (districts.val() == "-1") {
                    return;
                }
                $.ajax({
                    method: "GET",
                    url: _sppath + "PushMR/getUserCountInDistrict",
                    data: {
                        district: districts.val(),
                        city: cities.val()
                    }
                })
                    .done(function (msg) {
                        targetDevice.val(0);
                        var json_obj = $.parseJSON(msg);
                        var status_code = json_obj.status_code;
                        if (status_code == 0) {
                            alert(json_obj.status_message);
                            return;
                        }
                        var results = json_obj.results;//parse JSON

//                        console.log(results);
                        var user_count = json_obj.results.user_count;
                        targetDevice.val(user_count);
                        var user_ids = json_obj.results.user_ids;
                        userIds.val(user_ids);
                    });

            });

            $('#button_submit<?php 
        echo $identifier;
        ?>
').click(function () {
                var v<?php 
        echo $identifier;
        ?>
 = userIds.val();
                var tit<?php 
        echo $identifier;
        ?>
 = $('#title<?php 
        echo $identifier;
        ?>
').val();
                var msg<?php 
        echo $identifier;
        ?>
 = $('#message<?php 
        echo $identifier;
        ?>
').val();
                var img<?php 
        echo $identifier;
        ?>
 = $('#image<?php 
        echo $identifier;
        ?>
').val();
                if (v<?php 
        echo $identifier;
        ?>
 == ',' || v<?php 
        echo $identifier;
        ?>
 == '' || v<?php 
        echo $identifier;
        ?>
 == null) {
                    alert("No Target User");
                    return;
                }

                $.ajax({
                    method: "POST",
                    url: _sppath + "PushMR/pushByLocation",
                    data: {
                        id_users: v<?php 
        echo $identifier;
        ?>
,
                        title: tit<?php 
        echo $identifier;
        ?>
,
                        message: msg<?php 
        echo $identifier;
        ?>
,
                        image: img<?php 
        echo $identifier;
        ?>
                    }
                })
                    .done(function (msg) {
                        console.log(msg);
                        var json_obj = $.parseJSON(msg);//parse JSON

                        if (json_obj.status_code == 0) {
                            alert(json_obj.status_message);
                            return;
                        } else {
                            alert("Success");
                            var psh = json_obj.results.push;
                        }
                        lwrefresh("PushLocation");
                    });
            });
        </script>
        <?php 
    }
 public static function saveDistrictCityFromLatLng($lat, $lng, $accId, $isCashier)
 {
     $lat = trim($lat);
     $lng = trim($lng);
     if (self::IsNullOrEmptyString($lat) || self::IsNullOrEmptyString($lng)) {
         return;
     }
     $url = "https://maps.googleapis.com/maps/api/geocode/json?latlng={$lat},{$lng}&result_type=administrative_area_level_3&key=" . self::$geoCodingServerKey;
     $json = json_decode(file_get_contents($url), true);
     $addressComponents = $json['results'][0]['address_components'];
     if (count($addressComponents) < 2) {
         return;
     }
     $district = strtoupper($addressComponents[0]['long_name']);
     $city = strtoupper($addressComponents[1]['long_name']);
     $loc = new LocationModel();
     $arrLoc = $loc->getWhere("district='{$district}' AND city='{$city}'");
     if (count($arrLoc) == 0) {
         $l = new LocationModel();
         $l->district = $district;
         $l->city = $city;
         if ($isCashier) {
             $l->cashier_visit_count = 1;
             $l->user_visit_count = 0;
         } else {
             $l->cashier_visit_count = 0;
             $l->user_visit_count = 1;
         }
         $l->visit_count = 1;
         $l->save();
     } else {
         $l = new LocationModel();
         $l->getByID($arrLoc[0]->id_location);
         if ($isCashier) {
             $l->cashier_visit_count = $l->cashier_visit_count + 1;
         } else {
             $l->user_visit_count = $l->user_visit_count + 1;
         }
         $l->save();
     }
     if ($accId != 0 && !$isCashier) {
         $user = new UserModel();
         $user->getByID($accId);
         $user->last_lat = $lat;
         $user->last_long = $lng;
         $user->last_city = $city;
         $user->last_district = $district;
         if (Generic::IsNullOrEmptyString($user->latitude)) {
             $user->latitude = $lat;
         }
         if (Generic::IsNullOrEmptyString($user->longitude)) {
             $user->longitude = $lng;
         }
         if (Generic::IsNullOrEmptyString($user->district)) {
             $user->district = $district;
         }
         if (Generic::IsNullOrEmptyString($user->city)) {
             $user->city = $city;
         }
         $user->save();
     }
 }
 public function constraints()
 {
     $err = array();
     if (!isset($this->name)) {
         $err['name'] = Lang::t('Name cannot be empty');
     }
     if (!isset($this->address)) {
         $err['address'] = Lang::t('address cannot be empty');
     }
     if (!isset($this->district)) {
         $err['district'] = Lang::t('District cannot be empty');
     }
     if (!isset($this->city)) {
         $err['city'] = Lang::t('City cannot be empty');
     }
     if (!isset($this->email)) {
         $err['email'] = Lang::t('Email cannot be empty');
     }
     if (!isset($this->phone_no)) {
         $err['phone_no'] = Lang::t('phone no cannot be empty');
     }
     if (!isset($this->time_open)) {
         $err['time_open'] = Lang::t('Time open cannot be empty');
     }
     if (!isset($this->time_close)) {
         $err['time_close'] = Lang::t('Time Close cannot be empty');
     }
     if (!isset($this->halal)) {
         $err['halal'] = Lang::t('Halal cannot be empty');
     }
     //Set Default Values
     if (count($err) <= 0) {
         //SAVING NEW LOCATION TO Location DB
         $ll = new LocationModel();
         $arrLoc = $ll->getWhere("district='{$this->district}' AND city='{$this->city}'");
         if (count($arrLoc) <= 0) {
             $loc = new LocationModel();
             $loc->district = $this->district;
             $loc->city = $this->city;
             $loc->user_visit_count = 0;
             $loc->cashier_visit_count = 0;
             if (!Generic::IsNullOrEmptyString($this->district) && !Generic::IsNullOrEmptyString($this->city)) {
                 $loc->save();
             }
         }
         //MAKE id_categories always have 0
         if (Generic::IsNullOrEmptyString($this->id_categories)) {
             $this->id_categories;
         } else {
             $arrCats = explode(',', $this->id_categories);
             if (!in_array("0", $arrCats)) {
                 $arrCats[] = "0";
                 $this->id_categories = implode(',', $arrCats);
             }
         }
     }
     return $err;
 }