Thank you to anyone who has already donated - your generous donations helped make three months of treatment possible.

My brother Nate continues to fight stage IV Hodgkin's lymphoma. He's just 31, with a wife and baby girl. They have no active income (since he's been unable to return to work), no insurance, and cannot afford the treatment he needs. Nate and his family need your help. Please consider a donation, every dollar helps. Thanks.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
<!--
Redmine Widget for Zendesk

Allows ticket creation then linking to a pre-defined Redmine project
Allows subject, tracker and priority to be customised before submission
The support ticket description is used as issue body
Redmine issue status and basic info is displayed
Redmine issue can be linked/unlinked with the support ticket

Assumptions:
All tickets posted to redmine under a single redmine user identity
Uses ticket.external_id to keep Redmine issue ID
Assumes tracker_id 1 is Defect, 2 is Feature
Assumes priority 2, 1, 3, 4 is Low, Normal, High, Critical
Should work with Redmine 1.1 according to docs, but only selfishly tested on 1.2.1.devel.7250


Usage Note:
If you don't care about how it works, just modify the Configuration section.

Assuming your redmine site is located here:  http://www.example.com/redmine/
redmine_domain: www.example.com
redmine_base_url: redmine
use_ssl: false

redmine_api_key: setup a low-privilege account in Redmine, and get the user's api key in user setup - issues will be created under this user

redmine_project_id: numerical ID to project

zendesk_domain: self-explanatory
-->


<!-- ### Configuration ### -->

<div	id="redmine-issue-tracking" 
	redmine_domain="example.domain.com" 
	redmine_base_url="redmine"
	redmine_api_key="-------------"
	redmine_project_id="1"
	zendesk_domain="my.zendesk.com"
	use_ssl="false"
>
    <h3 id="title"></h3>
    <div id="content"></div>
</div>

<!-- ## End Configuration ### -->




<script>
var redmine_resource;
var ticket_id = {{ticket.id}};
var redmine_id = "{{ticket.external_id}}";
//ticket_id = "2";
//redmine_id = "1488";

var redmine_domain = $("redmine-issue-tracking").readAttribute("redmine_domain");
var redmine_baseurl = $("redmine-issue-tracking").readAttribute("redmine_base_url") + "/";
var redmine_url = "http://" + redmine_domain + "/" + redmine_baseurl;
var redmine_project_id = $("redmine-issue-tracking").readAttribute("redmine_project_id") + "/";
var zendesk_domain = $("redmine-issue-tracking").readAttribute("zendesk_domain");
var redmine_api_key = $("redmine-issue-tracking").readAttribute("redmine_api_key"); 

login = function() {
  return  '<form onsubmit="try { redmine_resource.submit_credentials(this); } finally { return false; }" class="form">' +
          '<label>Username</label><input type="text" id="username"/>' +
          '<label>Password</label><input type="password" id="password"/>' +
          '<input type="submit" value="Login" id="submit">' +
          '</form>'
};

application = function() {
	form_html = "" +
	"<form id='redmine_form' onsubmit='try { do_submit_form(this); } finally { return false; }' class='form'>" +
		"<h4>Issue Info</h4>" +
		"Current ID:<br><input type='text' name='existing_redmine_id' value='" + redmine_id + "' style='width: 75px'> " +
		'<input type="submit" value="Link" id="link_button"> ' +
		"<input type='button' style='" + (redmine_id == "" ? "display: none" : "") + "' value='Unlink' " +
		"onclick='do_unlink_redmine()'><br><br>" +

		"<div id='redmine-content' style='padding: 2px;'></div>" +

        	"<input type='hidden' name='issue[project_id]' value='" + redmine_project_id + "'>" +
		"<input type='hidden' name='issue[description]'>" +

		/* Request parameter to pluck from form fields for POST*/
         	'<input type="hidden" name="pluck_param" value="issue">' +

         	'<input type="hidden" name="event_reference" value="Escalation">' +
         	'<input type="hidden" name="event_log" value="id">' +

         	/* REST resource to POST to */
         	'<input type="hidden" name="resource" value="' + redmine_baseurl + 'issues.xml">' +

         	/* Format for REST resource POST (json or xml) */
		'<input type="hidden" name="media_type" value="application/xml">' +

		"<h4>Create Issue</h4>" +

		"Type:<br><select name='issue[tracker_id]' style='width: 100%'>" +
		"<option value='1'>Defect</option><option value='2'>Feature</option></select><br><br>" +

		"Title:<br>" +
		"<textarea name='issue[subject]' value='' style='width: 185px'></textarea><br><br>" +

		"Priority:<br><select name='issue[priority_id]' style='width: 100%'>" +
		"<option value='2'>Low</option><option value='1' selected>Normal</option><option value='3'>High</option><option value='4'>Critical</option>" +
		"</select><br><br>" +

		'<input type="submit" value="Create" id="submit" name="action"><br/><br/>' +

         '</form>';

	ticket = null;
	get_ticket_info(ticket_id, ticket, false, false, false, function(ticket, requester, organisation, current_user) {
		var form = $("redmine_form");
		form["issue[subject]"].value = ticket.subject;
	});

	return form_html;
};

ticket_stats = function(response) {
	var issue = response.issue;
	var assigned_to = (issue.assigned_to == null ? "-" : issue.assigned_to.name);
	var status = (issue.status == null ? "-" : issue.status.name);
	var target_version = (issue.fixed_version == null ? "-" : issue.fixed_version.name);

	$("redmine-content").innerHTML = "<a href='" + redmine_url + "issues/" + response.issue.id + "' target='_blank'>" + 
	"<strong>#" + response.issue.id + "</strong></a> " + response.issue.subject + "<br/><br/>" +
	"<strong>Status</strong>: " + status + "<br/><br/>" +
	"<strong>Assigned To</strong>: " + assigned_to + "<br/><br/>" +
	"<strong>Target Version</strong>: " + target_version + "<br/><br/>" +
	"<strong>Created On</strong>: " + response.issue.created_on + "<br/>";
};

ticket_stats_error = function(response) {
	$("redmine-content").innerHTML = "<p>Cannot determine Redmine issue status - it may have been deleted. " +
		"<small>" +response.responseText + "</small>" +
		"</p>";
}

onform_success = function(response) {
	redmine_id = response.response.issue.id;
	do_link_redmine(redmine_id);
	return;
};



var application_resources;
if (redmine_id != "") {
	application_resources = [ 
	{ resource: redmine_baseurl + "issues/" + redmine_id + ".json", on_success: ticket_stats, on_failure: ticket_stats_error }
	];
}
else {
	application_resources = []; 
}

function do_submit_form(form) {
	creating = form["action"].value == "Create";
	var existing_redmine_id = form["existing_redmine_id"].value;
	if (!creating && existing_redmine_id != "") {
		do_link_redmine(existing_redmine_id);
	}
	else {
		var ticket, requester, organisation, current_user;
		get_ticket_info(ticket_id, ticket, requester, organisation, current_user, submit_to_redmine);
	}
}

function get_ticket_info(ticket_id, ticket, requester, organisation, current_user, fn_next_step) {
	if (ticket == null) {
		new Ajax.Request('/tickets/' + ticket_id + '.json', {
			method: "get",
			contentType: "application/json",
			evanJSON: true,
			onSuccess: function(transport) { get_ticket_info(ticket_id, transport.responseJSON, requester, organisation, current_user, fn_next_step); },
			onFailure: function(transport) { alert(transport.error); }
		});
		return;
	}
	else if (requester != false && requester == null && ticket.requester_id != null) {
		new Ajax.Request('/users/' + ticket.requester_id + '.json', {
			method: "get",
			contentType: "application/json",
			evanJSON: true,
			onSuccess: function(transport) { get_ticket_info(ticket_id, ticket, transport.responseJSON,organisation, current_user, fn_next_step); },
			onFailure: function(transport) { alert(transport.error); }
		});
		return;
	}
	else if (organisation != false && organisation == null && requester.organization_id != null) {
		new Ajax.Request('/organizations/' + requester.organization_id + '.json', {
			method: "get",
			contentType: "application/json",
			evanJSON: true,
			onSuccess: function(transport) { get_ticket_info(ticket_id, ticket, requester, transport.responseJSON, current_user, fn_next_step); },
			onFailure: function(transport) { alert(transport.error); }
		});
		return;	
	}
	else if (current_user != false && current_user == null) {
		new Ajax.Request('/users/current.json', {
			method: "get",
			contentType: "application/json",
			evanJSON: true,
			onSuccess: function(transport) { get_ticket_info(ticket_id, ticket, requester, organisation, 
				transport.responseJSON, fn_next_step); },
			onFailure: function(transport) { alert(transport.error); }
		});
		return;
	}

	if (fn_next_step != null) {
		fn_next_step(ticket, requester, organisation, current_user);
	}
}

function submit_to_redmine(ticket, requester, organisation, current_user) {
	var form = $("redmine_form");
	
	var requested_by = "*" + requester.name + "*" + (organisation == null ? "" : (" of *" + organisation.name + "*"));

	form["issue[description]"].value = "Issue created from " +
		"*\"Zendesk Ticket " + ticket.nice_id + "\":http://" + zendesk_domain + "/tickets/" + ticket.nice_id + "*" +
		" requested by " + requested_by + ", escalated by *" + current_user.name + "*\n\n" + 
		ticket.description;

	redmine_resource.submit_data(form); 
}

function do_link_redmine(new_redmine_id)
{
	new Ajax.Request('/tickets/' + ticket_id + '.xml?_method=put', {
		method: "put",
	    	contentType: 'application/xml',
		postBody: "<ticket><external_id>" + new_redmine_id + "</external_id></ticket>",
		asynchronous: true,
	    	onSuccess: function(transport) { 
			redmine_id = new_redmine_id;
			redmine_resource.options.application_resources = [ { 
				resource: redmine_baseurl + "issues/" + redmine_id + ".json", on_success: ticket_stats, on_failure: ticket_stats_error
			} ];
			redmine_resource.render(); 
		},
	    	onFailure: function(transport){ alert('Failed to update ticket with Redmine ID: ' + transport.responseText); }
 	});
}


function do_unlink_redmine()
{
	new Ajax.Request('/tickets/' + ticket_id + '.xml?_method=put', {
		method: "put",
	    	contentType: 'application/xml',
		postBody: "<ticket><external_id></external_id></ticket>",
		asynchronous: true,
	    	onSuccess: function(transport) { 
			redmine_id = ""; 
			redmine_resource.options.application_resources = [];
			$("redmine-content").innerHTML = "";
			redmine_resource.render(); 
		},
	    	onFailure: function(transport){ alert('Failed to unlink Redmine issue: ' + transport.responseText); }
 	});
}




















if(!ZendeskX) { var ZendeskX = {}; }
ZendeskX.resources = [];
ZendeskX.Resource = Class.create();

ZendeskX.Resource.prototype = {
  initialize : function(options) {
    this.options      = options || {};
    this.options.user = options.user || Cookie.get(this.options.anchor + '_user');
    this.options.pass = options.pass || Cookie.get(this.options.anchor + '_pass') ;
    this.content_anchor = $$('#' + this.options.anchor + ' #content')[0];
    this.title_anchor   = $$('#' + this.options.anchor + ' #title')[0];

     Ajax.Responders.register({ onException: function(r, e) { alert("Exception:\n\n" + e); }});

    if (this.options.title) {
      this.title_anchor.innerHTML = this.options.title;
    }

    this.render();
  },
  submit_credentials : function(form) {
    this.options.user = form.username.value;
    this.options.pass = form.password.value;
    if (this.options.user.blank() && this.options.pass.blank()) {
      alert('Please provide both a username and a password');
    } else {
      Cookie.set(this.options.anchor + '_user', this.options.user);
      Cookie.set(this.options.anchor + '_pass', this.options.pass);
      this.render();
    }
  },
  logout : function() {
    var options = this.options;
    Cookie.erase(options.anchor + '_user');
    Cookie.erase(options.anchor + '_pass');
    this.options.user = this.options.pass = null;
    this.render();
  },
  submit_data : function(form) {
    var control = this;
    var parameters = Form.serialize(form);
    parameters += "&domain=" + control.options.domain + "&use_ssl=" + control.options.use_ssl;
    if (ticket_id) {
      parameters += "&ticket_id=" + ticket_id;
    }
    if ( parameters.indexOf("media_type=") === 0 ) {
      var media_type = control.options.media_type || 'application/xml';
      parameters += "&media_type=" + media_type;
    }
    new Ajax.Request('/proxy.js', {asynchronous:true, evalScripts:true, parameters:parameters,
      requestHeaders: { Authorization: "Basic " + Base64.encode(control.options.user + ':' + control.options.pass) },
      onSuccess: function(transport) { 
		control.resource_write_success(transport, control); 
		if (control.options.form_success) control.options.form_success(transport.responseJSON); 
		enable_submit(form); 
	},
      onFailure: function(transport) { control.resource_failure(transport, control); enable_submit(form); }
    });
    disable_submit(form);
  },
  render : function() {
    var control = this;
    var options = control.options;
    if (options.login_content != null && !(options.user && options.pass)) {
      control.content_anchor.innerHTML = options.login_content();
    } else {
      control.content_anchor.innerHTML =  options.application_content();
      options.application_resources.each(function(resource){ control.request(resource); });
    }
  },
  request : function(options) {
    var control = this;
    if (options.resource == null) {
      if (options.on_success != null) {
        options.on_success();
      }
    }
    else {
      var media_type = options.media_type || 'application/json';
      new Ajax.Request('/proxy.js', {
        method: 'get',
        parameters: { domain: control.options.domain, use_ssl: control.options.use_ssl,
                      resource: options.resource, media_type: media_type, cache_gets: control.options.cache_gets},
        requestHeaders: { Authorization: "Basic " + Base64.encode(control.options.user + ':' + control.options.pass) },
        onSuccess: function(transport) { control.resource_read_success(transport, control, options); },
        onFailure: function(transport){ control.resource_failure(transport, control, options); }
      });
    }
  },
  resource_failure : function(transport, control, options) {
    if (transport.status == 401) {
      control.options.user = null; control.options.pass = null;
      Cookie.erase(control.options.anchor + '_user'); Cookie.erase(control.options.anchor + '_pass');
      if (control.content_anchor.innerHTML != control.options.login_content()) {
        alert("Username and password are not correct. Please re-enter.");
      }
      control.render();
    } else if (options != null && options.on_failure != null) {
	options.on_failure(transport);
    } else {
      alert(transport.responseJSON.error);
    }
  },
  //Called when data has been read
  resource_read_success : function(transport, control, options) {
    if(options != null && options.on_success != null) {
      options.on_success(transport.responseJSON);
    }
  },

  //Called when data has been written
  resource_write_success : function(transport, control) {
    var requestParams = transport.request && transport.request.parameters;

    var audit;
    if (requestParams && requestParams.event_reference && requestParams.event_log && !requestParams.ticket_id.blank()) {
      var resourceAttributes = control.getLoggableAttributes(control, transport.responseJSON.response, requestParams.event_log);
      resourceAttributes.location = transport.getHeader('location') || '';
      audit = control.buildAudit(requestParams, resourceAttributes);
    }

    if(audit != null) {
      new Ajax.Request('/events.json', {
        contentType: 'application/json',
        postBody: $H(audit).toJSON(),
        method: 'post', asynchronous:true,
        onSuccess: function(transport) { },
        onFailure: function(transport){ alert('Failed to register event: '+transport.responseJSON.error); }
      });
    }
  },

  buildAudit: function(requestParams, resourceAttributes) {
    return { ticket_id: requestParams.ticket_id,
             reference: requestParams.event_reference,
             via_id:    14,
             resource_attributes: resourceAttributes }
  },

  getLoggableAttributes: function(control, params, eventLog) {
    var keys = eventLog.split(',');
    var attributes = {};
    var attribute;

    _(keys).each(function(key) {
      key = key.strip();
      if (attribute = control._deepSearch(params, key)) {
        attributes[key] = attribute;
      }
    });

    return attributes;
  },

  _deepSearch: function(object, searchKey) {
    var found;

    if (found = object[searchKey] || object[searchKey.toLowerCase()]) {
      return found;
    } else {
      var value;

      for (key in object) {
        if (object.hasOwnProperty(key)) {
          value = object[key];
          if (typeof(value) === 'object') {
            if (found = this._deepSearch(value, searchKey)) {
              return found;
            }
          }
        }
      }
    }
  }

};



enable_submit = function(form) {
  form.submit.value = form.submit.getAttribute('originalValue');
  form.enable();
  form.reset();
}

disable_submit = function(form) {
  form.submit.setAttribute('originalValue', form.submit.value);
  form.disable();
  form.submit.value = 'Submitting...';
}





redmine_resource = new ZendeskX.Resource(    { 
	title: "Escalation",
	anchor: "redmine-issue-tracking",
	domain: redmine_domain,
	use_ssl: $("redmine-issue-tracking").readAttribute("use_ssl") || "false",
	user: redmine_api_key,
	application_content: application,
	form_success: onform_success,
	application_resources: application_resources,
});

</script>