navigation  interaction  search

 other resources

Changeset 437

Show
Ignore:
Timestamp:
10/17/08 13:33:47
Author:
nate
Message:
  • initial update for verification stuff.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/nathan/include/imports.php

    r433 r437  
    253253                                                 
    254254                                                //if valid, insert, if not, log error and don't insert. 
    255                                                 if($rowFieldNum == $fieldNum) 
    256                                                         $theid = $this->table->insertRecord($trimmedRowData); 
    257                                                 else 
     255                                                if($rowFieldNum == $fieldNum){ 
     256                                                        $verify = $this->table->verifyVariables($trimmedRowData); 
     257                                                        if(!count($verify["error"])) 
     258                                                                $theid = $this->table->insertRecord($trimmedRowData); 
     259                                                }else 
    258260                                                        $this->error .= '<li> incorrect amount of fields for line number '.$rowNum.'.</li>'; 
    259261                                                 
     
    268270                                                }else 
    269271                                                        $this->error .= '<li> failed insert for line number '.$rowNum.'.</li>'; 
     272                                                 
     273                                                foreach($verify["error"] as $error) 
     274                                                        $this->error .= '<li class="subError">'.$error.'</li>'; 
    270275                                                 
    271276                                                $rowNum++; 
  • branches/nathan/include/tables.php

    r427 r437  
    3939 
    4040 
    41 class phpbmsTable{ 
    42          
    43         var $db = NULL; 
    44         var $backurl = NULL; 
    45          
    46         // The table definition record id. 
    47         var $id=0; 
    48          
    49         var $fields = array(); 
    50  
    51         function phpbmsTable($db,$tabledefid = 0,$backurl = NULL){ 
    52          
    53                 if(is_object($db)) 
    54                         if(get_class($db)=="db") 
    55                                 $this->db = $db; 
    56                 if($this->db === NULL) 
    57                         $error = new appError(-800,"database object is required for parameter 1.","Initializing phpbmsTable Class"); 
    58  
    59                 $this->id = ((int) $tabledefid); 
    60                  
    61                 if($backurl == NULL) 
    62                         $this->backurl = APP_PATH."search.php?id=".$this->id; 
    63                 else  
    64                         $this->backurl = $backurl; 
    65                          
    66                 if(!$this->getTableInfo()) 
    67                         $error = new appError(-810,"Table definition not found for id ".$this->id,"Initializing phpbmsTable Class"); 
    68         } 
    69          
    70          
    71         function getTableInfo(){ 
    72                 $querystatement = "SELECT * FROM tabledefs WHERE id=".$this->id; 
    73                  
    74                 $queryresult = $this->db->query($querystatement); 
    75                  
    76                 if($this->db->numRows($queryresult)){ 
    77                         foreach($this->db->fetchArray($queryresult) as $key => $value) 
    78                                 $this->$key = $value; 
    79                          
    80                         $this->fields = $this->db->tableInfo($this->maintable); 
    81                          
    82                         return true;                                             
    83                 } else 
    84                         return false; 
    85         } 
    86          
    87         function getDefaultByType($fieldtype){ 
    88                         $default = NULL; 
    89                          
    90                         switch ($fieldtype){ 
    91                                 case "blob": 
    92                                 case "string": 
    93                                         $default = ""; 
    94                                 break; 
    95                                 case "real": 
    96                                 case "int": 
    97                                         $default = 0; 
    98                                 break; 
    99                                 case "date": 
    100                                         $default=dateToString(mktime(),"SQL"); 
    101                                 break; 
    102                                 case "time": 
    103                                         $default=timeToString(mktime(),"SQL"); 
    104                                 break; 
    105                                 case "year": 
    106                                         $default=strftime("%Y"); 
    107                                 break; 
    108                                 case "datetime": 
    109                                 case "timestamp": 
    110                                         $default = dateToString(mktime(),"SQL")." ".timeToString(mktime(),"24 Hour"); 
    111                                 break; 
    112                         } 
    113                          
    114                         return $default; 
    115                          
    116         } 
    117          
    118         function prepareFieldForSQL($value,$type,$flags){ 
    119                         switch ($type){ 
    120                                  
    121                                 case "blob": 
    122                                 case "string": 
    123                                         if($value === "" or $value === NULL){ 
    124                                                 if(strpos($flags,"not_null") === false) 
    125                                                         $value = NULL; 
    126                                                 else 
    127                                                         $value = "''"; 
    128                                         } else                                   
    129                                                 $value = "'".$value."'"; 
    130                                 break; 
    131                                  
    132                                 case "real": 
    133                                         if($value === "" or $value === NULL){ 
    134                                                 if(strpos($flags,"not_null") === false) 
    135                                                         $value = NULL; 
    136                                                 else 
    137                                                         $value = 0; 
    138                                         } else                                   
    139                                                 $value = (real) $value; 
    140                                 break; 
    141                                  
    142                                 case "int": 
    143                                         if($value === "" or $value === NULL){ 
    144                                                 if(strpos($flags,"not_null") === false) 
    145                                                         $value = NULL; 
    146                                                 else 
    147                                                         $value = 0; 
    148                                         } else 
    149                                                 $value = (int) $value; 
    150                                 break; 
    151                                  
    152                                 case "date": 
    153                                         if($value === "" or $value === NULL){ 
    154                                                 if(strpos($flags,"not_null") === false) 
    155                                                         $value = NULL; 
    156                                                 else 
    157                                                         $value = "'".dateToString(mktime(),"SQL")."'"; 
    158                                         } else 
    159                                                 $value = "'".sqlDateFromString($value)."'"; 
    160                                 break; 
    161  
    162                                 case "time": 
    163                                         if($value === "" or $value === NULL){ 
    164                                                 if(strpos($flags,"not_null") === false) 
    165                                                         $value = NULL; 
    166                                                 else 
    167                                                         $value = "'".timeToString(mktime(),"SQL")."'"; 
    168                                         } else 
    169                                                 $value = "'".sqlTimeFromString($value)."'"; 
    170                                 break; 
    171                                  
    172                                 case "year": 
    173                                         if($value === "" or $value === NULL) 
    174                                                 if(strpos($flags,"not_null") === false) 
    175                                                         $value = NULL; 
    176                                                 else 
    177                                                         $value = strftime("%Y"); 
    178                                 break; 
    179                                  
    180                                 case "datetime": 
    181                                 case "timestamp": 
    182                                         if($value === "" or $value === NULL){ 
    183                                                 if(strpos($flags,"not_null") === false) 
    184                                                         $value = NULL; 
    185                                                 else 
    186                                                         $value = "'".dateToString(mktime(),"SQL")." ".timeToString(mktime(),"24 Hour")."'"; 
    187                                         } else{ 
    188                                                 $datetimearray = explode(" ",$value); 
    189                                                 if(count($datetimearray) > 1){ 
    190                                                         $value = "'".sqlDateFromString($datetimearray[0])." ".sqlTimeFromString($datetimearray[1])."'"; 
    191                                                 } else  
    192                                                 $value = "'".$value."'"; 
    193                                         } 
    194                                 break; 
    195                                 case "password": 
    196                                         $value = "ENCODE('".$value."','".ENCRYPTION_SEED."')"; 
    197                                 break;           
    198                         }//end case 
    199  
    200  
    201                         if($value === NULL) 
    202                                 $value = "NULL"; 
    203                         return $value; 
    204         }//end method 
    205          
    206          
    207         function getDefaults(){ 
    208                 $therecord = array(); 
    209                  
    210                 foreach($this->fields as $fieldname => $thefield){ 
    211                         switch($fieldname){ 
    212                                 case "id": 
    213                                 case "modifiedby":                       
    214                                 case "modifieddate": 
    215                                         $therecord[$fieldname] = NULL; 
    216                                 break; 
    217                                  
    218                                 case "createdby": 
    219                                         $therecord["createdby"] = $_SESSION["userinfo"]["id"]; 
    220                                 break; 
    221                                                                  
    222                                 default: 
    223                                         if(strpos($thefield["flags"],"not_null") === false) 
    224                                                 $therecord[$fieldname] = NULL; 
    225                                         else { 
    226                                                 $therecord[$fieldname] = $this->getDefaultByType($thefield["type"]); 
    227                                         } 
    228                                 break; 
    229                         }//end switch 
    230                 }//end foreach 
    231                  
    232                 return $therecord; 
    233         } 
    234          
    235          
    236         function getRecord($id = 0){ 
    237                 $id = (int) $id; 
    238                  
    239                 $querystatement = "SELECT "; 
    240                                  
    241                 foreach($this->fields as $fieldname => $thefield){ 
    242                         if(isset($thefield["select"])) 
    243                                 $querystatement .= "(".$thefield["select"].") AS `".$fieldname."`, "; 
    244                         else 
    245                                 $querystatement .= "`".$fieldname."`, "; 
    246                 }//end foreach 
    247                 $querystatement = substr($querystatement, 0, strlen($querystatement)-2); 
    248                  
    249                 $querystatement .= " FROM `".$this->maintable."` WHERE `".$this->maintable."`.`id` = ".$id; 
    250                                  
    251                 $queryresult = $this->db->query($querystatement); 
    252                  
    253                 if($this->db->numRows($queryresult)) 
    254                         $therecord = $this->db->fetchArray($queryresult); 
    255                 else  
    256                         $therecord = $this-> getDefaults(); 
    257                  
    258                 return $therecord; 
    259         }//end getRecord function 
    260          
    261          
    262         function updateRecord($variables, $modifiedby = NULL){ 
    263                 $variables = addSlashesToArray($variables); 
    264                  
    265                 if($modifiedby === NULL) 
    266                         if(isset($_SESSION["userinfo"]["id"])) 
    267                                 $modifiedby = $_SESSION["userinfo"]["id"]; 
    268                         else 
    269                                 $error = new appError(-840,"Session Timed Out.","Creating New Record"); 
    270  
    271                 if(!isset($variables["id"])) 
    272                         $error = new appError(-820,"id not set","Updating Record"); 
    273                  
    274                 $updatestatement = "UPDATE `".$this->maintable."` SET "; 
    275                  
    276                 foreach($this->fields as $fieldname => $thefield){ 
    277                         if(!isset($thefield["select"])){ 
     41        class phpbmsTable{ 
     42                 
     43                var $db = NULL; 
     44                var $backurl = NULL; 
     45                var $verifyErrors = array(); 
     46                 
     47                // The table definition record id. 
     48                var $id=0; 
     49                 
     50                var $fields = array(); 
     51         
     52                function phpbmsTable($db,$tabledefid = 0,$backurl = NULL){ 
     53                 
     54                        if(is_object($db)) 
     55                                if(get_class($db)=="db") 
     56                                        $this->db = $db; 
     57                        if($this->db === NULL) 
     58                                $error = new appError(-800,"database object is required for parameter 1.","Initializing phpbmsTable Class"); 
     59         
     60                        $this->id = ((int) $tabledefid); 
     61                         
     62                        if($backurl == NULL) 
     63                                $this->backurl = APP_PATH."search.php?id=".$this->id; 
     64                        else  
     65                                $this->backurl = $backurl; 
     66                                 
     67                        if(!$this->getTableInfo()) 
     68                                $error = new appError(-810,"Table definition not found for id ".$this->id,"Initializing phpbmsTable Class"); 
     69                } 
     70                 
     71                 
     72                function getTableInfo(){ 
     73                        $querystatement = "SELECT * FROM tabledefs WHERE id=".$this->id; 
     74                         
     75                        $queryresult = $this->db->query($querystatement); 
     76                         
     77                        if($this->db->numRows($queryresult)){ 
     78                                foreach($this->db->fetchArray($queryresult) as $key => $value) 
     79                                        $this->$key = $value; 
     80                                 
     81                                $this->fields = $this->db->tableInfo($this->maintable); 
     82                                 
     83                                return true;                                             
     84                        } else 
     85                                return false; 
     86                } 
     87                 
     88                function getDefaultByType($fieldtype){ 
     89                                $default = NULL; 
     90                                 
     91                                switch ($fieldtype){ 
     92                                        case "blob": 
     93                                        case "string": 
     94                                                $default = ""; 
     95                                        break; 
     96                                        case "real": 
     97                                        case "int": 
     98                                                $default = 0; 
     99                                        break; 
     100                                        case "date": 
     101                                                $default=dateToString(mktime(),"SQL"); 
     102                                        break; 
     103                                        case "time": 
     104                                                $default=timeToString(mktime(),"SQL"); 
     105                                        break; 
     106                                        case "year": 
     107                                                $default=strftime("%Y"); 
     108                                        break; 
     109                                        case "datetime": 
     110                                        case "timestamp": 
     111                                                $default = dateToString(mktime(),"SQL")." ".timeToString(mktime(),"24 Hour"); 
     112                                        break; 
     113                                } 
     114                                 
     115                                return $default; 
     116                                 
     117                } 
     118                 
     119                function prepareFieldForSQL($value,$type,$flags){ 
     120                                switch ($type){ 
     121                                         
     122                                        case "blob": 
     123                                        case "string": 
     124                                                if($value === "" or $value === NULL){ 
     125                                                        if(strpos($flags,"not_null") === false) 
     126                                                                $value = NULL; 
     127                                                        else 
     128                                                                $value = "''"; 
     129                                                } else                                   
     130                                                        $value = "'".$value."'"; 
     131                                        break; 
     132                                         
     133                                        case "real": 
     134                                                if($value === "" or $value === NULL){ 
     135                                                        if(strpos($flags,"not_null") === false) 
     136                                                                $value = NULL; 
     137                                                        else 
     138                                                                $value = 0; 
     139                                                } else                                   
     140                                                        $value = (real) $value; 
     141                                        break; 
     142                                         
     143                                        case "int": 
     144                                                if($value === "" or $value === NULL){ 
     145                                                        if(strpos($flags,"not_null") === false) 
     146                                                                $value = NULL; 
     147                                                        else 
     148                                                                $value = 0; 
     149                                                } else 
     150                                                        $value = (int) $value; 
     151                                        break; 
     152                                         
     153                                        case "date": 
     154                                                if($value === "" or $value === NULL){ 
     155                                                        if(strpos($flags,"not_null") === false) 
     156                                                                $value = NULL; 
     157                                                        else 
     158                                                                $value = "'".dateToString(mktime(),"SQL")."'"; 
     159                                                } else 
     160                                                        $value = "'".sqlDateFromString($value)."'"; 
     161                                        break; 
     162         
     163                                        case "time": 
     164                                                if($value === "" or $value === NULL){ 
     165                                                        if(strpos($flags,"not_null") === false) 
     166                                                                $value = NULL; 
     167                                                        else 
     168                                                                $value = "'".timeToString(mktime(),"SQL")."'"; 
     169                                                } else 
     170                                                        $value = "'".sqlTimeFromString($value)."'"; 
     171                                        break; 
     172                                         
     173                                        case "year": 
     174                                                if($value === "" or $value === NULL) 
     175                                                        if(strpos($flags,"not_null") === false) 
     176                                                                $value = NULL; 
     177                                                        else 
     178                                                                $value = strftime("%Y"); 
     179                                        break; 
     180                                         
     181                                        case "datetime": 
     182                                        case "timestamp": 
     183                                                if($value === "" or $value === NULL){ 
     184                                                        if(strpos($flags,"not_null") === false) 
     185                                                                $value = NULL; 
     186                                                        else 
     187                                                                $value = "'".dateToString(mktime(),"SQL")." ".timeToString(mktime(),"24 Hour")."'"; 
     188                                                } else{ 
     189                                                        $datetimearray = explode(" ",$value); 
     190                                                        if(count($datetimearray) > 1){ 
     191                                                                $value = "'".sqlDateFromString($datetimearray[0])." ".sqlTimeFromString($datetimearray[1])."'"; 
     192                                                        } else  
     193                                                        $value = "'".$value."'"; 
     194                                                } 
     195                                        break; 
     196                                        case "password": 
     197                                                $value = "ENCODE('".$value."','".ENCRYPTION_SEED."')"; 
     198                                        break;           
     199                                }//end case 
     200         
     201         
     202                                if($value === NULL) 
     203                                        $value = "NULL"; 
     204                                return $value; 
     205                }//end method 
     206                 
     207                 
     208                function getDefaults(){ 
     209                        $therecord = array(); 
     210                         
     211                        foreach($this->fields as $fieldname => $thefield){ 
    278212                                switch($fieldname){ 
    279213                                        case "id": 
    280                                         case "creationdate": 
     214                                        case "modifiedby":                       
     215                                        case "modifieddate": 
     216                                                $therecord[$fieldname] = NULL; 
     217                                        break; 
     218                                         
    281219                                        case "createdby": 
    282                                         break; 
    283                                          
    284                                         case "modifiedby": 
    285                                                 $updatestatement .= "`modifiedby` = ".((int) $modifiedby).", "; 
    286                                         break; 
    287          
    288                                         case "modifieddate": 
    289                                                 $updatestatement .= "`modifieddate` = NOW(), "; 
    290                                         break; 
    291                                          
     220                                                $therecord["createdby"] = $_SESSION["userinfo"]["id"]; 
     221                                        break; 
     222                                                                         
    292223                                        default: 
    293                                                 if(!isset($variables[$fieldname]) && strpos($thefield["flags"],"not_null") !== false) 
    294                                                         $variables[$fieldname] = $this->getDefaultByType($thefield["type"],true); 
     224                                                if(strpos($thefield["flags"],"not_null") === false) 
     225                                                        $therecord[$fieldname] = NULL; 
     226                                                else { 
     227                                                        $therecord[$fieldname] = $this->getDefaultByType($thefield["type"]); 
     228                                                } 
     229                                        break; 
     230                                }//end switch 
     231                        }//end foreach 
     232                         
     233                        return $therecord; 
     234                } 
     235                 
     236                 
     237                function getRecord($id = 0){ 
     238                        $id = (int) $id; 
     239                         
     240                        $querystatement = "SELECT "; 
     241                                         
     242                        foreach($this->fields as $fieldname => $thefield){ 
     243                                if(isset($thefield["select"])) 
     244                                        $querystatement .= "(".$thefield["select"].") AS `".$fieldname."`, "; 
     245                                else 
     246                                        $querystatement .= "`".$fieldname."`, "; 
     247                        }//end foreach 
     248                        $querystatement = substr($querystatement, 0, strlen($querystatement)-2); 
     249                         
     250                        $querystatement .= " FROM `".$this->maintable."` WHERE `".$this->maintable."`.`id` = ".$id; 
     251                                         
     252                        $queryresult = $this->db->query($querystatement); 
     253                         
     254                        if($this->db->numRows($queryresult)) 
     255                                $therecord = $this->db->fetchArray($queryresult); 
     256                        else  
     257                                $therecord = $this-> getDefaults(); 
     258                         
     259                        return $therecord; 
     260                }//end getRecord function 
     261                 
     262                 
     263                function verifyVariables($variables){ 
     264                         
     265                        $thereturn["error"] = array(); 
     266                         
     267                        if(!isset($this->_verifyResponse)) 
     268                                $this->_verifyResponse = array(); 
     269                         
     270                        if(isset($variables["id"])){ 
     271                                if(is_numeric($variables["id"])){ 
     272                                        if($variables["id"] <= 0) 
     273                                                $this->verifyErrors[] = "The `id` field must be a positive number."; 
     274                                }elseif($variables["id"]) 
     275                                        $this->verifyErrors[] = "The `id` field must be numeric."; 
     276                        }//end if 
     277                         
     278                        if(isset($variables["inactive"])) 
     279                                if($variables["inactive"] && $variables["inactive"] !== 1) 
     280                                        $this->verifyErrors[] = "The `inactive` field must be a boolean (equivalent to 0 or exactly 1)."; 
     281                         
     282                        if(isset($variables["webenabled"])) 
     283                                if($variables["webenabled"] && $variables["webenabled"] !== 1) 
     284                                        $this->verifyErrors[] = "The `webenabled` field must be a boolean (equivalent to 0 or exactly 1)."; 
     285                         
     286                        if(count($this->verifyErrors)) 
     287                                $thereturn["error"] = $this->verifyErrors; 
     288                         
     289                        $this->verifyErrors = array(); 
     290                         
     291                        return $thereturn; 
     292                         
     293                }//end method --verifyVariables-- 
     294                 
     295                 
     296                function updateRecord($variables, $modifiedby = NULL){ 
     297                         
     298                        $variables = addSlashesToArray($variables); 
     299                         
     300                        if($modifiedby === NULL) 
     301                                if(isset($_SESSION["userinfo"]["id"])) 
     302                                        $modifiedby = $_SESSION["userinfo"]["id"]; 
     303                                else 
     304                                        $error = new appError(-840,"Session Timed Out.","Creating New Record"); 
     305         
     306                        if(!isset($variables["id"])) 
     307                                $error = new appError(-820,"id not set","Updating Record"); 
     308                         
     309                        $updatestatement = "UPDATE `".$this->maintable."` SET "; 
     310                         
     311                        foreach($this->fields as $fieldname => $thefield){ 
     312                                if(!isset($thefield["select"])){ 
     313                                        switch($fieldname){ 
     314                                                case "id": 
     315                                                case "creationdate": 
     316                                                case "createdby": 
     317                                                break; 
    295318                                                 
    296                                                 if(isset($variables[$fieldname])) 
    297                                                         $updatestatement .= "`".$fieldname."` = ".$this->prepareFieldForSQL($variables[$fieldname],$thefield["type"],$thefield["flags"]).", "; 
    298                                         break; 
    299                                 }//end switch field name 
    300                         }//end if 
    301                 }//end foreach 
    302                 $updatestatement = substr($updatestatement, 0, strlen($updatestatement)-2); 
    303                  
    304                 $updatestatement .= " WHERE `id`=".((int) $variables["id"]); 
    305  
    306                 $updateresult = $this->db->query($updatestatement); 
    307                  
    308                 return true; 
    309         } 
    310          
    311          
    312         function insertRecord($variables,$createdby = NULL, $overrideID = false){ 
    313          
    314                 if($createdby === NULL) 
    315                         if(isset($_SESSION["userinfo"]["id"])) 
    316                                 $createdby = $_SESSION["userinfo"]