Changes between Version 2 and Version 3 of TracTicketsCustomFields


Ignore:
Timestamp:
Jan 25, 2009 10:55:11 AM (11 years ago)
Author:
trac
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • TracTicketsCustomFields

    v2 v3  
    22Trac supports adding custom, user-defined fields to the ticket module. Using custom fields, you can add typed, site-specific properties to tickets. 
    33 
    4 '''Note: In Trac 0.8, this feature is still experimental.''' 
    5  
    64== Configuration == 
    7 Configuring custom ticket fields is done in the TracIni config file. 
    8  
    9 All field definitions should be under a section named '''{{{[ticket-custom]}}}''' in the ini-file. 
     5Configuring custom ticket fields is done in the [wiki:TracIni trac.ini] file. All field definitions should be under a section named `[ticket-custom]`. 
    106 
    117The syntax of each field definition is: 
     
    2117   * label: Descriptive label. 
    2218   * value: Default value. 
    23    * order: Sort order placement. (Determines relative placement in forms.) 
     19   * order: Sort order placement. (Determines relative placement in forms with respect to other custom fields.) 
    2420 * '''checkbox''': A boolean value check box. 
    2521   * label: Descriptive label. 
     
    2723   * order: Sort order placement. 
    2824 * '''select''': Drop-down select box. Uses a list of values. 
     25   * label: Descriptive label. 
    2926   * options: List of values, separated by '''|''' (vertical pipe). 
    30    * value: Default value (Item #, starting at 0). 
     27   * value: Default value (one of the values from options). 
    3128   * order: Sort order placement. 
    3229 * '''radio''': Radio buttons. Essentially the same as '''select'''. 
    3330   * label: Descriptive label. 
    3431   * options: List of values, separated by '''|''' (vertical pipe). 
    35    * value: Default value (Item #, starting at 0). 
     32   * value: Default value (one of the values from options). 
    3633   * order: Sort order placement. 
    3734 * '''textarea''': Multi-line text area. 
     
    6057test_four.label = My selectbox 
    6158test_four.options = one|two|third option|four 
    62 test_four.value = 2 
     59test_four.value = two 
    6360 
    6461test_five = radio 
    6562test_five.label = Radio buttons are fun 
    6663test_five.options = uno|dos|tres|cuatro|cinco 
    67 test_five.value = 1 
     64test_five.value = dos 
    6865 
    6966test_six = textarea 
     
    7471}}} 
    7572 
    76 === Notes === 
    77 Do not insert space characters between '{{{|}}}'-separated option values for ''radio'' and ''select'' fields. 
    78  * ok: {{{FIELD_NAME.options = un|deux|trois}}} 
    79  * invalid: {{{FIELD_NAME.options = un | deux | trois}}} 
     73''Note: To make entering an option for a `select` type field optional, specify a leading `|` in the `fieldname.options` option.'' 
     74 
     75=== Reports Involving Custom Fields === 
     76 
     77Custom ticket fields are stored in the `ticket_custom` table, not in the `ticket` table. So to display the values from custom fields in a report, you will need a join on the 2 tables. Let's use an example with a custom ticket field called `progress`. 
     78 
     79{{{ 
     80#!sql 
     81SELECT p.value AS __color__, 
     82   id AS ticket, summary, owner, c.value AS progress 
     83  FROM ticket t, enum p, ticket_custom c 
     84  WHERE status IN ('assigned') AND t.id = c.ticket AND c.name = 'progress' 
     85AND p.name = t.priority AND p.type = 'priority' 
     86  ORDER BY p.value 
     87}}} 
     88'''Note''' that this will only show tickets that have progress set in them, which is '''not the same as showing all tickets'''. If you created this custom ticket field ''after'' you have already created some tickets, they will not have that field defined, and thus they will never show up on this ticket query. If you go back and modify those tickets, the field will be defined, and they will appear in the query. If that's all you want, you're set. 
     89 
     90However, if you want to show all ticket entries (with progress defined and without), you need to use a `JOIN` for every custom field that is in the query. 
     91{{{ 
     92#!sql 
     93SELECT p.value AS __color__, 
     94   id AS ticket, summary, component, version, milestone, severity, 
     95   (CASE status WHEN 'assigned' THEN owner||' *' ELSE owner END) AS owner, 
     96   time AS created, 
     97   changetime AS _changetime, description AS _description, 
     98   reporter AS _reporter, 
     99  (CASE WHEN c.value = '0' THEN 'None' ELSE c.value END) AS progress 
     100  FROM ticket t 
     101     LEFT OUTER JOIN ticket_custom c ON (t.id = c.ticket AND c.name = 'progress') 
     102     JOIN enum p ON p.name = t.priority AND p.type='priority' 
     103  WHERE status IN ('new', 'assigned', 'reopened') 
     104  ORDER BY p.value, milestone, severity, time 
     105}}} 
     106 
     107Note in particular the `LEFT OUTER JOIN` statement here. 
     108 
     109=== Updating the database === 
     110 
     111As noted above, any tickets created before a custom field has been defined will not have a value for that field. Here's a bit of SQL (tested with SQLite) that you can run directly on the Trac database to set an initial value for custom ticket fields. Inserts the default value of 'None' into a custom field called 'request_source' for all tickets that have no existing value: 
     112 
     113{{{ 
     114#!sql 
     115INSERT INTO ticket_custom 
     116   (ticket, name, value) 
     117   SELECT  
     118      id AS ticket, 
     119      'request_source' AS name, 
     120      'None' AS value 
     121   FROM ticket  
     122   WHERE id NOT IN ( 
     123      SELECT ticket FROM ticket_custom 
     124   ); 
     125}}} 
     126 
     127If you added multiple custom fields at different points in time, you should be more specific in the subquery on table {{{ticket}}} by adding the exact custom field name to the query: 
     128 
     129{{{ 
     130#!sql 
     131INSERT INTO ticket_custom 
     132   (ticket, name, value) 
     133   SELECT  
     134      id AS ticket, 
     135      'request_source' AS name, 
     136      'None' AS value 
     137   FROM ticket  
     138   WHERE id NOT IN ( 
     139      SELECT ticket FROM ticket_custom WHERE name = 'request_source' 
     140   ); 
     141}}} 
    80142 
    81143---- 
    82 See also: TracTickets, TracIni, TracIniReportCustomFieldSample 
     144See also: TracTickets, TracIni