1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
|
<?php
/*
* @version $Id: db.function.php 3829 2006-08-25 21:04:15Z moyo $
-------------------------------------------------------------------------
GLPI - Gestionnaire Libre de Parc Informatique
Copyright (C) 2003-2006 by the INDEPNET Development Team.
http://indepnet.net/ http://glpi-project.org
-------------------------------------------------------------------------
LICENSE
This file is part of GLPI.
GLPI is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
GLPI is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GLPI; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
--------------------------------------------------------------------------
*/
// ----------------------------------------------------------------------
// Original Author of file:
// Purpose of file:
// ----------------------------------------------------------------------
/**
* Count the number of elements in a table.
*
* @param $table table name
*
* return int nb of elements in table
*/
function countElementsInTable($table){
global $db;
$query="SELECT count(*) AS cpt
FROM $table";
$result=$db->query($query);
$ligne = $db->fetch_array($result);
return $ligne['cpt'];
}
/**
* Get the Name of the element of a Dropdown Tree table
*
* @param $table : Dropdown Tree table
* @param $ID : ID of the element
* @param $withcomments : 1 if you want to give the array with the comments
* @return string : name of the element
* @see getTreeValueCompleteName
*/
function getTreeLeafValueName($table,$ID,$withcomments=0)
{
global $db;
$query = "SELECT *
FROM $table
WHERE (ID = '$ID')";
$name="";
$comments="";
if ($result=$db->query($query)){
if ($db->numrows($result)==1){
$name=$db->result($result,0,"name");
$comments=$db->result($result,0,"comments");
}
}
if ($withcomments)
return array("name"=>$name,"comments"=>$comments);
else return $name;
}
/**
* Get completename of a Dropdown Tree table
*
* @param $table : Dropdown Tree table
* @param $ID : ID of the element
* @param $withcomments : 1 if you want to give the array with the comments
* @return string : completename of the element
* @see getTreeLeafValueName
*/
function getTreeValueCompleteName($table,$ID,$withcomments=0)
{
global $db;
$query = "SELECT *
FROM $table
WHERE (ID = '$ID')";
$name="";
$comments="";
if ($result=$db->query($query)){
if ($db->numrows($result)==1){
$name=$db->result($result,0,"completename");
$comments=$db->result($result,0,"comments");
}
}
if (empty($name)) $name=" ";
if ($withcomments)
return array("name"=>$name,"comments"=>$comments);
else return $name;
}
/**
* show name catgory
*
* @param $table
* @param $ID
* @param $wholename
* @param $level
* @return string name
*/
// DO NOT DELETE THIS FUNCTION : USED IN THE UPDATE
function getTreeValueName($table,$ID, $wholename="",$level=0)
{
global $db,$lang;
$query = "SELECT *
FROM $table
WHERE (ID = '$ID')";
$name="";
if ($result=$db->query($query)){
if ($db->numrows($result)>0){
$row=$db->fetch_array($result);
$parentID = $row["parentID"];
if($wholename == "")
{
$name = $row["name"];
} else
{
$name = $row["name"] . " > ";
}
$level++;
list($tmpname,$level)=getTreeValueName($table,$parentID, $name,$level);
$name = $tmpname. $name;
}
}
return array($name,$level);
}
/**
* Get the equivalent search query using ID that the search of the string argument
*
* @param $table
* @param $search the search string value
* @return string the query
*/
function getRealSearchForTreeItem($table,$search){
return " ( $table.completename ".makeTextSearch($search)." ) ";
/*if (empty($search)) return " ( $table.name LIKE '%$search%' ) ";
global $db;
// IDs to be present in the final query
$id_found=array();
// current ID found to be added
$found=array();
// First request init the varriables
$query="SELECT ID from $table WHERE name LIKE '%$search%'";
if ( ($result=$db->query($query)) && ($db->numrows($result)>0) ){
while ($row=$db->fetch_array($result)){
array_push($id_found,$row['ID']);
array_push($found,$row['ID']);
}
}else return " ( $table.name LIKE '%$search%') ";
// Get the leafs of previous founded item
while (count($found)>0){
// Get next elements
$query="SELECT ID from $table WHERE '0'='1' ";
foreach ($found as $key => $val)
$query.= " OR parentID = '$val' ";
// CLear the found array
unset($found);
$found=array();
$result=$db->query($query);
if ($db->numrows($result)>0){
while ($row=$db->fetch_array($result)){
if (!in_array($row['ID'],$id_found)){
array_push($id_found,$row['ID']);
array_push($found,$row['ID']);
}
}
}
}
// Construct the final request
if (count($id_found)>0){
$ret=" ( '0' = '1' ";
foreach ($id_found as $key => $val)
$ret.=" OR $table.ID = '$val' ";
$ret.=") ";
return $ret;
}else return " ( $table.name LIKE '%$search%') ";
*/
}
/**
* Get the equivalent search query using ID of soons that the search of the father's ID argument
*
* @param $table
* @param $IDf The ID of the father
* @param $reallink real field to link ($table.ID if not set)
* @return string the query
*/
function getRealQueryForTreeItem($table,$IDf,$reallink=""){
global $db;
if (empty($IDf)) return "";
if (empty($reallink)) $reallink=$table.".ID";
// IDs to be present in the final query
$id_found=array();
// current ID found to be added
$found=array();
// First request init the varriables
$query="SELECT ID
FROM $table
WHERE ID = '$IDf'";
if ( ($result=$db->query($query)) && ($db->numrows($result)>0) ){
while ($row=$db->fetch_array($result)){
array_push($id_found,$row['ID']);
array_push($found,$row['ID']);
}
} else return " ( $table.ID = '$IDf') ";
// Get the leafs of previous founded item
while (count($found)>0){
// Get next elements
$query="SELECT ID
FROM $table
WHERE '0'='1' ";
foreach ($found as $key => $val)
$query.= " OR parentID = '$val' ";
// CLear the found array
unset($found);
$found=array();
$result=$db->query($query);
if ($db->numrows($result)>0){
while ($row=$db->fetch_array($result)){
if (!in_array($row['ID'],$id_found)){
array_push($id_found,$row['ID']);
array_push($found,$row['ID']);
}
}
}
}
// Construct the final request
if (count($id_found)>0){
$ret=" ( ";
$i=0;
foreach ($id_found as $key => $val){
if ($i>0) $ret.=" OR ";
$ret.="$reallink = '$val' ";
$i++;
}
$ret.=") ";
return $ret;
}else return " ( $table.ID = '$IDf') ";
}
/**
* Get the level for an item in a tree structure
*
* @param $table
* @param $ID
* @return int level
*/
function getTreeItemLevel($table,$ID){
global $db;
$level=0;
$query="SELECT parentID
FROM $table
WHERE ID='$ID'";
while (1)
{
if (($result=$db->query($query))&&$db->numrows($result)==1){
$parentID=$db->result($result,0,"parentID");
if ($parentID==0) return $level;
else {
$level++;
$query="SELECT parentID
FROM $table
WHERE ID='$parentID'";
}
}
}
return -1;
}
/**
* Compute all completenames of Dropdown Tree table
*
* @param $table : dropdown tree table to compute
* @return nothing
*/
function regenerateTreeCompleteName($table){
global $db;
$query="SELECT ID
FROM $table";
$result=$db->query($query);
if ($db->numrows($result)>0){
while ($data=$db->fetch_array($result)){
list($name,$level)=getTreeValueName($table,$data['ID']);
$query="UPDATE $table
SET completename='".addslashes($name)."', level='$level'
WHERE ID='".$data['ID']."'";
$db->query($query);
}
}
}
/**
* Compute completename of Dropdown Tree table under the element of ID $ID
*
* @param $table : dropdown tree table to compute
* @param $ID : root ID to used : regenerate all under this element
* @return nothing
*/
function regenerateTreeCompleteNameUnderID($table,$ID){
global $db;
list($name,$level)=getTreeValueName($table,$ID);
$query="UPDATE $table
SET completename='".addslashes($name)."', level='$level'
WHERE ID='".$ID."'";
$db->query($query);
$query="SELECT ID
FROM $table
WHERE parentID='$ID'";
$result=$db->query($query);
if ($db->numrows($result)>0){
while ($data=$db->fetch_array($result)){
regenerateTreeCompleteNameUnderID($table,$data["ID"]);
}
}
}
/**
* Get the ID of the next Item
*
* @param $table table to search next item
* @param $ID current ID
* @return the next ID, -1 if not exist
*/
function getNextItem($table,$ID){
global $db,$cfg_glpi;
$nextprev_item=$cfg_glpi["nextprev_item"];
if ($table=="glpi_tracking"||ereg("glpi_device",$table)) $nextprev_item="ID";
$search=$ID;
if ($nextprev_item!="ID"){
$query="SELECT ".$nextprev_item."
FROM $table
WHERE ID='$ID'";
$result=$db->query($query);
$search=addslashes($db->result($result,0,0));
}
$query = "SELECT ID
FROM $table
WHERE ".$nextprev_item." > '$search' ";
if (in_array($table,$cfg_glpi["deleted_tables"]))
$query.="AND deleted='N'";
if (in_array($table,$cfg_glpi["template_tables"]))
$query.="AND is_template='0'";
$query.=" ORDER BY ".$nextprev_item." ASC";
$result=$db->query($query);
if ($db->numrows($result)>0)
return $db->result($result,0,"ID");
else return -1;
}
/**
* Get the ID of the previous Item
*
* @param $table table to search next item
* @param $ID current ID
* @return the previous ID, -1 if not exist
*/
function getPreviousItem($table,$ID){
global $db,$cfg_glpi;
$nextprev_item=$cfg_glpi["nextprev_item"];
if ($table=="glpi_tracking"||ereg("glpi_device",$table)) $nextprev_item="ID";
$search=$ID;
if ($nextprev_item!="ID"){
$query="SELECT ".$nextprev_item."
FROM $table
WHERE ID=$ID";
$result=$db->query($query);
$search=addslashes($db->result($result,0,0));
}
$query = "SELECT ID
FROM $table
WHERE ".$nextprev_item." < '$search' ";
if (in_array($table,$cfg_glpi["deleted_tables"]))
$query.="AND deleted='N'";
if (in_array($table,$cfg_glpi["template_tables"]))
$query.="AND is_template='0'";
$query.=" ORDER BY ".$nextprev_item." DESC";
$result=$db->query($query);
if ($db->numrows($result)>0)
return $db->result($result,0,"ID");
else return -1;
}
/**
* Get name of the user with ID=$ID (optional with link to user.info.php)
*
*@param $ID int : ID of the user.
*@param $link int : 1 = Show link to user.info.php 2 = return array with comments and link
*
*@return string : username string (realname if not empty and name if realname is empty).
*
**/
function getUserName($ID,$link=0){
global $db,$cfg_glpi,$lang;
$user="";
if ($link==2){
$user=array("name"=>"","link"=>"","comments"=>"");
}
if ($ID){
$query="SELECT *
FROM glpi_users
WHERE ID='$ID'";
$result=$db->query($query);
if ($link==2) $user=array("name"=>"","comments"=>"","link"=>"");
if ($db->numrows($result)==1){
$data=$db->fetch_assoc($result);
$before="";
$after="";
if ($link==1){
$before="<a href=\"".$cfg_glpi["root_doc"]."/front/user.info.php?ID=".$ID."\">";
$after="</a>";
}
if (strlen($data["realname"])>0) {
$temp=$data["realname"];
if (strlen($data["firstname"])>0)$temp.=" ".$data["firstname"];
$username=$before.$temp.$after;
}
else $username=$before.$data["name"].$after;
if ($link==2){
$user["name"]=$username;
$user["link"]=$cfg_glpi["root_doc"]."/front/user.info.php?ID=".$ID;
$user["comments"]=$lang["common"][16].": ".$username."<br>";
if (!empty($data["email"]))
$user["comments"].=$lang["setup"][14].": ".$data["email"]."<br>";
if (!empty($data["phone"]))
$user["comments"].=$lang["setup"][15].": ".$data["phone"]."<br>";
if ($data["location"])
$user["comments"].=$lang["common"][15].": ".getDropdownName("glpi_dropdown_locations",$data["location"],0)."<br>";
} else $user=$username;
}
}
return $user;
}
/**
* Verify if a DB table exists
*
*@param $tablename string : Name of the table we want to verify.
*
*@return bool : true if exists, false elseway.
*
**/
function TableExists($tablename) {
global $db;
// Get a list of tables contained within the database.
$result = $db->list_tables($db);
$rcount = $db->numrows($result);
// Check each in list for a match.
for ($i=0;$i<$rcount;$i++) {
if ($db->table_name($result, $i)==$tablename) return true;
}
$db->free_result($result);
return false;
}
/**
* Verify if a DB field exists
*
*@param $table string : Name of the table we want to verify.
*@param $field string : Name of the field we want to verify.
*
*@return bool : true if exists, false elseway.
*
**/
function FieldExists($table, $field) {
global $db;
if ($fields = $db->list_fields($table)){
if (isset($fields[$field]))
return true;
else return false;
} else return false;
}
// return true if the field $field of the table $table is a mysql index
// else return false
function isIndex($table, $field) {
global $db;
$result = $db->query("SHOW INDEX FROM ". $table);
if ($result&&$db->numrows($result)){
while ($data=$db->fetch_assoc($result))
if ($data["Key_name"]==$field){
// echo $table.".".$field."-> INDEX<br>";
return true;
}
}
//echo $table.".".$field."-> NOT INDEX<br>";
return false;
}
function exportArrayToDB($TAB) {
$EXPORT = "";
while (list($KEY,$VALUE) = each($TAB)) {
$EXPORT .= urlencode($KEY)."=>".urlencode($VALUE)." ";
}
return $EXPORT;
}
function importArrayFromDB($DATA) {
$TAB = array();
foreach(explode(" ", $DATA) as $ITEM) {
$A = explode("=>", $ITEM);
if (strlen($A[0])&&isset($A[1]))
$TAB[urldecode($A[0])] = urldecode($A[1]);
}
return $TAB;
}
//***************************************************************
// Cration automatique d'un nouveau code partir du gabarit
// @object : objet concern
// @field : nom de champ du gabarit contenant le format du code
// @isTemplate : true si template new
// @type : type d'objet
function autoName($objectName, $field, $isTemplate, $type){
global $LINK_ID_TABLE,$db;
//$objectName = isset($object->fields[$field]) ? $object->fields[$field] : '';
$len = strlen($objectName);
if($isTemplate && $len > 8 && substr($objectName,0,4) === '<' && substr($objectName,$len - 4,4) === '>') {
$autoNum = substr($objectName, 4, $len - 8);
$mask = '';
if(preg_match( "/\\#{1,10}/", $autoNum, $mask)){
$global = strpos($autoNum, '\\g') !== false && $type != INFOCOM_TYPE ? 1 : 0;
$autoNum = str_replace(array('\\y','\\Y','\\m','\\d','_','%','\\g'), array(date('y'),date('Y'),date('m'),date('d'),'\\_','\\%',''), $autoNum);
$mask = $mask[0];
$pos = strpos($autoNum, $mask) + 1;
$len = strlen($mask);
$like = str_replace('#', '_', $autoNum);
if ($global == 1){
$query = "";
$first = 1;
foreach($LINK_ID_TABLE as $t=>$table){
if ($t == COMPUTER_TYPE || $t == MONITOR_TYPE || $t == NETWORKING_TYPE || $t == PERIPHERAL_TYPE || $t == PRINTER_TYPE || $t == PHONE_TYPE){
$query .= ($first ? "SELECT " : " UNION SELECT ")." $field AS code
FROM $table
WHERE $field LIKE '$like'
AND deleted = 'N'
AND is_template = '0'";
$first = 0;
}
}
$query = "SELECT CAST(SUBSTRING(code, $pos, $len) AS unsigned) AS no
FROM ($query) AS codes";
} else {
$table = $LINK_ID_TABLE[$type];
$query = "SELECT CAST(SUBSTRING($field, $pos, $len) AS unsigned) AS no
FROM $table
WHERE $field LIKE '$like' ";
if ($type != INFOCOM_TYPE)
$query .= " AND deleted = 'N' AND is_template = '0'";
}
$query = "SELECT MAX(Num.no) AS lastNo
FROM (".$query.") AS Num";
$resultNo = $db->query($query);
if ($db->numrows($resultNo)>0) {
$data = $db->fetch_array($resultNo);
$newNo = $data['lastNo'] + 1;
} else $newNo = 0;
$objectName = str_replace(array($mask,'\\_','\\%'), array(str_pad($newNo, $len, '0', STR_PAD_LEFT),'_','%'), $autoNum);
}
}
return $objectName;
}
?>
|