grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026. -------------------------------------------------------------------------- */ /** @file * @brief */ if (!defined('GLPI_ROOT')) { die("Sorry. You can't access this file directly"); } /** * Ajax Class **/ class Ajax { /** * Create modal window * After display it using $name.dialog("open"); * * @since version 0.84 * * @param $name name of the js object * @param $url URL to display in modal * @param $options array of possible options: * - width (default 800) * - height (default 400) * - modal is a modal window ? (default true) * - container specify a html element to render (default empty to html.body) * - title window title (default empty) * - display display or get string ? (default true) **/ static function createModalWindow($name, $url, $options=array() ) { $param = array('width' => 800, 'height' => 400, 'modal' => true, 'container' => '', 'title' => '', 'extraparams' => array(), 'display' => true, 'js_modal_fields' => ''); if (count($options)) { foreach ($options as $key => $val) { if (isset($param[$key])) { $param[$key] = $val; } } } $out = "\n"; if ($param['display']) { echo $out; } else { return $out; } } /** * Create fixed modal window * After display it using $name.dialog("open"); * * @since version 0.84 * * @param $name name of the js object * @param $options array of possible options: * - width (default 800) * - height (default 400) * - modal is a modal window ? (default true) * - container specify a html element to render (default empty to html.body) * - title window title (default empty) * - display display or get string ? (default true) **/ static function createFixedModalWindow($name, $options=array() ) { $param = array('width' => 800, 'height' => 400, 'modal' => true, 'container' => '', 'title' => '', 'display' => true); if (count($options)) { foreach ($options as $key => $val) { if (isset($param[$key])) { $param[$key] = $val; } } } $out = ""; if ($param['display']) { echo $out; } else { return $out; } } /** * Create modal window in Iframe * After display it using Html::jsGetElementbyID($domid).dialog("open"); * * @since version 0.85 * * @param $domid DOM ID of the js object * @param $url URL to display in modal * @param $options array of possible options: * - width (default 800) * - height (default 400) * - modal is a modal window ? (default true) * - title window title (default empty) * - display display or get string ? (default true) * - reloadonclose reload main page on close ? (default false) **/ static function createIframeModalWindow($domid, $url, $options=array() ) { $param = array('width' => 1050, 'height' => 500, 'modal' => true, 'title' => '', 'display' => true, 'reloadonclose' => false); if (count($options)) { foreach ($options as $key => $val) { if (isset($param[$key])) { $param[$key] = $val; } } } $url .= (strstr($url,'?') ?'&' : '?').'_in_modal=1'; $out = "
"; $out .= "
"; $out .= ""; if ($param['display']) { echo $out; } else { return $out; } } /** * Input text used as search system in ajax system * * @param $id ID of the ajax item * @param $size size of the input text field (default 4) * @deprecated since version 0.85 **/ static function displaySearchTextForDropdown($id, $size=4) { echo self::getSearchTextForDropdown($id, $size); } /** * Input text used as search system in ajax system * @since version 0.84 * * @param $id ID of the ajax item * @param $size size of the input text field (default 4) * @deprecated since version 0.85 **/ static function getSearchTextForDropdown($id, $size=4) { global $CFG_GLPI; //TRANS: %s is the character used as wildcard in ajax search // return "\n"; return "\n"; } /** * Create Ajax Tabs apply to 'tabspanel' div. Content is displayed in 'tabcontent' * * @param $tabdiv_id ID of the div containing the tabs (default 'tabspanel') * @param $tabdivcontent_id ID of the div containing the content loaded by tabs * (default 'tabcontent') * @param $tabs array of tabs to create : * tabs is array('key' => array('title'=> 'x', * url => 'url_toload', * params => 'url_params')... * @param $type itemtype for active tab * @param $ID ID of element for active tab (default 0) * @param $orientation orientation of tabs (default vertical may also be horizontal) * * @return nothing **/ static function createTabs($tabdiv_id='tabspanel', $tabdivcontent_id='tabcontent', $tabs=array(), $type, $ID=0, $orientation='vertical') { global $CFG_GLPI; /// TODO need to clean params !! $active_tabs = Session::getActiveTab($type); $rand = mt_rand(); if (count($tabs) > 0) { echo "
"; if (CommonGLPI::isLayoutWithMain() && !CommonGLPI::isLayoutExcludedPage()) { $orientation = 'horizontal'; } echo ""; echo "
"; echo ""; $js = " forceReload$rand = false; $('#tabs$rand').tabs({ active: $selected_tab, // Loading indicator beforeLoad: function (event, ui) { if ($(ui.panel).html() && !forceReload$rand) { event.preventDefault(); } else { ui.panel.html($('#loadingtabs$rand').html()); forceReload$rand = false; } var newIndex = ui.tab.parent().children().index(ui.tab); $.get('".$CFG_GLPI['root_doc']."/ajax/updatecurrenttab.php', { itemtype: '$type', id: '$ID', tab: newIndex }); }, ajaxOptions: {type: 'POST'} });"; if ($orientation=='vertical') { $js .= "$('#tabs$rand').tabs().addClass( 'ui-tabs-vertical ui-helper-clearfix' );"; } if (CommonGLPI::isLayoutWithMain() && !CommonGLPI::isLayoutExcludedPage()) { $js .= "$('#tabs$rand').scrollabletabs();"; } else { $js .= "$('#tabs$rand').removeClass( 'ui-corner-top' ).addClass( 'ui-corner-left' );"; } $js .= "// force reload function reloadTab(add) { forceReload$rand = true; var current_index = $('#tabs$rand').tabs('option','active'); // Save tab currenthref = $('#tabs$rand ul>li a').eq(current_index).attr('href'); $('#tabs$rand ul>li a').eq(current_index).attr('href',currenthref+'&'+add); $('#tabs$rand').tabs( 'load' , current_index); // Restore tab $('#tabs$rand ul>li a').eq(current_index).attr('href',currenthref); };"; echo Html::scriptBlock($js); } } /** * Javascript code for update an item when another item changed * * @param $toobserve id (or array of id) of the select to observe * @param $toupdate id of the item to update * @param $url Url to get datas to update the item * @param $parameters array of parameters to send to ajax URL * @param $events array of the observed events (default 'change') * @param $minsize minimum size of data to update content (default -1) * @param $buffertime minimum time to wait before reload (default -1) * @param $forceloadfor array of content which must force update content * @param $display boolean display or get string (default true) **/ static function updateItemOnEvent($toobserve, $toupdate, $url, $parameters=array(), $events=array("change"), $minsize=-1, $buffertime=-1, $forceloadfor=array(), $display=true) { $output = ""; if ($display) { echo $output; } else { return $output; } } /** * Javascript code for update an item when a select item changed * * @param $toobserve id of the select to observe * @param $toupdate id of the item to update * @param $url Url to get datas to update the item * @param $parameters array of parameters to send to ajax URL * @param $display boolean display or get string (default true) **/ static function updateItemOnSelectEvent($toobserve, $toupdate, $url, $parameters=array(), $display=true) { return self::updateItemOnEvent($toobserve, $toupdate, $url, $parameters, array("change"), -1, -1, array(), $display); } /** * Javascript code for update an item when a Input text item changed * * @param $toobserve id of the Input text to observe * @param $toupdate id of the item to update * @param $url Url to get datas to update the item * @param $parameters array of parameters to send to ajax URL * @param $minsize minimum size of data to update content (default -1) * @param $buffertime minimum time to wait before reload (default -1) * @param $forceloadfor array of content which must force update content * @param $display boolean display or get string (default true) * **/ static function updateItemOnInputTextEvent($toobserve, $toupdate, $url, $parameters=array(), $minsize=-1, $buffertime=-1, $forceloadfor=array(), $display=true) { global $CFG_GLPI; if (count($forceloadfor) == 0) { // $forceloadfor = array($CFG_GLPI['ajax_wildcard']); $forceloadfor = array('*'); } // Need to define min size for text search if ($minsize < 0) { // $minsize = $CFG_GLPI['ajax_min_textsearch_load']; $minsize = 0; } if ($buffertime < 0) { $buffertime = 0; // $buffertime = $CFG_GLPI['ajax_buffertime_load']; } return self::updateItemOnEvent($toobserve, $toupdate, $url, $parameters, array("dblclick", "keyup"), $minsize, $buffertime, $forceloadfor, $display); } /** * Javascript code for update an item when another item changed (Javascript code only) * * @param $toobserve id (or array of id) of the select to observe * @param $toupdate id of the item to update * @param $url Url to get datas to update the item * @param $parameters array of parameters to send to ajax URL * @param $events array of the observed events (default 'change') * @param $minsize minimum size of data to update content (default -1) * @param $buffertime minimum time to wait before reload (default -1) * @param $forceloadfor array of content which must force update content * @param $display boolean display or get string (default true) **/ static function updateItemOnEventJsCode($toobserve, $toupdate, $url, $parameters=array(), $events=array("change"), $minsize = -1, $buffertime=-1, $forceloadfor=array(), $display=true) { if (is_array($toobserve)) { $zones = $toobserve; } else { $zones = array($toobserve); } $output = ''; foreach ($zones as $zone) { foreach ($events as $event) { if ($buffertime > 0) { $output .= "var last$zone$event = 0;"; } $output .= Html::jsGetElementbyID(Html::cleanId($zone)).".on( '$event', function(event) {"; /// TODO manage buffer time !! ? if ($buffertime > 0) { // $output.= "var elapsed = new Date().getTime() - last$zone$event; // last$zone$event = new Date().getTime(); // if (elapsed < $buffertime) { // return; // }"; } $condition = ''; if ($minsize >= 0) { $condition = Html::jsGetElementbyID(Html::cleanId($zone)).".val().length >= $minsize "; } if (count($forceloadfor)) { foreach ($forceloadfor as $value) { if (!empty($condition)) { $condition .= " || "; } $condition .= Html::jsGetElementbyID(Html::cleanId($zone)).".val() == '$value'"; } } if (!empty($condition)) { $output .= "if ($condition) {"; } $output .= self::updateItemJsCode($toupdate, $url, $parameters, $toobserve, false); if (!empty($condition)) { $output .= "}"; } $output .= "}"; $output .=");\n"; } } if ($display) { echo $output; } else { return $output; } } /** * Javascript code for update an item (Javascript code only) * * @param $options array of options * - toupdate : array / Update a specific item on select change on dropdown * (need value_fieldname, to_update, * url (@see Ajax::updateItemOnSelectEvent for information) * and may have moreparams) * @param $display boolean display or get string (default true) **/ static function commonDropdownUpdateItem($options, $display=true) { $field = ''; $fieldname = ''; $output = ''; // Old scheme if (isset($options["update_item"]) && (is_array($options["update_item"]) || (strlen($options["update_item"]) > 0))) { $field = "update_item"; $fieldname = 'myname'; } // New scheme if (isset($options["toupdate"]) && (is_array($options["toupdate"]) || (strlen($options["toupdate"]) > 0))) { $field = "toupdate"; $fieldname = 'name'; } if (!empty($field)) { $datas = $options[$field]; if (is_array($datas) && count($datas)) { // Put it in array if (isset($datas['to_update'])) { $datas = array($datas); } foreach ($datas as $data) { $paramsupdate = array(); if (isset($data['value_fieldname'])) { $paramsupdate = array($data['value_fieldname'] => '__VALUE__'); } if (isset($data["moreparams"]) && is_array($data["moreparams"]) && count($data["moreparams"])) { foreach ($data["moreparams"] as $key => $val) { $paramsupdate[$key] = $val; } } $output .= self::updateItemOnSelectEvent("dropdown_".$options["name"].$options["rand"], $data['to_update'], $data['url'], $paramsupdate, $display); } } } if ($display) { echo $output; } else { return $output; } } /** * Javascript code for update an item (Javascript code only) * * @param $toupdate id of the item to update * @param $url Url to get datas to update the item * @param $parameters array of parameters to send to ajax URL * @param $toobserve id of another item used to get value in case of __VALUE__ used * or * array of id to get value in case of __VALUE#__ used (default '') * @param $display boolean display or get string (default true) **/ static function updateItemJsCode($toupdate, $url, $parameters=array(), $toobserve="", $display=true) { $out = Html::jsGetElementbyID($toupdate).".load('$url'\n"; if (count($parameters)) { $out .= ",{"; $first = true; foreach ($parameters as $key => $val) { if ($first) { $first = false; } else { $out .= ","; } $out .= $key.":"; if (!is_array($val) && preg_match('/^__VALUE(\d+)__$/',$val,$regs)) { $out .= Html::jsGetElementbyID(Html::cleanId($toobserve[$regs[1]])).".val()"; } else if (!is_array($val) && $val==="__VALUE__") { $out .= Html::jsGetElementbyID(Html::cleanId($toobserve)).".val()"; } else { $out .= json_encode($val); } } $out .= "}\n"; } $out.= ")\n"; if ($display) { echo $out; } else { return $out; } } /** * Javascript code for update an item * * @param $toupdate id of the item to update * @param $url Url to get datas to update the item * @param $parameters array of parameters to send to ajax URL * @param $toobserve id of another item used to get value in case of __VALUE__ used * (default '') * @param $display boolean display or get string (default true) * **/ static function updateItem($toupdate, $url, $parameters=array(), $toobserve="", $display=true) { $output = ""; if ($display) { echo $output; } else { return $output; } } } ?>