recover files between hook, tpl and front controller - php

I create a hook allowing the user to send a csv file.
I want to get the data (the file) that the user sends via a form (present in views/templates/hook), to get it in controller/front/files.php, and insert it in my database, anyone have an idea?
my hook in mymodule.php
public function hookDisplayLeftColumnProduct($params)
{
$this->context->smarty->assign([
'files' => Tools::getValue('files')
]);
return $this->display(__FILE__, 'mymodule.tpl');
}
views/templates/hook/mymodule.tpl
<div id="mymodule_block_home">
<form method="POST">
<label for="files">Envoyer un fichier CSV</label>
<input type="file" name="files" id="files">
<button>Envoyer</button>
</form>
</div>
my front controller who's not good I guess
class MyModuleFilesFrontController extends ModuleFrontController
{
public function initContent()
{
parent::initContent();
$files = Tools::getValue('files');
$this->setTemplate('module:mymodule/views/templates/front/files.tpl');
}
}
Thank you for your help

It is not a PrestaShop-specific problem. You might want to read about uploading files in PHP there:
https://www.tutorialspoint.com/php/php_file_uploading.htm
A few hints:
check if there is something to upload, always
make sure to validate user input, always
One more thing. I see that you hook your module into the product page. You could have the logic of uploading a file directly in your hook. You don't need an extra controller for that.
You could do something like:
public function hookDisplayLeftColumnProduct($params)
{
if (Tools::isSubmit('yourSubmitButtonName') {
// here's the logic of uploading the file
}
// the rest of the code
}

Related

Pass variables from a form in a smarty tpl to a controller on form submit

I have a form in a tpl file:
<form action="{$link->getModuleLink('virtual_pos', 'validation', [], true)|escape:'html'}" method="post">
...
</form>
On submit I would like to get all the variables from the form and pass them to the controller 'validation'.
I don't wanna use any JS. It is a payment module for a store.
How can I do this?
I have found a solution in another thread.
When the link to the controller is created you can fill the variables that you need in the empty array parameter:
<form action="{$link->getModuleLink('virtual_pos', 'validation', ['id'=>$cart_id], true)|escape:'html'}" method="post">
Then in the controller you can get the data with the super global
$id_from_form_submit = $GET['id'];
If you know any other option please let me know.
In your module create a file controllers/front/validation.php.
There you need a class:
class virtual_posValidationModuleFrontController extends ModuleFrontController
{
public function postProcess()
{
/* where you get the values and validate the order */
}
public function initContent()
{
parent::initContent();
/* where you set data for a last page order confirmation */
}
}
Have you created this already?

post input data to codeigniter controller from outside codeigniter folder

I am facing a problem. I have a normal html files (contact.html) in cpanel's public_html folder. And admin panel which is developed in CodeIgniter. Now what i am trying to do is print or receive input data from contact.html file to codeigniter controller. Below mention the code
contact.html
<html>
<form name ="userinput" action="admin/index.php/contact/contact" method="post">
<div class="row contact-form">
<div class="col-lg-6">
<input type="text" name="yourname" id="yourname" class="form-control" placeholder="Your Name">
</div><!-- end col -->
</div><!-- end row -->
</form>
controller : contact.php
<?php
class Contact extends CI_Controller
{
function contact()
{
echo 'test';
echo $yourname = $this->input->post('yourname');
}
}?>
now when i am submitting data 'test' is printing but i can not get the post data of textbox.
The error is mention below.
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Contact::$input
Filename: controllers/contact.php
Line Number: 12
please help me how will i get it.
<?php
class Contact extends CI_Controller
{
public function contact(){
echo 'test';
echo $yourname = $this->input->post('yourname');
$this->load->view('index'); // put this file in application/views/index.php ,also rename to .php
}
}
?>
Try creating the function in your controller. Hope it helps !
You need to have a index function or another function
File name: Contact.php file name and class should have only first letter upper case with controllers and models.
<?php
class Contact extends CI_Controller {
public function index() {
$yourname = $this->input->post('yourname');
echo $yourname;
// Since you had named the view file with a .html you need to add .html
$this->load->view('contact.html');
}
}
If you name your view files with .php instead of a .html
$this->load->view('contact');
I would autoload the url helper on autoload.php and would look into form helper
It's possible that since your function name is same as class name that it's treating it as constructor. Rename your function to something else, update your action link in form and try again.
Better try native PHP way of getting value
echo $_POST['yourname'];
Hope this helps

handle multiple post requests to same url Laravel 5

I have two forms on a page both submits the data via POST method. I want to handle both requests on the same url i.e /home and use different controllers and methods for both forms. Below are the two routes.
Route::post('home' ,'FacebookControllers\PostsController#save');
Route::post('home' , 'FacebookControllers\MessageController#storeMessage');
In PostsController#save I am checking
if(isset($_POST['submitPost']) && $_SERVER['REQUEST_METHOD']=='POST'){
//do something
}
and in MessageController#storeMessage I am doing the same for other form
if(isset($_POST['sendMessage']) && $_SERVER['REQUEST_METHOD']=='POST'){
return "got it";
}
The problem is that only second route works. I don't if I am doing right or wrong. Please lead me to the right direction.
Route::post('home' ,'FacebookControllers\PostsController#save');
Route::post('home' , 'FacebookControllers\MessageController#storeMessage');
won't work - how should laravel determine where to post to? The way I would go is to create two jobs and one route, then check for the value in Request and dispatch the correct job.
First, create two job classes with
php artisan make:job FacebookSave
php artisan make:job FacebookStoreMessage
The generated file will look much like this:
<?php
namespace App\Jobs;
use App\Jobs\Job;
use Illuminate\Contracts\Bus\SelfHandling;
class FacebookSaveMessage extends Job implements SelfHandling
{
/**
* Create a new job instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
//
}
}
In the job's handle() method you can do what you wanted to do in your Controller.
Now the route, let's do
Route::post('home' ,'FacebookControllers\PostsController#findAction');
and according to this in your PostsController, add a method (I called it findAction) like this:
public function findAction(\Illuminate\Http\Request $request) {
if ($request->has('submitPost')) {
return $this->dispatch(new \App\Jobs\FacebookSave($request));
} else if ($request->has('storeMessage')) {
return $this->dispatch(new \App\Jobs\FacebookStoreMessage($request));
}
return 'no action found';
}
This way the correct action will be performed depending on the submitted value.
Change the job's constructor to something like:
public function __construct($data)
{
$this->data = $data;
}
And you can access the submitted values from the forms you have submitted inside the job's handle() method with $this->data
If the purpose is only to maintain the URL that appears in the browser, you may apply a trick in which you submit your post request to different ROUTES that are specifically exist for processing purpose and then redirect back to the same URL.
For example:
Route::post('home' ,'FacebookControllers\PostsController#save');
Route::post('message' , 'FacebookControllers\MessageController#storeMessage');
Then in your view , you maybe have two diffrent forms in the same view:
<!-- the first form -->
<form action="{{url('home')}}" method="post">
Enter your post:
<input type="text" name="post" />
<button type="submit" >Save</button>
</form>
<!-- the second form -->
<form action="{{url('message')}}" method="post">
Enter your post:
<input type="text" name="post" />
<button type="submit" >Save</button>
</form>
Now in your controller, in the both actions do something like :
public function save(Request $request)
{
// do stuff
$obj->save()
return redirect('home')->with('status' , 'you have saved your post')
}
public function storeMessage(Request $request)
{
// do stuff
$obj->save()
return redirect('home')->with('status' , 'your message has been saved')
}
By doing so, the URL will remain same for the user, after the process is done the user will be redirected back to the same URL with the status.
May be this is not a good coding practice, but it solve the problem by maintaining the same URL while having multiple posts to the same controller in the same view.
The most important point here is that you have to strictly prevent an error during the process by using conditions and redirect to the same URL in case of any problem, otherwise the action will try to show the user the tricky ROUTE (yoursite.com/message) for any error.

How to get Detail view of bootstrap html form after submit Codeigniter

I have a bootstrap form where after filling data its successfully get inserted to database .Now i want to show detail view of form with filled data but it taking me back to create view after submit with data added to database. i guess i have problem with site url. for better understanding hereby i am attaching my code.
my create view file code is :
<form class="form-horizontal" id="job" action="<?php echo site_url('admission/add_students')?>" method="POST" name="job">
where as controllers(admission):
function add_students() {
$this->load->model('admission_detail_model');
$data=array(
'student_id'=>'La-0002'.$this->input->post('student_id'),
'father_name'=>$this->input->post('father_name'),
'mother_name'=>$this->input->post('mother_name'),
'fname'=>$this->input->post('first_name'),
'lname'=>$this->input->post('Last_name'),
'place_of_birth'=>$this->input->post('place_birth'),
'mother_tounge'=>$this->input->post('mother_tounge'),
'd_o_b'=>$this->input->post('DOB'),
'nationality'=>$this->input->post('nationality'),
'religion'=>$this->input->post('religion'),
'sc_st_obc'=>$this->input->post('sc_st_obc'),
'caste'=>$this->input->post('caste'),
'address'=>$this->input->post('address'),
'admitting_student'=>$this->input->post('Admit_std'),
'father_edu_qual'=>$this->input->post('father_q'),
'mother_edu_qual'=>$this->input->post('mother_q'),
'annual_income'=>$this->input->post('annual_income'),
'father_occupation'=>$this->input->post('father_occupation')
);
$this->admission_detail_model->add_students($data);
$this->index();
}
Model:
class Admission_detail_model extends CI_Model {
function add_students($data) {
$this->db->insert('students',$data);
return; }
Everything working fine i just want to add detail view after submit form not another create form View. For detail view i have controller defined in my base controller(admission) :
public function detailed_admission()
{
$this->load->helper('url');
$this->load->view('Header');
$this->load->view('side_menu');
$this->load->view('admission/detailted_view');
$this->load->view('footer');
}
when i try to replace site url in create view file
"<?php echo site_url('admission/detailted_view')?>"`
it does not enter data in database redirect directly to this view without any data.
This is my first question so if i had made any mistake please avoid it.
Thankyou for helping
Check my comments in the code.
function add_students() {
$this->load->model('admission_detail_model');
$data=array(
'student_id'=>'La-0002'.$this->input->post('student_id'),
'father_name'=>$this->input->post('father_name'),
'mother_name'=>$this->input->post('mother_name'),
'fname'=>$this->input->post('first_name'),
'lname'=>$this->input->post('Last_name'),
'place_of_birth'=>$this->input->post('place_birth'),
'mother_tounge'=>$this->input->post('mother_tounge'),
'd_o_b'=>$this->input->post('DOB'),
'nationality'=>$this->input->post('nationality'),
'religion'=>$this->input->post('religion'),
'sc_st_obc'=>$this->input->post('sc_st_obc'),
'caste'=>$this->input->post('caste'),
'address'=>$this->input->post('address'),
'admitting_student'=>$this->input->post('Admit_std'),
'father_edu_qual'=>$this->input->post('father_q'),
'mother_edu_qual'=>$this->input->post('mother_q'),
'annual_income'=>$this->input->post('annual_income'),
'father_occupation'=>$this->input->post('father_occupation')
);
//To insert the record in the database
$this->admission_detail_model->add_students($data); after
//open the detail view page
// $this->index(); why did you call index function ??
$this->detailed_admission(); //call detailed function just after the form submission or you can perform redirect('admission/detailed_admission')
}

How to create a Layout for a custom view in SugarCRM

I have created a fully custom view, I want this view to only show certain fields in an editview format so I can update records. But this view is to be different from the normal editview. How do I add a custom metadata file to this view that will allow me to define the form and fields that I need? The view is tied to a custom button and currently just shows "works". This is working so far just need to understand how to define the layout.
the controller:
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
class CustomCasesController extends SugarController {
function action_resolve_Case() {
$this->view = 'resolve_case';
}
}
The view :
if (!defined('sugarEntry') || !sugarEntry)
die('Not A Valid Entry Point');
require_once('include/MVC/View/SugarView.php');
class CasesViewresolve_case extends SugarView {
public function CasesViewresolve_case() {
parent::SugarView();
}
function preDisplay() {
parent::preDisplay();
}
public function display() {
// include ('test.php');
echo "works";
}
}
Old, but still may help someone...
You can :
work inside the display function. Everything you do or echo here will be shown inside the main Sugar app screen (navbar, footer, etc) so it will look native.
Work directly inside the controller and echo everything out.
Build your form with the fields you want to edit and have as action a function in controller where you can use the bean->save() method for the POST results.
<form name="whatever" method="POST" action="index.php?module=Contacts&action=yourcustomaction">
ex:
`$contact = new Contact();
$contact->retrieve("{$_REQUEST['contactId']}");
//be sure to send the id in the button/link to the view
$contact->first_name =$_POST['first_name'];
$contact->last_name =$_POST['last_name'];
.....
$contact->save();`
Not very elegant but the only other option I know of is working with smarty templates which I'm not familiar with.
If anybody has a better solution, please post it.

Categories