Subversion Repositories php_library

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 rodolico 1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"
2
>
3
<html>
4
  <head>
5
    <title>library.php Manual</title>
6
    <meta name="GENERATOR" content="Quanta Plus">
7
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
8
  </head>
9
  <body>
10
 
11
    <h3>
12
       Administrative Interface 
13
    </h3>
14
    <p>
15
       The administrative interface allows you to do a "quick and dirty" admin interface to allow users to administer their site 
16
    </p>
17
    <p>
18
       The basics of creating an administrative interface is to do the following: 
19
    </p>
20
    <ul>
21
      <LI>
22
         Create a database definition file (see sql2admin_hash.pl documentation)
23
      </LI>
24
      <li>
25
         In an html file (say, admin.html), have the following two PHP commands execute 
26
      <ul>
27
        <li>
28
          <em>include_once &#039;library.php&#039;&#059;</em>
29
        </li>
30
        <LI>
31
          <em>doAdmin()&#059;</em>
32
        </LI>
33
      </ul>
34
      </li>
35
    </ul>
36
    <p>
37
       A database definition file requires one global variable, <em>$DATABASE_DEFINITION</em>, and can also have four contstants defined. The constants are <em>MAX_INPUT_FIELD_DISPLAY</em>,<em>IMAGE_DIRECTORY</em>,<em>EDIT_IMAGE_HEIGHT</em>, and <em>MAX_UPLOAD_FILE_SIZE</em>, which are defined in the table below 
38
    </p>
39
    <table border="1">
40
      <caption>
41
         Constants 
42
      </caption>
43
      <thead>
44
        <tr>
45
          <th>
46
             Constant 
47
          </th>
48
          <th>
49
             Type 
50
          </th>
51
          <th>
52
             Use 
53
          </th>
54
          <th>
55
             Example 
56
          </th>
57
        </tr>
58
      </thead>
59
      <tbody>
60
        <tr>
61
          <td>
62
             MAX_INPUT_FIELD_DISPLAY 
63
          </td>
64
          <td>
65
             Integer 
66
          </td>
67
          <td>
68
             Determines the display width of text entry fields 
69
          </td>
70
          <td>
71
             define ( MAX_INPUT_FIELD_DISPLAY, 80 ); 
72
          </td>
73
        </tr>
74
        <tr>
75
          <td>
76
             MAX_TEXTAREA_HEIGHT
77
          </td>
78
          <td>
79
             Integer 
80
          </td>
81
          <td>
82
             Determines the number of columns for Text fields
83
          </td>
84
          <td>
85
             define ( DEFAULT_TEXTAREA_HEIGHT, 5 ); 
86
          </td>
87
        </tr>
88
        <tr>
89
          <td>
90
             IMAGE_DIRECTORY 
91
          </td>
92
          <td>
93
             Text 
94
          </td>
95
          <td>
96
             Relative URL to images whose links are stored in database 
97
          </td>
98
          <td>
99
             define ( IMAGE_DIRECTORY, '/pictures/' ); 
100
          </td>
101
        </tr>
102
        <tr>
103
          <td>
104
             EDIT_IMAGE_HEIGHT 
105
          </td>
106
          <td>
107
             Integer 
108
          </td>
109
          <td>
110
             Height (in pixels) of thumbnails in edit area 
111
          </td>
112
          <td>
113
             define ( EDIT_IMAGE_HEIGHT, 100 ); 
114
          </td>
115
        </tr>
116
        <tr>
117
          <td>
118
             MAX_UPLOAD_FILE_SIZE 
119
          </td>
120
          <td>
121
             Integer 
122
          </td>
123
          <td>
124
             Maximum size of upload file allowed 
125
          </td>
126
          <td>
127
             define ( MAX_UPLOAD_FILE_SIZE, 1024*1024*10 ); 
128
          </td>
129
        </tr>
130
      </tbody>
131
    </table>
132
    <p>
133
      <em>$DATABASE_DEFINITION</em> is a multi-level hash and must be defined as global. Since it is multi-level, this document will work on a per-layer basis 
134
    </p>
135
    <p>
136
       At the top level, <em>$DATABASE_DEFINITION</em> is a hash of table definitions within the database. Each table to be directly edited must have a separat entry. Thus, if we are insterested in the tables table1, table2 and table3, our <em>$DATABASE_DEFINITION</em> definition would like like this 
137
    </p>
138
    <pre> global $DATABASE_DEFINITION;
139
$DATABASE_DEFINITION = array(
140
         'table1' => array( <span style="font-style : oblique;">table1 definition here</span>),
141
         'table2' => array( <span style="font-style : oblique;">table2 definition here</span>),
142
         'table3' => array( <span style="font-style : oblique;">table3 definition here</span>)
143
                            ); 
144
    </pre>
145
    <p>
146
       Each table definition is, as noted above, an addition hash. The table definition hash consists of the following keys 
147
    </p>
148
    <table border="1">
149
      <caption>
150
         Table Definition Hash Keys 
151
      </caption>
152
      <thead>
153
        <tr>
154
          <th>
155
             Key 
156
          </th>
157
          <th>
158
             Type 
159
          </th>
160
          <th>
161
             Use 
162
          </th>
163
          <th>
164
             Example 
165
          </th>
166
        </tr>
167
      </thead>
168
      <tbody>
169
        <tr>
170
          <td>
171
             table name 
172
          </td>
173
          <td>
174
             String 
175
          </td>
176
          <td>
177
             Name of current table 
178
          </td>
179
          <td>
180
             'table name' => '_global' 
181
          </td>
182
        </tr>
183
        <tr>
184
          <td>
185
             display name 
186
          </td>
187
          <td>
188
             String 
189
          </td>
190
          <td>
191
             Alternate text to display for table name
192
          </td>
193
          <td>
194
             'display name' => 'Global' 
195
          </td>
196
        </tr>
197
        <tr>
198
          <td>
199
             key field 
200
          </td>
201
          <td>
202
             Text or Array of Text 
203
          </td>
204
          <td>
205
             List of key fields for table 
206
          </td>
207
          <td>
208
             'key field' => 'description', 
209
          </td>
210
        </tr>
211
        <tr>
212
          <td>
213
             display columns 
214
          </td>
215
          <td>
216
             Array of Text 
217
          </td>
218
          <td>
219
             List of columns to be displayed when selecting record to edit 
220
          </td>
221
          <td>
222
             'display columns' => array('value','description') 
223
          </td>
224
        </tr>
225
        <tr>
226
          <td>
227
             display query 
228
          </td>
229
          <td>
230
             SQL Query Sting 
231
          </td>
232
          <td>
233
             Query to be executed to display list when multiple rows of a table are displayed. Basis for 'display columns' above. <strong>Must include the keyfield</strong>
234
          </td>
235
          <td>
236
             'display query' => 'select value,description from _global' 
237
          </td>
238
        </tr>
239
        <tr>
240
          <td>
241
             field info 
242
          </td>
243
          <td>
244
             Hash 
245
          </td>
246
          <td>
247
             Hash of Column Definitions for Table 
248
          </td>
249
          <td>
250
             'field info' => array(<em>Field Definition List</em>) 
251
          </td>
252
        </tr>
253
        <tr>
254
          <td>
255
             complex join 
256
          </td>
257
          <td>
258
             Hash 
259
          </td>
260
          <td>
261
             Hash of one or more 'complex join' relationship definitions. See the limited definition below 
262
          </td>
263
          <td>
264
             'complex join' => array(<em>complex join Definitions</em>) 
265
          </td>
266
        </tr>
267
      </tbody>
268
    </table>
269
    <p>
270
       The term <em>complex join</em> is not used in the standard way here. It is simply the way I use to describe the layout where two tables are related via an intermediate table. This intermediate table stores a key field for each table being joined, so it should be a many-to-many relationship, but I was having a bad night when I wrote this. So, until I go through and change all the code, it is called complex join 
271
    </p>
272
    <p>
273
       The 'field info' hash contains information about each column or field in the table. Each field is signified by its name (the key to the hash), followed by its attributes as in: 
274
      <br>
275
      <em>'fieldname' => array('attribute' => value, 'attribute' => value)</em>
276
      <br>
277
       See the following table 
278
    </p>
279
    <table border="1">
280
      <caption>
281
         Field Definition Attributes 
282
      </caption>
283
      <thead>
284
        <tr>
285
          <th>
286
             Attribute 
287
          </th>
288
          <th>
289
             Type 
290
          </th>
291
          <th>
292
             Use 
293
          </th>
294
          <th>
295
             Example 
296
          </th>
297
        </tr>
298
      </thead>
299
      <tbody>
300
        <tr>
301
          <td>
302
             type 
303
          </td>
304
          <td>
305
             String 
306
          </td>
307
          <td>
308
             Type of This column. Valid Types are shown in following table 
309
          </td>
310
          <td>
311
             'type' => 'string' 
312
          </td>
313
        </tr>
314
        <tr>
315
          <td>
316
             display name
317
          </td>
318
          <td>
319
             String
320
          </td>
321
          <td>
322
             In most cases, if this exists it will be displayed on edit screens instead of the actual column name
323
          </td>
324
          <td>
325
             'display name' => 'Nice Label'
326
          </td>
327
        </tr>
328
        <tr>
329
          <td>
330
             width 
331
          </td>
332
          <td>
333
             Integer 
334
          </td>
335
          <td>
336
             Maximum Width of Text Field, or number of columns for textarea
337
          </td>
338
          <td>
339
             'width' => 64 
340
          </td>
341
        </tr>
342
        <tr>
343
          <td>
344
             height
345
          </td>
346
          <td>
347
             Integer 
348
          </td>
349
          <td>
350
             Number of rows for textarea (<em>'type'=>'text'</em>)
351
          </td>
352
          <td>
353
             'height' => 64 
354
          </td>
355
        </tr>
356
        <tr>
357
          <td>
358
             required 
359
          </td>
360
          <td>
361
             Boolean 
362
          </td>
363
          <td>
364
             If non-zero (or true), form is not processed unless this field has a value unless <em>default</em> below is defined 
365
          </td>
366
          <td>
367
             'required' => true 
368
          </td>
369
        </tr>
370
        <tr>
371
          <td>
372
             default 
373
          </td>
374
          <td>
375
             String 
376
          </td>
377
          <td>
378
             Default value for field if is is null or empty 
379
          </td>
380
          <td>
381
             'default' => 'No Entry Made' 
382
          </td>
383
        </tr>
384
        <tr>
385
          <td>
386
             keyfield 
387
          </td>
388
          <td>
389
             Boolean 
390
          </td>
391
          <td>
392
             If set to non-zero or true, this is a key field 
393
          </td>
394
          <td>
395
             'keyfield' => true 
396
          </td>
397
        </tr>
398
        <tr>
399
          <td>
400
             height 
401
          </td>
402
          <td>
403
             Integer 
404
          </td>
405
          <td>
406
             Height of textarea box, in lines. 
407
          </td>
408
          <td>
409
             'height' => 5 
410
          </td>
411
        </tr>
412
        <tr>
413
          <td>
414
             filetype
415
          </td>
416
          <td>
417
             String
418
          </td>
419
          <td>
420
             For use in File type only, defines the file type to be a picture
421
          </td>
422
          <td>
423
             'filetype' => 'picture'
424
          </td>
425
        </tr>
426
      </tbody>
427
    </table>
428
    <table border="1">
429
      <caption>
430
         Valid Data Types 
431
      </caption>
432
      <thead>
433
        <tr>
434
          <th>
435
             Data Type 
436
          </th>
437
          <th>
438
             Database Types 
439
          </th>
440
          <th>
441
             Use 
442
          </th>
443
          <th>
444
             Notes 
445
          </th>
446
        </tr>
447
      </thead>
448
      <tbody>
449
        <tr>
450
          <td>
451
             string 
452
          </td>
453
          <td>
454
             varchar or char 
455
          </td>
456
          <td>
457
             Holds up to <em>'width'</em> characters of Data 
458
          </td>
459
          <td>
460
             'type' => 'string' 
461
          </td>
462
        </tr>
463
        <tr>
464
          <td>
465
             text 
466
          </td>
467
          <td>
468
             text or blob type 1 
469
          </td>
470
          <td>
471
             Holds unlimited text, with line returns, etc... 
472
          </td>
473
          <td>
474
          </td>
475
        </tr>
476
        <tr>
477
          <td>
478
             file 
479
          </td>
480
          <td>
481
             blob 
482
          </td>
483
          <td>
484
             Stores result of a file input field into the column 
485
          </td>
486
          <td>
487
          </td>
488
        </tr>
489
        <tr>
490
          <td>
491
             bool 
492
          </td>
493
          <td>
494
             char 
495
          </td>
496
          <td>
497
             On data entry, stores 1 or 0. Any non-zero value assumed to be true 
498
          </td>
499
          <td>
500
          </td>
501
        </tr>
502
        <tr>
503
          <td>
504
             int 
505
          </td>
506
          <td>
507
             int 
508
          </td>
509
          <td>
510
             Standard Integer 
511
          </td>
512
          <td>
513
          </td>
514
        </tr>
515
        <tr>
516
          <td>
517
             float 
518
          </td>
519
          <td>
520
             decimal 
521
          </td>
522
          <td>
523
             Floating Point Number 
524
          </td>
525
          <td>
526
          </td>
527
        </tr>
528
        <tr>
529
          <td>
530
             lookup 
531
          </td>
532
          <td>
533
             Integer 
534
          </td>
535
          <td>
536
             Field is an integer which is a foreign key into a different table. 
537
          </td>
538
          <td>
539
          </td>
540
        </tr>
541
      </tbody>
542
    </table>
543
    <p>
544
       Foreign Keys have their own set of attributes. A lookup field type is assumed to be a 1:1 key into a separate table. As such, the following attributes are defined only for lookup data types, and these are the only locations these attributes are found. 
545
    </p>
546
    <p>
547
       Foreign Keys are displayed as Select boxes, with the value being the keyfield of the foreign table and the displayed value determined by the field definition (see table) 
548
    </p>
549
    <table border="1">
550
      <caption>
551
         Foreign Key Attributes 
552
      </caption>
553
      <thead>
554
        <tr>
555
          <th>
556
             Attribute 
557
          </th>
558
          <th>
559
             Type 
560
          </th>
561
          <th>
562
             Use 
563
          </th>
564
          <th>
565
             Example 
566
          </th>
567
        </tr>
568
      </thead>
569
      <tbody>
570
        <tr>
571
          <td>
572
             table 
573
          </td>
574
          <td>
575
             String 
576
          </td>
577
          <td>
578
             Target table of join 
579
          </td>
580
          <td>
581
             'table' => 'category' 
582
          </td>
583
        </tr>
584
        <tr>
585
          <td>
586
             keyfield 
587
          </td>
588
          <td>
589
             String 
590
          </td>
591
          <td>
592
             Column name of keyfield in remote table 
593
          </td>
594
          <td>
595
             'keyfield' => 'category_id' 
596
          </td>
597
        </tr>
598
        <tr>
599
          <td>
600
             display_field 
601
          </td>
602
          <td>
603
             text 
604
          </td>
605
          <td>
606
             Column Name whose value is displayed to user for selection 
607
          </td>
608
          <td>
609
             'display_field' => 'description' 
610
          </td>
611
        </tr>
612
      </tbody>
613
    </table>
614
    <p>
615
       Many To Many Relationships (name here is one-to-many) are a special attribute of a table. In this case, the user may select many items for the current record to be related to. 'complex join' definitions must have the following underlying table structure 
616
    </p>
617
    <ul>
618
      <LI>
619
         Foreign Table with single Primary Key Entry of type int 
620
      </LI>
621
      <li>
622
         Intermediary Table must contain a single int column relating to current table 
623
      </li>
624
      <li>
625
         Intermediary Table must contain a single int column relating to foreign table 
626
      </li>
627
      <li>
628
         Primary Key for intermediary table must consist solely of the two columns above 
629
      </li>
630
    </ul>
631
    <p>
632
       The <em>complex join</em> entry is itself a hash, consisting of one or more separate relationships. The Key of the hash is the display name on the editing screens. 
633
    </p>
634
    <pre> 'complex join' => array(
635
                    'Targets' => array(<span style="font-style : oblique;">Targets Definition</span>),
636
                    'Other' => array(<span style="font-style : oblique;">Other Definition</span>)
637
                 ) 
638
    </pre>
639
    <p>
640
       Each definition contains three attributes in their hash; <em>null field</em>, and two hashes <em>values table</em> and a <em>join table</em>. The following table shows each attribute and its sub-attributes. 
641
    </p>
642
    <table border='1'>
643
      <caption>
644
         'complex join' entry Attributes 
645
      </caption>
646
      <thead>
647
        <tr>
648
          <th>
649
             Parent Attribute 
650
          </th>
651
          <th>
652
             Attribute 
653
          </th>
654
          <th>
655
             Type 
656
          </th>
657
          <th>
658
             Description 
659
          </th>
660
          <th>
661
             Notes 
662
          </th>
663
        </tr>
664
      </thead>
665
      <tbody>
666
        <tr>
667
          <td>
668
             null field 
669
          </td>
670
          <td>
671
          </td>
672
          <td>
673
             String 
674
          </td>
675
          <td>
676
             Unknown what this function is at this time 
677
          </td>
678
          <td>
679
          </td>
680
        </tr>
681
        <tr>
682
          <td>
683
             values table 
684
          </td>
685
          <td>
686
             table name 
687
          </td>
688
          <td>
689
             string 
690
          </td>
691
          <td>
692
             name of table that holds values to be displayed 
693
          </td>
694
          <td>
695
          </td>
696
        </tr>
697
        <tr>
698
          <td>
699
             values table 
700
          </td>
701
          <td>
702
             join condition 
703
          </td>
704
          <td>
705
             string 
706
          </td>
707
          <td>
708
             Not currently used 
709
          </td>
710
          <td>
711
             This was originally used to define the join from the intermediary table to the target table, but is no longer implemented 
712
          </td>
713
        </tr>
714
        <tr>
715
          <td>
716
             values table 
717
          </td>
718
          <td>
719
             display field 
720
          </td>
721
          <td>
722
             string 
723
          </td>
724
          <td>
725
             name of field to be displayed on edit 
726
          </td>
727
          <td>
728
             Do Not include the table name, ie do not use tablename.columnname here 
729
          </td>
730
        </tr>
731
        <tr>
732
          <td>
733
             values table 
734
          </td>
735
          <td>
736
             key field 
737
          </td>
738
          <td>
739
             string 
740
          </td>
741
          <td>
742
             Name of key field which is used to join to intermediary table 
743
          </td>
744
          <td>
745
             Do Not include the table name, ie do not use tablename.columnname here 
746
          </td>
747
        </tr>
748
        <tr>
749
          <td>
750
             join table 
751
          </td>
752
          <td>
753
             table name 
754
          </td>
755
          <td>
756
             String 
757
          </td>
758
          <td>
759
             Name of table used to join current table to values table 
760
          </td>
761
          <td>
762
             Note: This is the intermediary Table name 
763
          </td>
764
        </tr>
765
        <tr>
766
          <td>
767
             join table 
768
          </td>
769
          <td>
770
             values link 
771
          </td>
772
          <td>
773
             String 
774
          </td>
775
          <td>
7 rodolico 776
             Name of column in intermediate table (join table) that links to other table (values table)
1 rodolico 777
          </td>
778
          <td>
779
             Do Not include the table name, ie do not use tablename.columnname here 
780
          </td>
781
        </tr>
782
        <tr>
783
          <td>
784
             join table 
785
          </td>
786
          <td>
787
             my link 
788
          </td>
789
          <td>
790
             String 
791
          </td>
792
          <td>
7 rodolico 793
             Name of column in intermediate table (join table) that links to this column in this table
1 rodolico 794
          </td>
795
          <td>
796
             Do Not include the table name, ie do not use tablename.columnname here 
797
          </td>
798
        </tr>
799
        <tr>
800
          <td>
801
             join table 
802
          </td>
803
          <td>
804
             join condition 
805
          </td>
806
          <td>
807
             string 
808
          </td>
809
          <td>
810
             Not currently used 
811
          </td>
812
          <td>
813
             This was originally used to define the join from the intermediary table to the target table, but is no longer implemented 
814
          </td>
815
        </tr>
816
      </tbody>
817
    </table>
16 rodolico 818
    <h3>children</h3>
819
    <p>children is the way to define one or more child tables. A child table is similar to a lookup table, but it is more complex in that it may have multiple display columns, may be joined to other tables, etc... However, a child table is a direct descendant of the current table, ie it will not join ot another row in the current table.</p>
820
    <p>children only has two values; the name of the table which is a child, and the column in the child table that is used for the join. If the latter is missing, it is assumed to be the same as the primary key of the current table.</p>
821
    <p>During edit, all rows in the child table belonging to the current table are displayed in tabular form, using the 'display query' defined in that table.</p>
1 rodolico 822
    <h3>Example</h3>
823
    <p>Following example is based upon a real life application that stores knowledge base articles. Articles can be associated with one or more categories, and the categories table is hierachial in that each entry has a "parent node" column which links it to another entry in the same table</p>
824
    <p>Note the foreign key definition in table categories, and the many-to-many relationship in articles, going into categories.</p>
825
    <pre>
826
   define ( MAX_INPUT_FIELD_DISPLAY, 80 ); // this is the maximum input field size
827
   define ( IMAGE_DIRECTORY, '/pictures/' );  // relative URL where pictures are stored
828
   define ( EDIT_IMAGE_HEIGHT, 100 );         // height for thumbnail of pictuers
829
   define ( MAX_UPLOAD_FILE_SIZE, 1024*1024*10 ); // 10 meg
830
 
831
    global $DATABASE_DEFINITION;
832
    $DATABASE_DEFINITION = array(
833
      'article' => array( 
834
              'table name' => 'article',
835
              'key field' => 'article_id',
836
              'display columns' => array('article_id','title'),
837
              'display query' => 'select article_id,title from article',
838
              'field info' => array(
839
                      'article_id' => array('keyfield' => 1 , 'required' => true , 'readonly' => true , 'type' => 'int' , 'width' => 10),
840
                      'summary' => array('type' => 'string' , 'width' => 255),
841
                      'content' => array('type' => 'text', 'width'=> 80 ),
842
                      'title' => array('type' => 'string' , 'width' => 255)
843
              ),
844
            'complex join' => array(
845
              'professionals' => array(
846
                'null field'   => 'article_category.article_id',
847
                'values table' => array( // table that holds the values
848
                                  'table name' => 'category',
849
                                  'join condition' => 'article_category.category_id = category.category_id',
850
                                  'display field' => 'name',
851
                                  'key field'    => 'category_id'
852
                                  ),
853
                'join table'   => array(  // table that 'joins' this table to values table
854
                                  'table name' => 'article_category',
855
                                  'values link' => 'category_id',
856
                                  'my link' => 'article_id',
857
                                  'join condition' => 'article_category.article_id = article.article_id' )
858
              ) // professionals
859
            ) // complex join
860
 
861
      ),
862
      'article_category' => array( 
863
              'table name' => 'article_category',
864
              'key field' => array('article_id','category_id'),
865
              'display columns' => array('category_id','article_id'),
866
              'display query' => 'select category_id,article_id from article_category',
867
              'field info' => array(
868
                      'category_id' => array('keyfield' => 1 , 'required' => true , 'default' => '0' , 'type' => 'int' , 'width' => 10),
869
                      'article_id' => array('keyfield' => 1 , 'required' => true , 'default' => '0' , 'type' => 'int' , 'width' => 10)
870
              )
871
      ),
872
      'category' => array( 
873
              'table name' => 'category',
874
              'key field' => 'category_id',
875
              'display columns' => array('name'),
876
              'display query' => 'select category_id,name from category',
877
              'field info' => array(
878
                      'parent_category' => array( 'type' => 'lookup',   // this is the field type, and must be ints
879
                                            'table' => 'category',    // remote table name
880
                                            'keyfield' => 'category_id',  // keyfield in remote table, ie category.category_id 
881
                                            'display_field' => 'name' // this is the column displayed to the user
882
                                          ),
883
 
884
                      'category_id' => array('keyfield' => 1 , 'required' => true , 'readonly' => true , 'type' => 'int' , 'width' => 10),
885
                      'name' => array('required' => true , 'default' => '' , 'type' => 'string' , 'width' => 64)
886
              )
887
      )
888
    );
889
    </pre>
890
    <p>Modification required for update to new naming convention. In old source code, do the following search and replaces:</p>
891
    <ul><LI>s/foreignkey/lookup/</li>
892
<li>s/one to many/complex join/
893
</LI></ul>
894
  </body>
895
</html>
896
 
897