<?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
 *
 */

$part_link = '';
if ($task == 'search' || $task == 'simplesearch'){
    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;
}if ($task == 'ownermedias'){
    $other_uid = protectInjectionWithoutQuote('other_uid', '');
    $part_link .= 'task=ownermedias&other_uid='.$other_uid;
}

global $other_uid;

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

// $category->catid = $_REQUEST['catid'];
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');
?>

<noscript>
    Javascript is required to use MediaLibrary 
    <a href="https://ordasoft.com/media-library-joomla-extension">
        Media Library Software for create virtual library.
    </a>,
    <a href="https://ordasoft.com/media-library-joomla-extension">
        MediaLibrary this is software for create media store website for rent, sell audio, video, game and other digital product 
    </a>
</noscript>


<div id="medialibrary">
<?php positions_ml($params->get('singleuser01'));?>
<?php positions_ml($params->get('singlecategory01')); ?>
    <div class="componentheading<?php echo $params->get( 'pageclass_sfx' ); ?>">
      <?php echo $currentcat->header; ?>
    </div>
<?php positions_ml($params->get('singleuser02'));?>
<?php positions_ml($params->get('singlecategory02'));?>


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



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



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


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

        <div class="button-output-information ml-collumn-lg-12 ml-collumn-md-12 ml-collumn-sm-12 ml-collumn-xs-12">
                <?php
                if(isset($currentcat->img) && $currentcat->img != null 
                  && $currentcat->align == 'left' && $params->get( 'show_cat_pic') ){
                ?>
                <div>
                  <img src="<?php echo $currentcat->img; ?>" align="<?php echo $currentcat->align; ?>" alt="?"/>
                </div>
            <?php
                }
                ?>
                <div>
                  <?php echo $currentcat->descrip; ?>
                </div>
            <?php
                  if(isset($currentcat->img) && 
                     $currentcat->img != null && $currentcat->align == 'right'){
                ?>
                <div>
                  <img src="<?php echo $currentcat->img; ?>" align="<?php echo $currentcat->align; ?>" alt="?" />
                </div>
            <?php
              }
              
            ?>
            
              
        </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 ml-collumn-lg-12 ml-collumn-md-12 ml-collumn-sm-12 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')); ?>


        <?php } ?>


    </div>

      


<?php //positions_ml($params->get('singleuser05'));?>
<?php //positions_ml($params->get('singlecategory06')); ?>
         <?php
             //}else{
               ?>

                 <div id="gallery" class="galery">
                     <?php $total = count($rows);
                     foreach ($rows as $row) {
                         $link = /*sefRelToAbs(*/'index.php?option=' . $option
                          . '&amp;task=view&amp;tab=getmymediasTab&amp;is_show_data=1&amp;id='
                          . $row->id . '&amp;catid=' . $catid . '&amp;Itemid=' . $Itemid/*)*/;
                         $imageURL = $row->imageURL;
                         ?>
                         <div class="galery__item">
                            <div>
                             <a href="<?php echo sefRelToAbs($link); ?>" >
                             <?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"  />';
                             }
                             ?>
                             </a>
                            </div>
                             <div class="textmedia">
                             <div class="titlemedia">
                                <a href="<?php echo sefRelToAbs($link); ?>" >
                                  <?php if(strlen($row->title)>10) echo substr($row->title,0,10),'...';
                                  else {
                                      echo $row->title;
                                  }?>
                                </a>
                             </div>

                             <?php
                             if (isset($row->price) && isset($row->priceunit)) {
                             ?>
                             <div class="price"><?php     if ($medialibrary_configuration['price_unit_show'] == '1'){
                                    echo  formatMoney($row->price, true, $medialibrary_configuration[
                                    'price_format']) . ' ' .  $row->priceunit;
                                }
                                else {
                                    echo  $row->priceunit . ' ' . formatMoney($row->price, true,
                                    $medialibrary_configuration['price_format']);
                                } 
                            ?></div> <?php
                              }?>
                             </div>

                         </div>
                     <?php }?>
                 </div>
             <?php ?>

      <?php
     //if($total > 0) {
      if ($params->get('show_lendstatus') && $params->get('show_lendrequest') &&
          $params->get('lend_save')) {
        if($_SESSION) $user = JFactory::getUser();// && $available)
        ?>

            <div class="lend-information<?php echo $params->get('pageclass_sfx'); ?>">
              <?php echo _MEDIALIBRARY_LABEL_LEND_INFORMATIONS; ?>
              <input type="hidden"  name="mediaid" id="mediaid"  value="<?php echo $row->id
                ?>"  maxlength="80" />
            </div>

            <form id="adminForm" class="lend-request-form"  action="<?php echo sefRelToAbs("index.php");
              ?>" name="userForm" method="post">

            <div class="ml-row">

              <div class="ml-collumn-lg-6 ml-collumn-md-6 ml-collumn-sm-6 ml-collumn-xs-12" >
                  <p>
                      <?php echo _MEDIALIBRARY_LABEL_LEND_REQUEST_NAME; ?>:
                  </p>

                  <div class="input-group">
                      <span class="input-group-addon"><i class="fa fa-user"></i></span>
                      <input class="inputbox form-control" type="text" name="user_name" size="38" placeholder="John Smith" maxlength="80" required
                      value='<?php if($user->username != "" || $user->username!= NULL) {echo $user->username;}
                      else{}?>'/>
                      <span class="input-group-addon">
                          <i class="fa fa-edit"></i>
                          <i class="fa fa-check"></i>
                          <i class="fa fa-warning"></i>
                      </span>
                  </div>


              </div>

              <div class="ml-collumn-lg-6 ml-collumn-md-6 ml-collumn-sm-6 ml-collumn-xs-12">
                  <p>
                  <?php echo _MEDIALIBRARY_LABEL_LEND_REQUEST_EMAIL; ?>:
                  </p>

                  <div class="input-group">
                      <span class="input-group-addon"><i class="fa fa-envelope-o"></i></span>
                      <input class="inputbox form-control" type="email" name="user_email" placeholder="example@example.com" size="30" maxlength="80" required
                      value='<?php if($user->email != "" || $user->email!= NULL) {echo $user->email;}
                      else{}?>'/>
                      <span class="input-group-addon">
                        <i class="fa fa-edit"></i>
                        <i class="fa fa-check"></i>
                        <i class="fa fa-warning"></i>
                      </span>
                  </div>

              </div>

            </div>


            <div class="ml-row">


                <div class="ml-collumn-lg-6 ml-collumn-md-6 ml-collumn-sm-6 ml-collumn-xs-12">
                    <p>
                      <?php echo _MEDIALIBRARY_LABEL_LEND_REQUEST_MAILING; ?>:
                    </p>


                    <textarea name="user_mailing"  id="user_mailing" cols="50" rows="8" placeholder="Ukraine, Kharkiv, Sumskay St. 22" ></textarea>
                </div>
              
                        <?php
                            $media = $row->id;
                            $date_NA = available_dates($media);    
                        ?>

                    <script>
                      Date.prototype.toLocaleFormat = function(format) {
                    var f = {Y : this.getYear() + 1900,m : this.getMonth() + 1,d : this.getDate(),
                        H : this.getHours(),M : this.getMinutes(),S : this.getSeconds()}
                          for(k in f)
                          format = format.replace('%' + k, f[k] < 10 ? "0" + f[k] : f[k]);
                          return format;
                      };

                       var unavailableDates = Array();

                      window.onload = function()
                      {
      
                          var k=0;
                        <?php if(!empty($date_NA)){?>
                            <?php foreach ($date_NA as $N_A){ ?>
                                 unavailableDates[k]= '<?php echo $N_A; ?>';
                                k++;
                            <?php } ?>
                        <?php } ?>

                        function unavailable(date) {
                            dmy = date.getFullYear() + "-" + ('0'+(date.getMonth() + 1)).slice(-2) + 
                              "-" + ('0'+date.getDate()).slice(-2);
                            if (jQuerMedia.inArray(dmy, unavailableDates) == -1) {
                                return [true, ""];
                            } else {
                                return [false, "", "Unavailable"];
                            }
                        }

                        jQuerMedia( "#lend_from, #lend_until" ).datepicker(
                        {
                          minDate: "+0",
                          dateFormat: "<?php echo transforDateFromPhpToJqueryML();?>",
                          beforeShowDay: unavailable,
                        });


                      };


                  </script>

                <div class="ml-collumn-lg-6 ml-collumn-md-6 ml-collumn-sm-6 ml-collumn-xs-12">

                    <p>
                      <?php echo _MEDIALIBRARY_LABEL_LEND_REQUEST_FROM; ?>:<br />
                      <p>
                        <input type="text" id="lend_from" name="lend_from">
                      </p>
                    </p>


                    <p>
                      <?php echo _MEDIALIBRARY_LABEL_LEND_REQUEST_UNTIL; ?>:<br />
                      <p>
                        <input type="text" id="lend_until" name="lend_until">
                      </p>
                    </p>

                </div>

            </div>

              <?php } ?>
                    

                    <?php
                    if ($params->get('show_lendstatus') && $params->get('show_lendrequest') &&
                        $params->get('lend_save'))
                    {?>

                  <div class="ml-row">

                      <div class="ml-collumn-lg-12 ml-collumn-md-12 ml-collumn-sm-12 ml-collumn-xs-12 
                      lend-button-submit" >
                      <input type="hidden" name="option" value="<?php echo $option ?>"/>
                      <input type="hidden" name="task" value="save_lend_request"/>
                      <input type="hidden" name="Itemid" value="<?php echo $Itemid; ?>"/>
                      <input type="button" class="button"
                        value="<?php echo _MEDIALIBRARY_LABEL_BUTTON_LEND_REQU_SAVE; ?>"
                        onclick="ml_lend_request_submitbutton(
                          '<?php echo _MEDIALIBRARY_INFOTEXT_JS_LEND_REQ_NAME; ?>',
                          '<?php echo _MEDIALIBRARY_INFOTEXT_JS_LEND_REQ_EMAIL; ?>',
                          '<?php echo _MEDIALIBRARY_INFOTEXT_JS_LEND_REQ_MAILING; ?>',
                          '<?php echo _MEDIALIBRARY_INFOTEXT_JS_LEND_REQ_UNTIL; ?>')" />
                       <?php
                          if ($option != 'com_medialibrary')
                          {
                              ?>
                                  <input type="hidden" name="tab" value="getmymediastab"/>
                                  <input type="hidden" name="is_show_data" value="1"/>
                              <?php
                          }
                          ?>


                      <input type="hidden" name="mediaid" value="<?php echo $rows[0]->id;?>"/>
                      <input type="hidden" name="mid[]" value="<?php echo $rows[0]->id;?>"/>

                      </div>

                      </div>

                        <?php
                    } else {
?>
                       
                        <?php
                    }
?>
                  <!-- </div> -->

  </form>

<?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' ); ?>">
             <?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>


<style type="text/css">
.galery__item{
/*    width: <?php echo $medialibrary_configuration['foto']['width']+10;?>px;
    height: <?php echo $medialibrary_configuration['foto']['high']+65;?>px;*/
    float:left;
    padding:10px;
    position:relative;
    border:1px solid white;
    margin:5px;
    text-align: center;
}
/* .okno:hover{
    border:1px solid black;
} */
.galery__item img{

    width:<?php echo $medialibrary_configuration['foto']['width'];?>px;
    height: <?php echo $medialibrary_configuration['foto']['high'];?>px;
}
.galery__item .textmedia{
/*    width:<?php echo $medialibrary_configuration['foto']['width'];?>px;
    position:absolute;
    bottom:5px;*/
    max-width: 100%;
}
.galery__item .titlemedia{
    max-height: 50px;
    margin: 10px 0 0;
}
</style>



