<?php
defined('_JEXEC') or die('Restricted access');

/**
* @package OS CCK
* @copyright 2020 OrdaSoft.
* @author Andrey Kvasnevskiy(akbet@mail.ru),Roman Akoev (akoevroman@gmail.com)
* @link http://ordasoft.com/cck-content-construction-kit-for-joomla.html
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
* @description OrdaSoft Content Construction Kit
*/

$doc = JFactory::getDocument();


?>
<div class="cck-body" cid="<?php echo $category->cid; ?>" eid="<?php echo $category->fk_eid; ?>" lid="<?php echo $layout->lid; ?>">

    <script type="text/javascript">
    if(typeof(bitches) === 'undefined'){
        var bitches = new Array();
    }
      function order_field_submitbutton(moduleId){ 
        if (moduleId) {
          var form = document.getElementById('orderForm_cckmod_'+moduleId);
        }else{
          var form = document.orderForm;
        }
        form.submit();
      }

    function allreordering(moduleId){ 
        
       if (moduleId) { 
          if(document.getElementById('order_direction_cckmod_'+moduleId).value == 'ASC'){
            document.getElementById('order_direction_cckmod_'+moduleId).value = 'DESC';
          }else{
            document.getElementById('order_direction_cckmod_'+moduleId).value = 'ASC';
          }
          var form = document.getElementById('orderForm_cckmod_'+moduleId);
       }else{ 
          if(document.orderForm.order_direction.value == 'ASC'){ 
            document.orderForm.order_direction.value = 'DESC';
          }else{ 
            document.orderForm.order_direction.value = 'ASC';
          }
          var form = document.orderForm;
       }

      form.submit();
    }
    </script>
  <?php
  $os_cck_rand = rand();
      if(isset($layout_params['views']['show_layout_title']) && $layout_params['views']['show_layout_title'])
    {   
          echo "<h3>";
            echo $layout_params['views']['layout_title'];
          echo "</h3>";
    }
    $layout_fields = $layout_params['fields'];
    $fields = array();
    $show_fields = array();
    ?>
  <div>
  <?php 
    //for ($i = 0;$i < count($instancies);$i++) {
        //$entity_type_url = ($entity_type) ? '&entity_type=' . $entity_type : '';
        //$modId = ($moduleId) ? '&moduleId=' . $moduleId : '';
        //$instance = $instancies[$i];
        //$link = 'index.php?option=com_os_cck&amp;view=instance&amp;id=' . $instance->eiid . '&amp;catid=' . $catid . '&amp;Itemid=' . $Itemid . $entity_type_url.$modId;
      $field = new stdClass();
      $field->db_field_name = 'form-'.$layout->lid;
      $form_styling = get_field_styles($field, $layout);
      $form_custom_class = get_field_custom_class($field, $layout);
      $form_offset_animation = get_field_offset_animation($field, $layout);
      $form_hover_animation = get_field_hover_animation($field, $layout);
      ?>
      <div class="<?php echo $form_custom_class; ?> cat_fields" <?php echo $form_offset_animation; ?> <?php echo $form_hover_animation; ?> <?php echo $form_styling; ?> >
      <?php
      $layout_html = urldecode($layout->layout_html);
      $layout_html = cut_params($layout_html);
      $layout_html = remove_admin_classes($layout_html);
      $layout_html = str_replace('data-label-styling', 'style',  $layout_html);
      
      if(isset($layout_params['views']['show_request_layout'])){
        foreach ($layout_params['views']['show_request_layout'] as $key => $value) {
            
           $php_if = isset($layout_params['views']['request_layout_php_show'][$key][0]) ? $layout_params['views']['request_layout_php_show'][$key][0] : '';

           $php_result = true;
           if($php_if != ''){
                $php_result = processing_php_show($php_if, $instance, $layout_params, $layout_html, $layout);
           }
           if($php_result){

              if(isset($layout_params['views']['show_type_request_layout'][$key][0])){
                        $show_type = $layout_params['views']['show_type_request_layout'][$key][0];
              }else{
                $show_type = 1;
              }

              $field->db_field_name = "l".$key;
              $layout_field = $field;
              $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);
              $button_style = array('field_styling'=>$field_styling,'custom_class'=>$custom_class,'offset_animation'=>$offset_animation,'div_styling'=>$div_styling);
// var_dump($button_style);
              $button_name = isset($layout_params['views']['show_request_layout_button_name'][$key])?$layout_params['views']['show_request_layout_button_name'][$key][0]:'';

              if(strpos($layout_html,"{|l-".$key."|}")){
                  $field = new stdClass();
                $field->db_field_name = $key;
// var_dump($layout_params['views']['show_request_layout'][$key]);
                if(isset($layout_params['views']['show_request_layout'][$key])
                    && $layout_params['views']['show_request_layout'][$key][0] != '1'
                    && $layout_params['views']['show_request_layout'][$key][0] != ''){

                  $user = JFactory::getUser();
                  if(!checkAccess_cck($layout_params['views']['show_request_layout'][$key], $user->groups, $layout->fk_eid, 'fields')){
                    $layout_html = str_replace("{|l-".$key."|}", '', $layout_html);
                    continue;
                  }else{
                    // echo '11111111111111111';
                    // $layout_html = str_replace($key.'-label-hidden', '', $layout_html);
                  }
                }
                
                if(isset($layout_params['views']['show_request_layout_name'][$key]) &&
                    isset($layout_params['views']['show_request_layout_name'][$key][0]) &&
                    $layout_params['views']['show_request_layout_name'][$key][0] == 'on'){
                  $layout_html = str_replace($key.'-label-hidden', '', $layout_html);
                }
                
                $field_styling = get_field_styles($field, $layout);
                $custom_class = get_field_custom_class($field, $layout);
                $offset_animation = get_field_offset_animation($field, $layout);
                //get tag
                $shell_tag = isset($layout_params['fields']['field_tag_'.$field->db_field_name])?$layout_params['fields']['field_tag_'.$field->db_field_name]:'div';
                //end
                //if below fn works , that this is add_instance view

                if(isset($layout_params["views"]["instance_grid_".$key]) && $layout_params["views"]["instance_grid_$key"] == 1){ ?>

                <script>

                   jQuerOs(document).ready(function(){

                    jQuerOs(".instance_body_<?php echo $key; ?>").parent().addClass('flex-block');

                   });

                </script>

                <?php  }

                $unique_class = 'parent_child_unique_class_'.$layout->lid;
                $hover_styling = get_hover_css_style($layout_field, $layout_params, 'layout', $unique_class);

                echo $hover_styling;
//                var_dump($hover_styling);
                $cck_wrapper_instance = (isset($layout_params["views"]["instance_grid_$key"]) && $layout_params["views"]["instance_grid_$key"] == 1)?"cck-wrapper-instance":"";

                $resolutions = ($cck_wrapper_instance != "" && isset($layout_params["views"]["auto_custom_$key"]) && $layout_params["views"]["auto_custom_$key"] == "custom")?"instance-col-lg-".$layout_params["views"]["resolition_one_$key"]." "."instance-col-md-".$layout_params["views"]["resolition_two_$key"]." "."instance-col-sm-".$layout_params["views"]["resolition_three_$key"]." "."instance-col-xs-".$layout_params["views"]["resolition_four_$key"]." ":'';

                if($cck_wrapper_instance != "" &&  $resolutions == ""){

                  $count_inst_columns = (isset($layout_params["views"]["count_inst_columns_$key"]))?$layout_params["views"]["count_inst_columns_$key"]:"";
                  $min_width = (isset($layout_params["views"]["lay_min_width_$key"]))?$layout_params["views"]["lay_min_width_$key"]:"";
                  $space_between = (isset($layout_params["views"]["space_between_$key"]))?$layout_params["views"]["space_between_$key"]:"";

                  ?>
                    <script type="text/javascript">

                    jQuerOs( document ).ready(function() {
                        
                        var cckGridLayout1 = function (container, params) {
                            
                            if (!(this instanceof cckGridLayout1)) return new cckGridLayout1(container, params);

                            var defaults = {
                                minImgEnable : 1,
                                spaceBetween: 0,
                                minImgSize: 200,
                                numColumns: 4,
                            };

                            for (var param in defaults) {
                                if (!params[param]){
                                params[param] = defaults[param];
                                }
                            }
                            // gallery settings
                            var cckGrid = this;
                            // Params
                            cckGrid.params = params || defaults;

                                cckGrid.getImgBlockWidth = function (numColumns){
                                    if(typeof(numColumns) == 'undefined')numColumns = cckGrid.params.numColumns;
                                    spaceBetween = cckGrid.params.spaceBetween;
                                    mainBlockW = jQuerOs(container).width();
                                    imgBlockW = ((((mainBlockW-(spaceBetween*numColumns))/numColumns)-1)*100)/mainBlockW;

                                    if(cckGrid.params.minImgEnable){

                                        if(((imgBlockW*mainBlockW)/100) < cckGrid.params.minImgSize){
                                        numColumns--;
                                        cckGrid.getImgBlockWidth(numColumns);
                                        }
                                    }

                                    return imgBlockW;
                                }

                            //initialize function
                                cckGrid.init = function (){
                                imgBlockW = cckGrid.getImgBlockWidth();

                            // jQuerOs(container+" .item").css("width",imgBlockW+"%");


                                jQuerOs(container+">.instance_body_<?php echo $key; ?>").css({
                                'width': imgBlockW+"%",
                                'margin':  cckGrid.params.spaceBetween / 2 
                                });

                                cckGrid.resizeGallery = function (){
                                    imgBlockW = cckGrid.getImgBlockWidth();
                                    jQuerOs(container+">.instance_body_<?php echo $key; ?>").css("width",imgBlockW+"%");
                                }

                                jQuerOs(window).resize(function(event) {
                                cckGrid.resizeGallery();
                                });
                            }

                            cckGrid.init();
                        }


                    window.cckGridLayout1 = cckGridLayout1;

                     });


                    jQuerOs(document).ready(function() {

                        var gallery = new cckGridLayout1(".<?php echo $unique_class; ?>", {
                            minImgEnable : 1,
                            spaceBetween: '<?php echo $space_between; ?>',
                            minImgSize: '<?php echo $min_width; ?>',
                            numColumns: '<?php echo $count_inst_columns; ?>',
                        });



                      });

                    </script>
                  <?php

                }
                if($show_type == 2){
                  $field_styling = '';
                }

                  ob_start();
                  echo '<'.$shell_tag.' class="'.$cck_wrapper_instance. ' ' . $unique_class . ' '.$resolutions.' "  >';
                  $attached_layout = new os_cckLayout($db->_db);
                  $attached_layout->load($key);

                  Category::show_attached_layout($option, $attached_layout, '', $layout_params, $show_type,$button_name , $catid, $instancies, $button_style, 10, $Itemid);
                  
                  echo '</'.$shell_tag.'>';
                  $html = ob_get_contents();
                  $is_instances_not_found = strpos($html,'cck-nothing-found');
                  if( $is_instances_not_found === false ){
                  
                    //for show_instance(s) : und instances founded" 
                    $layout_html = str_replace("{|l-".$key."|}", $html, $layout_html);
                  
                  }else if( $is_instances_not_found !== false 
                     && ( !$is_exist_sub_categories && !($layout_params['views']['sub_category_level'] > 0 ) )
                   ){
                  
                    //for show_instance(s) : show "Not_found only if not exist sub_categories" 
                    $layout_html = str_replace("{|l-".$key."|}", $html, $layout_html);
                  
                  }else{
                    //clean layout placeholder
                    $html = "" ;
                    $layout_html = str_replace("{|l-".$key."|}", $html, $layout_html);
                  }
                  ob_end_clean();

              }
          }else{
                $layout_html = str_replace("{|l-".$key."|}", '', $layout_html);
          }
        }
      }
      
      $field = new stdClass();
      if(strpos($layout_html,"{|f-joom_instance_count|}")){
          $field->db_field_name = 'joom_instance_count';
          $php_if = isset($layout_params['fields'][$field->db_field_name.'_php_show']) ? $layout_params['fields'][$field->db_field_name.'_php_show'] : '';
          $php_result = true;
          if($php_if != ''){
               $php_result = processing_php_show($php_if, $instance, $layout_params, $layout_html, $layout);
          }

          if(!$php_result){
              $layout_html = str_replace("{|f-joom_instance_count|}", '', $layout_html);
          }
      }
      //check access
      if(isset($layout_params['fields']['access_joom_instance_count'])
          && $layout_params['fields']['access_joom_instance_count'] != '1'){
        $user = JFactory::getUser();
        if(!checkAccess_cck($layout_params['fields']['access_joom_instance_count'], $user->groups, $layout->fk_eid, 'fields')){
          $layout_html = str_replace("{|f-joom_instance_count|}", '', $layout_html);
        }
      }

      //category_instance_count
      if(strpos($layout_html,"{|f-joom_instance_count|}")){
        if(isset($currentcat->title)){
            $field = new stdClass();
          $field->db_field_name = 'joom_instance_count';
          $field_styling = get_field_styles($field, $layout);
          $field_styling = substr_replace($field_styling, ' display:block;"', strlen($field_styling)-1, strlen($field_styling));
          $hover_styling = get_hover_css_style($field, $layout_params);
          echo $hover_styling;
          $custom_class = get_field_custom_class($field, $layout);
          $offset_animation = get_field_offset_animation($field, $layout);
          $shell_tag = isset($layout_params['fields']['field_tag_'.$field->db_field_name])?$layout_params['fields']['field_tag_'.$field->db_field_name]:'div';
          
          if(isset($layout_params['fields'][$field->db_field_name.'_tooltip']) && isset($layout_params['fields']['description_'.$field->db_field_name]) && $layout_params['fields']['description_'.$field->db_field_name]=='on'){
            $tooltip = 'rel="tooltip" data-toggle="tooltip" data-placement="top" title="'.$layout_params['fields'][$field->db_field_name.'_tooltip'].'"';           
          }else{
            $tooltip = 'rel="tooltip" data-toggle="tooltip" data-placement="top" title=""';
          }

          $prefix = '';
          if(isset($layout_params['fields'][$field->db_field_name.'_add_icon_prefix_prefix']) && !empty($layout_params['fields'][$field->db_field_name.'_add_icon_prefix_prefix'])){
            $prefix .= '<span class="fa '.$layout_params['fields'][$field->db_field_name.'_add_icon_prefix_prefix'].'"></span>';
          }
          if(isset($layout_params['fields'][$field->db_field_name.'_prefix'])){
            $prefix .= '<span class="cck-prefix">'.$layout_params['fields'][$field->db_field_name.'_prefix'].'</span>';
          }
          if(isset($layout_params['fields'][$field->db_field_name.'_add_icon_prefix_suffix']) && !empty($layout_params['fields'][$field->db_field_name.'_add_icon_prefix_suffix'])){
            $prefix .= '<span class="fa '.$layout_params['fields'][$field->db_field_name.'_add_icon_prefix_suffix'].'"></span>';
          }

          $suffix = '';
          if(isset($field_from_params[$field->db_field_name.'_add_icon_suffix_prefix']) && !empty($field_from_params[$field->db_field_name.'_add_icon_suffix_prefix'])){
              $suffix .= '<span class="fa '.$field_from_params[$field->db_field_name.'_add_icon_suffix_prefix'].'"></span>';
          }
          if(isset($layout_params['fields'][$field->db_field_name.'_suffix'])){
              $suffix .= '<span class="cck-suffix">'.$layout_params['fields'][$field->db_field_name.'_suffix'].'</span>';
          }
          if(isset($field_from_params[$field->db_field_name.'_add_icon_suffix_suffix']) && !empty($field_from_params[$field->db_field_name.'_add_icon_suffix_suffix'])){
              $suffix .= '<span class="fa '.$field_from_params[$field->db_field_name.'_add_icon_suffix_suffix'].'"></span>';
          }
          
          if(isset($layout_params['fields'][$field->db_field_name.'_category_layout']) 
                    && $layout_params['fields'][$field->db_field_name.'_category_layout'] > -1){
            $modId = ($moduleId) ? '&moduleId=' . $moduleId : '';
            $link = 'index.php?option=com_os_cck&amp;view=category&amp;'
                        .'&amp;lid='.$layout_params['fields'][$field->db_field_name.'_category_layout']
                    .'&amp;catid='. $layout_params['catid'] . '&amp;Itemid=' . $Itemid . $modId;
            
            $cat_title = '<a '.$tooltip.' class="'.$custom_class. ' '. $field->db_field_name . '" ' . $offset_animation . ' ' .$field_styling.' href="'.JRoute::_($link).'">'.$prefix.$currentcat->inst_count.$suffix.'</a>';
            $layout_html = str_replace("{|f-joom_instance_count|}", $cat_title, $layout_html);
          }else{
            $cat_title = '<'.$shell_tag.' '.$tooltip.' class="'.$custom_class. ' ' . $field->db_field_name . '" '. $offset_animation . ' '.$field_styling.'>'.$prefix.$currentcat->inst_count.$suffix.'</'.$shell_tag.'>';
            $layout_html = str_replace("{|f-joom_instance_count|}", $cat_title, $layout_html);
          }
        }else{
          $layout_html = str_replace("{|f-joom_instance_count|}", '', $layout_html);
        }
      }











      if(strpos($layout_html,"{|f-category_title|}")){
          $field = new stdClass();
          $field->db_field_name = 'category_title';
          $php_if = isset($layout_params['fields'][$field->db_field_name.'_php_show']) ? $layout_params['fields'][$field->db_field_name.'_php_show'] : '';
          $php_result = true;
          if($php_if != ''){
               $php_result = processing_php_show($php_if, $instance, $layout_params, $layout_html, $layout);
          }

          if(!$php_result){
              $layout_html = str_replace("{|f-category_title|}", '', $layout_html);
          }
      }
      //check access
      if(isset($layout_params['fields']['access_category_title'])
          && $layout_params['fields']['access_category_title'] != '1'){
        $user = JFactory::getUser();
        if(!checkAccess_cck($layout_params['fields']['access_category_title'], $user->groups, $layout->fk_eid, 'fields')){
          $layout_html = str_replace("{|f-category_title|}", '', $layout_html);
        }
      }
      //category_title
      if(strpos($layout_html,"{|f-category_title|}")){
        if(isset($currentcat->title)){
            $field = new stdClass();
          $field->db_field_name = 'category_title';
          $field_styling = get_field_styles($field, $layout);
          $custom_class = get_field_custom_class($field, $layout);
          $offset_animation = get_field_offset_animation($field, $layout);
          $field_styling = substr_replace($field_styling, ' display:block;"', strlen($field_styling)-1, strlen($field_styling));
          $hover_styling = get_hover_css_style($field, $layout_params);
          echo $hover_styling;
          $shell_tag = isset($layout_params['fields']['field_tag_'.$field->db_field_name])?$layout_params['fields']['field_tag_'.$field->db_field_name]:'div';
          
          if(isset($layout_params['fields'][$field->db_field_name.'_tooltip']) && isset($layout_params['fields']['description_'.$field->db_field_name]) && $layout_params['fields']['description_'.$field->db_field_name]=='on'){
            $tooltip = 'rel="tooltip" data-toggle="tooltip" data-placement="top" title="'.$layout_params['fields'][$field->db_field_name.'_tooltip'].'"';           
          }else{
            $tooltip = 'rel="tooltip" data-toggle="tooltip" data-placement="top" title=""';
          }

          if(isset($layout_params['fields'][$field->db_field_name.'_prefix']) && $layout_params['fields'][$field->db_field_name.'_prefix'] != ''){
            $prefix = '<span class="cck-prefix">'.$layout_params['fields'][$field->db_field_name.'_prefix'].'</span>';
          }else{
            $prefix = '';
          }

          if(isset($layout_params['fields'][$field->db_field_name.'_suffix']) && $layout_params['fields'][$field->db_field_name.'_suffix'] != ''){
            $suffix = '<span class="cck-suffix">'.$layout_params['fields'][$field->db_field_name.'_suffix'].'</span>';
          }else{
            $suffix = '';
          }

          $max_lenght = (isset($layout_params['fields'][$field->db_field_name.'_max_lenght'])) ? $layout_params['fields'][$field->db_field_name.'_max_lenght'] : '250';

          if(strlen($currentcat->title) > $max_lenght){
              $currentcat->title = substr($currentcat->title, 0, $max_lenght)."...";
            } 
            //var_dump($layout_params['fields'][$field->db_field_name.'_category_layout']);
//          if((isset($layout_params['parent_layout_params']['views']['link_field'])
//                  && (array_search ( "cat_title" , $layout_params['parent_layout_params']['views']['link_field']) > 0
//                  || array_search ( "cat_title" , $layout_params['parent_layout_params']['views']['link_field']) === 0))
//                  || (isset($layout_params['parent_layout_params']['views']['subcat_link_field'])
//                  && (array_search ( "cat_title" , $layout_params['parent_layout_params']['views']['subcat_link_field']) > 0
//                  || array_search ( "cat_title" , $layout_params['parent_layout_params']['views']['subcat_link_field']) === 0))){
            if(isset($layout_params['fields'][$field->db_field_name.'_category_layout']) 
                    && $layout_params['fields'][$field->db_field_name.'_category_layout'] > -1){
            $modId = ($moduleId) ? '&moduleId=' . $moduleId : '';
            $redirect_type = (isset($layout_params['fields']['redirect_type_'.$field->db_field_name])) ? $layout_params['fields']['redirect_type_'.$field->db_field_name] : 'redirect';
            $target = ($redirect_type == 'redirect_blank') ? 'target="_blank"' : '';
            $link = 'index.php?option=com_os_cck&amp;view=category&amp;'
                        .'&amp;lid='.$layout_params['fields'][$field->db_field_name.'_category_layout']
                    .'&amp;catid='. $layout_params['catid'] . '&amp;Itemid=' . $Itemid . $modId;
//            if(isset($layout_params['parent_layout_params']['views']['category_layout'])){
//                $link = 'index.php?option=com_os_cck&amp;view=category&amp;'
//                        .'&amp;lid='.$layout_params['parent_layout_params']['views']['category_layout']
//                    .'&amp;catid='. $layout_params['catid'] . '&amp;Itemid=' . $Itemid . $modId;
//            }elseif (isset($layout_params['parent_layout_params']['views']['subcategories_layout'])) {
//                $link = 'index.php?option=com_os_cck&amp;view=category&amp;'
//                        .'&amp;lid='.$lid
//                    .'&amp;catid='. $layout_params['catid'] . '&amp;Itemid=' . $Itemid . $modId;
//            }
            $cat_title = '';
            if($redirect_type == 'popUp'){
                $cat_title .= "<a href='#' data-toggle='os_cck_modal' data-target='#show_request_layout_" . $lid . "_".$layout_params['catid']."'>";
                $cat_title .= $prefix. '<span>' . $currentcat->title . '</span>' .$suffix;
                $cat_title.="</a>";
                $cat_title .= '<div class="fade os_cck_modal" id="show_request_layout_' . $lid . '_' . $layout_params['catid'] . '" tabindex="-1" role="dialog" aria-hidden="true">
                  <div class="os_cck_modal-dialog os_cck_modal-dialog-centered" role="document">
                    <div class="os_cck_modal-content">
                      <div class="os_cck_modal-header">
                        <button type="button" class="close" data-dismiss="os_cck_modal" aria-label="Close">
                          <span aria-hidden="true">&times;</span>
                        </button>
                      </div>
                      <div class="os_cck_modal-body">';

                ob_start();
                Category::show_attached_layout($option, $layout_params['fields'][$field->db_field_name.'_category_layout'], 0, $layout_params, 1, '', $layout_params['catid']);
                $cat_title .= ob_get_contents();
                $cat_title .= '</div>
                                  
                                </div>
                              </div>
                            </div>';
                ob_end_clean();

            }else{

                if($redirect_type == 'hover'){
                  $cat_title = '<a '.$tooltip.' class="'.$custom_class.'" ' . $offset_animation . ' ' 
                    .$field_styling.' href="'.JRoute::_($link).'" '.$target.'>'. $prefix. '<span>' . $currentcat->title . '</span>' .$suffix.'</a>';

                  $cat_title .= '<ul class="os_cck_hover_child">';

                  ob_start();
                  Category::show_attached_layout($option, $layout_params['fields'][$field->db_field_name.'_category_layout'], 0, $layout_params, 1, '', $layout_params['catid']);
                  $cat_title .= ob_get_contents();
                  $cat_title .= '</ul>';
                  ob_end_clean();
                }else{
                    $cat_title = $prefix.'<'.$shell_tag.' '.$tooltip.' class="'.$custom_class. ' '. $field->db_field_name
                     . '" '. $offset_animation . ' '.$field_styling.'>' 
                     . '<a  href="'.JRoute::_($link).'" '.$target.'>' . $currentcat->title .'</a>'                    
                     . '</'.$shell_tag.'>'.$suffix;
                }
            }
            
            $layout_html = str_replace("{|f-category_title|}", $cat_title, $layout_html);
          }else{
            $cat_title = $prefix.'<'.$shell_tag.' '.$tooltip.' class="'.$custom_class. ' '. $field->db_field_name . '" '. $offset_animation . ' ' .$field_styling.'>'. $currentcat->title . '</'.$shell_tag.'>'.$suffix;
            $layout_html = str_replace("{|f-category_title|}", $cat_title, $layout_html);
          }
        }else{
          $layout_html = str_replace("{|f-category_title|}", '', $layout_html);
        }
      }

      if(strpos($layout_html,"{|f-category_description|}")){
          $field = new stdClass();
          $field->db_field_name = 'category_description';
          $php_if = isset($layout_params['fields'][$field->db_field_name.'_php_show']) ? $layout_params['fields'][$field->db_field_name.'_php_show'] : '';
          $php_result = true;
          if($php_if != ''){
               $php_result = processing_php_show($php_if, $instance, $layout_params, $layout_html, $layout);
          }

          if(!$php_result){
              $layout_html = str_replace("{|f-category_description|}", '', $layout_html);
          }
      }
      //check access
      if(isset($layout_params['fields']['access_category_description'])
          && $layout_params['fields']['access_category_description'] != '1'){
        $user = JFactory::getUser();
        if(!checkAccess_cck($layout_params['fields']['access_category_description'], $user->groups, $layout->fk_eid, 'fields')){
          $layout_html = str_replace("{|f-category_description|}", '', $layout_html);
        }
      }
      //category_description
      if(strpos($layout_html,"{|f-category_description|}")){
        if(isset($currentcat->descrip)){
            $field = new stdClass();
          $field->db_field_name = 'category_description';
          $field_styling = get_field_styles($field, $layout);
          $custom_class = get_field_custom_class($field, $layout);
          $offset_animation = get_field_offset_animation($field, $layout);
          $field_styling = substr_replace($field_styling, ' display:block;"', strlen($field_styling)-1, strlen($field_styling));
          $hover_styling = get_hover_css_style($field, $layout_params);
          echo $hover_styling;
        
          //get tag
          $shell_tag = isset($layout_params['fields']['field_tag_'.$field->db_field_name])?$layout_params['fields']['field_tag_'.$field->db_field_name]:'div';
          //end
          if(isset($layout_params['fields'][$field->db_field_name.'_tooltip']) && isset($layout_params['fields']['description_'.$field->db_field_name]) && $layout_params['fields']['description_'.$field->db_field_name]=='on'){
            $tooltip = 'rel="tooltip" data-toggle="tooltip" data-placement="top" title="'.$layout_params['fields'][$field->db_field_name.'_tooltip'].'"';           
          }else{
            $tooltip = 'rel="tooltip" data-toggle="tooltip" data-placement="top" title=""';
          }

          if(isset($layout_params['fields'][$field->db_field_name.'_prefix'])){
            $prefix = '<span class="cck-prefix">'.$layout_params['fields'][$field->db_field_name.'_prefix'].'</span>';
          }else{
            $prefix = '';
          }

          if(isset($layout_params['fields'][$field->db_field_name.'_suffix'])){
            $suffix = '<span class="cck-suffix">'.$layout_params['fields'][$field->db_field_name.'_suffix'].'</span>';
          }else{
            $suffix = '';
          }

          $max_lenght = (isset($layout_params['fields'][$field->db_field_name.'_max_lenght'])) ? $layout_params['fields'][$field->db_field_name.'_max_lenght'] : '250';

           if(strlen($currentcat->descrip) > $max_lenght){
              $currentcat->descrip = substr($currentcat->descrip, 0, $max_lenght)."...";
            } 

          if(isset($layout_params['fields'][$field->db_field_name.'_category_layout']) 
                    && $layout_params['fields'][$field->db_field_name.'_category_layout'] > -1){
            $modId = ($moduleId) ? '&moduleId=' . $moduleId : '';
            $redirect_type = (isset($layout_params['fields']['redirect_type_'.$field->db_field_name])) ? $layout_params['fields']['redirect_type_'.$field->db_field_name] : 'redirect';
            $target = ($redirect_type == 'redirect_blank') ? 'target="_blank"' : '';
//            $link = 'index.php?option=com_os_cck&amp;view=category&amp;'
//                    .'&amp;lid='.$layout_params['parent_layout_params']['views']['category_layout']
//                .'&amp;catid='. $layout_params['catid'] . '&amp;Itemid=' . $Itemid . $modId;
            $link = 'index.php?option=com_os_cck&amp;view=category&amp;'
                        .'&amp;lid='.$layout_params['fields'][$field->db_field_name.'_category_layout']
                    .'&amp;catid='. $layout_params['catid'] . '&amp;Itemid=' . $Itemid . $modId;
            $cat_description = '';
            if($redirect_type == 'popUp'){
                $cat_description .= "<a href='#' data-toggle='os_cck_modal' data-target='#show_request_layout_" . $lid . "_".$layout_params['catid']."'>";
                $cat_description .= $prefix. '<span>' . $currentcat->title . '</span>' .$suffix;
                $cat_description.="</a>";
                $cat_description .= '<div class="fade os_cck_modal" id="show_request_layout_' . $lid . '_' . $layout_params['catid'] . '" tabindex="-1" role="dialog" aria-hidden="true">
                  <div class="os_cck_modal-dialog os_cck_modal-dialog-centered" role="document">
                    <div class="os_cck_modal-content">
                      <div class="os_cck_modal-header">
                        <button type="button" class="close" data-dismiss="os_cck_modal" aria-label="Close">
                          <span aria-hidden="true">&times;</span>
                        </button>
                      </div>
                      <div class="os_cck_modal-body">';

                ob_start();
                Category::show_attached_layout($option, $layout_params['fields'][$field->db_field_name.'_category_layout'], 0, $layout_params, 1, '', $layout_params['catid']);
                $cat_description .= ob_get_contents();
                $cat_description .= '</div>
                                  
                                </div>
                              </div>
                            </div>';
                ob_end_clean();

            }else{
                $cat_description .= '<a '.$tooltip.' class="'.$custom_class.'" ' . $offset_animation . ' ' .$field_styling.' href="'.JRoute::_($link).'" '.$target.'>'. $prefix. '<span>' . $currentcat->title . '</span>' .$suffix.'</a>';
                
                if($redirect_type == 'hover'){
                    $cat_description .= '<ul class="os_cck_hover_child">';

                    ob_start();
                    Category::show_attached_layout($option, $layout_params['fields'][$field->db_field_name.'_category_layout'], 0, $layout_params, 1, '', $layout_params['catid']);
                    $cat_description .= ob_get_contents();
                    $cat_description .= '</ul>';
                    ob_end_clean();
                }
            }
            $layout_html = str_replace("{|f-category_description|}", $cat_description, $layout_html);
          }else{
            $cat_description = '<'.$shell_tag.' '.$tooltip.' class="'.$custom_class. ' ' . $field->db_field_name .'" '. $offset_animation . ' ' .$field_styling.'>'.$prefix.$currentcat->descrip.$suffix.'</'.$shell_tag.'>';
            $layout_html = str_replace("{|f-category_description|}", $cat_description, $layout_html);
          }
        }else{
          $layout_html = str_replace("{|f-category_description|}", '', $layout_html);
        }
      }

      if(strpos($layout_html,"{|f-category_image|}")){
          $field = new stdClass();
          $field->db_field_name = 'category_image';
          $php_if = isset($layout_params['fields'][$field->db_field_name.'_php_show']) ? $layout_params['fields'][$field->db_field_name.'_php_show'] : '';
          $php_result = true;
          if($php_if != ''){
               $php_result = processing_php_show($php_if, $instance, $layout_params, $layout_html, $layout);
          }

          if(!$php_result){
              $layout_html = str_replace("{|f-category_image|}", '', $layout_html);
          }
      }
      //check access
      if(isset($layout_params['fields']['access_category_image'])
          && $layout_params['fields']['access_category_image'] != '1'){
        $user = JFactory::getUser();
        if(!checkAccess_cck($layout_params['fields']['access_category_image'], $user->groups, $layout->fk_eid, 'fields')){
          $layout_html = str_replace("{|f-category_image|}", '', $layout_html);
        }
      }
      //category_image
      if(strpos($layout_html,"{|f-category_image|}")){
        $html = '';
        
        $field = new stdClass();
        $field->db_field_name = 'category_image';
        $field_styling = get_field_styles($field, $layout);
        $field_styling = substr_replace($field_styling, ' display:block;"', strlen($field_styling)-1, strlen($field_styling));
        $hover_styling = get_hover_css_style($field, $layout_params);
        echo $hover_styling;
        $custom_class = get_field_custom_class($field, $layout);
        $offset_animation = get_field_offset_animation($field, $layout);
        if(isset($layout_params['fields'][$field->db_field_name.'_tooltip']) && isset($layout_params['fields']['description_'.$field->db_field_name]) && $layout_params['fields']['description_'.$field->db_field_name]=='on'){
          $tooltip = 'rel="tooltip" data-toggle="tooltip" data-placement="top" title="'.$layout_params['fields'][$field->db_field_name.'_tooltip'].'"';           
        }else{
          $tooltip = 'rel="tooltip" data-toggle="tooltip" data-placement="top" title=""';
        }

        if(isset($layout_params['fields'][$field->db_field_name.'_prefix'])){
          $prefix = '<span class="cck-prefix">'.$layout_params['fields'][$field->db_field_name.'_prefix'].'</span>';
        }else{
          $prefix = '';
        }

        if(isset($layout_params['fields'][$field->db_field_name.'_suffix'])){
          $suffix = '<span class="cck-suffix">'.$layout_params['fields'][$field->db_field_name.'_suffix'].'</span>';
        }else{
          $suffix = '';
        }
        $def_img = (isset($layout_params['fields'][$field->db_field_name.'_def_img'])) ? $layout_params['fields'][$field->db_field_name.'_def_img'] : '';
        // var_dump($def_img);
        // _def_img
        // var_dump($currentcat->img);
        if(empty($currentcat->img)){
          if($def_img != ''){
            $currentcat->img = JURI::base().'images/os-cck/'.$def_img;
          }else{
            $currentcat->img = JURI::base().'components/com_os_cck/images/folder.png';
          }
        }
        $cck_image_object_fit  = (isset($layout_params["fields"]['cck_image_object_fit_category_image'])) ? $layout_params["fields"]['cck_image_object_fit_category_image'] : "cover";
        if(isset($currentcat->img) && !empty($currentcat->img)){
          $img_width = $layout_params['fields']['category_image']['width'];
          $img_height = $layout_params['fields']['category_image']['height'];
          if($img_width != ''){
            $img_width = $img_width.'px';
          }else{
            $img_width = '100%';
          }
          
          $style = 'style="height:'.$img_height.'px; width:'.$img_width.'; object-fit:'.$cck_image_object_fit. '"';
          if(isset($layout_params['fields'][$field->db_field_name.'_category_layout']) 
                    && $layout_params['fields'][$field->db_field_name.'_category_layout'] > -1){
            $modId = ($moduleId) ? '&moduleId=' . $moduleId : '';
            $redirect_type = (isset($layout_params['fields']['redirect_type_'.$field->db_field_name])) ? $layout_params['fields']['redirect_type_'.$field->db_field_name] : 'redirect';
            $target = ($redirect_type == 'redirect_blank') ? 'target="_blank"' : '';
            $link = 'index.php?option=com_os_cck&amp;view=category&amp;'
                        .'&amp;lid='.$layout_params['fields'][$field->db_field_name.'_category_layout']
                    .'&amp;catid='. $layout_params['catid'] . '&amp;Itemid=' . $Itemid . $modId;
            
            if($redirect_type == 'popUp'){
                $html .= "<a href='#' data-toggle='os_cck_modal' data-target='#show_request_layout_" . $lid . "_".$layout_params['catid']."'>";
                $html .= $prefix. '<span>' . $currentcat->title . '</span>' .$suffix;
                $html.="</a>";
                $html .= '<div class="os_cck_modal fade " id="show_request_layout_' . $lid . '_' . $layout_params['catid'] . '" tabindex="-1" role="dialog" aria-hidden="true">
                  <div class="os_cck_modal-dialog os_cck_modal-dialog-centered" role="document">
                    <div class="os_cck_modal-content">
                      <div class="os_cck_modal-header">
                        <button type="button" class="close" data-dismiss="os_cck_modal" aria-label="Close">
                          <span aria-hidden="true">&times;</span>
                        </button>
                      </div>
                      <div class="os_cck_modal-body">';

                ob_start();
                Category::show_attached_layout($option, $layout_params['fields'][$field->db_field_name.'_category_layout'], 0, $layout_params, 1, '', $layout_params['catid']);
                $html .= ob_get_contents();
                $html .= '</div>
                                  
                                </div>
                              </div>
                            </div>';
                ob_end_clean();

            }else{
                $html .= '<a '.$tooltip.' href="'.JRoute::_($link).'"><div  class="'.$custom_class. ' ' . $field->db_field_name .'" '. $offset_animation . ' ' . $field_styling.'>'. $prefix .
                        '<img ' . $style . 'src="'.$currentcat->img.'" alt="?"/>'.
                    $suffix . '</div></a>';
                
                if($redirect_type == 'hover'){
                    $html .= '<ul class="os_cck_hover_child">';

                    ob_start();
                    Category::show_attached_layout($option, $layout_params['fields'][$field->db_field_name.'_category_layout'], 0, $layout_params, 1, '', $layout_params['catid']);
                    $html .= ob_get_contents();
                    $html .= '</ul>';
                    ob_end_clean();
                }
            }
          }else{
            $html .= '<div '.$tooltip.' class="'.$custom_class. ' ' . $field->db_field_name .'" '. $offset_animation . ' ' . $field_styling.'>' . $prefix .
                        '<img '.$style.' src="'.$currentcat->img.'" alt="?"/>' .
                    $suffix . '</div>';
          }
        }
        $layout_html = str_replace("{|f-category_image|}", $html, $layout_html);
      }

      if(strpos($layout_html,"{|f-category_map|}")){
          $field = new stdClass();
          $field->db_field_name = 'category_map';
          $php_if = isset($layout_params['fields'][$field->db_field_name.'_php_show']) ? $layout_params['fields'][$field->db_field_name.'_php_show'] : '';
          $php_result = true;
          if($php_if != ''){
               $php_result = processing_php_show($php_if, $instance, $layout_params, $layout_html, $layout);
          }

          if(!$php_result){
              $layout_html = str_replace("{|f-category_map|}", '', $layout_html);
          }
      }
      //check access
      if(isset($layout_params['fields']['access_category_map'])
          && $layout_params['fields']['access_category_map'] != '1'){
        $user = JFactory::getUser();
        if(!checkAccess_cck($layout_params['fields']['access_category_map'], $user->groups, $layout->fk_eid, 'fields')){
          $layout_html = str_replace("{|f-category_map|}", '', $layout_html);
        }
      }
      //category_map
      if(strpos($layout_html,"{|f-category_map|}")){ 
        $field = new stdClass();
        $field->db_field_name = 'category_map';
        require getSiteUniqueFiledViewPath('com_os_cck', 'group_map');
      }



      //pagination
    if($mod_type == 0){
      if(strpos($layout_html,"{|f-joom_pagination|}")){
          $field = new stdClass();
        $field->db_field_name = 'joom_pagination';
        $field_styling = get_field_styles_without_Style($field, $layout);
        
        $field_styling .= 'display:block;' ;
        $hover_styling = get_hover_css_style($field, $layout_params);
        echo $hover_styling;
        $custom_class = get_field_custom_class($field, $layout);
        $offset_animation = get_field_offset_animation($field, $layout);
        $shell_tag = isset($layout_params['cat_layout_params']['fields']['field_tag_joom_pagination'])?$layout_params['cat_layout_params']['fields']['field_tag_joom_pagination']:'span';
        ob_start();
            if (!empty($pageNav) && ($pageNav->total > $pageNav->limit)) { 
              ?>
              <script type="text/javascript">
                   jQuerOs(document).ready(function() {
                    jQuerOs('#pagination').attr('style','<?php echo $field_styling;?>');
                  });
              </script>
              <?php
              echo '<'.$shell_tag.' id="pagination" class="' . $custom_class . ' ' . $field->db_field_name . '" ' . $offset_animation . '>';
                echo $pageNav->getPagesLinks();
              echo '</'.$shell_tag.'>';

            }
        $layout_html = str_replace("{|f-joom_pagination|}", ob_get_contents(), $layout_html);
        ob_end_clean();
      }
    }
     else {
         $layout_html = str_replace("{|f-joom_pagination|}", '', $layout_html);
    }


      //alphabetical
    if($mod_type == 0){
      if(strpos($layout_html,"{|f-joom_alphabetical|}")){
        $field = new stdClass();
        $field->db_field_name = 'joom_alphabetical';
        $field_styling = get_field_styles_without_Style($field, $layout);
        $field_styling .= 'display:block;' ;
        $hover_styling = get_hover_css_style($field, $layout_params);
        echo $hover_styling;
        $custom_class = get_field_custom_class($field, $layout);
        $offset_animation = get_field_offset_animation($field, $layout);
        $shell_tag = isset($layout_params['all_instance_layout_params']['fields']['label_tag_joom_alphabetical'])?$layout_params['all_instance_layout_params']['fields']['label_tag_joom_alphabetical']:'span';

        ob_start();
        ?>
          <script type="text/javascript">
                     jQuerOs(document).ready(function() {
                      jQuerOs('#alphabetical').attr('style','<?php echo $field_styling;?>');
                    });
          </script>
        <?php
        if(isset($list_str)){
          echo '<'.$shell_tag.' id="alphabetical"  class="' . $custom_class . ' ' . $field->db_field_name . '" ' . $offset_animation . '>';
          
              foreach($list_str as $a){
                    echo $a;
              }
            
          echo '</'.$shell_tag.'>';
        }
        $layout_html = str_replace("{|f-joom_alphabetical|}", ob_get_contents(), $layout_html);
        ob_end_clean();
      }
    }
     else {
         $layout_html = str_replace("{|f-joom_alphabetical|}", '', $layout_html);
    }

      
    
      //start render order by
    if($mod_type == 0 && $task != 'all_categories' && !empty($instancies)){
      if(strpos($layout_html,"{|f-category_order_by|}")){
          $field = new stdClass();
        $field->db_field_name = 'category_order_by';
        $field_styling = get_field_styles($field, $layout);
        $field_styling = substr_replace($field_styling, ' display:block;"', strlen($field_styling)-1, strlen($field_styling));
        $hover_styling = get_hover_css_style($field, $layout_params);
        echo $hover_styling;
        $custom_class = get_field_custom_class($field, $layout);
        $offset_animation = get_field_offset_animation($field, $layout);
        //check access
        $order_by_params = $layout_params['cat_layout_params'];
        
        if(isset($order_by_params['fields']['access_category_order_by'])
            && $order_by_params['fields']['access_category_order_by'] != '1'){
          $user = JFactory::getUser();
          if(!checkAccess_cck($order_by_params['fields']['access_category_order_by'], $user->groups, $layout->fk_eid, 'fields')){
            $layout_html = str_replace("{|f-category_order_by|}", '', $layout_html);
          }
        }
        
        ob_start();
        if(isset($order_by_params['fields']['category_order_by_published']) 
          && $order_by_params['fields']['category_order_by_published'] == 'on'){
            
        if(count($instancies) > 0 && isset($order_by_params['views']['sortField'])
                            && !empty($order_by_params['views']['sortField'])
                            && count($order_by_params['views']['sortField']) > 1){
            $sort_arr['sortField'] = $order_by_params['views']['sortField'];
            if(isset($order_by_params['views']['sortType_category_order_by'])){
                $sort_arr['order_direction'] = $order_by_params['views']['sortType_category_order_by'];
            }elseif (isset($order_by_params['fields']['sortType_category_order_by'])) {
                    $sort_arr['order_direction'] = $order_by_params['fields']['sortType_category_order_by'];
            }
            //$sort_arr['order_direction'] = $order_by_params['views']['sortType_category_order_by'];
            
            $selected = (isset($order_by_params['views']['selected'])) ? $order_by_params['views']['selected'] : '' ;
            session_set_order_data($sort_arr['order_direction'], $selected, $layout->lid);
//            $session->set('order_direction', $sort_arr['order_direction']);
//            $session->set('selected', $selected);
            // var_dump($sort_arr['sortField']);
            ?>
          
            <div id="CckOrderBy<?php echo ($moduleId) ? '_cckmod_'.$moduleId : '' ;?>" class="orderTable <?php echo $custom_class;?> <?php echo $field->db_field_name; ?>" <?php echo $offset_animation; ?> <?php echo $field_styling; ?>>
                <form id="orderForm<?php echo ($moduleId) ? '_cckmod_'.$moduleId : '' ;?>" method="POST" action="<?php echo JRoute::_($_SERVER["REQUEST_URI"]); ?>" name="orderForm<?php echo ($moduleId) ? '_cckmod_'.$moduleId : '' ;?>">
                    <input type="hidden" id="order_direction<?php echo ($moduleId) ? '_cckmod_'.$moduleId : '' ;?>" name="order_direction" value="<?php echo $sort_arr['order_direction']; ?>" >
                    <a title="Click to sort by this column." onclick="javascript:allreordering(<?php echo ($moduleId) ? $moduleId : 0 ;?>);return false;" href="#">
                        <img alt="" src="<?php echo JURI::root() ?>/components/com_os_cck/images/sort_<?php echo strtolower($sort_arr['order_direction']); ?>.png" />
                    </a>
                    <?php
                    echo cck_lang_text("COM_OS_CCK_LABEL_ORDER_BY"); ?>
                    <select size="1" class="inputbox" onchange="order_field_submitbutton(<?php echo ($moduleId) ? $moduleId : 0 ;?>);" id="order_field" name="order_field">
                    <?php

                    foreach($sort_arr['sortField'] as $key){
                        
                      if(!isset($key['value'])){?>
                        <option  value="ceid" <?php if ($selected == 'ceid')echo 'selected="selected"'; ?> >Index</option>
                    <?php
                      }else{ ?>
                        <option  value="<?php echo $key['value'] ?>" <?php if ($selected == $key['value'])echo 'selected="selected"'; ?> >  <?php echo $key['text']; ?> </option>
                        <?php
                      }
                    } ?>
                    </select>
                </form>
            </div>
            <?php 
        }//else{
//          JFactory::getApplication()->enqueueMessage(cck_lang_text('COM_OS_CCK_LAYOUT_ORDER_BY_FIELDS_TOOLTIP'), 'notice');
//        }
      }
        $layout_html = str_replace("{|f-category_order_by|}", ob_get_contents(), $layout_html);
        ob_end_clean();
      } 
    }
     else {
         $layout_html = str_replace("{|f-category_order_by|}", '', $layout_html);
    }
    //end render order by
    require getSiteUniqueFiledViewPath('com_os_cck', 'custom_code');
      //start render custom code
//       if(count($layout_params['custom_fields'])){
//         foreach ($layout_params['custom_fields'] as $cust_key => $custom_field) {
//           if(strpos($layout_html,"{|f-custom_code_field_".$cust_key."|}")){
//             //check access
//             if(isset($custom_field['custom_code_field_'.$cust_key.'_access'])
//                 && $custom_field['custom_code_field_'.$cust_key.'_access'] != '1'){
//               $user = JFactory::getUser();
//               if(!checkAccess_cck($custom_field['custom_code_field_'.$cust_key.'_access'], $user->groups, $layout->fk_eid, 'fields')){
//                 $layout_html = str_replace("{|f-custom_code_field_".$cust_key."|}", '', $layout_html);
//                 continue;
//               }
//             }
// //            $dispatcher = JDispatcher::getInstance();
//             JPluginHelper::importPlugin('content');
//             $plug_row = new stdClass();
//             $plug_row->text = $custom_field['custom_code_field_'.$cust_key.'_custom_code'];
//             preg_match_all('#\{loadmoduleid [0-9]*\}#', $plug_row->text, $matches);
//             foreach($matches as $res){
//               foreach($res as $str){
                
//                 $moduleId = str_replace('{loadmoduleid ', '', $str);
//                 $moduleId = str_replace('}', '', $moduleId);

//                 $mod_row =  JTable::getInstance ( 'Module', 'JTable' );//load module tables and params

//                 if (! $mod_row->load ( $moduleId )) {

//                   throw new Exception($mod_row->getError (), 500);
//                 }

//                 $module = JModuleHelper::getModule($mod_row->module,$mod_row->title);
//                 $options  = array('style' => 'xhtml');
//                 $mod_html = JModuleHelper::renderModule($module,$options);

//                 $plug_row->text = str_replace($str, $mod_html, $plug_row->text);

//               }
//             }
//             // JFactory::getApplication()->triggerEvent('onContentPrepare', array('com_os_cck', &$plug_row, &$plug_params, 0));
//             $plug_row->text = str_replace("{|this_entity_filesfolder|}", JURI::root() . '/components/com_os_cck/files/entity_files_'.$layout->fk_eid.'/', $plug_row->text);
//             $plug_row->text = str_replace("{|cck_images_folder|}", JURI::root() . 'images/os-cck/', $plug_row->text);
//             $custom_field['custom_code_field_'.$cust_key.'_custom_code'] = $plug_row->text;
//             //if below fn works , that this is add_instance view
//             $code_type = $custom_field['custom_code_field_'.$cust_key.'_custom_code_type'];
//             if($code_type == 'SCRIPT'){
//               $custom_code = '<script type="text/javascript">';
//               $custom_code .= $custom_field['custom_code_field_'.$cust_key.'_custom_code'];
//               $custom_code .= '</script>';
//               $layout_html = str_replace("{|f-custom_code_field_".$cust_key."|}", $custom_code, $layout_html);
//             }elseif($code_type == 'PHP'){
//               ob_start();
//               $custom_code = eval($custom_field['custom_code_field_'.$cust_key.'_custom_code']);
//               $layout_html = str_replace("{|f-custom_code_field_".$cust_key."|}", ob_get_contents(), $layout_html);
//               ob_end_clean();
//             }elseif($code_type == 'CSS'){
//               $custom_css = '<style>'.$custom_field['custom_code_field_'.$cust_key.'_custom_code'].'</style>';
//               $layout_html = str_replace("{|f-custom_code_field_".$cust_key."|}", $custom_css, $layout_html);
//             }else{
//               $custom_code = $custom_field['custom_code_field_'.$cust_key.'_custom_code'];
//               $layout_html = str_replace("{|f-custom_code_field_".$cust_key."|}", $custom_code, $layout_html);
//             }
//           }
//         }
//       }
      
      if(isset($layout_params['fields']['cck_pdf_button_published']) 
              && $layout_params['fields']['cck_pdf_button_published'] 
              && strpos($layout_html, "{|f-cck_pdf_button|}") !== false){
            $field = new stdClass();
            $field->db_field_name = 'cck_pdf_button';
            $field_styling = get_field_styles($field, $layout);
            
            $custom_class = get_field_custom_class($field, $layout);
            $offset_animation = get_field_offset_animation($field, $layout);
            $pdf_html = '';
            $pdf_html .='<span '.$field_styling.' class="col_box '.$custom_class. ' ' .$field->db_field_name . '"' . $offset_animation . '>';
            ob_start();
            require getSiteUniqueFiledViewPath('com_os_cck', 'pdf_button');
            $pdf_html .= ob_get_contents();
            ob_end_clean();
            $pdf_html .= '</span>';
            $layout_html = str_replace("{|f-cck_pdf_button|}", $pdf_html, $layout_html);
      }
      
      //end render 
      if(isset($layout_params['attachedModule'])){
        foreach ($layout_params['attachedModule'] as $attachedModule) {
          if($attachedModule){
            if(strpos($layout_html,"{|m-".$attachedModule->id."|}")){
                $field = new stdClass();
              $field->db_field_name = 'm_'.$attachedModule->id;
              $field_styling = get_field_styles($field, $layout);
              $custom_class = get_field_custom_class($field, $layout);
              $offset_animation = get_field_offset_animation($field, $layout);
              $module = JModuleHelper::getModule($attachedModule->type,$attachedModule->title);
              $options  = array('style' => 'xhtml');
              $html = '<div class="'.$custom_class.'" '. $offset_animation . ' ' .$field_styling.'>'.JModuleHelper::renderModule($module,$options).'</div>';
              $layout_html = str_replace("{|m-".$attachedModule->id."|}", $html, $layout_html);
            }
          }
        }
      }
      echo $layout_html;
      
    ?>
    <?php
        if (false && $layout_params['views']['cat_hits'] == 1) {
            ?>
        <div class="cat_hits">
            <div class="col_inline">
                <?php echo cck_lang_text("COM_OS_CCK_LABEL_HITS"); ?>
            </div>z
            <div class="col_inline">
                <?php echo $instance->hits; ?>
            </div>
        </div>
            <?php
        }
    ?>
    </div>
    <div>
       <input class="inputbox" type="hidden" name="bid[]" size="0" maxlength="0"
           value="<?php if(isset($instance))echo $instance->eiid; ?>"/>
    </div>
  </div>
  <?php 
            
    //var_dump($layout_params['catid']);
  if ($is_exist_sub_categories) {
    if($layout_params['views']['sub_category_level'] > 0){
      if(!function_exists('showCategorySubCat')){

          function showCategorySubCat($id, $cat_all, $params, $Itemid, $layout,$layout_params, $option, $lid, $layoum_mardgin = 0, $menu=false) {
          
          
              
          $layoum_mardgin+=25;
          for ($i = 0; $i < count($cat_all); $i++) { 
              
              
              
            if (($id == $cat_all[$i]->parent_id) && ($cat_all[$i]->display == 1) && $layout_params['catid'] == $cat_all[$i]->parent_id) {


                $field = new stdClass();
                $field->db_field_name = "l".$layout_params['views']['subcategories_layout'];
                $field_styling = get_field_styles($field, $layout);
                $custom_class = get_field_custom_class($field, $layout);
                $offset_animation = get_field_offset_animation($field, $layout);
                $button_style = array('field_styling'=>$field_styling,'custom_class'=>$custom_class,'offset_animation'=>$offset_animation);
                $shell_tag = isset($layout_params['fields']['field_tag_'.$field->db_field_name])?$layout_params['fields']['field_tag_'.$field->db_field_name]:'div';
                //pass the layout to display the subcategories when going to the category
                //var_dump($layout_params['views']['subcategories_layout']);
                if(!isset($layout_params['parent_layout_params']['views']['category_layout']) || $layout_params['parent_layout_params']['views']['category_layout'] == NULL){
                    $layout_params['views']['category_layout'] = $lid;
                } else {
                    $layout_params['views']['category_layout'] = $layout_params['parent_layout_params']['views']['category_layout'];
                }
                
                $style = 'style="margin-left:'.$layoum_mardgin.'px"';
                if($layout_params['views']['subcategories_layout'] == $layout->lid){
                    $attach_layout = $layout;
                }else{
                    $attach_layout = $layout_params['views']['subcategories_layout'];
                }
                if($layout_params['views']['subcategories_layout'] > -1){
                    echo '<'.$shell_tag.' class="cat_instance_body_child'.$custom_class.'" '. $offset_animation . ' ' .$style.'>';

                    Category::show_attached_layout($option, $attach_layout, $layout->fk_eid, $layout_params,
                                           1,'',$cat_all[$i]->cid, $cat_all, $button_style, 10, $Itemid, $params, $menu);
                    echo '</'.$shell_tag.'>';
                }
              
              showCategorySubCat($cat_all[$i]->cid, $cat_all, $params, $Itemid, $layout,$layout_params, $option, $lid, $layoum_mardgin, $menu);
              
            }else{
                
            }//end if
          }//end for
        }
      }

      showCategorySubCat($layout_params['catid'], $categories, $params, $Itemid, $layout,$layout_params, $option, $lid, 0, $menu);
      //Category::showCategory($option, $catid,$lid,$category_params);
    }
  } ?>
  <script>
    jQuerOs("div[class *='cck-row-'], div[id *='cck_col-']").each(function(index, el) {
      jQuerOs(el).attr("style",jQuerOs(el).data("block-styling"))
    });
    jQuerOs("div[class *='cck-row-']").addClass('row');
    //jQuerOs("*[class*='hide-field-name-'] , .delete-field, .delete-row, .delete-layout").remove();
    jQuerOs(".delete-field, .delete-row, .delete-layout").remove();
    jQuerOs(".content-row[class*='cck-row-']").each(function(index, el) {
      if(!jQuerOs(el).find(".drop-item").length){
        jQuerOs(el).remove();
      }
    });
    //jQuerOs("span.cck-help-string").remove();
    jQuerOs(function () {
      jQuerOs('[data-toggle="tooltip"]').tooltip()
    })
    
    // jQuerOs('.cck-body[lid=<?php //echo $layout->lid; ?>] a').on('click', function(e){ 15.10.2025  not undertend why ? - -not work for edite buttomn in layout
    //     event.preventDefault()
    //     var click_href = jQuerOs(this).attr('href')
    //     jQuerOs(this).parents('.cck-body').each(function(el){
    //         if(!jQuerOs(this).hasClass('instance_body')){
    //             jQuerOs.ajax({
    //                     dataType: "json",
    //                     type: 'POST',
    //                     url: '<?php echo JUri::root(); ?>index.php?option=com_os_cck&format=raw',
    //                     data: {
    //                         task: "setLayoutInSessionAjax",
    //                         lid: <?php //echo $layout->lid; ?>,
    //                     },
    //                     success: function(data){ 
    //                         if(e.shiftKey){
    //                             window.open(click_href);
    //                         }else{
    //                             window.location.href = click_href
    //                         }
    //                     }
    //               });
    //         }
    //     })
    // })
  </script>
</div>
 
