<?php
/**
 * Scale module
 * 
 * Delete given scale from back-office
 * 
 * @package linea21\core\scale
 * @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');
}
////////////

//////////// Check Droits utilisateur ////////////
if (!$l21auth->isSuperAdmin()) ReloadIndex('admin');
////////////

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


$link_cancel=$rub_link."&todo=det&amp;id=".$id;

$mask_mass_del="style=\"display:none;\"";
$mask_mass_mod="style=\"display:none;\"";
$mask_del_meth='';
$mask_mod_meth='';
$form_scale_transfert=-1;

$scale_object= new scale;

function getScaleChildren($data, $id) {
	
	$res = array();
	
	foreach ($data as $item) {
		
		if($item['scale_root'] == $id) {
			$children = getScaleChildren($data, $item['scale_id']);
			if ($children) {
				$item['children'] = $children;
			}
			$res[] = $item;
		}
	}
	return $res;
}

$scales = $sql_object->DBSelect(SQL_getScalesList());
$scales_children = getScaleChildren($scales, $id);

// get current id affected ressources
$affectedRows = $scale_object->get_affected_ressources($id, $sql_object);

// then, we add affected rows from children scales
if(count($scales_children) > 0) {
	foreach ($scales_children as $child) {
		$affectedRows = $scale_object->get_affected_ressources($child['scale_id'], $sql_object, $affectedRows);
	}
}

if(isset($_POST['valider'])) {
  $integrity=1;
  if(isset($_POST['sup_method'])) {
    $sup_method=$_POST['sup_method'];
    if($sup_method=='delete') {
      $type='MASS_DELETE';
      $mask_mass_del="style=\"display:block;\"";
      $mask_del_meth="checked=\"checked\"";
      $form_scale_transfert=-1;
    }
    if($sup_method=='modify') {
      $type='MASS_MODIFY';
      $mask_mod_meth="checked=\"checked\"";
      $mask_mass_mod="style=\"display:block;\"";
      if($_POST['transfert_scale']==-1) $integrity=_t('scale','sup_noscale');
      else $form_scale_transfert=$_POST['transfert_scale'];
    }
    if($_SESSION['securityaction']!=md5($_POST['securitycode'])) {
      $integrity=_t('security','message');
    }
    if(!is_string($integrity)) {
      $link_confirm="confirm.php?rub=".$rub."&todo=".$todo;
      $result=$scale_object->DeleteScale($id, $sql_object, $type, $form_scale_transfert);
      //exit;
      if(is_numeric($result)) {
          header("Location: ".safe_redirect($link_confirm));
          exit;
      }
      else  system_error();
    }
  }	else  {
    $integrity=_t('scale','sup_nomethod');
  }

}

include_once("../lib/input_helpers.php");
// select-box des échelles
#$req_list=SQL_getlistscale($id);
#$data=$sql_object->DBSelect($req_list);
#$select_root=ScaleSelectBox($data, "transfert_scale", $form_scale_transfert);

// select-box des échelles
$table_exclu=$scale_object->getMaxDepth($id, $sql_object);
$req=SQL_getMaxScalesDepth($table_exclu["depth"], $table_exclu["exclusion"]);
$data=$sql_object->DBSelect($req);
$select_root=ScaleSelectBox($data, "transfert_scale", $form_scale_transfert);



$req_det= SQL_getdetscale($id);
$result1=$sql_object->DBSelect($req_det);

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

$current_title=$result1[0]['scale_denomination'];

//////////// Check si pas suppresion échelle root ////////////
if ($result1[0]['scale_root']==0){
  ReloadIndex('admin');
}
////////////
?>
<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','scale'); ?></a></li>
	<li><span><?php echo _t('scale','sup').' : '.formatnavTitle($current_title); ?></span></li>
</ul>
</div>
<!--end barre haute -->

<div class="contentcontainer">
<?php
include_once(override($dir.'/menurub.php', THEME_ADMIN_PATH));
?>
<div id="content">
<?php
if(isset($integrity) && is_string($integrity)) display_errors($integrity);

$js = "$('#sup_method_del').click(function() {
					$('#mass_modify').hide();
					$('#transfert').hide();
					$('#mass_delete').fadeIn();
				});
				$('#sup_method_mod').click(function() {
					$('#mass_modify').fadeIn();
					$('#transfert').slideDown();
					$('#mass_delete').hide();
				});";

footerAddInlineJS($js);
?>
<h2><?php echo formatTitleh2($current_title); ?></h2>
<form id="supscale" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
	<p>
		<input type="radio" class="radio" name="sup_method" id="sup_method_del" value="delete" <?php echo $mask_del_meth; ?> required />
		<label for="sup_method_del" class="autowidth"><?php echo _t('scale','sup_massd'); ?></label>
	</p>
	<p>
		<input type="radio" class="radio" name="sup_method" id="sup_method_mod" value="modify" <?php echo $mask_mod_meth; ?> />
		<label for="sup_method_mod" class="autowidth"><?php echo _t('scale','sup_massm'); ?></label>
	</p>
	<p id="transfert" <?php echo $mask_mass_mod; ?>>
		<label for="transfert_scale"><?php echo _t('scale','sup_transfert'); ?></label>
		<?php echo $select_root; ?>
	</p>
	<p class="info">
		<strong><?php echo sprintf(_t('items', 'associated'), $affectedRows['total']); ?></strong><br />
		<span class="small">[<?php echo  _t('content', 'in_news') . ' : ' . $affectedRows['news'] . ' / ' . _t('content', 'in_publication') . ' : ' . $affectedRows['publication'] . ' / ' . _t('content', 'in_project') . ' : ' . $affectedRows['project'] .  ' / ' . _t('content', 'in_indicator') . ' : ' . $affectedRows['indicator']; ?>]</span>
	</p>
	<p class="code-confirmation">
		<img src="../lib/securitycode.php" alt="<?php echo _t('security','info2') ?>" id="securityimg" />
		<label for="securitycode" class="autowidth"><?php echo _t('security','info') ?></label>
		<input type="text" class="shorttextfield" id="securitycode" maxlength="5" name="securitycode" required />
	</p>
	<?php
	echo "<div class=\"info\" id=\"mass_delete\" ".$mask_mass_del.">\n";
	echo '<i class="fa fa-trash"></i> ';
	echo _t('scale','sup_scale_info2');
	echo "</div>\n";
	echo "<div class=\"info\" id=\"mass_modify\" ".$mask_mass_mod.">\n";
	echo '<i class="fa fa-exchange" aria-hidden="true"></i> ';
	echo _t('scale','sup_scale_info1');
	echo "</div>\n";
	?>
	<div>
		<input name="id" type="hidden" value="<?php echo $id; ?>" />
		<input name="rub" type="hidden" value="<?php echo $rub; ?>" id="rub" />
		<input name="todo" type="hidden" value="<?php echo $todo; ?>" id="todo" />
		<?php echo cancel_button('javascript:history.go(-1);'); ?>
		<input name="valider" type="submit" value="<?php echo _t('btn','valid') ?>" class="button" id="valider" />
	</div>
</form>
</div> <!-- closing #content -->
</div> <!-- closing .contentcontainer -->