File: triggers.inc.php

package info (click to toggle)
ibwebadmin 0.98-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,916 kB
  • ctags: 1,950
  • sloc: php: 12,454; makefile: 7
file content (305 lines) | stat: -rw-r--r-- 9,776 bytes parent folder | download | duplicates (2)
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
<?php
// File           triggers.inc.php / ibWebAdmin
// Purpose        functions working with triggerss, included from accessories.php
// Author         Lutz Brueckner <irie@gmx.de>
// Copyright      (c) 2000, 2001, 2002, 2003, 2004 by Lutz Brueckner,
//                published under the terms of the GNU General Public Licence v.2,
//                see file LICENCE for details
// Created        <03/01/19 15:52:24 lb>
//
// $Id: triggers.inc.php,v 1.15 2004/05/30 16:41:03 lbrueckner Exp $


//
// get the properties for all defined triggers 
//
function &get_triggers(&$oldtriggers) {
    global $dbhandle, $HTTP_POST_VARS;

    $lsql = 'SELECT RDB$TRIGGER_NAME AS NAME,'
                 .' RDB$RELATION_NAME AS TNAME,'
                 .' RDB$TRIGGER_SEQUENCE AS POS,'
                 .' RDB$TRIGGER_TYPE AS TTYPE,'
                 .' RDB$TRIGGER_SOURCE AS TSOURCE,'
                 .' RDB$TRIGGER_INACTIVE AS STATUS'
            .' FROM RDB$TRIGGERS'
           .' WHERE (RDB$SYSTEM_FLAG IS NULL  OR  RDB$SYSTEM_FLAG=0)'
             .' AND RDB$TRIGGER_NAME NOT IN (SELECT RDB$TRIGGER_NAME FROM RDB$CHECK_CONSTRAINTS)'
           .' ORDER BY RDB$TRIGGER_NAME';
    $res = ibase_query($dbhandle, $lsql) or ib_error(__FILE__, __LINE__, $lsql);

    $triggers = array();
    while ($obj = ibase_fetch_object($res)) {
        $name = trim($obj->NAME);
        $display = (isset($oldtriggers[$name])) ? $oldtriggers[$name]['display'] : 'close';

        // get the source code for the open triggers 
        $tsource = '';
        if ((isset($oldtriggers[$name]) &&  $display == 'open')
        ||  isset($HTTP_POST_VARS['acc_trigger_mod'])) {

            $bid = ibase_blob_open($obj->TSOURCE);
            $arr = ibase_blob_info($obj->TSOURCE);

            // $arr[0] holds the blob length
            $tsource = ibase_blob_get($bid, $arr[0]);
            ibase_blob_close($bid);

            // discard the 'AS ' from the source-string
            $tsource = substr(trim($tsource), 3);
        }

        $triggers[$name] = array('table'   => trim($obj->TNAME),
                                 'type'    => get_trigger_type($obj->TTYPE),
                                 'pos'     => $obj->POS,
                                 'status'  => get_trigger_status($obj->STATUS),
                                 'source'  => $tsource,
                                 'display' => $display);
    }

    return $triggers;
}


//
// create trigger from the definitions in $triggerdefs
//
function create_trigger($triggerdefs) {
    global $s_login, $isql, $binary_output, $binary_error;

    $isql = trigger_create_source($triggerdefs);

    if (DEBUG) add_debug('isql', __FILE__, __LINE__);

    // this must be done by isql because 'create trigger' is not supported from within php
    list($binary_output, $binary_error) = isql_execute($isql, $s_login['user'], $s_login['password'], $s_login['database'], $s_login['host']);

    return ($binary_error != ''  ||  count($binary_output) > 0) ? FALSE : TRUE;
}


function trigger_create_source($triggerdefs) {

    $isql  = "SET TERM !! ;\n"
            .'CREATE TRIGGER '.$triggerdefs['name'].' FOR '.$triggerdefs['table']
            .' '.$triggerdefs['status'].' '.$triggerdefs['type'];
    if ($triggerdefs['pos'] != 0) {
        $isql .= ' POSITION '.$triggerdefs['pos'];
    }
    
    $isql .= " AS\n".$triggerdefs['source']."\n"
            ."SET TERM ; !!\n";

    return $isql;
}


function modify_trigger($name, $triggerdefs) {
    global $s_login, $isql, $binary_output, $binary_error;

    $isql = 'DROP TRIGGER '.$name.";\n"
            .trigger_create_source($triggerdefs);

    if (DEBUG) add_debug('isql', __FILE__, __LINE__);

    list($binary_output, $binary_error) = isql_execute($isql, $s_login['user'], $s_login['password'], $s_login['database'], $s_login['host']);

    return ($binary_error != ''  ||  count($binary_output) > 0) ? FALSE : TRUE;
}


//
// drop the trigger $name off the database
//
function drop_trigger($name) {
    global $s_triggers, $dbhandle, $ib_error;
    global $lsql;

    $lsql = 'DROP TRIGGER '.$name;
    if (DEBUG) add_debug('lsql', __FILE__, __LINE__);
    if (!@ibase_query($dbhandle, $lsql)) {
        $ib_error = ibase_errmsg();
    }
    else {
        unset($s_triggers[$name]);
    }
}


//
// deliver the html for an opened view on the views panel
//
function &get_opened_trigger($name, &$trigger, $url) {
    global $dbhandle, $red_triangle_icon, $acc_strings, $ptitle_strings;

    $html = <<<EOT
        <nobr>
          <a href="$url"><img src="$red_triangle_icon" alt="${ptitle_strings['Close']}" title="${ptitle_strings['Close']}" border="0" hspace="7"></a><b>$name</b>
        </nobr>
        <nobr>
        <table cellpadding="0" cellspacing="0">
          <tr>
            <td width="26">
            </td>
            <td>
              <table border cellpadding="3" cellspacing="0">
                <tr>
                  <th>${acc_strings['Table']}</th>
                  <th>${acc_strings['Type']}</th>
                  <th>${acc_strings['Pos']}</th>
                  <th>${acc_strings['Status']}</th>
                  <th>${acc_strings['Source']}</th>
                </tr>
                <tr>
	          <td valign="top">&nbsp;${trigger['table']}</td>
	          <td valign="top">&nbsp;${trigger['type']}</td>
	          <td valign="top">&nbsp;${trigger['pos']}</td>
	          <td valign="top">&nbsp;${trigger['status']}</td>
	          <td valign="top"><pre>${trigger['source']}</pre></td>
                </tr>
              </table>
            </td>
          </tr>
        </table>
      </nobr>

EOT;

    return $html;
}


//
// return the definition sourcecode for a trigger
//
function get_trigger_source($name) {
    global $dbhandle;

    $tsource = '';
    $lsql = 'SELECT RDB$TRIGGER_SOURCE AS TSOURCE'
            .' FROM RDB$TRIGGERS'
           ." WHERE RDB\$TRIGGER_NAME='".$name."'";
    $res = ibase_query($dbhandle, $lsql) or ib_error(__FILE__, __LINE__, $lsql);
    $obj = @ibase_fetch_object($res);

    if (is_object($obj)) {
        $bid = ibase_blob_open($obj->TSOURCE);
        $arr = ibase_blob_info($obj->TSOURCE);

        // $arr[0] holds the blob length
        $tsource = trim(ibase_blob_get($bid, $arr[0]));
        ibase_blob_close($bid);

        // discard the 'AS ' from the source-string
        $tsource = substr($tsource, 3);
    }
    ibase_free_result($res);

    return $tsource;
}


//
// return the string equivalent for the trigger-status $int
//
function get_trigger_status($int) {

    if ($int == 0) {
        return 'Active';
    } elseif ($int == 1) {
        return 'Inactive';
    }
    die('Error: get_trigger_status() bad parameter');
}


//
// return the string equivalent for the trigger-type $int
//
function get_trigger_type($int) {
    global $trigger_types;

    return $trigger_types[$int];
}


//
// outputs a html-table with a form to define/modify a trigger 
//
// Variables:    $title     headline-string for the table
function trigger_definition($title) {
    global $s_triggerdefs, $trigger_types, $acc_strings, $s_cust;

    $trigger_source = htmlspecialchars($s_triggerdefs['source']);

?>
<table border cellpadding="3" cellspacing="0">
  <tr>
    <th colspan="5" align="left"><?php echo $title; ?></th>
  </tr>
  <tr>
    <td><b><?php echo $acc_strings['Name']; ?></b><br>
        <input type="text" size="20" maxlength="31" name="def_trigger_name" value="<?php if (isset($s_triggerdefs['name']))  echo $s_triggerdefs['name']; ?>">
    </td>
    <td><b><?php echo $acc_strings['Table']; ?></b><br>
        <select name="def_trigger_table">
          <?php build_table_options($s_triggerdefs['table'], array('noviews', 'owner')); ?>
      </select>
    </td>
    <td><b><?php echo $acc_strings['Type']; ?></b><br>
      <?php echo get_selectlist('def_trigger_type', $trigger_types, $s_triggerdefs['type'], FALSE); ?>
    </td>
    <td align="center"><b><?php echo $acc_strings['Position']; ?></b><br>
        <input type="text" size="2" maxlength="2" name="def_trigger_pos" value="<?php if (isset($s_triggerdefs['pos']))  echo $s_triggerdefs['pos']; ?>">
    </td>
    <td><b><?php echo $acc_strings['Status']; ?></b><br>
      <select name="def_trigger_status">
         <option<?php if ($s_triggerdefs['status'] == 'Active') echo ' selected'; ?>> Active
         <option<?php if ($s_triggerdefs['status'] == 'Inactive') echo ' selected'; ?>> Inactive
      </select>
    </td>
  </tr>
  <tr>
    <td colspan="5">
      <b><?php echo $acc_strings['Source']; ?></b><br>
      <textarea name="def_trigger_source" rows="<?php echo $s_cust['textarea']['rows']; ?>" cols="<?php echo $s_cust['textarea']['cols']; ?>" wrap="virtual"><?php echo $trigger_source; ?></textarea>
    </td>
  </tr>
</table>
<?php

}


//
// save the form vars we got from trigger_definition()
//
function save_triggerdefs() {
    global $s_triggerdefs, $HTTP_POST_VARS;

    $s_triggerdefs['name']   = strtoupper(get_request_data('def_trigger_name'));
    $s_triggerdefs['table']  = $HTTP_POST_VARS['def_trigger_table'];
    $s_triggerdefs['type']   = $HTTP_POST_VARS['def_trigger_type'];
    $s_triggerdefs['pos']    = $HTTP_POST_VARS['def_trigger_pos'];
    $s_triggerdefs['status'] = $HTTP_POST_VARS['def_trigger_status'];
    $s_triggerdefs['source'] = get_request_data('def_trigger_source');
}


//
// mark all triggers as opened or closed in $s_triggers
//
function &toggle_all_triggers(&$triggers, $status) {

    foreach (array_keys($triggers) as $name) {
        $triggers[$name]['display'] = $status;

        if ($status == 'open'  &&  empty($triggers[$name]['source'])) {
            $triggers[$name]['source'] = get_trigger_source($name);
        }
    }

    return $triggers;
}

?>