]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - service/example/example.html
Release 6.4.0
[Github/sugarcrm.git] / service / example / example.html
1 <!--
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  * 
23  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  * 
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37 -->
38 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
39 <html lang='en'>
40         <head>
41                 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
42                 <title>Sugar Rest Example</title>
43 <!-- Dependency -->
44
45 <!--CSS file (default YUI Sam Skin) -->
46 <link type="text/css" rel="stylesheet" href="http://yui.yahooapis.com/2.6.0/build/datatable/assets/skins/sam/datatable.css">
47
48 <!-- Dependencies -->
49
50 <script src="http://yui.yahooapis.com/2.6.0/build/yahoo/yahoo-min.js"></script>
51
52 <!-- Used for Custom Events and event listener bindings -->
53 <script src="http://yui.yahooapis.com/2.6.0/build/event/event-min.js"></script>
54
55 <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
56 <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/element/element-beta-min.js"></script>
57 <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/datasource/datasource-min.js"></script>
58
59 <!-- OPTIONAL: JSON Utility (for DataSource) -->
60 <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/json/json-min.js"></script>
61
62 <!-- OPTIONAL: Connection Manager (enables XHR for DataSource) -->
63 <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/connection/connection-min.js"></script>
64
65 <!-- OPTIONAL: Get Utility (enables dynamic script nodes for DataSource) -->
66 <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/get/get-min.js"></script>
67
68 <!-- OPTIONAL: Drag Drop (enables resizeable or reorderable columns) -->
69 <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/dragdrop/dragdrop-min.js"></script>
70
71 <!-- OPTIONAL: Calendar (enables calendar editors) -->
72 <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/calendar/calendar-min.js"></script>
73
74 <!-- Source files -->
75 <script type="text/javascript" src="http://yui.yahooapis.com/2.6.0/build/datatable/datatable-min.js"></script>
76
77 <script>
78
79 var SugarRest = function(){}
80 SugarRest.proxy_url  = 'Rest_Proxy.php';
81 SugarRest.server_url = 'http://localhost/[Sugar Path]/service/v2/rest.php'
82 SugarRest.leadFields = [ 'id','do_not_call', 'first_name', 'last_name', 'status', 'phone_work', 'lead_source', 'salutation', 'primary_address_country', 'primary_address_city','primary_address_state', 'primary_address_postalcode', 'department', 'title', 'account_name'];
83 SugarRest.moduleFields = {};
84 SugarRest.logResponse = function(o){
85         data = YAHOO.lang.JSON.parse(o.responseText);
86         //console.log(data);
87         return data;
88 }
89
90
91 SugarRest.call = function(fun, args, callback, params){
92         //console.log(args);
93         query = SugarRest.getQuery(fun, args);
94         YAHOO.util.Connect.asyncRequest('POST', SugarRest.proxy_url  , {success:callback, failure:callback, argument:params}, query);
95 }
96
97 SugarRest.getQuery = function(fun, args){
98         query = 'method=' + fun + '&input_type=json&response_type=json';
99         if(args != null){
100                 m = YAHOO.lang.JSON.stringify(args);
101                 query += '&rest_data=' + m;
102         }
103         return query;
104 }
105
106 SugarRest.getServerInfo =  function(){
107         //console.log('Getting Server Info');
108         SugarRest.call('get_server_info', '', SugarRest.test);
109 }
110
111 SugarRest.login =  function(name, password, application){
112         //console.log(name);
113         SugarRest.call('md5', password, SugarRest.performLogin, {name:name ,application:application});
114
115 }
116
117 SugarRest.performLogin = function(o){
118
119         //console.log('Logging In');
120         var loginData = [{
121                 user_name: o.argument.name,
122                 password:YAHOO.lang.JSON.parse(o.responseText)
123         },o.argument.application];
124         SugarRest.call('login', loginData, SugarRest.getUserId);
125 }
126
127 SugarRest.getUserId =  function(o){
128         data  = YAHOO.lang.JSON.parse(o.responseText);
129         SugarRest.session = data.id;
130         SugarRest.call('get_user_id', SugarRest.session, SugarRest.setUserId);
131 }
132
133 SugarRest.setUserId = function(o){
134         SugarRest.user_id =YAHOO.lang.JSON.parse(o.responseText);
135         SugarRest.getModuleFields('Leads', SugarRest.leadFields);
136 }
137 SugarRest.getModuleFields = function(module, fields){
138         SugarRest.call('get_module_fields', [SugarRest.session, module, fields], SugarRest.setModuleFields);
139 }
140
141 SugarRest.setModuleFields = function(o){
142         data  = SugarRest.logResponse(o);
143         console.log(data.module_fields);
144         SugarRest.moduleFields[data.module_name] = data.module_fields;
145         SugarRest.InlineCellEditing();
146 }
147 SugarRest.getLeadsQuery = function(){
148         var data = [SugarRest.session, 'Leads', " leads.do_not_call = 0 AND leads.status != 'Converted' AND leads.status != 'Dead' AND leads.assigned_user_id = '" + SugarRest.user_id + "' ", '', 0, SugarRest.leadFields, [{
149                 'name': 'email_addresses',
150                 'value': ['id', 'email_address', 'opt_out', 'primary_address',]
151         }], 500, 0];
152         q =  SugarRest.getQuery('get_entry_list', data, SugarRest.test);
153         console.log(q);
154         return q;
155 }
156
157 SugarRest.getFeeds = function(){
158         var data = [SugarRest.session, 'SugarFeed', "", '', 0, ['id', 'name', 'description', 'link_url', 'link_type', 'created_by', 'date_entered', 'related_id', 'related_module'], [], 500, 0];
159         SugarRest.call('get_entry_list', data, SugarRest.test);
160
161 }
162
163
164 SugarRest.getLeads = function(){
165         q = SugarRest.getLeadsQuery();
166          var myCallback = function() {
167             this.set("sortedBy", null);
168             this.onDataReturnAppendRows.apply(this,arguments);
169         };
170      SugarRest.myDataSource.sendRequest(q,
171                 {
172             success : myCallback,
173             failure : myCallback,
174             scope : SugarRest.myDataTable
175      });
176 }
177
178 SugarRest.saveChange = function(callback, newValue){
179         var r = this.getRecord();
180         var column = this.getColumn();
181         var id = r._oData['name_value_list.id'];
182         name = column.key.replace('name_value_list.', '');
183     name = name.replace('.value','');
184     
185         if(name == 'do_not_call'){
186                 newValue = (newValue == 'Do Not Call')? 1: 0;
187         }
188         //console.log("New Value:" + newValue);
189         if(name == 'status' && newValue == 'Converted'){
190                 SugarRest.window = window.open('');
191                 callback();
192                 SugarRest.seamless_login_url = 'module=Leads&action=ConvertLead&record=' + id;
193                 SugarRest.call('seamless_login', SugarRest.session, SugarRest.seamless, 'module=Leads&action=ConvertLead&record=' + id );
194                 return;
195         }
196         fields = {};
197         fields['id'] = id;
198         fields[name] = newValue;
199
200         data = [SugarRest.session, 'Leads',fields ];
201         SugarRest.call('set_entry', data, SugarRest.savedChanges, {
202                 callback: callback,
203                 newValue: newValue
204         });
205
206 }
207
208 SugarRest.savedChanges = function(o){
209         //console.log(o);
210         callback = o.argument.callback;
211          var r = YAHOO.lang.JSON.parse(o.responseText);
212      if (r.id) {
213          callback(true, o.argument.newValue);
214      } else {
215         //console.log('save failed');
216         callback();
217      }
218
219 }
220
221 SugarRest.editRecord = function(module, id){
222         query = 'module=' + module +'&record=' + id + '&action=EditView';
223         SugarRest.seamless_login_url = query;
224         SugarRest.call('seamless_login', SugarRest.session, SugarRest.seamless, query);
225
226 }
227
228 SugarRest.seamless = function(o){
229         if (o.responseText == 1) {
230                 surl = SugarRest.server_url +'/index.php?' + SugarRest.seamless_login_url + '&MSID=' +SugarRest.session;
231                 //console.log('opening:' + surl);
232                 SugarRest.window.location.href = surl;
233         }
234
235 }
236
237
238 SugarRest.buildColumnDefs = function(){
239
240
241 }
242
243
244
245  SugarRest.InlineCellEditing = function(){
246         var statusOptions = [];
247         for(i in SugarRest.moduleFields.Leads.status.options){
248                 statusOptions[statusOptions.length] = {'label': SugarRest.moduleFields.Leads.status.options[i].name, 'value':SugarRest.moduleFields.Leads.status.options[i].value};
249         }
250         var salutationOptions = [];
251         for(i in SugarRest.moduleFields.Leads.salutation.options){
252                 salutationOptions[salutationOptions.length] = {'label': SugarRest.moduleFields.Leads.salutation.options[i].name, 'value':SugarRest.moduleFields.Leads.salutation.options[i].value};
253         }
254          SugarRest.myColumnDefs = [
255                         {key:"name_value_list.id",formatter:SugarRest.editLink, label:'Edit'},
256            // {key:"name_value_list.salutation.value",sortable:true, label:SugarRest.moduleFields.Leads.salutation.label, editor: new YAHOO.widget.DropdownCellEditor({asyncSubmitter:SugarRest.saveChange, dropdownOptions:salutationOptions})},
257                         {key:"name_value_list.first_name.value",sortable:true, label:SugarRest.moduleFields.Leads.first_name.label, editor: new YAHOO.widget.TextboxCellEditor({asyncSubmitter:SugarRest.saveChange})},
258                         {key:"name_value_list.last_name.value",sortable:true, label:SugarRest.moduleFields.Leads.last_name.label ,editor: new YAHOO.widget.TextboxCellEditor({asyncSubmitter:SugarRest.saveChange})},
259             {key:"name_value_list.phone_work.value", formatter:SugarRest.callLink, label:SugarRest.moduleFields.Leads.phone_work.label},
260                         {key:"name_value_list.status.value",sortable:true, label:SugarRest.moduleFields.Leads.status.label, editor: new YAHOO.widget.DropdownCellEditor({asyncSubmitter:SugarRest.saveChange, dropdownOptions:statusOptions})},
261                         {key:"name_value_list.account_name.value",sortable:true, label:SugarRest.moduleFields.Leads.account_name.label, editor: new YAHOO.widget.TextboxCellEditor({asyncSubmitter:SugarRest.saveChange})},
262                         //{key:"name_value_list.department.value",sortable:true, label:SugarRest.moduleFields.Leads.department.label, editor: new YAHOO.widget.TextboxCellEditor({asyncSubmitter:SugarRest.saveChange})},
263                         {key:"name_value_list.title.value",sortable:true, label:SugarRest.moduleFields.Leads.title.label, editor: new YAHOO.widget.TextboxCellEditor({asyncSubmitter:SugarRest.saveChange})},
264                         {key:"name_value_list.primary_address_city.value",sortable:true, label:'City', editor: new YAHOO.widget.TextboxCellEditor({asyncSubmitter:SugarRest.saveChange})},
265                         {key:"name_value_list.primary_address_state.value",sortable:true, label:'State', editor: new YAHOO.widget.TextboxCellEditor({asyncSubmitter:SugarRest.saveChange})},
266                         //{key:"name_value_list.primary_address_country.value",sortable:true, label:'Country', editor: new YAHOO.widget.TextboxCellEditor({asyncSubmitter:SugarRest.saveChange})},
267                         {key:"name_value_list.primary_address_postalcode.value",sortable:true, label:'Postal', editor: new YAHOO.widget.TextboxCellEditor({asyncSubmitter:SugarRest.saveChange})},
268                         //{key:"name_value_list.lead_source.value", label:SugarRest.moduleFields.Leads.lead_source.label},
269                         {key:"name_value_list.do_not_call.value",formatter:SugarRest.checkboxField, label:SugarRest.moduleFields.Leads.do_not_call.label, editor: new YAHOO.widget.DropdownCellEditor({asyncSubmitter:SugarRest.saveChange, dropdownOptions:['Call', 'Do Not Call']})},
270         ];
271
272   SugarRest.myDataSource = new YAHOO.util.DataSource(SugarRest.proxy_url );
273   SugarRest.myDataSource.connMethodPost = true;
274   SugarRest.myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
275   SugarRest.myDataSource.connXhrMode = "queueRequests";
276
277   SugarRest.myDataSource.responseSchema = {
278     resultsList : "entry_list", // String pointer to result data
279   };
280  SugarRest.myDataSource.responseSchema['fields'] = SugarRest.myColumnDefs;
281
282
283
284   SugarRest.myDataTable = new YAHOO.widget.DataTable("cellediting",   SugarRest.myColumnDefs,
285
286           SugarRest.myDataSource, {initialRequest:SugarRest.getLeadsQuery()});
287  // Set up editing flow
288                 var highlightEditableCell = function(oArgs) {
289                     var elCell = oArgs.target;
290                     if(YAHOO.util.Dom.hasClass(elCell, "yui-dt-editable")) {
291                         this.highlightCell(elCell);
292                     }
293                 };
294                 SugarRest.myDataTable.subscribe("cellMouseoverEvent", highlightEditableCell);
295                 SugarRest.myDataTable.subscribe("cellMouseoutEvent", SugarRest.myDataTable.onEventUnhighlightCell);
296                 SugarRest.myDataTable.subscribe("cellClickEvent", SugarRest.myDataTable.onEventShowCellEditor);
297
298  }
299         // Custom formatter for "address" column to preserve line breaks
300   SugarRest.formatAddress = function(elCell, oRecord, oColumn, oData) {
301             elCell.innerHTML = "<pre class=\"address\">" + oData + "</pre>";
302   };
303
304    SugarRest.editLink = function(elCell, oRecord, oColumn, oData) {
305             elCell.innerHTML = "<input type='button' onclick='SugarRest.window = window.open(); SugarRest.editRecord(\"Leads\", \"" + oData + "\");' value='Edit'>";
306   };
307
308    SugarRest.callLink = function(elCell, oRecord, oColumn, oData) {
309             elCell.innerHTML = "<a href='callto://" + oData+ "'>"+ oData+ "</a>";
310   };
311
312    SugarRest.checkboxField = function(elCell, oRecord, oColumn, oData) {
313                         checked = (oData == '1')? ' CHECKED ': '';
314                         elCell.innerHTML = "<input type='checkbox' " + checked + ">";
315   };
316
317
318
319
320
321
322
323
324 </script>
325         </head>
326         <body class="yui-skin-sam">
327                 <div id="dialog1">
328                 <div class="hd">Please Login <span id="error"></span></div>
329                 <div class="bd">
330                                 <label for="username">User Name:</label><input id='username' type="text" name="username" value="will"/>
331                                 <label for="password">Password:</label><input id='password' type="password" name="password" value="will"/>
332                                 <input type="button" value="Login" onclick='SugarRest.login(document.getElementById("username").value,document.getElementById("password").value , "Sugar Rest Demo")'>
333                 </div></div>
334
335
336                 <div id="cellediting"></div>
337         </body>
338 </html>