I recently created a custom /download.php file in SugarCRM. There was nothing wrong with the original file, except our team had created a bunch of custom document modules, and we wanted to make sure that if any of the documents were requested, they could be downloaded, provided the user was authorized to download the file. Yes, that is a core file, and my bad for changing it, because a new Sugar update would destroy my work, but it serves to illustrate the problem for now. I thought some of the code, and quirks I ran into would be helpful for my SugarCRM readers. Let me walk you through the code and problems,
To help with debugging and error checking in the custom download.php code, I wrote a detailed message to the /sugarcrm.log, like so:
$GLOBALS['log']->ERROR("[" . basename(__FILE__) . ":" . __LINE__ . "] The current_user does not have access");
The PHP Magic constants __FILE__ and __LINE__ told me the file and line where the error occurred in the log, and the message helped me figure out why I got the error at that location. Just to clarify, those are double underlines between the words.
I followed the error message with a message on the front page of the application for the user to see, with this line:
SugarApplication::appendErrorMessage("This file is not available for download.");
The message appears on the user screen right below the header before the main content.
Right after the above call, I told Sugar to take me back to the page I requested, so the user was back where he started before requesting the file to be downloaded. That was done with a page redirect call, like so:
$page_url = $_SERVER['HTTP_REFERER']; SugarApplication::redirect($page_url);
This is where I started running into some problems. The problem was not in obtaining the $page_url, but in the value of the $page_url itself. It turned out that this worked fine on most of the standard, and some of the development team's custom modules, and you would get your originating page back up on the screen, but in some of the custom modules it didn't, we got a header with no content for the return page. Let me show you the problem.
Normally the $page_url would look something like this:
$page_url = "localhost/index.php?module=InternalDoc&action=DetailView&record=d6d86c3e-c09b-9d4c-03cc-555b54824ebe" This worked fine, instead on a couple of custom modules, we got this: $page_url = "localhost/index.php?action=ajaxui"
Some of you sharp SugarCRMers, know the problem is the custom module had AJAX enabled. With AJAX enabled the page redirect would not work properly. The solution is to turn off using AJAX for that module.
The reason AJAX is used is to speed up page loads and stop reloading pages when only a field is updated in the form, however, in most cases with smaller applications, the page loads quickly enough that reloading the page is not a problem, and AJAX can be turned off.
To turn off using AJAX with a module go to "Admin->Systems Settings" and in the right column is small print, you'll see "Configure AJAX User Interface Information." If you click on that link a new window opens that let's you drag and drop modules from "Enabled Modules" to "Disabled Modules."
Well I did that, but lo and behold, my module was not in the list, how do I turn off the AJAX, if my modules not in the list? My module was visible as a subpanel on my main screen, but when I went to "Admin->Display Modules and Subpanels" my module was not visible in either the "Navigation Bar" list or in the "Subpanel" list. Scratch my head.
How do we get the custom module to appear in both the Display modules list and in the AJAX list?
OK, go to "custom/Extension/application/Ext/Include" and look for your custom file in the list of files. If its not there, create a file with your modules name, as an example we'll use "InternalDoc.php" in that directory.
Open this new blank file, and put in the following content. Don't forget your opening php tag, not shown here.
$beanList['InternalDoc'] = 'InternalDoc'; $beanFiles['InternalDoc'] = 'modules/InternalDoc/InternalDoc.php'; //$modules_exempt_from_availability_check['doc_InternalDoc'] = 'doc_InternalDoc'; $report_include_modules['InternalDoc'] = 'InternalDoc'; //$modInvisList = 'InternalDoc'; //takes the module out of the no show list if it is there unset($modules_exempt_from_availability_check['InternalDoc']); unset($modInvisList['InternalDoc']); //add the module to modules list $moduleList = 'InternalDoc';
Let's review. Everywhere you see "InternalDoc" you should replace with your custom file name.
In the second paragraph, it is important to comment out the lines that are shown commented out. One of those makes the module invisible to the admin panel, make sure that's commented out. There is also a module no show list, we want to take our custom module off that list if its there with the "unset" command, and we want to make sure our custom module is on the $modulelist.
Once you have the contents set up, save the file, and do a rebuild.
Now, if you go to "Admin->Systems Settings->Configure AJAX User Interface Information" You'll find your custom module in the "Enabled" list, drag it to "Disabled" list, and save. Now, $page_url is correct, the AJAX is gone, and you will be brought back to your correct originating page.
All problems solved, have a good day :-).