/** * Format XML, ie indent it for pretty printing * * @param $xml simplexml object * @return string **/ static function formatXML($xml) { $string = str_replace("><", ">\n<", $xml->asXML()); $token = strtok($string, "\n"); $result = ''; $pad = 0; $matches = array(); $indent = 0; while ($token !== FALSE) { // 1. open and closing tags on same line - no change if (preg_match('/.+<\\/\\w[^>]*>$/', $token, $matches)) { $indent = 0; // 2. closing tag - outdent now } else { if (preg_match('/^<\\/\\w/', $token, $matches)) { $pad = $pad - 3; // 3. opening tag - don't pad this one, only subsequent tags } else { if (preg_match('/^<\\w[^>]*[^\\/]>.*$/', $token, $matches)) { $indent = 3; } else { $indent = 0; } } } $line = Toolbox::str_pad($token, strlen($token) + $pad, ' ', STR_PAD_LEFT); $result .= $line . "\n"; $token = strtok("\n"); $pad += $indent; $indent = 0; } return $result; }
/** * Display a simple progress bar * * @param $width Width of the progress bar * @param $percent Percent of the progress bar * @param $options array of possible options: * - title : string title to display (default Progesssion) * - simple : display a simple progress bar (no title / only percent) * - forcepadding : boolean force str_pad to force refresh (default true) * * @return nothing **/ static function displayProgressBar($width, $percent, $options = array()) { global $CFG_GLPI; $param['title'] = __('Progress'); $param['simple'] = false; $param['forcepadding'] = true; if (is_array($options) && count($options)) { foreach ($options as $key => $val) { $param[$key] = $val; } } $percentwidth = floor($percent * $width / 100); $output = "<div class='center'><table class='tab_cadre' width='" . ($width + 20) . "px'>"; if (!$param['simple']) { $output .= "<tr><th class='center'>" . $param['title'] . " " . $percent . "%</th></tr>"; } $output .= "<tr><td>\n <table class='tabcompact'><tr><td class='center' style='background:url(" . $CFG_GLPI["root_doc"] . "/pics/loader.png) repeat-x; padding: 0px;font-size: 10px;' width='" . $percentwidth . " px' height='12'>"; if ($param['simple']) { $output .= $percent . "%"; } else { $output .= ' '; } $output .= "</td></tr></table></td>"; $output .= "</tr></table>"; $output .= "</div>"; if (!$param['forcepadding']) { echo $output; } else { echo Toolbox::str_pad($output, 4096); self::glpi_flush(); } }
/** * Create a new name using a autoname field defined in a template * * @param $objectName autoname template * @param $field field to autoname * @param $isTemplate true if create an object from a template * @param $itemtype item type * @param $entities_id limit generation to an entity (default -1) * * @return new auto string **/ function autoName($objectName, $field, $isTemplate, $itemtype, $entities_id = -1) { global $DB, $CFG_GLPI; $len = Toolbox::strlen($objectName); if ($isTemplate && $len > 8 && Toolbox::substr($objectName, 0, 4) === '<' && Toolbox::substr($objectName, $len - 4, 4) === '>') { $autoNum = Toolbox::substr($objectName, 4, $len - 8); $mask = ''; if (preg_match("/\\#{1,10}/", $autoNum, $mask)) { $global = strpos($autoNum, '\\g') !== false && $itemtype != 'Infocom' ? 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 = Toolbox::strlen($mask); $like = str_replace('#', '_', $autoNum); if ($global == 1) { $query = ""; $first = 1; $types = array('Computer', 'Monitor', 'NetworkEquipment', 'Peripheral', 'Phone', 'Printer'); foreach ($types as $t) { $table = getTableForItemType($t); $query .= ($first ? "SELECT " : " UNION SELECT ") . " {$field} AS code\n FROM `{$table}`\n WHERE `{$field}` LIKE '{$like}'\n AND `is_deleted` = '0'\n AND `is_template` = '0'"; if ($CFG_GLPI["use_autoname_by_entity"] && $entities_id >= 0) { $query .= " AND `entities_id` = '{$entities_id}' "; } $first = 0; } $query = "SELECT CAST(SUBSTRING(code, {$pos}, {$len}) AS unsigned) AS no\n FROM ({$query}) AS codes"; } else { $table = getTableForItemType($itemtype); $query = "SELECT CAST(SUBSTRING({$field}, {$pos}, {$len}) AS unsigned) AS no\n FROM `{$table}`\n WHERE `{$field}` LIKE '{$like}' "; if ($itemtype != 'Infocom') { $query .= " AND `is_deleted` = '0'\n AND `is_template` = '0'"; if ($CFG_GLPI["use_autoname_by_entity"] && $entities_id >= 0) { $query .= " AND `entities_id` = '{$entities_id}' "; } } } $query = "SELECT MAX(Num.no) AS lastNo\n FROM (" . $query . ") AS Num"; $resultNo = $DB->query($query); if ($DB->numrows($resultNo) > 0) { $data = $DB->fetch_assoc($resultNo); $newNo = $data['lastNo'] + 1; } else { $newNo = 0; } $objectName = str_replace(array($mask, '\\_', '\\%'), array(Toolbox::str_pad($newNo, $len, '0', STR_PAD_LEFT), '_', '%'), $autoNum); } } return $objectName; }