<?php
defined('_JEXEC') or die('Restricted access');
/**
* @package OS CCK
* @copyright 2019 OrdaSoft.
* @author Andrey Kvasnevskiy(akbet@mail.ru),Roman Akoev (akoevroman@gmail.com), Vladislav Prikhodko (vlados.vp1@gmail.com)
* @link http://ordasoft.com/cck-content-construction-kit-for-joomla.html
* @description OrdaSoft Content Construction Kit
*/
$rand = rand();
$fName = $field->db_field_name;
// var_dump($key);
$action_button_text = (isset($layout_params['views'][$fName.'_text'])) ? $layout_params['views'][$fName.'_text'] : 'Edit';
$action_button_task = (isset($layout_params['fields'][$fName.'_button_task'])) ? $layout_params['fields'][$fName.'_button_task'] : 'edit';
// var_dump($layout_params['fields'][$fName.'_button_task']);
// var_dump($Itemid);
$href = '#';
if(!isset($form_styling)) $form_styling = '';
// var_dump($layout->type );
if($action_button_task == 'edit'){
//    if($layout->type == 'instance'){
        $href = JURI::root() . 'index.php?option=com_os_cck&task=edit_instance&eiid='.$entityInstance->eiid.'&old_lid='.$layout->lid.'&button='.$key.'&Itemid='.$Itemid;
//    }elseif($layout->type == 'instance'){
//        
//    }
}elseif($action_button_task == 'delete'){
  $href = JURI::root() . 'index.php?option=com_os_cck&task=delete_instances&eiid='.$entityInstance->eiid.'&old_lid='.$layout->lid . '&button='.$key.'&Itemid='.$Itemid;
}
if(!isset($session)) $session = JFactory::getSession();
$previous_page = $session->get('previous_page', '');
if(isset($_SERVER['HTTP_REFERER']) && stripos($_SERVER['HTTP_REFERER'], 'apply_instance') === false 
    && stripos($_SERVER['HTTP_REFERER'], 'save_instance') === false && stripos($_SERVER['HTTP_REFERER'], 'edit_instance') === false){
  $previous_page = $_SERVER['HTTP_REFERER'];
  $session->set('previous_page', $_SERVER['HTTP_REFERER']);
}

// var_dump($previous_page);W
//for redirect aftre instance save
$redirect_page = (isset($layout_params['fields'][$fName.'_redirect_page'])) ? $layout_params['fields'][$fName.'_redirect_page'] : 'previous_page';
// var_dump($redirect_page);
if($action_button_task == 'apply'){
  $redirect_page = '';
}elseif($redirect_page == 'previous_page'){
  $redirect_page = $previous_page;
}else{
  $redirect_page = (isset($layout_params['fields'][$fName.'_custom_page'])) ? $layout_params['fields'][$fName.'_custom_page'] : '';
}
// var_dump($redirect_page);

$field_styling = get_field_styles($field, $layout);
$custom_class = get_field_custom_class($field, $layout);
$offset_animation = get_field_offset_animation($field, $layout);
$div_styling = get_align_styles($field, $layout);

echo '<div '.$div_styling.' class="col_box  ' .$field->db_field_name . '" >';
?>

  <span <?php if(isset($layout_params['fields']['description_'.$field->db_field_name]) && $layout_params['fields']['description_'.$field->db_field_name]=='on' && !empty($layout_params['fields'][$field->db_field_name.'_tooltip']))
        {?>
rel="tooltip" data-toggle="tooltip" data-placement="top" title="<?php echo $layout_params['fields'][$field->db_field_name.'_tooltip'];?>"
    <?php } ?> >

    <?php
    if($action_button_task == 'edit' || $action_button_task == 'delete'){ ?>
  
      <a <?php echo $field_styling;?> href="<?php echo $href; ?>" class="button btn-info <?php echo $custom_class;?>" <?php echo $offset_animation; ?> ><?php echo $action_button_text; ?></a>

    <?php }elseif($action_button_task == 'save'){ ?>
      <input <?php echo $form_styling; ?> type="button" value="<?php echo $action_button_text; ?>" class="button btn-info <?php echo $custom_class; ?> <?php echo $field->db_field_name; ?>" <?php echo $offset_animation; ?> onclick="javascript:event.preventDefault(); sendRequest<?php echo $unique_form_id; ?>('save_instance', '<?php echo $unique_form_id?>', '<?php echo $fName; ?>');">
    <?php }elseif($action_button_task == 'apply'){ ?>
      <input <?php echo $form_styling; ?> type="button" value="<?php echo $action_button_text; ?>" class="button btn-info <?php echo $custom_class; ?> <?php echo $field->db_field_name; ?>" <?php echo $offset_animation; ?> onclick="javascript:event.preventDefault(); sendRequest<?php echo $unique_form_id; ?>('apply_instance', '<?php echo $unique_form_id?>', '<?php echo $fName; ?>');">
    <?php }elseif($action_button_task == 'cancel'){ ?>
      <input <?php echo $form_styling; ?> type="button" value="<?php echo $action_button_text; ?>" class="button btn-info <?php echo $custom_class; ?> <?php echo $field->db_field_name; ?>" <?php echo $offset_animation; ?> onclick="javascript:event.preventDefault(); sendRequest<?php echo $unique_form_id; ?>('cancel_instance', '<?php echo $unique_form_id?>', '<?php echo $fName; ?>');">
    <?php }?>
    
  </span>
<?php
echo "</div>";
?>

<script>

  //for redirect aftre instance save
  if(typeof redirect_arr == 'undefined'){ 
    redirect_arr = {}
  }
  redirect_arr.<?php echo $fName; ?> = '<?php echo $redirect_page; ?>'
  
</script>

