<?php

/**
 * @package     Articles Good Search
 *
 * @copyright   Copyright (C) 2017 Joomcar extensions. All rights reserved.
 * @license     GNU General Public License version 2 or later.
 */
 
defined('_JEXEC') or die ;

class JFormFieldSigImport extends JFormField {

	function getInput() {		
		return JFormFieldSigImport::fetchElement($this->name, $this->value, $this->element, $this->options['control']);
	}

    public function fetchElement($name, $value, &$node, $control_name) {		
		$this->MultiimagesImportTableCreate();
		$last_id = JFactory::getDBO()->setQuery("SELECT item_id FROM #__k2_multiimages_import ORDER BY id DESC LIMIT 0,1")->loadResult();
		if($last_id) {
			$totals = JFactory::getDBO()->setQuery("SELECT id FROM #__k2_items WHERE gallery != ''")->loadColumn();
			if($totals) {
				foreach($totals as $k=>$item_id) {
					$current = count($totals) - ($k + 1);
					if($item_id == $last_id) {
						break;
					}
				}	
				$already_imported = "{$current}th item(s) of " . count($totals) . " already imported (<a id='importreset' href='#'>Reset</a>)";
			}
		}
		return '
			<span style="font-size: 17px;">
				Import images from Simple Image Gallery: <br />
				<a id="sigimport" target="_blank" href="#">Run import</a>
				<span id="sigimport-ajax-result" style="display: block; text-align: center;">'.$already_imported.'</span>
			</span>
			
			<script>
				(function($) {
					$("#sigimport").on("click", function(e) {
						e.preventDefault();
						callAjaxImport();
					});
					$("#importreset").on("click", function(e) {
						e.preventDefault();
						jQuery.ajax({
							data: "multiimages=1&a=reset",
							type: "get",
							url: "",
							async: true,
							success: function(response) {
								jQuery("#sigimport-ajax-result").html(response);
								setTimeout(function() {
									window.location.reload();
								}, 500);
							},
							error: function(response) {
								jQuery("#sigimport-ajax-result").html(response);
							},
						});
					});
				})(jQuery)
				
				function callAjaxImport(last_id = 0) {
					var data = "multiimages=1&a=import&last_id='.$last_id.'";
					if(last_id != 0) {
						var data = "multiimages=1&a=import&last_id=" + last_id;	
					}
					jQuery("#sigimport-ajax-result").html("<img src=\"'.JURI::root().'media/k2/assets/images/backend/loader.gif\" />");
					jQuery.ajax({
						data: data,
						type: "get",
						url: "",
						async: true,
						success: function(response) {
							jQuery("#sigimport-ajax-result").html(response);
							var next_item_id = jQuery(response).filter(".next-item-id").text();
							setTimeout(function() {
								if(parseInt(next_item_id)) {
										callAjaxImport(parseInt(next_item_id));
								}
								else {
									jQuery("#sigimport-ajax-result").html("Done!");
									window.location.reload();
								}
							}, 4000);
						},
						error: function(response) {
							jQuery("#sigimport-ajax-result").html(response);
						},
					});
				}
			</script>
		';
    }
	
	function MultiimagesImportTableCreate() {	
		$query = "CREATE TABLE IF NOT EXISTS `#__k2_multiimages_import` (";
			$query .= "`id` int(21) NOT NULL AUTO_INCREMENT PRIMARY KEY,";
			$query .= "`item_id` int(11) NOT NULL";
		$query .= ") ENGINE=MyISAM  DEFAULT CHARSET=utf8;";
		JFactory::getDBO()->setQuery($query)->query();
	}
}

?>
