<?php
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '
    .basename(__FILE__).' is not allowed.' );

/**
 *
 * @package  MediaLibrary
 * @copyright 2021 Andrey Kvasnevskiy-OrdaSoft(akbet@mail.ru); Rob de Cleen(rob@decleen.com);
 * Homepage: https://ordasoft.com
 * @version: 4.6 Free
 * @license GNU General Public license version 2 or later; see LICENSE.txt
 *
 */

global $my;
$part_link = '';
if ($task == 'search'){
    foreach ($_GET as $key => $value) {
        $part_link .= "&".$key."=".$value;
    }
    $part_link = substr($part_link, 1);
}if ($task == 'alonecategory'){
    $part_link = $mosConfig_live_site.'/index.php?option=com_medialibrary&task=alonecategory&catid='.$catid
          .'Itemid='. $Itemid;
}


$user=Jfactory::getUser();
if(isset($_REQUEST['userId'])){
    if($option != 'com_medialibrary' and $_REQUEST['userId'] == $user->id) showTabs();
}
else{
    if($option != 'com_medialibrary') showTabs();
}

if($cat_name!=""){
            $category_url_part = "task=".$cat_name;
        } else{
            $category_url_part = "task=alonecategory&amp;catid=".$catid;
        }

        $session = JFactory::getSession();
        $arr = $session->get("array","default");
        $sort_arr['order_field'] = $params->get('sort_arr_order_field');
        $sort_arr['order_direction'] = $params->get('sort_arr_order_direction');
?>
<?php positions_ml($params->get('singlecategory01')); ?>
<?php positions_ml($params->get('search_mod1')); ?>
<div  class="ml_search_result_page ml_search_result_list" id="medialibrary">
  <?php 
if($pageNav->total > 0){
  $song_for = trim(protectInjectionWithoutQuote('searchtext')) ;
  $slash = '';
  if(!empty($song_for))$slash = '/';
  positions_ml($params->get('singleuser02'));
  positions_ml($params->get('singlecategory02'));
  positions_ml($params->get('search_mod2'));
   echo "<div class='search_result_title'><h2 class='search_expression'>" .$song_for . "</h2><span>$slash</span><h5>"._MEDIALIBRARY_FOUND." <strong>". $pageNav->total ."</strong> ".
    _MEDIALIBRARY_HEADER_NUMBER."</h5></div>" ;
}
?>
<?php positions_ml($params->get('singleuser03')); ?>
<?php positions_ml($params->get('singlecategory03')); ?>
<?php positions_ml($params->get('search_mod3')); ?>

    <div class="row media-tools-container">

        <?php
        ?>
            <div class="component_logo ml-collumn-lg-2 ml-collumn-md-2 ml-collumn-sm-3 ml-collumn-xs-12">
            <img src="<?php echo $currentcat->img; ?>" align="<?php echo $currentcat->align; ?>" alt="?" />
            </div>
        <?php?>

        <div class="button-output-information ml-collumn-lg-10 ml-collumn-md-10 ml-collumn-sm-9 ml-collumn-xs-12">
                <?php
                if($currentcat->img != null && $currentcat->align == 'left' && $params->get( 'show_cat_pic') ){
                ?>
                <div class="cat_image">
                  <img src="<?php echo $currentcat->img; ?>" align="<?php echo $currentcat->align; ?>" alt="?"/>
                </div>
            <?php
                }
                if($currentcat->descrip != null && $currentcat->align == 'left' && $params->get( 'show_cat_pic') ){
                ?>
                <div class="description">
                  <?php echo $currentcat->descrip; ?>
                </div>
            <?php
              }
            ?>
            <div  class="ml_rss">

                <a href="<?php echo sefRelToAbs(
                            "index.php?option=com_medialibrary&task=show_rss_categories&Itemid=" 
                            . $Itemid); ?>">
                            <img src="<?php echo $mosConfig_live_site; 
                            ?>/components/com_medialibrary/images/rss3.gif" alt="All categories RSS" 
                            align="right" title="All categories RSS"/>
                        </a>

            </div>
            <div class="ml_pdf">
              <?php if ($params->get( 'show_input_print_pdf')){?>
                <a onclick="window.open(this.href,'win2',
                'status=no,toolbar=no,scrollbars=yes,titlebar=no,menubar=no,resizable=yes,width=640,height=480,directories=no,location=no'
                ); return false;" rel="nofollow"
                href="<?php echo sefRelToAbs("index.php?option=com_medialibrary&".$category_url_part."&Itemid="
                  .$Itemid."&limitstart=".$limitstart."&printItem=pdf");?>" title="PDF"  rel="nofollow">
                <i class="fa fa-file-pdf-o"></i>
                <!--img src="<?php //echo  $mosConfig_live_site;
                  ?>/components/com_medialibrary/images/pdf_button.png" alt="PDF"  -->
                </a>
              <?php } ?>
            </div>

            <div class="ml_prnt">
              <?php if ($params->get( 'show_input_print_view')){?>
                <a onclick="window.open(this.href,'win2',
                  'status=no,toolbar=no,scrollbars=yes,titlebar=no,menubar=no,resizable=yes,width=640,height=480,directories=no,location=no'
                  );  return false;" rel="nofollow"
                href="<?php echo sefRelToAbs("index.php?option=com_medialibrary&".$category_url_part."&Itemid="
                  .$Itemid."&limitstart=".$limitstart."&printItem=print&amp;tmpl=component");?>"
                  title="Print"  rel="nofollow">
                  <i class="fa fa-print"></i>
                  <!--img src="<?php //echo  $mosConfig_live_site;
                  ?>/components/com_medialibrary/images/printButton.png" alt="Print"  -->
                </a>
              <?php } ?>
            </div>

          <div class="ml_mail">
          <?php if ($params->get( 'show_input_mail_to')){
                  if (version_compare(JVERSION, '1.6', 'lt')) {?>
                      <a href="<?php echo $mosConfig_live_site;
                      ?>/index.php?option=com_mailto&amp;tmpl=component&amp;link=<?php $url = JFactory::getURI();
                        echo base64_encode($url->toString()); ?>"
                           title="E-mail"
                           onclick="window.open(this.href,'win2',
                            'width=400,height=350,menubar=yes,resizable=yes'); return false;">
                            <i class="fa fa-envelope-o"></i>
                            <!--img src="<?php //echo $mosConfig_live_site;
                            ?>/components/com_medialibrary/images/emailButton.png" alt="E-mail"  /-->
                      </a>
                  <?php }else{
                        require_once JPATH_SITE . '/components/com_mailto/helpers/mailto.php';
                          $url = JFactory::getURI();
                          $url_c = $url->toString();
                          $link = 'index.php?option=com_mailto&amp;tmpl=component&amp;Itemid='.$Itemid.'&link='
                            .MailToHelper::addLink($url_c);?>
                            <a href="<?php echo sefRelToAbs($link);?>"
                            title="E-mail"
                            onclick="window.open(this.href,'win2',
                              'width=400,height=350,menubar=yes,resizable=yes'); return false;">
                          <?php
                            if (version_compare(JVERSION, "1.6.0", "lt")) { ?>
                              <i class="fa fa-envelope-o"></i>
                              <!--img src="<?php //echo $mosConfig_live_site;
                                ?>/media/system/images/emailButton.png" alt="E-mail" /-->
                                <?php }
                            else { ?>
                              <i class="fa fa-envelope-o"></i>
                              <!--img src="<?php //echo $mosConfig_live_site;
                                ?>/components/com_medialibary/images/emailButton.png" alt="E-mail" /-->
                             <?php  } ?>
                             </a>
                      <?php }
                              }?>
                      </div>
                </div>
  
<?php positions_ml($params->get('singlecategory03')); ?>

        <?php
        if(strpos($currentcat->header,_MEDIALIBRARY_LABEL_SEARCH) && count($rows) == 0  ){
        ?>
          <div class="componentheading<?php echo $params->get( 'pageclass_sfx' ); ?>">
                  <?php echo _MEDIALIBRARY_NOTHING_FOUND ; ?>
          </div>
        <?php
          return;
        }

        if($params->get('show_search')){
        ?>
          <div class="search-block<?php echo $params->get( 'pageclass_sfx' ); ?> ml-collumn-lg-10 ml-collumn-md-10 ml-collumn-sm-9 ml-collumn-xs-12">
                <?php

                    if($params->get('search_fieldshow')){
                        echo HTML_medialibrary::displaySimpleSearch();
                    }

                    if($params->get('advsearch_show')){

                        $href=sefRelToAbs(
                          "index.php?option=com_medialibrary&task=show_search&catid=0&Itemid=".$Itemid);
                        echo '<div style="font-size: 16px;line-height: 0.9em;" class="search-button"><a href="'.$href.'">'.
                          _MEDIALIBRARY_LABEL_SEARCH.'</a></div>';
                    }
                    $link = sefRelToAbs('index.php?option=com_medialibrary&task=show_search&catid='.
                    $catid. '&Itemid='. $Itemid);
                ?>
       </div>
        <?php
        }
        ?>

<?php
    $total = count($rows);
    if($total > 0) {

      $view_type =$params->get('view_type');
      if(isset($_REQUEST['viewtype'])) {
         $view_type_selected=mosGetParam($_REQUEST, 'viewtype');
         $session =JFactory::getSession();
         $session->set('view_type_selected', $view_type_selected);
      }
      else {
         $session =JFactory::getSession();
         $view_type_selected=$session->get('view_type_selected');
      }

      $is_show_view_type = false;
      //add view type selecting
       if($view_type==2){
         $redirect_url = $_SERVER["REQUEST_URI"];
         $pos = strpos($redirect_url,"viewtype");
         if( $pos !== FALSE ){
          if( strpos($_SERVER["REQUEST_URI"],'=') !== false){
           $redirect_url1 = str_replace("viewtype=1","viewtype=0",$redirect_url);
           $redirect_url2 = str_replace("viewtype=0","viewtype=1",$redirect_url);
          }else if( strpos($_SERVER["REQUEST_URI"],',') !== false)
          {
           $redirect_url1 = str_replace("/viewtype,1","/viewtype,0",$redirect_url);
           $redirect_url2 = str_replace("/viewtype,0","/viewtype,1",$redirect_url);
          }else
          {
           $redirect_url1 = str_replace("/viewtype:1","/viewtype:0",$redirect_url);
           $redirect_url2 = str_replace("/viewtype:0","/viewtype:1",$redirect_url);
          }
         } else {
          if(strpos($_SERVER["REQUEST_URI"],'=') !== false){
           $redirect_url1 = $_SERVER["REQUEST_URI"].'&viewtype=0';
           $redirect_url2 = $_SERVER["REQUEST_URI"].'&viewtype=1';
          }else if( strpos($_SERVER["REQUEST_URI"],',') !== false)
          {
           $redirect_url1 = $_SERVER["REQUEST_URI"].'/viewtype,0';
           $redirect_url2 = $_SERVER["REQUEST_URI"].'/viewtype,1';
          }else
          {
           $redirect_url1 = $_SERVER["REQUEST_URI"].'/viewtype:0';
           $redirect_url2 = $_SERVER["REQUEST_URI"].'/viewtype:1';
          }
         }
         $is_show_view_type = true;
       }
       $sort_arr['order_field'] = $params->get('sort_arr_order_field');
       $sort_arr['order_direction'] = $params->get('sort_arr_order_direction');

                 ?>
<?php positions_ml($params->get('singleuser03'));?>
<?php positions_ml($params->get('singlecategory04')); ?>

        <div id="ShowOrderBy" class="order_by ml-collumn-lg-10 ml-collumn-md-10 ml-collumn-sm-9 ml-collumn-xs-12" >

        <form method="POST" action="<?php
          echo $_SERVER["REQUEST_URI"];?>" name="orderForm">

        <input type="hidden" id="order_direction" name="order_direction" value="<?php
          echo $sort_arr['order_direction']; ?>" >
        <a title="Click to sort by this column."
          onclick="ml_allreordering();return false;" href="#">
          <img alt="" src="<?php
            echo $mosConfig_live_site; ?>/components/com_medialibrary/images/sort_<?php
           if(isset($sort_arr['order_direction']))
              echo $sort_arr['order_direction'];
            else
              echo "asc"; ?>.png"></a>
          <?php echo _MEDIALIBRARY_LABEL_ORDER_BY;?> <select size="1" class="inputbox"
           onchange="javascript:document.orderForm.order_direction.value='asc';
            document.orderForm.submit();" id="order_field" name="order_field">
          <option  value="date" <?php if($sort_arr['order_field'] == "release_date")
            echo 'selected="selected"'; ?> >  <?php echo _MEDIALIBRARY_RELEASE_DATA;
            ?> </option>
          <option value="price" <?php if($sort_arr['order_field'] == "price")
            echo 'selected="selected"'; ?> > <?php
            echo _MEDIALIBRARY_LABEL_PRICE; ?></option>
          <option value="title" <?php if($sort_arr['order_field'] == "title")
            echo 'selected="selected"'; ?> > <?php
            echo _MEDIALIBRARY_LABEL_TITLE; ?></option>
          <?php if($params->get('media_type') == 'book' ) { ?>
            <option value="people_name" <?php
              if($sort_arr['order_field'] == "people_name")
                echo 'selected="selected"'; ?> > <?php
                echo _MEDIALIBRARY_SHOW_SEARCH_FOR_AVTORS; ?></option>
          <?php } ?>
          <?php if($params->get('media_type') == 'music' ) { ?>
            <option value="people_name" <?php
              if($sort_arr['order_field'] == "people_name")
                echo 'selected="selected"'; ?> > <?php
                echo _MEDIALIBRARY_SHOW_SEARCH_FOR_ARTIST; ?></option>
          <?php } ?>
          <?php if($params->get('media_type') == 'game' ||
                   $params->get('media_type') == 'video' ) { ?>
            <option value="people_name" <?php
              if($sort_arr['order_field'] == "people_name")
                echo 'selected="selected"'; ?> > <?php
                echo _MEDIALIBRARY_SHOW_SEARCH_FOR_STUDIO; ?></option>
          <?php } ?>
          </select>
        </form>

        </div>

    </div>


<?php positions_ml($params->get('singleuser04'));?>
<?php positions_ml($params->get('singlecategory05')); ?>

<?php
  if($view_type==0||($view_type==2 && ($view_type_selected==0 ||!isset($view_type_selected)))){
?>
    <div id="list">
       <div  class="basictable ml-basictable">
           <div class="table-row table-header-row">
                <div height="20" class="sectiontableheader<?php echo $params->get( 'pageclass_sfx' ); ?>">
                  <?php echo _MEDIALIBRARY_LABEL_COVER; ?>
                </div>
                <div height="20" class="sectiontableheader<?php echo $params->get( 'pageclass_sfx' ); ?>">
                  <?php echo _MEDIALIBRARY_LABEL_TITLE." / "._MEDIALIBRARY_SHOW_SEARCH_FOR_AVTORS; ?>
                </div>
                <div height="20" class="sectiontableheader<?php echo $params->get( 'pageclass_sfx' ); ?>">
                  <?php echo _MEDIALIBRARY_LABEL_MEDIA_TYPE; ?>
                </div>
                <?php
                if ( $catid != 0 ) {
                  ?>
                <div height="20" class="sectiontableheader<?php echo $params->get( 'pageclass_sfx' ); ?>">
                  <?php echo _MEDIALIBRARY_CATEGORIES_NAME; ?>
                </div>
                <?php
                }
                  ?>
                <?php
                if ( $params->get( 'show_rating' ) ) {
                  ?>
                  <div height="20" class="sectiontableheader<?php echo $params->get( 'pageclass_sfx' ); ?>">
                    <?php echo _MEDIALIBRARY_LABEL_RATING; ?>
                  </div>
                <?php
                }
                if ( $params->get( 'hits' ) ) {
                  ?>
                  <div width="30" height="20" class="sectiontableheader<?php
                    echo $params->get( 'pageclass_sfx' ); ?>" align="right">
                    <?php echo _MEDIALIBRARY_LABEL_HITS; ?>
                  </div>
                  <?php
                }
                if( $params->get('show_lendstatus')){
                ?>
                  <div height="20" class="sectiontableheader<?php echo $params->get( 'pageclass_sfx' ); ?>">
                    <?php echo _MEDIALIBRARY_LABEL_LEND_CB; ?>
                  </div>
                <?php
                }
                ?>
              </div>

            <?php
            $available = false;
            $k = 0;
            foreach ($rows as $row) {
              $link = 'index.php?option=com_medialibrary&task=view&id='. $row->id . '&catid='. $catid
                .'&Itemid='. $Itemid;     //
              $imageURL = $row->imageURL;
            ?>


              <div class="<?php echo $tabclass[$k]; ?> table-row" >
                  <div style="padding-left:5px; padding-top:5px; padding-right:10px;" class="table-cell">
                      <a href="<?php echo sefRelToAbs($link); ?>" >
                          <div class="ramka">
                          <?php
                          if ($imageURL != '') {
                                  if($imageURL != '' && substr($imageURL,0,4) != "http"){
                                      $imageURL = $mosConfig_live_site . '/'. $imageURL;;
                                  }
                                  echo '<img style="height:'.$medialibrary_configuration['foto']['high'].'px;
                                    width:'.$medialibrary_configuration['foto']['width'].'px" src="'
                                      .$imageURL . '" border="0">';
                                  } else {
                                        echo '<img  style="height:'
                                          .$medialibrary_configuration['foto']['high'].'px; "
                                          width:'.$medialibrary_configuration['foto']['width'].'px; " src="' .
                                         './components/com_medialibrary/images/'._MEDIALIBRARY_NO_PICTURE .
                                         '" alt="no-img_eng_big.gif" border="0"  />';
                                 }

                          ?>
                          </div> 
                      </a>
                  </div>
                  <div class="table-cell">

                      <?php if ($row->title!="") { ?>

                          <div>

                              <span class="responsive-title">
                              <?php echo _MEDIALIBRARY_LABEL_TITLE; ?>:
                              </span>

                              <a href="<?php echo JRoute::_($link, false); ?>" class="category<?php
                              echo $params->get( 'pageclass_sfx' ); ?>">
                              <?php echo $row->title; ?>
                              </a>

                          </div>

                      <?php } ?>

                      <?php
                     if (count($row->people_name) > 0) { ?>

                        <div>
                            <span class="responsive-title">
                              <?php echo _MEDIALIBRARY_SHOW_SEARCH_FOR_AVTORS; ?>:
                            </span>
                
                            <span class="title_author_list">
                              <?php
                              $author_link = 'index.php?option=com_medialibrary&amp;task=view_author&amp;id='.
                                              $row->people_id.'&amp;Itemid='.$Itemid;
                              ?>
                              <a href="<?php echo sefRelToAbs($author_link); ?>" >
                                <?php
                                if(strlen($row->people_name)>10){
                                  echo substr($row->people_name,0,10),'...';
                                }else{
                                  echo $row->people_name;
                                }?>
                              </a>
                           </span>
                        </div>
                        <?php } ?>

                  </div>


                  <div  class="table-cell">
                      <span class="responsive-title">
                      <?php echo _MEDIALIBRARY_LABEL_MEDIA_TYPE; ?>:
                      </span>
                      <?php echo $row->media_type; ?>
                  </div>


                    <?php
                  if ( $catid != 0 ) {
                    ?>
                  <div class="table-cell">
                      <span class="responsive-title">
                      <?php echo _MEDIALIBRARY_CATEGORIES_NAME; ?>:
                      </span>
                      <?php echo $row->category; ?>
                  </div>
                  <?php
                }
                  ?>


                <?php
                if ( $params->get( 'show_rating' ) ) {
                  //if($row->rating == 0 && $row->rating2 != 0)  $row->rating = $row->rating2;
                  ?>
                  <div class="table-cell">
                    <span class="responsive-title raiting-title">
                        <?php echo _MEDIALIBRARY_LABEL_RATING; ?>:
                    </span>
                    <img src="<?php echo JURI::root();?>/components/com_medialibrary/images/rating-<?php
                      echo $row->rating; ?>.gif" alt="<?php
                      echo ($row->rating)/2; ?>" align="left" border="0" />&nbsp;&nbsp;
                  </div>


                <?php
                }
                if ( $params->get( 'hits' ) ) {
                  ?>
                  <div class="table-cell">

                      <span class="responsive-title">
                      <?php echo _MEDIALIBRARY_LABEL_HITS; ?>:
                      </span>

                      <span>
                      <?php echo $row->hits; ?>
                      </span>
                  </div>
                  <?php
                }

                if ($params->get('show_lendstatus') && $params->get('show_lendrequest')) {
                    $data1 = JFactory::getDBO();
                    $query = "SELECT  b.lend_from , b.lend_until  FROM #__medialibrary_lend  AS b
                      LEFT JOIN #__medialibrary AS c ON b.fk_mediaid = c.id
                      WHERE  c.id=" . $row->id . " AND c.published='1' AND c.approved='1' AND
                      b.lend_return IS NULL";

                    $data1->setQuery($query);
                    $lends1 = $data1->loadObjectList();
                ?>
                <div class="table-cell">
                    <span class="responsive-title">
                    <?php echo _MEDIALIBRARY_LABEL_LEND_CB; ?>:
                    </span>

        <?php
                    if (count($lends1) == 0 ) {
        ?>
        <?php
                        echo "<img src='" . $mosConfig_live_site . 
                          "/components/com_medialibrary/images/available.png' alt='Available' " .
                          " name='image' border='0' align='middle' />";
                    } else {
                        echo _MEDIALIBRARY_LABEL_LEND_FROM_UNTIL . "<br />";
                        for ($a = 0; $a < count($lends1); $a++) {
                          $d1=date(str_replace("%", "", $medialibrary_configuration['date_format']),strtotime($lends1[$a]->lend_from));
                          $d2=date(str_replace("%", "", $medialibrary_configuration['date_format']),strtotime($lends1[$a]->lend_until));
                          $from_until = substr($d1, 0, 10) ."&nbsp;/&nbsp;" . substr($d2, 0, 10) . "\n";
                          print_r($from_until);
                        }
                    }

        ?>
                </div>
        <?php } ?>
              </div>
      <?php
      $k = 1 - $k;
    }
    ?>
       </div>
  </div>
<?php positions_ml($params->get('singleuser05'));?>
<?php positions_ml($params->get('singlecategory06')); ?>
         <?php
             } ?>

<?php if ($pageNav !=null && $pageNav->total > $pageNav->limit ) { ?>

          <div id="ml-paginator-custom" class="pagination-custom">
              <?php echo $pageNav->getPagesLinks( ); ?>
          </div>

 <?php } ?>

     
      <?php }
      if ( $is_exist_sub_categories ){
      ?>


<?php positions_ml($params->get('singlecategory07')); ?>
        <div class="componentheading<?php echo $params->get( 'pageclass_sfx' ); ?> subcategories">
             <?php echo _MEDIALIBRARY_LABEL_FETCHED_SUBCATEGORIES . " : "
              . $params->get( 'category_name' ); ?>
        </div>
<?php positions_ml($params->get('singlecategory08')); ?>
    <?php
      HTML_medialibrary::listCategories($params, $categories, $catid, $tabclass , $currentcat);
    }
positions_ml($params->get('singlecategory10'));
    if ($params->get( 'ownerslist_show')){
        HTML_medialibrary::showOwnersButton();
    }
positions_ml($params->get('singlecategory11'));
    ?>

        <?php if ($params->get('back_button')!= ''): ?>
            <div style="text-align: center;" class="back_button">
                <?php mosHTML::BackButton ( $params, $hide_js ); ?>
            </div>
        <?php endif ?>

</div>
