<?php
/**
 * Workgroup module - display forum's topic
 * 
 * Topic detail page
 * 
 * @package linea21\modules\workshop
 * @author $Author$ - linea21 <info@linea21.com>
 * @version $Id$ 
 * @access public
 * @license http://opensource.org/licenses/gpl-3.0.html
 */

//////////// Check Inclusion de pages ////////////
if (!class_exists('auth')){
  include_once("../lib/lib_common.php");
  ReloadIndex('admin');
}
////////////

if (isset($_POST['id'])) $id = $_POST['id'];
if (isset($_GET['id'])) $id = $_GET['id'];

if (isset($_POST['com_id'])) $com_id = $_POST['com_id'];
if (isset($_GET['com_id'])) $com_id = $_GET['com_id'];

if(!isset($_GET['debut'])) $debut=0;
else $debut=$_GET['debut'];



// From that page, the administrator / manager is able to move a discussion to another
// we do that if needed
if(isset($_POST['valider'])) {
    
    if(isset($_POST['moveto_workshop']) && $_POST['moveto_workshop'] != 0) {
        $workshop_object= new workshop_com;
        $r = $workshop_object->MoveCom($com_id, $id, $_POST['moveto_workshop'], $sql_object);
        $link_confirm="index.php?rub=".$rub."&todo=".$todo."&id=".$_POST['moveto_workshop']."&com_id=".$com_id;

        if($r) {
            set_flash_msg(_t('workshop', 'move_success'));
            header("Location: ".safe_redirect($link_confirm));
            exit; // this exit is here necessary ! 
        }
        else  system_error();
        
    }
    
}

include_once("../lib/input_helpers.php");

$req_count_com = SQL_getWorkcom_detList($com_id);
$result_nb_com=$sql_object->DBSelect($req_count_com);
if(is_array($result_nb_com)) $bdd_nb_com=$result_nb_com[0]['nb_rep'];
else $bdd_nb_com=0;
$bdd_nb_com_display = "<p class=\"nb_display\">".$bdd_nb_com." "._t('workshop','nb_total_com_mes')."</p>\n";


$req_det = SQL_getoneCompleteWorkshop($id);
$result = $sql_object -> DBSelect($req_det);

// Do record exists?
if(!isset($result[0]['workshop_denomination'])) redirect_to('@module_default');


$link_det = $rub_link . "&amp;todo=det&amp;id=";
$link_sup_com=$rub_link."&amp;todo=sup_com&amp;id=".$id."&amp;com_id=";
$link_lock_com=$rub_link."&amp;todo=lock_com&amp;id=".$id."&amp;com_id=";
$link_mod_com=$rub_link."&amp;todo=mod_com&amp;id=".$id."&amp;com_id=";
$link_add_com=$rub_link."&amp;todo=add_com&amp;id=".$id."&amp;com_id=";
$link_list_com=$rub_link."&amp;todo=list_com&amp;id=".$id;
$link_det_com=$rub_link."&amp;todo=det_com&amp;id=".$id."&amp;com_id=";
$link_det_user="index.php?rub=user&amp;todo=det&amp;id=";

$link_self=$link_det_com.$com_id;

$workshop_denomination = formatText($result[0]['workshop_denomination'], '2HTML');
$workshop_resume = formatText($result[0]['workshop_resume'], '2HTML');

if (empty($result[0]['workshop_comment'])) $workshop_comment = _t('divers','none');
else $workshop_comment = formatText($result[0]['workshop_comment'], '2HTML');

$workshop_statut = display_statut($result[0]['workshop_statut']);
$display_date_crea = $result[0]['workshop_date_crea_display'];
$display_last_modify = $result[0]['workshop_last_modify_display'];

$link_back='';
$count_request=$bdd_nb_com;
$req_com_root = SQL_getOnetopicWorkcom($id, $com_id, $com_id);
$result_com_root = $sql_object -> DBSelect($req_com_root);
$com_num=-1;

// Do record exists?
if(!isset($result_com_root[0]['workcom_id'])) redirect_to('@module_default');

$req_list_com = SQL_getOnetopicWorkcom2($id, $com_id, $debut, SELECT_LIMIT, $com_num);
$result_com = $sql_object -> DBSelect($req_list_com);


$reca_com='';

if ($result_com_root [0]['workcom_statut']=='C') $class='task_out';
else $class='task';


// select-box de workshops
$data=$sql_object->DBSelect(SQL_get_all_workshop());
$select_workshop=WorkshopSelectBox($data, "moveto_workshop", $id);

$move_to  = '<div class="toggle_title">';
$move_to .= '<a id="resreport" href="#">'._t('workshop','move_discussion').'</a>';
$move_to .= '</div>';
$move_to .= '<div id="move-discussion-box" style="display:none">';
$move_to .= '<div><form method="post" action="'.$_SERVER['SCRIPT_NAME'].'" id="move-discussion">';
$move_to .= '<div class="box-toggle">';
$move_to .= '<div class="flex-container box-content row">';
$move_to .= '<div class="w150p" style="margin-top:1.5em;">' . _t('workshop', 'move_to') . '</div>';
$move_to .= '<div class="item-fluid" style="margin-top:0.6em;">'.$select_workshop.'</div>';
$move_to .= '<div class="w150p" style="  "><input name="rub" type="hidden" value="'.$rub.'" id="rub" />';
$move_to .= '<input name="todo" type="hidden" value="'.$todo.'" id="todo" />';
$move_to .= '<input name="id" type="hidden" value="' . $id . '" id="id_workshop" />';
$move_to .= '<input name="com_id" type="hidden" value="' . $com_id . '" id="com_id" />';
$move_to .= '<input type="submit" id="valider" class="button" value="'._t('btn','valid').'" name="valider" />';
$move_to .= '</div>';
$move_to .= '</div><!-- closing box-content --></div>';
$move_to .= '</form></div>';
$move_to .= '</div>';



$reca_com .= "<div class=\"".$class."\" id=\"msg-".$result_com_root [0]['workcom_id']."\">\n";
$reca_com .="<div class=\"task_user_profile\">\n";

//user avatar
$complete_uri='../'.$result_com_root[0]['profile_avatar'];
if($result_com_root[0]['profile_avatar'] == "") $user_profile_avatar='<img src="'.PUBLIC_THEME_URL.'/images/no_avatar.png" alt="'. formatText($result_com_root[0]['user_login'], '2HTML').' '.formatText($result_com_root[0]['user_login'], '2HTML').'" class="user_profile"/>';
else $user_profile_avatar='<img src="'.$complete_uri.'" alt="'. formatText($result_com_root[0]['user_login'], '2HTML').' '.formatText($result_com_root[0]['user_login'], '2HTML').'" class="user_profile"/>';

// display user informations and date message with or without link on user
if ($l21auth->isSuperAdmin()) $display_user="\n<a href=\"" . $link_det_user.$result_com_root[0]['workcom_user_id']."\" title=\"" . _t('workshop','com_author') . "\" class=\"auth_task\">". formatText($result_com_root[0]['user_login'], '2HTML') . "</a>\n";
else $display_user="\n<span class=\"auth_task\">".formatText($result_com_root[0]['user_login'], '2HTML')."</span>";
$reca_com .=$user_profile_avatar;
$reca_com .=$display_user;
$reca_com .= "\n<span class=\"task_date\">" . $result_com_root[0]['workcom_date_crea_display'] . "</span>\n";
$reca_com .="</div>\n";

//message content
$reca_com .="<div class=\"task_content\">\n";
if ($l21auth->isWorkgroupOrganiser ($l21auth->GetSessionElement('id'), $sql_object, $id) || $l21auth->isSuperAdmin()){
  $reca_com .="<a href=\"" . $link_sup_com . $com_id . "&amp;mes_id=".$result_com_root[0]['workcom_id']."\" title=\"" . _t('workshop','sup_com_mes') . "\" class=\"ico_sup2 action-delete\"><i class=\"fa fa-trash\"></i><span>" . _t('workshop','sup_com_mes') . "</span></a>\n";
  $reca_com .="<a href=\"" . $link_mod_com . $com_id . "&amp;mes_id=".$result_com_root[0]['workcom_id']."\" title=\"" . _t('workshop','mod_com_mes') . "\" class=\"ico_mod2\"><i class=\"fa fa-pencil-square-o\"></i><span>" . _t('workshop','mod_com_mes') . "</span></a>\n";
}
$reca_com .= "<h3 class=\"task_title\">" . formatText($result_com_root[0]['workcom_subject'], '2HTML') . "</h3>\n";
$reca_com .= formatText($result_com_root[0]['workcom_body'], '2HTML') . "\n";

$reca_com .="</div></div>\n";

$reca_com_list='';


if ($result_com<>0) {
  // display all messages on discussion
  for($i = 0; $i < count($result_com); $i++) {

    //locked messages
    if ($result_com_root [0]['workcom_statut']=='C') $class='task_out';
    else $class='task';


    $reca_com_list .= "<div class=\"".$class." root-com".$result_com_root [0]['workcom_id']."\" id=\"msg-".$result_com [$i]['workcom_id']."\">\n";
    $reca_com_list .="<div class=\"task_user_profile\">\n";

    //user avatar
    $complete_uri='../'.$result_com[$i]['profile_avatar'];
    if($result_com[$i]['profile_avatar'] == "") $user_profile_avatar='<img src="'.PUBLIC_THEME_URL.'/images/no_avatar.png" alt="'. formatText($result_com[$i]['user_login'], '2HTML').' '.formatText($result_com[$i]['user_login'], '2HTML').'" class="user_profile"/>';
    else $user_profile_avatar='<img src="'.$complete_uri.'" alt="'. formatText($result_com[$i]['user_login'], '2HTML').' '.formatText($result_com[$i]['user_login'], '2HTML').'" class="user_profile"/>';

    // display user informations and date message with or without link on user
    if ($l21auth->isSuperAdmin()) $display_user="\n<a href=\"" . $link_det_user.$result_com[$i]['workcom_user_id']."\" title=\"" . _t('workshop','com_author') . "\" class=\"auth_task\">". formatText($result_com[$i]['user_login'], '2HTML') . "</a>\n";
    else $display_user="\n<span class=\"auth_task\">".formatText($result_com[$i]['user_login'], '2HTML')."</span>";
    $reca_com_list .=$user_profile_avatar;
    $reca_com_list .=$display_user;
    $reca_com_list .= "\n<span class=\"task_date\">" . $result_com [$i]['workcom_date_crea_display'] . "</span>\n";
    $reca_com_list .="</div>\n";

    //message content
    $reca_com_list .="<div class=\"task_content\">\n";
    if ($l21auth->isWorkgroupOrganiser ($l21auth->GetSessionElement('id'), $sql_object, $id) || $l21auth->isSuperAdmin()){
      $reca_com_list .="<a href=\"" . $link_sup_com . $com_id . "&amp;mes_id=".$result_com [$i]['workcom_id']."\" title=\"" . _t('workshop','sup_com_mes') . "\" class=\"ico_sup2 action-delete\"><i class=\"fa fa-trash\"></i><span>" . _t('workshop','sup_com_mes') . "</span></a>\n";
      $reca_com_list .="<a href=\"" . $link_mod_com . $com_id . "&amp;mes_id=".$result_com [$i]['workcom_id']."\" title=\"" . _t('workshop','mod_com_mes') . "\" class=\"ico_mod2\"><i class=\"fa fa-pencil-square-o\"></i><span>" . _t('workshop','mod_com_mes') . "</span></a>\n";
    }
    $reca_com_list .= "<h3 class=\"task_title\">" . formatText($result_com [$i]['workcom_subject'], '2HTML') . "</h3>\n";

    $reca_com_list .= "<p>".formatText($result_com [$i]['workcom_body'], '2HTML') . "</p>\n";


    $reca_com_list .="</div></div>\n";

  }
}
else {
  $reca_com_list .="<div class=\"info\">"._t('workshop','no_com_rep')."</div>";
}


$str = '$(".action-delete").on( "click", function () {
	var url = $(this).attr("href");
	alertify.set({
		labels : {
			ok: "'._t('divers', 'yes').'", cancel: "'._t('divers', 'no').'" }
	});
	alertify.confirm("'._t('workshop','sup_com_mes_warning').'", function (e) {
		if (e) {
    	$.ajax({
      	  type: "GET",
          url: "'. SITE_ROOT_URL .'/workshop/_ajax_admin.php",
          data: { parameters: url },
          dataType: "json",
          success: function(data) {
						console.log(data);
           	if(data.status == 1) {
							alertify.success(data.msg);
							$(data.el).fadeOut("slow");
							if(data.isroot == "true") {
								$(data.el + "-children").css("opacity", "0.3");
								// we disable all links in children if is root conversation
								$(data.el + "-children a").unbind( "click" );
								$(data.el + "-children a").click(function(e) {  e.preventDefault();});
							}
            } else {
							alertify.error(data.msg);
						}
           }
       });
			
		}
	});
	return false;
});';

footerAddInlineJS($str);

?>
<div id="chemin">
<ul>
	<li><a href="index.php" class="chemin_home"><?php echo _t('way','home'); ?></a></li>
	<li><a href="<?php echo $rub_link; ?>" id="chemin_<?php echo $rub; ?>" class="chemin_rub"><?php echo _t('menu','workshop'); ?></a></li>
	<li><span><?php echo formatnavTitle(_t('workshop','det_com') . " : " .$result_com_root[0]['workcom_subject']); ?></span></li>
</ul>
</div>
<!--end barre haute -->

<div class="contentcontainer">
<?php
include_once(override($dir.'/menurub.php', THEME_ADMIN_PATH));
?>
<div id="content">
<?php afflinkbar($todo,$id, $lang); ?>
<?php
echo "<h2>" . formatTitleh2(formatText($result_com_root[0]['workcom_subject'], '2HTML')) ."</h2>\n";
?>
<div class="subtitle" id="workshop-subtitle"><?php echo $workshop_denomination; ?></div><?php 


echo $move_to;

display_flash_msg(true, 'info');

echo linkin_page($link_self, $count_request, $debut);
echo $reca_com;

if ($l21auth->isWorkgroupOrganiser ($l21auth->GetSessionElement('id'), $sql_object, $id) || $l21auth->isSuperAdmin()){
  if ($result_com_root[0]['workcom_statut']<>'C'){
    echo "<a href=\"" . $link_add_com . $com_id . "\" title=\"" . _t('workshop','add_com_mes')  . "\" class=\"ico_add2\"><i class=\"fa fa-plus-circle\" aria-hidden=\"true\"></i><span>".  _t('workshop','add_com_mes')."</span></a>";
  }
  if ($result_com_root[0]['workcom_statut']=='C'){
    echo "<a href=\"" . $link_lock_com ."".$com_id."&amp;lock=1&amp;mes_id=".$result_com_root[0]['workcom_id']."\" title=\"" . _t('workshop','unlock_com') . "\" class=\"ico_unlock2\"><i class=\"fa fa-unlock\" aria-hidden=\"true\"></i><span>" . _t('workshop','unlock_com') . "</span></a>";
  }else{
    echo "<a href=\"" . $link_lock_com ."".$com_id."&amp;lock=0&amp;mes_id=".$result_com_root[0]['workcom_id']."\" title=\"" . _t('workshop','lock_com') . "\" class=\"ico_lock2\"><i class=\"fa fa-lock\" aria-hidden=\"true\"></i><span>" . _t('workshop','lock_com') . "</span></a>";
  }
}
if($bdd_nb_com != 0) echo $bdd_nb_com_display;
else echo '<p class="ptm">&nbsp;</p>';
echo "<div class=\"task_list\" id=\"msg-".$result_com_root [0]['workcom_id']."-children\">";
echo $reca_com_list;
echo "</div>";
if ($l21auth->isWorkgroupOrganiser ($l21auth->GetSessionElement('id'), $sql_object, $id)|| $l21auth->isSuperAdmin()){
  if ($result_com_root[0]['workcom_statut']<>'C'){
    echo "<a href=\"" . $link_add_com . $com_id . "\" title=\"" . _t('workshop','add_com_mes')  . "\" class=\"ico_add2\"><i class=\"fa fa-plus-circle\" aria-hidden=\"true\"></i><span>".  _t('workshop','add_com_mes')."</span></a>";
  }
  if ($result_com_root[0]['workcom_statut']=='C'){
    echo "<a href=\"" . $link_lock_com ."".$com_id."&amp;lock=1&amp;mes_id=".$result_com_root[0]['workcom_id']."\" title=\"" . _t('workshop','unlock_com') . "\" class=\"ico_unlock2\"><i class=\"fa fa-unlock\" aria-hidden=\"true\"></i><span>" . _t('workshop','unlock_com') . "</span></a>";
  }else{
    echo "<a href=\"" . $link_lock_com ."".$com_id."&amp;lock=0&amp;mes_id=".$result_com_root[0]['workcom_id']."\" title=\"" . _t('workshop','lock_com') . "\" class=\"ico_lock2\"><i class=\"fa fa-lock\" aria-hidden=\"true\"></i><span>" . _t('workshop','lock_com') . "</span></a>";
  }
}
echo linkin_page($link_self, $count_request, $debut);
?>
<br class="brendstep" />
</div> <!-- closing #content -->
</div> <!-- closing .contentcontainer -->