Changes between Version 3 and Version 4 of TracTicketsCustomFields


Ignore:
Timestamp:
Jan 28, 2011, 2:52:13 PM (14 years ago)
Author:
trac
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • TracTicketsCustomFields

    v3 v4  
    11= Custom Ticket Fields =
    2 Trac support adding custom, user-defined, fields to the ticket module. Using custom fields, you can add typed, site-specific, properties to tickets.
     2Trac 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 
    6 == Configuriation ==
    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.
     4== Configuration ==
     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:
     
    1511 ...
    1612}}}
    17 Looking at the example below should help explain the syntax.
     13The example below should help to explain the syntax.
    1814
    1915=== Available Field Types and Options ===
     
    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.)
     20   * format: Either `plain` for plain text or `wiki` to interpret the content as WikiFormatting. (''since 0.11.3'')
    2421 * '''checkbox''': A boolean value check box.
    2522   * label: Descriptive label.
     
    2724   * order: Sort order placement.
    2825 * '''select''': Drop-down select box. Uses a list of values.
     26   * label: Descriptive label.
    2927   * options: List of values, separated by '''|''' (vertical pipe).
    30    * value: Default value (Item #, starting at 0).
     28   * value: Default value (one of the values from options).
    3129   * order: Sort order placement.
    3230 * '''radio''': Radio buttons. Essentially the same as '''select'''.
    3331   * label: Descriptive label.
    3432   * options: List of values, separated by '''|''' (vertical pipe).
    35    * value: Default value (Item #, starting at 0).
     33   * value: Default value (one of the values from options).
    3634   * order: Sort order placement.
    3735 * '''textarea''': Multi-line text area.
    3836   * label: Descriptive label.
    3937   * value: Default text.
    40    * width: Width in columns.
    41    * height: Height in lines.
     38   * cols: Width in columns.
     39   * rows: Height in lines.
    4240   * order: Sort order placement.
     41   * format: Either `plain` for plain text or `wiki` to interpret the content as WikiFormatting. (''since 0.11.3'')
    4342
    4443=== Sample Config ===
    4544{{{
    4645[ticket-custom]
     46
    4747test_one = text
    4848test_one.label = Just a text box
     
    5050test_two = text
    5151test_two.label = Another text-box
    52 test_two.value = Just a default value
     52test_two.value = Default [mailto:joe@nospam.com owner]
     53test_two.format = wiki
    5354
    5455test_three = checkbox
     
    5960test_four.label = My selectbox
    6061test_four.options = one|two|third option|four
    61 test_four.value = 2
     62test_four.value = two
    6263
    6364test_five = radio
    6465test_five.label = Radio buttons are fun
    6566test_five.options = uno|dos|tres|cuatro|cinco
    66 test_five.value = 1
     67test_five.value = dos
    6768
    6869test_six = textarea
    6970test_six.label = This is a large textarea
    7071test_six.value = Default text
    71 test_six.width = 60
    72 test_six.height = 30
     72test_six.cols = 60
     73test_six.rows = 30
     74}}}
     75
     76''Note: To make entering an option for a `select` type field optional, specify a leading `|` in the `fieldname.options` option.''
     77
     78=== Reports Involving Custom Fields ===
     79
     80Custom 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`.
     81
     82{{{
     83#!sql
     84SELECT p.value AS __color__,
     85   id AS ticket, summary, owner, c.value AS progress
     86  FROM ticket t, enum p, ticket_custom c
     87  WHERE status IN ('assigned') AND t.id = c.ticket AND c.name = 'progress'
     88AND p.name = t.priority AND p.type = 'priority'
     89  ORDER BY p.value
     90}}}
     91'''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.
     92
     93However, 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.
     94{{{
     95#!sql
     96SELECT p.value AS __color__,
     97   id AS ticket, summary, component, version, milestone, severity,
     98   (CASE status WHEN 'assigned' THEN owner||' *' ELSE owner END) AS owner,
     99   time AS created,
     100   changetime AS _changetime, description AS _description,
     101   reporter AS _reporter,
     102  (CASE WHEN c.value = '0' THEN 'None' ELSE c.value END) AS progress
     103  FROM ticket t
     104     LEFT OUTER JOIN ticket_custom c ON (t.id = c.ticket AND c.name = 'progress')
     105     JOIN enum p ON p.name = t.priority AND p.type='priority'
     106  WHERE status IN ('new', 'assigned', 'reopened')
     107  ORDER BY p.value, milestone, severity, time
     108}}}
     109
     110Note in particular the `LEFT OUTER JOIN` statement here.
     111
     112=== Updating the database ===
     113
     114As 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:
     115
     116{{{
     117#!sql
     118INSERT INTO ticket_custom
     119   (ticket, name, value)
     120   SELECT
     121      id AS ticket,
     122      'request_source' AS name,
     123      'None' AS value
     124   FROM ticket
     125   WHERE id NOT IN (
     126      SELECT ticket FROM ticket_custom
     127   );
     128}}}
     129
     130If 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:
     131
     132{{{
     133#!sql
     134INSERT INTO ticket_custom
     135   (ticket, name, value)
     136   SELECT
     137      id AS ticket,
     138      'request_source' AS name,
     139      'None' AS value
     140   FROM ticket
     141   WHERE id NOT IN (
     142      SELECT ticket FROM ticket_custom WHERE name = 'request_source'
     143   );
    73144}}}
    74145
Back to Top