[ Index ] |
|
Code source de SPIP Agora 1.4 |
1 <?php 2 require_once dirname(__FILE__)."/metier.php"; 3 require_once dirname(__FILE__)."/../../inc_meta.php"; 4 5 /** 6 * BD_mapping is a base class for mapping and article relation managements. 7 * @package BD 8 * @author Guillaume Grason <guillaume.grason@diplomatie.gouv.fr> 9 * @access public 10 */ 11 12 class BD_mapping { 13 14 // {{{ properties 15 16 17 /** 18 * Parameters instance used for PEAR::DB factory and 19 * Metier subclasses factories. 20 * 21 * @access private 22 */ 23 var $_dbParameters; 24 25 /** 26 * DB options array used for PEAR::DB options. 27 * 28 * @access private 29 */ 30 var $_dbOptions = false; 31 32 /** 33 * Article ID. 34 * @var int 35 * @access private 36 */ 37 var $_articleId; 38 39 /** 40 * Mapping ID. 41 * @var String 42 * @access private 43 */ 44 var $_mappingId; 45 46 // {{{ factory() 47 48 /** 49 * This method is a factory static method. It should be used to get any 50 * specific implementation instace of Article business data type. 51 * @param BD_parameters DB connection parameters 52 * @access public 53 */ 54 function &factory($dbParameters, $dbOptions = null) { 55 56 if(file_exists(dirname(__FILE__)."/".$dbParameters->_dbEngine."/mapping_".$dbParameters->_dbEngine.".php") == false) { 57 include_once(dirname(__FILE__)."/common/mapping_common.php"); 58 $classname = "BD_mapping_common"; 59 } 60 else { 61 include_once(dirname(__FILE__)."/".$dbParameters->_dbEngine."/mapping_".$dbParameters->_dbEngine.".php"); 62 $classname = "BD_mapping_".$dbParameters->_dbEngine; 63 } 64 65 if (!class_exists($classname)) { 66 return PEAR::raiseError("Cannot instanciate class $classname", null, 67 null, null, null, null, false); 68 } 69 70 $obj =& new $classname; 71 $result = $obj->setDbParameters($dbParameters); 72 if ($dbOptions != null) { 73 $obj->setDbOptions($dbOptions); 74 } 75 if (PEAR::isError($result)) { 76 return $result; 77 } else { 78 return $obj; 79 } 80 } 81 82 // }}} 83 84 // {{{ constructor 85 86 /** 87 * DB_article constructor. 88 * 89 * @access public 90 */ 91 92 function BD_mapping() { 93 } 94 95 // }}} 96 97 // {{{ getDbParameters() 98 99 /** 100 * Getter method to retreive the instance Parameters object 101 * 102 * @return DB parameters 103 * @access public 104 */ 105 106 function getDbParameters() { 107 return $this->_dbParameters; 108 } 109 110 // }}} 111 112 // {{{ setDbParameters() 113 114 /** 115 * Setter method to set the instance Parameters object 116 * 117 * @param $dbParameters new DB parameters 118 */ 119 120 function setDbParameters($dbParameters) { 121 if (strtolower(get_class($dbParameters)) == "bd_parameters") { 122 $this->_dbParameters = $dbParameters; 123 } else { 124 return PEAR::raiseError("Erreur! le parametre de la methode setDbParameters doit etre un objet de la hierarchie DB_parameters !", 125 null, null, null, null, null, false); 126 } 127 } 128 129 // }}} 130 131 // {{{ getDbOptions() 132 133 /** 134 * Getter method to retreive the instance Options array 135 * 136 * @return array 137 * @access public 138 */ 139 140 function getDbOptions() { 141 return $this->_dbOptions; 142 } 143 144 // }}} 145 146 // {{{ setDbOptions() 147 148 /** 149 * Setter method to set the instance Parameters object 150 * 151 * @param array array of PEAR::DB options 152 */ 153 154 function setDbOptions($dbOptions) { 155 $this->_dbOptions = $dbOptions; 156 } 157 158 // }}} 159 160 // {{{ _getDB() 161 162 /** 163 * Protected method used to get an PEAR::DB instance 164 * using the parameters stored in the _dbParameters field 165 * 166 * @return Newly creating PEAR::DB object 167 * according to the instance parameters 168 * @access private 169 */ 170 function &_getDB($dbParameters = null) { 171 require_once("PEAR.php"); 172 if($dbParameters == null) { 173 $DBManager = &DBManager::getDBManager($this->_dbParameters->getDSN(), $this->_dbOptions); 174 $db = $DBManager->getDB($this->_dbParameters->getDSN(), $this->_dbOptions); 175 } else { 176 $DBManager = &DBManager::getDBManager($dbParameters->getDSN(), $this->_dbOptions); 177 $db = $DBManager->getDB($dbParameters->getDSN(), $this->_dbOptions); 178 } 179 return $db; 180 } 181 182 // }}} 183 184 // {{{ getArticleId() 185 186 function getArticleId() { 187 return $this->_articleId; 188 } 189 190 // }}} 191 192 // {{{ setArticleId() 193 194 function setArticleId($articleId) { 195 $this->_articleId = $articleId; 196 } 197 198 // }}} 199 200 // {{{ getMappingId() 201 202 function getMappingId() { 203 return $this->_mappingId; 204 } 205 206 // }}} 207 208 // {{{ setMappingId() 209 210 function setMappingId($mappingId) { 211 $this->_mappingId = $mappingId; 212 } 213 214 // }}} 215 216 // {{{ _fetchData() 217 218 /** 219 * This method is used to fetch result set fields into the object fields 220 * @access private 221 * @param int $idArticle id of article to load 222 */ 223 224 function _fetchData($row) { 225 $this->setArticleId($row['id_article']); 226 $this->setMappingId($row['id_map']); 227 228 } 229 230 // }}} 231 232 // {{{ createEntry() 233 234 /** 235 * This method is used to create an entry in the database 236 * @access public 237 */ 238 239 function createEntry($articleId, $mappingId) { 240 $db = &$this->_getDB(); 241 if (DB::isError($db)) { 242 return PEAR::raiseError("[".get_class($this)." DB_mapping : createEntry()] ".$db->getMessage()."", null, 243 null, null, null, null, false); 244 } 245 $query = "INSERT INTO ".$GLOBALS['table_prefix']."_articles_mapping (id_map, id_article) VALUES ('$mappingId', '$articleId')"; 246 $result = $db->query($query); 247 if (DB::isError($result)) { 248 return PEAR::raiseError("[".get_class($this)." DB_mapping : createEntry()] ".$result->getMessage()."", null, 249 null, null, null, null, false); 250 } 251 } 252 253 // }}} 254 255 // {{{ deleteEntryByArticleIdAndMappingId() 256 257 /** 258 * This method is used to delete an entry from the database 259 * @access public 260 */ 261 262 function deleteEntryByArticleIdAndMappingId($articleId, $mappingId) { 263 $db = &$this->_getDB(); 264 if (DB::isError($db)) { 265 return PEAR::raiseError("[".get_class($this)." DB_mapping : deleteEntryByArticleIdAndMappingId()] ".$db->getMessage()."", null, 266 null, null, null, null, false); 267 } 268 $query = "DELETE FROM ".$GLOBALS['table_prefix']."_articles_mapping WHERE id_map = '$mappingId' AND id_article = '$articleId'"; 269 $result = $db->query($query); 270 if (DB::isError($result)) { 271 return PEAR::raiseError("[".get_class($this)." DB_mapping : deleteEntryByArticleIdAndMappingId()] ".$result->getMessage()."", null, 272 null, null, null, null, false); 273 } 274 } 275 276 // }}} 277 278 // {{{ deleteEntryByArticleId() 279 280 /** 281 * This method is used to delete multiples entries from the database 282 * @access public 283 */ 284 285 function deleteEntryByArticleId($articleId) { 286 $db = &$this->_getDB(); 287 if (DB::isError($db)) { 288 return PEAR::raiseError("[".get_class($this)." DB_mapping : deleteEntryByArticleId()] ".$db->getMessage()."", null, 289 null, null, null, null, false); 290 } 291 $query = "DELETE FROM ".$GLOBALS['table_prefix']."_articles_mapping id_article = '$articleId'"; 292 $result = $db->query($query); 293 if (DB::isError($result)) { 294 return PEAR::raiseError("[".get_class($this)." DB_mapping : deleteEntryByArticleId()] ".$result->getMessage()."", null, 295 null, null, null, null, false); 296 } 297 } 298 299 // }}} 300 301 // {{{ getMappings() 302 303 /** 304 * This method is used to list entries from the database corresponding to an article ID 305 * @access public 306 */ 307 308 function getMappings($articleId) { 309 $mappings = array(); 310 $db = &$this->_getDB(); 311 if (DB::isError($db)) { 312 return PEAR::raiseError("[".get_class($this)." DB_mapping : getMappings()] ".$db->getMessage()."", null, 313 null, null, null, null, false); 314 } 315 316 $query = "SELECT id_map, id_article FROM ".$GLOBALS['table_prefix']."_articles_mapping WHERE id_article=".$articleId; 317 $queryResult = $db->query($query); 318 319 if (DB::isError($queryResult)) { 320 return PEAR::raiseError("[".get_class($this)." DB_mapping : getMappings()] ".$queryResult->getMessage()."", null, 321 null, null, null, null, false); 322 } 323 324 while ($row = $queryResult->fetchRow()) { 325 $this->_fetchData($row); 326 $mappings[] = $this->getMappingId(); 327 } 328 $queryResult->free(); 329 return $mappings; 330 } 331 332 // }}} 333 334 // {{{ loadArticle() 335 336 /** 337 * This method is used to find the parent of a mapping from the database 338 * @access public 339 */ 340 341 function loadArticle($mappingId) { 342 $db = &$this->_getDB(); 343 if (DB::isError($db)) { 344 return PEAR::raiseError("[".get_class($this)." DB_mapping : loadArticle()] ".$db->getMessage()."", null, 345 null, null, null, null, false); 346 } 347 348 $query = "SELECT id_map, id_article FROM ".$GLOBALS['table_prefix']."_articles_mapping WHERE id_map=".$mappingId; 349 $queryResult = $db->query($query); 350 351 if (DB::isError($queryResult)) { 352 return PEAR::raiseError("[".get_class($this)." DB_mapping : loadArticle()] ".$queryResult->getMessage()."", null, 353 null, null, null, null, false); 354 } 355 356 $row = $queryResult->fetchRow(); 357 $this->_fetchData($row); 358 return $this->getArticleId(); 359 } 360 361 // }}} 362 363 } 364 365 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 14:40:03 2007 | par Balluche grâce à PHPXref 0.7 |