SugarCRM Print Address Labels

Do you like this?

This is a continuation of the articles "SugarCRM Adding Custom Button For Printing Labels Using Javascript" and "SugarCRM How To Add Custom Actions". This time, the custom code will help download an address file which can be opened in MS Word for further editing before printing.


To access earlier articles, please click the following links:

Suppose we want to add the print address button into the DetailView of the Accounts module. The following screenshot shows you the end result:
SugarCRM Adding Custom Button Using Javascript 1

Edit the file /custom/modules/Accounts/metadata/detailviewdefs.php
SugarCRM Print Address Labels 1

                'customCode' => '        
    <input type="button" value="Print Label" onclick="printLabel(\'{$}\')">'
Also in the same file, add this line of code :
SugarCRM Print Address Labels 2

Create a new Javascript file named printlabel.js
SugarCRM Print Address Labels 3

function printLabel(content) {'/index.php?module=Accounts&action=printLabel&id='+content,'Print Address','width=500,height=300');

Add a new file controller.php under the folder /custom/modules/Accounts
SugarCRM Print Address Labels 4


class AccountsController extends SugarController {
	function action_printLabel(){
		$id = $_REQUEST['id'];
		global $db;
		$query = "SELECT * from Accounts where id='" . $id . "'";
		$rs = $db->query($query);
		$rtf= file_get_contents('C:\\SourceCode\\sugarcrm\\custom\\modules\\Accounts\\label.rtf');
		while($row = $db->fetchByAssoc($rs))
			$rtf = str_replace('[name]', $row['name'] , $rtf);
			$shipping_address_street= str_replace(",", ",\par", $row['shipping_address_street']);
			$rtf = str_replace('[shipping_address_street]', $shipping_address_street , $rtf);
			$rtf = str_replace('[shipping_address_city]', $row['shipping_address_city'], $rtf);
			$rtf = str_replace('[shipping_address_state]', $row['shipping_address_state'], $rtf);
			$rtf = str_replace('[shipping_address_postalcode]', $row['shipping_address_postalcode'], $rtf);
			$rtf = str_replace('[shipping_address_country]', $row['shipping_address_country'], $rtf);
			$rtf = str_replace('[phone_office]', $row['phone_office'], $rtf);
		header('Pragma: public'); 	// required
		header('Expires: 0');		// no cache
		header('Content-type: text/rtf');
		header("Content-Disposition: attachment;Filename=address.rtf");
		echo $rtf;



comments powered by Disqus