How to create a module that changes other modules' behaviors - module

I want to create a module for SocialEngine that changes the signup behavior by adding the option to signup using phone number and sending text message to the user's phone and letting them confirm it. I guess I need to add code in the file application/modules/User/Form/Signup.php. For example, I can add a new field as follows:
$this->AddElement('Text', 'sms', [
'class' => 'signup-name',
'label' => 'Phone Number',
'required' => true,
'validators' => [
['StringLength', true, ['min'=>11, 'max'=>11]]
]
]
);
$this->sms->getValidator('StringLength')->setMessage('Phone must be 11 digits');
However, I don't want to manually add the code. I need to add the functionality by installing the plugin and disable it by removing or disabling the plugin. How can I do that?
Do I need to mess with installation php scripts and do file operations like overwriting files or is there a better way to separate my files completely, meanwhile making them serve as a complement for a given module?

Related

Yii2 - Advance Template with backend branches

Please guide,
I once created a Yii2 app using a basic template.
Now, I am moving to advanced template.
Because i am facing a problem like this.
Let say, We have Headquarters and branch offices.
My question is the structured of app.
- backend
- frontend
I have 2 question,if I have structured like this:
- backend-master (abstract/interfaces class is came from this)
- backend-headquarters
- backend-branch-offices-1
- backend-branch-offices-2
- frontend
Is it good approach method ?
How to use a one layout web to all backend
Please advise.
No. All you need is actually a sub module. not a separate backend module like this.
You may configure each sub-module to use the view file from any sub-module inside backend module in each of your sub-module configuration.
backend/module/headquaters/module/config/main.php (i assume)
'components' => [
... other components
'view' => [
'theme' => [
'pathMap' => [
// you may specify any directory as your view directory from here
'#backend/views' => '#backend/views',
'#frontend/adminlte/views' => '#frontend/themes/adminlte/views',
],
],
],
... other components

using yii rest api with default URL GET format rather than PATH format

I have a problem with a yii rest api. I configured it to work following the tutorial on the yii framework page, but after that i realised that my api works BUT NOT some big PORTIONS of my PAGE since it is based on the GET URL format rather than PATH which is required by the rest api.
So in my config/main.php i have the following setting
'urlManager' => array (
'urlFormat' => 'path',
'rules' => array (
'student/<id:\d+>/<title:.*?>' => 'student/view',
'students/<tag:.*?>' => 'student/index',
array (
'apistudent/register',
'pattern' => 'api/<model:\w+>',
'verb' => 'POST'
),
'<controller:\w+>/<action:\w+>' => '<controller>/<action>'
)
),
I also have a controller named ApiStudentController with a method called actionRegister().
So as already stated the api works normally but my page does not since i set the urlFormat to be 'path'.
The question is... how can i use the rest api but without the PATH url format and rather the default get url format (index.php?r=apistudent/register)?
I too faced the same problem in yii 1.x. I just need my API controller alone in old GET format rather than in PATH format (as i changed my websites URLs in PATH format). Finally i got it worked with a small hack in script file
$app = Yii::createWebApplication($env->configWeb); //store the app
//Change the UrlFormat for urlManager to get if a get request is given instead of a path format one.
if (isset($_GET['r'])) {
Yii::app()->urlManager->setUrlFormat('get');
}
$app->run(); //run the app
I dont know whether this solves your problem. But this can give you an idea. Happy Coding!

Catalyst Framework HTML::Formhandler shows vadiate errors, when using HTML GET to Access the Site

I have an HTML::Formhandler Form on my Catalyst Framework. The Problem is, that I get an error-message in the Form, when I load the Form-Site with an HTML GET-Requelst.
has_field 'name' => (type => 'Text', required => 1);
So if I load the Site via: localhost:3000/form no errors occurs.
But if I load the Site via localhost:3000/form?foo=bar the form says: "Field required".
Any Idea how to solve this?
By default HTML::FormHandler determines whether to validate a from by the existence of params. If you don't want to do that, you can use the 'posted' flag in the ->process statement. If you want the query parameter to provide a default to the form, you need to pass it in via an init_object: init_object => { foo => bar }.

app can't find module?

I am working on a project. It was fine until I installed new windows on my PC. But now project is same and when I access
Yii::app()->controller->module
It returns null. It also returns null for: Yii::app()->controller->module->id. When I viewed config file it had admin module in it. I don't know why is it's returning null. Can't find a way out.
Module in config file is like:
'modules' => array(
// uncomment the following to enable the Gii tool
'admin',
'gii' => array(
'generatorPaths' => array(
'bootstrap.gii'
),
'class' => 'system.gii.GiiModule',
'password' => '1234',
// If removed, Gii defaults to localhost only. Edit carefully to taste.
'ipFilters' => array('127.0.0.1', '::1'),
),
),
When you access Yii::app()->controller->module then it will return the module that current controller belongs to. It returns null if the controller does not belong to any module. Please make sure the current controller you accessed which is belong to any module you configured.
You can see this link: http://www.yiiframework.com/doc/guide/1.1/en/basics.module to work with Module.
If you want to see which modules are loaded then use:
print_r(Yii::app()->getModules());
You are not 'in' a module and want to get a specific module (like admin module): you can do
Yii::app()->getModule('admin');

How do Media Views work in cakephp?

I have arranged a file upload to happen on my application relative address: webroot/files
Now I need to force download on the uploaded files. After some googling and trying like most of the suggestions from this post I figured out the correct way to do this is using cakephps Media Views
What I have:
Main site with a table of records. Model -> Record; Table -> records;
These records have a primary key record_id.
In my database I have a Table -> files; Model -> File;
These files have a foreign key record_id and a field 'url' with the relative path to it's location.
After creating a record with files, the files are correctly uploaded to the folder, which relative address is e.g. webroot/files/record_name/file and the tables in database are correctly updated.
What I want to do:
After doubleclicking on one table row open a modal dialog with the information about the record. (done)
In this modal dialog I want to display links that will force download on these files.
I tried many variations of this:
//the retrieving of data after debug looks fine//
$this->loadModel('File');
$files = $this->File->find('list', array(
'conditions'=>array('File.record_id'=>$record_id),
'fields' => array('File.Name', 'File.Url');
))
//actual display of url
foreach($files as $file_name => $file_url) {
echo $this->Html->link($file_name, $file_url);
}
The resulting link looks exactly the way James Revillini presented
This is my actual question
Since that issue was not entirely solved, I thought it would be helpful not only for me, but for anybody who's searching for a quick solution for this problem to see a quick demonstration of how Media-views work. I have no idea where to move after making a dynamic download function:
public function download($name, $path) {
$this->viewClass = 'Media';
$params = array(
'id' => $name,
'name' => $name,
'download' => true,
'path' => $path
);
$this->set($params);
}
Point the link in the modal dialog for the resource to the download() function.
Pass the Record.id to that function. In it find the file and auto-render it.
It should work.