Browse Source

Add Pages backend.

mscherer 1 year ago
parent
commit
c0c32ccae9

+ 10 - 0
docs/Backend.md

@@ -0,0 +1,10 @@
+# Backend
+
+See `/admin/tools` in your browser once you made sure the plugin is loaded with routes enabled.
+
+## Available pages
+A list of all available pages of your "PagesController".
+Useful clickable quick-overview using the correct routing (dashed vs underscore etc).
+
+## Bitmask updating
+When working with bitmasks this can help to update the records in the DB.

+ 3 - 0
docs/README.md

@@ -68,6 +68,9 @@ Note: Using native enums is recommended since CakePHP 5.
 ### Command
 * [Inflect](Command/Inflect.md) to test inflection of words.
 
+### Backend
+* [Tools Backend](Backend.md) for useful backend tools.
+
 ## IDE compatibility improvements
 For some methods you can find a IdeHelper task in [IdeHelperExtra plugin](https://github.com/dereuromark/cakephp-ide-helper-extra/):
 - `IconHelper::render()` (deprecated)

+ 88 - 0
src/Controller/Admin/HelperController.php

@@ -0,0 +1,88 @@
+<?php
+
+namespace Tools\Controller\Admin;
+
+use App\Controller\AppController;
+use Cake\ORM\TableRegistry;
+use Cake\Utility\Hash;
+use Tools\Utility\Text;
+
+/**
+ * Display format helper specific debug info
+ *
+ * @property \Cake\Controller\Component\FlashComponent $Flash
+ */
+class HelperController extends AppController {
+
+	/**
+	 * @return \Cake\Http\Response|null|void
+	 */
+	public function bitmasks() {
+		$Table = TableRegistry::getTableLocator()->get('Table');
+
+		if ($this->request->is(['post', 'put'])) {
+			$matrix = $this->request->getData('matrix');
+			$modelClass = $this->request->getData('model');
+			$fieldName = $this->request->getData('field');
+			$matrixArray = explode(PHP_EOL, $matrix);
+
+			$result = [];
+			foreach ($matrixArray as $value) {
+				if (!str_contains($value, ':')) {
+					continue;
+				}
+				[$from, $to] = explode(':', $value, 2);
+				$tmp = [
+					'from' => Text::tokenize($from),
+					'to' => Text::tokenize($to),
+				];
+				$result[] = $tmp;
+			}
+			$bits = Hash::extract($result, '{n}.from');
+			if (empty($bits)) {
+				//$Table->invalidate('Tools.matrix');
+			} else {
+				$Table->addBehavior('Tools.Bitmasked', ['bits' => $bits]);
+				foreach ($result as $key => $value) {
+					$result[$key]['sql'] = $this->_bitmaskUpdateSnippet($value['from'], $value['to'], $modelClass, $fieldName);
+				}
+			}
+			$result = array_reverse($result);
+			$this->set(compact('result'));
+		}
+	}
+
+	/**
+	 * @param mixed $from
+	 * @param mixed $to
+	 * @param mixed $modelClass
+	 * @param mixed $fieldName
+	 * @return string
+	 */
+	protected function _bitmaskUpdateSnippet($from, $to, $modelClass, $fieldName) {
+		//[$class, $modelName] = pluginSplit($modelClass);
+		$Model = TableRegistry::getTableLocator()->get($modelClass);
+		$tableName = $Model->getTable(); // $dbo->fullTableName($Model);
+		$fieldName = '`' . $fieldName . '`';
+		$res = $fieldName;
+		$conditions = [];
+
+		$sql = [];
+		foreach ($from as $value) {
+			$conditions[] = $fieldName . ' & ' . $value . ' = ' . $value;
+			if (in_array($value, $to)) {
+				continue;
+			}
+			$res .= ' & ~' . $value;
+		}
+		$conditions = implode(' OR ', $conditions);
+
+		foreach ($to as $value) {
+			$res .= ' | ' . $value;
+		}
+		$sql[] = 'UPDATE ' . $tableName . ' SET ' . $fieldName . ' = ' . $res . ' WHERE ' . $conditions . ';';
+
+		return implode(PHP_EOL, $sql);
+	}
+
+}

+ 36 - 0
src/Controller/Admin/PagesController.php

@@ -0,0 +1,36 @@
+<?php
+
+namespace Tools\Controller\Admin;
+
+use App\Controller\AppController;
+use Cake\Utility\Inflector;
+use Shim\Filesystem\Folder;
+
+class PagesController extends AppController {
+
+	/**
+	 * @return \Cake\Http\Response|null|void
+	 */
+	public function index() {
+		$folder = ROOT . DS . 'templates' . DS . 'Pages' . DS;
+
+		$Folder = new Folder($folder);
+		$folders = $Folder->read();
+		$files = $folders[1];
+
+		$pages = [];
+		foreach ($files as $file) {
+			if (substr($file, -4) !== '.php') {
+				continue;
+			}
+			$page = substr($file, 0, -4);
+			$pages[$page] = [
+				'label' => Inflector::humanize($page),
+				'action' => Inflector::variable($page),
+			];
+		}
+
+		$this->set(compact('pages'));
+	}
+
+}

+ 40 - 0
templates/Admin/Helper/bitmasks.php

@@ -0,0 +1,40 @@
+<?php
+/**
+ * @var \App\View\AppView $this
+ */
+?>
+<h1>Bitmasks</h1>
+Using the BitmaskedBehavior
+
+<h2>Re-configure using SQL-Snippets</h2>
+Syntax: <b>OLDID[,...]:NEWID[,...]</b> (allowing multiple ids on each side<br />
+e.g.: <i>4:8,16</i>, single statements per line
+
+<div class="page form">
+<?php echo $this->Form->create();?>
+	<fieldset>
+		<legend><?php echo __('Adjustment Matrix'); ?></legend>
+	<?php
+		echo $this->Form->control('model', ['placeholder' => 'PluginName.ModelName']);
+		echo $this->Form->control('field', ['placeholder' => 'field_name']);
+		echo $this->Form->control('matrix', ['type' => 'textarea']);
+	?>
+	</fieldset>
+<?php echo $this->Form->submit(__('Submit')); echo $this->Form->end();?>
+</div>
+
+<?php if (!empty($result)) { ?>
+<h2>Result</h2>
+<?php
+foreach ($result as $key => $value) {
+	echo pre($value['from']);
+	echo pre($value['to']);
+
+}
+echo '<pre>';
+foreach ($result as $key => $value) {
+	echo $value['sql'] . PHP_EOL;
+}
+echo '</pre>';
+?>
+<?php } ?>

+ 1 - 0
templates/Admin/Icons/index.php

@@ -8,6 +8,7 @@
 	<div class="col-lg-6">
 		<h1>Font Icons</h1>
 		<p>As configured in app.php (through `Icon.map`)</p>
+		<p><i>Note: Deprecated, moved to Templating plugin backend.</i></p>
 
 		<?php
 		$icons = $this->Icon->getConfig('map');

+ 21 - 0
templates/Admin/Pages/index.php

@@ -0,0 +1,21 @@
+<?php
+/**
+ * @var \App\View\AppView $this
+ */
+?>
+
+<div class="col-sm-12">
+<h1>Pages</h1>
+
+<ul>
+<?php
+foreach ($pages as $page) {
+?><li>
+		<?php
+		echo $this->Html->linkReset($page['label'], ['controller' => 'Pages', 'action' => 'display', $page['action']]);
+		?>
+	</li>
+<?php } ?>
+</ul>
+
+</div>

+ 6 - 0
templates/Admin/Tools/index.php

@@ -8,6 +8,12 @@
 	<div class="col-12">
 		<h1>Tools plugin</h1>
 
+		<h2>Useful quick-tools</h2>
+		<ul>
+			<li><?php echo $this->Html->link('Available "pages"', ['controller' => 'Pages', 'action' => 'index']); ?></li>
+			<li><?php echo $this->Html->link('Bitmasks', ['controller' => 'Helper', 'action' => 'bitmasks']);?></li>
+		</ul>
+
 		<h2>Helper debugging</h2>
 		<ul>
 			<li><?php echo $this->Html->link('Icon helper and font icons', ['controller' => 'Icons', 'action' => 'index']); ?></li>

+ 26 - 0
tests/TestCase/Controller/Admin/HelperControllerTest.php

@@ -0,0 +1,26 @@
+<?php
+
+namespace Tools\Test\TestCase\Controller\Admin;
+
+use Cake\TestSuite\IntegrationTestTrait;
+use Shim\TestSuite\TestCase;
+use Tools\Controller\Admin\HelperController;
+
+#[\PHPUnit\Framework\Attributes\UsesClass(HelperController::class)]
+class HelperControllerTest extends TestCase {
+
+	use IntegrationTestTrait;
+
+	/**
+	 * @return void
+	 */
+	public function testBitmasks() {
+		$this->disableErrorHandlerMiddleware();
+
+		$this->get(['prefix' => 'Admin', 'plugin' => 'Tools', 'controller' => 'Helper', 'action' => 'bitmasks']);
+
+		$this->assertResponseCode(200);
+		$this->assertNoRedirect();
+	}
+
+}

+ 26 - 0
tests/TestCase/Controller/Admin/PagesControllerTest.php

@@ -0,0 +1,26 @@
+<?php
+
+namespace Tools\Test\TestCase\Controller\Admin;
+
+use Cake\TestSuite\IntegrationTestTrait;
+use Shim\TestSuite\TestCase;
+use Tools\Controller\Admin\PagesController;
+
+#[\PHPUnit\Framework\Attributes\UsesClass(PagesController::class)]
+class PagesControllerTest extends TestCase {
+
+	use IntegrationTestTrait;
+
+	/**
+	 * @return void
+	 */
+	public function testIndex() {
+		$this->disableErrorHandlerMiddleware();
+
+		$this->get(['prefix' => 'Admin', 'plugin' => 'Tools', 'controller' => 'Pages', 'action' => 'index']);
+
+		$this->assertResponseCode(200);
+		$this->assertNoRedirect();
+	}
+
+}

+ 2 - 1
tests/TestCase/Controller/Admin/ToolsControllerTest.php

@@ -14,12 +14,13 @@ class ToolsControllerTest extends TestCase {
 	/**
 	 * @return void
 	 */
-	public function testIcons() {
+	public function testIndex() {
 		$this->disableErrorHandlerMiddleware();
 
 		$this->get(['prefix' => 'Admin', 'plugin' => 'Tools', 'controller' => 'Tools', 'action' => 'index']);
 
 		$this->assertResponseCode(200);
+		$this->assertNoRedirect();
 	}
 
 }