00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 class CDBTree {
00078 var $db;
00079 var $table;
00080 var $id;
00081
00082
00083
00084
00085 var $left = 'cat_left';
00086 var $right = 'cat_right';
00087 var $level = 'cat_level';
00088
00089 var $qryParams = '';
00090 var $qryFields = '';
00091 var $qryTables = '';
00092 var $qryWhere = '';
00093 var $qryGroupBy = '';
00094 var $qryHaving = '';
00095 var $qryOrderBy = '';
00096 var $qryLimit = '';
00097 var $sqlNeedReset = true;
00098 var $sql;
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 function CDBTree(&$DB, $tableName, $itemId, $fieldNames=array()) {
00112 if(empty($tableName)) trigger_error("phpDbTree: Unknown table", E_USER_ERROR);
00113 if(empty($itemId)) trigger_error("phpDbTree: Unknown ID column", E_USER_ERROR);
00114 $this->db = $DB;
00115 $this->table = $tableName;
00116 $this->id = $itemId;
00117 if(is_array($fieldNames) && sizeof($fieldNames))
00118 foreach($fieldNames as $k => $v)
00119 $this->$k = $v;
00120 }
00121
00122
00123
00124
00125 function getElementInfo($ID) { return $this->getNodeInfo($ID); }
00126 function getNodeInfo($ID) {
00127 $this->sql = 'SELECT '.$this->left.','.$this->right.','.$this->level.' FROM '.$this->table.' WHERE '.$this->id.'=\''.$ID.'\'';
00128 if(($query=$this->db->query($this->sql)) && ($this->db->num_rows($query) == 1) && ($Data = $this->db->fetch_array($query)))
00129 return array((int)$Data[$this->left], (int)$Data[$this->right], (int)$Data[$this->level]);
00130 else trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
00131 }
00132
00133
00134
00135
00136 function clear($data=array()) {
00137
00138 if((!$this->db->query('TRUNCATE '.$this->table)) && (!$this->db->query('DELETE FROM '.$this->table))) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
00139
00140
00141 if(sizeof($data)) {
00142 $fld_names = implode(',', array_keys($data)).',';
00143 if(sizeof($data)) $fld_values = '\''.implode('\',\'', array_values($data)).'\',';
00144 }
00145 $fld_names .= $this->left.','.$this->right.','.$this->level;
00146 $fld_values .= '1,2,0';
00147
00148 // inserting new record
00149 $this->sql = 'INSERT INTO '.$this->table.'('.$fld_names.') VALUES('.$fld_values.')';
00150 if(!($this->db->query($this->sql))) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
00151
00152 return $this->db->insert_id();
00153 }
00154
00155 //************************************************************************
00156 // Updates a record
00157 // $ID : element ID
00158 // $data : array with data to update: array(<field_name> => <fields_value>)
00159 function update($ID, $data) {
00160 $sql_set = '';
00161 foreach($data as $k=>$v) $sql_set .= ','.$k.'=\''.addslashes($v).'\'';
00162 return $this->db->query('UPDATE '.$this->table.' SET '.substr($sql_set,1).' WHERE '.$this->id.'=\''.$ID.'\'');
00163 }
00164
00165
00166
00167
00168
00169
00170 function insert($ID, $data) {
00171 if(!(list($leftId, $rightId, $level) = $this->getNodeInfo($ID))) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
00172
00173
00174 if(sizeof($data)) {
00175 $fld_names = implode(',', array_keys($data)).',';
00176 $fld_values = '\''.implode('\',\'', array_values($data)).'\',';
00177 }
00178 $fld_names .= $this->left.','.$this->right.','.$this->level;
00179 $fld_values .= ($rightId).','.($rightId+1).','.($level+1);
00180
00181 // creating a place for the record being inserted
00182 if($ID) {
00183 $this->sql = 'UPDATE '.$this->table.' SET '
00184 . $this->left.'=IF('.$this->left.'>'.$rightId.','.$this->left.'+2,'.$this->left.'),'
00185 . $this->right.'=IF('.$this->right.'>='.$rightId.','.$this->right.'+2,'.$this->right.')'
00186 . 'WHERE '.$this->right.'>='.$rightId;
00187 if(!($this->db->query($this->sql))) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
00188 }
00189
00190 // inserting new record
00191 $this->sql = 'INSERT INTO '.$this->table.'('.$fld_names.') VALUES('.$fld_values.')';
00192 if(!($this->db->query($this->sql))) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
00193
00194 return $this->db->insert_id();
00195 }
00196
00197 //************************************************************************
00198 // Inserts a record into the table with nested sets
00199 // $ID : ID of the element after which (i.e. at the same level) the new element
00200 // is to be inserted
00201 // $data : array with data to be inserted: array(<field_name> => <field_value>)
00202 // Returns : true on success, or false on error
00203 function insertNear($ID, $data) {
00204 if(!(list($leftId, $rightId, $level) = $this->getNodeInfo($ID)))
00205 trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
00206
00207 // preparing data to be inserted
00208 if(sizeof($data)) {
00209 $fld_names = implode(',', array_keys($data)).',';
00210 $fld_values = '\''.implode('\',\'', array_values($data)).'\',';
00211 }
00212 $fld_names .= $this->left.','.$this->right.','.$this->level;
00213 $fld_values .= ($rightId+1).','.($rightId+2).','.($level);
00214
00215 // creating a place for the record being inserted
00216 if($ID) {
00217 $this->sql = 'UPDATE '.$this->table.' SET '
00218 .$this->left.'=IF('.$this->left.'>'.$rightId.','.$this->left.'+2,'.$this->left.'),'
00219 .$this->right.'=IF('.$this->right.'>'.$rightId.','.$this->right.'+2,'.$this->right.')'
00220 . 'WHERE '.$this->right.'>'.$rightId;
00221 if(!($this->db->query($this->sql))) trigger_error("phpDbTree error:".$this->db->error(), E_USER_ERROR);
00222 }
00223
00224 // inserting new record
00225 $this->sql = 'INSERT INTO '.$this->table.'('.$fld_names.') VALUES('.$fld_values.')';
00226 if(!($this->db->query($this->sql))) trigger_error("phpDbTree error:".$this->db->error(), E_USER_ERROR);
00227
00228 return $this->db->insert_id();
00229 }
00230
00231
00232 //************************************************************************
00233 // Assigns a node with all its children to another parent
00234 // $ID : node ID
00235 // $newParentID : ID of new parent node
00236 // Returns : false on error
00237 function moveAll($ID, $newParentId) {
00238 if(!(list($leftId, $rightId, $level) = $this->getNodeInfo($ID))) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
00239 if(!(list($leftIdP, $rightIdP, $levelP) = $this->getNodeInfo($newParentId))) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
00240 if($ID == $newParentId || $leftId == $leftIdP || ($leftIdP >= $leftId && $leftIdP <= $rightId)) return false;
00241
00242 // whether it is being moved upwards along the path
00243 if ($leftIdP < $leftId && $rightIdP > $rightId && $levelP < $level - 1 ) {
00244 $this->sql = 'UPDATE '.$this->table.' SET '
00245 . $this->level.'=IF('.$this->left.' BETWEEN '.$leftId.' AND '.$rightId.', '.$this->level.sprintf('%+d', -($level-1)+$levelP).', '.$this->level.'), '
00246 . $this->right.'=IF('.$this->right.' BETWEEN '.($rightId+1).' AND '.($rightIdP-1).', '.$this->right.'-'.($rightId-$leftId+1).', '
00247 .'IF('.$this->left.' BETWEEN '.($leftId).' AND '.($rightId).', '.$this->right.'+'.((($rightIdP-$rightId-$level+$levelP)/2)*2 + $level - $levelP - 1).', '.$this->right.')), '
00248 . $this->left.'=IF('.$this->left.' BETWEEN '.($rightId+1).' AND '.($rightIdP-1).', '.$this->left.'-'.($rightId-$leftId+1).', '
00249 .'IF('.$this->left.' BETWEEN '.$leftId.' AND '.($rightId).', '.$this->left.'+'.((($rightIdP-$rightId-$level+$levelP)/2)*2 + $level - $levelP - 1).', '.$this->left. ')) '
00250 . 'WHERE '.$this->left.' BETWEEN '.($leftIdP+1).' AND '.($rightIdP-1)
00251 ;
00252 } elseif($leftIdP < $leftId) {
00253 $this->sql = 'UPDATE '.$this->table.' SET '
00254 . $this->level.'=IF('.$this->left.' BETWEEN '.$leftId.' AND '.$rightId.', '.$this->level.sprintf('%+d', -($level-1)+$levelP).', '.$this->level.'), '
00255 . $this->left.'=IF('.$this->left.' BETWEEN '.$rightIdP.' AND '.($leftId-1).', '.$this->left.'+'.($rightId-$leftId+1).', '
00256 . 'IF('.$this->left.' BETWEEN '.$leftId.' AND '.$rightId.', '.$this->left.'-'.($leftId-$rightIdP).', '.$this->left.') '
00257 . '), '
00258 . $this->right.'=IF('.$this->right.' BETWEEN '.$rightIdP.' AND '.$leftId.', '.$this->right.'+'.($rightId-$leftId+1).', '
00259 . 'IF('.$this->right.' BETWEEN '.$leftId.' AND '.$rightId.', '.$this->right.'-'.($leftId-$rightIdP).', '.$this->right.') '
00260 . ') '
00261 . 'WHERE '.$this->left.' BETWEEN '.$leftIdP.' AND '.$rightId
00262 // !!! added this line (Maxim Matyukhin)
00263 .' OR '.$this->right.' BETWEEN '.$leftIdP.' AND '.$rightId
00264 ;
00265 } else {
00266 $this->sql = 'UPDATE '.$this->table.' SET '
00267 . $this->level.'=IF('.$this->left.' BETWEEN '.$leftId.' AND '.$rightId.', '.$this->level.sprintf('%+d', -($level-1)+$levelP).', '.$this->level.'), '
00268 . $this->left.'=IF('.$this->left.' BETWEEN '.$rightId.' AND '.$rightIdP.', '.$this->left.'-'.($rightId-$leftId+1).', '
00269 . 'IF('.$this->left.' BETWEEN '.$leftId.' AND '.$rightId.', '.$this->left.'+'.($rightIdP-1-$rightId).', '.$this->left.')'
00270 . '), '
00271 . $this->right.'=IF('.$this->right.' BETWEEN '.($rightId+1).' AND '.($rightIdP-1).', '.$this->right.'-'.($rightId-$leftId+1).', '
00272 . 'IF('.$this->right.' BETWEEN '.$leftId.' AND '.$rightId.', '.$this->right.'+'.($rightIdP-1-$rightId).', '.$this->right.') '
00273 . ') '
00274 . 'WHERE '.$this->left.' BETWEEN '.$leftId.' AND '.$rightIdP
00275 // !!! added this line (Maxim Matyukhin)
00276 . ' OR '.$this->right.' BETWEEN '.$leftId.' AND '.$rightIdP
00277 ;
00278 }
00279 return $this->db->query($this->sql) or trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
00280 }
00281
00282 //************************************************************************
00283 // Deletes a record wihtout deleting its children
00284 // $ID : an ID of the element to be deleted
00285 // Returns : true on success, or false on error
00286 function delete($ID) {
00287 if(!(list($leftId, $rightId, $level) = $this->getNodeInfo($ID))) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
00288
00289 // Deleting record
00290 $this->sql = 'DELETE FROM '.$this->table.' WHERE '.$this->id.'=\''.$ID.'\'';
00291 if(!$this->db->query($this->sql)) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
00292
00293
00294 $this->sql = 'UPDATE '.$this->table.' SET '
00295 . $this->left.'=IF('.$this->left.' BETWEEN '.$leftId.' AND '.$rightId.','.$this->left.'-1,'.$this->left.'),'
00296 . $this->right.'=IF('.$this->right.' BETWEEN '.$leftId.' AND '.$rightId.','.$this->right.'-1,'.$this->right.'),'
00297 . $this->level.'=IF('.$this->left.' BETWEEN '.$leftId.' AND '.$rightId.','.$this->level.'-1,'.$this->level.'),'
00298 . $this->left.'=IF('.$this->left.'>'.$rightId.','.$this->left.'-2,'.$this->left.'),'
00299 . $this->right.'=IF('.$this->right.'>'.$rightId.','.$this->right.'-2,'.$this->right.') '
00300 . 'WHERE '.$this->right.'>'.$leftId
00301 ;
00302 if(!$this->db->query($this->sql)) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
00303
00304 return true;
00305 }
00306
00307
00308
00309
00310
00311 function deleteAll($ID) {
00312 if(!(list($leftId, $rightId, $level) = $this->getNodeInfo($ID))) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
00313
00314
00315 $this->sql = 'DELETE FROM '.$this->table.' WHERE '.$this->left.' BETWEEN '.$leftId.' AND '.$rightId;
00316 if(!$this->db->query($this->sql)) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
00317
00318
00319 $deltaId = ($rightId - $leftId)+1;
00320 $this->sql = 'UPDATE '.$this->table.' SET '
00321 . $this->left.'=IF('.$this->left.'>'.$leftId.','.$this->left.'-'.$deltaId.','.$this->left.'),'
00322 . $this->right.'=IF('.$this->right.'>'.$leftId.','.$this->right.'-'.$deltaId.','.$this->right.') '
00323 . 'WHERE '.$this->right.'>'.$rightId
00324 ;
00325 if(!$this->db->query($this->sql)) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
00326
00327 return true;
00328 }
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340 function enumChildrenAll($ID) { return $this->enumChildren($ID, 1, 0); }
00341 function enumChildren($ID, $start_level=1, $end_level=1) {
00342 if($start_level < 0) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
00343
00344
00345 $whereSql1 = ' AND '.$this->table.'.'.$this->level;
00346 $whereSql2 = '_'.$this->table.'.'.$this->level.'+';
00347
00348 if(!$end_level) $whereSql = $whereSql1.'>='.$whereSql2.(int)$start_level;
00349 else {
00350 $whereSql = ($end_level <= $start_level)
00351 ? $whereSql1.'='.$whereSql2.(int)$start_level
00352 : ' AND '.$this->table.'.'.$this->level.' BETWEEN _'.$this->table.'.'.$this->level.'+'.(int)$start_level
00353 .' AND _'.$this->table.'.'.$this->level.'+'.(int)$end_level;
00354 }
00355
00356 $this->sql = $this->sqlComposeSelect(array(
00357 '',
00358 '',
00359 $this->table.' _'.$this->table.', '.$this->table,
00360 '_'.$this->table.'.'.$this->id.'=\''.$ID.'\''
00361 .' AND '.$this->table.'.'.$this->left.' BETWEEN _'.$this->table.'.'.$this->left.' AND _'.$this->table.'.'.$this->right
00362 .$whereSql
00363 ));
00364
00365 return $this->db->query($this->sql);
00366 }
00367
00368
00369
00370
00371
00372
00373 function enumPath($ID, $showRoot=false) {
00374 $this->sql = $this->sqlComposeSelect(array(
00375 '',
00376 '',
00377 $this->table.' _'.$this->table.', '.$this->table,
00378 '_'.$this->table.'.'.$this->id.'=\''.$ID.'\''
00379 .' AND _'.$this->table.'.'.$this->left.' BETWEEN '.$this->table.'.'.$this->left.' AND '.$this->table.'.'.$this->right
00380 .(($showRoot) ? '' : ' AND '.$this->table.'.'.$this->level.'>0'),
00381 '',
00382 '',
00383 $this->table.'.'.$this->left
00384 ));
00385
00386 return $this->db->query($this->sql);
00387 }
00388
00389
00390
00391
00392
00393
00394 function getParent($ID, $level=1) {
00395 if($level < 1) trigger_error("phpDbTree error: ".$this->db->error(), E_USER_ERROR);
00396
00397 $this->sql = $this->sqlComposeSelect(array(
00398 '',
00399 '',
00400 $this->table.' _'.$this->table.', '.$this->table,
00401 '_'.$this->table.'.'.$this->id.'=\''.$ID.'\''
00402 .' AND _'.$this->table.'.'.$this->left.' BETWEEN '.$this->table.'.'.$this->left.' AND '.$this->table.'.'.$this->right
00403 .' AND '.$this->table.'.'.$this->level.'=_'.$this->table.'.'.$this->level.'-'.(int)$level
00404 ));
00405
00406 return $this->db->query($this->sql);
00407 }
00408
00409
00410 function sqlReset() {
00411 $this->qryParams = ''; $this->qryFields = ''; $this->qryTables = '';
00412 $this->qryWhere = ''; $this->qryGroupBy = ''; $this->qryHaving = '';
00413 $this->qryOrderBy = ''; $this->qryLimit = '';
00414 return true;
00415 }
00416
00417
00418 function sqlSetReset($resetMode) { $this->sqlNeedReset = ($resetMode) ? true : false; }
00419
00420
00421 function sqlParams($param='') { return (empty($param)) ? $this->qryParams : $this->qryParams = $param; }
00422 function sqlFields($param='') { return (empty($param)) ? $this->qryFields : $this->qryFields = $param; }
00423 function sqlSelect($param='') { return $this->sqlFields($param); }
00424 function sqlTables($param='') { return (empty($param)) ? $this->qryTables : $this->qryTables = $param; }
00425 function sqlFrom($param='') { return $this->sqlTables($param); }
00426 function sqlWhere($param='') { return (empty($param)) ? $this->qryWhere : $this->qryWhere = $param; }
00427 function sqlGroupBy($param='') { return (empty($param)) ? $this->qryGroupBy : $this->qryGroupBy = $param; }
00428 function sqlHaving($param='') { return (empty($param)) ? $this->qryHaving : $this->qryHaving = $param; }
00429 function sqlOrderBy($param='') { return (empty($param)) ? $this->qryOrderBy : $this->qryOrderBy = $param; }
00430 function sqlLimit($param='') { return (empty($param)) ? $this->qryLimit : $this->qryLimit = $param; }
00431
00432
00433 function sqlComposeSelect($arSql) {
00434 $joinTypes = array('join'=>1, 'cross'=>1, 'inner'=>1, 'straight'=>1, 'left'=>1, 'natural'=>1, 'right'=>1);
00435
00436 $this->sql = 'SELECT '.$arSql[0].' ';
00437 if(!empty($this->qryParams)) $this->sql .= $this->sqlParams.' ';
00438
00439 if(empty($arSql[1]) && empty($this->qryFields)) $this->sql .= $this->table.'.'.$this->id;
00440 else {
00441 if(!empty($arSql[1])) $this->sql .= $arSql[1];
00442 if(!empty($this->qryFields)) $this->sql .= ((empty($arSql[1])) ? '' : ',') . $this->qryFields;
00443 }
00444 $this->sql .= ' FROM ';
00445 $isJoin = ($tblAr=explode(' ',trim($this->qryTables))) && ($joinTypes[strtolower($tblAr[0])]);
00446 if(empty($arSql[2]) && empty($this->qryTables)) $this->sql .= $this->table;
00447 else {
00448 if(!empty($arSql[2])) $this->sql .= $arSql[2];
00449 if(!empty($this->qryTables)) {
00450 if(!empty($arSql[2])) $this->sql .= (($isJoin)?' ':',');
00451 elseif($isJoin) $this->sql .= $this->table.' ';
00452 $this->sql .= $this->qryTables;
00453 }
00454 }
00455 if((!empty($arSql[3])) || (!empty($this->qryWhere))) {
00456 $this->sql .= ' WHERE ' . $arSql[3] . ' ';
00457 if(!empty($this->qryWhere)) $this->sql .= (empty($arSql[3])) ? $this->qryWhere : 'AND('.$this->qryWhere.')';
00458 }
00459 if((!empty($arSql[4])) || (!empty($this->qryGroupBy))) {
00460 $this->sql .= ' GROUP BY ' . $arSql[4] . ' ';
00461 if(!empty($this->qryGroupBy)) $this->sql .= (empty($arSql[4])) ? $this->qryGroupBy : ','.$this->qryGroupBy;
00462 }
00463 if((!empty($arSql[5])) || (!empty($this->qryHaving))) {
00464 $this->sql .= ' HAVING ' . $arSql[5] . ' ';
00465 if(!empty($this->qryHaving)) $this->sql .= (empty($arSql[5])) ? $this->qryHaving : 'AND('.$this->qryHaving.')';
00466 }
00467 if((!empty($arSql[6])) || (!empty($this->qryOrderBy))) {
00468 $this->sql .= ' ORDER BY ' . $arSql[6] . ' ';
00469 if(!empty($this->qryOrderBy)) $this->sql .= (empty($arSql[6])) ? $this->qryOrderBy : ','.$this->qryOrderBy;
00470 }
00471 if(!empty($arSql[7])) $this->sql .= ' LIMIT '.$arSql[7];
00472 elseif(!empty($this->qryLimit)) $this->sql .= ' LIMIT '.$this->qryLimit;
00473
00474 if($this->sqlNeedReset) $this->sqlReset();
00475
00476 return $this->sql;
00477 }
00478
00479 }
00480 ?>