Writing custom php script in joomla - joomla3.0

I am good in php and javascript. I need a guidance where to place my custom php script if i make a ajax call from joomla.
I need to fetch the file names from a specific folder. I could make an ajax that will communicate with my php code and in return, my php code will give me the files of the specific folder.
Where is that php script to be place and how it should be routed.
For example
$.ajax({
url:'....../fetchfiles.php',
success:function(data){
}
})
Hope i was clear.

If I understood correctly your question my answer would be that you can place your fetchfiles.php wherever you want providing that you put right path to the file in your $.ajax call.
Then in the fetches.php you should call Joomla framework before you start your code, something like this:
// no direct access
define( '_JEXEC', 1 );
// get joomla root
$jpath_root = $_POST['jpath_root'];
//load Joomla
define( 'JPATH_BASE', $jpath_root );
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );
jimport( 'joomla.application.application' );
jimport( 'joomla.filter.filteroutput' );
$japp = JFactory::getApplication('site');
/* now you are good to go with your code */
Just a few words about
// get joomla root
$jpath_root = $_POST['jpath_root'];
part.
My experience is that it is the best if you pass JPATH_ROOT variable via $.ajax( one way would be to have hidden input with value set to 'JPATH_ROOT' in your joomla file from where you call ajax and then in your ajax.js just pick that value up and pass to the ajax.php or fetches.php in your case)
You can define JPATH_BASE another way too, eg
define( 'JPATH_BASE', realpath(dirname(__FILE__)));
or
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../../..' ));
depending on your actual files structure but be aware that this way you practically redefined original JPATH_BASE constant and that it may cause conflicts in case you call, later within your code, some third party component functions/classes which may depend on original Joomla defined constants ...
I've learned it hard way with ZOO Component and my ajax calls ... :)
Hope this makes some sense.

Based on where you want to show the results you can create a component or a module (you can also easily include the module in an article to print the result in component position).
In the case of module:
https://docs.joomla.org/J3.x:Creating_a_simple_module/Developing_a_Basic_Module
Obviously your ajax call will be in tmpl/default.php and your php script in the module folder, simply you have to include it in the file list in mod_helloworld.xml. Then zip your folder and install it using Module Manager.
In case of problem with your jquery code, you have to use the noConflict() mode.

Related

How can I download a PDF file from a form using UI designer?? Bonita

The thing is I have found how upload a document and after that downolad it. But I just want to download it. I want to do it using the UI designer but I dont know how to do it.
Thanks :)
I dont know which tool are you using to design your UI, anyway this is concerning functionality, not design. In that point, i need to know wich language do you want (or can) use. For example, in PHP, it's very simple, you can make something like:
(create php file) downloadpdf.php
1st: (if you want to generate pdf "on the fly":
<?php
function download($foo){
content headers (type, force-download, etc)
database select to get data or harcode it.
echo data
}
?>
and call this function with some id to select from database or something (ignore if you want to hardcode it)
Other option to download a file, if it's stored on server is making a link to this file (statically or dyamically). If you wanna take control to file downloads, check this post:
http://www.media-division.com/the-right-way-to-handle-file-downloads-in-php/
I don't mean that it can be done with UI designer tools, and it's not concerned if it's from a form or not.
Cheers!
You should create link and variable which type is javascript expression. On Variable value write
return "/bonita/portal/" + $data.context.mainDoc_ref.url;
On link URL write your variable and to text
Download: {{context.mainDoc_ref.fileName}}
Here you can find excellent example for this case

How and where to write Webform submit hook?

I am new to Drupal(7) and hence need some help for following situations.
I have created one Webform(I have other webform too) and now instead of inserting in default webform_submitted_data table, I want for this webfrom to insert into myTable. From what I found, I need to write a hook for this. Actually I am getting confused for way to write this hook. I have below questions.
Where to write this hook (in which file).
How to write this hook only for one webform.
Please help and let me know if you need any more information for this.
First, be very sure before you start twisting Drupal's arm into making things work differently then they are supposed to. Rerouting the data for a Webform could potentially provide hiccups in how Webform works and this could bite you later. It may still expect the data to be saved in its own bookkeeping tables, but fail to find it there later if you overwrite its behavior.
That being said, if you wish to alter the behavior of other modules, such as Webform, you will have to write your own, tiny module. Some of these hooks can also be influenced via the templating layer (using your templates template.php file), but this is the wrong place to alter this kind of behavior in my opinion.
A Drupal 7 module is basically comprised out of a minimal of two files, a *.info file and a *.module file. The former contains some meta data necessary for Drupal to categorize your module and calculate possible dependencies. The latter contains the actual PHP code.
These files have to be saved in a directory with (preferably) the same name as you named your info and module file. For Drupal to find your module, you can place it under sites/all/modules.
If, for example, you name your module changemyform, these are the minimal required files:
changemyform.info
changemyform.module
And both should reside in: sites/all/modules/changemyform.
I suggest you check Drupal's developer's manual for a more detailed explanation about writing modules, including licensing, unit testing, ... . But for this example, the mentioned two files will do.
In your info file you have to at least write the name for the module, a small description, for which core version it is applicable and which dependencies it has. Something like this would suffice for our example:
name = Change my form
description = Changes the submission behavior of my form.
core = 7.x
dependencies[] = webform
Next we should write the logic for the module file itself. The general hook for intercepting any form submission (including a Webform) is:
function mymodule_form_alter( &$form, &$form_state,$form_id ){
...
}
With this hook you can, as the name suggests, alter all the forms rendered with Drupal. Not only the submission handler, but add/remove fields, add markup, ... . Replace mymodule with the actual name of your module, in our example case changemyform. Next you need to scope it down to only effect your desired form:
function changemyform_form_alter( &$form, &$form_state,$form_id ){
if ($form_id == 'my_desired_webform_form_id') {
$form['#submit'][] = 'changemyform_submit_handler';
}
}
Notice that I now replace mymodule with changemyform. As you can also see I have added a custom handler to the form's submit property. You will have to write this handler as a function which then will contain all the logic you desire. Thus the total module file now becomes (minus the <?php ?> tags):
function changemyform_form_alter( &$form, &$form_state,$form_id ){
if ($form_id == 'my_desired_webform_form_id') {
$form['#submit'][] = 'changemyform_submit_handler';
}
}
function changemyform_submit_handler($form, &$form_state) {
... your submission logic ...
}
Now you are ready to write all the logic you need to capture the data on submit and do as you please.
Since this is a module, you should, of course, enable it in your administration modules overview screen for it to function.
Also (as a nitpick) when writing your own modules, do decorate each function with documentation headers that describe what each function does and what each parameter could hold. Even for tiny, trivial functions.
You can use hook_webform_submission_insert().
function MYMODULE_webform_submission_insert($node, $submission) {
// Insert a record into a 3rd-party module table when a submission is added.
db_insert('mymodule_table')
->fields(array(
'nid' => $node->nid,
'sid' => $submission->sid,
'foo' => 'foo_data',
))
->execute();
}
In Drupal 8, the best way to refer to entity_insert hook -
Write this hook in MODULE_NAME.module file of your module folder
function MODULE_NAME_entity_insert(Drupal\Core\Entity\EntityInterface $entity) {
if($entity->getEntityTypeId() == 'webform_submission') {
$result = \Drupal::database()->insert('TABLE_NAME')
->fields([
'nid' => $node->nid,
'sid' => $submission->sid,
'foo' => 'foo_data'
])
->execute();
}
}

Should I create a new controller in cakephp

I'm new in CakePhp but experienced in CodeIgniter. I created a controller in "WelcomeController.php" in controller directory and run the page. I got two errors
1. Error: The view for WelcomeController::index() was not found.
2. Error: Confirm you have created the file: C:\xampp\htdocs\myc\app\View\Welcome\index.ctp.
My question
Why I am getting this error even though I have supplied index() function?
In codeigniter we may not create a directory for a view. I don't want to create a directory "Welcome" in view. I there any provision provided?
In Cakephp you have to create view for function or here it called action. In your case, Create index.ctp on App->View->Welcome folder. This Getting Start
will give you a basic idea.
1) You're getting that error because your missing the view, not the controller function. To fix, do what the error suggested:
Confirm you have created the file: C:\xampp\htdocs\myc\app\View\Welcome\index.ctp.
2) "I don't want to create a directory "Welcome" in view. I there any provision provided?".
Not really... I mean, no if you want that action to have a correspondent view to put the content. Otherwise you can use $this->autoRender = false to not show anything... But that'll mean the url localhost/welcomes/index will be blank.
I recommend you read the basics as Fazal said. I know every framework can give us "quirks" and we end up expecting every other framework to work the same way we are used to, but try to adapt to the cake-way.
Btw, should be "WelcomesController", according to cake conventions
Be sure that you have all the files and directories necessary for the modal, controller and view to link up correctly i.e.: Create the folder called welcome(s) in your views directory with an index.ctp file. This should get rid of that error.
Check out this brilliant blog tutorial: Link

cakephp Managing Plugin Views how to determine paths

I have a plugin installed that has its own layout overrides for different controllers. However I'm having trouble understanding the mechanism for modifying the paths.
In the plug-in controller if I tell it to use my layout
$this->layout = 'default_dashboard';
Which is in app/Views/Layout and references an image in app/webroot/default_images.
All the relative links work fine to default_images when I do this, but would like to use some of the Plugin template overides for other actions.
However if I modify the default.cpt file to include some of the images, like say a logo that is used in default_dashboard.ctp. It is unable to map to the same image location.
For example in default.ctp:
echo $this->Html->image('default_images/logo.png',array('alt' =>
'Logo','width'=>'284','height'=>'82'));
produces a path to /img/default_images/logo.png. The Plugin is configured to use the /img location, whereas I want to direct to /default_images in this case. I could make this ../default_images/logo.png, but this isn't very clean.
In addition I have js and css which is having a similar problem. Can someone please explain the mechanism for using a site-wide default.ctp so that it works with inherited plugin templates?
From hard coding the links into the template not using the Html Helper, I see that the browser's relative path is confused because of the routing. For example the first one works with the root specified, the second doesn't.
<img src="/default_images/logo.png" alt="works" width='284' height='82'>
<img src="default_images/logo.png" alt="lost" width='284' height='82'>
What's the best way to make sure that the Plugin layouts and non-plugin layouts can all find the correct path to /default_images ?
Following are the steps that you can follow to resolve relative path problem:
Create a file abc_constants.php in app\Config folder.
Include the file in app\Config\bootstrap.php
require_once(abc_constants.php);
abc_constants.php should contain:
define('HTTP_HOST', "http://" . $_SERVER['HTTP_HOST'].'/');
define('SITE_URL', HTTP_HOST.'your_app_name/');
define('IMAGE_HTTP_PATH', SITE_URL.'app/webroot/default_images/');
Use these constants in your view file accordingly.
<?php echo $this->Html->image(IMAGE_HTTP_PATH.'logo.png',array('alt' => 'Logo','width'=>'284','height'=>'82'));
It looks a bit lengthy process at first time, but once implemented, you can use these constants in Ajax calls in view files, controller's code etc.

CakePHP - Include class from a directory outside the app directory

I am trying to include a miscellaneous library class from outside my app (it gets used by different apps).
My app is located at:
/var/www/websites/my_website/app/
And the class is located at:
/var/www/websites/libs/CakePHP/MyClass.php
In my bootstrap I'm struggling to figure out how to add the path for loading the classes from that directory:
App::build(array('Lib' => array('/var/www/websites/lib/')));
App::uses('MyClass', 'CakePHP');
$myClass = new MyClass();
Loading shouldn't be done in your bootstrap, but in your AppController's beforeFilter method instead.
Also, there is a reserved place for non-Cake libraries, being the app/Vendor directory. You can place all your classes in there and then load team easily with:
App::uses('MyClass', 'Vendor');
If it really needs to be in an alternative path, you need to specify and call the full path instead. And make sure to use the same names. Right now, you're specifying Lib, yet calling CakePHP as if that was a build by itself (which it's not). This won't work. It should look like this instead:
App::build(array('Lib' => array('/var/www/websites/lib')));
App::uses('MyClass', 'Lib/CakePHP'); // Define the subdirectory here
Also check the documentation on loading vendor files, it has quite some examples.

Resources