<?php
defined('_JEXEC') or die('Restricted access');
/**
* @package Joomla OrdaSoft CCK
* @copyright 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 AdminRent{


  static function rent_instance($option, $eiid){
    AdminRent::edit_rent($option, $eiid);
  }

  //for edit_rent, for add_rent, for rent_return
  static function edit_rent($option, $eiid){
    global $db, $user, $task, $os_cck_state;

    $db = new os_cckDb();
    $session = JFactory::getSession();;
    if (!is_array($eiid) || count($eiid) < 1) {
      echo "<script> alert('Select an item to rent'); window.history.go(-1);</script>\n";
      exit;
    }


    $eiids = $eiid[0]; //only for 1 item
    $select="SELECT a.*, cc.name AS category, l.id as rent_id, cl.params as lay_params,ce.name AS entity, l.rent_from as rent_from, " .
        "\n l.rent_return as rent_return, l.rent_until as rent_until, " .
        "\n l.user_name as user_name, l.user_email as user_email, " .
        "\n cc.rent_request AS for_rent" .
        "\n FROM #__os_cck_entity_instance AS a" .
        "\n LEFT JOIN #__os_cck_categories_connect AS CC_rel ON CC_rel.fk_eiid = a.eiid" .
        "\n LEFT JOIN #__os_cck_categories AS cc ON cc.cid = CC_rel.fk_cid" .
        "\n LEFT JOIN #__os_cck_layout AS cl ON cl.lid = a.fk_lid ".
        "\n LEFT JOIN #__os_cck_entity AS ce ON ce.eid = a.fk_eid ".
        "\n LEFT JOIN #__os_cck_rent AS l ON l.fk_eiid = a.eiid" .
        "\n WHERE a.eiid = " . $eiids . 
        "\n GROUP BY rent_id ORDER BY l.id DESC";
    $db->setQuery($select);
    if (!$db->execute()) {
      echo "<script> alert('" . addslashes($db->getErrorMsg()) . "'); window.history.go(-1); </script>\n";
      exit ();
    }
    $rows = $db->loadObjectList();
// echo ":111111111111111111111:";
// print_r(count($rows) ); exit;

    $show_fields = $entityEaaray = $history_items = $items_for_return_or_edit = $item_for_rent = array();
    if(count($rows) > 0){
      foreach ($rows as $row) {
        if($row->rent_return || ($task == "add_rent" && $row->rent_from ) ){
          //added here all returned from rent and all what now under rent
          $history_items[] = $row;
        }else if($row->rent_until){
          $items_for_return_or_edit[] = $row;
        }
        if( $task == "add_rent" && count($item_for_rent) == 0 ) {
          $item_for_rent[] = $row;
        }

        $lay_params = unserialize($row->lay_params);
        $entityEaaray[] = $row->fk_eid;
        $layoutArray[] = $row->fk_lid;
      }
    
      if( ( $task == "edit_rent" || $task == "rent_return" ) && count($items_for_return_or_edit) == 0 ){
        echo "<script> alert('This Instance now not under rent!'); window.history.go(-1); </script>\n";
        exit ();
      }


      $entityEaaray = array_unique($entityEaaray) ;
      foreach( $entityEaaray as $key => $value){
        $entity = new os_cckEntity($db->_db);
        $entity->load($value);
        $layout = new os_cckLayout($db->_db);
        $layout->load($layoutArray[$key]);
        $bootstrap_version = $session->get( 'bootstrap','2');
        $layout_html = urldecode($layout->getLayoutHtml($bootstrap_version));
        $layout_params = unserialize($layout->params);
        $extra_fields_list = $entity->getFieldList();

        
        foreach($extra_fields_list as $Fieldvalue){
                  
          if($Fieldvalue->show_in_instance_menu && strpos($layout_html,"{|f-".$Fieldvalue->fid."|}")){
            $fieldNames[$key]['ent_name'] = $entity->eid;
            $fieldNames[$key]['field_type'] = $Fieldvalue->field_type;
            $fieldNames[$key]['fields'][] = $Fieldvalue->db_field_name;//need for use in search // [][table_name][column_mname]
            $show_fields[$value][]= $Fieldvalue;
          }
        }
      }
      ksort($show_fields);
    }

    if(!isset($os_cck_state->data) ){
        echo '<script>alert("'. cck_lang_text("COM_OS_CCK_YOUR_LICENSE_NEED_ACTIVE").'"); history.back(); </script>';
        return ;
    }
    
    // get list of users
    $db->setQuery("SELECT id AS value, name AS text, email  from #__users ORDER BY name");
    $user_array = $db->loadObjectList();

    $userlist[] = JHTML::_('select.option','-1', 'Select User');
    $db->setQuery("SELECT id AS value, name AS text from #__users ORDER BY name");
    $userlist = array_merge($userlist, $db->loadObjectList());
    $userlist = JHTML::_('select.genericlist',$userlist, 'userid',
        'class="inputbox" size="1" onchange="fill_user(this.value)"', 'value', 'text', '-1');


    AdminViewRent::showRentItems($option, $item_for_rent,$items_for_return_or_edit,$history_items, $userlist,$show_fields,$user_array);
  }


  static function rent_return($option, $eiid){
      
      AdminRent::edit_rent($option, $eiid);
      // rentReturnCCK($option, $eiid);

  }

  static function rent_return_confirm($option, $eiid){
      rentReturnCCK($option, $eiid);
  }


  static function saveRent($option, $eiid,$task = ""){
      saveRentCCK($option, $eiid,$task);
  }

  static function add_rent_confirm($option, $eiid,$task = ""){
    saveRentCCK($option, $eiid,$task);
  }

}
