bernicegolomo / leaveplan

Automatically exported from code.google.com/p/leaveplan
0 stars 0 forks source link

code update and optimization. #1

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
heres a little update for your plugin,  it uses css for the time off instead of 
static html on each element

it also re-uses 1 sql connection instead of making a 3 minimum, will help for 
very busy servers.

also there was a bug in the display of the selected date at the top, i have 
changed it so it uses what is selected instead of the current year month.   

and i closed the <option> tags rolling around the month pulldown.

<?php
/*************************************************************************/
/*               (c) ACTIVE SPACE TECHNOLOGIES  2010                     */
/*************************************************************************/

/* LEAVE PLAN for OrangeHRM 
 *
 * This script allows to have a public LEAVE PLAN for all your employees
 * using the OrangeHRM software. Tested with OrangeHRM 2.5
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3.0 of the License.
 * 
 * This library 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
 * Lesser General Public License for more details.
 *
 *
 * @category absences
 * @package OrangeHRM
 * @author André Tenreiro (andre.tenreiro@activespacetech.com)
 * @copyright (c) 2010 Active Space Technologies
 * @license http://www.gnu.org/licenses/gpl-3.0.txt (GPL 3.0)
 * @version 1.0
 * @link http://www.activespacetech.com
 * @since Class available since Release 1.0
 */

/*****************/
/* Configuration */
/*****************/

//Database information

$db_host = "hostname";
$db_user = "dbuser";
$db_pwd = "dbpassword";
$db_name = "databasename";
$db_port = 3306;

//Logo (optional) - Set empty has an empty string for none  (examples: 
"logo.png", "img/logo.png", "http://my.url.com/logo.png")
$logoLink = "";

/******************************/
/* DONT EDIT BELLOW THIS LINE */
/******************************/

    // * create $db database connection for global use below.
global $db;
$db = mysql_connect($db_host, $db_user, $db_pwd) or die(mysql_error());
$db = mysql_select_db($db_name) or die(mysql_error());

// what year have they selected, set it to this year if none selected
if (isset($_GET['year'])) $year = $_GET['year'];
else $year = date("Y");

// what month have they selected, set it to this month if none selected
if (isset($_GET['month']))  $month = $_GET['month'];
else $month = date("m");

$date_aux = $year . "-" . $month;
$seeDate = date("Y-m", strtotime($date_aux));   

$numDays = monthDays($month, $year);        // how many days in this month?

    // build array of months
$auxmonth = array (1=>"January", "February", "March", "April", "May", "June", 
"July", "August", "September", "October", "November", "December");
?>
<HTML>
<HEAD><TITLE>Vacation Plan</TITLE>
<script type="text/javascript" language="JavaScript">
function sort(form) {
    var Page = "?";
    var month = form.listMonth.selectedIndex+1;     
    var iyear = form.listYear.selectedIndex;
    var year = form.listYear.options[iyear].value;  
    var URL = Page + "month=" + month + "&year=" + year;
    window.location = URL;  
    return false;
}
</script>
<style type="text/css">
<!--
.style2 {font-size: 12px; }

/ * color block backgrounds  */
.holiday { background-color: #0000FF; }
.weekend { background-color: #999; }
.absense_full { background-color: #FF0000;}
.absense_partial_m {background-color: #FFFF00; text-align: center;}
.absense_partial_a {background-color: $FFFF00; text-align: center;}
.default { background-color: #00FF00; }
-->
</style>
</HEAD>
<BODY>

<?php if (!empty($logoLink))    
    echo "<img src='$logoLink'/>";
?>
<center>
  <div id="sort" style="padding-right: 10%">
    <form id="frmSort" name="frmSort" onSubmit="return sort(this); return false;">
      <?php echo "<h2>". $auxmonth[$month] . " " . $year . "</h2>"; ?>
      <div align="right">
      <strong>Month</strong>:
        <select name="listMonth">
            <?php
            foreach ($auxmonth as $key => $val) {
                echo "<option val=\"".$key."\"";
                if ($key == $month) {
                    echo " selected>".$val."\n";
                } else {
                    echo ">".$val."\n";
                }
                echo "</option>";
            }
            ?>
        </select>
      <img src="img/pixel.gif" width="1" height="1" /><img src="img/pixel.gif" width="1" height="1" /> <img src="img/pixel.gif" width="1" height="1" /><img src="img/pixel.gif" width="1" height="1" /> <img src="img/pixel.gif" width="1" height="1" /><strong>Year</strong>:
      <select name="listYear" id="listYear">
        <?php getYears($year); ?>
      </select>
      <img src="img/pixel.gif" width="1" height="1" /> <img src="img/pixel.gif" width="1" height="1" />
      <input type="submit" name="Submit" value="Select" />
    </form>
</div>
<div id="table">
<table width="90%" border="0" cellspacing="1" cellpadding="1">
    <tr>
        <td >&nbsp;</td>
        <? 
        for($i = 1; $i <= $numDays; $i++) {
             echo "<td bgcolor=\"#FFFFFF\" width=\"15\"><div align=\"center\"><strong>".$i."</strong></div></td>";
            }
        ?>
    </tr>

        <?

        $sql = "SELECT emp.*
                FROM hs_hr_employee AS emp
                INNER JOIN hs_hr_users AS users ON emp.emp_number = users.emp_number
                WHERE (users.status = 'Enabled')
                ORDER BY emp.emp_firstname ASC";

        $result = mysql_query($sql, $db)or die(mysql_error());  
        $result_count = mysql_num_rows($result);

        $even = 1;

        while ($row = mysql_fetch_array( $result ) ) {
            $employee_id = $row['employee_id'];
            $employee_num = $row['emp_number'];
            $employee_firstname = $row['emp_firstname'];
            $employee_lastname = $row['emp_lastname'];

            echo "<tr>";

            if ( $even % 2) 
                echo " <td bgcolor=\"#FFFFFF\" width=\"20%\"><div align=\"right\">".$employee_firstname . " " . $employee_lastname . "</div></td>";
            else
                echo " <td bgcolor=\"#FFFFFF\" width=\"10%\"><div align=\"right\">".$employee_firstname . " " . $employee_lastname . "</div></td>";

            $even++;

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

                $day = checkDay($employee_num, $i, $month, $year);

                switch($day) {              
                    case 'holiday':
                        echo ' <td class="holiday"></td>';
                        break;
                    case 'weekend':
                        echo ' <td class="weekend"></td>';
                        break;
                    case 'absence_full':
                        echo ' <td class="absense_full"></td>';
                        break;
                    case 'absence_partial_m':
                        echo ' <td class="absense_partial_m">M</td>';
                        break;
                    case 'absence_partial_a':
                        echo ' <td class="absnese_partial_a">A</td>';
                        break;
                    default:
                        echo ' <td class="default"></td>';
                        break;              
                }//switch
            }

            echo '</tr>';
        }//while

        ?>

</table>
</div>  
</center>   
<br/>
<div style="padding-left: 5%">
<table width="25%" border="0" cellspacing="1" cellpadding="1">
  <tr>
    <td class="default">&nbsp;</td>
    <td><div align="left" class="style2">Available</div></td>
  </tr>
  <tr>
    <td width="11%" class="absense">&nbsp;</td>
    <td width="89%"><div align="left" class="style2">Absence</div></td>
  </tr>
  <tr>
    <td class="weekend">&nbsp;</td>
    <td><div align="left" class="style2">WeekEnd</div></td>
  </tr>
  <tr>
    <td class="holiday">&nbsp;</td>
    <td><div align="left" class="style2">Public Holiday </div></td>
  </tr>
  <tr>
    <td class="absense_partial_m">M</td>
    <td><div align="left" class="style2">Partial Absence (Morning) </div></td>
  </tr>
  <tr>
    <td class="absense_partial_a">A</td>
    <td><span class="style2">Partial Absence (Afternoon) </span></td>
  </tr>
</table>
</div>
<br/>
<div align="center">© 2010 - <a 
href="http://code.google.com/p/leaveplan">Leave Plan Plugin</a> for OrangeHRM - 
Under GPL</div>
</BODY>
</HTML>

<?php
// ***********    fucntions!!     //
    // get Years
function getYears($year) {
    if(!isset($year)) $year = date("Y");

    $sql = "SELECT DISTINCT(EXTRACT(Year FROM leave_date)) as dateyear FROM hs_hr_leave WHERE 1 ORDER BY leave_date DESC;";
    $result = mysql_query($sql, $db) or die(mysql_error());  

    while ($row = mysql_fetch_array($result )) {
        if ($row['dateyear'] == $year) {
            echo "<option value=\"".$row['dateyear']."\" selected>".$row['(EXTRACT(Year FROM leave_date))']."</option><br>";
        } else {
            echo "<option value=\"".$row['dateyear']."\">".$row['(EXTRACT(Year FROM leave_date))']."</option><br>";
        }
    }
}

    /* Number of days in a month/Year */    
function monthDays($month, $year) {     return date("t", strtotime($year . "-" . 
$month . "-01"));  }
function weekDay($date) {               return date("D", strtotime($date));}

    // what is this persons status for this day?
function checkDay($employee_id, $i, $month, $year) {

    $date = $year . "-" . $month . "-" . $i;
    $sql = "SELECT * FROM hs_hr_holidays WHERE date = '$date';";

    $result = mysql_query($sql, $db) or die(msysql_error());  
    $result_count = mysql_num_rows($result);

    if ($result_count > 0) return "holiday";

    if( ( weekDay($date) == "Sat") || ( weekDay($date) == "Sun") ) {    return "weekend"; } 

        //Get Absences
    $sql = "SELECT leaves.* FROM `hs_hr_leave` AS leaves
    WHERE (leaves.employee_id = '$employee_id') AND (leaves.leave_date = '$date') AND (leaves.leave_status > 1)
    ORDER BY leaves.leave_date ASC;";   

    $result = mysql_query($sql, $db)or die(mysql_error());  
    $result_count = mysql_num_rows($result);

    if ( $result_count == 0 )  {
        return "default";
    } else {
        while ($row = mysql_fetch_array( $result ) ) {
            if ($row['leave_length_days'] < 1.0 ) {
                if ( ($row['start_time'] >= "08:00:00") && ($row['start_time'] <= "09:00:00")  )
                    return "absence_partial_m";
                else if ( ($row['start_time'] >= "13:00:00") && ($row['start_time'] <= "18:00:00")  )
                    return "absence_partial_a";
                } else
                    return "absence_full";                  
        }//
    }
}   

    // be nice to the sql server and close the sql link once page is done loading.
mysql_close($db);

?>

Original issue reported on code.google.com by mattbilodeau on 18 Sep 2010 at 8:28

GoogleCodeExporter commented 8 years ago
disregard this report,this code is buggy that i put in this ticket.

Original comment by mattbilodeau on 18 Sep 2010 at 10:37