Changeset 485 for trunk/phpbms/modules/bms/include/addresstorecord.php
- Timestamp:
- 04/07/09 11:44:18 (3 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
trunk/phpbms/modules/bms/include/addresstorecord.php
r427 r485 39 39 if(class_exists("addresses")){ 40 40 class addresstorecord extends addresses{ 41 41 42 var $availableTabledefIDs = array(); 43 var $availableTabledefNames = array(); 42 44 43 45 function createAddressToRecord($variables, $addressid, $createdby = NULL){ 44 46 // This functions adds the addresstorecord record tying the client and address 45 47 // records. 46 48 47 49 if(!$createdby && isset($_SESSION["userinfo"]["id"])) 48 50 $createdby = $_SESSION["userinfo"]["id"]; 49 51 50 52 $insertstatement = " 51 53 INSERT INTO … … 70 72 NOW(), 71 73 ".((int) $createdby).", 72 NOW() 74 NOW() 73 75 )"; 74 76 75 77 $insertresult = $this->db->query($insertstatement); 76 78 77 79 if($insertresult) 78 80 return $this->db->insertId(); 79 81 else 80 82 return false; 81 83 82 84 }//end method - createAddressToRecord 83 85 84 86 // CLASS OVERRIDES =============================================== 85 87 // =============================================================== 86 88 function getRecord($id){ 87 89 $id = (int) $id; 88 90 89 91 $querystatement = " 90 92 SELECT 91 * 92 FROM 93 addresstorecord 94 WHERE 95 id = ".$id; 96 93 * 94 FROM 95 addresstorecord 96 WHERE 97 id = ".$id; 98 97 99 $queryresult = $this->db->query($querystatement); 98 if($this->db->numRows($queryresult)) { 99 100 if($this->db->numRows($queryresult)) { 101 100 102 $therecord = $this->db->fetchArray($queryresult); 101 103 $addressrecord = parent::getRecord($therecord["addressid"]); 102 104 103 105 unset($addressrecord["id"], $addressrecord["createdby"], $addressrecord["creationdate"], $addressrecord["modifiedby"], $addressrecord["modifieddate"]); 104 106 105 107 $therecord = array_merge($addressrecord, $therecord); 106 107 } else 108 109 } else 108 110 $therecord = $this->getDefaults(); 109 111 110 112 return $therecord; 111 113 112 114 }//end method - getRecord 113 114 115 116 115 117 function getDefaults(){ 116 118 117 119 $therecord = parent::getDefaults(); 118 120 119 121 $therecord["addressid"] = 0; 120 122 $therecord["tabledefid"] = 0; 121 123 $therecord["recordid"] = NULL; 122 124 $therecord["defaultshipto"] = 0; 123 $therecord["primary"] = 0; 124 125 return $therecord; 126 125 $therecord["primary"] = 0; 126 127 return $therecord; 128 127 129 }//end method - getDefauls 128 129 130 131 function formatVariables($variables){ 132 130 131 132 function populateTabledefArrays(){ 133 134 $this->availableTabledefIDs = array(); 135 $this->availableTabledefNames = array(); 136 137 $querystatement = " 138 SELECT 139 `id`, 140 `maintable` 141 FROM 142 `tabledefs`; 143 "; 144 145 $queryresult = $this->db->query($querystatement); 146 147 if($this->db->numRows($queryresult)){ 148 while($therecord = $this->db->fetchArray($queryresult)){ 149 $this->availableTabledefIDs[] = $therecord["id"]; 150 $this->availableTabledefNames[$therecord["id"]] = $therecord["maintable"]; 151 }//end while 152 }else{ 153 $this->availableTabledefIDs[] = "none"; 154 $this->availableTabledefNames[] = "none"; 155 }//end if 156 157 }//end method --populateTabledefArray-- 158 159 160 function checkRecordID($recordid, $tablename){ 161 162 $recordid = ((int) $recordid); 163 $tablename = addslashes($tablename); 164 165 $querystatement = " 166 SELECT 167 `id` 168 FROM 169 `".$tablename."` 170 WHERE 171 `id` = '".$recordid."'; 172 "; 173 174 $queryresult = $this->db->query($querystatement); 175 176 return $this->db->numRows($queryresult); 177 178 }//end method --checkRecordID-- 179 180 181 function verifyVariables($variables){ 182 //POSSIBLY CHANGE>>>> NOT FINISHIED >>>> 183 //Check tabledefs 184 if(isset($variables["tabledefid"])){ 185 186 if(is_numeric($variables["tabledefid"])){ 187 188 if(!count($this->availableTabledefIDs) || !count($this->availableTabledefNames)) 189 $this->populateTabledefArrays(); 190 191 if(in_array($variables["tabledefid"], $this->availableTabledefIDs)){ 192 193 //check recordid 194 if(isset($variables["recordid"])){ 195 196 if((int) $variables["recordid"] > 0){ 197 198 if(!count($this->availableTabledefIDs) || !count($this->availableTabledefNames)) 199 $this->populateTabledefArrays(); 200 201 if(!$this->checkRecordID($variables["recordid"], $this->availableTabledefNames[$variables["tabledefid"]])) 202 $this->verifyErrors[] = "The `recordid` field does match an id number in ".$this->availableTabledefNames[$variables["tabledefid"]]."."; 203 204 }else 205 $this->verifyErrors[] = "The `recordid` field must be a positive number."; 206 207 }else 208 $this->verifyErrors[] = "The `recordid` field must be set."; 209 210 }else 211 $this->verifyErrors[] = "The `tabledefid` field does not give an existing/acceptable table definition id number."; 212 213 }else 214 $this->verifyErrors[] = "The `tabledefid` field must be numeric."; 215 216 }else 217 $this->verifyErrors[] = "The `tabledefid` field must be set."; 218 219 return parent::verifyVariables($variables); 220 221 }//end method --verifyVariables-- 222 223 224 function prepareVariables($variables){ 225 133 226 if(!isset($variables['primary'])) 134 227 $variables['primary'] = 0; … … 136 229 if(!isset($variables['defaultshipto'])) 137 230 $variables['defaultshipto'] = 0; 138 231 139 232 if(!isset($variables["existingaddressid"])) 140 233 $variables["existingaddressid"] = false; 141 142 return $variables; 143 234 235 if(isset($variables["id"])) 236 if($variables["id"]){// if update 237 238 $variables["id"] = $variables["addressid"]; 239 240 }//end if 241 242 return $variables; 243 144 244 }//end function 145 245 146 246 147 148 function updateRecord($variables, $modifiedby = NULL){149 150 $variables = $this->formatVariables($variables);151 152 $variables["id"] = $variables["addressid"];153 154 $thereturn = parent::updateRecord($variables, $modifiedby);155 156 return $thereturn;157 158 }//end method159 160 161 247 function insertRecord($variables, $createdby = NULL){ 162 248 163 $variables = $this->formatVariables($variables);249 //$variables = $this->prepareVariables($variables); 164 250 165 251 if($variables["existingaddressid"]){ 166 252 167 253 $this->createAddressToRecord($variables, $variables["existingaddressid"], $createdby); 168 254 169 255 $newAtrID = $variables["existingaddressid"]; 170 256 171 257 } else { 172 258 173 259 $newid = parent::insertRecord($variables, $createdby); 174 260 175 261 //create the addresstorecord 176 262 $newAtrID = $this->createAddressToRecord($variables, $newid, $createdby); 177 263 178 264 }//endif - existingaddressid 179 265 180 266 return $newAtrID; 181 267 182 268 }//end method 183 269 184 270 }//end class 185 271 }//end if … … 192 278 193 279 $whereclause = $this->buildWhereClause(); 194 280 195 281 //We need to itterate trhough each record 196 // to check for cross-record addresses 282 // to check for cross-record addresses 197 283 $querystatement = " 198 284 SELECT … … 201 287 addresstorecord 202 288 WHERE ".$whereclause; 203 289 204 290 $queryresult = $this->db->query($querystatement); 205 291 206 292 $removedCount = 0; 207 293 $deletedCount = 0; 208 294 $beenMarked = false; 209 295 210 296 while($therecord = $this->db->fetchArray($queryresult)){ 211 297 … … 213 299 // cannot be removed 214 300 if(!$therecord["primary"] && !$therecord["defaultshipto"]){ 215 301 216 302 //look up address to see if it is associated with other records 217 303 $querystatement = " … … 224 310 AND tabledefid = ".$therecord["tabledefid"]." 225 311 AND recordid != ".$therecord["recordid"]; 226 312 227 313 $lookupResult = $this->db->query($querystatement); 228 314 229 315 if(!$this->db->numRows($lookupResult)){ 230 316 231 317 //we can safely delete the address (no other associations) 232 318 $deletestatement = " … … 237 323 238 324 $this->db->query($deletestatement); 239 325 240 326 $deletedCount++; 241 327 $removedCount--; 242 328 243 329 }//end if - numRows 244 330 245 331 //remove the connecting record 246 332 $deletestatement = " … … 249 335 WHERE 250 336 id =".$therecord["id"]; 251 337 252 338 $this->db->query($deletestatement); 253 339 254 340 $removedCount++; 255 341 256 342 } else { 257 258 $beenMarked = true; 259 343 344 $beenMarked = true; 345 260 346 }//endif - primary or defaultshipto 261 347 262 348 }//endwhile - fetchArray 263 349 264 350 //next, craft the response 265 351 266 352 $message = ""; 267 268 if($removedCount){ 269 353 354 if($removedCount){ 355 270 356 $message .= $removedCount." address"; 271 357 272 358 if($removedCount > 1) 273 359 $message .= "es"; 274 360 275 361 $message .= " dissociated from record. "; 276 362 277 363 }//endif removedCount 278 279 if($deletedCount){ 280 364 365 if($deletedCount){ 366 281 367 $message .= $deletedCount." address"; 282 368 283 369 if($deletedCount > 1) 284 370 $message .= "es"; 285 371 286 372 $message .= " deleted. "; 287 373 288 374 }//endif removedCount 289 375 … … 294 380 $message .= "(Addresses marked primary or default ship to cannot be removed.)"; 295 381 return $message; 296 382 297 383 }//end method - delete 298 384 299 385 300 386 function markPrimary(){ 301 387 302 388 return $this->_markAs("primary")." primary address."; 303 389 304 390 }//end method - markPrimary 305 306 391 392 307 393 function markDefaultShipTo(){ 308 394 … … 323 409 WHERE 324 410 id =".((int) $this->idsArray[0]); 325 411 326 412 $relatedInfo = $this->db->fetchArray($this->db->query($querystatement)); 327 413 328 414 //Next, mark all addresses associated with record as false 329 415 $updatestatement = " … … 335 421 tabledefid = ".$relatedInfo["tabledefid"]." 336 422 AND recordid = ".$relatedInfo["recordid"]; 337 423 338 424 $this->db->query($updatestatement); 339 425 340 426 //Finally, mark the first record. 341 427 $updatestatement = " … … 346 432 WHERE 347 433 id = ".((int) $this->idsArray[0]); 348 434 349 435 $this->db->query($updatestatement); 350 436 … … 357 443 358 444 return $message; 359 445 360 446 }//end method _markAs 361 447