<?php

/**
 
 * @package     pkg_dearflip
 * @subpackage  com_dearflip
 * @since       1.0.0
 * @copyright   Copyright © 2007 Free Software Foundation, Inc. All rights reserved.
 * @license   GNU General Public License version 3 or later; see https://www.gnu.org/licenses/gpl-3.0.en.html
 
 */

// No direct access to this file
defined( '_JEXEC' ) or die( 'Restricted access' );

/**
 * Dearflip Model
 *
 * @since  0.0.1
 */
class DearflipModelDearflip extends JModelAdmin {
  /**
   * Method to get a table object, load it if necessary.
   *
   * @param string $type The table name. Optional.
   * @param string $prefix The class prefix. Optional.
   * @param array $config Configuration array for model. Optional.
   *
   * @return  JTable  A JTable object
   *
   * @since   1.6
   */
  public function getTable( $type = 'Dearflip', $prefix = 'DearflipTable', $config = array() ) {
    return JTable::getInstance( $type, $prefix, $config );
  }
  
  /**
   * Method to get the record form.
   *
   * @param array $data Data for the form.
   * @param boolean $loadData True if the form is to load its own data (default case), false if not.
   *
   * @return  mixed    A JForm object on success, false on failure
   *
   * @since   1.6
   */
  public function getForm( $data = array(), $loadData = true ) {
      //compare the joomla version and load fields accordingly 

      if (version_compare(JVERSION, '4.0', '>=')) 
      {
        // Load fields for Joomla 4.x
          $form = $this->loadForm(
          'com_dearflip.dearflip',
          'dearflip',
          array(
            'control'   => 'jform',
            'load_data' => $loadData
          )

          );        
        
      } 
      else {
        // Load fields for Joomla 3.x
          $form = $this->loadForm(
          'com_dearflip.dearflip',
          'dearflip_j3',
          array(
              'control'   => 'jform',
              'load_data' => $loadData
            )

          );        
        }
    
    // Get the form.
   /* $form = $this->loadForm(
        'com_dearflip.dearflip',
        'dearflip',
        array(
            'control'   => 'jform',
            'load_data' => $loadData
        )
    
    );
    */
 
    
    
    if ( empty( $form ) ) {
      return false;
    }
    
    return $form;
  }
  
  /**
   * Method to get the data that should be injected in the form.
   *
   * @return  mixed  The data for the form.
   *
   * @since   1.6
   */
  protected function loadFormData() {
    // Check the session for previously entered form data.
    $data = JFactory::getApplication()->getUserState(
        'com_dearflip.edit.dearflip.data',
        array()
    );
    
    if ( empty( $data ) ) {
      $data = $this->getItem();
    }
    
    return $data;
  }


    /**
     * Prepare a dearflip record for saving in the database
     */
    protected function prepareTable($table)
    {
        // Set ordering to the last item if not set
        if (empty($table->ordering))
        {
            $db = $this->getDbo();
            $query = $db->getQuery(true)
                ->select('MAX(ordering)')
                ->from('#__dearflip');

            $db->setQuery($query);
            $max = $db->loadResult();

            $table->ordering = $max + 1;
        }
    }
}