Location: PHPKode > projects > Project Man > projectman/user/functions.php
<?php
//////////////////////////////////////
function checkid($id)
{
	if(!is_numeric($id))
	die("All links are numeric.");
	$link = "SELECT * FROM users WHERE id='$id'";
	$res = mysql_query($link) or die(mysql_error());
	@$totalnum = mysql_num_rows($res);
	if($totalnum == 0)
	die("No item exists with that ID.");
}
//////////////////////////////////
function ownedit($id)
{
	if ($_SESSION['admin'] != 1)
	{
		$link = "SELECT name FROM users WHERE id='$id'";
		$res = mysql_query($link) or die(mysql_error());
		$row = mysql_fetch_row($res);
		if ($row[0] != $_SESSION['name'])
		die("Please only edit your own settings and details.");
	}
}
////////////////////////////////
function clean($string)
{ 
$cleanse = array("<",">","'",'"',"/");

	foreach ($cleanse as $val)
	{
		$string = str_replace($val,"",$string);
	}	

return $string;
}
////////////////////////////////
function err($field)
{
	$result="You either did not enter a value or the value was too large for: " . $field;
	return $result;
}
/////////////////////////////////////////////
function admin()
{
	if ($_SESSION['admin'] != 1)
	die("<p>Sorry, only Admins can perform this action.</p>");
}
////////////////////////////////////////
function addUser($name, $password, $admin)
{
		$sql = "INSERT INTO users (name , password, admin) VALUES ('".$name."' ,'". md5($password) ."', '".$admin."')";
		if (!mysql_query($sql))
  		{
  			return 'Error: ' . mysql_error();
  		}
		return "1 record added";
}
///////////////////////////////////////////
////////////////////////////////////////
function edituser($uid, $name, $password, $admin)
{
	$sql = "UPDATE `projectman`.`users` SET `name` ='".$name. "', `password` = '".md5($password)."', `admin` = '".$admin."' WHERE `id` = '".$uid."'";
				$result=$result = mysql_query($sql);
				if (!$result)
  				{
  					$result= 'Error: ' . mysql_error();
					return $result;
  				}
				return "Updated succesfuly"; 
}
///////////////////////////////////////
function deleteuser ($id)
{
	$sql="DELETE FROM `users` WHERE `id` = '".$id."'";
	$result = mysql_query($sql);
		if (!$result)
  		{
  			$result= 'Error: ' . mysql_error();
			return $result;
  		}
		return "Deleted succesfuly"; 
}
///////////////////////////////////

//////////////////////////////////////////
function addnewproject($name, $date, $des, $status, $project_manager, $client)
{
$start_date = strtotime($date);
$start_date=date("m-d-Y", $start_date);
$sql="INSERT INTO  `projectman`.`projects` (`name` , `date` , `des` ,`status` , `project_manager`, `client` ) VALUES ( '".$name."',  '". $start_date ."',  '". $des ."',  '". $status."',  '".$project_manager."',  '".$client."' )";
				$result=$result = mysql_query($sql);
				if (!$result)
  				{
  					$result= 'Error: ' . mysql_error();
					return $result;
  				}
				return "1 record added"; 
}
////////////////////////////////////////
function editproject($id, $name, $date, $des, $status, $project_manager, $client)
{
$start_date = strtotime($date);
$start_date=date("m-d-Y", $start_date);
$sql = "UPDATE `projectman`.`projects` SET `name` ='".$name. "', `date` = '".$start_date."', `des` = '".$des."', `status` = '".$status."',   `project_manager`='".$project_manager."',   `client`='".$client."' WHERE `id` = '".$id."'";

				$result=$result = mysql_query($sql);
				if (!$result)
  				{
  					$result= 'Error: ' . mysql_error();
					return $result;
  				}
				return "Updated succesfuly"; 
}
///////////////////////////////////////
function deleteProject ($id)
{
	$sql="DELETE FROM `projects` WHERE `id` = '".$id."'";
	$result = mysql_query($sql);
		if (!$result)
  		{
  			$result= 'Error: ' . mysql_error();
			return $result;
  		}
		return "Deleted succesfuly"; 
}
///////////////////////////////////
function getusername($id)
{
	$sql="select name from users where id='".$id."'";
	$result= mysql_query($sql);
	if(mysql_numrows($result)<=0)
	{
		$result= 'Error: ' . mysql_error();
			return $result;
	}
	while($row=mysql_fetch_array($result))
	return $row['name'];

}
////////////////////////////////
function getstatus($id)
{
	$sql="select * from status where id='".$id."'";
	$result= mysql_query($sql);
	if(mysql_numrows($result)<=0)
	{
		$result= 'Error: ' . mysql_error();
			return $result;
	}
	while($row=mysql_fetch_array($result))
	return $row['status'];

}
///////////////////////////////////
function getclient($id)
{
	$sql="select * from client where id='".$id."'";
	$result= mysql_query($sql);
	if(mysql_numrows($result)<=0)
	{
		$result= 'Error: ' . mysql_error();
			return $result;
	}
	while($row=mysql_fetch_array($result))
	return $row['name'];

}
////////////////////////////////////
function getproject($id)
{
	$sql="select * from projects where id='".$id."'";
	$result= mysql_query($sql);
	if(mysql_numrows($result)<=0)
	{
		$result= 'Error: ' . mysql_error();
			return $result;
	}
	while($row=mysql_fetch_array($result))
	return $row['name'];

}
///////////////////////////////////////////
function addtask($wbs1,$wbs2,$wbs3,$name,$des,$user,$project,$start_date,$finish_date,$status)
{
	

			$start_ts = strtotime($start_date);

			$end_ts = strtotime($finish_date);

			$diff = $end_ts - $start_ts;

			$duration= round($diff / 86400)+1;
			$start_ts=date("m-d-Y", $start_ts);
			$end_ts=date("m-d-Y", $end_ts);
			
			$psd="";
			$sql="select date from projects where id='".$project."'";
			$result= mysql_query($sql);
			while ($row= mysql_fetch_row($result))
			{
					$psd=strtotime($row['date']);
			}
			
			$diff_psd = $start_ts - $psd;

			$duration_psd= round($diff / 86400)+1;

			if($duration <= 0)
			{
				return "can not insert data error in date; task duration can not be zero!";
			}
			
			if($duration_psd <= 0)
			{
				return "can not insert data error in date; task could not start before project start date!";
			}
	$sql="INSERT INTO task (wbsl1,wbsl2,wbsl3,name,des,user,project,start_date,finish_date,status,duration)values ('".$wbs1."','".$wbs2."','".$wbs3."','".$name."','".$des."','".$user."','".$project."','".$start_ts."','".$end_ts."','".$status."','".$duration."')";
	$result= mysql_query($sql);
	if(mysql_affected_rows()<=0)
	{
		$result= 'Error: ' . mysql_error();
			return $result;
	}
	
	return mysql_affected_rows()."recorde inserted successfuly";

}

///////////////////////////////
function edittask($id,$wbs1,$wbs2,$wbs3,$name,$des,$user,$project,$start_date,$finish_date,$status)
{

			$start_ts = strtotime($start_date);

			$end_ts = strtotime($finish_date);

			$diff = $end_ts - $start_ts;

			$duration= round($diff / 86400)+1;
			$start_ts=date("m-d-Y", $start_ts);
			$end_ts=date("m-d-Y", $end_ts);


	$sql="update task set wbsl1='".$wbs1."',wbsl2='".$wbs2."',wbsl3='".$wbs3."',name='".$name."',des='".$des."',user='".$user."',project='".$project."',start_date='".$start_ts."',finish_date='".$end_ts."',status='".$status."',duration='".$duration."' WHERE `id` = '".$id."'";
	$result= mysql_query($sql);
	if(mysql_affected_rows()<=0)
	{
		$result= 'Error: ' . mysql_error();
			return $result;
	}
	return mysql_affected_rows()." recorde updated successfuly";

}
////////////////////////////////

function deletetask ($tid)
{
	$sql="DELETE FROM `task` WHERE `id` = '".$tid."'";
	$result = mysql_query($sql);
		if (!$result)
  		{
  			$result= 'Error: ' . mysql_error();
			return $result;
  		}
		return " Deleted succesfuly"; 
}
////////////////////////////////
function newclient($name, $address, $phone, $fax, $email)
{

$sql="INSERT INTO  `projectman`.`client` (`name` , `address` , `phone` ,`fax` , `email`) VALUES ( '".$name."',  '". $address ."',  '". $phone ."',  '". $fax."',  '".$email."' )";
				$result=$result = mysql_query($sql);
				if (!$result)
  				{
  					$result= 'Error: ' . mysql_error();
					return $result;
  				}
				return "1 record added"; 
}

////////////////////////////////////
function editclient($id, $name, $address, $phone, $fax, $email)
{
$sql = "UPDATE `projectman`.`client` SET `name` ='".$name. "', `address` = '".$address."', `phone` = '".$phone."', `fax` = '".$fax."',   `email`='".$email."' WHERE `id` = '".$id."'";

				$result=$result = mysql_query($sql);
				if (!$result)
  				{
  					$result= 'Error: ' . mysql_error();
					return $result;
  				}
				return "Updated succesfuly"; 
}
///////////////////////////////////////////
function deleteclient($cid)
{
	$sql="DELETE FROM `client` WHERE `id` = '".$cid."'";
	$result = mysql_query($sql);
		if (!$result)
  		{
  			$result= 'Error: ' . mysql_error();
			return $result;
  		}
		return "Deleted succesfuly"; 
}
//////////////////////////////////////////////
function newtime($currentuser,$tdate, $time, $tdes )
{
	$tsdate = strtotime($tdate);
	$tsdate=date("m-d-Y", $tsdate);
	$sql="INSERT INTO  `projectman`.`timesheet` (`userid` , `date` , `time` ,`des` ) VALUES ( '".$currentuser."',  '". $tsdate ."',  '". $time ."',  '". $tdes."' )";
				$result=$result = mysql_query($sql);
				if (!$result)
  				{
  					$result= 'Error: ' . mysql_error();
					return $result;
  				}
				return "1 record added";
}
//////////////////////////////////////////////////
function newmassege($from_user, $to_user, $title, $body, $date,$type)
{
	$sql="INSERT INTO  message (from_user, to_user, title, body, mdate, type) VALUES ( '".$from_user."',  '". $to_user ."',  '". $title ."',  '". $body."',  '". $date."',  '". $type."' )";
				$result=$result = mysql_query($sql);
				if (!$result)
  				{
  					$result= 'Error: ' . mysql_error();
					return $result;
  				}
				return " 1 Message sent ! ";
}
////////////////////////////////////////////////////
function deletemessage($mid)
{
	$sql="DELETE FROM message WHERE id = '".$mid."'";
	$result = mysql_query($sql);
		if (!$result)
  		{
  			$result= 'Error: ' . mysql_error();
			return $result;
  		}
		return "Message deleted succesfuly"; 
}
?>
Return current item: Project Man