File: 0.00.1a/core/entity/EntityManager.php (View as Code)

1: 2: /* ------------------------------------------------------------- 3: This file is part of FreeDESK 4: 5: FreeDESK is (C) Copyright 2012 David Cutting 6: 7: FreeDESK is free software: you can redistribute it and/or modify 8: it under the terms of the GNU General Public License as published by 9: the Free Software Foundation, either version 3 of the License, or 10: (at your option) any later version. 11: 12: FreeDESK is distributed in the hope that it will be useful, 13: but WITHOUT ANY WARRANTY; without even the implied warranty of 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15: GNU General Public License for more details. 16: 17: You should have received a copy of the GNU General Public License 18: along with FreeDESK. If not, see www.gnu.org/licenses 19: 20: For more information see www.purplepixie.org/freedesk/ 21: -------------------------------------------------------------- */ 22: 23: /** 24: * Entity Manager class 25: **/ 26: class EntityManager 27: { 28: /** 29: * FreeDESK instance 30: **/ 31: private $DESK = null; 32: 33: /** 34: * Constructor 35: * @param mixed &$freeDESK FreeDESK instance 36: **/ 37: function EntityManager(&$freeDESK) 38: { 39: $this->DESK = &$freeDESK; 40: } 41: 42: /** 43: * Create an entity 44: * @param string $entity Entity ID 45: * @return object Entity object 46: **/ 47: function Create($entity) 48: { 49: return EntityFactory::Create($this->DESK, $entity); 50: } 51: 52: /** 53: * Load an entity from the database (from a keyfield) 54: * @param string $entity Entity 55: * @param mixed $value Keyfield Value 56: * @return mixed Entity object on success or bool false on failure 57: **/ 58: function Load($entity, $value) 59: { 60: $table = $this->DESK->DataDictionary->GetTable($entity); 61: 62: if ($table === false) // no such entity in DD 63: return false; 64: 65: $keyfield = $table->keyfield; 66: 67: if ($keyfield == "") 68: return false; // no keyfield defined in DD 69: 70: $qb = new QueryBuilder(); 71: $qb->Add($keyfield, QueryType::Equal, $value); 72: 73: $q="SELECT * FROM ".$this->DESK->Database->Table($table->entity); 74: $q.=" WHERE ".$this->DESK->Database->Clause($qb); 75: 76: $r=$this->DESK->Database->Query($q); 77: 78: if ($row=$this->DESK->Database->FetchAssoc($r)) 79: { 80: $entity = $this->Create($entity); 81: $entity->LoadAssoc($row); 82: $this->DESK->Database->Free($r); 83: return $entity; 84: } 85: 86: return false; 87: } 88: 89: /** 90: * Save an entity 91: * @param object &$entity Entity object 92: * @return bool true on success or false on failure 93: **/ 94: function Save(&$entity) 95: { 96: $eid = $entity->GetEntity(); 97: $data = $entity->GetData(); 98: 99: $table = $this->DESK->DataDictionary->GetTable($eid); 100: 101: if ($table === false) 102: return false; 103: 104: $keyfield = $table->keyfield; 105: 106: if ($keyfield == "" || (!isset($data[$keyfield]))) 107: return false; 108: 109: $q="UPDATE ".$this->DESK->Database->Table($table->entity)." SET "; 110: 111: $first=true; 112: foreach($data as $key => $value) 113: { 114: if ($key != $keyfield) 115: { 116: if ($first) 117: $first=false; 118: else 119: $q.=","; 120: $q.=$this->DESK->Database->Field($key); 121: $q.="="; 122: $q.=$this->DESK->Database->SafeQuote($value); 123: } 124: } 125: 126: $q.=" WHERE ".$this->DESK->Database->Field($keyfield)."="; 127: $q.=$this->DESK->Database->SafeQuote($data[$keyfield]); 128: 129: $this->DESK->Database->Query($q); 130: 131: return true; 132: } 133: 134: /** 135: * Insert a (new) entity 136: * @param object &$entity Entity object 137: * @return bool true on success or false on failure 138: **/ 139: function Insert(&$entity) 140: { 141: $eid = $entity->GetEntity(); 142: $data = $entity->GetData(); 143: 144: $table = $this->DESK->DataDictionary->GetTable($eid); 145: 146: if ($table === false) 147: return false; 148: 149: $keyfield = $table->keyfield; 150: 151: $q="INSERT INTO ".$this->DESK->Database->Table($eid); 152: 153: $fieldlist = array(); 154: 155: foreach($data as $key => $value) 156: { 157: if (isset($_REQUEST[$key]) && 158: ( $key != $keyfield || $_REQUEST[$key]!="" ) ) 159: { 160: $fieldlist[]=$key; 161: } 162: } 163: 164: $q.="("; 165: $first=true; 166: foreach($fieldlist as $field) 167: { 168: if ($first) 169: $first=false; 170: else 171: $q.=","; 172: $q.=$this->DESK->Database->Field($field); 173: } 174: $q.=")"; 175: 176: $q.=" VALUES("; 177: $first=true; 178: foreach($fieldlist as $field) 179: { 180: if ($first) 181: $first=false; 182: else 183: $q.=","; 184: $q.=$this->DESK->Database->SafeQuote($_REQUEST[$field]); 185: } 186: $q.=")"; 187: 188: $this->DESK->Database->Query($q); 189: 190: return true; 191: } 192: 193: 194: } 195: ?> 196: