PhpLabWare version 0.3 
/dd/ -> 0_0038_inc.php

1  <?php
2 
3 
4 
5 
6  $newtableid=$db->GenID("tableoftables_gen_id_seq");
7  $newtablename=protocols;
8  $newtablelabel="protocols";
9  unset($hownew);
10  while (get_cell($db,"tableoftables","id","tablename",$newtablename)) {
11     $newtablename.="n";
12     $hownew++;
13  }
14  for ($i=0;$i<$hownew;$i++)
15     $newtablelabel.="new";
16  $newtableshortname=substr($newtablename,0,3).$newtableid;
17  $newtable_realname=$newtablename."_".$newtableid;
18  $newtable_desc_name=$newtable_realname."_desc";
19  $r=$db->Execute("INSERT INTO tableoftables (id,sortkey,tablename,shortname,display,permission,custom,real_tablename,table_desc_name,label,plugin_code) VALUES ('$newtableid','0','$newtablename','$newtableshortname','Y','Users',NULL,'$newtable_realname','$newtable_desc_name','$newtablelabel','plugins/protocols_plugin.php')");
20  $rg=$db->Execute ("SELECT id FROM groups");
21  while (!$rg->EOF) {
22     $groupid=$rg->fields[0];
23     $db->Execute ("INSERT INTO groupxtable_display VALUES ($groupid,$newtableid)");
24     $rg->MoveNext();
25  }
26 
27  if ($r) {
28     $rb=$db->Execute("CREATE TABLE $newtable_desc_name (
29        id int NOT NULL,
30        sortkey int,
31        label text,
32        columnname text,
33        display_table char(1),
34        display_record char(1),
35        required char(1),
36        type text,
37        datatype text,
38        associated_table text,
39        associated_column text,
40        associated_local_key text,
41        thumb_x_size int,
42        thumb_y_size int,
43        link_first text,
44        link_last text,
45        modifiable char(1) )");
46     if ($rb) {
47        $newid=$db->GenID("newtable_desc_name"."_id");
48        $db->Execute("INSERT INTO $newtable_desc_name VALUES($newid,'100','id','id','N','N','N','int','text','','','','','','','','')");
49        $newid=$db->GenID("newtable_desc_name"."_id");
50        $db->Execute("INSERT INTO $newtable_desc_name VALUES($newid,'110','access','access','N','N','N','varchar(9)','text','','','','','','','','')");
51        $newid=$db->GenID("newtable_desc_name"."_id");
52        $db->Execute("INSERT INTO $newtable_desc_name VALUES($newid,'120','ownerid','ownerid','N','N','N','int','text','','','','','','','','')");
53        $newid=$db->GenID("newtable_desc_name"."_id");
54        $db->Execute("INSERT INTO $newtable_desc_name VALUES($newid,'130','magic','magic','N','N','N','int','text','','','','','','','','')");
55        $newid=$db->GenID("newtable_desc_name"."_id");
56        $db->Execute("INSERT INTO $newtable_desc_name VALUES($newid,'150','lastmoddate','lastmoddate','N','N','N','int','text','','','','','','','','')");
57        $newid=$db->GenID("newtable_desc_name"."_id");
58        $db->Execute("INSERT INTO $newtable_desc_name VALUES($newid,'160','lastmodby','lastmodby','N','N','N','int','text','','','','','','','','')");
59        $newid=$db->GenID("newtable_desc_name"."_id");
60        $db->Execute("INSERT INTO $newtable_desc_name VALUES($newid,'170','date','date','N','N','N','int','text','','','','','','','','')");
61        $newid=$db->GenID("newtable_desc_name"."_id");
62        $db->Execute("INSERT INTO $newtable_desc_name VALUES($newid,'500','Category','type1','Y','Y','N','int','pulldown','protocols_new_10042ass_20','','','','','','','Y')");
63        $ass_table1="ass$newtableid";
64        $id_ass=$db->GenId($ass_table1,20);
65        $ass_table1.="_$id_ass";
66        $db->Execute("CREATE TABLE $ass_table1 (
67           id int PRIMARY KEY,
68           sortkey int,
69           type text,
70           typeshort text)");
71        $db->Execute("UPDATE $newtable_desc_name SET associated_table='$ass_table1' WHERE id=$newid");
72        $newid=$db->GenID("newtable_desc_name"."_id");
73        $db->Execute("INSERT INTO $newtable_desc_name VALUES($newid,'200','Authors','type2','Y','Y','N','int','pulldown','protocols_new_10042ass_21','','','','','','','Y')");
74        $ass_table2="ass$newtableid";
75        $id_ass=$db->GenId($ass_table2,20);
76        $ass_table2.="_$id_ass";
77        $db->Execute("CREATE TABLE $ass_table2 (
78           id int PRIMARY KEY,
79           sortkey int,
80           type text,
81           typeshort text)");
82        $db->Execute("UPDATE $newtable_desc_name SET associated_table='$ass_table2' WHERE id=$newid");
83        $newid=$db->GenID("newtable_desc_name"."_id");
84        $db->Execute("INSERT INTO $newtable_desc_name VALUES($newid,'140','Title','title','Y','Y','Y','text','text','','','','','','','','Y')");
85        $newid=$db->GenID("newtable_desc_name"."_id");
86        $db->Execute("INSERT INTO $newtable_desc_name VALUES($newid,'400','Notes','notes','Y','Y','N','text','textlong','','','','','','','','Y')");
87        $newid=$db->GenID("newtable_desc_name"."_id");
88        $filecolumnid=$newid;
89        $db->Execute("INSERT INTO $newtable_desc_name VALUES($newid,'600','File','file','Y','Y','N','text','file','','','','','','','','Y')");
90        $filecolumnid=$newid;
91        // and finally create the table
92        $rc=$db->Execute(" CREATE TABLE $newtable_realname (
93           id int NOT NULL,
94           access varchar(9) ,
95           ownerid int ,
96           magic int ,
97           lastmoddate int ,
98           lastmodby int ,
99           date int ,
100           type1 int ,
101           type2 int ,
102           title text ,
103           notes text ,
104           file text ) ");
105 
106     }
107 
108 
109     // copy data from 'old' protocol table to the new one
110     unset($counter);
111     set_magic_quotes_runtime(1);
112     $tablesfk=get_cell($db,"tableoftables","id","real_tablename","protocols");
113     $rcb=$db->Execute("SELECT * FROM protocols");
114     while (!$rcb->EOF && $rcb) {
115        $newid=$db->Genid($newtable_realname."_id_seq");
116        $row=$rcb->fields;
117        $rcopy=$db->Execute("INSERT INTO $newtable_realname VALUES ('$newid','$row[access]','$row[ownerid]','$row[magic]','$row[lastmoddate]','$row[lastmodby]','$row[date]','$row[type1]','$row[type2]','$row[title]','$row[notes]','$row[file]')");
118        if (!$rcopy)
119           $failed=true;
120        else {
121           $counter++;
122           //change ownership of file to new table:
123           $rfile=$db->Execute("UPDATE files SET tablesfk='$newtableid',ftableid='$newid',ftablecolumnid='$filecolumnid' WHERE tablesfk='$tablesfk' AND ftableid='$row[id]'");
124           //adjust trusted users:
125           $db->Execute("UPDATE trust SET tableid='$newtableid',recordid='$newid' WHERE tableid=$tablesfk AND recordid='$row[id]'");
126        }
127        $rcb->MoveNext();
128     }
129     if ($counter)
130        echo "(protocols:) Inserted $counter records.<br>";
131     // Now copy supporting table (type1=author, type2=categories
132     $rcj=$db->Execute("SELECT * FROM pr_type1 ORDER BY id");
133     while ($rcj && !$rcj->EOF) {
134        $newid=$db->Genid($ass_table1."_id_seq");
135        $row=$rcj->fields;
136        $rcj_copy=$db->Execute("INSERT INTO $ass_table1 VALUES ('$newid','$row[sortkey]','$row[type]','$row[typeshort]')");
137        if (!$rcj_copy)
138           $failed=true;
139        // since the ids are not necessarily the same, we'll have to adjust the links
140        // in the main table
141        $db->Execute("UPDATE $newtable_realname SET type1='$newid' WHERE type1='$row[id]'");
142        $rcj->MoveNext();
143     }
144     $rcc=$db->Execute("SELECT * FROM pr_type2 ORDER BY id");
145     while ($rcc && !$rcc->EOF) {
146        $newid=$db->Genid($ass_table2."_id_seq");
147        $row=$rcc->fields;
148        $rcc_copy=$db->Execute("INSERT INTO $ass_table2 VALUES ('$newid','$row[sortkey]','$row[type]','$row[typeshort]')");
149        if (!$rcc_copy)
150           $failed=true;
151        $db->Execute("UPDATE $newtable_realname SET type2='$newid' WHERE type2='$row[id]'");
152        $rcc->MoveNext();
153     }
154     if ($failed)
155        echo "Failed copying contents of table protocols.<br>";
156     else {
157        //echo "Succes!<br>";
158        // delete the old tables
159        $rnt=$db->Execute("SELECT * FROM $newtable_realname");
160        if ($rnt->Numrows==$rcb->Numrows) {
161           $db->Execute("DROP TABLE protocols");
162           $db->Execute("DROP TABLE protocols_id_seq");
163           $db->Execute("DROP SEQUENCE protocols_id_seq");
164           $db->Execute("DROP TABLE pr_type1");
165           $db->Execute("DROP TABLE pr_type1_id_seq");
166           $db->Execute("DROP SEQUENCE pr_type1_id_seq");
167           $db->Execute("DROP TABLE pr_type2");
168           $db->Execute("DROP TABLE pr_type2_id_seq");
169           $db->Execute("DROP SEQUENCE pr_type2_id_seq");
170           $db->Execute("DELETE FROM tableoftables WHERE tablename='protocols'");
171           $db->Execute("UPDATE tableoftables SET label='protocols' WHERE id='$newtableid'");
172        }
173        else
174           echo "Problems copying the content of table protocols to the new table protocols.<br>";
175     }
176  }
177 
178 
179  ?>


Generated: Sun Oct 5 21:17:35 2003 SourceForge Logo Generated by PHPXref 0.2