Joomla two module using same helper.php - joomla

I've built two custom modules for Joomla ("reservation" and "contact") which are working just fine, however time to time I have to upgrade them.
Is it possible to make them to share the same "helper.php" so I could keep code in one place? For example the post function is the same for these two modules. I want the "contact" module to use the "reservation" helper.php post function.
Thanks

You can simply include the reservation module helper inside the contact module and use it.
For example lets say that your modules are mod_reservation and mod_contact, in mod_contact.php you include the reservation helper file and use it like this:
require_once JPATH_SITE.DS."modules".DS."mod_reservation".DS."helper.php";
modReservationHelper::post();

Or you make a custom helper module,, which is needed in order for the other 2 to work. I dont know for sure but I thought you can check for other modules when installing a module. In that check you check for the helper module and if not present you give a error. You could also say that a certain module needs atleast version X.XX.XX of the helper module and if the version is too long also give an error in during the install of the module.

Related

Best practice to modular programming in Laravel 5+

I'm starting a new project and I want to reuse some parts of it, mainly the stuff related to user registration and authentication. I can copy and paste all of the code but I want to use again. I know there is Package Development in Laravel but it's not easy and feel like there must be a better way.
Some days ago I find a pingpong/modules but I don't know about it. It's third party plugin and don't trust it.
Use this plugin is true? Is this plugin is updated later? What's different between Embedd Package Laravel and pingpong/modules? or Do you have any suggestion?
Pingpong modules seems to be build for the earlier version of Laravel 5 and in how far they are compatible with future versions (and maybe current 5.1.11) I cannot say.
There isn't much activity going look the commit history for 2.1, as of today(18 dec) the last commit was over 6 months ago.
But is the package specifically designed for Laravel? It seems to. They offer a bunch of features which are useful for development. The only unfortunate thing is you get a LOT of code within your own git environment (is it a good thing? I don't know, what do you prefer).
Personally I don't like it in this way for development, I prefer them in the vendor/ folder else it's a pain to update it to newer a version.
Since Laravel 5 Taylor wanted to make package development not too specific anymore, like in Laravel 4. The only thing what you can do (but not have to) to make your package using Laravel is using the ServiceProvider's. The ServiceProvider is the bootstrap into the Laravel application.
If you want to extend or implement your own functionality, fork the repo and build it yourself on top off it and host it (through github/packagist or a private repo using Satis).
Pingpong modules (2.1) is build for Laravel 5 and they you described (Embedded Laravel Package) is more for Laravel 4, because the more specific way you have to write the package.
But, there is alternative?
Whenever you want a more active project/package for development you should tryout Asgard CMS. They are pretty modular and I thought I read somewhere it was inspired by this package (totally not sure).
How about building yourself?
Of course you can build your own packages to achieve the same result. And create it as modular as you want. I created a lot modules for my company and we can create pretty easy a entire system and using and extending/overriding modules. Even small parts from a module can be overwritten to project specific needs.
We have chosen for almost the same structure as the app/ folder which Laravel projects, in case of CMS/API modules.
A packages look like:
tests/
src/
Acme/
Controllers/
Requests/
Models/
Module.php // contains some specifc calculations for example
ModelServiceProvider.php
composer.json
In the composer.json file we autoload: "Module\\": "src/"
And in the config/app.php we register the ModuleServiceProvider. Now we injected the functionality into Laravel's container and can we use it through the app() instance.
But whenever we only want to use the Models with in another project or standalone, we can still use it because the autoloaded features from composer and the way we build the package. Possible to use:
<?php
require_once __DIR__ .'/vendor/autoload.php';
use Module\Models\Module;
$module = new Module;
Edit
The package structure we like to use, to have a section for API or CMS stuff:
tests/
src/
Cms/
Controllers/
Requests/
Api/
Controllers/
Transformers/
Models/
Module.php // contains some specifc calculations for example
Providers/
CmsServiceProvider.php // includes `ModuleServiceProvider`
ApiServiceProvider.php // includes `ModuleServiceProvider`
ModuleServiceProvider.php // contains global stuff like commands etc.
composer.json
and instead of registering ModuleServiceProvider in config/app.php we register the ApiServiceProvider or CmsServiceProvider depending on the wishes of the client/project.
To reuse your classes simply use php namespaces or use to call back your clases.
Using the namespace
namespace Acme\Tools;
class Foo
{
echo "me";
}
You can the call class foo
<?php
$foo = new \Acme\Tools\Foo();
Using Use.
You can also use use Statement as below :
<?php
use \Acme\Tools\Foo;
$foo = new Foo();
Use Middleware
You should also use middleware to filter who should use the scripts ie the Auth middle-ware , which will help you in filtering users , registrations , logins READ MORE http://laravel.com/docs/5.1/middleware
Use Eloquent
Use ORM to create REST apis to your models , its very simple , always let your controller class extend eloquent use Illuminate\Database\Eloquent\Model; ie as :
use Illuminate\Database\Eloquent\Model; .Read More http://laravel.com/docs/5.1/eloquent
Lastly Use Laravel In built Helper functions
There are numerous Laravel In built Helper functions , to use simply go over the documentation to help you
I've used pingpong modules. It a pretty cool package. I'm not sure if it's updated much. But it's a very simple package. The only thing it does is create a folder with almost the same structure as in the app folder + views. But these are modules. You can reuse it if you program them right. The same goes for the other answer from jimmy if you have a good structure you can reuse anything.
EDIT
In the image below you'll see an example of pingpong modules. As you it's pretty much the same structure as the app folder. Maybe more the root folder. Normally it runs start.php and you have a routes.php file int he Http folder. I customized mine a bit. And load the frontend and backend routes within the RouteServiceProvider. This is build with laravel 5.1.

ZF2 data table - configuring ZFTable

I am looking at zfmodules to get a datatable module, and have found the perfect one :
https://github.com/dudapiotr/ZfTable
..however there is very little explanation of how to configure it into an existing project, change the data source etc.
Does anyone have any experience of this module, or is successfully using it in production?
The thing that i understood from Question is " You want to inject this
Module in your current Application ", well i have checked the link [https://github.com/dudapiotr/ZfTable][1] and the module seems perfect to inject to your Application. *The only thing which seems unfitting is the 'data' folder, you should compile
'customer.sql' onto your database, and then remove the folder 'data', since it does not matches to the framework structure
**Try these steps **
*
create your sql database exactly as mentioned in customer.sql
copy this module to your application, with other residing modules (if any).
Add the module name to application.config.php in Application/config/autoload directory ( since you are adding module manually, you must add your module name there by yourself)
And last, Routing configuration of your module in Application/config/module.config.php
if you need any help in routing of step 4, go here [Zend framework not able to route the translated child routes
*

How to view drupal 7 sessions from an external PHP page?

I installed drupal 7 and I need to include a block of PHP code in the user profile page.
I created a new block and I put this code in it:
"include (http://mysite/sites/all/themes/campi_aggiuntivi.php)"
In "campi_aggiuntivi.php" I put the following code:
session_start ();
print_r ($ _SESSION);
But it returns an empty array.
How do I interact with drupal sessions in this external page?
Thank you very much for your answares.
Thank you all for your answers.
I'm trying to install a module and insert it into a block avege as I suggested.
I created the file test.info
name = Test Block
description = description Test Block
package = package Test Block
core = 7.x
dependencies[] = block
; Information added by drupal.org packaging script on 2013-02-01
version = "7.x-1.x-dev"
core = "7.x"
project = "Test Block"
datestamp = "1359680350"
But I can not figure out which function I put in the file test.module, to insert the test module management screen Blocks
Can you help me please?
Thank you again to all
I think you need to include Drupal Bootstrap and a few other necessary procedures. Here's a link to an issue on d.o that might help.
1st thing i want to suggest is don't include like this:
"include (http://mysite/sites/all/themes/campi_aggiuntivi.php)"
Use local path instead: "include (sites/all/themes/campi_aggiuntivi.php)"
But this depends on from where you are calling, if both the files are at same location(calling in user profile tpl file) then this might work: "include (campi_aggiuntivi.php)"
I presume you need session details to figure-out the user id, that information can be retrieved from $profile variable or arg(1)
I have not tested your code but the way you are trying to do is not the conventional way to extend Drupal functionality.
Also i hope print_r ($ _SESSION); is actually print_r ($_SESSION); in your code, i.e. without spaces.
If you want to add a block in user profile page, i recommend doing it the drupal way.
Drupal already has an excellent example for creating a block programmatically. Here is the link.
Have a look at block_example module inside it. It will give you relevant example codes that you can use to create the block.
The example also contains information about how to make the block appear in certain pages by default.
After you create the block place them in the region you would want to, like left sidebar, content etc of the relevant theme that is enabled on user profile pages.
Inside your custom module you would access to $_SESSION variable.
From here on you can include the code in campi_aggiuntivi.php inside your module itself.
Or use the following foolproof method to load the file. This would even work when you call it from a theme.
Say you want to use custom_module_name.campi_aggiuntivi.inc located in your custom module, from a theme then you can use module_load_include
For example,
module_load_include('inc', 'custom_module_name', 'custom_module_name.campi_aggiuntivi');
the above call would load custom_module_name.campi_aggiuntivi.inc from custom_module_name module folder. This way you don't have to hard code the path required to access the file.
Based on your update.. it sounds like you are now trying to create a block programmatically. There are example modules that you can download on Drupal.org. There is a block module in that download.

Utility Classes In Ruby on Rails

This is probably a stupid question, but I'm new to Ruby on Rails and I could use a little guidance. I want to have a helper/utility class that performs a group of network operations and returns results. Where do I put that class and how do I use it.
I've created network_helper.rb in my app/modulename/helpers directory. In my controller when I try to do
myNetworkHelper = ModuleName::NetworkHelper.new
results = myNetworkHelper.getResults
I get an error
undefined method `new' for MyModule::NetworkHelper:Module
I'm sure this is just a misunderstanding of how ruby on rails works. Can I get some clarification?
Would it be better to make this a class instead of a module and put it in libs? And can I add subfolders in libs and have them automatically loaded?
Lib or Classes
Little utility classes like this typically go in the lib folder, though some people prefer to create a folder called classes. Whichever you choose, make sure you import the folder in config/application.rb, as the lib folder is not autoloaded:
config.autoload_paths += %W(#{config.root}/lib)
Concerns
If instead of a utility class, you want to extend some of your models with reusable code, you may also wish to look at the new Rails 4 concerns folders which encourage you to extract reusable modules:
see: How to use concerns in Rails 4
To use new, the thing your calling it on must be a class, not a module. You're using a module. Change module to class in lib/utilities/network_utility.rb.
I cannot verify this at the moment, however I believe one place you can store your custom modules and classes is the lib directory. Alternatively, you should be able to store them in the app directory in the manner you have indicated by adding the following line to your environment.rb:
config.load_paths << File.join(Rails.root, "app", "modulename")
Also, check out Yehuda Katz's answer, which I think not only answers your question better, but also contains some very interesting and useful information and concepts relating to your situation. Hope that helps!
Add your class to app/lib folder instead of lib, so that you don't change autoload paths!
Explanations:
The accepted answer suggests adding the classes to lib.
But according to this discussion:
The lib folder does not belong to the autoload paths since Rails 3.
So it's discouraged to add lib under autoload path. Use app/lib instead.

Magento, how to disable module programmatically?

My goal is to disable the module programmatically (for example during some observer event).
The earliest observer I found is controller_front_init_before.
So my module is listening to it, and then do the next:
Mage::getConfig()->getModuleConfig('IG_LightBox')->active=(string)'false';
But the selected module is still active on each page.
Also I tried this approach (the same but in different way):
Mage::getConfig()->getNode('modules/IG_LightBox')->active=(string)'false';
Also I tried to reinit config after all and to loadModules one more time, but both won't help.
Mage::getConfig()->loadModules(); // won't help
Mage::getConfig()->reinit(); // won't help
Is it possible to disable the module programmatically?
Update 1.
This solution perfectly works for the back-end. active=false really disables the module, but I need it for the front-end too. So I keep my search.
Update 2
There are 2 methods in the app/Mage.php, called init and initSpecified, which allows to run the Magento with the selected number of modules only. But those methods are not called in the default flow.
Update 3
There is an observer event we can use for activating or deactivating the payment modules on the fly. It's called payment_method_is_active. This code example makes the check money order payment method being not active:
public function payment_method_is_active(Varien_Event_Observer $observer)
{
if($observer->getMethodInstance()->getCode()=='checkmo')
{
$observer->getResult()->isAvailable=false;
}
}
I think it depends on which kind of module you want to disable. My article worked for the kind of module I wanted to disable, but that module double-checked if it was activated, while most modules don't do that.
Magento loads all module configuration at once. Its impossible to create a module that will listen to this process because the module would have not been loaded while the process takes place. This creates a paradox. Its impossible to prevent a module from loading using another module.
As a result the only options you are left with are:
Edit the core.
Use modules which are better designed and explicitly allow themselves to be disabled through a config option or method.
Edit/extend the module you want to disable so that you can disable its functionality during runtime.
Hope this helps.. let me know if I can help you find a way to disable the module you're dealing with.
Here is one solution I found.

Resources