<?php
/**
 * @package linea21.modules
 * @subpackage project
 * @author linea21 <info@linea21.com>
 * @version $id SVN
 * @access public
 * @license http://opensource.org/licenses/gpl-3.0.html
 */

include_once('../project/' . SQL . '.inc.php');

/**
 * DisplayListProject()
 * Affichage de la liste des projets publics
 *
 * @param string $statut
 * @param integer $filter
 * @param string $id_filter
 * @param integer $debut
 * @return string $content
 */
function DisplayListProject($statut, $filter, $id_filter, $debut)
{
  if ($statut == 'public') {
    $pas = 100;
    $suffix = '';
  } else $pas = SELECT_LIMIT;
  if ($statut == 'archives_p') $suffix = ' "'.strtolower($GLOBALS['lang']['statut']['PA']).'"';
  $data = $GLOBALS['sql_object']->DBSelect(SQL_getProjectsList($debut, $pas, $statut, $filter, $id_filter));
  $datacount = $GLOBALS['sql_object']->DBSelect(SQL_getCountProjectsList($statut, $filter, $id_filter));
  $bdd_nb_com = $datacount[0]['num_rows'];
  $bdd_nb_com_display = '<p>' . $bdd_nb_com . ' ' . $GLOBALS['lang']['project']['name'] . $suffix . '</p>' . END_LINE;
  $content = '<h1 class="semnone">' . $GLOBALS['lang']['project']['list'] . '</h1>' . END_LINE;
  if ($data != 0) {
    // echo $bdd_nb_com_display;	// affichage du nombre d'enregistrements
    $current_link = array('rub' => $GLOBALS['links'][LANGUAGE]['project']['linkvalue']);
    Display_linkin_page($current_link, $bdd_nb_com, $debut, $pas);
    for($i = 0; $i < count($data); $i++) {
      $link_detail = array('rub' => $GLOBALS['links'][LANGUAGE]['project']['linkvalue'], 'id' => $data[$i]['project_id'], 'name' => $data[$i]['project_name']);
      $link_scale = array('rub' => $GLOBALS['links'][LANGUAGE]['project']['linkvalue'], 'filter' => $GLOBALS['filters'][LANGUAGE]['scale']['linkvalue'], 'id' => $data[$i]['project_scale_id'], 'name' => $data[$i]['scale_denomination']);
      $link_priority = array('rub' => $GLOBALS['links'][LANGUAGE]['project']['linkvalue'], 'filter' => $GLOBALS['filters'][LANGUAGE]['priority']['linkvalue'], 'id' => $data[$i]['priority_id'] , 'name' => $data[$i]['priority_name']);
      $link_user = array('rub' => $GLOBALS['links'][LANGUAGE]['user-profile']['linkvalue'], 'id' => $data[$i]['project_posted_by'], 'name' => $data[$i]['user_login']);
      $project_name = formatText($data[$i]['complete_project_name'], '2HTML');
      $project_description = formatText(cutText($data[$i]['project_description'], 300), '2HTML');
      $scale_denomination = formatText($data[$i]['scale_denomination'], '2HTML');
      $project_posted_on = formatText($data[$i]['project_published_date_display'], '2HTML');
      $project_posted_by = formatText($data[$i]['user_login'], '2HTML');
      $project_priority = formatText($data[$i]['priority_name'], '2HTML');

      $content .= '<h2 class="nobottommargin"><a href="' . HrefMaker($link_detail) . '" title="' . formatText($project_name, '2ATT') . '">' . $project_name . '</a>' . END_LINE;
      $content .= '</h2>' . END_LINE;
      $content .= '<p class="published_by">' . END_LINE;
      $content .= $GLOBALS['lang']['statut']['published_on'] . ' ' . $project_posted_on . ' ';
      $content .= $GLOBALS['lang']['divers']['by'].' <a href="' . HrefMaker($link_user) . '" title="' . $GLOBALS['links'][LANGUAGE]['user-profile']['desc'] . ' (' . $project_posted_by . ')">' . $project_posted_by . '</a>';
      $content .= '</p>' . END_LINE;
      	
      $content .= '<p class="list_textcontent">' . $project_description . '</p>' . END_LINE;
      $content .= '<p class="filter_info">';
      $content .= $GLOBALS['lang']['project']['priority'] . ' : ';
      $content .= ' <a href="' . HrefMaker($link_priority) . '" title="' . $GLOBALS['filters'][LANGUAGE]['priority']['desc'] . ' : ' . formatText($project_priority, '2ATT') . '" class="filter_info_priority">' . $project_priority . '</a> ' . END_LINE;
      $content .= $GLOBALS['lang']['divers']['scale'] . ' : ';
      $content .= ' <a href="' . HrefMaker($link_scale) . '" title="' . $GLOBALS['filters'][LANGUAGE]['scale']['desc'] . ' : ' . formatText($scale_denomination, '2ATT') . '" class="filter_info_scale">' . $scale_denomination . '</a></p>' . END_LINE;
      if($i<count($data)-1) $content .= '<hr />' . END_LINE;
    }
  } else {
    $content .= '<p class="info">' . $GLOBALS['lang']['divers']['nodata'] . '</p>';
  }

  echo $content;
}

/**
 * DisplayOneProject()
 * Affichage d'un projet en détail
 *
 * @param integer $project_id
 * @return string $stat status name
 */
function DisplayOneProject($project_id)
{
  $data = $GLOBALS['sql_object']->DBSelect(SQL_getoneCompleteProject($project_id));
  $content = '';
  if (count($data) != 1) exit;
  else {
    $link_scale = array('rub' => $GLOBALS['links'][LANGUAGE]['project']['linkvalue'], 'filter' => $GLOBALS['filters'][LANGUAGE]['scale']['linkvalue'], 'id' => $data[0]['project_scale_id'], 'name' => $data[0]['scale_denomination']);
    $link_priority = array('rub' => $GLOBALS['links'][LANGUAGE]['project']['linkvalue'], 'filter' => $GLOBALS['filters'][LANGUAGE]['priority']['linkvalue'], 'id' => $data[0]['project_priority_id'], 'name' => $data[0]['project_priority']);
    $link_user = array('rub' => $GLOBALS['links'][LANGUAGE]['user-profile']['linkvalue'], 'id' => $data[0]['project_posted_by'], 'name' => $data[0]['user_login']);
    $link_parent = array('rub' => $GLOBALS['links'][LANGUAGE]['project']['linkvalue'], 'id' => $data[0]['project_parent_id'], 'name' => $data[0]['parent_name']);
    $project_name = formatText($data[0]['project_name'], '2HTML');
    $project_description = linkin_content(formatText($data[0]['project_description'], '2HTML'));
    $project_body = linkin_content(formatText($data[0]['project_body'], '2HTML'));
    $project_parent = formatText($data[0]['parent_name'], '2HTML');
    $project_scale = formatText($data[0]['scale_denomination'], '2HTML');
    $project_priority = formatText($data[0]['project_priority'], '2HTML');
    $project_begin_date = formatText(empty_nc($data[0]['project_begin_date_display']), '2HTML');
    $project_estimated_date = formatText(empty_nc($data[0]['project_estimated_date_display']), '2HTML');
    $project_end_date = formatText(empty_nc($data[0]['project_end_date_display']), '2HTML');

    $schedule_table = getScheduleTable($data[0]['project_begin_date_display'], $data[0]['project_estimated_date_display'], $data[0]['project_end_date_display']);

    $project_posted_on = formatText($data[0]['project_published_date_display'], '2HTML');
    $project_modify_on = formatText($data[0]['project_last_modify_display'], '2HTML');
    $project_posted_by = formatText($data[0]['user_login'], '2HTML');
    $project_status = $data[0]['project_statut'];

    $content .= '<h1 class="nobottommargin">' . $project_name . '</h1>' . END_LINE;
    $content .= '<p class="published_by">' . END_LINE;
    $content .= $GLOBALS['lang']['statut']['published_on'] . ' ' . $project_posted_on . ' ';
    $content .= $GLOBALS['lang']['divers']['by'].' <a href="' . HrefMaker($link_user) . '" title="' . $GLOBALS['links'][LANGUAGE]['user-profile']['desc'] . ' (' . $project_posted_by . ')">' . $project_posted_by . '</a>';
    if ($project_status == 'PA') {
      $content .= ' (' . $GLOBALS['lang']['statut']['archived_on'] . ' ' . $project_modify_on . ')';
    }
    $content .= '</p>' . END_LINE;
    if($data[0]['project_parent_id'] != 0) {
      $content .= '<div id="parent_project">' . END_LINE;
      $content .= $GLOBALS['lang']['project']['parent'] . ' : ';
      $content .= '<a href="' . HrefMaker($link_parent) . '" title="' . $GLOBALS['lang']['project']['parent'] . ' : ' . formatText($project_parent, '2ATT') . '" class="filter_info_parent">' . $project_parent . '</a> ';
      $content .= '</div>' . END_LINE;
    }
    $content .= '<p class="chapo">' . $project_description . '</p>' . END_LINE;
    
    $content .= '<div class="schedule">' . $schedule_table . '</div>' . END_LINE;
    $content .= '<p class="textcontent">' . $project_body . '</p>' . END_LINE;
    $content .= getProjectChildren($project_id);
    $content .= '<p class="filter_info">' . END_LINE;
    $content .= $GLOBALS['lang']['project']['priority'] . ' : ';
    $content .= '<a href="' . HrefMaker($link_priority) . '" title="' . $GLOBALS['filters'][LANGUAGE]['priority']['desc'] . ' : ' . formatText($project_priority, '2ATT') . '" class="filter_info_priority">' . $project_priority . '</a> ' . END_LINE;
    $content .= ' ' . $GLOBALS['lang']['divers']['scale'] . ' : ';
    $content .= '<a href="' . HrefMaker($link_scale) . '" title="' . $GLOBALS['filters'][LANGUAGE]['scale']['desc'] . ' : ' . formatText($project_scale, '2ATT') . '" class="filter_info_scale">' . $project_scale . '</a>' . END_LINE;
    $content .= '</p>' . END_LINE;
  }
  echo $content;
  return GetStatusName($project_status);
}

/**
 * GetProjectChildren()
 *
 * @param  $id
 * @return string
 */
function getProjectChildren($id)
{
  $data = $GLOBALS['sql_object']->DBSelect(SQL_getProjectChildren($id));

  $content = '';
  if (count($data) != 0 && $data[0]!=0) {
    
    $content .= '<div id="linkinparts">'.END_LINE;
    $content .= '<p>'.$GLOBALS['lang']['project']['sub-projects'].' : </p><ul>'.END_LINE;
    for($i = 0; $i < count($data); $i++) {
      $link = array('rub' => $GLOBALS['links'][LANGUAGE]['project']['linkvalue'], 'id' => $data[$i]['project_id'], 'name' => $data[$i]['project_name']);
      $name = formatText($data[$i]['project_name'], '2HTML');
      
      $content .= '<li><a href="' . HrefMaker($link) . '" title="' . $GLOBALS['lang']['project']['parent'] . ' : ' . formatText($name, '2ATT') . '" class="filter_info_parent">' . $name . '</a></li>' .END_LINE;
    }
    $content .= '</ul></div>'.END_LINE;
  }

  return $content;
}

/**
 * GetStatusName()
 *
 * @param  $status
 * @return $stat Status name
 */
function GetStatusName($status)
{
  switch ($status) {
    case 'P':
      $stat = 'public';
      break;
    case 'PA':
      $stat = 'archives_p';
      break;
    case 'AA':
      $stat = 'archives_a';
      break;
    default:
      $stat = 'public';
  }
  return $stat;
}

/**
 * getScheduleTable()
 *
 * @param string $s_date
 * @param string $es_date
 * @param string $e_date
 * @return string $content
 */
function getScheduleTable($s_date, $es_date, $e_date) {

  $es_date_class = 'nothing';
  $e_date_class = 'nothing';

  if(!isNullDate($es_date)) {
    if(!isNullDate($e_date)) $es_date_class = date_compare($e_date, $es_date, '>') ? 'warn' : 'good';
    else $es_date_class = date_compare(date("Y-m-d"), $es_date, '>') ? 'bad' : 'good';
  }
  if(!isNullDate($e_date)) {
    if(!isNullDate($es_date)) $e_date_class = date_compare($e_date, $es_date, '>') ? 'bad' : 'good';
    else $e_date_class = 'good';
  } else {
    if(!isNullDate($es_date)) {
      $e_date_class = date_compare(date("Y-m-d"), $es_date, '>') ? 'warn' : 'nothing';
    }
  }

  $content  = '<table class="project" cellpading="0" cellspacing="0">'.END_LINE;
  $content .= '<thead>'.END_LINE;
  $content .= '<tr>'.END_LINE;
  $content .= '<th>'.$GLOBALS['lang']['project']['begin_date'].'</th>'.END_LINE;
  $content .= '<th>'.$GLOBALS['lang']['project']['estimated_date'].'</th>'.END_LINE;
  $content .= '<th>'.$GLOBALS['lang']['project']['end_date'].'</th>'.END_LINE;
  $content .= '</tr>'.END_LINE;
  $content .= '</thead>'.END_LINE;
  $content .= '<tbody>'.END_LINE;
  $content .= '<tr>'.END_LINE;
  $content .= '<td>'.formatText(empty_nc($s_date)).'</td>'.END_LINE;
  $content .= '<td class="'.$es_date_class.'">'.formatText(empty_nc($es_date)).'</td>'.END_LINE;
  $content .= '<td class="'.$e_date_class.'">'.formatText(empty_nc($e_date)).'</td>'.END_LINE;
  $content .= '</tr>'.END_LINE;
  $content .= '</tbody>'.END_LINE;
  $content .= '</table>'.END_LINE;
  return $content;
}


?>

