<?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 AdminViewReview{

  static function showReviews($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_COM_TITLE_REVIEWS') . "</div>";
    $app = JFactory::getApplication();
    $app->JComponentTitle = $html;
    $onclick = (version_compare(JVERSION, "1.6.0", "lt")) ? "checkAll(" . count($rows_item) . ");" : "Joomla.checkAll(this);";


    $form_name = 'adminForm';
    $form_id = 'adminForm' ;
    $table = new os_cckViewTable($form_name, $form_id, '', 'index.php?option=com_os_cck&task=manage_review', 'manage_review');
    $what_check = 'instance' ;
    $table->create_button('Publish', 'publish_reviews', JURI::root() . 'components/com_os_cck/images/published.png', 
      'fa-check','',$what_check);
    $table->create_button('Unpublish', 'unpublish_reviews', JURI::root() . 'components/com_os_cck/images/btn_d.png', 
      'fa-close','',$what_check);
    $table->create_button(cck_lang_text("COM_OS_CCK_LABEL_DELETE"), 'delete_review', 
      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>';

    $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 review 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_REVIEW_DATE'), 'created', $sort_arr,protectInjectionWithoutQuote('task')), '', 'admin-order-title-date');   
    $table->create_first_row_collumn(cck_sort_head(cck_lang_text('COM_OS_CCK_LABEL_ENTITY'), 'inst_entity', $sort_arr,protectInjectionWithoutQuote('task')), '', 'admin-order-title-email');   
    $table->create_first_row_collumn(cck_lang_text('COM_OS_CCK_LABEL_PUBLIC'), '', 'admin-order-title-date'); 
    $table->create_first_row_collumn(cck_lang_text('COM_OS_CCK_LABEL_APPROVED'), '', 'admin-order-title-date'); 
    $table->create_first_row_collumn(cck_lang_text('COM_OS_CCK_LABEL_CONTROL'), '', 'admin-order-title-date'); 

    for($i = 0; $i < count($rows_item); $i++) {
      $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="#edit_review" onClick="return cckSendForm(\'cb'.$i
        .'\',\'edit_review\',\''.$form_name.'\')">'.$row->eiid.'</a>';


      //add additional fields data to reviewManager
      $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;

      $task = $row->published ? 'unpublish_reviews' : 'publish_reviews';
      $alt = $row->published ? 'Unpublish' : 'Publish';
      $img = $row->published ? 'tick.png' : 'publish_x.png';
      $task1 = $row->approved ? 'unapprove_reviews' : 'approve_reviews';
      $alt1 = $row->approved ? 'Unapproved' : 'Approved';
      $img1 = $row->approved ? 'tick.png' : 'publish_x.png';
      $img = "components/com_os_cck/images/{$img}";
      $img1 = "components/com_os_cck/images/{$img1}";

      $temp_row_data = '';

      if (JFactory::getUser()->authorise('publish_reviews', 'com_os_cck')){
        $temp_row_data .= '<a href="javascript: void(0);" onClick="return cckSendForm(\'cb'.$i.'\',\''.$task.'\',\''.$form_name.'\')">';
      }
      
      $temp_row_data .= '<img src="'.$img.'" width="12" height="12" border="0" alt="'.$alt.'"/>';
      if (JFactory::getUser()->authorise('publish_reviews', 'com_os_cck')){
        $temp_row_data .= '</a>';
      }
      $request_data[] = $temp_row_data;

      $temp_row_data = '';
      if (JFactory::getUser()->authorise('publish_instances', 'com_os_cck')){
        $temp_row_data .= '<a href="javascript: void(0);" onClick="return cckSendForm(\'cb'.$i.'\',\''.$task1.'\',\''.$form_name.'\')">';
      }
      
      $temp_row_data .= '<img src="'.$img1.'" width="12" height="12" border="0" alt="'.$alt.'"/>';
      if (JFactory::getUser()->authorise('publish_instances', 'com_os_cck')){
        $temp_row_data .= '</a>';
      }
      $request_data[] = $temp_row_data;

      if ($row->checked_out) {
        $request_data[] = $row->editor;
      }else{
        $request_data[] = '';
      }


      $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_reviews');
      
    }

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

    echo $table->viewTable();

  }

  static function editReview($option, $entityInstance, $layout, $layout_params){
    global $session, $os_cck_configuration, $app, $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_COM_TITLE_REVIEWS') . "</div>";
    $app = JFactory::getApplication();
    $app->JComponentTitle = $html;
    // $doc->addStyleSheet(JUri::root() . "/components/com_os_cck/assets/css/fine-uploader-new.css");
//    JUri::root() . "/components/com_os_cck/assets/js/fine-uploader.js" 
            
    require getLayoutPathCCK::getAdminLayoutViewPath('com_os_cck','edit_review');
  }

  static function showParentInstance($option, $entityInstance, $layout, $layout_params){
    global $session, $os_cck_configuration, $app, $doc;

    $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_COM_TITLE_REVIEWS') . "</div>";
    $app = JFactory::getApplication();
    $app->JComponentTitle = $html;
    require getLayoutPathCCK::getAdminLayoutViewPath('com_os_cck','parent_instance');
  }

}
