This manual is deprecated. Please visit https://groupoffice.readthedocs.io for the latest documentation.

Difference between revisions of "Exporting to PDF or CSV"

From Group-Office Groupware and CRM Documentation
Jump to: navigation, search
(New page: Group-Office has an easy interface to create exports to CSV or PDF. We'll look at the addressbook module for an example: It starts with saving the SQL query in the session. We do this in ...)
(No difference)

Revision as of 07:53, 17 May 2010

Group-Office has an easy interface to create exports to CSV or PDF. We'll look at the addressbook module for an example:

It starts with saving the SQL query in the session. We do this in modules/addressbook/classes/addressbook.class.inc.php where we search for contacts:

In the function search_contacts we build an SQL query like:

$sql = "SELECT * FROM ab_contacts";

We save this query like this:

$_SESSION['GO_SESSION']['export_queries']['search_contacts']=array(
				'query'=>$sql, //the SQL query
				'method'=>'format_contact_record', //a public static method that will format each record.
				'class'=>'addressbook',//The class to use for the method
				'require'=>__FILE__); //The file to include so the class is available

You can also set a row body column that will be displayed after each row with a column spanning the whole table: 'pdf_row_body_column'=>'preview_text'

And you may define some extra columns that are not in your grid: 'extra_columns'=>array(array('header'=>'Preview', 'column'=>'preview_text')),


The method to format a record looks like this:

public static function format_contact_record(&$record, $cf=false) {
		$record['name'] = String::format_name($record['last_name'], $record['first_name'], $record['middle_name']);
		$record['ctime']=Date::get_timestamp($record['ctime']);
		$record['mtime']=Date::get_timestamp($record['mtime']);


		if(!isset($GLOBALS['now']))
			$GLOBALS['now']=time();

		$record['age']='';

		if($record['birthday']!='0000-00-00'){
			$btime = strtotime($record['birthday']);
			$age = date('Y')-date('Y', $btime);

			$month = date('n');
			$bmonth = date('n', $btime);

			if($month<$bmonth || ($month==$bmonth && date('j')<date('j', $btime))) {
				$age--;
			}
			$record['age']=$age;
		}

		$record['birthday'] = Date::format($record['birthday'], false);

		if($cf)
			$cf->format_record($record, 2, true);
	}

Now the server knows how to export a query called "search_contacts". Now all we need to do is add a button to execute the export query in modules/addressboo/MainPanel.js:

{
			iconCls: 'btn-export', 
			text: GO.lang.cmdExport, 
			cls: 'x-btn-text-icon', 
			handler:function(){
				var activetab = this.tabPanel.getActiveTab();
				var config = {};
				switch(activetab.id)
				{
					case 'ab-contacts-grid':
						config.query='search_contacts';
						config.colModel = this.contactsGrid.getColumnModel();
						
					break;
					case 'ab-company-grid':					
						config.query='search_companies';
						config.colModel = this.companiesGrid.getColumnModel();
					break;				
				}	
				
				
				config.title = activetab.title;
				var query = this.searchPanel.queryField.getValue();
				if(!GO.util.empty(query))
				{
					config.subtitle= GO.lang.searchQuery+': '+query;
				}else
				{
					config.subtile='';
				}
				
				if(!this.exportDialog)
				{
					this.exportDialog = new GO.ExportQueryDialog({
							query:config.query
						});
				}			
				this.exportDialog.show(config);

			},  
			scope: this		
		}

The show function of the export dialog takes the following config properties:

title, subtitle and colModel. Where colModel is a column model of a standard gridpanel.

Now you're done and you can export your grid data!


More control

You can create custom exports per installation as described here: Custom exports.

But you can also create a custom export class in your module if you need some more control: