{% extends 'admin.twig' %}

{% block content %}

	<div vue-entry="CoreHome.ContentBlock" content-title="{{ title }}" rate="true">
		<p>
			This plugin helps you to Backup and export your database, and also to restore it.
		</p>
		<p>
			Make sure your DB user has Process tables privileges.
<pre>
GRANT PROCESS ON {{dbName}}.* TO '{{dbUser}}'@'YOUR_HOST_OR_%';
FLUSH PRIVILEGES;
</pre>
		</p>
		<p>
			<form action="{{ linkTo({'action': 'downloadDbDump'}) }}" method="post">
				<p>
					<span>
						<label>
							<input type="radio" name="download-preference" id="no-compression" value="none" {% if downloadPreference == "none" %} checked {% endif %}/>
							<span>No compression</span>
						</label>
					</span>
				</p>
				<p>
					<span>
						<label>
							<input type="radio" name="download-preference" id="zip-download" value="zip" {% if downloadPreference == "zip" %} checked {% endif %}/>
							<span>Download as .zip</span>
						</label>
					</span>
				</p>
				<p>
					<span>
						<label>
							<input type="radio" name="download-preference" id="tar-download" value="tar" {% if downloadPreference == "tar" %} checked {% endif %}/>
							<span>Download as .tar.gz</span>
						</label>
					</span>
				</p>
				<p>
					<button type="submit" class="btn btn-primary">Download DB Dump</button>
				</p>
			</form>
		</p>
	</div>

	<div vue-entry="CoreHome.ContentBlock" content-title="{{ import_title }}" rate="true">
		<form action="{{ linkTo({'action': 'handleDbUpload'}) }}" method="post" enctype="multipart/form-data">
			<p>
				This is a great feature if you need to import a database dump into your local machine.
																        If you are using this feature on your production server, make sure that you know
																        what you are doing and that you have a recent backup of your database.
			</p>
			<p>
				<b>Your current database will be deleted and replaced by the uploaded database dump.</b>
			</p>
			<p>
				<b>Your PHP Constraints:</b>
				<ul>
					<li>memory_limit:
						{{phpSettings.memory_limit}}</li>
					<li>upload_max_filesize:
						{{phpSettings.upload_max_filesize}}</li>
					<li>post_max_size:
						{{phpSettings.post_max_size}}</li>
				</ul>
			</p>
			<p>
				Select database dump(.sql) to upload:
				<input type="file" name="dbDump" id="dbDump">
			</p>
			<br>
			<input type="submit" class="btn btn-primary" value="Upload Database" name="submit">
		</form>
	</div>
	<div vue-entry="CoreHome.ContentBlock" content-title="{{ csv_title }}" rate="true">
		<p>
			I have collected a few queries from the Matomos faq:
			<a href="https://matomo.org/faq/how-to/how-do-i-write-sql-queries-to-select-visitors-list-of-pageviews-searches-events-in-the-matomo-database/" title="How do I write SQL queries to select visitors, list of pageviews, searches, events in the Matomo database?" target="_blank" rel="noreferrer">How do I write SQL queries...</a>.
		</p>
		<p>
			Download Raw logs into a CSV. For performance reasons, it is only possible to export per day.
		</p>
		<h2>Select all visits and actions</h2>
		<p>
			<b>Example:</b>
			If you want to export the logs for the 24th of February 2024, you would select the date 2024-02-24.<br>
			The Query that runs behind the scenes is:
<pre>
SELECT *
FROM matomo_log_visit 
LEFT JOIN matomo_log_link_visit_action ON matomo_log_visit.idvisit = matomo_log_link_visit_action.idvisit 
LEFT JOIN matomo_log_action ON matomo_log_action.idaction = matomo_log_link_visit_action.idaction_url 
LEFT JOIN matomo_log_conversion ON matomo_log_visit.idvisit = matomo_log_conversion.idvisit 
LEFT JOIN matomo_log_conversion_item ON matomo_log_visit.idvisit = matomo_log_conversion_item.idvisit
WHERE visit_last_action_time >= "2024-02-24 00:00:00"
AND visit_last_action_time <= "2024-02-24 23:59:59";
</pre>
		</p>
		<form action="{{ linkTo({ 'module': 'DataExport', 'action': 'selectAllVisitsAndActions', }) }}" method="post">
			<input type="hidden" name="idSite" value="0">
			<input type="hidden" name="getAll" value="1">
			<p>
				<label for="date">
					<b>
						Date:
					</b>
				</label>
			</p>
			<p>
				<input type="date" id="date" name="date" value="" style="max-width:200px;">
			</p>
			<p>
				<button type="submit" class="btn btn-primary">Select all visits and actions: Export to CSV</button>
			</p>
		</form>
	</div>

	<div vue-entry="DataExport.CsvSection">
	</div>
	
	<div vue-entry="CoreHome.ContentBlock" content-title="{{ clear_backups }}" rate="true">
		<form action="{{ linkTo({'action': 'deleteFiles'}) }}" method="post" enctype="multipart/form-data">
			<p>
				For your performance, you can delete the database dumps.
			</p>
			<p>
				Total file sizes:
				{{ totalFilesSize }}<br>
			</p>
			{% if files %}
				<p>
					<label>
						<input type="checkbox" id="de-sel-files" name="de-sel-files" value="all"/>
						<span>Select all files</span>
					</label>
					<ul class="de-all-files">
						{% for file in files %}
							<li>
								<label>
									<input type="checkbox" name="files[]" class="de-files" value="{{ file }}"><span>{{ file }}</span>
								</label>&nbsp;<a href="{{ linkTo({'action': 'downloadFile', 'file': file|url_encode }) }}" title="Download {{ file }}">Download</a>
							</li>
						{% endfor %}
					</ul>
				</p>
				<p>
					<input type="submit" class="btn btn-primary" value="Delete Files" name="submit">
				</p>
			{% else %}
				<p>
					Dir is empty.
				</p>
			{% endif %}
		</form>
	</div>

	<div vue-entry="CoreHome.ContentBlock" content-title="{{ settings }}" rate="true">
		Link to the settings page:
		<a href="index.php?module=CoreAdminHome&amp;action=generalSettings&amp;idSite=1&amp;period=day&amp;date=yesterday#/DataExport" title="Settings">Settings</a>
	</div>
{% endblock %}
