This code snippet shows the sample of a Gadget shipped with 12d Synergy - it provides the list of all upcoming tasks for the current job.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Contracts;
using Contracts.Data;
using Contracts.Databases;
namespace SynergyServer.Gadgets.Tasks
{
// Gadget to show all upcoming tasks in the current job
public class JobUpcomingTasksGadget : SynergyServerScripting.Gadgets.Gadget
{
// constants for inputs
private const string INPUT_TASK_STATUS = "Task status";
private const string INPUT_RANGE = "Staring/ending date range";
private const string INPUT_MY_TASKS_ONLY = "Limit to my tasks only";
private const string INPUT_INCLUDE_SUB_JOBS = "Include sub jobs";
// always use the max height of the gadget
public override bool UseMaxHeight => true;
// Cosmetic properties of the gadget
public override string Name => "Upcoming Tasks - Current job";
public override string Description => "Show a list of upcoming tasks for the current job.";
public override string Category => "Tasks";
public JobUpcomingTasksGadget() : base(Guid.Parse("{F5A38B49-CD5F-470B-B676-28DB488BBB59}"))
{
}
// Generate the gadget
public override bool Generate(EntityID project_id, Dictionary<string, ScriptInput> inputs, EntityID user_id, IDatabase db, out GadgetData data)
{
data = new GadgetData();
DateTime start_time;
DateTime end_time;
// grab any date range inputs
if (!inputs.ContainsKey(INPUT_RANGE)) return false;
((PrimitiveScriptInput)inputs[INPUT_RANGE]).GetDateRange(out start_time, out end_time);
if (!inputs.ContainsKey(INPUT_INCLUDE_SUB_JOBS)) return false;
// Get our Job information
var project_info = SynergyServerScripting.Entities.Project.GetProjectInfo(project_id, false, db);
bool include_sub_jobs = ((PrimitiveScriptInput) inputs[INPUT_INCLUDE_SUB_JOBS]).type_value == "Yes";
// Build up a search query
SearchQuery query = new SearchQuery();
query.AddDomainValue(AttributeSearchContext.TaskAttribute, "Project", new QueryValueOperation(new ProjectQueryValue(project_info), AttributeMatchOperation.EqualTo));
query.AddDomainValue(AttributeSearchContext.TaskAttribute, "IncludeSubProjects", new QueryValueOperation(new PrimitiveQueryValue(include_sub_jobs), AttributeMatchOperation.EqualTo));
query.AddDomainValue(AttributeSearchContext.TaskAttribute, "IncludeNonMatchingSubTasks", new QueryValueOperation(new PrimitiveQueryValue(false), AttributeMatchOperation.EqualTo));
query.AddDomainValue(AttributeSearchContext.TaskAttribute, "IsClosed", new QueryValueOperation(new PrimitiveQueryValue(false), AttributeMatchOperation.EqualTo));
// Are we filtering by date on our start date, or our due date?
if (((ListScriptInput) inputs[INPUT_TASK_STATUS]).current_value == "Starting tasks")
{
query.AddDomainValue(AttributeSearchContext.TaskAttribute, "StartDate", new QueryValueOperation(new DateRangeQueryValue(start_time, end_time), AttributeMatchOperation.EqualTo));
}
else
{
query.AddDomainValue(AttributeSearchContext.TaskAttribute, "DueDate", new QueryValueOperation(new DateRangeQueryValue(start_time, end_time), AttributeMatchOperation.EqualTo));
}
// Are we looking for my tasks only?
if (((PrimitiveScriptInput) inputs[INPUT_MY_TASKS_ONLY]).type_value == "Yes")
{
var contact = SynergyServerScripting.Entities.Contact.GetContactInfo(user_id, false, db);
query.AddDomainValue(AttributeSearchContext.TaskAttribute, "Assignee", new QueryValueOperation(new ContactQueryValue(contact), AttributeMatchOperation.EqualTo));
}
// Perform the task search
List<TaskItem> tasks = SynergyServerScripting.Entities.TaskList.SearchTasks(query, user_id, db);
// Use the results to build the table
var table = new GadgetTable();
table.AddColumn("Task Name", "TaskName");
table.AddColumn("Assignee", "Assignee");
table.AddColumn("Start Date", "StartDate");
table.AddColumn("Due Date", "DueDate");
foreach (TaskItem task in tasks)
{
var row = table.NewRow();
row.Colour = System.Drawing.Color.FromArgb(task.colour);
row[table.GetColumnIndexByVariable("TaskName")] = task.name;
row[table.GetColumnIndexByVariable("Assignee")] = task.assigned_entity == null ? "" : task.assigned_entity.DisplayName;
row[table.GetColumnIndexByVariable("StartDate")] = task.start_date_utc == DateTime.MaxValue ? "Unknown" : task.start_date_utc.ToLocalTime().ToString("dd/MM/yyyy");
row[table.GetColumnIndexByVariable("DueDate")] = task.due_date_utc == DateTime.MaxValue ? "Unknown" : task.due_date_utc.ToLocalTime().ToString("dd/MM/yyyy");
table.AddRow(row);
}
table.Width = DefaultWidth;
table.DefaultHeight = DefaultHeight;
data.width = DefaultWidth;
data.height = DefaultHeight;
data.html = table.ToString();
return true;
}
// Get all our inputs
protected override List<ScriptInput> GetScriptInputs(IDatabase db)
{
return new List<ScriptInput>
{
new ListScriptInput(INPUT_TASK_STATUS, "Show starting tasks or ending tasks.", false, new[] {"Staring tasks", "Ending tasks"}) {current_value = "Starting tasks"},
new PrimitiveScriptInput(INPUT_RANGE, "Filter tasks by starting/ending date range.", false, PrimitiveScriptInputType.DateRange) {type_value = "-0D;+2W"},
new PrimitiveScriptInput(INPUT_MY_TASKS_ONLY, "Whether or not to show my tasks only.", false, PrimitiveScriptInputType.YesNo) {type_value = "Yes"},
new PrimitiveScriptInput(INPUT_INCLUDE_SUB_JOBS, "Whether or not to search tasks in the sub jobs.", false, PrimitiveScriptInputType.YesNo) {type_value = "Yes"}
};
}
}
}
This language is not supported or no code example is available.