<?php

/**
 * @package		SP Transfer
 * @subpackage	Components
 * @copyright	SP CYEND - All rights reserved.
 * @author		SP CYEND
 * @link		http://www.cyend.com
 * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/

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

include_once JPATH_COMPONENT . '/models/com.php';

class SPTransferModelCom_Menus extends SPTransferModelCom
{

    public function categories($ids = null)    {
        $this->destination_table = $this->factory->getTable('Category', 'JTable');        
        $this->task->state = 2; //state for success
        
        parent::categories($ids);
    }
    
    public function menu_types($ids = null)    {        
        $this->destination_table = $this->factory->getTable('MenuType', 'JTable');
        $this->alias = 'menutype';
        $this->task->query = 'SELECT * 
            FROM #__' . $this->task->name . '
            WHERE ' . $this->id . ' > 0';
        $this->task->state = 4; //state for success
        
        $this->items($ids);   
    }
    
    public function menu($ids = null)    {        
        $this->destination_table = $this->factory->getTable('Menu', 'JTable');
        if(!($this->task->query = $this->menu_query())) return;
        
        $this->items($ids);   
    }
    
    public function categories_fix($ids = null)    {       
        $this->destination_table = $this->factory->getTable('Category', 'JTable');        
        $this->task->state = 4; //state for success
        
        parent::categories_fix($ids);
    }
    public function menu_types_fix($ids = null)    {
        $task = $this->task;
        $this->factory->writeLog('<h2>' . JText::_($task->extension_name) . ' - ' . JText::_($task->extension_name . '_' . $task->name) . ' - ' . JText::_('COM_SPTRANSFER_FIX') . '</h2>');
        $this->factory->writeLog('<p>' . JText::_(COM_SPTRANSFER_NOTHING_TO_TRANSFER) . '</p>');
        return;
    }
    
    public function menu_fix($ids = null)    {
        $this->destination_table = $this->factory->getTable('Menu', 'JTable');
        $this->task->query = 'SELECT * 
            FROM #__' . $this->task->name . '
            WHERE parent_id > 0';
        
        $this->items_fix($ids);
    }
    
    private function menu_query() {
        //Filter per menu_types already transferred
        $query = $this->destination_db->getQuery(true);
        $query->select('a.menutype');
        $query->from('#__menu_types AS a');
        $query->join('LEFT', '`#__sptransfer_log` AS b ON b.destination_id = a.id');
        $query->where('b.tables_id = 15 AND b.state >= 2');
        $query->order('b.id ASC');
        $this->destination_db->setQuery($query);
        $result = $this->destination_db->query();
        if (!$result) {
            $this->jAp->enqueueMessage(JText::sprintf('COM_SPTRANSFER_MSG_ERROR_QUERY', $this->destination_db->getErrorMsg()), 'error');
            $message = '<p><b><font color="red">' . JText::sprintf('COM_SPTRANSFER_MSG_ERROR_QUERY', $this->destination_db->getErrorMsg()) . '</font></b></p>';
            $this->factory->writeLog($message);
            return false;
        }
        $temp2 = $this->destination_db->loadResultArray();

        if (is_null($temp2[0])) {
            $message = '<p>' . JText::_('COM_SPTRANSFER_MENUTYPE_UNAVAILABLE') . '</p>';
            $this->factory->writeLog($message);
            return false;
        }

        foreach ($temp2 as $i => $temp3) {
            if (strpos($temp3, '-sp-')) {
                $temp4 = explode('-sp-', $temp3);
                $temp3 = $temp4[0];
            }
            $temp2[$i] = '"' . $temp3 . '"';
        }

        $query = 'SELECT * 
            FROM #__menu
            WHERE id > 1';
        $query .= ' AND menutype IN (' . implode(',', $temp2) . ')';

        return $query;
    }
    
}
