Browse Source

NEW: template admin

Giancarlo Voci 7 years ago
parent
commit
32fbbfa161

+ 1 - 1
src/OpenMind/Admin/Admin.php

@@ -16,7 +16,7 @@ class Admin extends PluginStrategy
 	{
 		$this->AddRoute('GET @admin: /admin', __NAMESPACE__ . '\Pages->Admin');
 		$this->AddRoute('GET @admin: /admin/eslstatus', __NAMESPACE__ . '\Pages->ESLStatus');
-		$this->AddRoute('GET @admin: /admin/templates', __NAMESPACE__ . '\Pages->Templates');
+		$this->AddRoute('GET @admin: /admin/templates/@dt/@clearcache', __NAMESPACE__ . '\Pages->Templates');
 		$this->AddRoute('GET @admin: /admin/apstatus', __NAMESPACE__ . '\Pages->APStatus');
 		$this->AddRoute('GET @admin: /admin/omlsettings', __NAMESPACE__ . '\Pages->OML_Settings');
 		// Admin Load/Save

+ 15 - 9
src/OpenMind/Admin/Pages.php

@@ -160,26 +160,32 @@ class Pages extends Controller
 		return;
 	}
 
-	public function Templates($f3)
+	public function Templates($f3, $args)
 	{
-		$tplNameList = [];
+		global $frameworkUtils;
 
 		$fb_template = $this->findBoxCore->CallAPI('/1.0.0/index.php/smartesl.gettemplates/1.0.0/', '[]');
 		$templates = $this->findBoxCore->getLabelsFromResponse($fb_template);
 
+		if ($args['clearcache'] == 1)
+			$frameworkUtils->rrmdir("fbtpl");
+
 		$tplFileNames = [];
 		if ($templates != null) {
 			foreach($templates as $item) {
-				$tplFileNames[] = "{$item['name']}.svg";
-				if (!file_exists("fbtpl/{$item['name']}.svg"))
-					$this->findBoxCore->GetTemplateImage($item['name']);
+
+				$DTs = explode(',', $item['labeltypes']);
+				if (($args['dt'] != 0) && !in_array($args['dt'], $DTs))
+					continue;
+
+				if (!file_exists("fbtpl/{$item['template_id']}.svg"))
+					$this->findBoxCore->GetTemplateImage($item['template_id']);
+
+				$tplFileNames[$item['template_id']] = file_get_contents("fbtpl/{$item['template_id']}.svg");
 			}
 		}
 
-
 		$f3->set('TPLPATH', $tplFileNames);
-		$f3->set('PAGENAME', 'ESL Templates');
-		$f3->set('CONTENT', 'templates.tpl');
-		echo Template::instance()->render('masterpage.tpl');
+		echo Template::instance()->render('templates.tpl');
 	}
 }

+ 13 - 0
src/OpenMind/FindBox/FindBoxCoreSystem.php

@@ -310,9 +310,22 @@ class FindBoxCoreSystem
 				$zip->close();
 				rename('fbtpl/source.svg', $dest);
 				unlink("fbtpl/{$templateID}.zip");
+				$this->fixFindBoxSVG($dest);
 				return $dest;
 			}
 		}
 		return NULL;
 	}
+
+	private function fixFindBoxSVG($svgPath)
+	{
+		$svg_data = file_get_contents($svgPath);
+
+		$svgTemplate = simplexml_load_string($svg_data);
+		$svgTemplate->attributes()->width = 300;
+		$svgTemplate->attributes()->height = 180;
+		$svgTemplate->attributes()->transform = '';
+
+		$svgTemplate->saveXML($svgPath);
+	}
 }

+ 1 - 0
src/OpenMind/System/ErrorHandlers.php

@@ -40,6 +40,7 @@ class ErrorHandlers
 		}
 
 		if (!$this->f3->get('AJAX')) {
+			$this->f3->set('UI', 'ui/frontend/');
 			echo Template::instance()->render('errors.tpl');
 			return;
 		}

+ 20 - 0
src/OpenMind/System/OMUtils.php

@@ -160,4 +160,24 @@ class OMUtils
 		}
 		return $errno;
 	}
+
+	/**
+	 * Recurse delete folder
+	 *
+	 * @param string $dir /path/for/the/directory/
+	 */
+	function rrmdir($dir)
+	{
+		if (is_dir($dir)) {
+			$files = glob($dir . '*', GLOB_MARK); //GLOB_MARK adds a slash to directories returned
+
+			foreach($files as $file) {
+				$this->rrmdir($file);
+			}
+
+			@rmdir($dir);
+		} elseif (is_file($dir)) {
+			@unlink($dir);
+		}
+	}
 }

+ 8 - 0
ui/admin/assets/js/OMGUtils.js

@@ -31,3 +31,11 @@ export function AjaxReq(url, options) {
 				return Promise.reject(resp);
 		});
 }
+
+Array.prototype.countOccurrences = function (column, val) {
+	return this.reduce((a, v) => (v[column] == val ? a + 1 : a), 0);
+}
+
+Array.prototype.uniq = function () {
+	return [...new Set(this)]
+};

+ 27 - 0
ui/admin/assets/js/admin.js

@@ -49,6 +49,32 @@ function ESLMultiOperations(method) {
 	});
 }
 
+function TemplateSelector() {
+	$('.templateselector').on('shown.bs.modal', function () {
+		let selectedLabels = $('#datatable').DataTable().rows({selected: true}).data().toArray();
+		let labels = selectedLabels.map(v => [v[1], v[5]]);
+		let listedTypes = selectedLabels.map(v => [v[5]]);
+		console.log(listedTypes.uniq());
+		//console.log(labels.countOccurrences(1, 31));
+
+		/*
+				OMGUtils.AjaxReq(`${BASEURL}/admin/templates/@dt/@clearcache`, {
+					method: 'POST',
+					body: JSON.stringify({"method": method, "labelid": labelID[1]})
+				})
+					.then(json => {
+						if (json.err == 0)
+							toastr.success(json.value);
+						else
+							toastr.error(json.value);
+					})
+					.catch(err => toastr.error(err.url, `${err.status} (${err.statusText})`));
+		*/
+	});
+
+	$('.templateselector').modal('show');
+}
+
 // MAIN JS
 document.addEventListener('DOMContentLoaded', function () {
 
@@ -98,5 +124,6 @@ document.addEventListener('DOMContentLoaded', function () {
 	window.LoadConfig = LoadConfig;
 	window.SaveConfig = SaveConfig;
 	window.ESLMultiOperations = ESLMultiOperations;
+	window.TemplateSelector = TemplateSelector;
 
 });

+ 31 - 0
ui/admin/labelstatus.tpl

@@ -11,6 +11,8 @@
 				<a href="javascript:ESLMultiOperations('blinklabels')" class="dropdown-item">Blink</a>
 				<!-- item-->
 				<a href="javascript:ESLMultiOperations('unbind');" class="dropdown-item">Unbind</a>
+				<!-- item-->
+				<a href="javascript:TemplateSelector();" class="dropdown-item">Change Template</a>
 			</div>
 		</div>
 
@@ -124,3 +126,32 @@
 		</div>
 	</div>
 </div>
+
+{* Modal Template Selector*}
+<div class="modal fade templateselector" tabindex="-1" role="dialog" aria-labelledby="templateselector" aria-hidden="true" style="display: none;">
+	<div class="modal-dialog modal-dialog-centered">
+		<div class="modal-content">
+			<div class="modal-header">
+				<h4 class="modal-title mt-0" id="templateselector">BBB</h4>
+				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
+			</div>
+
+			<div class="modal-body">
+
+				<div class="row">
+
+					<div class="col-6">
+						<div class="card">
+							<h5 class="card-header">Template</h5>
+							<div class="card-body">
+								CCC
+							</div>
+						</div>
+					</div>
+
+				</div>
+
+			</div>
+		</div>
+	</div>
+</div>

+ 6 - 6
ui/admin/templates.tpl

@@ -1,6 +1,6 @@
 <div class="d-flex flex-wrap">
 
-	<repeat group="{{ @TPLPATH }}" value="{{ @TPL }}">
+	<repeat group="{{ @TPLPATH }}" key="{{@TPL_NAME}}" value="{{@TPL}}">
 
 		<div class="card-box">
 			<div class="dropdown pull-right">
@@ -9,16 +9,16 @@
 				</a>
 				<div class="dropdown-menu dropdown-menu-right">
 					<!-- item-->
-					<a href="javascript:ESLMultiOperations('resetfailcounter');" class="dropdown-item">Reset Fail Counter</a>
+					<a href="javascript:void(0);" class="dropdown-item">1</a>
 					<!-- item-->
-					<a href="javascript:ESLMultiOperations('blinklabels')" class="dropdown-item">Blink</a>
+					<a href="javascript:void(0);" class="dropdown-item">2</a>
 					<!-- item-->
-					<a href="javascript:ESLMultiOperations('unbind');" class="dropdown-item">Unbind</a>
+					<a href="javascript:void(0);" class="dropdown-item">3</a>
 				</div>
 			</div>
 
-			<h4 class="header-title mt-0 m-b-30">Template</h4>
-			<img style="max-width: 300px;" src="/omlink/fbtpl/{{@TPL}}"/>
+			<h4 class="header-title mt-0 m-b-30">{{@TPL_NAME}}</h4>
+			{{@TPL|raw}}
 		</div>
 
 	</repeat>