diff --git a/module-example-table/mod_table_example/mod_table_example.xml b/module-example-table/mod_table_example/mod_table_example.xml new file mode 100644 index 0000000..f6d1a6b --- /dev/null +++ b/module-example-table/mod_table_example/mod_table_example.xml @@ -0,0 +1,13 @@ + + + Joomla example table module + 1.0.1 + me + today + Demonstrates use of Table class APIs + My\Module\TableExample + + services + src + + diff --git a/module-example-table/mod_table_example/services/provider.php b/module-example-table/mod_table_example/services/provider.php new file mode 100644 index 0000000..c3faeb5 --- /dev/null +++ b/module-example-table/mod_table_example/services/provider.php @@ -0,0 +1,19 @@ +registerServiceProvider(new ModuleDispatcherFactoryServiceProvider('\\My\\Module\\TableExample')); + $container->registerServiceProvider(new HelperFactoryServiceProvider('\\My\\Module\\TableExample\\Site\\Helper')); + $container->registerServiceProvider(new ModuleServiceProvider()); + } +}; diff --git a/module-example-table/mod_table_example/src/Dispatcher/Dispatcher.php b/module-example-table/mod_table_example/src/Dispatcher/Dispatcher.php new file mode 100644 index 0000000..5695350 --- /dev/null +++ b/module-example-table/mod_table_example/src/Dispatcher/Dispatcher.php @@ -0,0 +1,54 @@ +module = $module; + $this->input = $input; + } + + public function dispatch() + { + // This is the entry point for our module code + echo '

Hello ' . $this->module->id . '

'; + + // Pass the work off into a helper class + // The default Joomla Factory classes set the Database object within the Helper class, + // but not within the Dispatcher class, and we need the dbo for passing to the Table + $helper = $this->getHelperFactory()->getHelper('ExampleTableHelper'); + $helper->doBasicTableOperations($this->module->id, $this->input); + $helper->doAdvancedTableOperations($this->module->id, $this->input); + } +} diff --git a/module-example-table/mod_table_example/src/Helper/ExampleTableHelper.php b/module-example-table/mod_table_example/src/Helper/ExampleTableHelper.php new file mode 100644 index 0000000..7a9a2c8 --- /dev/null +++ b/module-example-table/mod_table_example/src/Helper/ExampleTableHelper.php @@ -0,0 +1,109 @@ +getDatabase()); + + // we load the database record in the #__modules table which relates to this module + if ($moduleTable->load($id)) + { + // demonstrates that the properties are set via the load() call + echo "Module title is {$moduleTable->title}
"; + + // The header tag is held as one of the JSON-encoded params, so we need to decode the params + $moduleParams = json_decode($moduleTable->params, true); + $moduleParams['header_tag'] = 'h2'; + // The params will be JSON-encoded in the bind() method before storing in the database because we set in our table class + // $_jsonEncode = array('params'); + + // get the demonote= HTTP GET parameter and put it into the note field + $note = $input->get('demonote', '', "STRING"); + $data = array("note" => $note, "params" => $moduleParams); + + // bind the updated data in the $data array + $moduleTable->bind($data); + + $moduleTable->check(); + + // store the data - this will result in a SQL UPDATE to this module's record in the #__modules table + $moduleTable->store(); + } + } + + /* + * doAdvancedTableOperations demonstrates use of the advanced Table methods + * + * $id - the id of this module + * $input - the Joomla Input instance + */ + public function doAdvancedTableOperations($id, $input) + { + $moduleTable = new ExampleModuleTable($this->getDatabase()); + $user = \Joomla\CMS\Factory::getApplication()->getIdentity(); + + if ($moduleTable->load($id)) + { + // checkout/checkin + if ($moduleTable->isCheckedOut($user->id)) + { + echo "
module record isCheckedOut call returned true
"; + } + else + { + echo "
module record isCheckedOut call returned false
"; + } + + // Read ACL rules - you can also try moving this code to after the setRules() call + if ($rules = $moduleTable->getRules()) + { + $rulesString = (string) $rules; + echo "
ACL Rules: $rulesString
"; + } + else + { + echo "
As expected, getRules() didn't return anything
"; + } + + // Set the ACL on this module + $userGroups = $user->getAuthorisedGroups(); + $randomIndex = array_rand($userGroups); + $newRule = array("core.edit" => array($userGroups[$randomIndex] => 1)); + echo "Setting rules to " . json_encode($newRule) . "
"; + $moduleTable->setRules($newRule); + $moduleTable->store(); + + // Ordering - change the ordering with other modules in the same template position + $where = 'POSITION = "' . $moduleTable->position . '"'; + echo "
Next order value: " . $moduleTable->getNextOrder($where) . "
"; + + $moduleTable->move(2, $where); + echo "Ordering value is now: {$moduleTable->ordering}
"; + + $where .= " and published = 1"; + $moduleTable->reorder($where); + + // Reflection method - getTableName + echo "
Table name is {$moduleTable->getTableName()}
"; + } + } +} \ No newline at end of file diff --git a/module-example-table/mod_table_example/src/Table/ExampleModuleTable.php b/module-example-table/mod_table_example/src/Table/ExampleModuleTable.php new file mode 100644 index 0000000..73a526a --- /dev/null +++ b/module-example-table/mod_table_example/src/Table/ExampleModuleTable.php @@ -0,0 +1,74 @@ +note .= " added via module"; + return true; + } + + protected function _getAssetName() + { + return "com_modules.module." . $this->id; + } + + protected function _getAssetTitle() + { + return $this->title; + } + + // this function copied from the ModuleTable in src/Table/Module.php + protected function _getAssetParentId(?Table $table = null, $id = null) + { + $assetId = null; + + // This is a module that needs to parent with the extension. + if ($assetId === null) { + // Build the query to get the asset id of the parent component. + $db = $this->getDatabase(); + $query = $db->getQuery(true) + ->select($db->quoteName('id')) + ->from($db->quoteName('#__assets')) + ->where($db->quoteName('name') . ' = ' . $db->quote('com_modules')); + + // Get the asset id from the database. + $db->setQuery($query); + + if ($result = $db->loadResult()) { + $assetId = (int) $result; + } + } + + // Return the asset id. + if ($assetId) { + return $assetId; + } + + return parent::_getAssetParentId($table, $id); + } +} \ No newline at end of file