Location: PHPKode > projects > PhpProjectMaster > www/inc/task.lib.php
<?php
/* 
 task related functions
 (c) 2004-2007 by "Oleg Savchuk" <hide@address.com>

 part of phpProjectMaster project
 http://phpprojmaster.sourceforge.net

 The contents of this file are subject to the GNU GENERAL PUBLIC LICENSE
 http://www.gnu.org/copyleft/gpl.html
*/

 require_once "sitelib.php";
 require_once "form_utils.php";
 require_once "upload_utils.php";
 require_once "project.lib.php";
 require_once dirname(__FILE__)."/user.php";
 require_once "prio.lib.php";

//preset module variables

 $task_vars=array(
   'table_name'   => 'task',
   'table_key_id' => 't_id',
   'upload_url'   => "/upload/task",
   'upload_path'  => "$site_root_offline/upload/task",
 );


//get item fields from database
function get_task($item_id){
 global $task_vars;

 $sql="select * from $task_vars[table_name] where $task_vars[table_key_id]=$item_id";
 return db_row($sql);
}

//get name
function get_task_name($item_id){
 $hT=get_task($item_id);
 return $hT['iname'];
}

//mark item as removed
function delete_task($item_id){
 global $task_vars;

 db_query("update $task_vars[table_name] set status=127 where $task_vars[table_key_id]=$item_id");
}

//********************* for uploads
function get_upload_url_task($item_id, $is_preview=''){
 global $task_vars;

 $nameadd='';
 if ($is_preview) $nameadd='_s';

 return get_upload_url($item_id, $task_vars['upload_path'], $task_vars['upload_url'], $nameadd);
}

function get_upload_path_task($item_id, $is_preview='', $ext='', $nocheck=''){
 global $task_vars;

 $nameadd='';
 if ($is_preview) $nameadd='_s';

 return get_upload_path($item_id, $task_vars['upload_path'], $nameadd, $ext, $nocheck);
}

//******************* add task info to comments
function add_task2comm($hTASK){

 $hP=get_project($hTASK['p_id']);

 $ps=array_merge($hTASK,array(
   'p_id_name'     => $hP['iname'],
   'u_id_name'     => get_user_name($hTASK['u_id']),
   'prio_id_name'  => get_prio_name($hTASK['prio_id']),
 ));
 $msg=parse_page("/admin/task", "new_task_comm.html", $ps, 'v');

 $IFORM=array(
    'p_id' => $hTASK['p_id'],
    't_id' => $hTASK['t_id'],
    'idesc' => n2br($msg),
    'is_sys' => 1,
    'add_u_id' => $_SESSION['u_id'],
 );

 $sql="insert into comm ".get_sqlinsert_set($IFORM,', add_time',', now()');
// rw($sql);
 db_query($sql);

 add_task_comm_ctr($hTASK['t_id']);

}

//***************** send email to user about new task
function send_task_new($u_id, $hTASK){
 $hTOU=get_user($u_id);
 if (!$hTOU['email']) return 0;

 $hP=get_project($hTASK['p_id']);

 $ps=array_merge($hTASK,array(
   'p_id_name'     => $hP['iname'],
   'u_id_name'     => $hTOU['fname'].' '.$hTOU['lname'],
   'prio_id_name'  => get_prio_name($hTASK['prio_id']),
   'add_u_id_name' => get_user_name($hTASK['add_u_id']),
   'ROOT_DOMAIN'   => $GLOBALS['root_domain'],
 ));
 $msg_body=parse_page("/emails", "new_task.txt", $ps, 'v');
 $msg_body=preg_replace("/<[^>]*>/", "", $msg_body);  #remove HTML
// rw($msg_body);
 list($msg_subj, $msg_body)=email2subj_body($msg_body);

 send_email($hTOU['email'], $msg_subj, $msg_body);

}

//********** add cached counter
function add_task_comm_ctr($item_id, $ctr=1){
 db_query("update task set comm_ctr=comm_ctr+$ctr where t_id=".dbq($item_id));
}

//************ check access of logged user to att
// returns:
// 100 - full access: view, create, edit, delete + any other
// 40 - view+create+edit+delete
// 30 - view+create+edit
// 20 - view+create
// 10 - view only
// 0 - no access
function check_access_task($task_id){
 #!!!TODO
 $hT=get_task($task_id);

 if (!$_SESSION['u_id']){  #not logged in - no access to tasks at all
    return 0;
 }

 if ($hT['add_u_id']==$_SESSION['u_id']){
    return 40;
 }

 return 100;  //full access
}

//******************* check task changes
// $hOLD should contain t_id
function check_task_changes($hNEW, $hOLD){
 global $site_templ;

 $t_id=$hOLD['t_id'];
 $hCH=array(); #changes
 
# rw("OLD");
# print_r($hOLD);
# rw("NEW");
# print_r($hNEW);

 if ($hNEW['p_id']!=$hOLD['p_id']){
    $hCH['p_id_name_old']=get_project_name($hOLD['p_id']);
    $hCH['p_id_name_new']=get_project_name($hNEW['p_id']);
    $hCH['p_id']=1;
 }
 if ($hNEW['u_id']!=$hOLD['u_id']){
    $hCH['u_id_name_old']=get_user_name($hOLD['u_id']);
    $hCH['u_id_name_new']=get_user_name($hNEW['u_id']);
    $hCH['u_id']=1;
 }
 $hNEW['prio_id']+=0;
 $hOLD['prio_id']+=0;
 if ($hNEW['prio_id']!=$hOLD['prio_id']){
    $hCH['prio_id_name_old']=get_prio_name($hOLD['prio_id']);
    $hCH['prio_id_name_new']=get_prio_name($hNEW['prio_id']);
    $hCH['prio_id']=1;
 }
 $hNEW['is_private']+=0;
 $hOLD['is_private']+=0;
 if ($hNEW['is_private']!=$hOLD['is_private']){
    $hCH['is_private_name_old']=$hOLD['is_private'];
    $hCH['is_private_name_new']=$hNEW['is_private'];
    $hCH['is_private']=1;
 }
 if ($hNEW['perc_compl']!=$hOLD['perc_compl']){
    $hCH['perc_compl_old']=$hOLD['perc_compl'];
    $hCH['perc_compl_new']=$hNEW['perc_compl'];
    $hCH['perc_compl']=1;
 }
 if ($hNEW['build_opened']!=$hOLD['build_opened']){
    $hCH['build_opened_old']=$hOLD['build_opened'];
    $hCH['build_opened_new']=$hNEW['build_opened'];
    $hCH['build_opened']=1;
 }
 if ($hNEW['build_fixed']!=$hOLD['build_fixed']){
    $hCH['build_fixed_old']=$hOLD['build_fixed'];
    $hCH['build_fixed_new']=$hNEW['build_fixed'];
    $hCH['build_fixed']=1;
 }
 $hNEW['severity']+=0;
 $hOLD['severity']+=0;
 if ($hNEW['severity']!=$hOLD['severity']){
    $hCH['severity_name_old']=get_select_name("$site_templ/common/sel/severity.sel",$hOLD['severity']);
    $hCH['severity_name_new']=get_select_name("$site_templ/common/sel/severity.sel",$hNEW['severity']);
    $hCH['severity']=1;
 }

 #prepare times format
 $hNEW['start_time']=preg_replace('/~!NULL/','',$hNEW['start_time']);
 $hNEW['end_time']=preg_replace('/~!NULL/','',$hNEW['end_time']);
 $hNEW['end_time2']=preg_replace('/~!NULL/','',$hNEW['end_time2']);

 #remove time, leave just date
 $hOLD['start_time']=StrDate2SQL(SQLDate2Str($hOLD['start_time']));
 $hOLD['end_time']=StrDate2SQL(SQLDate2Str($hOLD['end_time']));
 $hOLD['end_time2']=StrDate2SQL(SQLDate2Str($hOLD['end_time2']));

 if ($hNEW['start_time']!=$hOLD['start_time']){
    $hCH['start_time_old']=$hOLD['start_time'];
    $hCH['start_time_new']=$hNEW['start_time'];
    $hCH['start_time']=1;
 }
 if ($hNEW['end_time']!=$hOLD['end_time']){
    $hCH['end_time_old']=$hOLD['end_time'];
    $hCH['end_time_new']=$hNEW['end_time'];
    $hCH['end_time']=1;
 }
 if ($hNEW['end_time2']!=$hOLD['end_time2']){
    $hCH['end_time2_old']=$hOLD['end_time2'];
    $hCH['end_time2_new']=$hNEW['end_time2'];
    $hCH['end_time2']=1;
 }
 $hNEW['status']+=0;
 $hOLD['status']+=0;
 if ($hNEW['status']!=$hOLD['status']){
    $hCH['status_name_old']=get_select_name("$site_templ/common/sel/status.sel",$hOLD['status']);
    $hCH['status_name_new']=get_select_name("$site_templ/common/sel/status.sel",$hNEW['status']);
    $hCH['status']=1;
 }

 if ($hNEW['iname']!=$hOLD['iname']){
    $hCH['iname_old']=$hOLD['iname'];
    $hCH['iname_new']=$hNEW['iname'];
    $hCH['iname']=1;
 }
 if ($hNEW['idesc']!=$hOLD['idesc']){
    $hCH['idesc_old']=$hOLD['idesc'];
    $hCH['idesc_new']=$hNEW['idesc'];
    $hCH['idesc']=1;
 }


 if ( count($hCH) ){
//    print_r($hCH);

    $ps=array_merge($hCH,array(
      'itype'         => $hOLD['itype'],
    ));
    $msg=parse_page("/admin/task", "ch_task_comm.html", $ps, 'v');
    $msg=preg_replace("/[\n\r]+/", "\n", $msg);
//    rw(n2br($msg));

    $IFORM=array(
       't_id'     => $t_id,
       'p_id'     => $hNEW['p_id'],
       'idesc'    => $msg,
       'is_sys'   => 1,
       'add_u_id' => $_SESSION['u_id'],
    );

    $sql="insert into comm ".get_sqlinsert_set($IFORM,', add_time',', now()');
//    logger($sql);
    $sth=db_query($sql);
    $item_id=get_identity();

    //add cached counter
    add_task_comm_ctr($t_id);

    #send notification to all related users
    send_comm_new($item_id);
 }
}


//*****************************************
function mark_task_read($item_id, $u_id=0){
 if (!$item_id) return 0;
 if (!$u_id) $u_id=$_SESSION['u_id']+0;

 $row=db_row("select * from tu_status where t_id=$item_id and u_id=$u_id");
 if ( $row['t_id']){  //record exists - updating
    db_query("update tu_status set is_read=1, read_time=now() where t_id=$item_id and u_id=$u_id and is_read=0");
    $result=1;

 }else{ //record not exists - inserting

    $IFORM=array(
       't_id'     => $item_id,
       'u_id'     => $u_id,
       'is_read'  => 1,
    );

    $sql="insert into tu_status ".get_sqlinsert_set($IFORM,', read_time',', now()');
    db_query($sql);
    $result=2;
 }

 return $result;
}

//*****************************************
function mark_task_flag($item_id, $flag, $u_id=0){
 if (!$item_id) return 0;
 if (!$u_id) $u_id=$_SESSION['u_id']+0;

 if ($flag!=0 && $flag!=1) $flag=0;

 $row=db_row("select * from tu_status where t_id=$item_id and u_id=$u_id");
 if ( $row['t_id']){  //record exists - updating
    db_query("update tu_status set is_flagged=$flag, flag_time=now() where t_id=$item_id and u_id=$u_id ");
    $result=1;

 }else{ //record not exists - inserting

    $IFORM=array(
       't_id'     => $item_id,
       'u_id'     => $u_id,
       'is_flagged' => $flag,
    );

    $sql="insert into tu_status ".get_sqlinsert_set($IFORM,', flag_time',', now()');
    db_query($sql);
    $result=2;
 }

 return $result;
}

?>
Return current item: PhpProjectMaster