1<?php 2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */ 3 4/** 5 * ADT DB bridge base class 6 * 7 * @author Jörg Lützenkirchen <luetzenkirchen@leifos.com> 8 * @version $Id$ 9 * @ingroup ServicesADT 10 */ 11abstract class ilADTDBBridge 12{ 13 protected $adt; // [ilADT] 14 protected $table; // [string] 15 protected $id; // [string] 16 protected $primary = []; // [array] 17 18 /** 19 * Constructor 20 * 21 * @param ilADT $a_adt 22 * @return self 23 */ 24 public function __construct(ilADT $a_adt) 25 { 26 $this->setADT($a_adt); 27 } 28 29 // 30 // properties 31 // 32 33 /** 34 * Check if given ADT is valid 35 * 36 * :TODO: This could be avoided with type-specifc constructors 37 * :TODO: bridge base class? 38 * 39 * @param ilADT $a_adt 40 */ 41 abstract protected function isValidADT(ilADT $a_adt); 42 43 /** 44 * Set ADT 45 * 46 * @throws Exception 47 * @param ilADT $a_adt 48 */ 49 protected function setADT(ilADT $a_adt) 50 { 51 if (!$this->isValidADT($a_adt)) { 52 throw new Exception('ADTDBBridge Type mismatch.'); 53 } 54 55 $this->adt = $a_adt; 56 } 57 58 /** 59 * Get ADT 60 * 61 * @return ilADT 62 */ 63 public function getADT() 64 { 65 return $this->adt; 66 } 67 68 /** 69 * Set table name 70 * 71 * @param string $a_table 72 */ 73 public function setTable($a_table) 74 { 75 $this->table = (string) $a_table; 76 } 77 78 /** 79 * Get table name 80 * 81 * @return string 82 */ 83 public function getTable() 84 { 85 return $this->table; 86 } 87 88 /** 89 * Set element id (aka DB column[s] [prefix]) 90 * 91 * @param string $a_value 92 */ 93 public function setElementId($a_value) 94 { 95 $this->id = (string) $a_value; 96 } 97 98 /** 99 * Get element id 100 * 101 * @return string 102 */ 103 public function getElementId() 104 { 105 return $this->id; 106 } 107 108 /** 109 * Set primary fields (in MDB2 format) 110 * 111 * @param array $a_value 112 */ 113 public function setPrimary(array $a_value) 114 { 115 $this->primary = $a_value; 116 } 117 118 /** 119 * Get primary fields 120 * 121 * @return array 122 */ 123 public function getPrimary() 124 { 125 return $this->primary; 126 } 127 128 /** 129 * Convert primary keys array to sql string 130 * 131 * @see ilADTActiveRecord (:TODO: needed for multi) 132 * @return string 133 */ 134 public function buildPrimaryWhere() 135 { 136 global $DIC; 137 138 $ilDB = $DIC['ilDB']; 139 140 $sql = array(); 141 142 foreach ($this->primary as $field => $def) { 143 $sql[] = $field . "=" . $ilDB->quote($def[1], $def[0]); 144 } 145 146 return implode(" AND ", $sql); 147 } 148 149 150 // 151 // CRUD 152 // 153 154 /** 155 * Import DB values to ADT 156 * 157 * @param array $a_row 158 */ 159 abstract public function readRecord(array $a_row); 160 161 /** 162 * Prepare ADT values for insert 163 * 164 * @param array &$a_fields 165 */ 166 abstract public function prepareInsert(array &$a_fields); 167 168 /** 169 * After insert hook to enable sub-tables 170 */ 171 public function afterInsert() 172 { 173 } 174 175 /** 176 * Prepare ADT values for update 177 * 178 * @see prepareInsert() 179 * @param array &$a_fields 180 */ 181 public function prepareUpdate(array &$a_fields) 182 { 183 $this->prepareInsert($a_fields); 184 } 185 186 /** 187 * After update hook to enable sub-tables 188 */ 189 public function afterUpdate() 190 { 191 } 192 193 /** 194 * After delete hook to enable sub-tables 195 */ 196 public function afterDelete() 197 { 198 } 199} 200