kustom visitor.inc (tambah informasi jumlah kunjungan)

Write here everything about development of SLiMS' additional features or plugins or add-ons from member of SLiMS users communities.

Moderators: wynerst, buitenzorg812, purwoko, sutriadi, ido, Dicarve, ridorido

Post Reply
User avatar
jushadi
Posts: 35
Joined: 27 Sep 2011, 16:29
Location: Makassar
Contact:

kustom visitor.inc (tambah informasi jumlah kunjungan)

Post by jushadi » 11 Mar 2014, 10:01

:|

Code: Select all

<?php
/**
 *
 * Visitor Counter
 * Copyright (C) 2010 Arie Nugraha (dicarve@yahoo.com)
 * Modified By Eddy Subratha (eddy.subratha@gmail.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * https://www.facebook.com/zaemakhrus {
	add cookie check double insert data visitor
	add message double
 }
 * https://www.facebook.com/sazekage {
	fix $photo photo.png to person.png
	fix // check member ID or name div to bootstrap style
	fix <div class="error"> to <div class="alert alert-danger">
	add info pengunjung tiap hari
	add info sudah berkunjung berapa kali tiap member
	add info total kunjungan tercatat
	}
 */

// be sure that this file not accessed directly
if (!defined('INDEX_AUTH')) {
    die("can not access this file directly");
} elseif (INDEX_AUTH != 1) {
    die("can not access this file directly");
}

$allowed_counter_ip = array(
    '127.0.0.1'
);
$remote_addr        = $_SERVER['REMOTE_ADDR'];
$confirmation       = 0;

foreach ($allowed_counter_ip as $ip) {
    // change wildcard
    $ip = preg_replace('@\*$@i', '.', $ip);
    if ($ip == $remote_addr || $_SERVER['HTTP_HOST'] == 'localhost' || preg_match("@$ip@i", $ip)) {
        $confirmation = 1;
    }
}

if (!$confirmation) {
    header("location: index.php");
}

// start the output buffering for main content
ob_start();

define('INSTITUTION_EMPTY', 11);

if (isset($_POST['counter'])) {
    if (trim($_POST['memberID']) == '') {
        die();
    }
    $member_name = 'Guest';
    $photo       = 'person.png';
    $expire      = 0;

    // sleep for a while
    sleep(2);
    /**
     * Insert counter data to database
     */
    function setCounter($str_member_ID)
    {
        global $dbs, $member_name, $photo, $expire, $total, $jumlah, $baris;
        // check if ID exists
        $str_member_ID = $dbs->escape_string($str_member_ID);
        $_q            = $dbs->query("SELECT member_id,member_name,member_image,inst_name, IF(TO_DAYS(NOW())>TO_DAYS(expire_date), 1, 0) AS is_expire FROM member WHERE member_id='$str_member_ID'");
        // if member is already registered
        if ($_q->num_rows > 0) {
            
            $_p = $_q->fetch_assoc();
            if ($_p['is_expire'] == 1) {
                $expire = 1;
            }
            
            $member_id    = $_p['member_id'];
            $member_name  = $_p['member_name'];
            $member_name  = preg_replace("/'/", "\'", $member_name);
            $photo        = trim($_p['member_image']) ? trim($_p['member_image']) : 'person.png';
            $_institution = $dbs->escape_string(trim($_p['inst_name'])) ? $dbs->escape_string(trim($_p['inst_name'])) : null;
            
            $_checkin_date = date('Y-m-d H:i:s');
			
            $_h     = $dbs->query("SELECT member_name FROM visitor_count");
            $jumlah = $_h->num_rows;
            $jumlah = $jumlah + 1; //jumlah pengunjung https://www.facebook.com/sazekage
			                      
            $_a    = $dbs->query("SELECT member_name FROM visitor_count WHERE checkin_date BETWEEN TIMESTAMP( CURRENT_DATE ) + INTERVAL 1 HOUR AND TIMESTAMP( CURRENT_DATE ) + INTERVAL 23 HOUR");
            $baris = $_a->num_rows;
            $baris = $baris + 1; //pengunjung hari ini
            
            $_d            = $dbs->query("SELECT m.member_name FROM member AS m LEFT JOIN visitor_count AS v ON m.member_id = v.member_id WHERE v.member_id ='$str_member_ID'");
            $total         = $_d->num_rows; 
            $total         = $total + 1; //total kunjungan anggota
            
            $_i = $dbs->query("INSERT INTO visitor_count (member_id, member_name, institution, checkin_date) VALUES ('$member_id', '$member_name', '$_institution', '$_checkin_date')");
            
            // setting cookie visitor
            setcookie("visit_id", "$member_id", time() + 7200); //2 jam interval
            setcookie("visit_nama", "$member_name", time() + 7200); //2 jam interval
            
        } else {
            // non member
            $_p           = $_q->fetch_assoc();
            $member_name   = $dbs->escape_string(trim($_POST['memberID']));
            $_institution  = $dbs->escape_string(trim($_POST['institution']));
            $_checkin_date = date('Y-m-d H:i:s');
            if (!$_institution) {
                return INSTITUTION_EMPTY;
            } else {
            $_h     = $dbs->query("SELECT member_name FROM visitor_count");
            $jumlah = $_h->num_rows;
            $jumlah = $jumlah + 1; //jumlah pengunjung 
			                      
            $_a    = $dbs->query("SELECT member_name FROM visitor_count WHERE checkin_date BETWEEN TIMESTAMP( CURRENT_DATE ) + INTERVAL 1 HOUR AND TIMESTAMP( CURRENT_DATE ) + INTERVAL 23 HOUR");
            $baris = $_a->num_rows;
            $baris = $baris + 1; //pengunjung hari ini
			
            $_d            = $dbs->query("SELECT member_name FROM visitor_count  WHERE member_name = '$member_name' AND institution = '$_institution'");
            $total         = $_d->num_rows;
            $total         = $total + 1;
               
            $_i = $dbs->query("INSERT INTO visitor_count (member_name, institution, checkin_date) VALUES ('$member_name', '$_institution', '$_checkin_date')");
                
                // setting cookie visitor
                setcookie("visit_name", "$member_name", time() + 7200); //2 jam interval
                setcookie("inst_name", "$_institution", time() + 7200); //2 jam interval
            }
        }
        return true;
    }
    
    
    $memberID    = trim($_POST['memberID']);
    $institution = trim($_POST['institution']);
    
    // get cookie https://www.facebook.com/zaemakhrus
    $visit_id   = $_COOKIE['visit_id'];
    $visit_name = $_COOKIE['visit_name'];
    $visit_nama = $_COOKIE['visit_nama']; //https://www.facebook.com/sazekage
    $inst_name  = $_COOKIE['inst_name']; //https://www.facebook.com/sazekage
    
    if ($memberID === $visit_id) {
        echo '<div class="alert alert-warning">' . __('Member: Maaf  ' . $visit_nama . ', anda sudah memasukan data sebelumnya.') . '</div><span id="memberImage" src="images/persons/' . urlencode($photo) . '"></span>'; //https://www.facebook.com/sazekage
    } elseif ($memberID === $visit_name && $institution === $inst_name) {
        echo '<div class="alert alert-warning">' . __('Non Member: Maaf ' . $visit_name . ', anda sudah memasukan data sebelumnya.') . '</div><span id="memberImage" src="images/persons/' . urlencode($photo) . '"></span>'; //https://www.facebook.com/sazekage
    } else {
        $counter = setCounter($memberID);
    }
    
    if ($counter === true) {
        echo '<div class="alert alert-info">' . __('Anda pengunjung ke ' . $baris . ' hari ini, Anda telah berkunjung sebanyak ' . $total . ' kali, Jumlah pengunjung hingga hari ini ' . $jumlah . ' orang') . '</div>
  <div class="alert alert-success">' . __($member_name . ' thank you for inserting your data to our visitor log') . '.</div>
  <span id="memberImage" src="images/persons/' . urlencode($photo) . '"></span>';
        if ($expire) {
            echo '<div class="alert alert-danger">' . __('Your membership already EXPIRED, please renew/extend your membership immediately') . '</div>';
        }
        
    } else if ($counter === INSTITUTION_EMPTY) {
        echo '<div class="alert alert-warning">' . __('Sorry, Please fill institution field if you are not library member') . '</div>';
    } else {
        echo '<div class="alert alert-danger">' . __('Error inserting counter data to database!') . '</div>';
    }
    exit();
}

?>
<div id="masking"></div>
    <div class="container">
        <div class="row">
            <div class="span4 offset4">
                <div class="visitor">
                    <h4><?php
echo __('Visitor Counter');
?></h4>
                    <hr/>
                    <div class="info"><?php
echo __('Please insert your library member ID otherwise your full name instead');
?></div>
                    <hr/>
                    <img id="visitorCounterPhoto" src="./images/persons/person.png" class="photo img-circle" />
                    <hr/>
                    <div id="counterInfo">&nbsp;</div>
                    <form action="index.php?p=visitor" name="visitorCounterForm" id="visitorCounterForm" method="post" class="form-inline">
                        <div class="control-group">
                            <label class="control-label"><?php
echo __('Member ID');
?> / <?php
echo __('Visitor Name');
?></label>
                            <div class="controls">
                                <input type="text" name="memberID" id="memberID"  class="input-block-level" />
                            </div>
                        </div>

                        <div class="control-group">
                            <label class="control-label"><?php
echo __('Institution');
?> / <?php
echo __('Visitor Name');
?></label>
                            <div class="controls">
                                <input type="text" name="institution" id="institution"  class="input-block-level" />
                            </div>
                        </div>

                        <div class="control-group">
                            <div class="controls">
                                <input type="submit" id="counter" name="counter" value="<?php
echo __('Add');
?>" class="btn-block btn btn-primary" />
                            </div>
                        </div>

                        <div class="marginTop" ></div>
                    </form>
                    <hr/>
                    <small>Powered By SLiMS 7 Cendana</small>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
jQuery(document).ready( function() {

// give focus to first field
jQuery('#memberID').focus();

var visitorCounterForm = jQuery('#visitorCounterForm');

// AJAX counter error handler
visitorCounterForm.ajaxError( function() {
    alert('Error inserting counter data to database!');
    jQuery(this).enableForm().find('input[type=text]').val('');
    jQuery('#memberID').focus();
});

// AJAX counter complete handler
visitorCounterForm.ajaxComplete( function() {
    jQuery(this).enableForm().find('input[type=text]').val('');
    var memberImage = jQuery('#memberImage');
    if (memberImage) {
// update visitor photo
var imageSRC = memberImage.attr('src'); memberImage.remove();
jQuery('#visitorCounterPhoto')[0].src = imageSRC;
}
jQuery('#memberID').focus();
});

// register event
visitorCounterForm.submit(function(evt) {
    evt.preventDefault();
// check member ID or name
if (jQuery.trim(jQuery('#memberID').val()) == '') {
    jQuery('#counterInfo').html('<div class="alert alert-warning">Please fill your member ID or name</div>');
    return false;
}
var theForm = jQuery(this);
var formAction = theForm.attr('action');
var formData = theForm.serialize();
formData += '&counter=true';
// block the form
theForm.disableForm();
jQuery('#counterInfo').css({'display': 'block'}).html('PLEASE WAIT...');
// create AJAX request for submitting form
jQuery.ajax(
    { url: formAction,
        type: 'POST',
        async: false,
        data: formData,
        cache: false,
        success: function(respond) {
          jQuery('#counterInfo').html(respond);
          // reset counter
          setTimeout(function() { jQuery('#visitorCounterPhoto').attr('src', './images/persons/person.png');
            jQuery('#counterInfo').html('&nbsp;'); }, 5000);
        }
    });
});

});
</script>

<?php
// main content
$main_content = ob_get_clean();
// page title
$page_title   = $sysconf['library_name'] . ' :: Visitor Counter';
require_once $sysconf['template']['dir'] . '/' . $sysconf['template']['theme'] . '/login_template.inc.php';
exit();
Attachments
add info1.png
add info1.png (27.4 KiB) Viewed 3677 times
fix_member_img2.png
fix_member_img2.png (37.12 KiB) Viewed 3677 times
custom visitor.inc.tar
(171 KiB) Downloaded 451 times
Read!

Post Reply