php8
[web.mtrack] / MTrack / Ticket_CustomField.php
1 <?php
2
3 require_once 'MTrack/Config.php';
4 class MTrackTicket_CustomField {
5   var $name;
6   var $type;
7   var $label;
8   var $group;
9   var $order = 0;
10   var $default;
11   var $options;
12
13   static function canonName($name) {
14     if (!preg_match("/^x_/", $name)) {
15       $name = "x_$name";
16     }
17     return $name;
18   }
19
20   /** load the field definition from the configuration file */
21   static function load($name) {
22     if (!preg_match("/^x_[a-z_]+$/", $name)) {
23       throw new Exception("invalid field name $name");
24     }
25
26     $field = new MTrackTicket_CustomField;
27     $field->name = $name;
28
29     $field->type  = MTrackConfig::get('ticket.custom', "$name.type");
30     $field->label = MTrackConfig::get('ticket.custom', "$name.label");
31     $field->group = MTrackConfig::get('ticket.custom', "$name.group");
32     $field->order   = (int)MTrackConfig::get('ticket.custom', "$name.order");
33     $field->default = MTrackConfig::get('ticket.custom', "$name.default");
34     $field->options = MTrackConfig::get('ticket.custom', "$name.options");
35
36     return $field;
37   }
38
39   function save() {
40     if (!preg_match("/^x_[a-z_]+$/", $this->name)) {
41       throw new Exception("invalid field name $this->name");
42     }
43     $name = $this->name;
44     MTrackConfig::set('ticket.custom', "$name.type", $this->type);
45     MTrackConfig::set('ticket.custom', "$name.label", $this->label);
46     MTrackConfig::set('ticket.custom', "$name.group", $this->group);
47     MTrackConfig::set('ticket.custom', "$name.order", (int)$this->order);
48     MTrackConfig::set('ticket.custom', "$name.default", $this->default);
49     MTrackConfig::set('ticket.custom', "$name.options", $this->options);
50   }
51
52   function ticketData() {
53     /* compatible with the $FIELDSET data used in web/ticket.php */
54     $data = array(
55       'label' => $this->label,
56       'type' => $this->type,
57     );
58
59     if (strlen($this->default)) {
60       $data['default'] = $this->default;
61     }
62
63     switch ($this->type) {
64       case 'multi':
65       case 'wiki':
66       case 'shortwiki':
67         $data['ownrow'] = true;
68         $data['rows'] = 5;
69         $data['cols'] = 78;
70         break;
71       case 'select':
72       case 'multiselect':
73         $options = array('' => ' --- ');
74         foreach (explode('|', $this->options) as $opt) {
75           $options[$opt] = $opt;
76         }
77         $data['options'] = $options;
78         break;
79     }
80     return $data;
81   }
82 }