From a14dc2e8fe85e3ebc8bd4791503c50c092cb9401 Mon Sep 17 00:00:00 2001 From: mokaddem Date: Thu, 2 Sep 2021 11:30:09 +0200 Subject: [PATCH] chg: [tags] Transformed feature into a pluging - WiP --- composer.json | 5 +- plugins/Tags/config/routes.php | 20 ++++ plugins/Tags/src/Controller/AppController.php | 23 ++++ .../Tags/src/Controller/TagsController.php | 111 ++++++++++++++++++ .../Tags/src}/Model/Behavior/TagBehavior.php | 10 +- .../Tags/src}/Model/Entity/Tag.php | 0 .../Tags/src}/Model/Entity/Tagged.php | 0 .../Tags/src}/Model/Table/TaggedTable.php | 5 +- .../Tags/src}/Model/Table/TagsTable.php | 3 +- plugins/Tags/src/Plugin.php | 41 +++++++ src/Application.php | 5 +- src/Controller/AppController.php | 3 +- src/Controller/TagsController.php | 80 ------------- src/Model/Table/IndividualsTable.php | 2 +- src/Model/Table/OrganisationsTable.php | 2 +- 15 files changed, 213 insertions(+), 97 deletions(-) create mode 100644 plugins/Tags/config/routes.php create mode 100644 plugins/Tags/src/Controller/AppController.php create mode 100644 plugins/Tags/src/Controller/TagsController.php rename {src => plugins/Tags/src}/Model/Behavior/TagBehavior.php (98%) rename {src => plugins/Tags/src}/Model/Entity/Tag.php (100%) rename {src => plugins/Tags/src}/Model/Entity/Tagged.php (100%) rename {src => plugins/Tags/src}/Model/Table/TaggedTable.php (86%) rename {src => plugins/Tags/src}/Model/Table/TagsTable.php (89%) create mode 100644 plugins/Tags/src/Plugin.php delete mode 100644 src/Controller/TagsController.php diff --git a/composer.json b/composer.json index 13f42e8..c233ebd 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,6 @@ "cakephp/cakephp": "^4.0", "cakephp/migrations": "^3.0", "cakephp/plugin-installer": "^1.2", - "dereuromark/cakephp-tags": "^1.2", "erusev/parsedown": "^1.7", "mobiledetect/mobiledetectlib": "^2.8" }, @@ -30,12 +29,14 @@ }, "autoload": { "psr-4": { - "App\\": "src/" + "App\\": "src/", + "Tags\\": "plugins/Tags/src/" } }, "autoload-dev": { "psr-4": { "App\\Test\\": "tests/", + "Tags\\Test\\": "plugins/Tags/tests/", "Cake\\Test\\": "vendor/cakephp/cakephp/tests/" } }, diff --git a/plugins/Tags/config/routes.php b/plugins/Tags/config/routes.php new file mode 100644 index 0000000..3fab36a --- /dev/null +++ b/plugins/Tags/config/routes.php @@ -0,0 +1,20 @@ +plugin( + 'Tags', + ['path' => '/tags'], + function ($routes) { + $routes->setRouteClass(DashedRoute::class); + + $routes->connect( + '/{action}/*', + ['controller' => 'Tags'] + ); + + // $routes->get('/', ['controller' => 'Tags']); + // $routes->get('/{id}', ['controller' => 'Tags', 'action' => 'view']); + // $routes->put('/{id}', ['controller' => 'Tags', 'action' => 'edit']); + } +); \ No newline at end of file diff --git a/plugins/Tags/src/Controller/AppController.php b/plugins/Tags/src/Controller/AppController.php new file mode 100644 index 0000000..28c7ae4 --- /dev/null +++ b/plugins/Tags/src/Controller/AppController.php @@ -0,0 +1,23 @@ +loadComponent('RequestHandler'); + // $this->loadComponent('ParamHandler', [ + // 'request' => $this->request + // ]); + // $this->loadComponent('CRUD', [ + // 'request' => $this->request, + // 'table' => $this->{$this->modelClass}, + // 'MetaFields' => $this->MetaFields, + // 'MetaTemplates' => $this->MetaTemplates + // ]); + } +} \ No newline at end of file diff --git a/plugins/Tags/src/Controller/TagsController.php b/plugins/Tags/src/Controller/TagsController.php new file mode 100644 index 0000000..ab75bc9 --- /dev/null +++ b/plugins/Tags/src/Controller/TagsController.php @@ -0,0 +1,111 @@ +CRUD->index([ + 'filters' => ['label', 'colour'], + 'quickFilters' => [['label' => true], 'colour'] + ]); + $responsePayload = $this->CRUD->getResponsePayload(); + if (!empty($responsePayload)) { + return $responsePayload; + } + } + + public function add() + { + $this->CRUD->add(); + $responsePayload = $this->CRUD->getResponsePayload(); + if (!empty($responsePayload)) { + return $responsePayload; + } + } + + public function view($id) + { + $this->CRUD->view($id); + $responsePayload = $this->CRUD->getResponsePayload(); + if (!empty($responsePayload)) { + return $responsePayload; + } + } + + public function edit($id) + { + $this->CRUD->edit($id); + $responsePayload = $this->CRUD->getResponsePayload(); + if (!empty($responsePayload)) { + return $responsePayload; + } + $this->render('add'); + } + + public function delete($id) + { + $this->CRUD->delete($id); + $responsePayload = $this->CRUD->getResponsePayload(); + if (!empty($responsePayload)) { + return $responsePayload; + } + } + + // public function tag($model, $id) + // { + // $controller = $this->getControllerBeingTagged($model); + // $controller->CRUD->tag($id); + // $responsePayload = $controller->CRUD->getResponsePayload(); + // if (!empty($responsePayload)) { + // return $responsePayload; + // } + // return $controller->getResponse(); + // } + + // public function untag($model, $id) + // { + // $controller = $this->getControllerBeingTagged($model); + // $controller->CRUD->untag($id); + // $responsePayload = $controller->CRUD->getResponsePayload(); + // if (!empty($responsePayload)) { + // return $responsePayload; + // } + // return $controller->getResponse(); + // } + + // public function viewTags($model, $id) + // { + // $controller = $this->getControllerBeingTagged($model); + // $controller->CRUD->viewTags($id); + // $responsePayload = $controller->CRUD->getResponsePayload(); + // if (!empty($responsePayload)) { + // return $responsePayload; + // } + // return $controller->getResponse(); + // } + + // private function getControllerBeingTagged($model) + // { + // $modelName = Inflector::camelize($model); + // $controllerName = "\\App\\Controller\\{$modelName}Controller"; + // if (!class_exists($controllerName)) { + // throw new MethodNotAllowedException(__('Model `{0}` does not exists', $model)); + // } + // $controller = new $controllerName; + // // Make sure that the request is correctly assigned to this controller + // return $controller; + // } +} diff --git a/src/Model/Behavior/TagBehavior.php b/plugins/Tags/src/Model/Behavior/TagBehavior.php similarity index 98% rename from src/Model/Behavior/TagBehavior.php rename to plugins/Tags/src/Model/Behavior/TagBehavior.php index 437e74c..3db1b67 100644 --- a/src/Model/Behavior/TagBehavior.php +++ b/plugins/Tags/src/Model/Behavior/TagBehavior.php @@ -1,6 +1,6 @@ 'label', 'tagsAssoc' => [ - 'className' => 'Tags', - // 'joinTable' => 'tagged', // uncomment me! - 'joinTable' => 'tags_tagged', // remove me! + 'className' => 'Tags.Tags', + 'joinTable' => 'tags_tagged', 'foreignKey' => 'fk_id', 'targetForeignKey' => 'tag_id', 'propertyName' => 'tags', ], 'tagsCounter' => ['counter'], 'taggedAssoc' => [ - 'className' => 'Tagged', + 'className' => 'Tags.Tagged', 'foreignKey' => 'fk_id' ], 'implementedEvents' => [ diff --git a/src/Model/Entity/Tag.php b/plugins/Tags/src/Model/Entity/Tag.php similarity index 100% rename from src/Model/Entity/Tag.php rename to plugins/Tags/src/Model/Entity/Tag.php diff --git a/src/Model/Entity/Tagged.php b/plugins/Tags/src/Model/Entity/Tagged.php similarity index 100% rename from src/Model/Entity/Tagged.php rename to plugins/Tags/src/Model/Entity/Tagged.php diff --git a/src/Model/Table/TaggedTable.php b/plugins/Tags/src/Model/Table/TaggedTable.php similarity index 86% rename from src/Model/Table/TaggedTable.php rename to plugins/Tags/src/Model/Table/TaggedTable.php index 289cbbc..f6604d2 100644 --- a/src/Model/Table/TaggedTable.php +++ b/plugins/Tags/src/Model/Table/TaggedTable.php @@ -1,6 +1,6 @@ setTable('tagged'); $this->setTable('tags_tagged'); $this->belongsTo('Tags', [ - 'className' => 'Tags', + 'className' => 'Tags.Tags', 'foreignKey' => 'tag_id', 'propertyName' => 'tag', ]); diff --git a/src/Model/Table/TagsTable.php b/plugins/Tags/src/Model/Table/TagsTable.php similarity index 89% rename from src/Model/Table/TagsTable.php rename to plugins/Tags/src/Model/Table/TagsTable.php index 6a885dc..2149134 100644 --- a/src/Model/Table/TagsTable.php +++ b/plugins/Tags/src/Model/Table/TagsTable.php @@ -1,6 +1,6 @@ setTable('tags'); $this->setTable('tags_tags'); $this->setDisplayField('label'); // Change to name? $this->addBehavior('Timestamp'); diff --git a/plugins/Tags/src/Plugin.php b/plugins/Tags/src/Plugin.php new file mode 100644 index 0000000..84c8702 --- /dev/null +++ b/plugins/Tags/src/Plugin.php @@ -0,0 +1,41 @@ +addPlugin('DebugKit'); } $this->addPlugin('Authentication'); - $this->addPlugin('Tags'); + $this->addPlugin('Tags', ['routes' => true]); // Load more plugins here } diff --git a/src/Controller/AppController.php b/src/Controller/AppController.php index 498b0ae..ee1c1b1 100644 --- a/src/Controller/AppController.php +++ b/src/Controller/AppController.php @@ -63,9 +63,10 @@ class AppController extends Controller ]); $this->loadModel('MetaFields'); $this->loadModel('MetaTemplates'); + $table = $this->getTableLocator()->get($this->modelClass); $this->loadComponent('CRUD', [ 'request' => $this->request, - 'table' => $this->{$this->modelClass}, + 'table' => $table, 'MetaFields' => $this->MetaFields, 'MetaTemplates' => $this->MetaTemplates ]); diff --git a/src/Controller/TagsController.php b/src/Controller/TagsController.php deleted file mode 100644 index 57481e0..0000000 --- a/src/Controller/TagsController.php +++ /dev/null @@ -1,80 +0,0 @@ -Table = TableRegistry::getTableLocator()->get('Tags.Tags'); - $this->CRUD->Table = $this->Table; - $this->CRUD->TableAlias = $this->CRUD->Table->getAlias(); - $this->CRUD->ObjectAlias = Inflector::singularize($this->CRUD->TableAlias); - } - - public function index() - { - $this->CRUD->index([ - 'filters' => ['label', 'colour'], - 'quickFilters' => [['label' => true], 'colour'] - ]); - $responsePayload = $this->CRUD->getResponsePayload(); - if (!empty($responsePayload)) { - return $responsePayload; - } - $this->set('metaGroup', $this->isAdmin ? 'Administration' : 'Cerebrate'); - } - - public function add() - { - $this->CRUD->add(); - $responsePayload = $this->CRUD->getResponsePayload(); - if (!empty($responsePayload)) { - return $responsePayload; - } - $this->set('metaGroup', $this->isAdmin ? 'Administration' : 'Cerebrate'); - } - - public function view($id) - { - $this->CRUD->view($id); - $responsePayload = $this->CRUD->getResponsePayload(); - if (!empty($responsePayload)) { - return $responsePayload; - } - $this->set('metaGroup', $this->isAdmin ? 'Administration' : 'Cerebrate'); - } - - public function edit($id) - { - $this->CRUD->edit($id); - $responsePayload = $this->CRUD->getResponsePayload(); - if (!empty($responsePayload)) { - return $responsePayload; - } - $this->set('metaGroup', $this->isAdmin ? 'Administration' : 'Cerebrate'); - $this->render('add'); - } - - public function delete($id) - { - $this->CRUD->delete($id); - $responsePayload = $this->CRUD->getResponsePayload(); - if (!empty($responsePayload)) { - return $responsePayload; - } - $this->set('metaGroup', $this->isAdmin ? 'Administration' : 'Cerebrate'); - } -} diff --git a/src/Model/Table/IndividualsTable.php b/src/Model/Table/IndividualsTable.php index 3a621b3..f8c41f1 100644 --- a/src/Model/Table/IndividualsTable.php +++ b/src/Model/Table/IndividualsTable.php @@ -14,7 +14,7 @@ class IndividualsTable extends AppTable { parent::initialize($config); $this->addBehavior('UUID'); - $this->addBehavior('Tag'); + $this->addBehavior('Tags.Tag'); $this->hasMany( 'Alignments', [ diff --git a/src/Model/Table/OrganisationsTable.php b/src/Model/Table/OrganisationsTable.php index a43b7e9..2ac2b66 100644 --- a/src/Model/Table/OrganisationsTable.php +++ b/src/Model/Table/OrganisationsTable.php @@ -18,7 +18,7 @@ class OrganisationsTable extends AppTable public function initialize(array $config): void { parent::initialize($config); - $this->addBehavior('Tag'); + $this->addBehavior('Tags.Tag'); $this->hasMany( 'Alignments', [