Monday 16 January 2012

Sugarcrm custom chart

Creating a sugar chart dashlet is done the same way as you would create a simple dashlet, just extending 2 functions, for this reason I will not cover the way a sugar dashlet is created. Notes: The dashlets will be named  "CustomLineDashlet" and "CustomPieDashlet" and the module is "abc_Sample". You can use the predefined sugar chart definitions for parsing data (modules\Charts\chartdefs.php) or you can create your own to meet any custom demands. This is done by editing(creating if dose not exist) "custom\Charts\chartDefs.ext.php". For CustomPieDashlet : custom\Charts\chartDefs.ext.php:
$chartDefs['custom_pie_chart'] = array('type' => 'code',
 'id' => 'custom_pie_chart',
 'label' => 'custom_pie_chart label',
 'chartUnits' => 'The unit definition',
 'chartType' => 'pie chart',
 // important value that will be used to group the data
 'groupBy' => array( 'name' ), 
 'base_url'=> 
  array(  'module' => 'abc_Sample',
   'action' => 'index',
   'query' => 'true',
   'searchFormTab' => 'advanced_search',
   )   
);
custom\modules\abc_Sample\Dashlets\CustomPieDashlet\CustomPieDashlet.php
....
public function display() {
 $currency_symbol = $GLOBALS['sugar_config']['default_currency_symbol'];
 require ("modules/Charts/chartdefs.php");
 // here we load the chart definition (custom\Charts\chartDefs.ext.php)
 $chartDef = $chartDefs['custom_pie_chart'];
 require_once ('include/SugarCharts/SugarChart.php');
 $sugarChart = new SugarChart();
 $sugarChart -> setProperties('', translate('LBL_OPP_SIZE', 'Charts') . ' ' . currency_symbol . '1' . translate('LBL_OPP_THOUSANDS', 'Charts'), $chartDef['chartType']);
 $sugarChart -> base_url = $chartDef['base_url'];
 $sugarChart -> group_by = $chartDef['groupBy'];
 $sugarChart -> url_params = array();
 $sugarChart -> getData($this -> constructQuery());
 $xmlFile = $sugarChart -> getXMLFileName($this -> id);
 $sugarChart -> saveXMLFile($xmlFile, $sugarChart -> generateXML());
 return $this -> getTitle('
') . '
' . $sugarChart -> display($this -> id, $xmlFile, '100%', '480', false) . '
'; } protected function constructQuery() { // this is the query used to get data and create the xml // for pie chart we have a simple query $query = " SELECT count(*) as total, name FROM table GROUP BY name ORDER BY name"; return $query; }
For CustomLineDashlet : custom\Charts\chartDefs.ext.php:
$chartDefs['custom_line_chart'] = array(  'type' => 'code',
 'id' => 'custom_line_chart',
 'label' => 'custom_line_chart label',
 'chartUnits' => 'The unit definition',
 'chartType' => 'line chart',
 // the legend will be the name and the x intervals will be the date
 'groupBy' => array( 'date','name' ), 
 'base_url'=> 
  array(  'module' => 'sam_Sample',
   'action' => 'index',
   'query' => 'true',
   'searchFormTab' => 'advanced_search'
 )   
);
custom\modules\abc_Sample\Dashlets\CustomLineDashlet\CustomLineDashlet.php
....
public function display() {
 $currency_symbol = $GLOBALS['sugar_config']['default_currency_symbol'];
 require ("modules/Charts/chartdefs.php");
 // here we load the chart definition (custom\Charts\chartDefs.ext.php)
 $chartDef = $chartDefs['custom_line_chart'];
 require_once ('include/SugarCharts/SugarChart.php');
 $sugarChart = new SugarChart();
 $sugarChart -> setProperties('', translate('LBL_OPP_SIZE', 'Charts') . ' ' . currency_symbol . '1' . translate('LBL_OPP_THOUSANDS', 'Charts'), $chartDef['chartType']);
 $sugarChart -> base_url = $chartDef['base_url'];
 $sugarChart -> group_by = $chartDef['groupBy'];
 $sugarChart -> url_params = array();
 $sugarChart -> getData($this -> constructQuery());
 $xmlFile = $sugarChart -> getXMLFileName($this -> id);
 $sugarChart -> saveXMLFile($xmlFile, $sugarChart -> generateXML());
 return $this -> getTitle('
') . '
' . $sugarChart -> display($this -> id, $xmlFile, '100%', '480', false) . '
'; } protected function constructQuery() { // this is the query used to get data and create the xml // for pie chart we have a simple query $query = " SELECT count(*) as total, name, date FROM table GROUP BY date,name ORDER BY name"; return $query; }
the meta files ( custom\modules\abc_Sample\Dashlets\CustomLineDashlet\CustomPieDashlet.meta.php and custom\modules\abc_Sample\Dashlets\CustomLineDashlet\CustomLineDashlet.meta.php) will look like this:
$dashletMeta['CustomSamplePieCart'] = array('module'  => 'abc_Sample',
            'title'       => 'Custom Pie Chart', 
                                          'description' => 'Custom description',
                                          'icon'        => 'themes/default/images/icon_sam_Sample_32.gif',
                                          'category'    => 'Charts');
and
$dashletMeta['CustomSampleLineCart'] = array('module'  => 'abc_Sample',
            'title'       => 'Custom Chart', 
                                          'description' => 'Custom description',
                                          'icon'        => 'themes/default/images/icon_sam_Sample_32.gif',
                                          'category'    => 'Charts');


Notes:
custom\Charts\chartDefs.ext.php is the place where you will define your chart type, important fields:
'groupBy' => array( 'date','name' ) (the x and legend fields)
'chartType' => 'line chart', (the swf name, php will capitalize and add .swf, so it will be lineChar.swf)

in the sql your x values will have to coincide (be the same) for all lines

Sunday 1 January 2012

Gotomeeting php api(oauth) create meting

This is from https://developer.citrixonline.com/forum/create-meeting-error , the code from bcantoni:
Create a METING:


 <;∧php

// sample GoToMeeting API call: create meeting
// docs: https://developer.citrixonline.com/api/GoToMeeting%2520REST%2520API/Create%2520Meeting-150

// assume have valid $access_token from OAuth flow
$access_token = 'xyz';

$url = "https://api.citrixonline.com/G2M/rest/meetings";
$headers = array (
    "Accept: application/json",
    "Content-Type: application/json",
    "Authorization: OAuth oauth_token=$access_token"
);
$data = array ('test meeting',
    'starttime' => '2012-02-01T08:00:00',
    'endtime' => '2012-02-01T09:00:00',
    'timezonekey' => '67', // Pacific time
    'meetingtype' => 'Scheduled',
    'passwordrequired' => 'false',
    'conferencecallinfo' => 'Hybrid' // normal PSTN + VOIP options
);
$data_json = json_encode ($data);
 
$ch = curl_init();
curl_setopt_array ($ch, array (
    CURLOPT_URL => $url,
    CURLOPT_HEADER => false,
    CURLOPT_FOLLOWLOCATION => false,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_TIMEOUT => 10,
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $data_json,
));
 
$results = curl_exec ($ch);
$info = curl_getinfo ($ch);
curl_close ($ch);
 
print "data sent: $data_json\n";
//print "headers sent: " . print_r($headers,1) . "\n";
//print "curl info: " . print_r($info,1) . "\n";
print "data returned: " . print_r($results,1 ) . "\n";
//print "data returned decoded: " . print_r(json_decode($results),1) . "\n";

And the response:

$ php post.php
 
data sent: {"subject":"test meeting","starttime":"2012-02-01T08:00:00","endtime":"2012-02-01T09:00:00","timezonekey":"67","meetingtype":"Scheduled","passwordrequired":"false","conferencecallinfo":"Hybrid"}
 
data returned: {"joinURL":"https:\/\/www3.gotomeeting.com\/join\/123456789","maxParticipants":26,"uniqueMeetingId":2000007959645,"meetingid":123456789}
EDIT:


thaks for the response, but I still get the same error with your code :). Here is the output from the php:

data sent:
Array
(
    [0] => test meeting
    [starttime] => 2012-02-01T08:00:00
    [endtime] => 2012-02-01T09:00:00
    [timezonekey] => 67
    [meetingtype] => Scheduled
    [passwordrequired] => false
    [conferencecallinfo] => Hybrid
)

data returned:
{"int_err_code":"Server.userException","msg":"java.lang.NumberFormatException: Invalid date/time"}curl info:
Array
(
    [url] => https://api.citrixonline.com/G2M/rest/meetings
    [content_type] => text/xml; charset=utf-8
    [http_code] => 404
    [header_size] => 224
    [request_size] => 483
    [filetime] => -1
    [ssl_verify_result] => 20
    [redirect_count] => 0
    [total_time] => 2.797
    [namelookup_time] => 0
    [connect_time] => 0
    [pretransfer_time] => 0.438
    [size_upload] => 712
    [size_download] => 98
    [speed_download] => 35
    [speed_upload] => 254
    [download_content_length] => 98
    [upload_content_length] => 712
    [starttransfer_time] => 2.438
    [redirect_time] => 0
    [certinfo] => Array
        (
        )

)


And from fidler:

POST https://api.citrixonline.com/G2M/rest/meetings HTTP/1.1
Host: api.citrixonline.com
Accept: application/json
Authorization: OAuth oauth_token=1c8187be36bb86997db59e5772268d5a
Content-Length: 712
Expect: 100-continue
Content-Type: application/json; boundary=----------------------------9530b5163dde

------------------------------9530b5163dde
Content-Disposition: form-data; name="starttime"

2012-02-01T08:00:00
------------------------------9530b5163dde
Content-Disposition: form-data; name="endtime"

2012-02-01T09:00:00
------------------------------9530b5163dde
Content-Disposition: form-data; name="timezonekey"

67
------------------------------9530b5163dde
Content-Disposition: form-data; name="meetingtype"

Scheduled
------------------------------9530b5163dde
Content-Disposition: form-data; name="passwordrequired"

false
------------------------------9530b5163dde
Content-Disposition: form-data; name="conferencecallinfo"

Hybrid
------------------------------9530b5163dde--



and the response:


HTTP/1.1 404 Not Found
Date: Sun, 01 Jan 2012 10:57:14 GMT
Server: Apache
Content-Length: 98
Content-Type: text/xml; charset=utf-8
Cneonction: close
Vary: Accept-Encoding
Connection: close

{"int_err_code":"Server.userException","msg":"java.lang.NumberFormatException: Invalid date/time"}