Viewing File core/entity/EntityManager.php of 0.01.1a
|
1: <?php 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: $q.=" WHERE ".$this->DESK->Database->Field($keyfield)."=".$this->DESK->Database->SafeQuote($value); 76: 77: //echo $q; 78: 79: $r=$this->DESK->Database->Query($q); 80: 81: if ($row=$this->DESK->Database->FetchAssoc($r)) 82: { 83: $entity = $this->Create($entity); 84: $entity->LoadAssoc($row); 85: $this->DESK->Database->Free($r); 86: return $entity; 87: } 88: 89: return false; 90: } 91: 92: /** 93: * Save an entity 94: * @param object &$entity Entity object 95: * @return bool true on success or false on failure 96: **/ 97: function Save(&$entity) 98: { 99: $eid = $entity->GetEntity(); 100: $data = $entity->GetData(); 101: 102: $table = $this->DESK->DataDictionary->GetTable($eid); 103: 104: if ($table === false) 105: return false; 106: 107: $keyfield = $table->keyfield; 108: 109: if ($keyfield == "" || (!isset($data[$keyfield]))) 110: return false; 111: 112: $q="UPDATE ".$this->DESK->Database->Table($table->entity)." SET "; 113: 114: $first=true; 115: foreach($data as $key => $value) 116: { 117: if ($key != $keyfield) 118: { 119: if ($first) 120: $first=false; 121: else 122: $q.=","; 123: $q.=$this->DESK->Database->Field($key); 124: $q.="="; 125: $q.=$this->DESK->Database->SafeQuote($value); 126: } 127: } 128: 129: $q.=" WHERE ".$this->DESK->Database->Field($keyfield)."="; 130: $q.=$this->DESK->Database->SafeQuote($data[$keyfield]); 131: 132: $this->DESK->Database->Query($q); 133: 134: return true; 135: } 136: 137: /** 138: * Insert a (new) entity 139: * @param object &$entity Entity object 140: * @return bool true on success or false on failure 141: **/ 142: function Insert(&$entity) 143: { 144: $eid = $entity->GetEntity(); 145: $data = $entity->GetData(); 146: 147: $table = $this->DESK->DataDictionary->GetTable($eid); 148: 149: if ($table === false) 150: return false; 151: 152: $keyfield = $table->keyfield; 153: 154: $q="INSERT INTO ".$this->DESK->Database->Table($eid); 155: 156: $fieldlist = array(); 157: 158: foreach($data as $key => $value) 159: { 160: if (isset($_REQUEST[$key]) && 161: ( $key != $keyfield || $_REQUEST[$key]!="" ) ) 162: { 163: $fieldlist[]=$key; 164: } 165: } 166: 167: $q.="("; 168: $first=true; 169: foreach($fieldlist as $field) 170: { 171: if ($first) 172: $first=false; 173: else 174: $q.=","; 175: $q.=$this->DESK->Database->Field($field); 176: } 177: $q.=")"; 178: 179: $q.=" VALUES("; 180: $first=true; 181: foreach($fieldlist as $field) 182: { 183: if ($first) 184: $first=false; 185: else 186: $q.=","; 187: $q.=$this->DESK->Database->SafeQuote($_REQUEST[$field]); 188: } 189: $q.=")"; 190: 191: $this->DESK->Database->Query($q); 192: 193: return true; 194: } 195: 196: 197: } 198: ?> 199: