00001 <?php
00002 ############################################################################
00003 # Copyright (C) 2004 by Paolo Armani & Stefano Menegon #
00004 # paolo.armani@gmail.com menegon@itc.it #
00005 # #
00006 # This program is free software; you can redistribute it and#or modify #
00007 # it under the terms of the GNU Library General Public License as #
00008 # published by the Free Software Foundation; either version 2 of the #
00009 # License, or (at your option) any later version. #
00010 # #
00011 # This program is distributed in the hope that it will be useful, #
00012 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
00013 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
00014 # GNU General Public License for more details. #
00015 # #
00016 # You should have received a copy of the GNU Library General Public #
00017 # License along with this program; if not, write to the #
00018 # Free Software Foundation, Inc., #
00019 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
00020 ############################################################################
00021
00022
00024
00026
00027 * \param $db Pgdb object, to interface with postgresql db
00028 * \param $pkey name of column to use as a pkey ($pkey[schema][table]=column)
00029 * \param $ref hash of references to other tables
00030 * \param $inh if a table inherits other tables
00031 * \param $perms not used, to manage perms in the future
00032 * \param $priority table priority, not used
00033 * \param $form html form informations
00034 * \param $actions array of action allowed (edit, insert....)
00035 * \param $operator array of operator for each pseudotype
00036 * \param $user not used
00037 * \param $group not used
00038 * \param $configSchema not used
00039 * \param $indexPriotity priority priority of column, if must be reorder from postgres column order ($priority[schema][table][column]=number)
00040 * \param $erros Pgmessage object
00041 * \param $messages Pgmessage object
00042 * \param $STRINGS array of strings, form multilanguage support
00043 */
00044
00045 class Pgtable{
00046 var $db;
00047
00048 var $pkey;
00049 var $ref;
00050 var $inh;
00051 var $perms;
00052 var $priority;
00053
00054 var $form;
00055 var $data;
00056 var $links;
00057
00058 var $actions;
00059
00060 var $user;
00061 var $group;
00062 var $configSchema;
00063
00064 var $operator;
00065
00066 var $indexPriority;
00067
00068 var $errors;
00069 var $messages;
00070 var $STRINGS;
00071
00073
00075
00076 * \param $db_conn valid postgresql connection (the returned value of pg_connect() function)
00077 * \param $configFile ini file in wich there are set some variables as icons urls etc.
00078 * \param $users not used
00079 * \param $group not used
00080 * \param $configSchema not used
00081 * \return
00082 */
00083 function Pgtable($db_conn, $configFile, $user='', $group='', $configSchema=''){
00084 $this->db=new Pgdb($db_conn);
00085
00086 $this->user=$user;
00087 $this->group=$group;
00088 $this->configSchema=$configSchema;
00089
00090 $this->form=array();
00091 $this->data=array();
00092 $this->perms=array();
00093 $this->priority=array();
00094
00095 $this->errors = new Pgmessages();
00096 $this->messages = new Pgmessages();
00097
00098 $this->data["in"]=array_merge($_GET, $_POST);
00099
00100 $this->conf=parse_ini_file($configFile,true);
00101
00102 $this->formName=$this->conf["html"]["formName"];
00103 $this->db->SQL_DEBUG=$this->conf["debug"]["SQL"];
00104 $this->db->filesDir=$this->conf["DbFiles"];
00105
00106 $this->STRINGS=parse_ini_file($this->conf["strings"],true);
00107
00108 $this->operator=array("text"=>array("~*"=>$this->STRINGS["match"],
00109 "!~*"=>$this->STRINGS["not_match"],
00110 "="=>$this->STRINGS["eq"],
00111 "!="=>$this->STRINGS["not_eq"],
00112 ">"=>$this->STRINGS["greater"],
00113 "<"=>$this->STRINGS["minor"],
00114 ">="=>$this->STRINGS["greater_eq"],
00115 "<="=>$this->STRINGS["minor_eq"]
00116 ),
00117 "numeric"=>array("="=>$this->STRINGS["eq"],
00118 "!="=>$this->STRINGS["not_eq"],
00119 ">"=>$this->STRINGS["greater"],
00120 "<"=>$this->STRINGS["minor"],
00121 ">="=>$this->STRINGS["greater_eq"],
00122 "<="=>$this->STRINGS["minor_eq"]
00123 ),
00124 "generic"=>array("="=>$this->STRINGS["eq"],
00125 "!="=>$this->STRINGS["not_eq"]
00126 ),
00127 "geometry"=>array("&&"=>$this->STRINGS["in_box"]
00128
00129 )
00130 );
00131
00132 }
00133
00135
00137
00138 */
00139 function sort_field(){
00140 $this->set_priority();
00141 }
00142
00144
00146
00147 * \param $priority integer vvalue
00148 * \param $schema schema of table
00149 * \param $table table name
00150 * \param $field column name
00151 */
00152 function set_priority($priority=0, $schema=false, $table=false, $field=false){
00153 if($schema && $table && $field){
00154 $this->indexPriority[$schema][$table][$field] =$priority;
00155 }
00156 arsort($this->indexPriority);
00157 }
00158
00160
00162
00163 * \param $schema schema of table
00164 * \param $table table name
00165 */
00166
00167
00168 function set_perms_priority( $tbSchema, $tbName){
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193 foreach($this->db->attribs[$tbSchema][$tbName] as $field=>$val){
00194 $attrib=&$this->db->attribs[$tbSchema][$tbName][$field];
00195 $this->indexPriority[$tbSchema][$tbName][$field]=$attrib["priority"];
00196 }
00197
00198 $this->sort_field();
00199
00200
00201
00202
00203
00204
00205 }
00206
00208
00210
00211 * \param $tbSchema schema of table
00212 * \param $tbName table name
00213 */
00214 function set_defaults($tbSchema, $tbName){
00215
00216
00217
00218
00219 $this->actions[$tbSchema][$tbName]=array('search','edit','insert','delete','view', 'preMultipleInsert', 'copy', 'modifySearch', 'multipleInsert');
00220
00221 $this->perms[$tbSchema][$tbName]=false;
00222 $this->piority[$tbSchema][$tbName]=false;
00223
00224
00225
00226 foreach($this->db->attribs[$tbSchema][$tbName] as $key=>$val){
00227 $field=&$this->db->attribs[$tbSchema][$tbName][$key];
00228
00229
00230
00231
00232
00233 $this->form[$tbSchema][$tbName][$key]=array(
00234 "default"=>$field['adsrc'],
00235 "type"=>$field['type'],
00236 "name"=>$field['attname'],
00237 "input-name"=>'['.$tbSchema.']['.$tbName.']['.$field['attname'].']',
00238 "interpolate"=>true
00239 );
00240
00241
00242
00243 }
00244 }
00245
00247
00249
00250 * \param $tbSchema schema of table
00251 * \param $tbName table name
00252 * \param $pkey the primary key column of the table
00253 */
00254 function add_table($tbSchema, $tbName, $pkey=false){
00255
00256 $this->pkey[$tbSchema][$tbName]=$pkey?$pkey:'oid';
00257
00258
00259
00260 $this->db->get_table_attributes($tbSchema, $tbName);
00261
00262
00263
00264 $this->ref[$tbSchema][$tbName]= $this->db->get_foreign_keys($tbSchema, $tbName);
00265
00266 $this->inh[$tbSchema][$tbName]= $this->db->get_inh_tables($tbSchema, $tbName);
00267
00268 $this->set_defaults($tbSchema, $tbName);
00269 $this->set_perms_priority($tbSchema, $tbName);
00270
00271 }
00272
00274
00276
00277 * \note if db is created with foeing key or references commandes, this is called automatically
00278 * \param $schemaParent schema of parent table
00279 * \param $parent parent table
00280 * \param $parentcol parent table column
00281 * \param $schemaChild schema of child table
00282 * \param $child child table
00283 * \param $childcol child table column
00284 */
00285 function join_child($schemaParent='public', $parent, $parentcol, $chemaChild='public', $child, $childcol){
00286 $this->ref[$schemaParent][$parent][$parentcol]["child"][]=array('table'=>$child, 'field'=>$childcol, 'schema'=>$schemaChild);
00287 }
00288
00289
00291
00293
00294 * \note if db is created with foeing key or references commandes, this is called automatically and a popup window is opened when necessary. In this case, for small parent references, it is somtimes better use a select list of possible values: see alter_parent() function for this.
00295 * \param $schemaChild schema of child table
00296 * \param $child child table
00297 * \param $childcol child table column
00298 * \param $schemaParent schema of parent table
00299 * \param $parent parent table
00300 * \param $parentcol parent table column
00301 * \param $query valid sql query that select all valid childs
00302 * \note $query must is a valid sql query that returns two columns, first mast be renamed (with AS sintax) as "OID" , the second as $field. First must correspond to values to insert in $tbName.$field and second as a label of this.
00303 * \note $query must have no semicolon
00304 * \param $type='popup' or 'select' : the first open a new window, the second make a select menu(DONT USE THIS FOR LONG TABLES!!|)
00305 * \param $when=array('search', 'view', 'preSearch', 'insert', 'edit') when join
00306 */
00307 function join_parent($schemaChild='public', $child, $childcol, $schemaParent='public', $parent, $parentcol, $schema='public', $query="", $type="popup", $when=array('search', 'view', 'preSearch', 'new', 'edit')){
00308 $this->ref[$schemaChild][$child][$childcol]["parent"]=array('table'=>$parent, 'field'=>$parentcol, "schema"=>$schemaParent, "when"=>$when, "query"=>$query, "type"=>$type);
00309 }
00310
00311
00313
00315
00316 * \param $schemaChild schema of child table
00317 * \param $child child table
00318 * \param $childcol child table column
00319 * \param $arr hash with this facoltative keys: table, field, query, type=>(popup or select), when=>array('search', 'view', 'preSearch', 'insert', 'edit')
00320 * \note $query must is a valid sql query that returns two columns, first mast be renamed (with AS sintax) as "OID" , the second as $field. First must correspond to values to insert in $tbName.$field and second as a label of this.
00321 * \note $query must have no semicolon
00322 */
00323 function alter_parent($schemaChild, $child, $childcol, $arr=array()){
00324 $this->ref[$schemaChild][$child][$childcol]["parent"]=array_merge($this->ref[$schema][$child][$childcol]["parent"],$arr);
00325 }
00326
00327
00329
00331
00332 * \param $when when this function is called (search, insert, edit....)
00333 */
00334 function auto_join($tbSchema, $tbName, $field, $when){
00335 if(is_array($this->ref[$tbSchema][$tbName][$field]["parent"]) && is_array($this->ref[$tbSchema][$tbName][$field]["parent"]["when"]) && in_array($when, $this->ref[$tbSchema][$tbName][$field]["parent"]["when"])){
00336 $ref_field=$this->ref[$tbSchema][$tbName][$field]["parent"]["field"];
00337 $ref_table=$this->ref[$tbSchema][$tbName][$field]["parent"]["table"];
00338 $query=$this->ref[$tbSchema][$tbName][$field]["parent"]["query"];
00339 if($query==''){
00340 $query='SELECT "'.pg_escape_string($ref[$field]["parent"]["field"]).'" AS "OID", "'.pg_escape_string($ref[$field]["parent"]["field"]).'" AS "'.pg_escape_string($field).'" FROM "';
00341 $query.=$this->db->useSchema?(pg_escape_string($tbSchema).'"."'):'';
00342 $query.=pg_escape_string($ref[$field]["parent"]["table"]).'" ORDER BY "OID"';
00343 }
00344
00345 switch($this->ref[$tbSchema][$tbName][$field]["parent"]["type"]){
00346 case 'select':
00347 $this->form[$tbSchema][$tbName][$field]['values']=prepare_array_select($this->db->dbConn, $query);
00348 $this->form[$tbSchema][$tbName][$field]['type']='select';
00349 if($this->db->SQL_DEBUG) echo $query."<br>\n";
00350 break;
00351 default:
00352 $str0='['.urlencode($tbSchema).']['.urlencode($ref_table).']';
00353 $str1=$str0.'['.urlencode($ref_field).']';
00354 $this->form[$tbSchema][$tbName][$field]['ref_table']["search"]=$_SERVER["PHP_SELF"].'?tbInfo'.$str0.'[pkey]='.urlencode($ref_field).'&action='.$this->STRINGS["submit"]["search"];
00355 $this->form[$tbSchema][$tbName][$field]['ref_table']["view"]=$_SERVER["PHP_SELF"].'?tbInfo'.$str0.'[pkey]='.urlencode($ref_field).'&action='.$this->STRINGS["submit"]["view"]."&pkey".$str0."=";
00356 $this->form[$tbSchema][$tbName][$field]['type']='popup';
00357 }
00358 }
00359 }
00360
00362
00364
00365 * \param $when when this function is called (search, insert, edit....)
00366 */
00367 function auto_join_query($tbSchema, $tbName, $when){
00368
00369 if($when!='search' && $when!='view') return '';
00370 $ref=&$this->ref[$tbSchema][$tbName];
00371 $i=0;
00372
00373 $schemaTable='"'.($this->db->useSchema?(pg_escape_string($tbSchema).'"."'):'').pg_escape_string($tbName).'"';
00374 $join=" FROM $schemaTable ";
00375
00376 $select="SELECT ";
00377 if($this->pkey[$tbSchema][$table]=='' || $this->pkey[$tbSchema][$table]=='oid') $select.="$schemaTable.oid, ";
00378 $first=true;
00379 foreach($this->db->attribs[$tbSchema][$tbName] as $field=>$val){
00380 if($first) $first=false;
00381 else $select.=', ';
00382
00383
00384 if(is_array($ref[$field]["parent"]) && is_array($ref[$field]["parent"]["when"]) && in_array($when, $ref[$field]["parent"]["when"]) && $ref[$field]["parent"]["type"]=="select"){
00385 $this->form[$tbSchema][$tbName][$field]['type']='select';
00386
00387
00388 if($ref[$field]["parent"]["query"]==''){
00389 $query='SELECT "'.pg_escape_string($ref[$field]["parent"]["field"]).'" AS "OID", "'.pg_escape_string($ref[$field]["parent"]["field"]).'" AS "'.pg_escape_string($field).'" FROM "';
00390 $query.=$this->db->useSchema?(pg_escape_string($tbSchema).'"."'):'';
00391 $query.=pg_escape_string($ref[$field]["parent"]["table"]).'" ORDER BY "OID"';
00392 }
00393 else{
00394 $query=$ref[$field]["parent"]["query"];
00395 }
00396
00397 $join.=$this->db->useSchema
00398 ?(" LEFT JOIN (".$query.') AS "TMP'.$i.'" ON("'.$tbSchema.'"."'.$tbName.'"."'.pg_escape_string($field)."\"=\"TMP$i\".\"OID\") ")
00399 :(" LEFT JOIN (".$query.') AS "TMP'.$i.'" ON("'.$tbName.'"."'.pg_escape_string($field)."\"=\"TMP$i\".\"OID\") ");
00400
00401 $select.="\"TMP$i\".\"".pg_escape_string($field).'"';
00402 $i++;
00403 }
00404 else{
00405 $select.='"'.pg_escape_string($field).'"';
00406 }
00407 }
00408 $select.=$join;
00409
00410 if($i) return $select;
00411 else return '';
00412 }
00413
00414
00416
00418
00419 * \param $name html input name value
00420 * \param $type type of current field(int, text...)
00421 * \param $defVal='' default value
00422 */
00423 function operator($name, $type='', $defVal=false){
00424 switch($type){
00425 case 'text':
00426 case 'varchar':
00427 $src= html_select($name,$this->operator["text"],$defVal);
00428 break;
00429 case 'date':
00430 case 'time':
00431 case 'timestamp':
00432 case 'timestamptz':
00433 case 'float8':
00434 case 'int8':
00435 case 'int4':
00436 case 'oid':
00437 $src= html_select($name,$this->operator["numeric"],$defVal);
00438 break;
00439 case 'geometry':
00440 $src= html_select($name,$this->operator["geometry"],$defVal);
00441 break;
00442 default: $src= html_select($name,$this->operator["generic"],$defVal);
00443 }
00444 return $src;
00445 }
00446
00448
00450
00451 * \param $tbSchema schema name of the table
00452 * \param $tbName table name
00453 */
00454 function delete($tbSchema, $tbName){
00455 $this->parse_data_in($this->data["in"]["val"]);
00456
00457 $arr=&$this->data["in"];
00458
00459 $numDelete=$numError=0;
00460 if(is_array($arr["pkey"][$tbSchema][$tbName]))
00461 foreach($arr["pkey"][$tbSchema][$tbName] as $num=>$pkey){
00462
00463 $ok=$this->db->delete($tbSchema, $tbName, $pkey, $this->pkey[$tbSchema][$tbName]);
00464
00465 if($ok===false){
00466 $numError++;
00467 }
00468 else{
00469 array_unset($arr["pkey"][$tbSchema][$tbName], $num);
00470
00471 $numDelete++;
00472 }
00473 }
00474 if($numError) $this->errors->add($this->STRINGS["ERROR"].": $numError ".$this->STRINGS["record_not_del"]);
00475 if($numDelete) $this->messages->add($this->STRINGS["NOTICE"].": $numDelete ".$this->STRINGS["record_del"]);
00476 }
00477
00478
00479
00481
00483
00484 * \param $tbSchema schema name of the table
00485 * \param $tbName table name
00486 */
00487 function update($tbSchema, $tbName){
00488 $this->parse_data_in($this->data["in"]["val"]);
00489
00490 $arr=&$this->data["in"];
00491
00492 $numUpdate=$numError=0;
00493 if(is_array($arr["pkey"][$tbSchema][$tbName]))
00494 foreach($arr["pkey"][$tbSchema][$tbName] as $num=>$pkey){
00495
00496 $pkey=$this->db->update($tbSchema, $tbName, $arr["val"][$tbSchema][$tbName][$num], $pkey, $this->pkey[$tbSchema][$tbName], $num);
00497
00498 if($pkey===false){
00499 $numError++;
00500 }
00501 else{
00502 $arr["pkey"][$tbSchema][$tbName][$num]=$pkey;
00503 $numUpdate++;
00504 }
00505 }
00506 if($numError) $this->errors->add($this->STRINGS["ERROR"].": $numError ".$this->STRINGS["record_not_up"]);
00507 if($numUpdate) $this->messages->add($this->STRINGS["NOTICE"].": $numUpdate ".$this->STRINGS["record_up"]);
00508 }
00509
00510
00512
00514
00515 * \param $tbSchema schema name of the table
00516 * \param $tbName table name
00517 * \return bidimensional array, that corespond to html table
00518 */
00519 function copy($tbSchema, $tbName){
00520 return $this->view_edit($tbSchema, $tbName, 'new', $this->data["in"]);
00521 }
00522
00524
00526
00527 * \param $tbSchema schema name of the table
00528 * \param $tbName table name
00529 */
00530 function insert($tbSchema, $tbName){
00531 $this->parse_data_in($this->data["in"]["val"]);
00532
00533 $arr=&$this->data["in"];
00534
00535 $numInsert=$numError=0;
00536 foreach($arr["val"][$tbSchema][$tbName] as $num=>$val){
00537
00538 $pkey=$this->db->insert($tbSchema, $tbName, $arr["val"][$tbSchema][$tbName][$num], $this->pkey[$tbSchema][$tbName], $num);
00539
00540 if($pkey===false){
00541 $numError++;
00542 }
00543 else{
00544 $arr["pkey"][$tbSchema][$tbName][$num]=$pkey;
00545 $numInsert++;
00546 }
00547 }
00548 if($numError) $this->errors->add($this->STRINGS["ERROR"].": $numError ".$this->STRINGS["record_not_in"]);
00549 if($numInsert) $this->messages->add($this->STRINGS["NOTICE"].": $numInsert ".$this->STRINGS["record_in"]);
00550 }
00551
00553
00555
00556 * \param $tbSchema schema name of the table
00557 * \param $tbName table name
00558 * \return bidimensional array, that corespond to html table
00559 */
00560 function pre_search_new( $tbSchema, $tbName){
00561 return $this->pre_search( $tbSchema, $tbName, true);
00562 }
00563
00565
00567
00568 * \param $tbSchema schema name of the table
00569 * \param $tbName table name
00570 * \return bidimensional array, that corespond to html table
00571 */
00572 function pre_search_modify( $tbSchema, $tbName){
00573 return $this->pre_search( $tbSchema, $tbName, false);
00574 }
00575
00577
00579
00580 * \param $tbSchema schema name of the table
00581 * \param $tbName table name
00582 * \param $new if is a new query
00583 * \return bidimensional array, that corespopnd to html table
00584 */
00585 function pre_search( $tbSchema, $tbName, $new=true){
00586
00587 foreach($this->indexPriority[$tbSchema][$tbName] as $field=>$priority){
00588 if ($this->db->attribs[$tbSchema][$tbName][$field]["display"]["preSearch"]){
00589
00590 if($new || !is_array($this->data["in"]["s"][$tbSchema][$tbName][$field])) $max=0;
00591 else $max=count($this->data["in"]["s"][$tbSchema][$tbName][$field]);
00592
00593 for($i=0; $i<=$max; $i++){
00594 $this->data["out"][$tbSchema][$tbName][$i][$field]='';
00595 }
00596 }
00597 }
00598
00599 $this->parse_data_out('preSearch', $tbSchema, $tbName);
00600 return $this->src[$tbSchema][$tbName];
00601 }
00602
00603
00605
00607
00608 * \param $tbSchema schema name of the table
00609 * \param $tbName table name
00610 * \return bidimensional array, that corespond to html table
00611 */
00612 function view($tbSchema, $tbName){
00613 return $this->view_edit($tbSchema, $tbName, 'view', $this->data["in"]);
00614 }
00615
00617
00619
00620 * \param $tbSchema schema name of the table
00621 * \param $tbName table name
00622 * \return bidimensional array, that corespond to html table
00623 */
00624 function edit($tbSchema, $tbName){
00625 return $this->view_edit($tbSchema, $tbName, 'edit', $this->data["in"]);
00626 }
00627
00628
00630
00632
00633 * \param $tbSchema schema name of the table
00634 * \param $tbName table name
00635 * \return bidimensional array, that corespond to html table
00636 */
00637 function pre_insert($tbSchema, $tbName){
00638
00639 $lines=$this->data["in"]["numInsert"][$tbSchema][$tbName];
00640 $lines=$lines?$lines:1;
00641
00642 $this->single[$tbSchema][$tbName]=($lines==1);
00643
00644
00645 for($i=1; $i<=$lines; $i++){
00646 foreach($this->indexPriority[$tbSchema][$tbName] as $field=>$priority){
00647 if ($this->db->attribs[$tbSchema][$tbName][$field]["display"]["new"]){
00648 if($i==1) $this->src[$tbSchema][$tbName]['___field'][$field]=$field;
00649 $this->data["out"][$tbSchema][$tbName][$i][$field]='';
00650 }
00651 }
00652 }
00653
00654 $this->parse_data_out("new", $tbSchema, $tbName);
00655
00656 return $this->src[$tbSchema][$tbName];
00657 }
00658
00660
00662
00663 * \param $tbSchema schema name of the table
00664 * \param $tbName table name
00665 * \param $when when is called (new, edit, search,...)
00666 * \param $datain input data ($_POST or $_GET)
00667 * \return bidimensional array, that corespopnd to html table
00668 */
00669 function view_edit($tbSchema, $tbName, $when, &$datain){
00670 $pkey=$this->pkey[$tbSchema][$tbName];
00671 $ref=&$this->ref[$tbSchema][$tbName];
00672
00673 if(!is_array($datain["pkey"][$tbSchema][$tbName])) return;
00674 $query=$this->auto_join_query($tbSchema, $tbName, $when);
00675
00676 $lines=0;
00677 foreach($datain["pkey"][$tbSchema][$tbName] as $num=>$pkeyVal){
00678 $arr=$this->db->set_value_from_pkey($tbSchema, $tbName, $pkeyVal, $pkey, $query);
00679 $fields=0;
00680 foreach($this->indexPriority[$tbSchema][$tbName] as $field=>$priority){
00681 if ($this->db->attribs[$tbSchema][$tbName][$field]["display"][$when]){
00682 $this->data["out"][$tbSchema][$tbName][$num][$field]=$arr[$field];
00683
00684
00685 if(array_key_exists('file',$this->db->attribs[$tbSchema][$tbName][$field]))
00686 $this->data["out"][$tbSchema][$tbName][$num][$field]=($when!='new')?$arr:'';
00687
00688
00689
00690 if($when!="new" && is_array($ref[$field]["child"])){
00691 foreach($ref[$field]["child"] as $val){
00692 $refLink[$num][]=array('schema'=>$tbSchema, 'table'=>$val["table"], "field"=>$val["field"], "val"=>$arr[$field]);
00693 }
00694 }
00695
00697
00698 if($lines==0){
00699 if($when!="new") $this->src[$tbSchema][$tbName]['___field'][' ']='';
00700 if ($when=='search'){
00701 $this->src[$tbSchema][$tbName]['___field'][$field]=$this->order($tbSchema, $tbName, $field);
00702 }
00703 else{
00704 $this->src[$tbSchema][$tbName]['___field'][$field]=$field;
00705 }
00706 }
00707
00708 if(!$fields){
00709 $pkeys[$num]=$pkeyVal;
00710 }
00712
00713 $fields++;
00714 }
00715 }
00716 $lines++;
00717 }
00718
00719
00721 $this->single[$tbSchema][$tbName]=($lines==1);
00722
00723 if($when!='new' & $lines>=1){
00724 if ($lines>1 && !(array_key_exists("ref_field",$this->data["in"]["tbInfo"][$tbSchema][$tbName]))){
00725 $js="<script language=\"javascript\">\nfunction checkAll(form){\n";
00726 foreach($pkeys as $num=>$pkeyVal){
00727 $this->src[$tbSchema][$tbName][$num][' ']=$this->actions($tbSchema, $tbName, $pkey, $num, $pkeyVal, $refLink[$num]);
00728 $input="pkey[$tbSchema][$tbName][$num]";
00729
00730 $input=addslashes($input);
00731
00732 $js.="form.elements['$input'].checked=form.elements['___checkAll'].checked;\n";
00733 }
00734 $js.="\n};\n</script>\n";
00735
00736 $old_search=arr_to_hidden($this->links[$tbSchema][$tbName]["search"]);
00737
00738 $html=$js.'<h5><input type="checkbox" name="___checkAll" onclick="checkAll(this.form);" checked />'.$this->STRINGS["select_all"].$old_search.'</h5>';
00739
00740 $this->src[$tbSchema][$tbName]['___field'][' ']=$html;
00741 }
00742 else{
00743 if ($when=='search'){
00744 $old_search=arr_to_hidden($this->links[$tbSchema][$tbName]["search"]);
00745 $this->src[$tbSchema][$tbName][$num][' ']='<input type="hidden" name="tbInfo['.$tbSchema.']['.$tbName.'][single]" value="1" />'.$old_search;
00746 }
00747
00748 foreach($pkeys as $num=>$pkeyVal){
00749 $this->src[$tbSchema][$tbName][$num][' '].=$this->actions($tbSchema, $tbName, $pkey, $num, $pkeyVal, $refLink[$num], "hidden");
00750 }
00751 }
00752 }
00753
00754
00756
00757 $this->parse_data_out($when, $tbSchema, $tbName);
00758 return $this->src[$tbSchema][$tbName];
00759 }
00760
00761
00763
00765
00766 * \param $tbSchema schema name of the table
00767 * \param $tbName table name
00768 * \param $field column name
00769 * \return html code
00770 */
00771 function order($tbSchema, $tbName, $field){
00772
00773
00774 $thisOrder=($this->links[$tbSchema][$tbName]["record"]==$field);
00775 $orderDesc=($this->links[$tbSchema][$tbName]["order"][$field]["order"][$tbSchema][$tbName]=='DESC');
00776 $link=$_SERVER["PHP_SELF"].'?'.arr_to_url($this->links[$tbSchema][$tbName]["order"][$field]);
00777 $title=$this->STRINGS["view_order"].($orderDesc?$this->STRINGS["biggest"]:$this->STRINGS["smallest"]);
00778
00779 $img=$thisOrder?'':'1';
00780 $img=$orderDesc ? $this->conf["Images"]['up'.$img] : $this->conf["Images"]['down'.$img];
00781
00782 $html=$field.'<div id="searchIcon"><a href="'.$link.'"><img target="'.$title.'" title="'.$title.'" src="'.$img.'"></a></div>';
00783
00784
00785 return $html;
00786 }
00787
00789
00791
00792 * \param $tbSchema schema name of the table
00793 * \param $tbName table name
00794 * \param $field column name
00795 * \param $num line number of current sets of records
00796 * \param $pkey pkey value of current record
00797 * \param $refLink hash to manage refernces to subtables
00798 * \param $type="checkbox" if use a checkbox or a hidden input type
00799 * \return html code
00800 */
00801 function actions($tbSchema, $tbName, $field, $num, $pkey, $refLink=array(), $type="checkbox"){
00802 if(array_key_exists("ref_field",$this->data["in"]["tbInfo"][$tbSchema][$tbName])){
00803 $tbInfo=&$this->data["in"]["tbInfo"][$tbSchema][$tbName];
00804 $onclick="opener.document.".$tbInfo["ref_inputForm"].".elements['".$tbInfo["ref_inputInput"]."'].value='".$pkey."'; window.close();";
00805 $icons='<div id="searchIcon"><a href="javascript:'.$onclick.'" ><img title="'.$this->STRINGS["select"].'" target="'.$this->STRINGS["select"].'" src="'.$this->conf["Images"]["select"].'" class="searchIcon" /></a></div>';
00806 return $icons;
00807 }
00808
00809 if($type=="checkbox")
00810 $checkBox= "<input type=\"checkbox\" name=\"pkey[$tbSchema][$tbName][$num]\" value=\"$pkey\" checked /> ";
00811 else
00812 $checkBox= "<input type=\"hidden\" name=\"pkey[$tbSchema][$tbName][$num]\" value=\"$pkey\"/> ";
00813
00814 $tmp=$this->links[$tbSchema][$tbName]["search"];
00815 $tmp["tbInfo"]=$this->data["in"]["tbInfo"];
00816 $tmp["pkey"][$tbSchema][$tbName][0]=$pkey;
00817 $tmp["action"]=$this->STRINGS["submit"]["delete"];
00818
00819 $tmp1["tbInfo"]=$this->data["in"]["tbInfo"];
00820 $tmp1["pkey"][$tbSchema][$tbName][0]=$pkey;
00821 $tmp1["action"]=$this->STRINGS["submit"]["view"];
00822
00823 $tmp2["tbInfo"]=$this->data["in"]["tbInfo"];
00824 $tmp2["pkey"][$tbSchema][$tbName][0]=$pkey;
00825 $tmp2["action"]=$this->STRINGS["submit"]["edit"];
00826
00827 $tmp3["tbInfo"]=$this->data["in"]["tbInfo"];
00828 $tmp3["action"]=
00829 $tmp3["pkey"][$tbSchema][$tbName][0]=$pkey;
00830
00831 $icons=(in_array('view',$this->actions[$tbSchema][$tbName])) ? ('<a href="'.$_SERVER["PHP_SELF"]."?".arr_to_url($tmp1).'"><img title="'.$this->STRINGS["submit"]["view"].'" target="'.$this->STRINGS["submit"]["view"].'" src="'.$this->conf["Images"]["view"].'" class="searchIcon"></a>') :'';
00832 $icons.=(in_array('edit',$this->actions[$tbSchema][$tbName])) ? ('<a href="'.$_SERVER["PHP_SELF"]."?".arr_to_url($tmp2).'"><img title="'.$this->STRINGS["submit"]["edit"].'" target="'.$this->STRINGS["submit"]["edit"].'" src="'.$this->conf["Images"]["edit"].'" class="searchIcon"></a>') :'';
00833 $icons.=(in_array('delete',$this->actions[$tbSchema][$tbName])) ? ('<a href="'.$_SERVER["PHP_SELF"]."?".arr_to_url($tmp).'" onclick="return confirm(\''.$this->STRINGS["confirm_rm_one"].'\');"><img title="'.$this->STRINGS["submit"]["delete"].'" target="'.$this->STRINGS["submit"]["delete"].'" src="'.$this->conf["Images"]["delete"].'" class="searchIcon"></a>') :'';
00834
00835
00836 $tableRef='';
00837 $i=true;
00838 if(is_array($refLink)){
00839 foreach($refLink as $num1=>$val1){
00840 if($i) $tableRef='<br>'.$this->STRINGS["element_subtables"];
00841 $i=false;
00842 $schemaTable="[".urlencode($val1["schema"])."][".urlencode($val1["table"])."]";
00843 $fieldUrl="[".urlencode($val1["field"])."][0]";
00844 $url=$_SERVER["PHP_SELF"]."?tbInfo".$schemaTable."[pkey]=&s".$schemaTable.$fieldUrl."=".urlencode($val1["val"])."&op".$schemaTable.$fieldUrl."=".urlencode("=")."&action=".urlencode($this->STRINGS["submit"]["search"]);
00845
00846 $tableRef.='<br><a href="'.$url.'" target="_blank">'.htmlentities($val1["table"]).'</a>';
00847 }
00848 }
00849
00850 $html='<div id="searchIcon">'.$checkBox.$icons.'</div>'.$tableRef;
00851 return $html;
00852 }
00853
00855
00857
00858 * \param $tbSchema schema name of the table
00859 * \param $tbName table name
00860 * \return file link
00861 */
00862 function export( $tbSchema, $tbName){
00863 $this->messages->add($this->STRINGS["exporting"]);
00864 $inherited=&$this->data["in"]["inherited"][$tbSchema][$tbName];
00865 $inherited=($inherited=='0')?0:1;
00866
00867 $query=$this->db->selectClause($tbSchema, $tbName, "*", $inherited);
00868 $query.=$this->search($tbSchema, $tbName, true);
00869
00870 $file=$this->db->export($query);
00871
00872 return $file;
00873 }
00874
00876
00878
00879 * \param $tbSchema schema name of the table
00880 * \param $tbName table name
00881 * \param $export=false
00882 * \return bidimensional array corespond to html table if $export=false, sql query if $export=true
00883 */
00884 function search( $tbSchema, $tbName, $export=false){
00885 $this->parse_data_in($this->data["in"]["s"],'search');
00886
00887 $value=&$this->data["in"]["s"][$tbSchema][$tbName];
00888 $op=&$this->data["in"]["op"][$tbSchema][$tbName];
00889 $logic=&$this->data["in"]["logic"][$tbSchema][$tbName];
00890 $mainLogic=&$this->data["in"]["mainLogic"][$tbSchema][$tbName];
00891 $offset=$this->data["in"]["offset"][$tbSchema][$tbName];
00892
00893 $limit=&$this->data["in"]["limit"][$tbSchema][$tbName];
00894 $record=$this->data["in"]["record"][$tbSchema][$tbName];
00895 $order=$this->data["in"]["order"][$tbSchema][$tbName];
00896 $inherited=&$this->data["in"]["inherited"][$tbSchema][$tbName];
00897
00898 $inherited=($inherited=='0')?0:1;
00900
00901
00902 $where_sql='';
00903 $first_levelClause=false;
00904 if(is_array($value))
00905 foreach($value as $field=>$val1){
00906
00907 $second_levelClause=false;
00908 if(is_array($value[$field]))
00909 foreach($value[$field] as $num=>$val){
00910
00911 if($val){
00912
00913 if(!$second_levelClause){
00914 if($first_levelClause){
00915 $where_sql.="$mainLogic (";
00916 }
00917 else{
00918 $where_sql.="(";
00919 }
00920 }
00921 else{
00922 $where_sql.=$logic[$field][$num];
00923 }
00924
00925 $where_sql.=$this->db->whereClause($tbSchema, $tbName, $field, $val, $op[$field][$num]);
00926
00927 $first_levelClause=$second_levelClause=true;
00928
00929 }
00930 else{
00931 array_unset($value[$field], $num);
00932 array_unset($op[$field], $num);
00933 array_unset($logic[$field], $num);
00934 }
00935 }
00936
00937 if(!$second_levelClause){
00938 array_unset($value[$field], $num);
00939 array_unset($op[$field], $num);
00940 array_unset($logic[$field], $num);
00941 }
00942 else{
00943 $where_sql.=') ';
00944 }
00945
00946 }
00947
00948 if($where_sql){
00949 $where_sql=' WHERE '.$where_sql;
00950 }
00951
00952
00954
00955
00956 if($record && $order){
00957 $order_sql=$this->db->orderClause($tbSchema, $tbName, $record, $order);
00958 }
00959 else{
00960 $order_sql='';
00961 }
00962
00963 if ($export) return($where_sql.$order_sql);
00964
00966
00967 if(!($limit>=1 && $limit<="100")){
00968 $limit=10;
00969 }
00970
00971
00972 $time=getmicrotime();
00973 $total=$this->db->numRecords($tbSchema, $tbName, $inherited, $where_sql);
00974 $time=round(getmicrotime()-$time,3);
00975 $this->messages->add("$total ".$this->STRINGS["found_in"]." $time ".$this->STRINGS["seconds"]);
00976
00977
00978 if($offset!=''){
00979 if($offset<=$total){
00980 $offset_sql=$offset-1;
00981 }
00982 else
00983 $offset_sql=$offset-$limit-1;
00984 }
00985 else{
00986 $offset=1;
00987 $offset_sql=0;
00988 }
00989
00990 $limit_sql=$this->db->limitClause($limit, $offset_sql);
00991
00993
00994
00995 $select_sql=$this->db->selectClause($tbSchema, $tbName, $this->pkey[$tbSchema][$tbName], $inherited);
00996 $query=$select_sql.$where_sql.$order_sql.$limit_sql;
00997
00998
00999 $tmp=transpose($this->db->select($query));
01000
01001
01002
01003 $pkeys["pkey"][$tbSchema][$tbName]=$tmp[ $this->pkey[$tbSchema][$tbName] ];
01004
01006
01007
01008
01009
01010 $x=$this->data["in"];
01011 $tmp=array("s"=>$x["s"],
01012 "op"=>$x["op"],
01013 "logic"=>$x["logic"],
01014 "mainLogic"=>$x["mainLogic"],
01015 "limit"=>$x["limit"],
01016
01017 "inherited"=>$x["inherited"],
01018 "action"=>$this->STRINGS["submit"]["search"],
01019 "offset"=>$x["offset"],
01020 "order"=>$x["order"],
01021 "record"=>$x["record"],
01022 "tbInfo"=>$x["tbInfo"],
01023 );
01024
01025
01026
01027
01028 $links=&$this->links[$tbSchema][$tbName];
01029
01030 $links["offset"]=$offset;
01031 $links["limit"]=$limit;
01032 $links["record"]=$record;
01033 $links["total"]=$total;
01034
01035
01036
01037 $links["search"]=$tmp;
01038 array_unset($links["search"], "action");
01039
01040
01041 $links["searchBar"]=$tmp;
01042 array_unset($links["searchBar"]["offset"][$tbSchema], $tbName);
01043
01044
01045 $attribs=&$this->db->attribs[$tbSchema][$tbName];
01046 foreach($attribs as $field=>$val){
01047 $links["order"][$field]=$tmp;
01048 $links["order"][$field]["offset"][$tbSchema][$tbName]=1;
01049 $links["order"][$field]["order"][$tbSchema][$tbName]=($record==$field && $order=='ASC')?'DESC':'ASC';
01050 $links["order"][$field]["record"][$tbSchema][$tbName]=$val["attname"];
01051 }
01052
01053
01054
01055 # echo array_to_html($links["order"]);
01056
01057
01058 return ($this->view_edit($tbSchema, $tbName, 'search', $pkeys));
01059
01060 }
01061
01062
01063
01065
01067
01068 * \note because of recursion of query, the fallowing functions must works also if is called more than once on the same data : so dont coud for example add a constant string to a parameter because in some cases it adds more times the same string....
01069 * \param $arr hash to parse
01070 * \param $when when is calles(search, edit, insert...)
01071 */
01072 function parse_data_in(&$arr,$when=''){
01073 if (is_array($arr)){
01074 foreach($arr as $tbSchema=> $arr1){
01075 if (is_array($arr1)){
01076 foreach($arr1 as $tbName=> $arr2){
01077 if (is_array($arr2)){
01078 foreach($arr2 as $num=> $record){
01079 if (is_array($record)){
01080 foreach($record as $field=> $val){
01081 if(is_array($val)){
01082 if(array_key_exists('year',$val) && array_key_exists('month',$val) && array_key_exists('day',$val)){
01083 if($val['year'] && $val['month'] && $val['day']){
01084 $arr[$tbSchema][$tbName][$num][$field]=$val['year'].'-'.$val['month'].'-'.$val['day'];
01085 if((array_key_exists('hour',$val) && array_key_exists('minute',$val)) && $val['hour']!='' && $val['minute']!='')
01086 $arr[$tbSchema][$tbName][$num][$field].=' '.$val['hour'].':'.$val['minute'];
01087
01088 }
01089 else{
01090 $arr[$tbSchema][$tbName][$num][$field]='';
01091 }
01092 }
01093 elseif(array_key_exists('hour',$val) && array_key_exists('minute',$val)){
01094 if($val['hour']!='' && $val['minute']!='')
01095 $arr[$tbSchema][$tbName][$num][$field]=$val['hour'].':'.$val['minute'];
01096
01097 }
01098 else{
01099
01100
01101 }
01102 }
01103 else{
01104
01105 $field1= ($when=='search') ? $num : $field;
01106 switch($this->db->attribs[$tbSchema][$tbName][$field1]["type"]){
01107 case 'float8':
01108
01109 $arr[$tbSchema][$tbName][$num][$field]=str_replace(",", ".", $val);
01110 break;
01111
01112
01113
01114
01115
01116
01117
01118
01119
01120
01121
01122
01123 break;
01124 default:
01125 $arr[$tbSchema][$tbName][$num][$field]=trim($arr[$tbSchema][$tbName][$num][$field]);
01126 }
01127 }
01128 }}
01129 }}
01130 }}
01131 }}
01132 }
01133
01135
01137
01138 * \param $when when is calles(search, edit, insert...)
01139 * \param $tbSchema schema of table
01140 * \param $tbName table name
01141 */
01142 function parse_data_out($when, $tbSchema, $tbName){
01143 $first=true;
01144 if(is_array($this->data["out"][$tbSchema][$tbName]))
01145 foreach($this->data["out"][$tbSchema][$tbName] as $num=>$record){
01146 foreach($this->data["out"][$tbSchema][$tbName][$num] as $field=>$val){
01147
01148
01149
01150 if($first) $this->auto_join($tbSchema, $tbName, $field, $when);
01151
01152 $form=&$this->form[$tbSchema][$tbName][$field];
01153 $attribs=&$this->db->attribs[$tbSchema][$tbName][$field];
01154 $src=&$this->src[$tbSchema][$tbName][$num][$field];
01155 $value='';
01156
01157
01158 switch($when){
01159 case 'preSearch':
01160 if($num!=0){
01161 $logic=($this->data["in"]["logic"][$tbSchema][$tbName][$field][$num]!='')? $this->data["in"]["logic"][$tbSchema][$tbName][$field][$num] : 'AND';
01162 $src= html_select("logic[$tbSchema][$tbName][$field][$num]", array("AND"=>$this->STRINGS["_and"],"OR"=>$this->STRINGS["_or"], "AND NOT"=>$this->STRINGS["and_not"], "OR NOT"=>$this->STRINGS["or_not"]), $logic,1,0,1);
01163 }
01164 $src.=$this->operator("op[$tbSchema][$tbName][$field][$num]", $form["type"],$this->data["in"]["op"][$tbSchema][$tbName][$field][$num]);
01165 $value=$this->data["in"]["s"][$tbSchema][$tbName][$field][$num];
01166 break;
01167 case 'new':
01168
01169 if($this->data["out"][$tbSchema][$tbName][$num][$field]=='' && $attribs['adsrc']!=''){
01170 $tmp =$this->db->select('SELECT '.$attribs['adsrc'].";");
01171 $value=current($tmp[0]);
01172 }
01173 else{
01174 $value=$this->data["out"][$tbSchema][$tbName][$num][$field];
01175 }
01176 break;
01177 default:
01178 $value=$this->data["out"][$tbSchema][$tbName][$num][$field];
01179 }
01180
01181
01182 switch($when){
01183 case 'view':
01184 case 'search':
01185 $src.=$this->view_sub($tbSchema, $tbName, $field, $num, $when, $value);
01186 break;
01187 default:
01188 $src.=$this->form_sub($tbSchema, $tbName, $field, $num, $when, $value);
01189 }
01190 $fieldNum++;
01191 }
01192 $first=false;
01193 $rowNum++;
01194 }
01195 }
01196
01197
01199
01201
01202 * \param $tbSchema schema name of the table
01203 * \param $tbName table name
01204 * \param $field column name
01205 * \param $num line number of current sets of records
01206 * \param $when when is calles(search, edit, insert...)
01207 * \param $def current value of the field
01208 */
01209 function form_sub($tbSchema, $tbName, $field, $num, $when, $def){
01210
01211 $name=($when=="preSearch")?'s':'val';
01212 $form=&$this->form[$tbSchema][$tbName][$field];
01213 $attribs=&$this->db->attribs[$tbSchema][$tbName][$field];
01214 $refTable=&$this->ref[$tbSchema][$tbName][$field];
01215
01216 if($when=="preSearch"){
01217 $name=$name."[$tbSchema][$tbName][$field][$num]";
01218 }
01219 else{
01220 $name=$name."[$tbSchema][$tbName][$num][$field]";
01221 }
01222
01223 $src='';
01224 switch($form["type"]){
01225 case 'bool':
01226 $src= html_select($name,array("t"=>$this->STRINGS["_yes"], "f"=>$this->STRINGS["_no"]), $def,1,0,0);
01227 break;
01228 case 'select':
01229 $src= html_select($name,$form["values"] ,$def,1,0,0) ;
01230 break;
01231 case 'popup':
01232 $refTbInfo='&tbInfo['.urlencode($tbSchema).']['.urlencode($this->ref[$tbSchema][$tbName][$field]["parent"]["table"]).']';
01233 $url=$form["ref_table"]["search"].$refTbInfo."[ref_field]=".urlencode($this->ref[$tbSchema][$tbName][$field]["parent"]["field"]).$refTbInfo."[ref_inputInput]=".urlencode($name).$refTbInfo."[ref_inputForm]=".$this->formName;
01234 $src='<input type="text" size="40" name="'.$name.'" value="'.$def.'" />';
01235 $src.='<input type="button" onclick="window.open(\''.$url.'\');" value="'.$this->STRINGS["select"].'" />';
01236
01237 break;
01238 case 'date':
01239 $src= date_select($name,$def,0) ;
01240 break;
01241 case 'time':
01242 $src= time_select($name,$def,0) ;
01243 break;
01244 case 'timestamptz':
01245
01246
01247 case 'timestamp':
01248 list( $date, $time)=split(' ', $def);
01249 $src.= date_select($name,$date,0) ;
01250 $src.= time_select($name,$time,0) ;
01251 break;
01252
01253 case 'file':
01254 case 'lobject':
01255 $linkName="<b>".$def[$attribs["file"]["name_col"]]."</b>";
01256 if($attribs["file"]["size_col"]) $linkName.="<br><b>Size:</b> ".$def[$attribs["file"]["size_col"]];
01257 if($attribs["file"]["mime_type_col"]) $linkName.="<br><b>Type:</b> ".$def[$attribs["file"]["mime_type_col"]];
01258
01259 $mimeType=$def[$attribs["file"]["mime_type_col"]];
01260 $link=$this->file_url($def[$field], $form["type"]=='lobject', $mimeType);
01261 $thumbnail=$this->file_url($def[$attribs["file"]["thumbnail_col"]], $form["type"]=='lobject', 'image/jpeg');
01262
01263 $src.=$this->file_view($linkName, $link, $thumbnail, $mimeType);
01264
01265 if($link && !$this->db->attribs[$tbSchema][$tbName][$field]["attnotnull"]) $src.='<br><input name="'.$name.'[del]" type="checkbox" value="1" />'.$this->STRINGS["delete"];
01266 $src.='<br><input type="file" size="10" name="'.$name.'[file]" />';
01267 break;
01268
01269
01270
01271
01272
01273
01274
01275
01276
01277
01278
01279
01280
01281
01282
01283
01284
01285
01286
01287 default:
01288 $tmp=(in_array($attribs["type"],array('float8', 'int4', 'int8', 'oid')) && $form["type"]!='select') ? 'style="text-align: right;" ' :'';
01289 $src='<input type="text" size="40" '.$tmp.'name="'.$name.'" value="'.($form["interpolate"]?htmlentities($def):$def).'" />';
01290 }
01291
01292 if($when=='edit'){
01293 $src.='<input type="hidden" name="old'."[$tbSchema][$tbName][$num][$field]".'" value="'.($form["interpolate"]?htmlentities($def):$def).'" />';
01294 }
01295
01296 return $src;
01297 }
01298
01299
01301
01303
01304 * \param $tbSchema schema name of the table
01305 * \param $tbName table name
01306 * \param $field column name
01307 * \param $num line number of current sets of records
01308 * \param $when when is calles(search, edit, insert...)
01309 * \param $def current value of the field
01310 */
01311 function view_sub($tbSchema, $tbName, $field, $num, $when, $def){
01312
01313 $name='val';
01314 $form=&$this->form[$tbSchema][$tbName][$field];
01315 $attribs=&$this->db->attribs[$tbSchema][$tbName][$field];
01316 $refTable=&$this->ref[$tbSchema][$tbName][$field];
01317
01318 $src='';
01319 switch($form["type"]){
01320 case 'bool':
01321 if ($def=='t'){$src=$this->STRINGS["_yes"];}
01322 elseif($def=='f'){$src=$this->STRINGS["_no"];}
01323 break;
01324
01325 case 'popup':
01326 $ref='['.urlencode($tbSchema).']['.urlencode($this->ref[$tbSchema][$tbName][$field]["parent"]["table"]).']';
01327 $pkeyField=urlencode($this->ref[$tbSchema][$tbName][$field]["parent"]["field"]);
01328 $pkey=urlencode($def);
01329 $url="tbInfo".$ref."[pkey]=$pkeyField&pkey".$ref."[0]=$pkey&action=".$this->STRINGS["submit"]["view"];
01330 $src='<a href="?'.$url.'" target="_blank" title="'.$this->STRINGS["references_view"].'">'.($form["interpolate"]?htmlentities($def):$def).'</a>';
01331 break;
01332 case 'file':
01333 case 'lobject':
01334 $linkName="<b>".$def[$attribs["file"]["name_col"]]."</b>";
01335 if($attribs["file"]["size_col"]) $linkName.="<br><b>".$this->STRINGS["size"].":</b> ".$def[$attribs["file"]["size_col"]];
01336 if($attribs["file"]["mime_type_col"]) $linkName.="<br><b>".$this->STRINGS["type"].":</b> ".$def[$attribs["file"]["mime_type_col"]];
01337
01338 $mimeType=$def[$attribs["file"]["mime_type_col"]];
01339 $link=$this->file_url($def[$field], $form["type"]=='lobject', $mimeType);
01340 $thumbnail=$this->file_url($def[$attribs["file"]["thumbnail_col"]], $form["type"]=='lobject', 'image/jpeg');
01341
01342 $src.=$this->file_view($linkName, $link, $thumbnail, $mimeType);
01343 break;
01344
01345
01346
01347
01348
01349
01350
01351
01352
01353
01354
01355
01356
01357
01358
01359
01360 default:
01361 if(in_array($attribs["type"],array('float8', 'int4', 'int8', 'oid')) && $form["type"]!='select'){
01362 $src='<div style="text-align: right; max-width: 150px;">'.$def.'</div>';
01363 }
01364 else{
01365 $src= $form["interpolate"] ? htmlentities($def) : $def;
01366 }
01367 }
01368 return $src;
01369 }
01370
01371
01373
01375
01376 * \note to call before alter_type if alter_type is called!!!
01377 * \param $tbSchema schema name of the table
01378 * \param $tbName table name
01379 * \param $field column name, or arrays of column names, or false if all the columns
01380 * \param $value=true if true view selected columns, if false hide they
01381 * \param $when when apply current changes (new, edit, search...) it could be an array
01382 */
01383 function alter_display($schema, $table, $fields=false, $value=true, $when=array("new", "edit", "search", "view", "preSearch", "export")){
01384 if(!is_array($when)) $when=array($when);
01385
01386 if($fields){
01387 if(!is_array($fields)) $fields=array($fields);
01388 }
01389 else{
01390 $fields=array_keys($this->db->attribs[$schema][$table]);
01391 }
01392
01393 foreach($fields as $key1=>$field){
01394 foreach($when as $key=>$val1){
01395 $this->db->attribs[$schema][$table][$field]["display"][$val1]=$value;
01396 }
01397 }
01398 }
01399
01400
01402
01404
01405 * \param $tbSchema='public' schema name
01406 * \param $tbName table name
01407 * \param $tbField field name
01408 * \param $type new type to change, eg "file" or "lobject" to store files directly into the database
01409 * \param $proprieties : array for file or lobject types: it could be contain key as:
01410 maxsize (in byte)
01411 type (it could be an array, and is matched with mime-type)
01412 mime_type_col (column of the same table in wich send mime_type of the file)
01413 name_col (column of the same table in wich send filename)
01414 size_col (column of the same table in wich send file size in byte)
01415 thumbnail_col (column of the same table of type oid in wich send thumbnail)
01416 thumbnail_size (size in pixel of thumbnail)
01417 exif_col (column of the same table in wich send exif info of the image)
01418 mime_type (if you would insert a unique type of file, and so mime_type_col and type are not necessary )
01419 * \param change_propriety if alter display array
01420 * \param where=false if change only form or attribs array or both
01421 */
01422
01423 function alter_type($tbSchema="public", $tbName, $tbField, $type, $proprieties=false, $change_propriety=true, $where=false){
01424
01425 if($where){
01426 $this->{$where}[$name]["type"]=$type;
01427 }
01428 else{
01429 $this->db->attribs[$tbSchema][$tbName][$tbField]["type"]=$type;
01430 $this->form[$tbSchema][$tbName][$tbField]["type"]=$type;
01431 }
01432
01433 if($proprieties["mime_type"]){
01434 $proprieties["type"]=$proprieties["mime_type"];
01435 }
01436
01437 $this->db->attribs[$tbSchema][$tbName][$tbField]["file"]=$proprieties;
01438
01439 $this->form[$tbSchema][$tbName][$tbField]["interpolate"]=false;
01440 if($proprieties["exif_col"]){
01441 $this->form[$tbSchema][$tbName][$proprieties["exif_col"]]["interpolate"]=false;
01442 }
01443
01444
01445 if($change_propriety){
01446 switch($type){
01447 case "file":
01448 case "lobject":
01449 $this->alter_display($tbSchema, $tbName,$tbField, true, array("new", "edit", "view","search"));
01450 $this->alter_display($tbSchema, $tbName,$tbField, false, "preSearch");
01451 break;
01452 case "pkey":
01453 $this->alter_display($tbSchema, $tbName,$tbField, false,array("new", "edit"));
01454 $this->alter_display($tbSchema, $tbName,$tbField, true,array("search", "view","preSearch"));
01455 break;
01456 }
01457
01458
01459 $xxx=array("thumbnail_col");
01460 foreach($xxx as $key=>$val){
01461 if($proprieties[$val]){
01462 $this->alter_display($tbSchema, $tbName, $proprieties[$val],false);
01463 }
01464 }
01465
01466 $xxx=array("exif_col", "size_col", "name_col", "mime_type_col");
01467 foreach($xxx as $key=>$val){
01468 if($proprieties[$val]){
01469 $this->alter_display($tbSchema, $tbName, $proprieties[$val],false,array("new", "edit"));
01470 $this->alter_display($tbSchema, $tbName, $proprieties[$val],true,array("search", "view","preSearch", "export"));
01471 }
01472 }
01473 }
01474 }
01475
01477
01479
01480 if(!$file) return '';
01481 if($lobject){
01482 return ('./lobjview.php?loid='.$file.'&mime-type='.$mimeType);
01483 }
01484 else{
01485 return $this->db->filesDir["remote"].$file;
01486 }
01487 }
01488
01490
01492
01493 if(!$link) return '';
01494 $src='';
01495 $class="thumbnail";
01496 if(!$thumbnail){
01497 $class="icon";
01498
01499 $file=$this->conf["MimeTypeIcons"]["remote"].$mimeType.".png";
01500 $unknow=$this->conf["MimeTypeIcons"]["remote"]."unknow.png";
01501 $fileLocal=$this->conf["MimeTypeIcons"]["local"].$mimeType.".png";
01502 $thumbnail=file_exists($fileLocal) ? $file : $unknow;
01503 }
01504 return ('<a class="file" href="'.$link. '" target="_blank"><img src="'.$thumbnail.'" class="'.$class.'" /><br>'.$linkName.'</a>');
01505 }
01506
01507
01509
01511
01512 * \param $tbSchema='public' schema name
01513 * \param $tbName table name
01514 * \return a hash with keys: text: text to display at the bottom of the table
01515 search_bar: search bar
01516 submit: submit buttons
01517 table: html table
01518 */
01519 function switch_actions($tbSchema='public', $tbName){
01520 $action=($this->data["in"]["action"])?$this->data["in"]["action"]:$this->STRINGS["submit"]["preSearch"];
01521 $tmp= array_search($action, $this->STRINGS["submit"]);
01522 $action=$tmp?$tmp:$this->STRINGS["submit"]["preSearch"];
01523
01524
01525 if(!in_array($action,$this->actions[$tbSchema][$tbName]) && !in_array('search',$this->actions[$tbSchema][$tbName])){
01526 echo "Permission denied !!!";
01527 exit;
01528 }
01529
01530 switch ($action){
01531
01532 case "preMultipleInsert":
01533 $resultArr["text"]=$this->STRINGS["Insert_"].html_select("numInsert[$tbSchema][$tbName]",range(1,25), 1,0,0,1).$this->STRINGS["new_records"];
01534 $resultArr["submit"]=$this->submit($tbSchema, $tbName, array('multipleInsert', 'preSearch'));
01535 break;
01536
01537 case "copy":
01538 $tb=$this->copy($tbSchema, $tbName);
01539
01540 if($this->single[$tbSchema][$tbName]==true){
01541 $hc=0; $hr=2; $tr=1;
01542 }
01543 else{
01544 $hc=2; $tr=0; $hr=0;
01545 }
01546
01547 $resultArr["table"]=array("table"=>$tb, "headerCol"=>$hc, "headerRow"=>$hr, "transpose"=>$tr);
01548 $resultArr["submit"]=$this->submit($tbSchema, $tbName, array('insert','preSearch'));
01549 break;
01550 case "new":
01551 case "multipleInsert":
01552
01553 $tb=$this->pre_insert($tbSchema, $tbName);
01554
01555 if($this->single[$tbSchema][$tbName]==true){
01556 $hc=0; $hr=2; $tr=1;
01557 }
01558 else{
01559 $hc=2; $tr=0; $hr=0;
01560 }
01561
01562 $resultArr["table"]=array("table"=>$tb, "headerCol"=>$hc, "headerRow"=>$hr, "transpose"=>$tr);
01563 $resultArr["submit"]=$this->submit($tbSchema, $tbName, array('insert','preSearch'));
01564 break;
01565
01566 case "view":
01567 $tb=$this->view($tbSchema, $tbName);
01568
01569 if($this->single[$tbSchema][$tbName]==true){
01570 $hc=0; $hr=2; $tr=1;
01571 }
01572 else{
01573 $hc=2; $tr=0; $hr=0;
01574 }
01575
01576 $resultArr["table"]=array("table"=>$tb, "headerCol"=>$hc, "headerRow"=>$hr, "transpose"=>$tr);
01577
01578 $resultArr["submit"]=$this->submit($tbSchema, $tbName, array('edit','delete', 'preSearch', 'copy', 'new', 'preMultipleInsert','export'));
01579 break;
01580
01581 case "insert":
01582 $this->insert($tbSchema, $tbName);
01583 list($resultArr["table"], $resultArr["submit"])=$this->switch_edit($tbSchema, $tbName);
01584 breaK;
01585
01586 case "save":
01587 $this->update($tbSchema, $tbName);
01588 list($resultArr["table"], $resultArr["submit"])=$this->switch_edit($tbSchema, $tbName);
01589 breaK;
01590
01591 case "edit":
01592 list($resultArr["table"], $resultArr["submit"])=$this->switch_edit($tbSchema, $tbName);
01593 break;
01594
01595
01596 case "delete":
01597 $this->delete($tbSchema, $tbName);
01598
01599
01600 $resultArr["submit"]=$this->submit($tbSchema, $tbName, array('preSearch','new', 'preMultipleInsert'));
01601
01602
01603
01604
01605 break;
01606
01607 case "export":
01608 $file=$this->export($tbSchema, $tbName);
01609 $resultArr["text"]="<a href=$file>$file</a>";
01610 $resultArr["submit"]=$this->submit($tbSchema, $tbName, array('newSearch', 'new', 'preMultipleInsert'));
01611 break;
01612
01613 case "search":
01614
01615 $resultArr["table"]=array("table"=>$this->search($tbSchema, $tbName), "headerCol"=>2, "headerRow"=>0, "noInterpolate"=>$intArr, "noIntType"=>0);
01616 $xxx=&$this->links[$tbSchema][$tbName];
01617
01618 $resultArr["search_bar"]=$this->search_bar($xxx['searchBar'], $this->conf["Images"]["previous"], $this->conf["Images"]["next"], $this->conf["Images"]["search"], $xxx["total"], $xxx["offset"], $xxx["limit"], 10, "offset[$tbSchema][$tbName]");
01619
01620 if(array_key_exists("ref_field",$this->data["in"]["tbInfo"][$tbSchema][$tbName])){
01621 $resultArr["submit"]=$this->submit($tbSchema, $tbName, array('modifySearch', 'newSearch'));
01622 }
01623 else{
01624 $resultArr["submit"]=$this->submit($tbSchema, $tbName, array('modifySearch', 'newSearch','edit', 'delete', 'copy', 'new', 'preMultipleInsert','export'));
01625 }
01626 break;
01627
01628 case "modifySearch":
01629 $resultArr["table"]=array("table"=>$this->pre_search_modify($tbSchema, $tbName), "headerCol"=>0, "headerRow"=>1, "transpose"=>1);
01630 $limit=$this->data["in"]["limit"][$tbSchema][$tbName];
01631 $mainLogic=$this->data["in"]["mainLogic"][$tbSchema][$tbName];
01632 $inherited=$this->data["in"]["inherited"][$tbSchema][$tbName];
01633 default:
01634 if($action!='modifySearch')
01635 $resultArr["table"]=array("table"=>$this->pre_search_new($tbSchema, $tbName), "headerCol"=>0, "headerRow"=>1, "transpose"=>1);
01636 if(array_key_exists("ref_field",$this->data["in"]["tbInfo"][$tbSchema][$tbName])){
01637 $resultArr["submit"]=$this->submit($tbSchema, $tbName, array('search'));
01638 }
01639 else{
01640 $resultArr["submit"]=$this->submit($tbSchema, $tbName, array('search','new', 'preMultipleInsert'));
01641 }
01642 $resultArr["text"]=$this->STRINGS["View_"].html_select("limit[$tbSchema][$tbName]",array(10, 25, 50, 100), $limit,0,0,1).$this->STRINGS["lines_a_page"];
01643 $resultArr["text"].='<br>'.$this->STRINGS["Search_"].html_select("mainLogic[$tbSchema][$tbName]",array("AND"=>$this->STRINGS["all_match"], 'OR'=>$this->STRINGS["one_match"]), $mainLogic,1,0,1).'.';
01644 $resultArr["text"].=$this->inh[$tbSchema][$tbName]?('<br><br><input type="radio" value="0" name="inherited['.$tbSchema.']['.$tbName.']" '.(($inherited=='0')?'checked':'').' />'.$this->STRINGS["no_inherited"].'<br><input type="radio" value="1" name="inherited['.$tbSchema.']['.$tbName.']" '.(($inherited!='0')?'checked':'').' />'.$this->STRINGS["inherited"]):'';
01645
01646
01647 }
01648
01649 $resultArr["text"].=arr_to_hidden($this->data["in"]["tbInfo"], 'tbInfo');
01650
01651
01652
01653
01654 if(is_array($resultArr["table"])){
01655 $resultArr["table"]= html_table($resultArr["table"]["table"],$resultArr["table"]["headerCol"],$resultArr["table"]["headerRow"],1,1,1,0, $resultArr["table"]["transpose"], false);
01656 }
01657
01658
01659 $submit='';
01660 foreach($resultArr["submit"] as $key=>$val){
01661 if($val==$resultArr["submit"]["delete"]){
01662 $submit.='<input type="submit" value="'.$val.'" name="action" onclick="return confirm(\''.$this->STRINGS["confirm_rm_more"].'\');" />';
01663 }
01664 else{
01665 $submit.='<input type="submit" value="'.$val.'" name="action" />';
01666 }
01667 }
01668 $resultArr["submit"]=$submit;
01669
01670 return $resultArr;
01671 }
01672
01674
01676
01677 $this->data["in"]["tbInfo"][$tbSchema][$tbName]["oldAction"]='edit';
01678 $this->data["in"]["tbInfo"][$tbSchema][$tbName]["oldSingle"]=$this->data["in"]["tbInfo"][$tbSchema][$tbName]["single"];
01679
01680 $tb=$this->edit($tbSchema, $tbName);
01681
01682 if($this->single[$tbSchema][$tbName]==true){
01683 $hc=0; $hr=2; $tr=1;
01684 }
01685 else{
01686 $hc=2; $tr=0; $hr=0;
01687 }
01688
01689 $resultArr["table"]=array("table"=>$tb, "headerCol"=>$hc, "headerRow"=>$hr, "transpose"=>$tr);
01690
01691 $resultArr["submit"]=$this->submit($tbSchema, $tbName, array('save','delete','view','preSearch', 'copy', 'new', 'preMultipleInsert','export'));
01692 return (array($resultArr["table"], $resultArr["submit"]));
01693 }
01694
01695
01697
01699
01700 * \return list(schemaname, tablename, primary key)
01701 */
01702 function get_table(){
01703 $tbInfo=$this->data["in"]["tbInfo"];
01704 if(!is_array($tbInfo)) user_error("Malformend POST or GET request!! tbInfo parameter absent!! ",E_USER_ERROR);
01705 $tbSchema=key($tbInfo);
01706 if(!is_array($tbInfo[$tbSchema])) user_error("Malformend POST or GET request!! tbInfo parameter absent!! ",E_USER_ERROR);
01707 $tbName=key($tbInfo[$tbSchema]);
01708 if(!is_array($tbInfo[$tbSchema][$tbName])) user_error("Malformend POST or GET request!! tbInfo parameter absent!! ",E_USER_ERROR);
01709 $tbPkey=$tbInfo[$tbSchema][$tbName]["pkey"];
01710 return array($tbSchema, $tbName, $tbPkey);
01711 }
01712
01713
01715
01717
01718
01719 $searchUrl=$_SERVER["PHP_SELF"].'?'.arr_to_url($searchUrl);
01720
01721 $numPages=($numPages===false)?10:$numPages;
01722 return search_bar($searchUrl, $previousUrl, $nextUrl, $otherUrl, $total, $offset, $limit, $numPages, $nameOffset);
01723 }
01724
01726
01728
01729 $ret=array();
01730 foreach($arr as $key=>$val){
01731 $val1=$val;
01732 switch($val){
01733 case 'new': $val1='insert';
01734 break;
01735 case 'save': $val1='edit';
01736 break;
01737 case 'newSearch': $val1='search';
01738 break;
01739 case 'preSearch': $val1='search';
01740 break;
01741 }
01742 if( in_array($val1,$this->actions[$tbSchema][$tbName])){
01743 $ret[]=$this->STRINGS["submit"][$val];
01744 }
01745 }
01746 return $ret;
01747 }
01748
01749 }
01750 ?>