<?php

/******************************************************************************
 *  
 *  PROJECT: Flynax Classifieds Software
 *  VERSION: 4.5.2
 *  LICENSE: FL4HLD2A8696 - http://www.flynax.com/license-agreement.html
 *  PRODUCT: Real Estate Classifieds
 *  DOMAIN: gevimmo.com
 *  FILE: LOCATIONFINDER.INC.PHP
 *  
 *  The software is a commercial product delivered under single, non-exclusive,
 *  non-transferable license for one domain or IP address. Therefore distribution,
 *  sale or transfer of the file in whole or in part without permission of Flynax
 *  respective owners is considered to be illegal and breach of Flynax License End
 *  User Agreement.
 *  
 *  You are not allowed to remove this information from the file without permission
 *  of Flynax respective owners.
 *  
 *  Flynax Classifieds Software 2017 | All copyrights reserved.
 *  
 *  http://www.flynax.com/
 ******************************************************************************/

$rlHook->load('apPhpListingsTop');

if ($_GET['action'] == 'update') {
    $bcAStep = $lang['locationFinder_import_update_database'];

    // fix location finder database version
    if (!$config['locationFinder_update_database']) {
        $rows = $rlDb->getRow("SELECT COUNT(*) AS `Count` FROM `" . RL_DBPREFIX . "geo_mapping`");
        if (round($rows['Count'], -3) == 121000) {
            $rlConfig->setConfig('locationFinder_update_database', '1.0');
            $config['locationFinder_update_database'] = '1.0';
        }
    }

    // fix multifields database version
    if (isset($_GET['fix']) && $config['mf_db_version'] != 'locations5') {
        $rlConfig->setConfig('mf_db_version', 'locations5');
        $config['mf_db_version'] = 'locations5';

        $aUrl = array('controller' => $controller, 'action' => 'update');
        $reefless->redirect($aUrl);
    }

    $rlSmarty->assign('db_update', true);
    $update_error = false;

    if ($config['mf_db_version'] != 'locations5') {
        $update_error = true;
    }
    $rlSmarty->assign_by_ref('update_error', $update_error);
} else if ($_GET['action'] == 'create') {
    $sql = "CREATE TABLE IF NOT EXISTS `" . RL_DBPREFIX . "geo_mapping` (
      `ID` int(11) NOT NULL AUTO_INCREMENT,
      `Format_key` varchar(255) NOT NULL,
      `Google_key` varchar(255) DEFAULT NULL,
      `Neighborhood` enum('0','1') NOT NULL DEFAULT '0',
      `Target` enum('region','city') NOT NULL DEFAULT 'city',
      `Lat` double NOT NULL,
      `Lng` double NOT NULL,
      `Verified` enum('0','1') NOT NULL DEFAULT '1',
      PRIMARY KEY (`ID`),
      KEY `Format_key` (`Format_key`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;";

    if ($rlDb->query($sql)) {
        $reefless->loadClass('Notice');
        $rlNotice->saveNotice($lang['locationFinder_database_structure_created']);

        $aUrl = array('controller' => $controller);
        $reefless->redirect($aUrl);
    } else {
        $rlDebug->logger('Location Finder: Unable to create "geo_mapping" database.');
        $sError = true;
    }
} else {
    $is_mapping_available = true;

    // define mapping settings
    if (!$rlHook->aHooks['multiField']) {
        $is_mapping_available = false;
        if ($rlDb->getOne('ID', "`Key` = 'multiField'", 'plugins')) {
            $mapping_error = $lang['locationFinder_mapping_mf_inactive_error'];
        } else {
            $mapping_error = $lang['locationFinder_mapping_mf_not_installed_error'];
        }

        $href = $rlSmarty->_tpl_vars['rlBase'] . 'index.php?controller=plugins';
        $mapping_error = preg_replace('/(\[(.*)?\])/', '<a href="' . $href . '">$2</a>', $mapping_error);
    } elseif (!$rlDb->getRow("SHOW TABLES LIKE '" . RL_DBPREFIX . "geo_mapping'")) {
        $is_mapping_available = false;
        $href = $rlSmarty->_tpl_vars['rlBase'] . 'index.php?controller=' . $_GET['controller'] . '&action=create';
        $mapping_error = preg_replace('/(\[(.*)?\])/', '<a href="' . $href . '">$2</a>', $lang['locationFinder_mapping_table_error']);
    }

    $rlSmarty->assign_by_ref('is_mapping_available', $is_mapping_available);

    // available mapping case
    if ($is_mapping_available) {
        $rlSmarty->assign('cpTitle', $lang['locationFinder_mapping_manager']);

        // get related fields data
        foreach (array('locationFinder_mapping_country', 'locationFinder_mapping_state', 'locationFinder_mapping_city') as $field) {
            if ($config[$field] && $data = $rlDb->fetch(array('Key', 'Type', 'Condition'), array('Key' => $config[$field]), null, 1, 'listing_fields', 'row')) {
                $data['pName'] = 'listing_fields+name+' . $config[$field];
                $fields[] = $data;
            }
        }

        if ($fields) {
            $fields = $rlCommon->fieldValuesAdaptation($fields, 'listing_fields', 'listings'); // TODO
            $rlSmarty->assign_by_ref('fields', $fields);
        } else {
            $group_id = $rlDb->getOne('ID', "`Key` = 'locationFinder'", 'config_groups');
            $href = $rlSmarty->_tpl_vars['rlBase'] . 'index.php?controller=settings&group=' . $group_id;
            $rlSmarty->assign_by_ref('href', $href);
        }
    }
    // no mapping case
    else {
        $rlSmarty->assign_by_ref('mapping_error', $mapping_error);
    }
}