<?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
* @description OrdaSoft Content Construction Kit
*/


class AdminViewRequest{

  static function showRequests($option, & $rows_item, & $clist,
      & $publist, & $search, & $pageNav, & $sort_arr, $show_fields,$entity_list)
  {
      global $doc, $user, $app, $session, $db, $entityFieldLists, $entities_type;

      $db = new os_cckDb();
      $html = "<div class='os_cck_caption' ><img src='./components/com_os_cck/images/os_cck_logo.png' alt ='Config' />" . cck_lang_text('COM_OS_CCK_ADMIN_LABLE_SUBMISSIONS') . "</div>";
      $app = JFactory::getApplication();
      $app->JComponentTitle = $html;
      $onclick = "Joomla.checkAll(this);";

      $form_name = 'adminForm';
      $form_id = 'adminForm' ;
      $table = new os_cckViewTable($form_name, $form_id, '', 'index.php?option=com_os_cck&task=show_requests', 'show_requests');
      $what_check = 'instance' ;
      $table->create_button(cck_lang_text("COM_OS_CCK_LABEL_DELETE"), 'remove_request_item', JURI::root() . 'components/com_os_cck/images/btn_d.png', 'fa-close','',$what_check);
      $table->create_button(cck_lang_text("COM_OS_CCK_LABEL_BUTTON_MARK_AS_VIEWED"), 'is_readed', JURI::root() . 'components/com_os_cck/images/published.png', 'fa-check','',$what_check);
      
      $table->create_filters('<div class="search_block"><input type="text" placeholder="'.cck_lang_text('COM_OS_CCK_SHOW_SEARCH').'" name="search" value="'.$search.'" class="inputbox" onChange="document.adminForm.submit();"/><button type="submit" class="cck_search_button" title="" data-original-title="Search"><span class="icon-search"></span></button></div>');
      $table->create_filters('<div>'.$publist.'</div>');
      $table->create_filters('<div>'.$clist.'</div>');
      $table->create_filters('<div>'.$entity_list.'</div>');
      
      $table->title_class = 'admin-request-title';
      $table->first_row_custom_class = 'admin-request-standart-fields';
      $table->limitbox = '<div class="btn-group pull-right hidden-phone">
          <label for="limit" class="element-invisible">'. cck_lang_text('OS_LIB_SEARCH_SEARCHLIMIT_DESC') .'</label>'. $pageNav->getLimitBox() .'</div>';


      //prepere fields for first row: Titles
      $table->create_first_row_collumn(JHtml::_('grid.checkall'), '', 'admin-order-title-checkbox');  
      $table->create_first_row_collumn(cck_sort_head(cck_lang_text('COM_OS_CCK_LABEL_INSTANCE_ID'), 'inst_id', $sort_arr,protectInjectionWithoutQuote('task')), '', 'admin-order-title-id'); 

      //add aditional fields to Request Manager
      os_cck_site_controller::create_first_row_collumns_titles($table,$show_fields,$sort_arr);

      $table->create_first_row_collumn(cck_sort_head(cck_lang_text('COM_OS_CCK_REQUEST_DATE'), 'created', $sort_arr,protectInjectionWithoutQuote('task')), '', 'admin-order-title-email');   
      $table->create_first_row_collumn(cck_sort_head(cck_lang_text('COM_OS_CCK_REQUEST_ENTITY'), 'entity', $sort_arr,protectInjectionWithoutQuote('task')), '', 'admin-order-title-date');


      //prepere fields Data
      for($i = 0; $i < count($rows_item); $i++) { 
        
        //prepere fields for first row: Data
        $request_data = array();
        $row = $rows_item[$i];

        if ($row->checked_out && $row->checked_out != $user->id) {
          $request_data[] = '';
        
        } else {
          $request_data[] =  JHTML::_('grid.id',$i, $rows_item[$i]->eiid, ($rows_item[$i]->checked_out && $rows_item[$i]->checked_out != $user->id), 'eiid');
        }
        $request_data[] = '<a href="#show_request_item" onClick="return cckSendForm(\'cb'.$i.'\',\'show_request_item\',\''.$form_name.'\')">'.$row->eiid.'</a>';

        //add aditional fields data to Request Manager
        $request_data = array_merge($request_data, os_cck_site_controller::create_first_row_collumns_data($table,$rows_item[$i],$show_fields) );  

        $request_data[] = $rows_item[$i]->created;
        $request_data[] = $rows_item[$i]->entity;

        $style = ''; 
        $class = 'row' . ($i % 2) . ' '; 
        if($rows_item[$i]->notreaded == 1){
            $class .= ' admin-order-not-readed';
        }
        $table->create_first_row_data($request_data, $style, $class);

        //second row add always(also with empty data), count the second rows Must Be === count first row !!!!!!
        os_cck_site_controller::second_row_prepere($table,$rows_item[$i],'show_in_requests');

      }

//         $table->paginator = $pageNav->getListFooter();
      $table->pageNav = $pageNav;

      echo $table->viewTable();

  }

  static function showRequestItem($option, & $entityInstance, & $str_list, $recurse){
    global $os_cck_configuration,$user, $app, $session, $doc, $entities_type;

    $html = "<div class='os_cck_caption' ><img src='./components/com_os_cck/images/os_cck_logo.png' alt ='Config' />" . cck_lang_text('COM_OS_CCK_ADMIN_LABLE_SUBMISSIONS') . "</div>";
    // $app->JComponentTitle = $html;
    // $doc->addStyleSheet(JURI::root()."components/com_os_cck/assets/css/jquerOs-ui.min.css");
    // $doc->addStyleSheet(JURI::root()."components/com_os_cck/assets/css/admin_style.css");
    
    // $doc->addScript(JURI::root()."components/com_os_cck/assets/js/jquerOs-ui.min.js");
    
    // $doc->addScript(JUri::root().'components/com_os_cck/assets/js/jquery.raty.js');
    // $doc->addStyleSheet( JUri::root().'/components/com_os_cck/assets/lightbox/css/lightbox.css');
    // $doc->addScriptDeclaration('jQuerOs=jQuerOs.noConflict();');
    // $doc->addScript(JUri::root().'/components/com_os_cck/assets/lightbox/js/lightbox-2.6.min.js');
    // $doc->addScript('//maps.googleapis.com/maps/api/js');
    
    $form_name = 'adminForm';
    $form_id = 'adminForm' ;
    $table = new os_cckViewTable($form_name, $form_id, 'add_entity_form wrapper_add_entity', 
      'index.php?option=com_os_cck&task=show_requests', '');
    $table->create_button('Back', 'show_requests', '', 'fa-chevron-left');
    

    if($recurse == 1){
      $div_id = 'instance';
    }else{
      $div_id = 'psubmission';
    }  
    $html = '';
    ob_start();
    ?>
    <div id="<?php echo $div_id; ?>">
      <?php
      
      $layout_params = $str_list['layout_params'];
      $layout = $str_list['layout'];
      $bootstrap_version = $session->get( 'bootstrap','2');

      $layout->layout_html = $layout->getLayoutHtml($bootstrap_version);
      
      $layout_html = urldecode($layout->layout_html);
      $field_from_params = $layout_params["fields"];
      $fields_list = $str_list['extra_fields_list'];

      //add child selects to layout
      $addChildSelectToLayout = addChildSelectToLayout($fields_list, $entityInstance, $layout_params, $layout_html, $layout);
      $layout_html = $addChildSelectToLayout['layout_html'];
      $layout_params = $addChildSelectToLayout['layout_params'];
      $parent = $addChildSelectToLayout['select_parent'];
      $layout->params = serialize($layout_params);
      //add child selects to layout
      $hover_animated = array();
      $moduleId = 0;
      $Itemid = 0;
      $layout_params['custom_fields'] = unserialize($layout->custom_fields);
      $child_entities = (isset($layout_params['child_entities'])) ? $layout_params['child_entities'] : '';

      $layout_html = preg_replace("/{\|l-[0-9]*?\|}/", '', $layout_html); //remove child layouts - like add review
      $layout_html = preg_replace("/{\|f-cck_mail_[0-9]*?\|}/", '', $layout_html); //remove mail fields
      $layout_html = str_replace(array("{|f-cck_send_button|}","Button Send"), '', $layout_html);//button remove
      $layout_html = str_replace(array("{|f-cck_add_to_cart_button|}","Add to cart button"), '', $layout_html); //cart remove
      $layout_html = str_replace(array("{|f-cck_action_button_1|}",
          "{|f-cck_action_button_2|}","{|f-cck_action_button_3|}","Action button"), '', $layout_html); //actions buttons remove
      
      $layout_html = $layout->getLayoutViews($entityInstance, $fields_list, $layout_html, $layout_params, $hover_animated, $option, $child_entities, $moduleId, $Itemid, 10);


      echo $layout_html;
      
      ?>
    </div>
    <?php
    
    if(!empty($str_list['parent_instance']) && is_array($str_list['parent_instance'])){
        foreach($str_list['parent_instance'] as $parent_instance){
            AdminRequest::showRequestItem($option, $parent_instance->fid_parent, 1);
        }
    }
    $html = ob_get_contents();
    ob_end_clean();
    $table->set_custom_html($html);
    echo $table->viewTable();
    
  }

}
